• 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 DISTRIBUTEDDB_TYPES_H
17 #define DISTRIBUTEDDB_TYPES_H
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 #include "db_constant.h"
26 #include "store_observer.h"
27 #include "types_export.h"
28 
29 namespace DistributedDB {
30 using TableName = std::string;
31 using Timestamp = uint64_t;
32 using ContinueToken = void *;
33 using DeviceID = std::string;
34 using TimeOffset = int64_t;
35 using ErrorCode = int;
36 using SyncId = uint64_t;
37 using WaterMark = uint64_t;
38 using DatabaseCorruptHandler = std::function<void()>;
39 using DatabaseLifeCycleNotifier = std::function<void(const std::string &, const std::string &)>;
40 const uint32_t MTU_SIZE = 5 * 1024 * 1024; // 5 M, 1024 is scale
41 
42 struct DataItem {
43     Key key;
44     Value value;
45     Timestamp timestamp = 0;
46     uint64_t flag = 0;
47     std::string origDev;
48     Timestamp writeTimestamp = 0;
49     std::string dev;
50     bool neglect = false;
51     Key hashKey{};
52     Timestamp modifyTime = 0;
53     Timestamp createTime = 0;
54     std::string version; // use for cloud
55     std::string gid; // use for cloud
56     uint32_t cloud_flag = 0; // use for cloud
57     static constexpr uint64_t DELETE_FLAG = 0x01;
58     static constexpr uint64_t LOCAL_FLAG = 0x02;
59     static constexpr uint64_t REMOVE_DEVICE_DATA_FLAG = 0x04; // only use for cachedb
60     static constexpr uint64_t REMOVE_DEVICE_DATA_NOTIFY_FLAG = 0x08; // only use for cachedb
61     // Only use for query sync and subscribe. ATTENTION!!! this flag should not write into mainDB.
62     // Mark the changed row data does not match with query sync(or subscribe) condition.
63     static constexpr uint64_t REMOTE_DEVICE_DATA_MISS_QUERY = 0x10;
64     static constexpr uint64_t UPDATE_FLAG = 0X20;
65 };
66 
67 struct PragmaPublishInfo {
68     Key key;
69     bool deleteLocal = false;
70     bool updateTimestamp = false;
71     KvStoreNbPublishAction action;
72 };
73 
74 struct PragmaUnpublishInfo {
75     Key key;
76     bool isDeleteSync = false;
77     bool isUpdateTime = false;
78 };
79 
80 struct IOption {
81     static constexpr int LOCAL_DATA = 1;
82     static constexpr int SYNC_DATA = 2;
83     int dataType = LOCAL_DATA;
84 };
85 
86 struct DataSizeSpecInfo {
87     uint32_t blockSize = MTU_SIZE;
88     size_t packetSize = DBConstant::MAX_HPMODE_PACK_ITEM_SIZE;
89 };
90 
91 enum NotificationEventType {
92     DATABASE_COMMIT_EVENT = 0
93 };
94 
95 // Following are schema related common definition
96 using FieldName = std::string;
97 using FieldPath = std::vector<std::string>;
98 // Normally, LEAF_FIELD_NULL will not appear in valid schema. LEAF_FIELD_LONG contain LEAF_FIELD_INTEGER, both are
99 // signed type and LEAF_FIELD_DOUBLE contain LEAF_FIELD_LONG. We don't parse into an array, so array are always leaf
100 // type. We parse into an object, LEAF_FIELD_OBJECT means an empty object, INTERNAL_FIELD_OBJECT however not empty.
101 enum class FieldType {
102     LEAF_FIELD_NULL,
103     LEAF_FIELD_BOOL,
104     LEAF_FIELD_INTEGER,
105     LEAF_FIELD_LONG,
106     LEAF_FIELD_DOUBLE,
107     LEAF_FIELD_STRING,
108     LEAF_FIELD_ARRAY,
109     LEAF_FIELD_OBJECT,
110     INTERNAL_FIELD_OBJECT,
111 };
112 using TypeValue = std::pair<FieldType, FieldValue>; // Define for parameter convenience
113 
114 // Schema compatibility check behave differently for different value source
115 enum class ValueSource {
116     FROM_LOCAL,
117     FROM_SYNC,
118     FROM_DBFILE,
119 };
120 // Represent raw-value from database to avoid copy. the first is the value start pointer, the second is the length.
121 using RawValue = std::pair<const uint8_t *, uint32_t>;
122 using RawString = const std::string::value_type *;
123 
124 enum class OperatePerm {
125     NORMAL_PERM,
126     NORMAL_WRITE,
127     REKEY_MONOPOLIZE_PERM,
128     IMPORT_MONOPOLIZE_PERM,
129     DISABLE_PERM,
130     RESTART_SYNC_PERM,
131 };
132 
133 enum SingleVerConflictResolvePolicy {
134     DEFAULT_LAST_WIN = 0,
135     DENY_OTHER_DEV_AMEND_CUR_DEV_DATA = 1,
136 };
137 
138 struct SyncTimeRange {
139     Timestamp beginTime = 0;
140     Timestamp deleteBeginTime = 0;
141     Timestamp endTime = static_cast<Timestamp>(INT64_MAX);
142     Timestamp deleteEndTime = static_cast<Timestamp>(INT64_MAX);
143     Timestamp lastQueryTime = 0;
IsValidSyncTimeRange144     bool IsValid() const
145     {
146         return (beginTime <= endTime && deleteBeginTime <= deleteEndTime);
147     }
148 };
149 
150 // field types stored in sqlite
151 enum class StorageType : int32_t {
152     STORAGE_TYPE_NONE = 0,
153     STORAGE_TYPE_NULL,
154     STORAGE_TYPE_INTEGER,
155     STORAGE_TYPE_REAL,
156     STORAGE_TYPE_TEXT,
157     STORAGE_TYPE_BLOB
158 };
159 
160 // Table mode of device data for relational store
161 enum DistributedTableMode : int {
162     COLLABORATION = 0, // Save all devices data in user table
163     SPLIT_BY_DEVICE // Save device data in each table split by device
164 };
165 
166 struct CaseInsensitiveComparator {
operatorCaseInsensitiveComparator167     bool operator() (const std::string& first, const std::string& second) const
168     {
169         std::string str1(first.length(), ' ');
170         std::string str2(second.length(), ' ');
171         std::transform(first.begin(), first.end(), str1.begin(), ::tolower);
172         std::transform(second.begin(), second.end(), str2.begin(), ::tolower);
173         return str1 < str2;
174     }
175 };
176 
177 enum class SortType {
178     NONE = 0,
179     TIMESTAMP_ASC,
180     TIMESTAMP_DESC
181 };
182 
183 struct DeviceTimeInfo {
184     Timestamp recordTime = 0; // info generate time
185     TimeOffset systemTimeOffset = 0; // raw system time offset
186     int64_t rtt = 0;
187 };
188 
189 using ObserverAction =
190     std::function<void(const std::string &device, ChangedData &&changedData, bool isChangedData)>;
191 } // namespace DistributedDB
192 #endif // DISTRIBUTEDDB_TYPES_H
193