MemManager.h 2.5 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

8 9 10 11
#include "IDGenerator.h"
#include "Status.h"
#include "Meta.h"

12 13
#include <map>
#include <string>
X
Xu Peng 已提交
14
#include <ctime>
X
Xu Peng 已提交
15 16
#include <memory>
#include <mutex>
17

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

22 23 24 25
namespace meta {
    class Meta;
}

26
template <typename EngineT>
27 28
class MemVectors {
public:
X
Xu Peng 已提交
29 30 31
    using EnginePtr = typename EngineT::Ptr;
    using MetaPtr = meta::Meta::Ptr;
    using Ptr = std::shared_ptr<MemVectors<EngineT>>;
X
Xu Peng 已提交
32

X
Xu Peng 已提交
33
    explicit MemVectors(const std::shared_ptr<meta::Meta>&,
34
            const meta::TableFileSchema&, const Options&);
35

X
Xu Peng 已提交
36
    void Add(size_t n_, const float* vectors_, IDNumbers& vector_ids_);
37

X
Xu Peng 已提交
38
    size_t Total() const;
39

X
Xu Peng 已提交
40
    size_t ApproximateSize() const;
41

X
Xu Peng 已提交
42
    Status Serialize(std::string& table_id);
43 44 45

    ~MemVectors();

X
Xu Peng 已提交
46
    const std::string& Location() const { return schema_.location; }
X
Xu Peng 已提交
47

48
private:
X
Xu Peng 已提交
49 50 51 52
    MemVectors() = delete;
    MemVectors(const MemVectors&) = delete;
    MemVectors& operator=(const MemVectors&) = delete;

X
Xu Peng 已提交
53
    MetaPtr pMeta_;
X
Xu Peng 已提交
54
    Options options_;
55
    meta::TableFileSchema schema_;
X
Xu Peng 已提交
56
    IDGenerator* pIdGenerator_;
X
Xu Peng 已提交
57
    EnginePtr pEE_;
58 59 60 61

}; // MemVectors


62

63
template<typename EngineT>
64 65
class MemManager {
public:
X
Xu Peng 已提交
66 67 68
    using MetaPtr = meta::Meta::Ptr;
    using MemVectorsPtr = typename MemVectors<EngineT>::Ptr;
    using Ptr = std::shared_ptr<MemManager<EngineT>>;
69

X
Xu Peng 已提交
70 71
    MemManager(const std::shared_ptr<meta::Meta>& meta, const Options& options)
        : pMeta_(meta), options_(options) {}
72

X
Xu Peng 已提交
73
    MemVectorsPtr GetMemByTable(const std::string& table_id);
74

X
Xu Peng 已提交
75 76
    Status InsertVectors(const std::string& table_id,
            size_t n, const float* vectors, IDNumbers& vector_ids);
77

X
Xu Peng 已提交
78
    Status Serialize(std::vector<std::string>& table_ids);
X
Xu Peng 已提交
79

80
private:
X
Xu Peng 已提交
81 82 83 84 85 86 87 88 89
    Status InsertVectorsNoLock(const std::string& table_id,
            size_t n, const float* vectors, IDNumbers& vector_ids);
    Status ToImmutable();

    using MemMap = std::map<std::string, MemVectorsPtr>;
    using ImmMemPool = std::vector<MemVectorsPtr>;
    MemMap memMap_;
    ImmMemPool immMems_;
    MetaPtr pMeta_;
X
Xu Peng 已提交
90
    Options options_;
X
Xu Peng 已提交
91
    std::mutex mutex_;
X
Xu Peng 已提交
92
    std::mutex serialization_mtx_;
93 94 95
}; // MemManager


X
Xu Peng 已提交
96 97 98
} // namespace engine
} // namespace vecwise
} // namespace zilliz
99
#include "MemManager.inl"