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 #define LOG_TAG "RdbAssetLoader"
17 #include "rdb_asset_loader.h"
18
19 #include "error/general_error.h"
20 #include "log_print.h"
21 #include "rdb_cloud.h"
22 #include "value_proxy.h"
23
24 using namespace DistributedDB;
25 using ValueProxy = OHOS::DistributedData::ValueProxy;
26 namespace OHOS::DistributedRdb {
RdbAssetLoader(std::shared_ptr<DistributedData::AssetLoader> cloudAssetLoader,BindAssets * bindAssets)27 RdbAssetLoader::RdbAssetLoader(std::shared_ptr<DistributedData::AssetLoader> cloudAssetLoader, BindAssets* bindAssets)
28 : assetLoader_(std::move(cloudAssetLoader)), snapshots_(bindAssets)
29 {
30 }
31
Download(const std::string & tableName,const std::string & gid,const Type & prefix,std::map<std::string,Assets> & assets)32 DBStatus RdbAssetLoader::Download(const std::string &tableName, const std::string &gid, const Type &prefix,
33 std::map<std::string, Assets> &assets)
34 {
35 DistributedData::VBucket downLoadAssets = ValueProxy::Convert(assets);
36 std::set<std::string> skipAssets;
37 std::set<std::string> deleteAssets;
38 PostEvent(skipAssets, downLoadAssets, DistributedData::AssetEvent::DOWNLOAD, deleteAssets);
39 DistributedDB::Type prefixTemp = prefix;
40 auto error = assetLoader_->Download(tableName, gid, ValueProxy::Convert(std::move(prefixTemp)), downLoadAssets);
41 PostEvent(skipAssets, downLoadAssets, DistributedData::AssetEvent::DOWNLOAD_FINISHED, deleteAssets);
42 assets = ValueProxy::Convert(std::move(downLoadAssets));
43 return skipAssets.empty() ? RdbCloud::ConvertStatus(static_cast<DistributedData::GeneralError>(error))
44 : CLOUD_RECORD_EXIST_CONFLICT;
45 }
46
RemoveLocalAssets(const std::vector<Asset> & assets)47 DBStatus RdbAssetLoader::RemoveLocalAssets(const std::vector<Asset> &assets)
48 {
49 DistributedData::VBucket deleteAssets = ValueProxy::Convert(std::map<std::string, Assets>{{ "", assets }});
50 auto error = assetLoader_->RemoveLocalAssets("", "", {}, deleteAssets);
51 return RdbCloud::ConvertStatus(static_cast<DistributedData::GeneralError>(error));
52 }
53
PostEvent(std::set<std::string> & skipAssets,std::map<std::string,DistributedData::Value> & assets,DistributedData::AssetEvent eventId,std::set<std::string> & deleteAssets)54 void RdbAssetLoader::PostEvent(std::set<std::string>& skipAssets, std::map<std::string, DistributedData::Value>& assets,
55 DistributedData::AssetEvent eventId, std::set<std::string>& deleteAssets)
56 {
57 for (auto& asset : assets) {
58 auto* downLoadAssets = Traits::get_if<DistributedData::Assets>(&asset.second);
59 if (downLoadAssets == nullptr) {
60 return;
61 }
62 PostEvent(eventId, *downLoadAssets, skipAssets, deleteAssets);
63 }
64 }
65
PostEvent(DistributedData::AssetEvent eventId,DistributedData::Assets & assets,std::set<std::string> & skipAssets,std::set<std::string> & deleteAssets)66 void RdbAssetLoader::PostEvent(DistributedData::AssetEvent eventId, DistributedData::Assets& assets,
67 std::set<std::string>& skipAssets, std::set<std::string>& deleteAssets)
68 {
69 for (auto& downLoadAsset : assets) {
70 if (downLoadAsset.status == DistributedData::Asset::STATUS_DELETE) {
71 deleteAssets.insert(downLoadAsset.uri);
72 continue;
73 }
74 if (snapshots_->bindAssets == nullptr) {
75 continue;
76 }
77 auto it = snapshots_->bindAssets->find(downLoadAsset.uri);
78 if (it == snapshots_->bindAssets->end() || it->second == nullptr) {
79 continue;
80 }
81 auto snapshot = it->second;
82 if (eventId == DistributedData::DOWNLOAD) {
83 snapshot->Download(downLoadAsset);
84 if (snapshot->GetAssetStatus(downLoadAsset) == DistributedData::STATUS_WAIT_DOWNLOAD) {
85 skipAssets.insert(downLoadAsset.uri);
86 }
87 } else {
88 auto skipPos = skipAssets.find(downLoadAsset.uri);
89 auto deletePos = deleteAssets.find(downLoadAsset.uri);
90 if (skipPos != skipAssets.end() || deletePos != skipAssets.end()) {
91 continue;
92 }
93 snapshot->Downloaded(downLoadAsset);
94 }
95 }
96 }
97 } // namespace OHOS::DistributedRdb