1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * \author guonix
17 * \date Oct 2020
18 * \brief
19 */
20
21#pragma once
22
23#include <atomic>
24#include <unordered_map>
25#include "meta/meta_store.h"
26#include "sqlite_statement.h"
27
28namespace proxima {
29namespace be {
30namespace meta {
31namespace sqlite {
32
33/*!
34 * SQLiteMateStore
35 */
36class SQLiteMetaStore : public MetaStore {
37 public:
38 //! Constructor
39 SQLiteMetaStore();
40
41 //! Destructor
42 ~SQLiteMetaStore() override;
43
44 public:
45 //! initialize metastore
46 int initialize(const ailego::Uri *uri) override;
47
48 //! cleanup
49 int cleanup() override;
50
51 //! Create the collection
52 int create_collection(const CollectionObject &collection) override;
53
54 //! Update collection declaration
55 int update_collection(const CollectionObject &collection) override;
56
57 //! Delete collection
58 int delete_collection(const std::string &name) override;
59
60 //! Delete collection by uuid
61 int delete_collection_by_uuid(const std::string &uuid) override;
62
63 //! Retrieve all collections
64 int list_collections(CollectionAllocator allocator) const override;
65
66 //! Create column
67 int create_column(const ColumnObject &column) override;
68
69 //! Delete columns
70 int delete_columns_by_uid(const std::string &uid) override;
71
72 //! Delete columns
73 int delete_columns_by_uuid(const std::string &uuid) override;
74
75 //! Retrieve all collections
76 int list_columns(ColumnAllocator allocator) const override;
77
78 //! Create repository
79 int create_repository(const DatabaseRepositoryObject &repository) override;
80
81 //! Delete repositories
82 int delete_repositories_by_uid(const std::string &uid) override;
83
84 //! Delete repositories
85 int delete_repositories_by_uuid(const std::string &uuid) override;
86
87 //! Retrieve all repositories
88 int list_repositories(DatabaseRepositoryAllocator allocator) const override;
89
90 //! Flush all changes to storage
91 int flush() const override;
92
93 private:
94 //! Cleanup
95 int do_cleanup();
96
97 //! Sync tables to database
98 int sync(sqlite3 *handle);
99
100 //! Initialize all the statements
101 int init_statements(const std::string &database);
102
103 //! Get statement
104 StatementPtr &statement(uint32_t index) const;
105
106 //! Put statement into map
107 int put(uint32_t hash, const StatementPtr &stmt);
108
109 private:
110 //! Initialized flag
111 std::atomic_bool initialized_{false};
112
113 //! DB storage path
114 std::string database_{};
115
116 //! Statements
117 mutable std::unordered_map<uint32_t, StatementPtr> statements_{};
118};
119
120
121} // namespace sqlite
122} // namespace meta
123} // namespace be
124} // namespace proxima
125