MemTable.cpp 2.5 KB
Newer Older
Z
zhiru 已提交
1
#include "MemTable.h"
S
starlord 已提交
2
#include "db/Log.h"
Z
zhiru 已提交
3

Z
update  
zhiru 已提交
4

Z
zhiru 已提交
5 6 7 8
namespace zilliz {
namespace milvus {
namespace engine {

Z
update  
zhiru 已提交
9 10 11 12 13 14
MemTable::MemTable(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 已提交
15 16 17

}

Z
update  
zhiru 已提交
18 19
Status MemTable::Add(VectorSource::Ptr &source) {

Z
zhiru 已提交
20
    while (!source->AllAdded()) {
Z
update  
zhiru 已提交
21 22

        MemTableFile::Ptr current_mem_table_file;
Z
zhiru 已提交
23
        if (!mem_table_file_list_.empty()) {
Z
update  
zhiru 已提交
24
            current_mem_table_file = mem_table_file_list_.back();
Z
zhiru 已提交
25
        }
Z
update  
zhiru 已提交
26

Z
zhiru 已提交
27
        Status status;
Z
update  
zhiru 已提交
28 29 30
        if (mem_table_file_list_.empty() || current_mem_table_file->IsFull()) {
            MemTableFile::Ptr new_mem_table_file = std::make_shared<MemTableFile>(table_id_, meta_, options_);
            status = new_mem_table_file->Add(source);
Z
zhiru 已提交
31
            if (status.ok()) {
Z
update  
zhiru 已提交
32
                mem_table_file_list_.emplace_back(new_mem_table_file);
Z
zhiru 已提交
33
            }
Z
update  
zhiru 已提交
34 35
        } else {
            status = current_mem_table_file->Add(source);
Z
zhiru 已提交
36
        }
Z
update  
zhiru 已提交
37

Z
zhiru 已提交
38
        if (!status.ok()) {
Z
update  
zhiru 已提交
39 40 41
            std::string err_msg = "MemTable::Add failed: " + status.ToString();
            ENGINE_LOG_ERROR << err_msg;
            return Status::Error(err_msg);
Z
zhiru 已提交
42 43 44 45 46
        }
    }
    return Status::OK();
}

Z
update  
zhiru 已提交
47
void MemTable::GetCurrentMemTableFile(MemTableFile::Ptr &mem_table_file) {
Z
zhiru 已提交
48
    mem_table_file = mem_table_file_list_.back();
Z
zhiru 已提交
49 50
}

Z
zhiru 已提交
51
size_t MemTable::GetTableFileCount() {
Z
zhiru 已提交
52 53 54 55
    return mem_table_file_list_.size();
}

Status MemTable::Serialize() {
Z
update  
zhiru 已提交
56 57
    for (auto mem_table_file = mem_table_file_list_.begin(); mem_table_file != mem_table_file_list_.end();) {
        auto status = (*mem_table_file)->Serialize();
Z
zhiru 已提交
58
        if (!status.ok()) {
Z
update  
zhiru 已提交
59 60 61
            std::string err_msg = "MemTable::Serialize failed: " + status.ToString();
            ENGINE_LOG_ERROR << err_msg;
            return Status::Error(err_msg);
Z
zhiru 已提交
62
        }
Z
zhiru 已提交
63
        std::lock_guard<std::mutex> lock(mutex_);
Z
update  
zhiru 已提交
64
        mem_table_file = mem_table_file_list_.erase(mem_table_file);
Z
zhiru 已提交
65 66
    }
    return Status::OK();
Z
zhiru 已提交
67 68
}

Z
zhiru 已提交
69 70 71 72
bool MemTable::Empty() {
    return mem_table_file_list_.empty();
}

Z
update  
zhiru 已提交
73
const std::string &MemTable::GetTableId() const {
Z
zhiru 已提交
74 75 76
    return table_id_;
}

Z
zhiru 已提交
77 78
size_t MemTable::GetCurrentMem() {
    std::lock_guard<std::mutex> lock(mutex_);
Z
update  
zhiru 已提交
79 80 81
    size_t total_mem = 0;
    for (auto &mem_table_file : mem_table_file_list_) {
        total_mem += mem_table_file->GetCurrentMem();
Z
zhiru 已提交
82
    }
Z
update  
zhiru 已提交
83
    return total_mem;
Z
zhiru 已提交
84 85
}

Z
zhiru 已提交
86 87 88
} // namespace engine
} // namespace milvus
} // namespace zilliz