1 /*
2 * Copyright (C) 2021 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 #define MLOG_TAG "Distributed"
16
17 #include "medialibrary_sync_table.h"
18 #include "media_log.h"
19 #include "medialibrary_tracer.h"
20 #include "medialibrary_errno.h"
21
22 namespace OHOS {
23 namespace Media {
24 using namespace std;
25 using namespace OHOS::AppExecFwk;
26 constexpr int TABLE_NUM = 4;
27 static std::array<std::string, TABLE_NUM> table_arr = {
28 MEDIALIBRARY_TABLE, SMARTALBUM_TABLE, SMARTALBUM_MAP_TABLE, CATEGORY_SMARTALBUM_MAP_TABLE
29 };
30
SyncPullAllTableByDeviceId(const shared_ptr<RdbStore> & rdbStore,const std::string & bundleName,std::vector<std::string> & devices)31 bool MediaLibrarySyncTable::SyncPullAllTableByDeviceId(
32 const shared_ptr<RdbStore> &rdbStore, const std::string &bundleName, std::vector<std::string> &devices)
33 {
34 if (rdbStore == nullptr) {
35 MEDIA_ERR_LOG("MediaLibrarySyncTable SyncPullAllTable rdbStore is null");
36 return false;
37 }
38
39 for (auto &table_name : table_arr) {
40 auto ret = SyncPullTable(rdbStore, bundleName, table_name, devices);
41 if (!ret) {
42 MEDIA_ERR_LOG("sync pull table %{public}s failed, err %{public}d", table_name.c_str(), ret);
43 }
44 }
45
46 return true;
47 }
48
SyncPullTable(const shared_ptr<RdbStore> & rdbStore,const std::string & bundleName,const std::string & tableName,std::vector<std::string> & devices,bool isLast)49 bool MediaLibrarySyncTable::SyncPullTable(
50 const shared_ptr<RdbStore> &rdbStore, const std::string &bundleName, const std::string &tableName,
51 std::vector<std::string> &devices, bool isLast)
52 {
53 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, false, "Rdb Store is not initialized");
54 // start sync
55 DistributedRdb::SyncOption option;
56 option.mode = DistributedRdb::SyncMode::PULL;
57 option.isBlock = true;
58
59 NativeRdb::AbsRdbPredicates predicate(tableName.c_str());
60 (devices.size() > 0) ? predicate.InDevices(devices) : predicate.InAllDevices();
61
62 DistributedRdb::SyncCallback callback = [tableName](const DistributedRdb::SyncResult& syncResult) {
63 // update device db
64 for (auto iter = syncResult.begin(); iter != syncResult.end(); iter++) {
65 if (iter->first.empty()) {
66 MEDIA_ERR_LOG("SyncPullTable networkId is empty");
67 continue;
68 }
69 if (iter->second != 0) {
70 MEDIA_ERR_LOG("SyncPullTable tableName = %{public}s device = %{private}s syncResult = %{private}d",
71 tableName.c_str(), iter->first.c_str(), iter->second);
72 continue;
73 }
74 if (tableName == MEDIALIBRARY_TABLE) {
75 MediaLibraryDevice::GetInstance()->UpdateDeviceSyncStatus(iter->first, DEVICE_SYNCSTATUS_COMPLETE);
76 }
77 MEDIA_ERR_LOG("SyncPullTable tableName = %{public}s device = %{private}s success",
78 tableName.c_str(), iter->first.c_str());
79 }
80 };
81
82 uint32_t count = 0;
83 while (count++ < RETRY_COUNT) {
84 MediaLibraryTracer tracer;
85 tracer.Start("abilityHelper->Query");
86 int ret = rdbStore->Sync(option, predicate, callback);
87 if (ret == E_OK) {
88 return true;
89 }
90 }
91 return false;
92 }
93
SyncPushTable(const shared_ptr<RdbStore> & rdbStore,const std::string & bundleName,const std::string & tableName,std::vector<std::string> & devices,bool isBlock)94 bool MediaLibrarySyncTable::SyncPushTable(const shared_ptr<RdbStore> &rdbStore, const std::string &bundleName,
95 const std::string &tableName, std::vector<std::string> &devices, bool isBlock)
96 {
97 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, false, "Rdb Store is not initialized");
98 // start sync
99 DistributedRdb::SyncOption option;
100 option.mode = DistributedRdb::SyncMode::PUSH;
101 option.isBlock = isBlock;
102
103 NativeRdb::AbsRdbPredicates predicate(tableName.c_str());
104 (devices.size() > 0) ? predicate.InDevices(devices) : predicate.InAllDevices();
105
106 DistributedRdb::SyncCallback callback = [tableName](const DistributedRdb::SyncResult& syncResult) {
107 // update device db
108 for (auto iter = syncResult.begin(); iter != syncResult.end(); iter++) {
109 if (iter->first.empty()) {
110 MEDIA_ERR_LOG("SyncPushTable networkId is empty");
111 continue;
112 }
113 if (iter->second != 0) {
114 MEDIA_ERR_LOG("SyncPushTable tableName = %{public}s device = %{private}s syncResult = %{private}d",
115 tableName.c_str(), iter->first.c_str(), iter->second);
116 continue;
117 }
118 MEDIA_INFO_LOG("SyncPushTable tableName = %{public}s, device = %{private}s success",
119 tableName.c_str(), iter->first.c_str());
120 }
121 };
122
123 MediaLibraryTracer tracer;
124 tracer.Start("SyncPushTable rdbStore->Sync");
125 int ret = rdbStore->Sync(option, predicate, callback);
126
127 return ret == E_OK;
128 }
129 } // namespace Media
130 } // namespace OHOS
131