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