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.

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
youny626 已提交
178
        milvus::engine::QueryResults results;
X
Xu Peng 已提交
179 180 181 182 183
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

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

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

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

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

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

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

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

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

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

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
    {
Y
youny626 已提交
287 288
        milvus::engine::QueryResults results;
        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"};
Y
youny626 已提交
295 296
        milvus::engine::QueryResults results;
        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

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

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

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

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


X
xiaojun.lin 已提交
326
#endif
S
starlord 已提交
327
}
Y
c  
yu yunfeng 已提交
328

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

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

339
    int64_t nb = VECTOR_COUNT;
Y
Yu Kun 已提交
340 341 342
    std::vector<float> xb;
    BuildVectors(nb, xb);

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

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

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

S
starlord 已提交
361 362 363
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

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

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

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

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

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

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

Y
Yu Kun 已提交
423 424 425 426 427 428 429 430 431 432
    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 已提交
433
    milvus::engine::TableIndex index_out;
S
starlord 已提交
434
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
435 436 437
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
    ASSERT_EQ(index.nlist_, index_out.nlist_);
S
starlord 已提交
438
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
439 440 441 442 443

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

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

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

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

G
groot 已提交
466
    uint64_t size;
Z
zhiru 已提交
467 468
    db_->Size(size);

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

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

G
groot 已提交
480
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
481 482 483

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

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

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

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

    uint64_t size;
    db_->Size(size);

503
    int64_t nb = VECTOR_COUNT;
G
groot 已提交
504 505 506
    std::vector<float> xb;
    BuildVectors(nb, xb);

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

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

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
519
}
520 521

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

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

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

    uint64_t size;
    db_->Size(size);
536
    ASSERT_EQ(size, 0UL);
537

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

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

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

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

555
    stat = db_->DeleteTable(TABLE_NAME, dates);
S
starlord 已提交
556
    ASSERT_TRUE(stat.ok());
557 558 559 560

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