test_db.cpp 17.5 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.

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

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

G
groot 已提交
33 34
namespace {

Y
youny626 已提交
35
static const char* TABLE_NAME = "test_group";
S
starlord 已提交
36 37 38 39 40 41
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 已提交
42
milvus::engine::meta::TableSchema
S
starlord 已提交
43
BuildTableSchema() {
S
starlord 已提交
44
    milvus::engine::meta::TableSchema table_info;
S
starlord 已提交
45 46 47 48
    table_info.dimension_ = TABLE_DIM;
    table_info.table_id_ = TABLE_NAME;
    return table_info;
}
G
groot 已提交
49

S
starlord 已提交
50
void
Y
youny626 已提交
51
BuildVectors(int64_t n, std::vector<float>& vectors) {
S
starlord 已提交
52 53
    vectors.clear();
    vectors.resize(n * TABLE_DIM);
Y
youny626 已提交
54
    float* data = vectors.data();
S
starlord 已提交
55 56 57
    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 已提交
58
    }
S
starlord 已提交
59
}
G
groot 已提交
60

S
starlord 已提交
61 62 63 64 65 66 67 68
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);
69

Y
youny626 已提交
70 71
    std::string str =
        std::to_string(t.tm_year + 1900) + "-" + std::to_string(t.tm_mon + 1) + "-" + std::to_string(t.tm_mday);
72

S
starlord 已提交
73 74
    return str;
}
75

S
starlord 已提交
76
void
Y
youny626 已提交
77 78
ConvertTimeRangeToDBDates(const std::string& start_value, const std::string& end_value,
                          std::vector<milvus::engine::meta::DateT>& dates) {
S
starlord 已提交
79
    dates.clear();
80

S
starlord 已提交
81 82
    time_t tt_start, tt_end;
    tm tm_start, tm_end;
S
starlord 已提交
83
    if (!milvus::server::CommonUtil::TimeStrToTime(start_value, tt_start, tm_start)) {
S
starlord 已提交
84 85
        return;
    }
86

S
starlord 已提交
87
    if (!milvus::server::CommonUtil::TimeStrToTime(end_value, tt_end, tm_end)) {
S
starlord 已提交
88 89
        return;
    }
90

Y
youny626 已提交
91
    int64_t days = (tt_end > tt_start) ? (tt_end - tt_start) / DAY_SECONDS : (tt_start - tt_end) / DAY_SECONDS;
S
starlord 已提交
92 93 94
    if (days == 0) {
        return;
    }
95

S
starlord 已提交
96 97 98
    for (int64_t i = 0; i < days; i++) {
        time_t tt_day = tt_start + DAY_SECONDS * i;
        tm tm_day;
S
starlord 已提交
99
        milvus::server::CommonUtil::ConvertTime(tt_day, tm_day);
100

Y
youny626 已提交
101
        int64_t date = tm_day.tm_year * 10000 + tm_day.tm_mon * 100 + tm_day.tm_mday;  // according to db logic
S
starlord 已提交
102
        dates.push_back(date);
103
    }
G
groot 已提交
104 105
}

Y
youny626 已提交
106
}  // namespace
S
starlord 已提交
107

X
Xu Peng 已提交
108 109
TEST_F(DBTest, CONFIG_TEST) {
    {
S
starlord 已提交
110
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf("wrong"));
111
        /* EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); */
X
Xu Peng 已提交
112 113
    }
    {
S
starlord 已提交
114
        milvus::engine::ArchiveConf conf("delete");
X
Xu Peng 已提交
115 116
        ASSERT_EQ(conf.GetType(), "delete");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
117
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
118 119
    }
    {
S
starlord 已提交
120
        milvus::engine::ArchiveConf conf("swap");
X
Xu Peng 已提交
121 122
        ASSERT_EQ(conf.GetType(), "swap");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
123
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
124 125
    }
    {
S
starlord 已提交
126 127 128
        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 已提交
129
        auto criterias = conf.GetCriterias();
S
starlord 已提交
130 131
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["disk"], 1024);
X
Xu Peng 已提交
132 133
    }
    {
S
starlord 已提交
134 135 136
        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 已提交
137
        auto criterias = conf.GetCriterias();
S
starlord 已提交
138 139
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["days"], 100);
X
Xu Peng 已提交
140 141
    }
    {
S
starlord 已提交
142 143 144
        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 已提交
145
        auto criterias = conf.GetCriterias();
S
starlord 已提交
146 147 148
        ASSERT_EQ(criterias.size(), 2);
        ASSERT_EQ(criterias["days"], 100);
        ASSERT_EQ(criterias["disk"], 200);
X
Xu Peng 已提交
149 150 151
    }
}

152
TEST_F(DBTest, DB_TEST) {
S
starlord 已提交
153
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
154
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
155

S
starlord 已提交
156
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
157
    table_info_get.table_id_ = TABLE_NAME;
G
groot 已提交
158
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
159
    ASSERT_TRUE(stat.ok());
G
groot 已提交
160
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
G
groot 已提交
161

S
starlord 已提交
162 163
    milvus::engine::IDNumbers vector_ids;
    milvus::engine::IDNumbers target_ids;
X
Xu Peng 已提交
164

G
groot 已提交
165 166 167
    int64_t nb = 50;
    std::vector<float> xb;
    BuildVectors(nb, xb);
G
groot 已提交
168

G
groot 已提交
169 170 171
    int64_t qb = 5;
    std::vector<float> qxb;
    BuildVectors(qb, qxb);
X
Xu Peng 已提交
172

X
Xu Peng 已提交
173
    std::thread search([&]() {
Y
youny626 已提交
174
        milvus::engine::QueryResults results;
X
Xu Peng 已提交
175 176 177 178 179
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

        INIT_TIMER;
        std::stringstream ss;
G
groot 已提交
180 181
        uint64_t count = 0;
        uint64_t prev_count = 0;
X
Xu Peng 已提交
182

S
starlord 已提交
183
        for (auto j = 0; j < 10; ++j) {
X
Xu Peng 已提交
184
            ss.str("");
X
Xu Peng 已提交
185
            db_->Size(count);
X
Xu Peng 已提交
186
            prev_count = count;
X
Xu Peng 已提交
187 188

            START_TIMER;
Y
youny626 已提交
189
            stat = db_->Query(TABLE_NAME, k, qb, 10, qxb.data(), results);
S
starlord 已提交
190
            ss << "Search " << j << " With Size " << count / milvus::engine::M << " M";
X
Xu Peng 已提交
191 192
            STOP_TIMER(ss.str());

S
starlord 已提交
193
            ASSERT_TRUE(stat.ok());
Y
youny626 已提交
194 195
            for (auto k = 0; k < qb; ++k) {
                ASSERT_EQ(results[k][0].first, target_ids[k]);
X
Xu Peng 已提交
196
                ss.str("");
Y
youny626 已提交
197 198 199
                ss << "Result [" << k << "]:";
                for (auto result : results[k]) {
                    ss << result.first << " ";
X
Xu Peng 已提交
200
                }
201
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
202
            }
X
Xu Peng 已提交
203
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
204 205 206 207
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

G
groot 已提交
208
    int loop = INSERT_LOOP;
X
Xu Peng 已提交
209

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

X
Xu Peng 已提交
220
    search.join();
S
starlord 已提交
221 222 223

    uint64_t count;
    stat = db_->GetTableRowCount(TABLE_NAME, count);
S
starlord 已提交
224
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
225 226
    ASSERT_GT(count, 0);
}
X
xj.lin 已提交
227

228
TEST_F(DBTest, SEARCH_TEST) {
S
starlord 已提交
229 230
    std::string config_path(CONFIG_PATH);
    config_path += CONFIG_FILE;
Y
youny626 已提交
231
    milvus::server::Config& config = milvus::server::Config::GetInstance();
S
starlord 已提交
232
    milvus::Status s = config.LoadConfigFile(config_path);
233

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

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

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

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

    // result data
Y
youny626 已提交
265
    // std::vector<long> nns_gt(k*nq);
S
starlord 已提交
266
    std::vector<int64_t> nns(k * nq);  // nns = nearst neg search
Y
youny626 已提交
267
    // std::vector<float> dis_gt(k*nq);
S
starlord 已提交
268
    std::vector<float> dis(k * nq);
X
xj.lin 已提交
269 270 271 272

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

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

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

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

Z
update  
zhiru 已提交
298
#ifdef CUSTOMIZATION
Y
youny626 已提交
299
    // test FAISS_IVFSQ8H optimizer
300
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
Y
youny626 已提交
301
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
302 303

    {
Y
youny626 已提交
304 305
        milvus::engine::QueryResults results;
        stat = db_->Query(TABLE_NAME, k, nq, 10, xq.data(), results);
306 307 308
        ASSERT_TRUE(stat.ok());
    }

Y
Yu Kun 已提交
309
    {
Y
youny626 已提交
310 311
        milvus::engine::QueryResults large_nq_results;
        stat = db_->Query(TABLE_NAME, k, 200, 10, xq.data(), large_nq_results);
Y
Yu Kun 已提交
312 313 314
        ASSERT_TRUE(stat.ok());
    }

Y
youny626 已提交
315
    {  // search by specify index file
316 317
        milvus::engine::meta::DatesT dates;
        std::vector<std::string> file_ids = {"1", "2", "3", "4", "5", "6"};
Y
youny626 已提交
318 319
        milvus::engine::QueryResults results;
        stat = db_->Query(TABLE_NAME, file_ids, k, nq, 10, xq.data(), dates, results);
320 321
        ASSERT_TRUE(stat.ok());
    }
Y
Yu Kun 已提交
322

X
xiaojun.lin 已提交
323
#endif
S
starlord 已提交
324
}
Y
c  
yu yunfeng 已提交
325

Y
Yu Kun 已提交
326
TEST_F(DBTest, PRELOADTABLE_TEST) {
S
starlord 已提交
327
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
328
    auto stat = db_->CreateTable(table_info);
Y
Yu Kun 已提交
329

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

336
    int64_t nb = VECTOR_COUNT;
Y
Yu Kun 已提交
337 338 339
    std::vector<float> xb;
    BuildVectors(nb, xb);

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

S
starlord 已提交
347
    milvus::engine::TableIndex index;
Y
youny626 已提交
348 349
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IDMAP;
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
Y
Yu Kun 已提交
350

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

S
starlord 已提交
358 359 360
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

S
starlord 已提交
361
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
362
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
363 364 365 366 367 368 369 370 371
    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 已提交
372
    milvus::engine::IDNumbers ids;
S
starlord 已提交
373 374 375 376 377 378 379 380 381 382
    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 已提交
383
    milvus::engine::TableIndex index;
S
starlord 已提交
384 385 386 387 388 389
    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 已提交
390
    milvus::engine::meta::DatesT dates;
Y
youny626 已提交
391 392
    milvus::engine::QueryResults results;
    stat = db_->Query(table_info.table_id_, 1, 1, 1, nullptr, dates, results);
S
starlord 已提交
393 394
    ASSERT_FALSE(stat.ok());
    std::vector<std::string> file_ids;
Y
youny626 已提交
395
    stat = db_->Query(table_info.table_id_, file_ids, 1, 1, 1, nullptr, dates, results);
S
starlord 已提交
396 397 398 399 400 401 402
    ASSERT_FALSE(stat.ok());

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

TEST_F(DBTest, INDEX_TEST) {
S
starlord 已提交
403
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
404
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
405 406 407 408 409

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

S
starlord 已提交
410
    milvus::engine::IDNumbers vector_ids;
S
starlord 已提交
411 412 413
    db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
    ASSERT_EQ(vector_ids.size(), nb);

S
starlord 已提交
414
    milvus::engine::TableIndex index;
Y
youny626 已提交
415 416
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
    index.metric_type_ = (int)milvus::engine::MetricType::IP;
S
starlord 已提交
417 418 419
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

Y
youny626 已提交
420
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
Y
Yu Kun 已提交
421 422 423 424 425 426 427 428 429
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

#ifdef CUSTOMIZATION
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());
#endif

S
starlord 已提交
430
    milvus::engine::TableIndex index_out;
S
starlord 已提交
431
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
432 433 434
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
    ASSERT_EQ(index.nlist_, index_out.nlist_);
S
starlord 已提交
435
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
436 437 438 439 440

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

G
groot 已提交
441
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
S
starlord 已提交
442
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
443
    auto stat = db_->CreateTable(table_info);
Z
zhiru 已提交
444

S
starlord 已提交
445
    std::vector<milvus::engine::meta::TableSchema> table_schema_array;
G
groot 已提交
446
    stat = db_->AllTables(table_schema_array);
S
starlord 已提交
447
    ASSERT_TRUE(stat.ok());
G
groot 已提交
448
    bool bfound = false;
Y
youny626 已提交
449
    for (auto& schema : table_schema_array) {
S
starlord 已提交
450
        if (schema.table_id_ == TABLE_NAME) {
G
groot 已提交
451 452 453 454 455 456
            bfound = true;
            break;
        }
    }
    ASSERT_TRUE(bfound);

S
starlord 已提交
457
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
458 459
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
460
    ASSERT_TRUE(stat.ok());
G
groot 已提交
461
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
Z
zhiru 已提交
462

G
groot 已提交
463
    uint64_t size;
Z
zhiru 已提交
464 465
    db_->Size(size);

G
groot 已提交
466 467 468
    int64_t nb = 10;
    std::vector<float> xb;
    BuildVectors(nb, xb);
Z
zhiru 已提交
469

G
groot 已提交
470
    int loop = INSERT_LOOP;
S
starlord 已提交
471
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
472
        milvus::engine::IDNumbers vector_ids;
G
groot 已提交
473
        db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
Z
zhiru 已提交
474 475 476
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

G
groot 已提交
477
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
478 479 480

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

G
groot 已提交
484
TEST_F(DBTest2, DELETE_TEST) {
S
starlord 已提交
485
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
486
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
487

S
starlord 已提交
488
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
489 490
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
491
    ASSERT_TRUE(stat.ok());
G
groot 已提交
492

S
starlord 已提交
493 494 495
    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);
G
groot 已提交
496 497 498 499

    uint64_t size;
    db_->Size(size);

500
    int64_t nb = VECTOR_COUNT;
G
groot 已提交
501 502 503
    std::vector<float> xb;
    BuildVectors(nb, xb);

S
starlord 已提交
504
    milvus::engine::IDNumbers vector_ids;
505
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
506
    milvus::engine::TableIndex index;
507
    stat = db_->CreateIndex(TABLE_NAME, index);
G
groot 已提交
508

S
starlord 已提交
509
    std::vector<milvus::engine::meta::DateT> dates;
G
groot 已提交
510 511 512
    stat = db_->DeleteTable(TABLE_NAME, dates);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
513 514 515

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
516
}
517 518

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

S
starlord 已提交
522
    milvus::engine::meta::TableSchema table_info_get;
523 524
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
525
    ASSERT_TRUE(stat.ok());
526 527 528 529 530 531 532

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

    uint64_t size;
    db_->Size(size);
533
    ASSERT_EQ(size, 0UL);
534

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

S
starlord 已提交
539
    milvus::engine::IDNumbers vector_ids;
540
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
541
    milvus::engine::TableIndex index;
542 543 544 545
    stat = db_->CreateIndex(TABLE_NAME, index);

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

S
starlord 已提交
547
    std::vector<milvus::engine::meta::DateT> dates;
548 549
    std::string start_value = CurrentTmDate();
    std::string end_value = CurrentTmDate(1);
550 551
    ConvertTimeRangeToDBDates(start_value, end_value, dates);

552
    stat = db_->DeleteTable(TABLE_NAME, dates);
S
starlord 已提交
553
    ASSERT_TRUE(stat.ok());
554 555 556 557

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