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 <mutex>
19 #include "iAssetLoader.h"
20 
21 namespace DistributedDB {
22 using DownloadCallBack = std::function<void (std::map<std::string, Assets> &assets)>;
23 using RemoveAssetsCallBack = std::function<DBStatus (const std::vector<Asset> &assets)>;
24 using RemoveLocalAssetsCallBack = std::function<DBStatus (std::map<std::string, Assets> &assets)>;
25 class VirtualAssetLoader : public IAssetLoader {
26 public:
27     VirtualAssetLoader() = default;
28     ~VirtualAssetLoader() override = default;
29 
30     DBStatus Download(const std::string &tableName, const std::string &gid, const Type &prefix,
31         std::map<std::string, Assets> &assets) override;
32 
33     DBStatus RemoveLocalAssets(const std::vector<Asset> &assets) override;
34 
35     DBStatus RemoveLocalAssets(const std::string &tableName, const std::string &gid, const Type &prefix,
36              std::map<std::string, Assets> &assets) override;
37 
38     void SetDownloadStatus(DBStatus status);
39 
40     void ForkDownload(const DownloadCallBack &callback);
41 
42     void ForkRemoveLocalAssets(const RemoveAssetsCallBack &callback);
43 
44     void SetRemoveLocalAssetsCallback(const RemoveLocalAssetsCallBack &callback);
45 private:
46     std::mutex dataMutex_;
47     DBStatus downloadStatus_ = OK;
48     DownloadCallBack downloadCallBack_;
49     RemoveAssetsCallBack removeAssetsCallBack_;
50     RemoveLocalAssetsCallBack removeLocalAssetsCallBack_;
51 };
52 }
53 #endif // VIRTUAL_ASSETLOADER_H
54