MemManager.cpp 4.1 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
6 7 8
#ifndef MEMMANGE_CPP__
#define MEMMANGE_CPP__

9
#include <iostream>
X
Xu Peng 已提交
10 11
#include <sstream>
#include <thread>
X
Xu Peng 已提交
12
#include <easylogging++.h>
13

X
Xu Peng 已提交
14 15
#include "MemManager.h"
#include "Meta.h"
16 17


X
Xu Peng 已提交
18 19 20
namespace zilliz {
namespace vecwise {
namespace engine {
21

22 23
template<typename EngineT>
MemVectors<EngineT>::MemVectors(const std::shared_ptr<meta::Meta>& meta_ptr,
X
Xu Peng 已提交
24 25 26 27
        const meta::GroupFileSchema& schema, const Options& options)
  : pMeta_(meta_ptr),
    options_(options),
    schema_(schema),
28
    _pIdGenerator(new SimpleIDGenerator()),
29
    pEE_(new EngineT(schema_.dimension, schema_.location)) {
30 31
}

32 33
template<typename EngineT>
void MemVectors<EngineT>::add(size_t n_, const float* vectors_, IDNumbers& vector_ids_) {
X
Xu Peng 已提交
34
    _pIdGenerator->getNextIDNumbers(n_, vector_ids_);
X
Xu Peng 已提交
35
    pEE_->AddWithIds(n_, vectors_, vector_ids_.data());
36 37
}

38 39
template<typename EngineT>
size_t MemVectors<EngineT>::total() const {
X
Xu Peng 已提交
40
    return pEE_->Count();
41 42
}

43 44
template<typename EngineT>
size_t MemVectors<EngineT>::approximate_size() const {
X
Xu Peng 已提交
45
    return pEE_->Size();
46 47
}

48 49
template<typename EngineT>
Status MemVectors<EngineT>::serialize(std::string& group_id) {
X
Xu Peng 已提交
50 51
    group_id = schema_.group_id;
    auto rows = approximate_size();
X
Xu Peng 已提交
52
    pEE_->Serialize();
X
Xu Peng 已提交
53 54 55
    schema_.rows = rows;
    schema_.file_type = (rows >= options_.index_trigger_size) ?
        meta::GroupFileSchema::TO_INDEX : meta::GroupFileSchema::RAW;
X
Xu Peng 已提交
56 57

    auto status = pMeta_->update_group_file(schema_);
X
Xu Peng 已提交
58

X
Xu Peng 已提交
59 60 61
    LOG(DEBUG) << "New " << ((schema_.file_type == meta::GroupFileSchema::RAW) ? "raw" : "to_index")
        << " file " << schema_.file_id << " of size " << pEE_->PhysicalSize() / (1024*1024) << " M";

X
Xu Peng 已提交
62
    pEE_->Cache();
X
Xu Peng 已提交
63

X
Xu Peng 已提交
64
    return status;
65 66
}

67 68
template<typename EngineT>
MemVectors<EngineT>::~MemVectors() {
69 70 71 72 73 74 75 76 77 78
    if (_pIdGenerator != nullptr) {
        delete _pIdGenerator;
        _pIdGenerator = nullptr;
    }
}

/*
 * MemManager
 */

79
template<typename EngineT>
80
typename MemManager<EngineT>::MemVectorsPtr MemManager<EngineT>::get_mem_by_group(
81
        const std::string& group_id) {
X
Xu Peng 已提交
82 83 84
    auto memIt = _memMap.find(group_id);
    if (memIt != _memMap.end()) {
        return memIt->second;
85
    }
86

87
    meta::GroupFileSchema group_file;
X
Xu Peng 已提交
88 89
    group_file.group_id = group_id;
    auto status = _pMeta->add_group_file(group_file);
90 91 92
    if (!status.ok()) {
        return nullptr;
    }
X
Xu Peng 已提交
93

94
    _memMap[group_id] = MemVectorsPtr(new MemVectors<EngineT>(_pMeta, group_file, options_));
X
Xu Peng 已提交
95
    return _memMap[group_id];
96 97
}

98 99
template<typename EngineT>
Status MemManager<EngineT>::add_vectors(const std::string& group_id_,
100 101 102
        size_t n_,
        const float* vectors_,
        IDNumbers& vector_ids_) {
103
    std::unique_lock<std::mutex> lock(_mutex);
104
    return add_vectors_no_lock(group_id_, n_, vectors_, vector_ids_);
105 106
}

107 108
template<typename EngineT>
Status MemManager<EngineT>::add_vectors_no_lock(const std::string& group_id,
109
        size_t n,
110
        const float* vectors,
X
Xu Peng 已提交
111
        IDNumbers& vector_ids) {
112
    MemVectorsPtr mem = get_mem_by_group(group_id);
113
    if (mem == nullptr) {
X
Xu Peng 已提交
114
        return Status::NotFound("Group " + group_id + " not found!");
115
    }
X
Xu Peng 已提交
116 117 118
    mem->add(n, vectors, vector_ids);

    return Status::OK();
119 120
}

121 122
template<typename EngineT>
Status MemManager<EngineT>::mark_memory_as_immutable() {
123
    std::unique_lock<std::mutex> lock(_mutex);
X
Xu Peng 已提交
124 125 126
    for (auto& kv: _memMap) {
        _immMems.push_back(kv.second);
    }
X
Xu Peng 已提交
127

X
Xu Peng 已提交
128
    _memMap.clear();
129
    return Status::OK();
X
Xu Peng 已提交
130 131
}

132 133
template<typename EngineT>
Status MemManager<EngineT>::serialize(std::vector<std::string>& group_ids) {
X
Xu Peng 已提交
134
    mark_memory_as_immutable();
X
Xu Peng 已提交
135
    std::unique_lock<std::mutex> lock(serialization_mtx_);
136 137
    std::string group_id;
    group_ids.clear();
X
Xu Peng 已提交
138
    for (auto& mem : _immMems) {
139 140
        mem->serialize(group_id);
        group_ids.push_back(group_id);
X
Xu Peng 已提交
141 142
    }
    _immMems.clear();
143
    return Status::OK();
X
Xu Peng 已提交
144 145
}

146

X
Xu Peng 已提交
147 148 149
} // namespace engine
} // namespace vecwise
} // namespace zilliz
150 151

#endif