VectorSource.cpp 2.2 KB
Newer Older
Z
zhiru 已提交
1 2 3 4
#include "VectorSource.h"
#include "ExecutionEngine.h"
#include "EngineFactory.h"
#include "Log.h"
Z
zhiru 已提交
5
#include "metrics/Metrics.h"
Z
zhiru 已提交
6

Z
update  
zhiru 已提交
7

Z
zhiru 已提交
8 9 10 11 12 13 14
namespace zilliz {
namespace milvus {
namespace engine {


VectorSource::VectorSource(const size_t &n,
                           const float *vectors) :
Z
update  
zhiru 已提交
15 16 17
    n_(n),
    vectors_(vectors),
    id_generator_(new SimpleIDGenerator()) {
Z
zhiru 已提交
18 19 20
    current_num_vectors_added = 0;
}

Z
update  
zhiru 已提交
21 22 23 24
Status VectorSource::Add(const ExecutionEnginePtr &execution_engine,
                         const meta::TableFileSchema &table_file_schema,
                         const size_t &num_vectors_to_add,
                         size_t &num_vectors_added) {
Z
zhiru 已提交
25

Z
zhiru 已提交
26
    auto start_time = METRICS_NOW_TIME;
Z
zhiru 已提交
27

Z
zhiru 已提交
28 29
    num_vectors_added = current_num_vectors_added + num_vectors_to_add <= n_ ?
                        num_vectors_to_add : n_ - current_num_vectors_added;
Z
zhiru 已提交
30 31
    IDNumbers vector_ids_to_add;
    id_generator_->GetNextIDNumbers(num_vectors_added, vector_ids_to_add);
Z
zhiru 已提交
32 33 34
    Status status = execution_engine->AddWithIds(num_vectors_added,
                                                 vectors_ + current_num_vectors_added * table_file_schema.dimension_,
                                                 vector_ids_to_add.data());
Z
zhiru 已提交
35 36
    if (status.ok()) {
        current_num_vectors_added += num_vectors_added;
Z
zhiru 已提交
37 38 39
        vector_ids_.insert(vector_ids_.end(),
                           std::make_move_iterator(vector_ids_to_add.begin()),
                           std::make_move_iterator(vector_ids_to_add.end()));
Z
update  
zhiru 已提交
40
    } else {
Z
zhiru 已提交
41 42 43
        ENGINE_LOG_ERROR << "VectorSource::Add failed: " + status.ToString();
    }

Z
zhiru 已提交
44 45
    auto end_time = METRICS_NOW_TIME;
    auto total_time = METRICS_MICROSECONDS(start_time, end_time);
Z
zhiru 已提交
46 47 48
    server::Metrics::GetInstance().AddVectorsPerSecondGaugeSet(static_cast<int>(n_),
                                                               static_cast<int>(table_file_schema.dimension_),
                                                               total_time);
Z
zhiru 已提交
49

Z
zhiru 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    return status;
}

size_t VectorSource::GetNumVectorsAdded() {
    return current_num_vectors_added;
}

bool VectorSource::AllAdded() {
    return (current_num_vectors_added == n_);
}

IDNumbers VectorSource::GetVectorIds() {
    return vector_ids_;
}

} // namespace engine
} // namespace milvus
} // namespace zilliz