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_KV_DB_MULTI_VER_DATA_STORAGE_H
17 #define SQLITE_KV_DB_MULTI_VER_DATA_STORAGE_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <string>
21 #include <vector>
22 #include <mutex>
23 #include <map>
24 #include <thread>
25 #include <condition_variable>
26 
27 #include "db_types.h"
28 #include "kvdb_properties.h"
29 #include "ikvdb_multi_ver_data_storage.h"
30 #include "macro_utils.h"
31 #include "multi_ver_value_object.h"
32 #include "generic_multi_ver_kv_entry.h"
33 #include "sqlite_multi_ver_transaction.h"
34 
35 namespace DistributedDB {
36 class SQLiteMultiVerDataStorage : public IKvDBMultiVerDataStorage {
37 public:
38     SQLiteMultiVerDataStorage();
39     ~SQLiteMultiVerDataStorage() override;
40 
41     // Delete the copy and assign constructors
42     DISABLE_COPY_ASSIGN_MOVE(SQLiteMultiVerDataStorage);
43 
44     int Open(const Property &property) override;
45 
46     int StartWrite(KvDataType dataType, IKvDBMultiVerTransaction *&transaction) override;
47 
48     int CommitWritePhaseOne(IKvDBMultiVerTransaction *transaction,
49         const UpdateVerTimestamp &multiVerTimestamp) override;
50 
51     int RollbackWritePhaseOne(IKvDBMultiVerTransaction *transaction, const Version &versionInfo) override;
52 
53     int RollbackWrite(IKvDBMultiVerTransaction *transaction) override;
54 
55     void CommitWritePhaseTwo(IKvDBMultiVerTransaction *transaction) override;
56 
57     IKvDBMultiVerTransaction *StartRead(KvDataType dataType, const Version &versionInfo, int &errCode) override;
58 
59     void ReleaseTransaction(IKvDBMultiVerTransaction *transaction) override;
60 
61     void Close() override;
62 
63     int RunRekeyLogic(CipherType type, const CipherPassword &passwd);
64 
65     int RunExportLogic(CipherType type, const CipherPassword &passwd, const std::string &dbDir);
66 
67     int CheckVersion(const Property &property, bool &isDbExist) const override;
68 
69     int GetVersion(const Property &property, int &version, bool &isDbExisted) const override;
70 
71     int BackupCurrentDatabase(const Property &property, const std::string &dir) override;
72 
73     int ImportDatabase(const Property &property, const std::string &dir, const CipherPassword &passwd) override;
74 
75 private:
76     Property property_;
77     std::string uri_;
78     std::map<IKvDBMultiVerTransaction *, bool> readTransactions_;
79     SQLiteMultiVerTransaction *writeTransaction_;
80     bool writeTransactionUsed_;
81     std::mutex transactionMutex_;
82     std::condition_variable readCondition_;
83     std::condition_variable writeCondition_;
84     std::thread::id writeHolderId_;
85 };
86 } // namespace DistributedDB
87 
88 #endif // SQLITE_KV_DB_MULTI_VER_DATA_STORAGE_H
89 #endif