test_db.cpp 17.0 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "db/utils.h"
G
groot 已提交
19
#include "db/DB.h"
X
Xu Peng 已提交
20
#include "db/DBImpl.h"
S
starlord 已提交
21
#include "db/Constants.h"
S
starlord 已提交
22
#include "db/meta/MetaConsts.h"
S
starlord 已提交
23
#include "db/DBFactory.h"
Y
Yu Kun 已提交
24
#include "cache/CpuCacheMgr.h"
25
#include "utils/CommonUtil.h"
26
#include "server/Config.h"
G
groot 已提交
27

J
jinhai 已提交
28 29 30 31 32
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#include <thread>
#include <random>

33

G
groot 已提交
34 35
namespace {

36 37
static const char *CONFIG_FILE_PATH = "./milvus/conf/server_config.yaml";

S
starlord 已提交
38 39 40 41 42 43 44
static const char *TABLE_NAME = "test_group";
static constexpr int64_t TABLE_DIM = 256;
static constexpr int64_t VECTOR_COUNT = 25000;
static constexpr int64_t INSERT_LOOP = 1000;
static constexpr int64_t SECONDS_EACH_HOUR = 3600;
static constexpr int64_t DAY_SECONDS = 24 * 60 * 60;

S
starlord 已提交
45
milvus::engine::meta::TableSchema
S
starlord 已提交
46
BuildTableSchema() {
S
starlord 已提交
47
    milvus::engine::meta::TableSchema table_info;
S
starlord 已提交
48 49 50 51
    table_info.dimension_ = TABLE_DIM;
    table_info.table_id_ = TABLE_NAME;
    return table_info;
}
G
groot 已提交
52

S
starlord 已提交
53 54 55 56 57 58 59 60
void
BuildVectors(int64_t n, std::vector<float> &vectors) {
    vectors.clear();
    vectors.resize(n * TABLE_DIM);
    float *data = vectors.data();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < TABLE_DIM; j++) data[TABLE_DIM * i + j] = drand48();
        data[TABLE_DIM * i] += i / 2000.;
G
groot 已提交
61
    }
S
starlord 已提交
62
}
G
groot 已提交
63

S
starlord 已提交
64 65 66 67 68 69 70 71
std::string
CurrentTmDate(int64_t offset_day = 0) {
    time_t tt;
    time(&tt);
    tt = tt + 8 * SECONDS_EACH_HOUR;
    tt = tt + 24 * SECONDS_EACH_HOUR * offset_day;
    tm t;
    gmtime_r(&tt, &t);
72

S
starlord 已提交
73 74
    std::string str = std::to_string(t.tm_year + 1900) + "-" + std::to_string(t.tm_mon + 1)
        + "-" + std::to_string(t.tm_mday);
75

S
starlord 已提交
76 77
    return str;
}
78

S
starlord 已提交
79 80 81
void
ConvertTimeRangeToDBDates(const std::string &start_value,
                          const std::string &end_value,
S
starlord 已提交
82
                          std::vector<milvus::engine::meta::DateT> &dates) {
S
starlord 已提交
83
    dates.clear();
84

S
starlord 已提交
85 86
    time_t tt_start, tt_end;
    tm tm_start, tm_end;
S
starlord 已提交
87
    if (!milvus::server::CommonUtil::TimeStrToTime(start_value, tt_start, tm_start)) {
S
starlord 已提交
88 89
        return;
    }
90

S
starlord 已提交
91
    if (!milvus::server::CommonUtil::TimeStrToTime(end_value, tt_end, tm_end)) {
S
starlord 已提交
92 93
        return;
    }
94

S
starlord 已提交
95 96 97 98 99
    int64_t days = (tt_end > tt_start) ? (tt_end - tt_start) / DAY_SECONDS : (tt_start - tt_end) /
        DAY_SECONDS;
    if (days == 0) {
        return;
    }
100

S
starlord 已提交
101 102 103
    for (int64_t i = 0; i < days; i++) {
        time_t tt_day = tt_start + DAY_SECONDS * i;
        tm tm_day;
S
starlord 已提交
104
        milvus::server::CommonUtil::ConvertTime(tt_day, tm_day);
105

S
starlord 已提交
106 107 108
        int64_t date = tm_day.tm_year * 10000 + tm_day.tm_mon * 100 +
            tm_day.tm_mday;//according to db logic
        dates.push_back(date);
109
    }
G
groot 已提交
110 111
}

S
starlord 已提交
112 113
} // namespace

X
Xu Peng 已提交
114 115
TEST_F(DBTest, CONFIG_TEST) {
    {
S
starlord 已提交
116
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf("wrong"));
117
        /* EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); */
X
Xu Peng 已提交
118 119
    }
    {
S
starlord 已提交
120
        milvus::engine::ArchiveConf conf("delete");
X
Xu Peng 已提交
121 122
        ASSERT_EQ(conf.GetType(), "delete");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
123
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
124 125
    }
    {
S
starlord 已提交
126
        milvus::engine::ArchiveConf conf("swap");
X
Xu Peng 已提交
127 128
        ASSERT_EQ(conf.GetType(), "swap");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
129
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
130 131
    }
    {
S
starlord 已提交
132 133 134
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "disk:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "disk:a"));
        milvus::engine::ArchiveConf conf("swap", "disk:1024");
X
Xu Peng 已提交
135
        auto criterias = conf.GetCriterias();
S
starlord 已提交
136 137
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["disk"], 1024);
X
Xu Peng 已提交
138 139
    }
    {
S
starlord 已提交
140 141 142
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "days:a"));
        milvus::engine::ArchiveConf conf("swap", "days:100");
X
Xu Peng 已提交
143
        auto criterias = conf.GetCriterias();
S
starlord 已提交
144 145
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["days"], 100);
X
Xu Peng 已提交
146 147
    }
    {
S
starlord 已提交
148 149 150
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "days:a"));
        milvus::engine::ArchiveConf conf("swap", "days:100;disk:200");
X
Xu Peng 已提交
151
        auto criterias = conf.GetCriterias();
S
starlord 已提交
152 153 154
        ASSERT_EQ(criterias.size(), 2);
        ASSERT_EQ(criterias["days"], 100);
        ASSERT_EQ(criterias["disk"], 200);
X
Xu Peng 已提交
155 156 157
    }
}

158
TEST_F(DBTest, DB_TEST) {
S
starlord 已提交
159
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
160
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
161

S
starlord 已提交
162
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
163
    table_info_get.table_id_ = TABLE_NAME;
G
groot 已提交
164
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
165
    ASSERT_TRUE(stat.ok());
G
groot 已提交
166
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
G
groot 已提交
167

S
starlord 已提交
168 169
    milvus::engine::IDNumbers vector_ids;
    milvus::engine::IDNumbers target_ids;
X
Xu Peng 已提交
170

G
groot 已提交
171 172 173
    int64_t nb = 50;
    std::vector<float> xb;
    BuildVectors(nb, xb);
G
groot 已提交
174

G
groot 已提交
175 176 177
    int64_t qb = 5;
    std::vector<float> qxb;
    BuildVectors(qb, qxb);
X
Xu Peng 已提交
178

X
Xu Peng 已提交
179
    std::thread search([&]() {
S
starlord 已提交
180
        milvus::engine::QueryResults results;
X
Xu Peng 已提交
181 182 183 184 185
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

        INIT_TIMER;
        std::stringstream ss;
G
groot 已提交
186 187
        uint64_t count = 0;
        uint64_t prev_count = 0;
X
Xu Peng 已提交
188

S
starlord 已提交
189
        for (auto j = 0; j < 10; ++j) {
X
Xu Peng 已提交
190
            ss.str("");
X
Xu Peng 已提交
191
            db_->Size(count);
X
Xu Peng 已提交
192
            prev_count = count;
X
Xu Peng 已提交
193 194

            START_TIMER;
Y
Yu Kun 已提交
195
            stat = db_->Query(TABLE_NAME, k, qb, 10, qxb.data(), results);
S
starlord 已提交
196
            ss << "Search " << j << " With Size " << count / milvus::engine::M << " M";
X
Xu Peng 已提交
197 198
            STOP_TIMER(ss.str());

S
starlord 已提交
199
            ASSERT_TRUE(stat.ok());
S
starlord 已提交
200
            for (auto k = 0; k < qb; ++k) {
G
groot 已提交
201
                ASSERT_EQ(results[k][0].first, target_ids[k]);
X
Xu Peng 已提交
202 203 204
                ss.str("");
                ss << "Result [" << k << "]:";
                for (auto result : results[k]) {
G
groot 已提交
205
                    ss << result.first << " ";
X
Xu Peng 已提交
206
                }
207
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
208
            }
X
Xu Peng 已提交
209
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
210 211 212 213
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

G
groot 已提交
214
    int loop = INSERT_LOOP;
X
Xu Peng 已提交
215

S
starlord 已提交
216 217
    for (auto i = 0; i < loop; ++i) {
        if (i == 40) {
G
groot 已提交
218
            db_->InsertVectors(TABLE_NAME, qb, qxb.data(), target_ids);
219
            ASSERT_EQ(target_ids.size(), qb);
X
Xu Peng 已提交
220
        } else {
G
groot 已提交
221
            db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
X
Xu Peng 已提交
222
        }
X
Xu Peng 已提交
223
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
224
    }
X
xj.lin 已提交
225

X
Xu Peng 已提交
226
    search.join();
S
starlord 已提交
227 228 229

    uint64_t count;
    stat = db_->GetTableRowCount(TABLE_NAME, count);
S
starlord 已提交
230
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
231 232
    ASSERT_GT(count, 0);
}
X
xj.lin 已提交
233

234
TEST_F(DBTest, SEARCH_TEST) {
235 236 237
    milvus::server::Config &config = milvus::server::Config::GetInstance();
    milvus::Status s = config.LoadConfigFile(CONFIG_FILE_PATH);

S
starlord 已提交
238
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
239
    auto stat = db_->CreateTable(table_info);
X
xj.lin 已提交
240

S
starlord 已提交
241
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
242
    table_info_get.table_id_ = TABLE_NAME;
G
groot 已提交
243
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
244
    ASSERT_TRUE(stat.ok());
G
groot 已提交
245
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
X
xj.lin 已提交
246 247

    // prepare raw data
G
groot 已提交
248
    size_t nb = VECTOR_COUNT;
X
xj.lin 已提交
249 250
    size_t nq = 10;
    size_t k = 5;
S
starlord 已提交
251 252 253
    std::vector<float> xb(nb * TABLE_DIM);
    std::vector<float> xq(nq * TABLE_DIM);
    std::vector<int64_t> ids(nb);
X
xj.lin 已提交
254 255 256 257

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis_xt(-1.0, 1.0);
S
starlord 已提交
258
    for (size_t i = 0; i < nb * TABLE_DIM; i++) {
X
xj.lin 已提交
259
        xb[i] = dis_xt(gen);
S
starlord 已提交
260
        if (i < nb) {
X
xj.lin 已提交
261 262 263
            ids[i] = i;
        }
    }
S
starlord 已提交
264
    for (size_t i = 0; i < nq * TABLE_DIM; i++) {
X
xj.lin 已提交
265 266 267 268 269
        xq[i] = dis_xt(gen);
    }

    // result data
    //std::vector<long> nns_gt(k*nq);
S
starlord 已提交
270
    std::vector<int64_t> nns(k * nq);  // nns = nearst neg search
X
xj.lin 已提交
271
    //std::vector<float> dis_gt(k*nq);
S
starlord 已提交
272
    std::vector<float> dis(k * nq);
X
xj.lin 已提交
273 274 275 276

    // insert data
    const int batch_size = 100;
    for (int j = 0; j < nb / batch_size; ++j) {
S
starlord 已提交
277 278
        stat = db_->InsertVectors(TABLE_NAME, batch_size, xb.data() + batch_size * j * TABLE_DIM, ids);
        if (j == 200) { sleep(1); }
S
starlord 已提交
279
        ASSERT_TRUE(stat.ok());
X
xj.lin 已提交
280 281
    }

S
starlord 已提交
282 283
    milvus::engine::TableIndex index;
    index.engine_type_ = (int) milvus::engine::EngineType::FAISS_IDMAP;
284
    db_->CreateIndex(TABLE_NAME, index); // wait until build index finish
X
xj.lin 已提交
285

S
starlord 已提交
286
    {
S
starlord 已提交
287
        milvus::engine::QueryResults results;
Y
Yu Kun 已提交
288
        stat = db_->Query(TABLE_NAME, k, nq, 10, xq.data(), results);
S
starlord 已提交
289
        ASSERT_TRUE(stat.ok());
S
starlord 已提交
290 291
    }

X
xj.lin 已提交
292
    {//search by specify index file
S
starlord 已提交
293
        milvus::engine::meta::DatesT dates;
294
        std::vector<std::string> file_ids = {"1", "2", "3", "4", "5", "6"};
S
starlord 已提交
295
        milvus::engine::QueryResults results;
Y
Yu Kun 已提交
296
        stat = db_->Query(TABLE_NAME, file_ids, k, nq, 10, xq.data(), dates, results);
S
starlord 已提交
297
        ASSERT_TRUE(stat.ok());
X
xj.lin 已提交
298
    }
X
xj.lin 已提交
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    //test FAISS_IVFSQ8H optimizer
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    db_->CreateIndex(TABLE_NAME, index); // wait until build index finish

    {
        milvus::engine::QueryResults results;
        stat = db_->Query(TABLE_NAME, k, nq, 10, xq.data(), results);
        ASSERT_TRUE(stat.ok());
    }

    {//search by specify index file
        milvus::engine::meta::DatesT dates;
        std::vector<std::string> file_ids = {"1", "2", "3", "4", "5", "6"};
        milvus::engine::QueryResults results;
        stat = db_->Query(TABLE_NAME, file_ids, k, nq, 10, xq.data(), dates, results);
        ASSERT_TRUE(stat.ok());
    }


S
starlord 已提交
319
    // TODO(lxj): add groundTruth assert
S
starlord 已提交
320
}
Y
c  
yu yunfeng 已提交
321

Y
Yu Kun 已提交
322
TEST_F(DBTest, PRELOADTABLE_TEST) {
S
starlord 已提交
323
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
324
    auto stat = db_->CreateTable(table_info);
Y
Yu Kun 已提交
325

S
starlord 已提交
326
    milvus::engine::meta::TableSchema table_info_get;
Y
Yu Kun 已提交
327 328
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
329
    ASSERT_TRUE(stat.ok());
Y
Yu Kun 已提交
330 331
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);

332
    int64_t nb = VECTOR_COUNT;
Y
Yu Kun 已提交
333 334 335
    std::vector<float> xb;
    BuildVectors(nb, xb);

Y
Yu Kun 已提交
336
    int loop = 5;
S
starlord 已提交
337
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
338
        milvus::engine::IDNumbers vector_ids;
339 340
        db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
        ASSERT_EQ(vector_ids.size(), nb);
Y
Yu Kun 已提交
341
    }
342

S
starlord 已提交
343 344
    milvus::engine::TableIndex index;
    index.engine_type_ = (int) milvus::engine::EngineType::FAISS_IDMAP;
345
    db_->CreateIndex(TABLE_NAME, index); // wait until build index finish
Y
Yu Kun 已提交
346

S
starlord 已提交
347
    int64_t prev_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
348
    stat = db_->PreloadTable(TABLE_NAME);
S
starlord 已提交
349
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
350
    int64_t cur_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
351 352 353
    ASSERT_TRUE(prev_cache_usage < cur_cache_usage);
}

S
starlord 已提交
354 355 356
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

S
starlord 已提交
357
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
358
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
359 360 361 362 363 364 365 366 367
    ASSERT_FALSE(stat.ok());

    stat = db_->DescribeTable(table_info);
    ASSERT_FALSE(stat.ok());

    bool has_table = false;
    stat = db_->HasTable(table_info.table_id_, has_table);
    ASSERT_FALSE(stat.ok());

S
starlord 已提交
368
    milvus::engine::IDNumbers ids;
S
starlord 已提交
369 370 371 372 373 374 375 376 377 378
    stat = db_->InsertVectors(table_info.table_id_, 0, nullptr, ids);
    ASSERT_FALSE(stat.ok());

    stat = db_->PreloadTable(table_info.table_id_);
    ASSERT_FALSE(stat.ok());

    uint64_t row_count = 0;
    stat = db_->GetTableRowCount(table_info.table_id_, row_count);
    ASSERT_FALSE(stat.ok());

S
starlord 已提交
379
    milvus::engine::TableIndex index;
S
starlord 已提交
380 381 382 383 384 385
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());

    stat = db_->DescribeIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());

S
starlord 已提交
386 387
    milvus::engine::meta::DatesT dates;
    milvus::engine::QueryResults results;
S
starlord 已提交
388 389 390 391 392 393 394 395 396 397 398
    stat = db_->Query(table_info.table_id_, 1, 1, 1, nullptr, dates, results);
    ASSERT_FALSE(stat.ok());
    std::vector<std::string> file_ids;
    stat = db_->Query(table_info.table_id_, file_ids, 1, 1, 1, nullptr, dates, results);
    ASSERT_FALSE(stat.ok());

    stat = db_->DeleteTable(table_info.table_id_, dates);
    ASSERT_FALSE(stat.ok());
}

TEST_F(DBTest, INDEX_TEST) {
S
starlord 已提交
399
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
400
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
401 402 403 404 405

    int64_t nb = VECTOR_COUNT;
    std::vector<float> xb;
    BuildVectors(nb, xb);

S
starlord 已提交
406
    milvus::engine::IDNumbers vector_ids;
S
starlord 已提交
407 408 409
    db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
    ASSERT_EQ(vector_ids.size(), nb);

S
starlord 已提交
410 411 412
    milvus::engine::TableIndex index;
    index.engine_type_ = (int) milvus::engine::EngineType::FAISS_IVFSQ8;
    index.metric_type_ = (int) milvus::engine::MetricType::IP;
S
starlord 已提交
413 414 415
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

S
starlord 已提交
416
    milvus::engine::TableIndex index_out;
S
starlord 已提交
417
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
418 419 420
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
    ASSERT_EQ(index.nlist_, index_out.nlist_);
S
starlord 已提交
421
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
422 423 424 425 426

    stat = db_->DropIndex(table_info.table_id_);
    ASSERT_TRUE(stat.ok());
}

G
groot 已提交
427
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
S
starlord 已提交
428
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
429
    auto stat = db_->CreateTable(table_info);
Z
zhiru 已提交
430

S
starlord 已提交
431
    std::vector<milvus::engine::meta::TableSchema> table_schema_array;
G
groot 已提交
432
    stat = db_->AllTables(table_schema_array);
S
starlord 已提交
433
    ASSERT_TRUE(stat.ok());
G
groot 已提交
434
    bool bfound = false;
S
starlord 已提交
435 436
    for (auto &schema : table_schema_array) {
        if (schema.table_id_ == TABLE_NAME) {
G
groot 已提交
437 438 439 440 441 442
            bfound = true;
            break;
        }
    }
    ASSERT_TRUE(bfound);

S
starlord 已提交
443
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
444 445
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
446
    ASSERT_TRUE(stat.ok());
G
groot 已提交
447
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
Z
zhiru 已提交
448

G
groot 已提交
449
    uint64_t size;
Z
zhiru 已提交
450 451
    db_->Size(size);

G
groot 已提交
452 453 454
    int64_t nb = 10;
    std::vector<float> xb;
    BuildVectors(nb, xb);
Z
zhiru 已提交
455

G
groot 已提交
456
    int loop = INSERT_LOOP;
S
starlord 已提交
457
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
458
        milvus::engine::IDNumbers vector_ids;
G
groot 已提交
459
        db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
Z
zhiru 已提交
460 461 462
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

G
groot 已提交
463
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
464 465 466

    db_->Size(size);
    LOG(DEBUG) << "size=" << size;
S
starlord 已提交
467
    ASSERT_LE(size, 1 * milvus::engine::G);
S
starlord 已提交
468
}
Z
zhiru 已提交
469

G
groot 已提交
470
TEST_F(DBTest2, DELETE_TEST) {
S
starlord 已提交
471
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
472
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
473

S
starlord 已提交
474
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
475 476
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
477
    ASSERT_TRUE(stat.ok());
G
groot 已提交
478

S
starlord 已提交
479 480 481
    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);
G
groot 已提交
482 483 484 485

    uint64_t size;
    db_->Size(size);

486
    int64_t nb = VECTOR_COUNT;
G
groot 已提交
487 488 489
    std::vector<float> xb;
    BuildVectors(nb, xb);

S
starlord 已提交
490
    milvus::engine::IDNumbers vector_ids;
491
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
492
    milvus::engine::TableIndex index;
493
    stat = db_->CreateIndex(TABLE_NAME, index);
G
groot 已提交
494

S
starlord 已提交
495
    std::vector<milvus::engine::meta::DateT> dates;
G
groot 已提交
496 497 498
    stat = db_->DeleteTable(TABLE_NAME, dates);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
499 500 501

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
502
}
503 504

TEST_F(DBTest2, DELETE_BY_RANGE_TEST) {
S
starlord 已提交
505
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
506
    auto stat = db_->CreateTable(table_info);
507

S
starlord 已提交
508
    milvus::engine::meta::TableSchema table_info_get;
509 510
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
511
    ASSERT_TRUE(stat.ok());
512 513 514 515 516 517 518

    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);

    uint64_t size;
    db_->Size(size);
519
    ASSERT_EQ(size, 0UL);
520

521
    int64_t nb = VECTOR_COUNT;
522 523 524
    std::vector<float> xb;
    BuildVectors(nb, xb);

S
starlord 已提交
525
    milvus::engine::IDNumbers vector_ids;
526
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
527
    milvus::engine::TableIndex index;
528 529 530 531
    stat = db_->CreateIndex(TABLE_NAME, index);

    db_->Size(size);
    ASSERT_NE(size, 0UL);
532

S
starlord 已提交
533
    std::vector<milvus::engine::meta::DateT> dates;
534 535
    std::string start_value = CurrentTmDate();
    std::string end_value = CurrentTmDate(1);
536 537
    ConvertTimeRangeToDBDates(start_value, end_value, dates);

538
    stat = db_->DeleteTable(TABLE_NAME, dates);
S
starlord 已提交
539
    ASSERT_TRUE(stat.ok());
540 541 542 543

    uint64_t row_count = 0;
    db_->GetTableRowCount(TABLE_NAME, row_count);
    ASSERT_EQ(row_count, 0UL);
S
starlord 已提交
544
}