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     DBStatus errCode = DBStatus::OK;
53     if (removeLocalAssetsCallBack_) {
54         errCode = removeLocalAssetsCallBack_(assets);
55     }
56     if (errCode != DBStatus::OK) {
57         return errCode;
58     }
59     LOGD("RemoveLocalAssets GID:%s", gid.c_str());
60     for (auto &item: assets) {
61         for (auto &asset: item.second) {
62             LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag);
63             asset.status = static_cast<uint32_t>(AssetStatus::NORMAL);
64         }
65     }
66     return DBStatus::OK;
67 }
68 
SetDownloadStatus(DBStatus status)69 void VirtualAssetLoader::SetDownloadStatus(DBStatus status)
70 {
71     std::lock_guard<std::mutex> autoLock(dataMutex_);
72     LOGD("[VirtualAssetLoader] set download status :%d", static_cast<int>(status));
73     downloadStatus_ = status;
74 }
75 
ForkDownload(const DownloadCallBack & callback)76 void VirtualAssetLoader::ForkDownload(const DownloadCallBack &callback)
77 {
78     downloadCallBack_ = callback;
79 }
80 
ForkRemoveLocalAssets(const RemoveAssetsCallBack & callback)81 void VirtualAssetLoader::ForkRemoveLocalAssets(const RemoveAssetsCallBack &callback)
82 {
83     removeAssetsCallBack_ = callback;
84 }
85 
SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack & callback)86 void VirtualAssetLoader::SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack &callback)
87 {
88     removeLocalAssetsCallBack_ = callback;
89 }
90 }
91