test_db.cpp 18.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 {

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

S
starlord 已提交
51 52 53 54 55 56 57 58
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 已提交
59
    }
S
starlord 已提交
60
}
G
groot 已提交
61

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

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

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

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

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

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

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

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

S
starlord 已提交
104 105 106
        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);
107
    }
G
groot 已提交
108 109
}

S
starlord 已提交
110 111
} // namespace

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

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

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

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

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

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

X
Xu Peng 已提交
177
    std::thread search([&]() {
Y
yudong.cai 已提交
178 179
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
X
Xu Peng 已提交
180 181 182 183 184
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

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

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

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

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

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

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

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

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

233
TEST_F(DBTest, SEARCH_TEST) {
S
starlord 已提交
234 235
    std::string config_path(CONFIG_PATH);
    config_path += CONFIG_FILE;
236
    milvus::server::Config &config = milvus::server::Config::GetInstance();
S
starlord 已提交
237
    milvus::Status s = config.LoadConfigFile(config_path);
238

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

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

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

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

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

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

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

S
starlord 已提交
287
    {
Y
yudong.cai 已提交
288 289 290
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
        stat = db_->Query(TABLE_NAME, k, nq, 10, xq.data(), result_ids, result_distances);
S
starlord 已提交
291
        ASSERT_TRUE(stat.ok());
S
starlord 已提交
292 293
    }

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

Z
update  
zhiru 已提交
303
#ifdef CUSTOMIZATION
304 305 306 307 308
    //test FAISS_IVFSQ8H optimizer
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    db_->CreateIndex(TABLE_NAME, index); // wait until build index finish

    {
Y
yudong.cai 已提交
309 310 311
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
        stat = db_->Query(TABLE_NAME, k, nq, 10, xq.data(), result_ids, result_distances);
312 313 314
        ASSERT_TRUE(stat.ok());
    }

Y
Yu Kun 已提交
315
    {
Y
yudong.cai 已提交
316 317 318
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
        stat = db_->Query(TABLE_NAME, k, 200, 10, xq.data(), result_ids, result_distances);
Y
Yu Kun 已提交
319 320 321
        ASSERT_TRUE(stat.ok());
    }

322 323 324
    {//search by specify index file
        milvus::engine::meta::DatesT dates;
        std::vector<std::string> file_ids = {"1", "2", "3", "4", "5", "6"};
Y
yudong.cai 已提交
325 326 327
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
        stat = db_->Query(TABLE_NAME, file_ids, k, nq, 10, xq.data(), dates, result_ids, result_distances);
328 329
        ASSERT_TRUE(stat.ok());
    }
Y
Yu Kun 已提交
330 331


X
xiaojun.lin 已提交
332
#endif
S
starlord 已提交
333
}
Y
c  
yu yunfeng 已提交
334

Y
Yu Kun 已提交
335
TEST_F(DBTest, PRELOADTABLE_TEST) {
S
starlord 已提交
336
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
337
    auto stat = db_->CreateTable(table_info);
Y
Yu Kun 已提交
338

S
starlord 已提交
339
    milvus::engine::meta::TableSchema table_info_get;
Y
Yu Kun 已提交
340 341
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
342
    ASSERT_TRUE(stat.ok());
Y
Yu Kun 已提交
343 344
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);

345
    int64_t nb = VECTOR_COUNT;
Y
Yu Kun 已提交
346 347 348
    std::vector<float> xb;
    BuildVectors(nb, xb);

Y
Yu Kun 已提交
349
    int loop = 5;
S
starlord 已提交
350
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
351
        milvus::engine::IDNumbers vector_ids;
352 353
        db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
        ASSERT_EQ(vector_ids.size(), nb);
Y
Yu Kun 已提交
354
    }
355

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

S
starlord 已提交
360
    int64_t prev_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
361
    stat = db_->PreloadTable(TABLE_NAME);
S
starlord 已提交
362
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
363
    int64_t cur_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
364 365 366
    ASSERT_TRUE(prev_cache_usage < cur_cache_usage);
}

S
starlord 已提交
367 368 369
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

S
starlord 已提交
370
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
371
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
372 373 374 375 376 377 378 379 380
    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 已提交
381
    milvus::engine::IDNumbers ids;
S
starlord 已提交
382 383 384 385 386 387 388 389 390 391
    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 已提交
392
    milvus::engine::TableIndex index;
S
starlord 已提交
393 394 395 396 397 398
    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 已提交
399
    milvus::engine::meta::DatesT dates;
Y
yudong.cai 已提交
400 401 402
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_distances;
    stat = db_->Query(table_info.table_id_, 1, 1, 1, nullptr, dates, result_ids, result_distances);
S
starlord 已提交
403 404
    ASSERT_FALSE(stat.ok());
    std::vector<std::string> file_ids;
Y
yudong.cai 已提交
405
    stat = db_->Query(table_info.table_id_, file_ids, 1, 1, 1, nullptr, dates, result_ids, result_distances);
S
starlord 已提交
406 407 408 409 410 411 412
    ASSERT_FALSE(stat.ok());

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

TEST_F(DBTest, INDEX_TEST) {
S
starlord 已提交
413
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
414
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
415 416 417 418 419

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

S
starlord 已提交
420
    milvus::engine::IDNumbers vector_ids;
S
starlord 已提交
421 422 423
    db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
    ASSERT_EQ(vector_ids.size(), nb);

S
starlord 已提交
424 425 426
    milvus::engine::TableIndex index;
    index.engine_type_ = (int) milvus::engine::EngineType::FAISS_IVFSQ8;
    index.metric_type_ = (int) milvus::engine::MetricType::IP;
S
starlord 已提交
427 428 429
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

Y
Yu Kun 已提交
430 431 432 433 434 435 436 437 438 439
    index.engine_type_ = (int) milvus::engine::EngineType::FAISS_IVFFLAT;
    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 已提交
440
    milvus::engine::TableIndex index_out;
S
starlord 已提交
441
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
442 443 444
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
    ASSERT_EQ(index.nlist_, index_out.nlist_);
S
starlord 已提交
445
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
446 447 448 449 450

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

G
groot 已提交
451
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
S
starlord 已提交
452
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
453
    auto stat = db_->CreateTable(table_info);
Z
zhiru 已提交
454

S
starlord 已提交
455
    std::vector<milvus::engine::meta::TableSchema> table_schema_array;
G
groot 已提交
456
    stat = db_->AllTables(table_schema_array);
S
starlord 已提交
457
    ASSERT_TRUE(stat.ok());
G
groot 已提交
458
    bool bfound = false;
S
starlord 已提交
459 460
    for (auto &schema : table_schema_array) {
        if (schema.table_id_ == TABLE_NAME) {
G
groot 已提交
461 462 463 464 465 466
            bfound = true;
            break;
        }
    }
    ASSERT_TRUE(bfound);

S
starlord 已提交
467
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
468 469
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
470
    ASSERT_TRUE(stat.ok());
G
groot 已提交
471
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
Z
zhiru 已提交
472

G
groot 已提交
473
    uint64_t size;
Z
zhiru 已提交
474 475
    db_->Size(size);

G
groot 已提交
476 477 478
    int64_t nb = 10;
    std::vector<float> xb;
    BuildVectors(nb, xb);
Z
zhiru 已提交
479

G
groot 已提交
480
    int loop = INSERT_LOOP;
S
starlord 已提交
481
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
482
        milvus::engine::IDNumbers vector_ids;
G
groot 已提交
483
        db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
Z
zhiru 已提交
484 485 486
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

G
groot 已提交
487
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
488 489 490

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

G
groot 已提交
494
TEST_F(DBTest2, DELETE_TEST) {
S
starlord 已提交
495
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
496
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
497

S
starlord 已提交
498
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
499 500
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
501
    ASSERT_TRUE(stat.ok());
G
groot 已提交
502

S
starlord 已提交
503 504 505
    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);
G
groot 已提交
506 507 508 509

    uint64_t size;
    db_->Size(size);

510
    int64_t nb = VECTOR_COUNT;
G
groot 已提交
511 512 513
    std::vector<float> xb;
    BuildVectors(nb, xb);

S
starlord 已提交
514
    milvus::engine::IDNumbers vector_ids;
515
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
516
    milvus::engine::TableIndex index;
517
    stat = db_->CreateIndex(TABLE_NAME, index);
G
groot 已提交
518

S
starlord 已提交
519
    std::vector<milvus::engine::meta::DateT> dates;
G
groot 已提交
520 521 522
    stat = db_->DeleteTable(TABLE_NAME, dates);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
523 524 525

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
526
}
527 528

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

S
starlord 已提交
532
    milvus::engine::meta::TableSchema table_info_get;
533 534
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
535
    ASSERT_TRUE(stat.ok());
536 537 538 539 540 541 542

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

    uint64_t size;
    db_->Size(size);
543
    ASSERT_EQ(size, 0UL);
544

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

S
starlord 已提交
549
    milvus::engine::IDNumbers vector_ids;
550
    stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
S
starlord 已提交
551
    milvus::engine::TableIndex index;
552 553 554 555
    stat = db_->CreateIndex(TABLE_NAME, index);

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

S
starlord 已提交
557
    std::vector<milvus::engine::meta::DateT> dates;
558 559
    std::string start_value = CurrentTmDate();
    std::string end_value = CurrentTmDate(1);
560 561
    ConvertTimeRangeToDBDates(start_value, end_value, dates);

562
    stat = db_->DeleteTable(TABLE_NAME, dates);
S
starlord 已提交
563
    ASSERT_TRUE(stat.ok());
564 565 566 567

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