• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DISTRIBUTED_KVSTORE_TYPES_H
17 #define DISTRIBUTED_KVSTORE_TYPES_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 #include <variant>
23 #include "store_errno.h"
24 #include "blob.h"
25 #include "visibility.h"
26 
27 namespace OHOS {
28 namespace DistributedKv {
29 // key set by client, can be any non-empty bytes array, and less than 1024 size.
30 using Key = OHOS::DistributedKv::Blob;
31 
32 // value set by client, can be any bytes array.
33 using Value = OHOS::DistributedKv::Blob;
34 
35 // user identifier from user-account
36 struct UserId {
37     std::string userId;
38 };
39 
40 // app identifier from Bms
41 struct API_EXPORT AppId {
42     std::string appId;
43 
44     // support AppId convert to std::string
45     operator std::string &() noexcept
46     {
47         return appId;
48     }
49     // support AppId convert to const std::string
50     operator const std::string &() const noexcept
51     {
52         return appId;
53     }
54 
IsValidAppId55     inline bool IsValid() const
56     {
57         if (appId.empty() || appId.size() > MAX_APP_ID_LEN) {
58             return false;
59         }
60         int count = 0;
61         auto iter = std::find_if_not(appId.begin(), appId.end(),
62             [&count](char c) {
63             count = (c == SEPARATOR_CHAR) ? (count + 1) : (count >= SEPARATOR_COUNT ? count : 0);
64             return (std::isprint(c) && c != '/');
65         });
66 
67         return (iter == appId.end()) && (count < SEPARATOR_COUNT);
68     }
69 private:
70     static constexpr int MAX_APP_ID_LEN = 256;
71     static constexpr int SEPARATOR_COUNT = 3;
72     static constexpr char SEPARATOR_CHAR = '#';
73 };
74 
75 // kvstore name set by client by calling GetKvStore,
76 // storeId len must be less or equal than 256,
77 // and can not be empty and all space.
78 struct API_EXPORT StoreId {
79     std::string storeId;
80 
81     // support StoreId convert to std::string
82     operator std::string &() noexcept
83     {
84         return storeId;
85     }
86     // support StoreId convert to const std::string
87     operator const std::string &() const noexcept
88     {
89         return storeId;
90     }
91 
92     bool operator<(const StoreId &id) const noexcept
93     {
94         return this->storeId < id.storeId;
95     }
96 
IsValidStoreId97     inline bool IsValid() const
98     {
99         if (storeId.empty() || storeId.size() > MAX_STORE_ID_LEN) {
100             return false;
101         }
102         auto iter = std::find_if_not(storeId.begin(), storeId.end(),
103             [](char c) { return (std::isdigit(c) || std::isalpha(c) || c == '_'); });
104         return (iter == storeId.end());
105     }
106 private:
107     static constexpr int MAX_STORE_ID_LEN = 128;
108 };
109 
110 struct KvStoreTuple {
111     std::string userId;
112     std::string appId;
113     std::string storeId;
114 };
115 
116 struct AppThreadInfo {
117     std::int32_t pid;
118     std::int32_t uid;
119 };
120 
121 enum SubscribeType : uint32_t {
122     SUBSCRIBE_TYPE_LOCAL = 1, // local changes of syncable kv store
123     SUBSCRIBE_TYPE_REMOTE = 2, // synced data changes from remote devices
124     SUBSCRIBE_TYPE_ALL = 3, // both local changes and synced data changes
125 };
126 
127 struct Entry {
128     Key key;
129     Value value;
130 
131     static constexpr size_t MAX_KEY_LENGTH = 1024;
132     static constexpr size_t MAX_VALUE_LENGTH = 4 * 1024 * 1024;
133     /* write blob size and data to memory buffer. return error when bufferLeftSize not enough. */
WriteToBufferEntry134     bool WriteToBuffer(uint8_t *&cursorPtr, int &bufferLeftSize) const
135     {
136         return key.WriteToBuffer(cursorPtr, bufferLeftSize) && value.WriteToBuffer(cursorPtr, bufferLeftSize);
137     }
138     /* read a blob from memory buffer. */
ReadFromBufferEntry139     bool ReadFromBuffer(const uint8_t *&cursorPtr, int &bufferLeftSize)
140     {
141         return key.ReadFromBuffer(cursorPtr, bufferLeftSize) && value.ReadFromBuffer(cursorPtr, bufferLeftSize);
142     }
143 
RawSizeEntry144     int RawSize() const
145     {
146         return key.RawSize() + value.RawSize();
147     }
148 };
149 
150 enum SyncMode : int32_t {
151     PULL,
152     PUSH,
153     PUSH_PULL,
154 };
155 
156 enum KvStoreType : int32_t {
157     DEVICE_COLLABORATION,
158     SINGLE_VERSION,
159     MULTI_VERSION,
160     INVALID_TYPE,
161 };
162 
163 enum SecurityLevel : int32_t {
164     NO_LABEL,
165     S0,
166     S1,
167     S2,
168     S3_EX,
169     S3,
170     S4,
171 };
172 
173 enum Area : int32_t {
174     EL0,
175     EL1,
176     EL2,
177     EL3,
178     EL4
179 };
180 
181 enum KvControlCmd : int32_t {
182     SET_SYNC_PARAM = 1,
183     GET_SYNC_PARAM,
184 };
185 
186 using KvParam = OHOS::DistributedKv::Blob;
187 
188 struct KvSyncParam {
189     uint32_t allowedDelayMs { 0 };
190 };
191 
192 enum class DeviceChangeType {
193     DEVICE_OFFLINE = 0,
194     DEVICE_ONLINE = 1,
195 };
196 
197 struct DeviceInfo {
198     std::string deviceId;
199     std::string deviceName;
200     std::string deviceType;
201 };
202 
203 enum class DeviceFilterStrategy {
204     FILTER = 0,
205     NO_FILTER = 1,
206 };
207 
208 enum PolicyType : uint32_t {
209     TERM_OF_SYNC_VALIDITY,
210     IMMEDIATE_SYNC_ON_ONLINE,
211     IMMEDIATE_SYNC_ON_CHANGE,
212     POLICY_BUTT
213 };
214 
215 struct SyncPolicy {
216     uint32_t type;
217     std::variant<std::monostate, uint32_t> value;
218 };
219 
220 struct Options {
221     bool createIfMissing = true;
222     bool encrypt = false;
223     bool persistent = true;
224     bool backup = true;
225     bool autoSync = true;
226     bool syncable = true; // true if is distributed store, false if is local store
227     bool rebuild = false;
228     int32_t securityLevel = NO_LABEL;
229     int32_t area = EL1;
230     KvStoreType kvStoreType = DEVICE_COLLABORATION;
231 
232     std::vector<SyncPolicy> policies{ { IMMEDIATE_SYNC_ON_CHANGE } };
233     std::string schema = "";
234     std::string hapName = "";
235     std::string baseDir = "";
236 
IsValidTypeOptions237     inline bool IsValidType() const
238     {
239         return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION;
240     }
241 };
242 
243 template<typename T>
TransferTypeToByteArray(const T & t)244 std::vector<uint8_t> TransferTypeToByteArray(const T &t)
245 {
246     return std::vector<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<T *>(&t)),
247                                 reinterpret_cast<uint8_t *>(const_cast<T *>(&t)) + sizeof(T));
248 }
249 
250 template<typename T>
TransferByteArrayToType(const std::vector<uint8_t> & blob)251 T TransferByteArrayToType(const std::vector<uint8_t> &blob)
252 {
253     // replace assert to HILOG_FATAL when HILOG_FATAL is ok.
254     if (blob.size() != sizeof(T) || blob.size() == 0) {
255         constexpr int tSize = sizeof(T);
256         uint8_t tContent[tSize] = { 0 };
257         return *reinterpret_cast<T *>(tContent);
258     }
259     return *reinterpret_cast<T *>(const_cast<uint8_t *>(&blob[0]));
260 }
261 }  // namespace DistributedKv
262 }  // namespace OHOS
263 #endif  // DISTRIBUTED_KVSTORE_TYPES_H
264