1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SQLITE_LOCAL_KV_DB_CONNECTION_H
17 #define SQLITE_LOCAL_KV_DB_CONNECTION_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <set>
21 #include <mutex>
22 
23 #include "db_errno.h"
24 #include "kvdb_properties.h"
25 #include "generic_kvdb_connection.h"
26 #include "sqlite_local_storage_executor.h"
27 
28 namespace DistributedDB {
29 class SQLiteLocalKvDB;
30 
31 class SQLiteLocalKvDBConnection : public GenericKvDBConnection {
32 public:
33     explicit SQLiteLocalKvDBConnection(SQLiteLocalKvDB *kvDB);
34     ~SQLiteLocalKvDBConnection() override;
35 
36     // Delete the copy and assign constructors
37     DISABLE_COPY_ASSIGN_MOVE(SQLiteLocalKvDBConnection);
38 
39     // Get the value from the sqlite database
40     int Get(const IOption &option, const Key &key, Value &value) const override;
41 
42     // Put the value to the sqlite database
43     int Put(const IOption &option, const Key &key, const Value &value) override;
44 
45     // Delete the value from the sqlite database
46     int Delete(const IOption &option, const Key &key) override;
47 
48     // Clear all the data from the sqlite database
49     int Clear(const IOption &option) override;
50 
51     // Get all the data which have the prefix key from the sqlite database
52     int GetEntries(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const override;
53 
54     // Put the batch data to the sqlite database
55     int PutBatch(const IOption &option, const std::vector<Entry> &entries) override;
56 
57     // Delete the batch data from the sqlite database according to the key from the set
58     int DeleteBatch(const IOption &option, const std::vector<Key> &keys) override;
59 
60     // Get the snapshot
61     int GetSnapshot(IKvDBSnapshot *&snapshot) const override;
62 
63     // Release the snapshot
64     void ReleaseSnapshot(IKvDBSnapshot *&snapshot) override;
65 
66     // Next step interface
67     // Start the transaction
68     int StartTransaction() override;
69 
70     // Commit the transaction
71     int Commit() override;
72 
73     // Roll back the transaction
74     int RollBack() override;
75 
76     // Check if the transaction already started manually
77     bool IsTransactionStarted() const override;
78 
79     // Called when close the connection
80     int PreClose() override;
81 
82     // Parse event types(from observer mode).
83     int TranslateObserverModeToEventTypes(unsigned mode, std::list<int> &eventTypes) const override;
84 
85     int Rekey(const CipherPassword &passwd) override;
86 
87     int Export(const std::string &filePath, const CipherPassword &passwd) override;
88 
89     int Import(const std::string &filePath, const CipherPassword &passwd) override;
90 
91 private:
92     // Start the transaction
93     int StartTransactionInner(bool &isAuto);
94 
95     // Commit the transaction
96     int CommitInner();
97 
98     // Roll back the transaction
99     int RollBackInner();
100 
101     int CheckDataStatus(const Key &key, const Value &value, bool isDeleted) const;
102 
103     SQLiteLocalStorageExecutor *writeHandle_; // only existed while in transaction.
104 
105     mutable std::mutex transactionMutex_;
106     mutable std::set<IKvDBSnapshot *> snapshots_;
107     mutable std::mutex snapshotMutex_;
108     std::mutex importMutex_;
109 };
110 };  // namespace DistributedDB
111 #endif // OMIT_MULTI_VER
112 #endif // SQLITE_LOCAL_KV_DB_CONNECTION_H
113