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 #include "virtual_asset_loader.h"
16 #include "log_print.h"
17
18 namespace DistributedDB {
Download(const std::string & tableName,const std::string & gid,const Type & prefix,std::map<std::string,Assets> & assets)19 DBStatus VirtualAssetLoader::Download(const std::string &tableName, const std::string &gid, const Type &prefix,
20 std::map<std::string, Assets> &assets)
21 {
22 {
23 std::lock_guard<std::mutex> autoLock(dataMutex_);
24 downloadCount_++;
25 bool isNeedSetStatus = downloadFailRange_.isAllFail || (downloadCount_ >= downloadFailRange_.failBeginIndex
26 && downloadCount_ <= downloadFailRange_.failEndIndex);
27 if (downloadStatus_ != OK && isNeedSetStatus) {
28 return downloadStatus_;
29 }
30 }
31 LOGD("Download GID:%s, table %s", gid.c_str(), tableName.c_str());
32 if (downloadCallBack_) {
33 downloadCallBack_(tableName, assets);
34 }
35 for (auto &item: assets) {
36 for (auto &asset: item.second) {
37 LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag);
38 asset.status = static_cast<uint32_t>(AssetStatus::NORMAL);
39 }
40 }
41 return OK;
42 }
43
RemoveLocalAssets(const std::vector<Asset> & assets)44 DBStatus VirtualAssetLoader::RemoveLocalAssets(const std::vector<Asset> &assets)
45 {
46 {
47 std::lock_guard<std::mutex> autoLock(dataMutex_);
48 if (removeStatus_ != OK) {
49 return removeStatus_;
50 }
51 }
52 if (removeAssetsCallBack_) {
53 return removeAssetsCallBack_(assets);
54 }
55 return DBStatus::OK;
56 }
57
RemoveLocalAssetsInner(const std::string & tableName,const std::string & gid,const Type & prefix,std::map<std::string,Assets> & assets)58 DBStatus VirtualAssetLoader::RemoveLocalAssetsInner(const std::string &tableName, const std::string &gid,
59 const Type &prefix, std::map<std::string, Assets> &assets)
60 {
61 DBStatus errCode = DBStatus::OK;
62 if (removeLocalAssetsCallBack_) {
63 errCode = removeLocalAssetsCallBack_(assets);
64 }
65 if (errCode != DBStatus::OK) {
66 return errCode;
67 }
68 LOGD("RemoveLocalAssets GID:%s", gid.c_str());
69 for (auto &item: assets) {
70 for (auto &asset: item.second) {
71 LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag);
72 asset.status = static_cast<uint32_t>(AssetStatus::NORMAL);
73 }
74 }
75 return DBStatus::OK;
76 }
77
RemoveLocalAssets(const std::string & tableName,const std::string & gid,const Type & prefix,std::map<std::string,Assets> & assets)78 DBStatus VirtualAssetLoader::RemoveLocalAssets(const std::string &tableName, const std::string &gid, const Type &prefix,
79 std::map<std::string, Assets> &assets)
80 {
81 {
82 std::lock_guard<std::mutex> autoLock(dataMutex_);
83 if (removeStatus_ != OK) {
84 return removeStatus_;
85 }
86 }
87 return RemoveLocalAssetsInner(tableName, gid, prefix, assets);
88 }
89
SetDownloadStatus(DBStatus status)90 void VirtualAssetLoader::SetDownloadStatus(DBStatus status)
91 {
92 std::lock_guard<std::mutex> autoLock(dataMutex_);
93 LOGD("[VirtualAssetLoader] set download status :%d", static_cast<int>(status));
94 downloadStatus_ = status;
95 }
96
SetDownloadFailRange(const DownloadFailRange & setRange)97 void VirtualAssetLoader::SetDownloadFailRange(const DownloadFailRange &setRange)
98 {
99 std::lock_guard<std::mutex> autoLock(dataMutex_);
100 LOGD("[VirtualAssetLoader] set download fail range :isAllFail=%d, from %u to %u",
101 setRange.isAllFail, setRange.failBeginIndex, setRange.failEndIndex);
102 downloadFailRange_ = setRange;
103 }
104
SetRemoveStatus(DBStatus status)105 void VirtualAssetLoader::SetRemoveStatus(DBStatus status)
106 {
107 std::lock_guard<std::mutex> autoLock(dataMutex_);
108 LOGD("[VirtualAssetLoader] set remove status :%d", static_cast<int>(status));
109 removeStatus_ = status;
110 }
111
SetBatchRemoveStatus(DBStatus status)112 void VirtualAssetLoader::SetBatchRemoveStatus(DBStatus status)
113 {
114 std::lock_guard<std::mutex> autoLock(dataMutex_);
115 LOGD("[VirtualAssetLoader] set batch remove status :%d", static_cast<int>(status));
116 batchRemoveStatus_ = status;
117 }
118
ForkDownload(const DownloadCallBack & callback)119 void VirtualAssetLoader::ForkDownload(const DownloadCallBack &callback)
120 {
121 downloadCallBack_ = callback;
122 }
123
ForkRemoveLocalAssets(const RemoveAssetsCallBack & callback)124 void VirtualAssetLoader::ForkRemoveLocalAssets(const RemoveAssetsCallBack &callback)
125 {
126 removeAssetsCallBack_ = callback;
127 }
128
SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack & callback)129 void VirtualAssetLoader::SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack &callback)
130 {
131 removeLocalAssetsCallBack_ = callback;
132 }
133
BatchDownload(const std::string & tableName,std::vector<AssetRecord> & downloadAssets)134 void VirtualAssetLoader::BatchDownload(const std::string &tableName, std::vector<AssetRecord> &downloadAssets)
135 {
136 batchDownloadCount_++;
137 int index = 0;
138 for (auto &[gid, prefix, assets, status] : downloadAssets) {
139 if (batchDownloadCallback_) {
140 status = batchDownloadCallback_(index, assets);
141 } else {
142 status = Download(tableName, gid, prefix, assets);
143 }
144 index++;
145 }
146 }
147
BatchRemoveLocalAssets(const std::string & tableName,std::vector<AssetRecord> & removeAssets)148 void VirtualAssetLoader::BatchRemoveLocalAssets(const std::string &tableName,
149 std::vector<AssetRecord> &removeAssets)
150 {
151 removeCount_++;
152 for (auto &[gid, prefix, assets, status] : removeAssets) {
153 if (batchRemoveStatus_ != OK) {
154 status = batchRemoveStatus_;
155 } else {
156 status = RemoveLocalAssetsInner(tableName, gid, prefix, assets);
157 }
158 }
159 }
160
GetBatchDownloadCount()161 uint32_t VirtualAssetLoader::GetBatchDownloadCount()
162 {
163 return batchDownloadCount_;
164 }
165
GetBatchRemoveCount()166 uint32_t VirtualAssetLoader::GetBatchRemoveCount()
167 {
168 return removeCount_;
169 }
170
Reset()171 void VirtualAssetLoader::Reset()
172 {
173 removeCount_ = 0;
174 batchDownloadCount_ = 0;
175 downloadCount_ = 0;
176 downloadFailRange_.isAllFail = true;
177 }
178
ForkBatchDownload(const BatchDownloadCallback & callback)179 void VirtualAssetLoader::ForkBatchDownload(const BatchDownloadCallback &callback)
180 {
181 batchDownloadCallback_ = callback;
182 }
183
CancelDownload()184 DBStatus VirtualAssetLoader::CancelDownload()
185 {
186 cancelCount_++;
187 return OK;
188 }
189
GetCancelCount() const190 uint32_t VirtualAssetLoader::GetCancelCount() const
191 {
192 return cancelCount_;
193 }
194 }
195