DBImpl.h 3.2 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
namespace zilliz {
J
jinhai 已提交
19
namespace milvus {
X
Xu Peng 已提交
20
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
    virtual Status CreateTable(meta::TableSchema& table_schema) override;
G
groot 已提交
36
    virtual Status DeleteTable(const std::string& table_id, const meta::DatesT& dates) override;
X
Xu Peng 已提交
37 38
    virtual Status DescribeTable(meta::TableSchema& table_schema) override;
    virtual Status HasTable(const std::string& table_id, bool& has_or_not) override;
G
groot 已提交
39 40
    virtual Status AllTables(std::vector<meta::TableSchema>& table_schema_array) override;
    virtual Status GetTableRowCount(const std::string& table_id, uint64_t& row_count) override;
41

X
Xu Peng 已提交
42
    virtual Status InsertVectors(const std::string& table_id,
G
groot 已提交
43
                                 uint64_t n, const float* vectors, IDNumbers& vector_ids) override;
X
Xu Peng 已提交
44

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

G
groot 已提交
48
    virtual Status Query(const std::string& table_id, uint64_t k, uint64_t nq,
X
Xu Peng 已提交
49 50
            const float* vectors, const meta::DatesT& dates, QueryResults& results) override;

X
Xu Peng 已提交
51
    virtual Status DropAll() override;
X
Xu Peng 已提交
52

G
groot 已提交
53
    virtual Status Size(uint64_t& result) override;
X
Xu Peng 已提交
54

X
Xu Peng 已提交
55
    virtual ~DBImpl();
56

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

G
groot 已提交
61
    Status QueryAsync(const std::string& table_id, uint64_t k, uint64_t nq,
G
groot 已提交
62 63
            const float* vectors, const meta::DatesT& dates, QueryResults& results);

64

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

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

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

X
Xu Peng 已提交
81 82
    Env* const env_;
    const Options options_;
X
Xu Peng 已提交
83

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

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

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

X
Xu Peng 已提交
96 97
    MetaPtr pMeta_;
    MemManagerPtr pMemMgr_;
X
Xu Peng 已提交
98

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

101

X
Xu Peng 已提交
102
} // namespace engine
J
jinhai 已提交
103
} // namespace milvus
X
Xu Peng 已提交
104
} // namespace zilliz