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 #include "data_handler.h"
17
18 #include "dfs_error.h"
19 #include "utils_log.h"
20 namespace OHOS {
21 namespace FileManagement {
22 namespace CloudSync {
23 using namespace std;
DataHandler(int32_t userId,const string & bundleName,const std::string & table)24 DataHandler::DataHandler(int32_t userId, const string &bundleName, const std::string &table)
25 : cloudPrefImpl_(userId, bundleName, table)
26 {
27 cloudPrefImpl_.GetString(START_CURSOR, startCursor_);
28 cloudPrefImpl_.GetString(NEXT_CURSOR, nextCursor_);
29 cloudPrefImpl_.GetString(NEXT_CURSOR, tempNextCursor_);
30 cloudPrefImpl_.GetInt(BATCH_NO, batchNo_);
31 cloudPrefImpl_.GetInt(RECORD_SIZE, recordSize_);
32 cloudPrefImpl_.GetBool(CHECKING_FLAG, isChecking_);
33 }
34
GetNextCursor(DriveKit::DKQueryCursor & cursor)35 void DataHandler::GetNextCursor(DriveKit::DKQueryCursor &cursor)
36 {
37 if (tempNextCursor_.empty()) {
38 cursor = startCursor_;
39 return;
40 }
41 cursor = tempNextCursor_;
42 }
43
SetTempStartCursor(const DriveKit::DKQueryCursor & cursor)44 void DataHandler::SetTempStartCursor(const DriveKit::DKQueryCursor &cursor)
45 {
46 tempStartCursor_ = cursor;
47 cloudPrefImpl_.SetString(TEMP_START_CURSOR, tempStartCursor_);
48 }
49
GetTempStartCursor(DriveKit::DKQueryCursor & cursor)50 void DataHandler::GetTempStartCursor(DriveKit::DKQueryCursor &cursor)
51 {
52 cursor = tempStartCursor_;
53 }
54
SetTempNextCursor(const DriveKit::DKQueryCursor & cursor,bool isFinish)55 void DataHandler::SetTempNextCursor(const DriveKit::DKQueryCursor &cursor, bool isFinish)
56 {
57 LOGI("batchNo_ %{public}d set temp next cursor %{public}s, isFinish %{public}d",
58 batchNo_, cursor.c_str(), isFinish);
59 tempNextCursor_ = cursor;
60 cursorMap_.insert(std::pair<int32_t, DriveKit::DKQueryCursor>(batchNo_, cursor));
61 cursorFinishMap_.insert(std::pair<int32_t, bool>(batchNo_, false));
62 if (!isFinish) {
63 batchNo_ ++;
64 } else {
65 isFinish_ = true;
66 }
67 }
68
GetBatchNo()69 int32_t DataHandler::GetBatchNo()
70 {
71 return batchNo_;
72 }
73
IsPullRecords()74 bool DataHandler::IsPullRecords()
75 {
76 return startCursor_.empty();
77 }
78
ClearCursor()79 void DataHandler::ClearCursor()
80 {
81 startCursor_.clear();
82 nextCursor_.clear();
83 tempStartCursor_.clear();
84 tempNextCursor_.clear();
85 batchNo_ = 0;
86 recordSize_ = 0;
87 isFinish_ = false;
88 isChecking_ = false;
89 cursorMap_.clear();
90 cursorFinishMap_.clear();
91 cloudPrefImpl_.SetString(START_CURSOR, startCursor_);
92 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
93 cloudPrefImpl_.SetInt(BATCH_NO, batchNo_);
94 cloudPrefImpl_.Delete(TEMP_START_CURSOR);
95 cloudPrefImpl_.Delete(RECORD_SIZE);
96 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
97 }
98
FinishPull(const int32_t batchNo)99 void DataHandler::FinishPull(const int32_t batchNo)
100 {
101 std::lock_guard<std::mutex> lock(mutex_);
102 LOGI("batchNo %{public}d finish pull", batchNo);
103 cursorFinishMap_[batchNo] = true;
104 if (cursorFinishMap_.begin()->first == batchNo) {
105 while (!cursorFinishMap_.empty() && cursorFinishMap_.begin()->second) {
106 nextCursor_ = cursorMap_.begin()->second;
107 cloudPrefImpl_.SetInt(BATCH_NO, cursorFinishMap_.begin()->first);
108 cursorMap_.erase(cursorMap_.begin()->first);
109 cursorFinishMap_.erase(cursorFinishMap_.begin()->first);
110 }
111 }
112 LOGD("The next cursor is %{public}s", nextCursor_.c_str());
113 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
114
115 if (cursorMap_.empty() && isFinish_) {
116 if (IsPullRecords()) {
117 LOGD("PullRecords finish all");
118 startCursor_ = tempStartCursor_;
119 tempStartCursor_.clear();
120 recordSize_ = 0;
121 isChecking_ = false;
122 cloudPrefImpl_.Delete(TEMP_START_CURSOR);
123 cloudPrefImpl_.Delete(RECORD_SIZE);
124 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
125 } else {
126 LOGD("PullDatabaseChanged finish all");
127 startCursor_ = nextCursor_;
128 }
129 nextCursor_.clear();
130 tempNextCursor_.clear();
131 cloudPrefImpl_.SetString(START_CURSOR, startCursor_);
132 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
133 cloudPrefImpl_.Delete(BATCH_NO);
134 batchNo_ = 0;
135 isFinish_ = false;
136 }
137 LOGD("When batchNo finish pull startCursor %{public}s, nextCursor %{public}s, tempStartCursor %{public}s,",
138 startCursor_.c_str(), nextCursor_.c_str(), tempStartCursor_.c_str());
139 LOGD("tempNextCursor %{public}s, batchNo_ %{public}d", tempNextCursor_.c_str(), batchNo_);
140 }
141
SetRecordSize(const int32_t recordSize)142 void DataHandler::SetRecordSize(const int32_t recordSize)
143 {
144 recordSize_ = recordSize;
145 cloudPrefImpl_.SetInt(RECORD_SIZE, recordSize_);
146 }
147
GetRecordSize()148 int32_t DataHandler::GetRecordSize()
149 {
150 return recordSize_;
151 }
152
GetCheckFlag()153 bool DataHandler::GetCheckFlag()
154 {
155 return isChecking_;
156 }
157
SetChecking()158 void DataHandler::SetChecking()
159 {
160 ClearCursor();
161 isChecking_ = true;
162 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
163 }
164
GetCheckRecords(std::vector<DriveKit::DKRecordId> & checkRecords,const std::shared_ptr<std::vector<DriveKit::DKRecord>> & records)165 int32_t DataHandler::GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords,
166 const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records)
167 {
168 return E_OK;
169 }
170
GetFileModifiedRecords(vector<DriveKit::DKRecord> & records)171 int32_t DataHandler::GetFileModifiedRecords(vector<DriveKit::DKRecord> &records)
172 {
173 return E_OK;
174 }
175
OnModifyFdirtyRecords(const map<DriveKit::DKRecordId,DriveKit::DKRecordOperResult> & map)176 int32_t DataHandler::OnModifyFdirtyRecords(const map<DriveKit::DKRecordId,
177 DriveKit::DKRecordOperResult> &map)
178 {
179 return E_OK;
180 }
181
GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> & outAssetsToDownload)182 int32_t DataHandler::GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload)
183 {
184 return E_OK;
185 }
186
GetDownloadAsset(string cloudId,vector<DriveKit::DKDownloadAsset> & outAssetsToDownload)187 int32_t DataHandler::GetDownloadAsset(string cloudId,
188 vector<DriveKit::DKDownloadAsset> &outAssetsToDownload)
189 {
190 return E_OK;
191 }
192
OnDownloadSuccess(const DriveKit::DKDownloadAsset & asset)193 int32_t DataHandler::OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset)
194 {
195 return E_OK;
196 }
197
OnDownloadThumb(const std::map<DriveKit::DKDownloadAsset,DriveKit::DKDownloadResult> & resultMap)198 int32_t DataHandler::OnDownloadThumb(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap)
199 {
200 return E_OK;
201 }
202
OnDownloadThumb(const DriveKit::DKDownloadAsset & asset)203 int32_t DataHandler::OnDownloadThumb(const DriveKit::DKDownloadAsset &asset)
204 {
205 return E_OK;
206 }
207
Clean(const int action)208 int32_t DataHandler::Clean(const int action)
209 {
210 return E_OK;
211 }
212 } // namespace CloudSync
213 } // namespace FileManagement
214 } // namespace OHOS