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

12 13 14 15
#include <mutex>
#include <condition_variable>
#include <memory>
#include <atomic>
G
groot 已提交
16
#include <thread>
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;
}

X
Xu Peng 已提交
28 29
class DBImpl : public DB {
public:
30
    using MetaPtr = meta::Meta::Ptr;
G
groot 已提交
31
    using MemManagerPtr = typename MemManager::Ptr;
X
Xu Peng 已提交
32

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

X
Xu Peng 已提交
35 36 37
    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;
38

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

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

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

X
Xu Peng 已提交
48
    virtual Status DropAll() override;
X
Xu Peng 已提交
49

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

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

X
Xu Peng 已提交
54
private:
G
groot 已提交
55 56 57 58 59 60
    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);

61

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

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

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

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

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

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

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

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

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

98

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