MemManager.h 2.0 KB
Newer Older
1 2 3 4 5
#ifndef STORAGE_VECENGINE_MEMVECTORS_H_
#define STORAGE_VECENGINE_MEMVECTORS_H_

#include <map>
#include <string>
X
Xu Peng 已提交
6
#include <ctime>
X
Xu Peng 已提交
7 8
#include <memory>
#include <mutex>
X
Xu Peng 已提交
9 10
#include "IDGenerator.h"
#include "Status.h"
11

X
Xu Peng 已提交
12 13 14
namespace faiss {
    class Index;
}
15 16


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

21 22 23 24
namespace meta {
    class Meta;
}

25 26
class MemVectors {
public:
27 28 29
    explicit MemVectors(const std::string& group_id,
            size_t dimension,
            const std::string& file_location);
30

X
Xu Peng 已提交
31
    void add(size_t n_, const float* vectors_, IDNumbers& vector_ids_);
32 33 34 35 36

    size_t total() const;

    size_t approximate_size() const;

37
    Status serialize(std::string& group_id);
38 39 40

    ~MemVectors();

X
Xu Peng 已提交
41 42
    const std::string& location() const { return _file_location; }

43
private:
X
Xu Peng 已提交
44 45 46 47
    MemVectors() = delete;
    MemVectors(const MemVectors&) = delete;
    MemVectors& operator=(const MemVectors&) = delete;

48
    std::string group_id_;
X
Xu Peng 已提交
49
    const std::string _file_location;
50 51
    IDGenerator* _pIdGenerator;
    size_t _dimension;
52
    faiss::Index* pIndex_;
53 54 55 56

}; // MemVectors


X
Xu Peng 已提交
57
typedef std::shared_ptr<MemVectors> VectorsPtr;
58

59 60
class MemManager {
public:
61
    MemManager(const std::shared_ptr<meta::Meta>& meta_)
X
Xu Peng 已提交
62
        : _pMeta(meta_) /*_last_compact_time(std::time(nullptr))*/ {}
63

X
Xu Peng 已提交
64
    VectorsPtr get_mem_by_group(const std::string& group_id_);
65

66 67 68
    Status add_vectors(const std::string& group_id_,
            size_t n_, const float* vectors_, IDNumbers& vector_ids_);

69
    Status serialize(std::vector<std::string>& group_ids);
X
Xu Peng 已提交
70

71
private:
72 73
    Status add_vectors_no_lock(const std::string& group_id_,
            size_t n_, const float* vectors_, IDNumbers& vector_ids_);
X
Xu Peng 已提交
74
    Status mark_memory_as_immutable();
75

X
Xu Peng 已提交
76 77
    typedef std::map<std::string, VectorsPtr> MemMap;
    typedef std::vector<VectorsPtr> ImmMemPool;
78
    MemMap _memMap;
X
Xu Peng 已提交
79
    ImmMemPool _immMems;
80
    std::shared_ptr<meta::Meta> _pMeta;
X
Xu Peng 已提交
81
    /* std::time_t _last_compact_time; */
X
Xu Peng 已提交
82
    std::mutex _mutex;
83 84 85
}; // MemManager


X
Xu Peng 已提交
86 87 88
} // namespace engine
} // namespace vecwise
} // namespace zilliz
89 90

#endif