1 /*
2  * Copyright (c) 2023 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 SINGLE_VER_UTILS_H
17 #define SINGLE_VER_UTILS_H
18 
19 #include "kvdb_properties.h"
20 #include "storage_executor.h"
21 #include "types_export.h"
22 
23 namespace DistributedDB {
24 
25 const uint64_t CACHE_RECORD_DEFAULT_VERSION = 1;
26 
27 enum class DbType : int32_t {
28     MAIN,
29     META,
30     CACHE
31 };
32 
33 struct OpenDbProperties {
34     std::string uri {};
35     bool createIfNecessary = true;
36     bool isMemDb = false;
37     std::vector<std::string> sqls {};
38     CipherType cipherType = CipherType::AES_256_GCM;
39     CipherPassword passwd {};
40     std::string schema {};
41     std::string subdir {};
42     SecurityOption securityOpt {};
43     int conflictReslovePolicy = DEFAULT_LAST_WIN;
44     bool createDirByStoreIdOnly = false;
45     uint32_t iterTimes = DBConstant::DEFAULT_ITER_TIMES;
46     // newly added RD properties
47     std::string rdConfig {};
48     bool isNeedIntegrityCheck = false;
49     bool isNeedRmCorruptedDb = false;
50     bool readOnly = false;
51     bool isHashTable = false;
52 };
53 
54 int GetPathSecurityOption(const std::string &filePath, SecurityOption &secOpt);
55 
56 std::string GetDbDir(const std::string &subDir, DbType type);
57 
58 std::string GetDatabasePath(const KvDBProperties &kvDBProp);
59 
60 std::string GetSubDirPath(const KvDBProperties &kvDBProp);
61 
62 int ClearIncompleteDatabase(const KvDBProperties &kvDBPro);
63 
64 int CreateNewDirsAndSetSecOption(const OpenDbProperties &option);
65 
66 int GetExistedSecOpt(const OpenDbProperties &option, SecurityOption &secOption);
67 
68 int CheckStoreStatus(const OpenDbProperties &opt);
69 
70 void InitCommitNotifyDataKeyStatus(SingleVerNaturalStoreCommitNotifyData *committedData, const Key &hashKey,
71     const DataOperStatus &dataStatus);
72 } // namespace DistributedDB
73 
74 #endif // SINGLE_VER_UTILS_H