• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (downloadStatus_ != OK) {
25             return downloadStatus_;
26         }
27     }
28     LOGD("Download GID:%s", gid.c_str());
29     if (downloadCallBack_) {
30         downloadCallBack_(assets);
31     }
32     for (auto &item: assets) {
33         for (auto &asset: item.second) {
34             LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag);
35             asset.status = static_cast<uint32_t>(AssetStatus::NORMAL);
36         }
37     }
38     return OK;
39 }
40 
RemoveLocalAssets(const std::vector<Asset> & assets)41 DBStatus VirtualAssetLoader::RemoveLocalAssets(const std::vector<Asset> &assets)
42 {
43     if (removeAssetsCallBack_) {
44         return removeAssetsCallBack_(assets);
45     }
46     return DBStatus::OK;
47 }
48 
RemoveLocalAssets(const std::string & tableName,const std::string & gid,const Type & prefix,std::map<std::string,Assets> & assets)49 DBStatus VirtualAssetLoader::RemoveLocalAssets(const std::string &tableName, const std::string &gid, const Type &prefix,
50     std::map<std::string, Assets> &assets)
51 {
52     if (removeLocalAssetsCallBack_) {
53         removeLocalAssetsCallBack_(assets);
54     }
55     LOGD("RemoveLocalAssets GID:%s", gid.c_str());
56     for (auto &item: assets) {
57         for (auto &asset: item.second) {
58             LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag);
59             asset.status = static_cast<uint32_t>(AssetStatus::NORMAL);
60         }
61     }
62     return DBStatus::OK;
63 }
64 
SetDownloadStatus(DBStatus status)65 void VirtualAssetLoader::SetDownloadStatus(DBStatus status)
66 {
67     std::lock_guard<std::mutex> autoLock(dataMutex_);
68     LOGD("[VirtualAssetLoader] set download status :%d", static_cast<int>(status));
69     downloadStatus_ = status;
70 }
71 
ForkDownload(const DownloadCallBack & callback)72 void VirtualAssetLoader::ForkDownload(const DownloadCallBack &callback)
73 {
74     downloadCallBack_ = callback;
75 }
76 
ForkRemoveLocalAssets(const RemoveAssetsCallBack & callback)77 void VirtualAssetLoader::ForkRemoveLocalAssets(const RemoveAssetsCallBack &callback)
78 {
79     removeAssetsCallBack_ = callback;
80 }
81 
SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack & callback)82 void VirtualAssetLoader::SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack &callback)
83 {
84     removeLocalAssetsCallBack_ = callback;
85 }
86 }
87