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