MemTableFile.cpp 3.5 KB
Newer Older
Z
zhiru 已提交
1
#include "MemTableFile.h"
S
starlord 已提交
2 3 4
#include "db/Constants.h"
#include "db/Log.h"
#include "db/engine/EngineFactory.h"
Z
zhiru 已提交
5
#include "metrics/Metrics.h"
Z
zhiru 已提交
6 7 8

#include <cmath>

Z
update  
zhiru 已提交
9

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

Z
update  
zhiru 已提交
14 15 16 17 18 19
MemTableFile::MemTableFile(const std::string &table_id,
                           const std::shared_ptr<meta::Meta> &meta,
                           const Options &options) :
    table_id_(table_id),
    meta_(meta),
    options_(options) {
Z
zhiru 已提交
20 21

    current_mem_ = 0;
Z
update  
zhiru 已提交
22 23 24 25
    auto status = CreateTableFile();
    if (status.ok()) {
        execution_engine_ = EngineFactory::Build(table_file_schema_.dimension_,
                                                 table_file_schema_.location_,
Z
update  
zhiru 已提交
26
                                                 (EngineType) table_file_schema_.engine_type_);
Z
update  
zhiru 已提交
27
    }
Z
zhiru 已提交
28 29 30 31 32 33 34 35 36
}

Status MemTableFile::CreateTableFile() {

    meta::TableFileSchema table_file_schema;
    table_file_schema.table_id_ = table_id_;
    auto status = meta_->CreateTableFile(table_file_schema);
    if (status.ok()) {
        table_file_schema_ = table_file_schema;
Z
update  
zhiru 已提交
37 38 39
    } else {
        std::string err_msg = "MemTableFile::CreateTableFile failed: " + status.ToString();
        ENGINE_LOG_ERROR << err_msg;
Z
zhiru 已提交
40 41 42 43
    }
    return status;
}

Y
Yu Kun 已提交
44
Status MemTableFile::Add(const VectorSource::Ptr &source, IDNumbers& vector_ids) {
Z
zhiru 已提交
45

Z
zhiru 已提交
46
    if (table_file_schema_.dimension_ <= 0) {
Z
update  
zhiru 已提交
47 48 49 50
        std::string err_msg = "MemTableFile::Add: table_file_schema dimension = " +
            std::to_string(table_file_schema_.dimension_) + ", table_id = " + table_file_schema_.table_id_;
        ENGINE_LOG_ERROR << err_msg;
        return Status::Error(err_msg);
Z
zhiru 已提交
51 52
    }

Z
update  
zhiru 已提交
53 54 55 56 57
    size_t single_vector_mem_size = table_file_schema_.dimension_ * VECTOR_TYPE_SIZE;
    size_t mem_left = GetMemLeft();
    if (mem_left >= single_vector_mem_size) {
        size_t num_vectors_to_add = std::ceil(mem_left / single_vector_mem_size);
        size_t num_vectors_added;
Y
Yu Kun 已提交
58
        auto status = source->Add(execution_engine_, table_file_schema_, num_vectors_to_add, num_vectors_added, vector_ids);
Z
zhiru 已提交
59
        if (status.ok()) {
Z
update  
zhiru 已提交
60
            current_mem_ += (num_vectors_added * single_vector_mem_size);
Z
zhiru 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
        }
        return status;
    }
    return Status::OK();
}

size_t MemTableFile::GetCurrentMem() {
    return current_mem_;
}

size_t MemTableFile::GetMemLeft() {
    return (MAX_TABLE_FILE_MEM - current_mem_);
}

Z
zhiru 已提交
75
bool MemTableFile::IsFull() {
Z
update  
zhiru 已提交
76 77
    size_t single_vector_mem_size = table_file_schema_.dimension_ * VECTOR_TYPE_SIZE;
    return (GetMemLeft() < single_vector_mem_size);
Z
zhiru 已提交
78 79
}

Z
zhiru 已提交
80 81 82 83 84 85 86 87 88 89 90
Status MemTableFile::Serialize() {

    auto start_time = METRICS_NOW_TIME;

    auto size = GetCurrentMem();

    execution_engine_->Serialize();
    auto end_time = METRICS_NOW_TIME;
    auto total_time = METRICS_MICROSECONDS(start_time, end_time);
    table_file_schema_.size_ = size;

Z
update  
zhiru 已提交
91
    server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet((double) size / total_time);
Z
zhiru 已提交
92 93

    table_file_schema_.file_type_ = (size >= options_.index_trigger_size) ?
Z
update  
zhiru 已提交
94
                                    meta::TableFileSchema::TO_INDEX : meta::TableFileSchema::RAW;
Z
zhiru 已提交
95 96 97

    auto status = meta_->UpdateTableFile(table_file_schema_);

S
starlord 已提交
98 99
    ENGINE_LOG_DEBUG << "New " << ((table_file_schema_.file_type_ == meta::TableFileSchema::RAW) ? "raw" : "to_index")
               << " file " << table_file_schema_.file_id_ << " of size " << size << " bytes";
Z
zhiru 已提交
100

S
starlord 已提交
101 102 103
    if(options_.insert_cache_immediately_) {
        execution_engine_->Cache();
    }
Z
zhiru 已提交
104 105 106 107

    return status;
}

Z
zhiru 已提交
108 109 110
} // namespace engine
} // namespace milvus
} // namespace zilliz