DBImpl.h 2.9 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;
38 39 40 41
    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_,
42
                                   meta::GroupFilesSchema& group_files_info_) override;
X
Xu Peng 已提交
43

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

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

X
Xu Peng 已提交
50 51 52
    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 已提交
53 54
    virtual Status drop_all() override;

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

X
Xu Peng 已提交
57
    virtual ~DBImpl();
58

X
Xu Peng 已提交
59
private:
60

X
Xu Peng 已提交
61
    void background_build_index();
X
Xu Peng 已提交
62 63
    Status build_index(const meta::GroupFileSchema&);
    Status try_build_index();
64 65 66 67
    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 已提交
68

69
    void try_schedule_compaction();
X
Xu Peng 已提交
70 71
    void start_timer_task(int interval_);
    void background_timer_task(int interval_);
72

X
Xu Peng 已提交
73 74 75 76
    static void BGWork(void* db);
    void background_call();
    void background_compaction();

X
Xu Peng 已提交
77 78 79
    Env* const _env;
    const Options _options;

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

X
Xu Peng 已提交
86
    std::mutex build_index_mutex_;
X
Xu Peng 已提交
87
    bool bg_build_index_started_;
X
Xu Peng 已提交
88
    std::condition_variable bg_build_index_finish_signal_;
X
Xu Peng 已提交
89

X
Xu Peng 已提交
90
    MetaPtr _pMeta;
91
    MemManagerPtr _pMemMgr;
X
Xu Peng 已提交
92

X
Xu Peng 已提交
93 94
}; // DBImpl

95

X
Xu Peng 已提交
96 97 98
} // namespace engine
} // namespace vecwise
} // namespace zilliz
99 100

#include "DBImpl.cpp"