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 OHOS_CLOUD_SYNC_SERVICE_CLOUD_DISK_DATA_HANDLER_H 17 #define OHOS_CLOUD_SYNC_SERVICE_CLOUD_DISK_DATA_HANDLER_H 18 19 #include "cloud_disk_data_convertor.h" 20 #include "medialibrary_db_const.h" 21 #include "medialibrary_type_const.h" 22 #include "rdb_data_handler.h" 23 #include "clouddisk_type_const.h" 24 25 namespace OHOS { 26 namespace FileManagement { 27 namespace CloudSync { 28 class CloudDiskDataHandler : public RdbDataHandler { 29 public: 30 CloudDiskDataHandler(int32_t userId, const std::string &bundleName, 31 std::shared_ptr<NativeRdb::RdbStore> rdb, std::shared_ptr<bool> stopFlag); 32 virtual ~CloudDiskDataHandler() = default; 33 34 void GetFetchCondition(FetchCondition &cond) override; 35 virtual int32_t OnFetchRecords(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records, 36 OnFetchParams ¶ms) override; 37 virtual int32_t GetRetryRecords(std::vector<DriveKit::DKRecordId> &records) override; 38 virtual int32_t GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords, 39 const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records) override; 40 int32_t GetDownloadAsset(std::string cloudId, std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 41 int32_t Clean(const int32_t action) override; 42 int32_t GetCreatedRecords(std::vector<DriveKit::DKRecord> &records) override; 43 int32_t GetDeletedRecords(std::vector<DriveKit::DKRecord> &records) override; 44 int32_t GetMetaModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 45 int32_t GetFileModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 46 int32_t OnCreateRecords(const std::map<DriveKit::DKRecordId, 47 DriveKit::DKRecordOperResult> &map) override; 48 int32_t OnDeleteRecords(const std::map<DriveKit::DKRecordId, 49 DriveKit::DKRecordOperResult> &map) override; 50 int32_t OnModifyMdirtyRecords(const std::map<DriveKit::DKRecordId, 51 DriveKit::DKRecordOperResult> &map) override; 52 int32_t OnModifyFdirtyRecords(const std::map<DriveKit::DKRecordId, 53 DriveKit::DKRecordOperResult> &map) override; 54 int32_t OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset) override; 55 int32_t OnDownloadAssets(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap) override; 56 int32_t OnDownloadAssets(const DriveKit::DKDownloadAsset &asset) override; 57 void Reset(); 58 int32_t CleanCache(const std::string &uri); 59 private: 60 static inline const int32_t LIMIT_SIZE = 5; 61 static inline const int32_t PULL_LIMIT_SIZE = 100; 62 static inline const int32_t MODIFY_BATCH_NUM = 20; 63 static inline const int32_t DELETE_BATCH_NUM = 20; 64 static const int32_t MAX_RENAME = 10; 65 DriveKit::DKRecordType recordType_ = "file"; 66 DriveKit::DKFieldKeyArray desiredKeys_; 67 DriveKit::DKFieldKeyArray checkedKeys_ = {"version", "id"}; 68 std::vector<std::string> modifyFailSet_; 69 std::vector<std::string> createFailSet_; 70 71 int32_t userId_{0}; 72 std::string bundleName_; 73 DriveKit::DKRecordId rootId_; 74 75 int32_t CleanCloudRecord(const int32_t action); 76 void HandleCreateConvertErr(int32_t err, NativeRdb::ResultSet &resultSet); 77 void HandleFdirtyConvertErr(int32_t err, NativeRdb::ResultSet &resultSet); 78 CloudDiskDataConvertor createConvertor_ = { 79 userId_, bundleName_, FILE_CREATE, 80 std::bind(&CloudDiskDataHandler::HandleCreateConvertErr, this, std::placeholders::_1, std::placeholders::_2) 81 }; 82 CloudDiskDataConvertor deleteConvertor_ = { userId_, bundleName_, FILE_DELETE }; 83 CloudDiskDataConvertor mdirtyConvertor_ = { userId_, bundleName_, FILE_METADATA_MODIFY }; 84 CloudDiskDataConvertor fdirtyConvertor_ = { 85 userId_, bundleName_, FILE_DATA_MODIFY, 86 std::bind(&CloudDiskDataHandler::HandleFdirtyConvertErr, this, std::placeholders::_1, std::placeholders::_2) 87 }; 88 int32_t OnCreateRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 89 const std::unordered_map<std::string, LocalInfo> &localMap); 90 int32_t OnDeleteRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry); 91 int32_t OnModifyRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 92 const std::unordered_map<std::string, LocalInfo> &localMap); 93 bool IsTimeChanged(const DriveKit::DKRecord &record, const std::unordered_map<std::string, LocalInfo> &localMap, 94 const std::string &cloudId, const std::string &type); 95 int32_t GetLocalInfo(const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &map, 96 std::unordered_map<std::string, LocalInfo> &infoMap, const std::string &type); 97 int32_t BuildInfoMap(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 98 std::unordered_map<std::string, LocalInfo> &cloudMap, const std::string &type); 99 std::tuple<std::shared_ptr<NativeRdb::ResultSet>, std::map<std::string, int>> QueryLocalByCloudId( 100 const std::vector<std::string> &recordIds); 101 int32_t PullRecordInsert(DriveKit::DKRecord &record, OnFetchParams ¶ms); 102 int32_t PullRecordUpdate(DriveKit::DKRecord &record, NativeRdb::ResultSet &local, 103 OnFetchParams ¶ms); 104 int32_t PullRecordDelete(DriveKit::DKRecord &record, NativeRdb::ResultSet &local); 105 int32_t RecycleFile(const std::string &recordId); 106 int32_t GetMetaFilePath(const std::string &cloudId, std::string &path); 107 int32_t PullRecordConflict(DriveKit::DKRecord &record); 108 int32_t HandleConflict(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 109 std::string &fullName, const int &lastDot); 110 int32_t FindRenameFile(const std::shared_ptr<NativeRdb::ResultSet> resultSet, std::string &renameFileCloudId, 111 std::string &fullName, const int &lastDot); 112 int32_t ConflictReName(const std::string &cloudId, std::string newFileName); 113 int SetRetry(const std::string &recordId); 114 void AppendFileToDownload(const std::string &cloudId, 115 const std::string &fieldKey, 116 std::vector<DriveKit::DKDownloadAsset> &assetsToDownload); 117 118 CloudDiskDataConvertor localConvertor_ = { userId_, bundleName_, FILE_DOWNLOAD }; 119 int64_t UTCTimeMilliSeconds(); 120 std::mutex rdbMutex_; 121 }; 122 } // namespace CloudSync 123 } // namespace FileManagement 124 } // namespace OHOS 125 #endif // OHOS_CLOUD_SYNC_SERVICE_CLOUD_DISK_DATA_HANDLER_H 126