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 VIRTUAL_ASSETLOADER_H 17 #define VIRTUAL_ASSETLOADER_H 18 #include <atomic> 19 #include <mutex> 20 #include "iAssetLoader.h" 21 22 namespace DistributedDB { 23 using DownloadCallBack = std::function<void (const std::string &tableName, std::map<std::string, Assets> &assets)>; 24 using RemoveAssetsCallBack = std::function<DBStatus (const std::vector<Asset> &assets)>; 25 using RemoveLocalAssetsCallBack = std::function<DBStatus (std::map<std::string, Assets> &assets)>; 26 using BatchDownloadCallback = std::function<DBStatus (int rowIndex, std::map<std::string, Assets> &assets)>; 27 28 struct DownloadFailRange { 29 bool isAllFail = true; 30 uint32_t failBeginIndex = 0; 31 uint32_t failEndIndex = 0; 32 }; 33 34 class VirtualAssetLoader : public IAssetLoader { 35 public: 36 VirtualAssetLoader() = default; 37 ~VirtualAssetLoader() override = default; 38 39 DBStatus Download(const std::string &tableName, const std::string &gid, const Type &prefix, 40 std::map<std::string, Assets> &assets) override; 41 42 DBStatus RemoveLocalAssets(const std::vector<Asset> &assets) override; 43 44 DBStatus RemoveLocalAssets(const std::string &tableName, const std::string &gid, const Type &prefix, 45 std::map<std::string, Assets> &assets) override; 46 47 void SetDownloadStatus(DBStatus status); 48 49 void SetRemoveStatus(DBStatus status); 50 51 void SetBatchRemoveStatus(DBStatus status); 52 53 void ForkDownload(const DownloadCallBack &callback); 54 55 void ForkRemoveLocalAssets(const RemoveAssetsCallBack &callback); 56 57 void SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack &callback); 58 59 void BatchDownload(const std::string &tableName, std::vector<AssetRecord> &downloadAssets) override; 60 61 void BatchRemoveLocalAssets(const std::string &tableName, std::vector<AssetRecord> &removeAssets) override; 62 63 uint32_t GetBatchDownloadCount(); 64 65 uint32_t GetBatchRemoveCount(); 66 67 void Reset(); 68 69 void ForkBatchDownload(const BatchDownloadCallback &callback); 70 71 DBStatus CancelDownload() override; 72 73 uint32_t GetCancelCount() const; 74 75 void SetDownloadFailRange(const DownloadFailRange &setRange); 76 private: 77 DBStatus RemoveLocalAssetsInner(const std::string &tableName, const std::string &gid, const Type &prefix, 78 std::map<std::string, Assets> &assets); 79 80 std::mutex dataMutex_; 81 DBStatus downloadStatus_ = OK; 82 DBStatus removeStatus_ = OK; 83 DBStatus batchRemoveStatus_ = OK; 84 std::atomic<uint32_t> batchDownloadCount_ = 0; 85 std::atomic<uint32_t> removeCount_ = 0; 86 std::atomic<uint32_t> cancelCount_ = 0; 87 DownloadCallBack downloadCallBack_; 88 RemoveAssetsCallBack removeAssetsCallBack_; 89 RemoveLocalAssetsCallBack removeLocalAssetsCallBack_; 90 BatchDownloadCallback batchDownloadCallback_; 91 DownloadFailRange downloadFailRange_; 92 std::atomic<uint32_t> downloadCount_ = 0; 93 }; 94 } 95 #endif // VIRTUAL_ASSETLOADER_H 96