1 /* 2 * Copyright (c) 2023 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 DRIVE_KIT_DATABASE_H 17 #define DRIVE_KIT_DATABASE_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 #include <vector> 23 #include <list> 24 25 #include "dk_asset_read_session.h" 26 #include "dk_assets_downloader.h" 27 #include "dk_cloud_callback.h" 28 #include "dk_context.h" 29 #include "dk_error.h" 30 #include "dk_record.h" 31 #include "dk_result.h" 32 33 namespace DriveKit { 34 enum class DKSavePolicy { 35 DK_SAVE_IF_UNCHANGED = 0, 36 DK_CURRENT_WRITER_WINS, 37 }; 38 39 enum class DKDatabaseScope { 40 DK_PUBLIC_DATABASE = 0, 41 DK_PRIVATE_DATABASE, 42 DK_SHARED_DATABASE, 43 }; 44 45 using DKSchemaRawData = std::string; 46 struct DKSchemaField { 47 DKFieldKey name; 48 DKRecordFieldType type; 49 bool primary; 50 bool nullable; 51 bool sortable; 52 bool searchable; 53 bool queryable; 54 DKRecordFieldType listType; 55 DKRecordType refRecordType; 56 }; 57 struct DKSchemaNode { 58 DKRecordType recordType; 59 std::string tableName; 60 std::map<DKFieldKey, DKSchemaField> fields; 61 std::vector<DKFieldKey> dupCheckFields; 62 }; 63 struct DKOrderTable { 64 DKRecordType recordType; 65 std::string tableName; 66 }; 67 struct DKSchema { 68 int version; 69 std::map<DKRecordType, DKSchemaNode> recordTypes; 70 DKSchemaRawData schemaData; 71 std::vector<DKOrderTable> orderTables; 72 }; 73 74 struct DKPredicate { 75 std::string predicateFormat; 76 }; 77 struct DKSortDescriptor { 78 bool ascending; 79 DKFieldKey key; 80 }; 81 using DKSortDescriptors = std::vector<DKSortDescriptor>; 82 struct DKQuery { 83 DKRecordType recordType; 84 DKPredicate predicate; 85 DKSortDescriptors sortDescriptors; 86 }; 87 88 class DKRecordOperResult : public DKResult { 89 public: DKRecordOperResult()90 DKRecordOperResult() {} DKRecordOperResult(DKLocalErrorCode code)91 DKRecordOperResult(DKLocalErrorCode code) 92 { 93 error_.SetLocalError(code); 94 } 95 public: SetDKRecord(const DKRecord & record)96 void SetDKRecord(const DKRecord &record) 97 { 98 record_ = record; 99 } SetDKRecord(DKRecord && record)100 void SetDKRecord(DKRecord &&record) 101 { 102 record_ = std::move(record); 103 } GetDKRecord()104 DKRecord GetDKRecord() const 105 { 106 return record_; 107 } StealDKRecord(DKRecord & record)108 void StealDKRecord(DKRecord &record) 109 { 110 record = std::move(record_); 111 return; 112 } 113 114 private: 115 DKRecord record_; 116 }; 117 118 struct DKLock { 119 int lockInterval; 120 std::string lockSessionId; 121 }; 122 using DKFieldKeyArray = std::vector<DKFieldKey>; 123 using DKQueryCursor = std::string; 124 class DKContainer; 125 class DKAssetsUploadHelper; 126 class DKDatabase : public std::enable_shared_from_this<DKDatabase> { 127 friend class DKContainer; 128 public: 129 DKDatabase(std::shared_ptr<DKContainer> container, DKDatabaseScope scope); ~DKDatabase()130 ~DKDatabase() {} 131 132 using SaveRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 133 std::shared_ptr<DKDatabase>, 134 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>>, 135 const DKError &)>; 136 DKLocalErrorCode SaveRecords(std::shared_ptr<DKContext> context, 137 std::vector<DKRecord> &&records, 138 DKSavePolicy policy, 139 SaveRecordsCallback callback); 140 141 using SaveRecordCallback = std::function<void(std::shared_ptr<DKContext>, 142 std::shared_ptr<DKDatabase>, 143 DKRecordId, 144 DKRecordOperResult, 145 const DKError &)>; 146 DKLocalErrorCode SaveRecord(std::shared_ptr<DKContext> context, 147 DKRecord &&record, 148 DKSavePolicy policy, 149 SaveRecordCallback callback); 150 151 using FetchRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 152 std::shared_ptr<DKDatabase>, 153 std::shared_ptr<std::vector<DKRecord>>, 154 DKQueryCursor nextCursor, 155 const DKError &)>; 156 DKLocalErrorCode FetchRecords(std::shared_ptr<DKContext> context, 157 DKRecordType recordType, 158 DKFieldKeyArray &desiredKeys, 159 int resultLimit, 160 DKQueryCursor cursor, 161 FetchRecordsCallback callback); 162 163 using FetchRecordCallback = std::function<void(std::shared_ptr<DKContext>, 164 std::shared_ptr<DKDatabase>, 165 DKRecordId, 166 DKRecord &, 167 const DKError &)>; 168 DKLocalErrorCode FetchRecordWithId(std::shared_ptr<DKContext> context, 169 DKRecordType recordType, 170 DKRecordId recordId, 171 DKFieldKeyArray &desiredKeys, 172 FetchRecordCallback callback); 173 174 using DeleteRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 175 std::shared_ptr<DKDatabase>, 176 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>>, 177 const DKError &)>; 178 DKLocalErrorCode DeleteRecords(std::shared_ptr<DKContext> context, 179 std::vector<DKRecord> &&records, 180 DKSavePolicy policy, 181 DeleteRecordsCallback callback); 182 183 using ModifyRecordsCallback = 184 std::function<void(std::shared_ptr<DKContext>, 185 std::shared_ptr<DKDatabase>, 186 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>> saveResult, 187 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>> delResult, 188 const DKError &)>; 189 DKLocalErrorCode ModifyRecords(std::shared_ptr<DKContext> context, 190 std::vector<DKRecord> &&recordsToSave, 191 std::vector<DKRecord> &&recordsToDelete, 192 DKSavePolicy policy, 193 bool atomically, 194 ModifyRecordsCallback callback); 195 196 DKLocalErrorCode FetchRecordsWithQuery(std::shared_ptr<DKContext> context, 197 DKFieldKeyArray &desiredKeys, 198 DKQuery query, 199 int resultLimit, 200 DKQueryCursor cursor, 201 FetchRecordsCallback callback); 202 203 using FetchDatabaseCallback = std::function<void(std::shared_ptr<DKContext>, 204 std::shared_ptr<DKDatabase>, 205 std::shared_ptr<std::vector<DKRecord>>, 206 DKQueryCursor nextCursor, 207 bool hasMore, 208 const DKError &)>; 209 DKLocalErrorCode FetchDatabaseChanges(std::shared_ptr<DKContext> context, 210 DKRecordType recordType, 211 DKFieldKeyArray &desiredKeys, 212 int resultLimit, 213 DKQueryCursor cursor, 214 FetchDatabaseCallback callback); 215 216 DKError GetStartCursor(DKRecordType recordType, DKQueryCursor &cursor); 217 DKError GenerateIds(int count, std::vector<DKRecordId> &ids); 218 DKError GetRootId(DKRecordId &id); 219 DKError GetLock(DKLock &lock); 220 void DeleteLock(DKLock lock); 221 void Release(); 222 223 std::shared_ptr<DKAssetsDownloader> GetAssetsDownloader(); 224 std::shared_ptr<DKAssetReadSession> 225 NewAssetReadSession(DKRecordType recordType, DKRecordId recordId, DKFieldKey assetKey, DKAssetPath assetPath); 226 std::list<std::shared_ptr<DKAssetsUploadHelper>> assetUploadHplperLst_; 227 protected: 228 void Init(); 229 private: 230 DKContainerName containerName_; 231 }; 232 } // namespace DriveKit 233 #endif