RequestTask.cpp 22.1 KB
Newer Older
G
groot 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
G
groot 已提交
6
#include "RequestTask.h"
G
groot 已提交
7 8 9 10
#include "ServerConfig.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
G
groot 已提交
11
#include "DBWrapper.h"
G
groot 已提交
12
#include "version.h"
G
groot 已提交
13 14

namespace zilliz {
J
jinhai 已提交
15
namespace milvus {
G
groot 已提交
16 17
namespace server {

G
groot 已提交
18 19
using namespace ::milvus;

G
groot 已提交
20 21
static const std::string DQL_TASK_GROUP = "dql";
static const std::string DDL_DML_TASK_GROUP = "ddl_dml";
G
groot 已提交
22
static const std::string PING_TASK_GROUP = "ping";
G
groot 已提交
23

J
jinhai 已提交
24 25
using DB_META = zilliz::milvus::engine::meta::Meta;
using DB_DATE = zilliz::milvus::engine::meta::DateT;
G
groot 已提交
26 27

namespace {
G
groot 已提交
28 29 30 31 32
    engine::EngineType EngineType(int type) {
        static std::map<int, engine::EngineType> map_type = {
                {0, engine::EngineType::INVALID},
                {1, engine::EngineType::FAISS_IDMAP},
                {2, engine::EngineType::FAISS_IVFFLAT},
G
groot 已提交
33
                {3, engine::EngineType::FAISS_IVFSQ8},
G
groot 已提交
34 35 36 37 38 39 40
        };

        if(map_type.find(type) == map_type.end()) {
            return engine::EngineType::INVALID;
        }

        return map_type[type];
G
groot 已提交
41
    }
G
groot 已提交
42

G
groot 已提交
43 44 45 46 47
    int IndexType(engine::EngineType type) {
        static std::map<engine::EngineType, int> map_type = {
                {engine::EngineType::INVALID, 0},
                {engine::EngineType::FAISS_IDMAP, 1},
                {engine::EngineType::FAISS_IVFFLAT, 2},
G
groot 已提交
48
                {engine::EngineType::FAISS_IVFSQ8, 3},
G
groot 已提交
49 50 51 52 53 54 55 56 57
        };

        if(map_type.find(type) == map_type.end()) {
            return 0;
        }

        return map_type[type];
    }

G
groot 已提交
58
    void
G
groot 已提交
59 60
    ConvertRowRecordToFloatArray(const std::vector<thrift::RowRecord>& record_array,
                                 uint64_t dimension,
G
groot 已提交
61 62 63
                                 std::vector<float>& float_array,
                                 ServerError& error_code,
                                 std::string& error_msg) {
G
groot 已提交
64 65 66 67 68
        uint64_t vec_count = record_array.size();
        float_array.resize(vec_count*dimension);//allocate enough memory
        for(uint64_t i = 0; i < vec_count; i++) {
            const auto& record = record_array[i];
            if(record.vector_data.empty()) {
G
groot 已提交
69 70 71
                error_code = SERVER_INVALID_ROWRECORD;
                error_msg = "Rowrecord float array is empty";
                return;
G
groot 已提交
72 73 74 75
            }
            uint64_t vec_dim = record.vector_data.size()/sizeof(double);//how many double value?
            if(vec_dim != dimension) {
                error_code = SERVER_INVALID_VECTOR_DIMENSION;
G
groot 已提交
76 77 78
                error_msg = "Invalid rowrecord dimension: " + std::to_string(vec_dim)
                                 + " vs. table dimension:" + std::to_string(dimension);
                return;
G
groot 已提交
79 80 81 82 83 84 85 86 87 88 89 90
            }

            //convert double array to float array(thrift has no float type)
            const double* d_p = reinterpret_cast<const double*>(record.vector_data.data());
            for(uint64_t d = 0; d < vec_dim; d++) {
                float_array[i*vec_dim + d] = (float)(d_p[d]);
            }
        }
    }

    static constexpr long DAY_SECONDS = 86400;

G
groot 已提交
91
    void
G
groot 已提交
92
    ConvertTimeRangeToDBDates(const std::vector<thrift::Range> &range_array,
G
groot 已提交
93 94 95
                              std::vector<DB_DATE>& dates,
                              ServerError& error_code,
                              std::string& error_msg) {
G
groot 已提交
96 97 98 99 100 101
        dates.clear();
        for(auto& range : range_array) {
            time_t tt_start, tt_end;
            tm tm_start, tm_end;
            if(!CommonUtil::TimeStrToTime(range.start_value, tt_start, tm_start)){
                error_code = SERVER_INVALID_TIME_RANGE;
G
groot 已提交
102 103
                error_msg = "Invalid time range: " + range.start_value;
                return;
G
groot 已提交
104 105 106 107
            }

            if(!CommonUtil::TimeStrToTime(range.end_value, tt_end, tm_end)){
                error_code = SERVER_INVALID_TIME_RANGE;
G
groot 已提交
108 109
                error_msg = "Invalid time range: " + range.start_value;
                return;
G
groot 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
            }

            long days = (tt_end > tt_start) ? (tt_end - tt_start)/DAY_SECONDS : (tt_start - tt_end)/DAY_SECONDS;
            for(long i = 0; i <= days; i++) {
                time_t tt_day = tt_start + DAY_SECONDS*i;
                tm tm_day;
                CommonUtil::ConvertTime(tt_day, tm_day);

                long date = tm_day.tm_year*10000 + tm_day.tm_mon*100 + tm_day.tm_mday;//according to db logic
                dates.push_back(date);
            }
        }
    }
G
groot 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CreateTableTask::CreateTableTask(const thrift::TableSchema& schema)
: BaseTask(DDL_DML_TASK_GROUP),
  schema_(schema) {

}

BaseTaskPtr CreateTableTask::Create(const thrift::TableSchema& schema) {
    return std::shared_ptr<BaseTask>(new CreateTableTask(schema));
}

ServerError CreateTableTask::OnExecute() {
    TimeRecorder rc("CreateTableTask");
P
peng.xu 已提交
138

G
groot 已提交
139
    try {
G
groot 已提交
140
        //step 1: check arguments
G
groot 已提交
141 142 143 144 145
        if(schema_.table_name.empty()) {
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
        }
        if(schema_.dimension <= 0) {
            return SetError(SERVER_INVALID_TABLE_DIMENSION, "Invalid table dimension: " + std::to_string(schema_.dimension));
G
groot 已提交
146 147
        }

G
groot 已提交
148 149
        engine::EngineType engine_type = EngineType(schema_.index_type);
        if(engine_type == engine::EngineType::INVALID) {
G
groot 已提交
150
            return SetError(SERVER_INVALID_INDEX_TYPE, "Invalid index type: " + std::to_string(schema_.index_type));
G
groot 已提交
151 152 153
        }

        //step 2: construct table schema
G
groot 已提交
154
        engine::meta::TableSchema table_info;
G
groot 已提交
155 156 157 158 159
        table_info.dimension_ = (uint16_t)schema_.dimension;
        table_info.table_id_ = schema_.table_name;
        table_info.engine_type_ = (int)EngineType(schema_.index_type);
        table_info.store_raw_data_ = schema_.store_raw_vector;

G
groot 已提交
160
        //step 3: create table
G
groot 已提交
161
        engine::Status stat = DBWrapper::DB()->CreateTable(table_info);
G
groot 已提交
162
        if(!stat.ok()) {//table could exist
G
groot 已提交
163
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
G
groot 已提交
164 165 166
        }

    } catch (std::exception& ex) {
G
groot 已提交
167
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
168 169 170 171 172 173 174 175 176
    }

    rc.Record("done");

    return SERVER_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DescribeTableTask::DescribeTableTask(const std::string &table_name, thrift::TableSchema &schema)
G
groot 已提交
177
    : BaseTask(DDL_DML_TASK_GROUP),
G
groot 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
      table_name_(table_name),
      schema_(schema) {
    schema_.table_name = table_name_;
}

BaseTaskPtr DescribeTableTask::Create(const std::string& table_name, thrift::TableSchema& schema) {
    return std::shared_ptr<BaseTask>(new DescribeTableTask(table_name, schema));
}

ServerError DescribeTableTask::OnExecute() {
    TimeRecorder rc("DescribeTableTask");

    try {
G
groot 已提交
191 192
        //step 1: check arguments
        if(table_name_.empty()) {
G
groot 已提交
193
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
194 195 196
        }

        //step 2: get table info
G
groot 已提交
197
        engine::meta::TableSchema table_info;
G
groot 已提交
198
        table_info.table_id_ = table_name_;
G
groot 已提交
199
        engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
G
groot 已提交
200
        if(!stat.ok()) {
G
groot 已提交
201
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
G
groot 已提交
202 203
        }

G
groot 已提交
204 205 206 207 208
        schema_.table_name = table_info.table_id_;
        schema_.index_type = IndexType((engine::EngineType)table_info.engine_type_);
        schema_.dimension = table_info.dimension_;
        schema_.store_raw_vector = table_info.store_raw_data_;

G
groot 已提交
209
    } catch (std::exception& ex) {
G
groot 已提交
210
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
211 212 213 214 215 216 217
    }

    rc.Record("done");

    return SERVER_SUCCESS;
}

P
peng.xu 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BuildIndexTask::BuildIndexTask(const std::string& table_name)
    : BaseTask(DDL_DML_TASK_GROUP),
      table_name_(table_name) {
}

BaseTaskPtr BuildIndexTask::Create(const std::string& table_name) {
    return std::shared_ptr<BaseTask>(new BuildIndexTask(table_name));
}

ServerError BuildIndexTask::OnExecute() {
    try {
        TimeRecorder rc("BuildIndexTask");

        //step 1: check arguments
        if(table_name_.empty()) {
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
        }

        //step 2: check table existence
        engine::Status stat = DBWrapper::DB()->BuildIndex(table_name_);
        if(!stat.ok()) {
            return SetError(SERVER_BUILD_INDEX_ERROR, "Engine failed: " + stat.ToString());
        }

        rc.Elapse("totally cost");
    } catch (std::exception& ex) {
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HasTableTask::HasTableTask(const std::string& table_name, bool& has_table)
    : BaseTask(DDL_DML_TASK_GROUP),
      table_name_(table_name),
      has_table_(has_table) {

}

BaseTaskPtr HasTableTask::Create(const std::string& table_name, bool& has_table) {
    return std::shared_ptr<BaseTask>(new HasTableTask(table_name, has_table));
}

ServerError HasTableTask::OnExecute() {
    try {
        TimeRecorder rc("HasTableTask");

        //step 1: check arguments
G
groot 已提交
268 269
        if(table_name_.empty()) {
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
270 271 272 273
        }

        //step 2: check table existence
        engine::Status stat = DBWrapper::DB()->HasTable(table_name_, has_table_);
G
groot 已提交
274 275 276
        if(!stat.ok()) {
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
        }
G
groot 已提交
277 278 279

        rc.Elapse("totally cost");
    } catch (std::exception& ex) {
G
groot 已提交
280
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
281 282 283 284 285
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
286 287 288 289 290 291 292
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DeleteTableTask::DeleteTableTask(const std::string& table_name)
    : BaseTask(DDL_DML_TASK_GROUP),
      table_name_(table_name) {

}

G
groot 已提交
293 294
BaseTaskPtr DeleteTableTask::Create(const std::string& table_name) {
    return std::shared_ptr<BaseTask>(new DeleteTableTask(table_name));
G
groot 已提交
295 296 297
}

ServerError DeleteTableTask::OnExecute() {
G
groot 已提交
298 299 300
    try {
        TimeRecorder rc("DeleteTableTask");

G
groot 已提交
301
        //step 1: check arguments
G
groot 已提交
302
        if (table_name_.empty()) {
G
groot 已提交
303
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
304 305 306 307 308
        }

        //step 2: check table existence
        engine::meta::TableSchema table_info;
        table_info.table_id_ = table_name_;
G
groot 已提交
309
        engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
G
groot 已提交
310
        if(!stat.ok()) {
G
groot 已提交
311 312 313 314 315
            if(stat.IsNotFound()) {
                return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name_ + " not exists");
            } else {
                return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
            }
G
groot 已提交
316 317 318
        }

        rc.Record("check validation");
G
groot 已提交
319

G
groot 已提交
320 321
        //step 3: delete table
        std::vector<DB_DATE> dates;
G
groot 已提交
322
        stat = DBWrapper::DB()->DeleteTable(table_name_, dates);
G
groot 已提交
323
        if(!stat.ok()) {
G
groot 已提交
324
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
G
groot 已提交
325 326 327 328 329
        }

        rc.Record("deleta table");
        rc.Elapse("totally cost");
    } catch (std::exception& ex) {
G
groot 已提交
330
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
331 332 333
    }

    return SERVER_SUCCESS;
G
groot 已提交
334 335
}

G
groot 已提交
336 337
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ShowTablesTask::ShowTablesTask(std::vector<std::string>& tables)
G
groot 已提交
338
    : BaseTask(DDL_DML_TASK_GROUP),
G
groot 已提交
339 340 341 342 343 344 345 346 347
      tables_(tables) {

}

BaseTaskPtr ShowTablesTask::Create(std::vector<std::string>& tables) {
    return std::shared_ptr<BaseTask>(new ShowTablesTask(tables));
}

ServerError ShowTablesTask::OnExecute() {
G
groot 已提交
348
    std::vector<engine::meta::TableSchema> schema_array;
G
groot 已提交
349
    engine::Status stat = DBWrapper::DB()->AllTables(schema_array);
G
groot 已提交
350
    if(!stat.ok()) {
G
groot 已提交
351
        return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
G
groot 已提交
352 353 354 355 356 357
    }

    tables_.clear();
    for(auto& schema : schema_array) {
        tables_.push_back(schema.table_id_);
    }
G
groot 已提交
358 359 360

    return SERVER_SUCCESS;
}
G
groot 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AddVectorTask::AddVectorTask(const std::string& table_name,
                                       const std::vector<thrift::RowRecord>& record_array,
                                       std::vector<int64_t>& record_ids)
    : BaseTask(DDL_DML_TASK_GROUP),
      table_name_(table_name),
      record_array_(record_array),
      record_ids_(record_ids) {
    record_ids_.clear();
}

BaseTaskPtr AddVectorTask::Create(const std::string& table_name,
                                       const std::vector<thrift::RowRecord>& record_array,
                                       std::vector<int64_t>& record_ids) {
    return std::shared_ptr<BaseTask>(new AddVectorTask(table_name, record_array, record_ids));
}

ServerError AddVectorTask::OnExecute() {
    try {
        TimeRecorder rc("AddVectorTask");

G
groot 已提交
383 384
        //step 1: check arguments
        if (table_name_.empty()) {
G
groot 已提交
385
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
386 387
        }

G
groot 已提交
388
        if(record_array_.empty()) {
G
groot 已提交
389
            return SetError(SERVER_INVALID_ROWRECORD_ARRAY, "Row record array is empty");
G
groot 已提交
390 391
        }

G
groot 已提交
392
        //step 2: check table existence
G
groot 已提交
393
        engine::meta::TableSchema table_info;
G
groot 已提交
394
        table_info.table_id_ = table_name_;
G
groot 已提交
395
        engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
G
groot 已提交
396
        if(!stat.ok()) {
G
groot 已提交
397 398 399 400 401
            if(stat.IsNotFound()) {
                return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name_ + " not exists");
            } else {
                return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
            }
G
groot 已提交
402 403
        }

G
groot 已提交
404
        rc.Record("check validation");
G
groot 已提交
405

G
groot 已提交
406
        //step 3: prepare float data
G
groot 已提交
407
        std::vector<float> vec_f;
G
groot 已提交
408 409 410 411 412
        ServerError error_code = SERVER_SUCCESS;
        std::string error_msg;
        ConvertRowRecordToFloatArray(record_array_, table_info.dimension_, vec_f, error_code, error_msg);
        if(error_code != SERVER_SUCCESS) {
            return SetError(error_code, error_msg);
G
groot 已提交
413 414 415 416
        }

        rc.Record("prepare vectors data");

G
groot 已提交
417
        //step 4: insert vectors
G
groot 已提交
418
        uint64_t vec_count = (uint64_t)record_array_.size();
G
groot 已提交
419
        stat = DBWrapper::DB()->InsertVectors(table_name_, vec_count, vec_f.data(), record_ids_);
G
groot 已提交
420 421
        rc.Record("add vectors to engine");
        if(!stat.ok()) {
G
groot 已提交
422
            return SetError(SERVER_CACHE_ERROR, "Cache error: " + stat.ToString());
G
groot 已提交
423 424
        }

G
groot 已提交
425
        if(record_ids_.size() != vec_count) {
G
groot 已提交
426 427 428
            std::string msg = "Add " + std::to_string(vec_count) + " vectors but only return "
                    + std::to_string(record_ids_.size()) + " id";
            return SetError(SERVER_ILLEGAL_VECTOR_ID, msg);
G
groot 已提交
429 430
        }

G
groot 已提交
431 432
        rc.Record("do insert");
        rc.Elapse("totally cost");
G
groot 已提交
433 434

    } catch (std::exception& ex) {
G
groot 已提交
435
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
436 437 438 439 440 441
    }

    return SERVER_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
442 443 444 445
SearchVectorTask::SearchVectorTask(const std::string &table_name,
                                   const std::vector<std::string>& file_id_array,
                                   const std::vector<thrift::RowRecord> &query_record_array,
                                   const std::vector<thrift::Range> &query_range_array,
G
groot 已提交
446
                                   const int64_t top_k,
447
                                   std::vector<thrift::TopKQueryResult> &result_array)
G
groot 已提交
448 449 450 451 452 453 454
    : BaseTask(DQL_TASK_GROUP),
      table_name_(table_name),
      file_id_array_(file_id_array),
      record_array_(query_record_array),
      range_array_(query_range_array),
      top_k_(top_k),
      result_array_(result_array) {
G
groot 已提交
455 456 457 458

}

BaseTaskPtr SearchVectorTask::Create(const std::string& table_name,
459
                                     const std::vector<std::string>& file_id_array,
G
groot 已提交
460
                                     const std::vector<thrift::RowRecord> & query_record_array,
G
groot 已提交
461
                                     const std::vector<thrift::Range> & query_range_array,
G
groot 已提交
462 463
                                     const int64_t top_k,
                                     std::vector<thrift::TopKQueryResult>& result_array) {
464
    return std::shared_ptr<BaseTask>(new SearchVectorTask(table_name, file_id_array,
G
groot 已提交
465
            query_record_array, query_range_array, top_k, result_array));
G
groot 已提交
466 467 468 469 470 471
}

ServerError SearchVectorTask::OnExecute() {
    try {
        TimeRecorder rc("SearchVectorTask");

G
groot 已提交
472 473
        //step 1: check arguments
        if (table_name_.empty()) {
G
groot 已提交
474
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
475 476
        }

G
groot 已提交
477 478 479 480 481
        if(top_k_ <= 0) {
            return SetError(SERVER_INVALID_TOPK, "Invalid topk: " + std::to_string(top_k_));
        }
        if(record_array_.empty()) {
            return SetError(SERVER_INVALID_ROWRECORD_ARRAY, "Row record array is empty");
G
groot 已提交
482 483
        }

G
groot 已提交
484
        //step 2: check table existence
G
groot 已提交
485
        engine::meta::TableSchema table_info;
G
groot 已提交
486
        table_info.table_id_ = table_name_;
G
groot 已提交
487
        engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
G
groot 已提交
488
        if(!stat.ok()) {
G
groot 已提交
489 490 491 492 493
            if(stat.IsNotFound()) {
                return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name_ + " not exists");
            } else {
                return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
            }
G
groot 已提交
494 495
        }

G
groot 已提交
496 497
        //step 3: check date range, and convert to db dates
        std::vector<DB_DATE> dates;
G
groot 已提交
498 499 500 501 502
        ServerError error_code = SERVER_SUCCESS;
        std::string error_msg;
        ConvertTimeRangeToDBDates(range_array_, dates, error_code, error_msg);
        if(error_code != SERVER_SUCCESS) {
            return SetError(error_code, error_msg);
G
groot 已提交
503 504
        }

G
groot 已提交
505 506 507
        rc.Record("check validation");

        //step 3: prepare float data
G
groot 已提交
508
        std::vector<float> vec_f;
G
groot 已提交
509 510 511
        ConvertRowRecordToFloatArray(record_array_, table_info.dimension_, vec_f, error_code, error_msg);
        if(error_code != SERVER_SUCCESS) {
            return SetError(error_code, error_msg);
G
groot 已提交
512 513 514 515
        }

        rc.Record("prepare vector data");

G
groot 已提交
516
        //step 4: search vectors
G
groot 已提交
517
        engine::QueryResults results;
G
groot 已提交
518
        uint64_t record_count = (uint64_t)record_array_.size();
519 520

        if(file_id_array_.empty()) {
G
groot 已提交
521
            stat = DBWrapper::DB()->Query(table_name_, (size_t) top_k_, record_count, vec_f.data(), dates, results);
522
        } else {
G
groot 已提交
523
            stat = DBWrapper::DB()->Query(table_name_, file_id_array_, (size_t) top_k_, record_count, vec_f.data(), dates, results);
524 525
        }

G
groot 已提交
526
        rc.Record("search vectors from engine");
G
groot 已提交
527
        if(!stat.ok()) {
G
groot 已提交
528 529 530 531 532
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
        }

        if(results.empty()) {
            return SERVER_SUCCESS; //empty table
G
groot 已提交
533 534 535
        }

        if(results.size() != record_count) {
G
groot 已提交
536 537 538
            std::string msg = "Search " + std::to_string(record_count) + " vectors but only return "
                              + std::to_string(results.size()) + " results";
            return SetError(SERVER_ILLEGAL_SEARCH_RESULT, msg);
G
groot 已提交
539 540
        }

G
groot 已提交
541 542 543
        rc.Record("do search");

        //step 5: construct result array
G
groot 已提交
544 545 546 547 548
        for(uint64_t i = 0; i < record_count; i++) {
            auto& result = results[i];
            const auto& record = record_array_[i];

            thrift::TopKQueryResult thrift_topk_result;
G
groot 已提交
549
            for(auto& pair : result) {
G
groot 已提交
550
                thrift::QueryResult thrift_result;
G
groot 已提交
551
                thrift_result.__set_id(pair.first);
J
jinhai 已提交
552
                thrift_result.__set_distance(pair.second);
G
groot 已提交
553 554

                thrift_topk_result.query_result_arrays.emplace_back(thrift_result);
G
groot 已提交
555 556
            }

G
groot 已提交
557 558 559
            result_array_.emplace_back(thrift_topk_result);
        }
        rc.Record("construct result");
G
groot 已提交
560
        rc.Elapse("totally cost");
561

G
groot 已提交
562
    } catch (std::exception& ex) {
G
groot 已提交
563
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
564 565 566 567 568
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
569 570
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GetTableRowCountTask::GetTableRowCountTask(const std::string& table_name, int64_t& row_count)
G
groot 已提交
571
: BaseTask(DDL_DML_TASK_GROUP),
G
groot 已提交
572 573 574 575 576 577 578 579 580 581
  table_name_(table_name),
  row_count_(row_count) {

}

BaseTaskPtr GetTableRowCountTask::Create(const std::string& table_name, int64_t& row_count) {
    return std::shared_ptr<BaseTask>(new GetTableRowCountTask(table_name, row_count));
}

ServerError GetTableRowCountTask::OnExecute() {
G
groot 已提交
582 583 584
    try {
        TimeRecorder rc("GetTableRowCountTask");

G
groot 已提交
585
        //step 1: check arguments
G
groot 已提交
586
        if (table_name_.empty()) {
G
groot 已提交
587
            return SetError(SERVER_INVALID_TABLE_NAME, "Empty table name");
G
groot 已提交
588 589 590 591
        }

        //step 2: get row count
        uint64_t row_count = 0;
G
groot 已提交
592
        engine::Status stat = DBWrapper::DB()->GetTableRowCount(table_name_, row_count);
G
groot 已提交
593
        if (!stat.ok()) {
G
groot 已提交
594
            return SetError(DB_META_TRANSACTION_FAILED, "Engine failed: " + stat.ToString());
G
groot 已提交
595 596 597 598 599 600 601
        }

        row_count_ = (int64_t) row_count;

        rc.Elapse("totally cost");

    } catch (std::exception& ex) {
G
groot 已提交
602
        return SetError(SERVER_UNEXPECTED_ERROR, ex.what());
G
groot 已提交
603 604
    }

G
groot 已提交
605
    return SERVER_SUCCESS;
G
groot 已提交
606 607
}

G
groot 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PingTask::PingTask(const std::string& cmd, std::string& result)
    : BaseTask(PING_TASK_GROUP),
      cmd_(cmd),
      result_(result) {

}

BaseTaskPtr PingTask::Create(const std::string& cmd, std::string& result) {
    return std::shared_ptr<BaseTask>(new PingTask(cmd, result));
}

ServerError PingTask::OnExecute() {
    if(cmd_ == "version") {
G
groot 已提交
622
        result_ = MILVUS_VERSION;
G
groot 已提交
623 624 625 626 627
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
628 629 630
}
}
}