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