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