1 /*
2  * Copyright (C) 2021-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 OHOS_MEDIALIBRARY_SYNC_TABLE_H
17 #define OHOS_MEDIALIBRARY_SYNC_TABLE_H
18 
19 #include <string>
20 #include "abs_rdb_predicates.h"
21 #include "distributed_kv_data_manager.h"
22 #include "medialibrary_db_const.h"
23 #include "medialibrary_rdbstore.h"
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_store.h"
27 #include "rdb_store_config.h"
28 #include "rdb_types.h"
29 #include "single_kvstore.h"
30 
31 namespace OHOS {
32 namespace Media {
33 using namespace OHOS::NativeRdb;
34 #ifdef DISTRIBUTED
35 struct MediaLibrarySyncOpts {
36     std::shared_ptr<MediaLibraryRdbStore> rdbStore;
37     std::shared_ptr<DistributedKv::SingleKvStore> kvStore;
38     std::string table;
39     std::string bundleName;
40     std::string row;
41 };
42 #endif
43 class SyncStatus {
44 public:
45     std::condition_variable cond_;
46     std::mutex mtx_;
47     bool isSyncComplete_{false};
48 };
49 
50 class MediaLibrarySyncCallback : public DistributedKv::KvStoreSyncCallback {
51 public:
52     MediaLibrarySyncCallback() = default;
~MediaLibrarySyncCallback()53     ~MediaLibrarySyncCallback() override {}
54     void SyncCompleted(const std::map<std::string, DistributedKv::Status> &results) override;
55     bool WaitFor();
56 private:
57     SyncStatus status_;
58 };
59 #ifdef DISTRIBUTED
60 class MediaLibrarySyncOperation {
61 public:
62     MediaLibrarySyncOperation() = delete;
63     ~MediaLibrarySyncOperation() = delete;
64 
65     static bool SyncPullAllTableByNetworkId(MediaLibrarySyncOpts &syncOpts, std::vector<std::string> &devices);
66     static bool SyncPullTable(MediaLibrarySyncOpts &syncOpts, std::vector<std::string> &devices);
67     static bool SyncPushTable(MediaLibrarySyncOpts &syncOpts, std::vector<std::string> &devices, bool isBlock = false);
68     static DistributedKv::Status SyncPushKvstore(const std::shared_ptr<DistributedKv::SingleKvStore> &kvStore,
69         const std::vector<std::string> &key, const std::string &networkId);
70     static DistributedKv::Status SyncPullKvstore(const std::shared_ptr<DistributedKv::SingleKvStore> &kvStore,
71         const std::vector<std::string> &key, const std::string &networkId);
72 
73 private:
74     static void GetOnlineDevices(const std::string &bundleName, const std::vector<std::string> &originalDevices,
75         std::vector<std::string> &onlineDevices);
76 };
77 #endif
78 } // namespace Media
79 } // namespace OHOS
80 #endif // OHOS_MEDIALIBRARY_SYNC_TABLE_H
81