DBImpl.h 3.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.
 ******************************************************************************/
X
Xu Peng 已提交
6
#pragma once
X
Xu Peng 已提交
7

X
Xu Peng 已提交
8 9
#include <mutex>
#include <condition_variable>
X
Xu Peng 已提交
10
#include <memory>
X
Xu Peng 已提交
11
#include <atomic>
X
Xu Peng 已提交
12 13
#include "DB.h"
#include "MemManager.h"
X
Xu Peng 已提交
14
#include "Types.h"
15 16
#include "FaissExecutionEngine.h"
#include "Traits.h"
X
Xu Peng 已提交
17

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

class Env;

24 25 26 27
namespace meta {
    class Meta;
}

28
template <typename EngineT>
X
Xu Peng 已提交
29 30
class DBImpl : public DB {
public:
X
Xu Peng 已提交
31
    typedef typename meta::Meta::Ptr MetaPtr;
32
    typedef typename MemManager<EngineT>::Ptr MemManagerPtr;
X
Xu Peng 已提交
33

X
Xu Peng 已提交
34
    DBImpl(const Options& options);
X
Xu Peng 已提交
35

36 37
    virtual Status add_group(meta::GroupSchema& group_info) override;
    virtual Status get_group(meta::GroupSchema& group_info) override;
X
Xu Peng 已提交
38
    virtual Status delete_vectors(const std::string& group_id, const meta::DatesT& dates) override;
39 40 41 42
    virtual Status has_group(const std::string& group_id_, bool& has_or_not_) override;

    virtual Status get_group_files(const std::string& group_id_,
                                   const int date_delta_,
43
                                   meta::GroupFilesSchema& group_files_info_) override;
X
Xu Peng 已提交
44

45 46
    virtual Status add_vectors(const std::string& group_id_,
            size_t n, const float* vectors, IDNumbers& vector_ids_) override;
X
Xu Peng 已提交
47

X
Xu Peng 已提交
48 49 50
    virtual Status search(const std::string& group_id, size_t k, size_t nq,
            const float* vectors, QueryResults& results) override;

X
Xu Peng 已提交
51 52 53
    virtual Status search(const std::string& group_id, size_t k, size_t nq,
            const float* vectors, const meta::DatesT& dates, QueryResults& results) override;

X
Xu Peng 已提交
54 55
    virtual Status drop_all() override;

X
Xu Peng 已提交
56 57
    virtual Status count(const std::string& group_id, long& result) override;

X
Xu Peng 已提交
58 59
    virtual Status size(long& result) override;

X
Xu Peng 已提交
60
    virtual ~DBImpl();
61

X
Xu Peng 已提交
62
private:
63

X
Xu Peng 已提交
64
    void background_build_index();
X
Xu Peng 已提交
65 66
    Status build_index(const meta::GroupFileSchema&);
    Status try_build_index();
67 68 69 70
    Status merge_files(const std::string& group_id,
            const meta::DateT& date,
            const meta::GroupFilesSchema& files);
    Status background_merge_files(const std::string& group_id);
X
Xu Peng 已提交
71

72
    void try_schedule_compaction();
X
Xu Peng 已提交
73 74
    void start_timer_task(int interval_);
    void background_timer_task(int interval_);
75

X
Xu Peng 已提交
76 77 78 79
    static void BGWork(void* db);
    void background_call();
    void background_compaction();

X
Xu Peng 已提交
80 81 82
    Env* const _env;
    const Options _options;

X
Xu Peng 已提交
83 84 85 86
    std::mutex _mutex;
    std::condition_variable _bg_work_finish_signal;
    bool _bg_compaction_scheduled;
    Status _bg_error;
X
Xu Peng 已提交
87
    std::atomic<bool> _shutting_down;
X
Xu Peng 已提交
88

X
Xu Peng 已提交
89
    std::mutex build_index_mutex_;
X
Xu Peng 已提交
90
    bool bg_build_index_started_;
X
Xu Peng 已提交
91
    std::condition_variable bg_build_index_finish_signal_;
X
Xu Peng 已提交
92

X
Xu Peng 已提交
93 94
    std::thread bg_timer_thread_;

X
Xu Peng 已提交
95
    MetaPtr _pMeta;
96
    MemManagerPtr _pMemMgr;
X
Xu Peng 已提交
97

X
Xu Peng 已提交
98 99
}; // DBImpl

100

X
Xu Peng 已提交
101 102 103
} // namespace engine
} // namespace vecwise
} // namespace zilliz
104 105

#include "DBImpl.cpp"