1 /*
2 * Copyright (c) 2022 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 KV_DATASERVICE_CONSTANT_H
17 #define KV_DATASERVICE_CONSTANT_H
18
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <algorithm>
22 #include <cctype>
23 #include <locale>
24 #include <string>
25 #include <vector>
26
27 #ifndef API_EXPORT
28 #define API_EXPORT __attribute__((visibility ("default")))
29 #endif
30 #ifndef KVSTORE_API
31 #define KVSTORE_API API_EXPORT
32 #endif
33
34 namespace OHOS {
35 namespace DistributedKv {
36 class KvStoreMetaRow {
37 public:
38 KVSTORE_API static const std::string KEY_PREFIX;
39
40 KVSTORE_API static std::vector<uint8_t> GetKeyFor(const std::string &key);
41 };
42
43 class SecretMetaRow {
44 public:
45 KVSTORE_API static const std::string KEY_PREFIX;
46
47 KVSTORE_API static std::vector<uint8_t> GetKeyFor(const std::string &key);
48 };
49
50 class Constant {
51 public:
52 // concatenate strings and return a composition string.
53 KVSTORE_API static std::string Concatenate(std::initializer_list<std::string> stringList);
54
55 // delete left bland in s by reference.
56 template<typename T>
57 static void LeftTrim(T &s);
58
59 // delete right bland in s by reference.
60 template<typename T>
61 static void RightTrim(T &s);
62
63 // delete both left and right bland in s by reference.
64 template<typename T>
65 static void Trim(T &s);
66
67 // delete left bland in s by reference, not change raw string.
68 template<typename T>
69 static T LeftTrimCopy(T s);
70
71 // delete right bland in s by reference, not change raw string.
72 template<typename T>
73 static T RightTrimCopy(T s);
74
75 // delete both left and right bland in s by reference, not change raw string.
76 template<typename T>
77 static T TrimCopy(T s);
78
79 // get default device account id.
80 KVSTORE_API static std::string GetDefaultDeviceAccountId();
81
82 // get default harmony account name.
83 KVSTORE_API static std::string GetDefaultHarmonyAccountName();
84
85 // default group id for synchronization based on harmony account.
86 KVSTORE_API static const std::string DEFAULT_GROUP_ID;
87
88 // Indicates whether only storeid are used as hash materials for the DistributedDB path generated.
89 KVSTORE_API static const bool STOREID_ONLY_FLAG;
90
91 // version for distributed kv data service.
92 KVSTORE_API static const std::string VERSION;
93
94 // meta name for distributed kv data service.
95 KVSTORE_API static const std::string META_DIR_NAME;
96
97 // name for distributed kv data service.
98 KVSTORE_API static const std::string SERVICE_NAME;
99
100 // root path for distributed kv data service.
101 KVSTORE_API static const std::string ROOT_PATH;
102
103 // root path for distributeddata service and system services.
104 KVSTORE_API static const std::string ROOT_PATH_DE;
105
106 // root path for self-developed and non-self-developed app.
107 KVSTORE_API static const std::string ROOT_PATH_CE;
108
109 // the max length for key is 256.
110 KVSTORE_API static const size_t MAX_KEY_LENGTH;
111
112 // the max length for value is 1M.
113 KVSTORE_API static const size_t MAX_VALUE_LENGTH;
114
115 // the max length for StoreId is 64.
116 KVSTORE_API static const size_t MAX_STORE_ID_LENGTH;
117
118 // the max batch for putBatch is 128.
119 KVSTORE_API static const size_t MAX_BATCH_SIZE;
120
121 // the max capacity for ipc is 800KB.
122 KVSTORE_API static const size_t MAX_IPC_CAPACITY;
123
124 // service meta db name.
125 KVSTORE_API static const std::string SERVICE_META_DB_NAME;
126
127 KVSTORE_API static const std::string KEY_SEPARATOR;
128
129 KVSTORE_API static const mode_t DEFAULT_MODE;
130
131 KVSTORE_API static const mode_t DEFAULT_MODE_DIR;
132
133 KVSTORE_API static const mode_t DEFAULT_MODE_FILE;
134
135 KVSTORE_API static const int SWITCH_RAW_DATA_SIZE;
136
137 KVSTORE_API static const int MAX_OPEN_KVSTORES;
138
139 // name for process label (bus name for communication). compatible with HwDDMP
140 KVSTORE_API static const std::string ROOT_KEY_GENERATED;
141 };
142
143 // trim from start (in place)
144 template<typename T>
LeftTrim(T & s)145 void Constant::LeftTrim(T &s)
146 {
147 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
148 }
149
150 // trim from end (in place)
151 template<typename T>
RightTrim(T & s)152 void Constant::RightTrim(T &s)
153 {
154 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
155 }
156
157 // trim from both ends (in place)
158 template<typename T>
Trim(T & s)159 void Constant::Trim(T &s)
160 {
161 LeftTrim(s);
162 RightTrim(s);
163 }
164
165 // trim from start (copying)
166 template<typename T>
LeftTrimCopy(T s)167 T Constant::LeftTrimCopy(T s)
168 {
169 LeftTrim(s);
170 return s;
171 }
172
173 // trim from end (copying)
174 template<typename T>
RightTrimCopy(T s)175 T Constant::RightTrimCopy(T s)
176 {
177 RightTrim(s);
178 return s;
179 }
180
181 // trim from both ends (copying)
182 template<typename T>
TrimCopy(T s)183 T Constant::TrimCopy(T s)
184 {
185 Trim(s);
186 return s;
187 }
188 } // namespace DistributedKv
189 } // namespace OHOS
190 #endif // KV_DATASERVICE_CONSTANT_H
191