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

G
groot 已提交
8
#include "ExecutionEngine.h"
9 10 11
#include "IDGenerator.h"
#include "Status.h"
#include "Meta.h"
Z
zhiru 已提交
12
#include "MemManagerAbstract.h"
13

14 15
#include <map>
#include <string>
X
Xu Peng 已提交
16
#include <ctime>
X
Xu Peng 已提交
17 18
#include <memory>
#include <mutex>
G
groot 已提交
19
#include <set>
20

Z
update  
zhiru 已提交
21

X
Xu Peng 已提交
22
namespace zilliz {
J
jinhai 已提交
23
namespace milvus {
X
Xu Peng 已提交
24
namespace engine {
25

26
namespace meta {
Z
update  
zhiru 已提交
27
class Meta;
28 29
}

30
class MemVectors {
Z
update  
zhiru 已提交
31
 public:
X
Xu Peng 已提交
32
    using MetaPtr = meta::Meta::Ptr;
G
groot 已提交
33
    using Ptr = std::shared_ptr<MemVectors>;
X
Xu Peng 已提交
34

Z
update  
zhiru 已提交
35 36
    explicit MemVectors(const std::shared_ptr<meta::Meta> &,
                        const meta::TableFileSchema &, const Options &);
37

Z
update  
zhiru 已提交
38
    Status Add(size_t n_, const float *vectors_, IDNumbers &vector_ids_);
39

G
groot 已提交
40
    size_t RowCount() const;
41

G
groot 已提交
42
    size_t Size() const;
43

Z
update  
zhiru 已提交
44
    Status Serialize(std::string &table_id);
45 46 47

    ~MemVectors();

Z
update  
zhiru 已提交
48
    const std::string &Location() const { return schema_.location_; }
X
Xu Peng 已提交
49

G
groot 已提交
50 51
    std::string TableId() const { return schema_.table_id_; }

Z
update  
zhiru 已提交
52
 private:
X
Xu Peng 已提交
53
    MemVectors() = delete;
Z
update  
zhiru 已提交
54 55
    MemVectors(const MemVectors &) = delete;
    MemVectors &operator=(const MemVectors &) = delete;
X
Xu Peng 已提交
56

G
groot 已提交
57
    MetaPtr meta_;
X
Xu Peng 已提交
58
    Options options_;
59
    meta::TableFileSchema schema_;
Z
update  
zhiru 已提交
60
    IDGenerator *id_generator_;
G
groot 已提交
61
    ExecutionEnginePtr active_engine_;
62 63 64 65

}; // MemVectors


66

Z
zhiru 已提交
67
class MemManager : public MemManagerAbstract {
Z
update  
zhiru 已提交
68
 public:
X
Xu Peng 已提交
69
    using MetaPtr = meta::Meta::Ptr;
G
groot 已提交
70 71
    using MemVectorsPtr = typename MemVectors::Ptr;
    using Ptr = std::shared_ptr<MemManager>;
72

Z
update  
zhiru 已提交
73
    MemManager(const std::shared_ptr<meta::Meta> &meta, const Options &options)
G
groot 已提交
74
        : meta_(meta), options_(options) {}
75

Z
update  
zhiru 已提交
76 77
    Status InsertVectors(const std::string &table_id,
                         size_t n, const float *vectors, IDNumbers &vector_ids) override;
78

Z
update  
zhiru 已提交
79
    Status Serialize(std::set<std::string> &table_ids) override;
X
Xu Peng 已提交
80

Z
update  
zhiru 已提交
81
    Status EraseMemVector(const std::string &table_id) override;
G
groot 已提交
82

Z
zhiru 已提交
83
    size_t GetCurrentMutableMem() override;
84

Z
zhiru 已提交
85
    size_t GetCurrentImmutableMem() override;
86

Z
zhiru 已提交
87
    size_t GetCurrentMem() override;
X
Xu Peng 已提交
88

Z
update  
zhiru 已提交
89 90
 private:
    MemVectorsPtr GetMemByTable(const std::string &table_id);
G
groot 已提交
91

Z
update  
zhiru 已提交
92 93
    Status InsertVectorsNoLock(const std::string &table_id,
                               size_t n, const float *vectors, IDNumbers &vector_ids);
X
Xu Peng 已提交
94 95
    Status ToImmutable();

G
groot 已提交
96 97 98 99 100
    using MemIdMap = std::map<std::string, MemVectorsPtr>;
    using MemList = std::vector<MemVectorsPtr>;
    MemIdMap mem_id_map_;
    MemList immu_mem_list_;
    MetaPtr meta_;
X
Xu Peng 已提交
101
    Options options_;
X
Xu Peng 已提交
102
    std::mutex mutex_;
X
Xu Peng 已提交
103
    std::mutex serialization_mtx_;
104 105 106
}; // MemManager


X
Xu Peng 已提交
107
} // namespace engine
J
jinhai 已提交
108
} // namespace milvus
X
Xu Peng 已提交
109
} // namespace zilliz