DBImpl.h 3.0 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:
31 32
    using MetaPtr = meta::Meta::Ptr;
    using MemManagerPtr = typename MemManager<EngineT>::Ptr;
X
Xu Peng 已提交
33

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

X
Xu Peng 已提交
36 37 38
    virtual Status CreateTable(meta::TableSchema& table_schema) override;
    virtual Status DescribeTable(meta::TableSchema& table_schema) override;
    virtual Status HasTable(const std::string& table_id, bool& has_or_not) override;
39

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

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

X
Xu Peng 已提交
46
    virtual Status Query(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
    virtual Status DropAll() override;
X
Xu Peng 已提交
50

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

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

X
Xu Peng 已提交
55
private:
G
groot 已提交
56 57 58 59 60 61
    Status QuerySync(const std::string& table_id, size_t k, size_t nq,
            const float* vectors, const meta::DatesT& dates, QueryResults& results);

    Status QueryAsync(const std::string& table_id, size_t k, size_t nq,
            const float* vectors, const meta::DatesT& dates, QueryResults& results);

62

X
Xu Peng 已提交
63 64 65 66
    void BackgroundBuildIndex();
    Status BuildIndex(const meta::TableFileSchema&);
    Status TryBuildIndex();
    Status MergeFiles(const std::string& table_id,
67
            const meta::DateT& date,
68
            const meta::TableFilesSchema& files);
X
Xu Peng 已提交
69
    Status BackgroundMergeFiles(const std::string& table_id);
X
Xu Peng 已提交
70

X
Xu Peng 已提交
71 72 73
    void TrySchedule();
    void StartTimerTasks(int interval);
    void BackgroundTimerTask(int interval);
74

X
Xu Peng 已提交
75
    static void BGWork(void* db);
X
Xu Peng 已提交
76 77
    void BackgroundCall();
    void BackgroundCompaction();
X
Xu Peng 已提交
78

X
Xu Peng 已提交
79 80
    Env* const env_;
    const Options options_;
X
Xu Peng 已提交
81

X
Xu Peng 已提交
82 83 84 85 86
    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 已提交
87

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

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

X
Xu Peng 已提交
94 95
    MetaPtr pMeta_;
    MemManagerPtr pMemMgr_;
X
Xu Peng 已提交
96

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

99

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

104
#include "DBImpl.inl"