DBImpl.h 2.6 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 "DB.h"
#include "MemManager.h"
X
Xu Peng 已提交
10
#include "Types.h"
11
#include "Traits.h"
X
Xu Peng 已提交
12

13 14 15 16 17
#include <mutex>
#include <condition_variable>
#include <memory>
#include <atomic>

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::TableSchema& table_schema) override;
    virtual Status get_group(meta::TableSchema& table_schema) override;
X
Xu Peng 已提交
38
    virtual Status has_group(const std::string& table_id, bool& has_or_not) override;
39

X
Xu Peng 已提交
40 41
    virtual Status add_vectors(const std::string& table_id,
            size_t n, const float* vectors, IDNumbers& vector_ids) override;
X
Xu Peng 已提交
42

43
    virtual Status search(const std::string& table_id, size_t k, size_t nq,
X
Xu Peng 已提交
44 45
            const float* vectors, QueryResults& results) override;

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

X
Xu Peng 已提交
49 50
    virtual Status drop_all() override;

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

X
Xu Peng 已提交
53
    virtual ~DBImpl();
54

X
Xu Peng 已提交
55
private:
56

X
Xu Peng 已提交
57 58 59 60
    void BackgroundBuildIndex();
    Status BuildIndex(const meta::TableFileSchema&);
    Status TryBuildIndex();
    Status MergeFiles(const std::string& table_id,
61
            const meta::DateT& date,
62
            const meta::TableFilesSchema& files);
X
Xu Peng 已提交
63
    Status BackgroundMergeFiles(const std::string& table_id);
X
Xu Peng 已提交
64

X
Xu Peng 已提交
65 66 67
    void TrySchedule();
    void StartTimerTasks(int interval);
    void BackgroundTimerTask(int interval);
68

X
Xu Peng 已提交
69
    static void BGWork(void* db);
X
Xu Peng 已提交
70 71
    void BackgroundCall();
    void BackgroundCompaction();
X
Xu Peng 已提交
72

X
Xu Peng 已提交
73 74
    Env* const env_;
    const Options options_;
X
Xu Peng 已提交
75

X
Xu Peng 已提交
76 77 78 79 80
    std::mutex mutex_;
    std::condition_variable bg_work_finish_signal_;
    bool bg_compaction_scheduled_;
    Status bg_error_;
    std::atomic<bool> shutting_down_;
X
Xu Peng 已提交
81

X
Xu Peng 已提交
82
    std::mutex build_index_mutex_;
X
Xu Peng 已提交
83
    bool bg_build_index_started_;
X
Xu Peng 已提交
84
    std::condition_variable bg_build_index_finish_signal_;
X
Xu Peng 已提交
85

X
Xu Peng 已提交
86 87
    std::thread bg_timer_thread_;

X
Xu Peng 已提交
88 89
    MetaPtr pMeta_;
    MemManagerPtr pMemMgr_;
X
Xu Peng 已提交
90

X
Xu Peng 已提交
91 92
}; // DBImpl

93

X
Xu Peng 已提交
94 95 96
} // namespace engine
} // namespace vecwise
} // namespace zilliz
97

98
#include "DBImpl.inl"