MemTableFile.cpp 3.4 KB
Newer Older
Z
zhiru 已提交
1 2 3
#include "MemTableFile.h"
#include "Constants.h"
#include "Log.h"
Z
update  
zhiru 已提交
4
#include "EngineFactory.h"
Z
zhiru 已提交
5
#include "metrics/Metrics.h"
Z
zhiru 已提交
6 7 8 9 10 11 12 13

#include <cmath>

namespace zilliz {
namespace milvus {
namespace engine {

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

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

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;
    }
    else {
        std::string errMsg = "MemTableFile::CreateTableFile failed: " + status.ToString();
        ENGINE_LOG_ERROR << errMsg;
    }
    return status;
}

Status MemTableFile::Add(const VectorSource::Ptr& source) {

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

Z
zhiru 已提交
53 54 55 56 57
    size_t singleVectorMemSize = table_file_schema_.dimension_ * VECTOR_TYPE_SIZE;
    size_t memLeft = GetMemLeft();
    if (memLeft >= singleVectorMemSize) {
        size_t numVectorsToAdd = std::ceil(memLeft / singleVectorMemSize);
        size_t numVectorsAdded;
Z
update  
zhiru 已提交
58
        auto status = source->Add(execution_engine_, table_file_schema_, numVectorsToAdd, numVectorsAdded);
Z
zhiru 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        if (status.ok()) {
            current_mem_ += (numVectorsAdded * singleVectorMemSize);
        }
        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
zhiru 已提交
76 77 78 79
    size_t singleVectorMemSize = table_file_schema_.dimension_ * VECTOR_TYPE_SIZE;
    return (GetMemLeft() < singleVectorMemSize);
}

Z
zhiru 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;

    server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet((double)size/total_time);

    table_file_schema_.file_type_ = (size >= options_.index_trigger_size) ?
                         meta::TableFileSchema::TO_INDEX : meta::TableFileSchema::RAW;

    auto status = meta_->UpdateTableFile(table_file_schema_);

    LOG(DEBUG) << "New " << ((table_file_schema_.file_type_ == meta::TableFileSchema::RAW) ? "raw" : "to_index")
               << " file " << table_file_schema_.file_id_ << " of size " << (double)size / (double)M << " M";

    execution_engine_->Cache();

    return status;
}

Z
zhiru 已提交
106 107 108
} // namespace engine
} // namespace milvus
} // namespace zilliz