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 I_KV_DB_MULTI_VER_DATA_STORAGE_H
17 #define I_KV_DB_MULTI_VER_DATA_STORAGE_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <string>
21 #include <vector>
22 
23 #include "db_types.h"
24 #include "ikvdb_multi_ver_transaction.h"
25 #include "multi_ver_def.h"
26 #include "multi_ver_kv_entry.h"
27 
28 namespace DistributedDB {
29 enum class KvDataType {
30     KV_DATA_LOCAL,
31     KV_DATA_SYNC_P2P,
32 };
33 
34 struct UpdateVerTimestamp {
35     uint64_t timestamp = 0uLL;
36     bool isNeedUpdate = false;
37 };
38 
39 class IKvDBMultiVerDataStorage {
40 public:
41     struct Property final {
42         std::string path;
43         std::string identifierName;
44         bool isNeedCreate = true;
45         CipherType cipherType = CipherType::AES_256_GCM;
46         CipherPassword passwd;
47     };
48 
~IKvDBMultiVerDataStorage()49     virtual ~IKvDBMultiVerDataStorage() {};
50     virtual int Open(const Property &property) = 0;
51     virtual int StartWrite(KvDataType dataType, IKvDBMultiVerTransaction *&transaction) = 0;
52     virtual int CommitWritePhaseOne(IKvDBMultiVerTransaction *transaction,
53         const UpdateVerTimestamp &multiVerTimestamp) = 0;
54     virtual int RollbackWritePhaseOne(IKvDBMultiVerTransaction *transaction, const Version &versionInfo) = 0;
55     virtual int RollbackWrite(IKvDBMultiVerTransaction *transaction) = 0;
56     virtual void CommitWritePhaseTwo(IKvDBMultiVerTransaction *transaction) = 0;
57     virtual IKvDBMultiVerTransaction *StartRead(KvDataType dataType, const Version &versionInfo, int &errCode) = 0;
58     virtual void ReleaseTransaction(IKvDBMultiVerTransaction *transaction) = 0;
59     virtual void Close() = 0;
60     virtual int CheckVersion(const Property &property, bool &isDbExist) const = 0;
61     virtual int GetVersion(const Property &property, int &version, bool &isDbExisted) const = 0;
62     virtual int BackupCurrentDatabase(const Property &property, const std::string &dir) = 0;
63     virtual int ImportDatabase(const Property &property, const std::string &dir, const CipherPassword &passwd) = 0;
64 };
65 } // namespace DistributedDB
66 
67 #endif  // I_KV_DB_MULTI_VER_DATA_STORAGE_H
68 #endif