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 MULTI_VER_KV_DATA_STORAGE_H
17 #define MULTI_VER_KV_DATA_STORAGE_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <mutex>
21 #include <string>
22 
23 #include "ikvdb.h"
24 #include "macro_utils.h"
25 
26 namespace DistributedDB {
27 class SliceTransaction {
28 public:
29     SliceTransaction(bool isWrite, IKvDBConnection *connect);
30     ~SliceTransaction();
31     int Close();
32     int PutData(const Key &key, const Value &value, bool isAddCount);
33     int GetData(const Key &key, Value &value) const;
34     int DeleteData(const Key &key);
35     int StartTransaction();
36     int CommitTransaction();
37     int RollbackTransaction();
38 private:
39     bool isWrite_;
40     IKvDBConnection *connect_;
41 };
42 
43 class MultiVerKvDataStorage {
44 public:
45     struct Property final {
46         std::string dataDir;
47         std::string identifierName;
48         bool isNeedCreate = true;
49         CipherType cipherType = CipherType::AES_256_GCM;
50         CipherPassword passwd;
51     };
52 
53     MultiVerKvDataStorage();
54     ~MultiVerKvDataStorage();
55 
56     DISABLE_COPY_ASSIGN_MOVE(MultiVerKvDataStorage);
57 
58     int Open(const Property &property);
59 
60     void Close();
61 
62     int PutMetaData(const Key &key, const Value &value);
63 
64     int GetMetaData(const Key &key, Value &value) const;
65 
66     SliceTransaction *GetSliceTransaction(bool isWrite, int &errCode);
67 
68     void ReleaseSliceTransaction(SliceTransaction *&transaction);
69 
70     int RunRekeyLogic(CipherType type, const CipherPassword &passwd);
71 
72     int RunExportLogic(CipherType type, const CipherPassword &passwd, const std::string &dbDir) const;
73 
74     int CheckVersion(const Property &property, bool &isDbAllExist) const;
75 
76     int BackupCurrentDatabase(const Property &property, const std::string &dir);
77 
78     int ImportDatabase(const Property &property, const std::string &dir, const CipherPassword &passwd);
79 
80 private:
81     int GetVersion(const Property &property, int &metaVer, bool &isMetaDbExist,
82         int &sliceVer, bool &isSliceDbExist) const;
83 
84     IKvDB *kvStorage_;
85     IKvDB *metaStorage_;
86     IKvDBConnection *kvStorageConnection_;
87     IKvDBConnection *metaStorageConnection_;
88     mutable std::mutex metaDataMutex_;
89     mutable std::mutex kvDataMutex_;
90 };
91 }
92 
93 #endif // MULTI_VER_KV_DATA_STORAGE_H
94 #endif
95