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