From 5df733ca99bf14bdc3c2af284bb846c31299e899 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Wed, 19 Apr 2023 04:18:06 +0000 Subject: [PATCH 1/3] add data convertor Signed-off-by: jiahaoluo Change-Id: I3cad3e8272504a4d6102bb58971f621347b0b28a --- services/cloudsyncservice/BUILD.gn | 1 + .../gallery_data_sync/data_convertor.h | 66 +-- .../gallery_data_sync/file_data_convertor.h | 88 ++++ .../gallery_data_sync/file_data_handler.h | 60 +-- .../gallery_data_sync/gallery_file_const.h | 55 +++ .../include/data_sync/sdk_helper.h | 4 + .../src/data_sync/data_syncer.cpp | 6 + .../gallery_data_sync/data_convertor.cpp | 123 ++---- .../gallery_data_sync/file_data_convertor.cpp | 396 ++++++++++++++++++ .../gallery_data_sync/file_data_handler.cpp | 62 +-- .../gallery_data_sync/gallery_data_syncer.cpp | 2 +- .../src/data_sync/sdk_helper.cpp | 29 +- services/distributedfile.cfg | 4 +- utils/log/include/dfs_error.h | 1 + 14 files changed, 678 insertions(+), 219 deletions(-) create mode 100644 services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h create mode 100644 services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_file_const.h create mode 100644 services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index 44826b046..2e251ab19 100644 --- a/services/cloudsyncservice/BUILD.gn +++ b/services/cloudsyncservice/BUILD.gn @@ -36,6 +36,7 @@ ohos_shared_library("cloudsync_sa") { "src/data_sync/gallery_data_sync/file_data_handler.cpp", "src/data_sync/gallery_data_sync/album_data_handler.cpp", "src/data_sync/gallery_data_sync/data_convertor.cpp", + "src/data_sync/gallery_data_sync/file_data_convertor.cpp" ] sources = [ diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/data_convertor.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/data_convertor.h index 76c5d06c6..6675a648d 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/data_convertor.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/data_convertor.h @@ -26,71 +26,23 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { -enum DataType : int32_t { - INT, - LONG, - STRING, - DOUBLE, - BOOL, - BLOB, - ASSET -}; - class DataConvertor { public: - DataConvertor(const std::vector localColumns, - const std::vector cloudColumns, - const std::vector types, int32_t size); + DataConvertor() = default; virtual ~DataConvertor() = default; + virtual int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet) = 0; + int32_t ResultSetToRecords(const std::unique_ptr resultSet, std::vector &records); - int32_t RecordToValueBucket(const DriveKit::DKRecord &record, - NativeRdb::ValuesBucket &valueBucket); - int32_t RecordsToValueBuckets(const std::vector &records, - std::vector &valueBuckets); - - const std::vector &GetLocalColumns() const - { - return localColumns_; - } - - const std::vector &GetCloudColumns() const - { - return cloudColumns_; - } - - const std::vector &GetTypes() const - { - return types_; - } - - int32_t GetSize() const - { - return size_; - } - -private: - void HandleInt(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet); - void HandleLong(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet); - void HandleString(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet); - void HandleAsset(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet); - - /* handler map */ - using ConvertorHandler = void (DataConvertor::*)(DriveKit::DKRecordData &data, - const DriveKit::DKFieldKey &key, int32_t index, NativeRdb::ResultSet &resultSet); - std::map opToHandlerMap_; + int32_t RecordToValueBucket(const DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket); - const std::vector localColumns_; - const std::vector cloudColumns_; - const std::vector types_; - const int32_t size_; +protected: + int32_t GetInt(const std::string &key, int32_t &val, NativeRdb::ResultSet &resultSet); + int32_t GetLong(const std::string &key, int64_t &val, NativeRdb::ResultSet &resultSet); + int32_t GetString(const std::string &key, std::string &val, NativeRdb::ResultSet &resultSet); }; } // namespace CloudSync } // namespace FileManagement } // namespace OHOS -#endif // OHOS_CLOUD_SYNC_SERVICE_DATA_CONVERTOR_H \ No newline at end of file +#endif // OHOS_CLOUD_SYNC_SERVICE_DATA_CONVERTOR_H diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h new file mode 100644 index 000000000..f21a563b2 --- /dev/null +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_CONVERTOR_H +#define OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_CONVERTOR_H + +#include + +#include "data_convertor.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class FileDataConvertor : public DataConvertor { +public: + FileDataConvertor(int32_t userId, std::string &bundleName); + ~FileDataConvertor() = default; + + int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet); + +private: + /* basic */ + int32_t HandleAlbumId(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFileName(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleHashId(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleSource(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFileType(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleCreatedTime(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFavorite(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleDescription(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + + /* properties */ + int32_t HandleProperties(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + + int32_t HandleHeight(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleRotation(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleWidth(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandlePosition(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleDataModified(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleDetailTime(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleFileCreateTime(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleFirstUpdateTime(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleRelativeBucketId(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleSourceFileName(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleSourcePath(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + int32_t HandleTimeZone(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); + + /* attachments */ + int32_t HandleAttachments(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + + int32_t HandleContent(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet); + int32_t HandleThumbnail(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet); + int32_t HandleLcd(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet); + + /* identifier */ + int32_t userId_; + std::string bundleName_; + static std::string recordType_; + + /* path */ + static std::string realPrefix_; + static std::string suffix_; + static std::string sandboxPrefix_; + + /* map */ + static std::unordered_map map_; + static std::unordered_map pMap_; + static std::unordered_map aMap_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_CONVERTOR_H diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h index fc75327fe..b40f3e870 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h @@ -16,15 +16,18 @@ #ifndef OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H #define OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H +#include "medialibrary_db_const.h" +#include "medialibrary_type_const.h" + #include "rdb_data_handler.h" -#include "data_convertor.h" +#include "file_data_convertor.h" namespace OHOS { namespace FileManagement { namespace CloudSync { class FileDataHandler : public RdbDataHandler { public: - FileDataHandler(std::shared_ptr rdb); + FileDataHandler(int32_t userId, const std::string &bundleName, std::shared_ptr rdb); virtual ~FileDataHandler() = default; /* download */ @@ -55,51 +58,23 @@ private: DriveKit::DKFieldKeyArray desiredKeys_ = { "albumId", "createdTime", "editedTime", "favorite", "fileName", "fileType", "hashId", "sha256", "id", "properties", "size", "source", "recycledTime", "recycled", "pictureMetadata", "videoMetadata", "cipher", "description" }; + static const std::vector GALLERY_FILE_COLUMNS; + + /* identifier */ + int32_t userId_; + std::string bundleName_; /* create */ int32_t createOffset_ = 0; - DataConvertor createConvertor_ = { - { "file_id", "data", "size", "data" }, - { "file_id", "data", "size", "asset" }, - { INT, STRING, INT, ASSET }, - 4 - }; - DataConvertor onCreateConvertor_ = { - { "file_id", "data", "size" }, - { "file_id", "data", "size" }, - { INT, STRING, INT }, - 3 - }; + FileDataConvertor createConvertor_ = { userId_, bundleName_ }; /* delete */ int32_t deleteOffset_ = 0; - DataConvertor deleteConvertor_ = { - { "file_id", "data", "size" }, - { "id", "path", "size" }, - { INT, STRING, INT }, - 3 - }; - DataConvertor onDeleteConvertor_ = { - { "file_id", "data", "size" }, - { "id", "path", "size" }, - { INT, STRING, INT }, - 3 - }; + FileDataConvertor deleteConvertor_ = { userId_, bundleName_ }; /* update */ int32_t updateOffset_ = 0; - DataConvertor updateConvertor_ = { - { "file_id", "data", "size" }, - { "id", "path", "size" }, - { INT, STRING, INT }, - 3 - }; - DataConvertor onUpdateConvertor_ = { - { "file_id", "data", "size" }, - { "id", "path", "size" }, - { INT, STRING, INT }, - 3 - }; + FileDataConvertor updateConvertor_ = { userId_, bundleName_ }; /* fetch */ @@ -109,14 +84,9 @@ private: int32_t PullRecordDelete(const DriveKit::DKRecord &record, const DriveKit::DKRecord &local); /* db result to record */ - DataConvertor localConvertor_ = { - { "file_id", "data", "size" }, - { "id", "path", "size" }, - { INT, STRING, INT }, - 3 - }; + FileDataConvertor localConvertor_ = { userId_, bundleName_ }; }; } // namespace CloudSync } // namespace FileManagement } // namespace OHOS -#endif // OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H \ No newline at end of file +#endif // OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_file_const.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_file_const.h new file mode 100644 index 000000000..a88718cbe --- /dev/null +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/gallery_file_const.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_CLOUD_SYNC_SERVICE_GALLERY_FILE_CONST_H +#define OHOS_CLOUD_SYNC_SERVICE_GALLERY_FILE_CONST_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +/* basic */ +const std::string FILE_ALBUM_ID = "albumId"; +const std::string FILE_FILE_NAME = "fileName"; +const std::string FILE_HASH_ID = "hashId"; +const std::string FILE_SOURCE = "source"; +const std::string FILE_FILETYPE = "fileType"; +const std::string FILE_CREATED_TIME = "createdTime"; +const std::string FILE_FAVORITE = "favorite"; +const std::string FILE_DESCRIPTION = "description"; +/* properties */ +const std::string FILE_PROPERTIES = "properties"; +const std::string FILE_HEIGHT = "height"; +const std::string FILE_ROTATION = "rotation"; +const std::string FILE_WIDTH = "width"; +const std::string FILE_POSITION = "position"; +const std::string FILE_DATA_MODIFIED = "data_modified"; +const std::string FILE_DETAIL_TIME = "detail_time"; +const std::string FILE_FILE_CREATE_TIME = "fileCreateTime"; +const std::string FILE_FIRST_UPDATE_TIME = "first_update_time"; +const std::string FILE_RELATIVE_BUCKET_ID = "relative_bucket_id"; +const std::string FILE_SOURCE_FILE_NAME = "sourceFileName"; +const std::string FILE_SOURCE_PATH = "sourcePath"; +const std::string FILE_TIME_ZONE = "time_zone"; +/* attachments */ +const std::string FILE_ATTACHMENTS = "attachments"; +const std::string FILE_CONTENT = "content"; +const std::string FILE_THUMBNAIL = "thumbnail"; +const std::string FILE_LCD = "lcdThumbnail"; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // OHOS_CLOUD_SYNC_SERVICE_GALLERY_FILE_CONST_H diff --git a/services/cloudsyncservice/include/data_sync/sdk_helper.h b/services/cloudsyncservice/include/data_sync/sdk_helper.h index 70ed15c1f..78aa5f0bc 100644 --- a/services/cloudsyncservice/include/data_sync/sdk_helper.h +++ b/services/cloudsyncservice/include/data_sync/sdk_helper.h @@ -31,6 +31,10 @@ public: SdkHelper(const int32_t userId, const std::string bundleName); ~SdkHelper() = default; + /* lock */ + int32_t GetLock(DriveKit::DKLock &lock); + void DeleteLock(DriveKit::DKLock &lock); + /* record download */ using FetchRecordsCallback = std::function, std::shared_ptr, diff --git a/services/cloudsyncservice/src/data_sync/data_syncer.cpp b/services/cloudsyncservice/src/data_sync/data_syncer.cpp index 832ec70b8..66a89ddd2 100644 --- a/services/cloudsyncservice/src/data_sync/data_syncer.cpp +++ b/services/cloudsyncservice/src/data_sync/data_syncer.cpp @@ -439,6 +439,12 @@ void DataSyncer::OnCreateRecords(shared_ptr context, return; } + /* unlock */ + DKLock *lock = static_cast(context->GetData()); + if (lock != nullptr) { + sdkHelper_.DeleteLock(*lock); + } + /* push more */ ret = AsyncRun(ctx, &DataSyncer::CreateRecords); if (ret != E_OK) { diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp index 8367b67ab..cd2de252e 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp @@ -15,6 +15,8 @@ #include "data_convertor.h" +#include "rdb_errno.h" + #include "dfs_error.h" #include "utils_log.h" #include "sdk_helper.h" @@ -25,20 +27,6 @@ namespace CloudSync { using namespace std; using namespace NativeRdb; -DataConvertor::DataConvertor(const vector localColumns, - const vector cloudColumns, - const vector types, int32_t size) - : localColumns_(localColumns), - cloudColumns_(cloudColumns), - types_(types), - size_(size) -{ - opToHandlerMap_[INT] = &DataConvertor::HandleInt; - opToHandlerMap_[LONG] = &DataConvertor::HandleLong; - opToHandlerMap_[STRING] = &DataConvertor::HandleString; - opToHandlerMap_[ASSET] = &DataConvertor::HandleAsset; -} - int32_t DataConvertor::ResultSetToRecords(const unique_ptr resultSet, std::vector &records) { @@ -51,108 +39,79 @@ int32_t DataConvertor::ResultSetToRecords(const unique_ptr } records.reserve(rowCount); - auto size = this->GetSize(); - auto &types = this->GetTypes(); - auto &cloudColumns = this->GetCloudColumns(); - /* iterate all rows */ while (resultSet->GoToNextRow() == 0) { DriveKit::DKRecord record; - DriveKit::DKRecordData data; - for (int i = 0; i < size; i++) { - DataType type = types[i]; - auto entry = opToHandlerMap_.find(type); - if (entry == opToHandlerMap_.end() || !entry->second) { - LOGE("invalid type %d", type); - return E_INVAL_ARG; - } - (this->*(entry->second))(data, cloudColumns[i], i, *resultSet); + int32_t ret = Convert(record, *resultSet); + if (ret != E_OK) { + LOGE("covert result to record err %{public}d", ret); + continue; } - record.SetRecordData(data); records.emplace_back(move(record)); } return E_OK; } -void DataConvertor::HandleInt(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet) +int32_t DataConvertor::GetInt(const string &key, int32_t &val, NativeRdb::ResultSet &resultSet) { - int32_t val; - int32_t err = resultSet.GetInt(index, val); + int32_t index; + int32_t err = resultSet.GetColumnIndex(key, index); + if (err != NativeRdb::E_OK) { + LOGE("result set get column index err %{public}d", err); + return E_RDB; + } + + err = resultSet.GetInt(index, val); if (err != 0) { LOGE("result set get int err %{public}d", err); + return E_RDB; } - data[key] = DriveKit::DKRecordField(val); + + return E_OK; } -void DataConvertor::HandleLong(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet) +int32_t DataConvertor::GetLong(const string &key, int64_t &val, NativeRdb::ResultSet &resultSet) { - int64_t val; - int32_t err = resultSet.GetLong(index, val); + int32_t index; + int32_t err = resultSet.GetColumnIndex(key, index); + if (err != NativeRdb::E_OK) { + LOGE("result set get column index err %{public}d", err); + return E_RDB; + } + + err = resultSet.GetLong(index, val); if (err != 0) { LOGE("result set get int err %{public}d", err); + return E_RDB; } - data[key] = DriveKit::DKRecordField(val); + + return E_OK; } -void DataConvertor::HandleString(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet) +int32_t DataConvertor::GetString(const string &key, string &val, NativeRdb::ResultSet &resultSet) { - string val; - int32_t err = resultSet.GetString(index, val); - if (err != 0) { - LOGE("result set get string err %{public}d", err); + int32_t index; + int32_t err = resultSet.GetColumnIndex(key, index); + if (err != NativeRdb::E_OK) { + LOGE("result set get column index err %{public}d", err); + return E_RDB; } - data[key] = DriveKit::DKRecordField(val); -} -void DataConvertor::HandleAsset(DriveKit::DKRecordData &data, const DriveKit::DKFieldKey &key, - int32_t index, NativeRdb::ResultSet &resultSet) -{ - string val; - int32_t err = resultSet.GetString(index, val); + err = resultSet.GetString(index, val); if (err != 0) { LOGE("result set get string err %{public}d", err); + return E_RDB; } - data[key] = DriveKit::DKRecordField(val); + + return E_OK; } int32_t DataConvertor::RecordToValueBucket(const DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket) { - auto size = this->GetSize(); - auto &types = this->GetTypes(); - auto &localColumns = this->GetLocalColumns(); - auto &cloudColumns = this->GetCloudColumns(); - - DriveKit::DKRecordData data; - record.GetRecordData(data); - for (int32_t j = 0; j < size; j++) { - DataType type = types[j]; - switch (type) { - case INT: { - valueBucket.PutInt(localColumns[j], data[cloudColumns[j]]); - break; - } - case LONG: { - valueBucket.PutLong(localColumns[j], data[cloudColumns[j]]); - break; - } - case STRING: { - valueBucket.PutString(localColumns[j], data[cloudColumns[j]]); - break; - } - default: { - LOGE("invalid data type %{public}d", type); - break; - } - } - } - return E_OK; } } // namespace CloudSync } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp new file mode 100644 index 000000000..8afcd0734 --- /dev/null +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp @@ -0,0 +1,396 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_data_convertor.h" + +#include "medialibrary_db_const.h" + +#include "dfs_error.h" +#include "utils_log.h" +#include "sdk_helper.h" +#include "gallery_file_const.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace NativeRdb; +using namespace Media; + +/* record type */ +string FileDataConvertor::recordType_ = "media"; + +/* path */ +string FileDataConvertor::realPrefix_ = "/data/service/el2/"; +string FileDataConvertor::suffix_ = "/hmdfs/account/files"; +string FileDataConvertor::sandboxPrefix_ = "/storage/media/local/files"; + +/* basic map */ +unordered_map FileDataConvertor::map_ = { + /* basic */ + { FILE_ALBUM_ID, &FileDataConvertor::HandleAlbumId }, + { FILE_FILE_NAME, &FileDataConvertor::HandleFileName }, + { FILE_HASH_ID, &FileDataConvertor::HandleHashId }, + { FILE_SOURCE, &FileDataConvertor::HandleSource }, + { FILE_FILETYPE, &FileDataConvertor::HandleFileType }, + { FILE_CREATED_TIME, &FileDataConvertor::HandleCreatedTime }, + { FILE_FAVORITE, &FileDataConvertor::HandleFavorite }, + { FILE_DESCRIPTION, &FileDataConvertor::HandleDescription }, + /* properties */ + { FILE_PROPERTIES, &FileDataConvertor::HandleProperties }, + /* attachments */ + { FILE_ATTACHMENTS, &FileDataConvertor::HandleAttachments }, +}; + +/* properties map */ +unordered_map FileDataConvertor::pMap_ = { + /* HO */ + { FILE_HEIGHT, &FileDataConvertor::HandleHeight }, + { FILE_ROTATION, &FileDataConvertor::HandleRotation }, + { FILE_WIDTH, &FileDataConvertor::HandleWidth }, + { FILE_POSITION, &FileDataConvertor::HandlePosition }, + { FILE_DATA_MODIFIED, &FileDataConvertor::HandleDataModified }, + { FILE_DETAIL_TIME, &FileDataConvertor::HandleDetailTime }, + { FILE_FILE_CREATE_TIME, &FileDataConvertor::HandleFileCreateTime }, + { FILE_FIRST_UPDATE_TIME, &FileDataConvertor::HandleFirstUpdateTime }, + { FILE_RELATIVE_BUCKET_ID, &FileDataConvertor::HandleRelativeBucketId }, + { FILE_SOURCE_FILE_NAME, &FileDataConvertor::HandleSourceFileName }, + { FILE_SOURCE_PATH, &FileDataConvertor::HandleSourcePath }, + { FILE_TIME_ZONE, &FileDataConvertor::HandleTimeZone }, + /* OH */ +}; + +/* attachments map */ +unordered_map FileDataConvertor::aMap_ = { + { FILE_CONTENT, &FileDataConvertor::HandleContent }, + { FILE_THUMBNAIL, &FileDataConvertor::HandleThumbnail }, + { FILE_LCD, &FileDataConvertor::HandleLcd }, +}; + +FileDataConvertor::FileDataConvertor(int32_t userId, string &bundleName) : userId_(userId), + bundleName_(bundleName) +{ +} + +int32_t FileDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet) +{ + /* data */ + DriveKit::DKRecordDatas data; + for (auto it = map_.begin(); it != map_.end(); it++) { + string &key = const_cast(it->first); + int32_t ret = (this->*(it->second))(key, data, resultSet); + if (ret != E_OK) { + LOGE("%{private}s convert err %{public}d", key.c_str(), ret); + return ret; + } + } + record.SetRecordDatas(data); + + /* control info */ + record.SetRecordType(recordType_); + record.SetNewCreate(true); + + return E_OK; +} + +/* properties */ +int32_t FileDataConvertor::HandleProperties(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + DriveKit::DKRecordFieldMap map; + for (auto it = pMap_.begin(); it != pMap_.end(); it++) { + string &key = const_cast(it->first); + int32_t ret = (this->*(it->second))(key, map, resultSet); + if (ret != E_OK) { + LOGE("%{private}s convert err %{public}d", key.c_str(), ret); + return ret; + } + } + data[key] = DriveKit::DKRecordField(map); + return E_OK; +} + +/* attachments */ +int32_t FileDataConvertor::HandleAttachments(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + DriveKit::DKRecordFieldList attachments; + for (auto it = aMap_.begin(); it != aMap_.end(); it++) { + int32_t ret = (this->*(it->second))(attachments, resultSet); + if (ret != E_OK) { + LOGE("%{private}s convert err %{public}d", key.c_str(), ret); + return ret; + } + } + data[key] = DriveKit::DKRecordField(attachments); + return E_OK; +} + +/* basic */ +int32_t FileDataConvertor::HandleAlbumId(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + data[key] = DriveKit::DKRecordField("default-album-300--1005381015-1682043649074"); + return E_OK; +} + +int32_t FileDataConvertor::HandleFileName(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_NAME, val, resultSet); + if (ret != E_OK) { + return ret; + } + data[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandleHashId(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + data[key] = DriveKit::DKRecordField("11111111"); + return E_OK; +} + +int32_t FileDataConvertor::HandleSource(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + data[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +int32_t FileDataConvertor::HandleFileType(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + data[key] = DriveKit::DKRecordField(1); + return E_OK; +} + +int32_t FileDataConvertor::HandleCreatedTime(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + int64_t val; + int32_t ret = GetLong(MEDIA_DATA_DB_DATE_ADDED, val, resultSet); + if (ret != E_OK) { + return ret; + } + data[key] = DriveKit::DKRecordField(to_string(val)); + return E_OK; +} + +int32_t FileDataConvertor::HandleFavorite(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + int32_t val; + int32_t ret = GetInt(MEDIA_DATA_DB_NAME, val, resultSet); + if (ret != E_OK) { + return ret; + } + data[key] = DriveKit::DKRecordField(!!val); + return E_OK; +} + +int32_t FileDataConvertor::HandleDescription(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +{ + data[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +/* properties */ +int32_t FileDataConvertor::HandleHeight(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int32_t val; + int32_t ret = GetInt(MEDIA_DATA_DB_HEIGHT, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandleRotation(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int32_t val; + int32_t ret = GetInt(MEDIA_DATA_DB_ORIENTATION, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandleWidth(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int32_t val; + int32_t ret = GetInt(MEDIA_DATA_DB_WIDTH, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandlePosition(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + map[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +int32_t FileDataConvertor::HandleDataModified(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int64_t val; + int32_t ret = GetLong(MEDIA_DATA_DB_WIDTH, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(to_string(val)); + return E_OK; +} + +int32_t FileDataConvertor::HandleDetailTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + map[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +int32_t FileDataConvertor::HandleFileCreateTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int64_t val; + int32_t ret = GetLong(MEDIA_DATA_DB_DATE_ADDED, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(to_string(val)); + return E_OK; +} + +int32_t FileDataConvertor::HandleFirstUpdateTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + map[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +int32_t FileDataConvertor::HandleRelativeBucketId(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + int32_t val; + int32_t ret = GetInt(MEDIA_DATA_DB_BUCKET_ID, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(to_string(val)); + return E_OK; +} + +int32_t FileDataConvertor::HandleSourceFileName(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_NAME, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandleSourcePath(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_FILE_PATH, val, resultSet); + if (ret != E_OK) { + return ret; + } + map[key] = DriveKit::DKRecordField(val); + return E_OK; +} + +int32_t FileDataConvertor::HandleTimeZone(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +{ + map[key] = DriveKit::DKRecordField(""); + return E_OK; +} + +/* attachments */ +int32_t FileDataConvertor::HandleContent(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +{ + /* path */ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_FILE_PATH, val, resultSet); + if (ret != E_OK) { + return ret; + } + size_t pos = val.find_first_of(sandboxPrefix_); + if (pos == string::npos) { + LOGE("invalid path %{private}s", val.c_str()); + return E_PATH; + } + string path = realPrefix_ + to_string(userId_) + suffix_ + val.substr(pos + sandboxPrefix_.size()); + + /* asset */ + DriveKit::DKAsset content; + content.uri = path; + content.assetName = FILE_CONTENT; + content.operationType = DriveKit::DKAssetOperType::DK_ASSET_ADD; + list.push_back(DriveKit::DKRecordField(content)); + + return E_OK; +} + +int32_t FileDataConvertor::HandleThumbnail(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +{ + /* tmp */ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_FILE_PATH, val, resultSet); + if (ret != E_OK) { + return ret; + } + size_t pos = val.find_first_of(sandboxPrefix_); + if (pos == string::npos) { + LOGE("invalid path %{private}s", val.c_str()); + return E_PATH; + } + string path = realPrefix_ + to_string(userId_) + suffix_ + val.substr(pos + sandboxPrefix_.size()); + + /* asset */ + DriveKit::DKAsset content; + content.uri = path; + content.assetName = FILE_THUMBNAIL; + content.operationType = DriveKit::DKAssetOperType::DK_ASSET_ADD; + list.push_back(DriveKit::DKRecordField(content)); + + return E_OK; +} + +int32_t FileDataConvertor::HandleLcd(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +{ + /* tmp */ + string val; + int32_t ret = GetString(MEDIA_DATA_DB_FILE_PATH, val, resultSet); + if (ret != E_OK) { + return ret; + } + size_t pos = val.find_first_of(sandboxPrefix_); + if (pos == string::npos) { + LOGE("invalid path %{private}s", val.c_str()); + return E_PATH; + } + string path = realPrefix_ + to_string(userId_) + suffix_ + val.substr(pos + sandboxPrefix_.size()); + + /* asset */ + DriveKit::DKAsset content; + content.uri = path; + content.assetName = FILE_LCD; + content.operationType = DriveKit::DKAssetOperType::DK_ASSET_ADD; + list.push_back(DriveKit::DKRecordField(content)); + + return E_OK; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp index 6f5699263..876a90c12 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp @@ -15,11 +15,9 @@ #include "file_data_handler.h" -#include "medialibrary_db_const.h" -#include "medialibrary_type_const.h" - #include "dfs_error.h" #include "utils_log.h" +#include "gallery_file_const.h" namespace OHOS { namespace FileManagement { @@ -27,9 +25,25 @@ namespace CloudSync { using namespace std; using namespace NativeRdb; using namespace DriveKit; - -FileDataHandler::FileDataHandler(std::shared_ptr rdb) - : RdbDataHandler(TABLE_NAME, rdb) +using namespace Media; + +const vector FileDataHandler::GALLERY_FILE_COLUMNS = { + MEDIA_DATA_DB_NAME, + MEDIA_DATA_DB_MEDIA_TYPE, + MEDIA_DATA_DB_DATE_ADDED, + MEDIA_DATA_DB_IS_FAV, + MEDIA_DATA_DB_HEIGHT, + MEDIA_DATA_DB_WIDTH, + MEDIA_DATA_DB_DATE_MODIFIED, + MEDIA_DATA_DB_BUCKET_ID, + MEDIA_DATA_DB_FILE_PATH, + MEDIA_DATA_DB_THUMBNAIL, + MEDIA_DATA_DB_LCD, + MEDIA_DATA_DB_ORIENTATION +}; + +FileDataHandler::FileDataHandler(int32_t userId, const string &bundleName, std::shared_ptr rdb) + : RdbDataHandler(TABLE_NAME, rdb), userId_(userId), bundleName_(bundleName) { } @@ -50,7 +64,7 @@ int32_t FileDataHandler::OnFetchRecords(const shared_ptr> &reco predicates.SetWhereClause(Media::MEDIA_DATA_DB_CLOUD_ID + " = ?"); predicates.SetWhereArgs({record.GetRecordId()}); predicates.Limit(LIMIT_SIZE); - auto resultSet = Query(predicates, localConvertor_.GetLocalColumns()); + auto resultSet = Query(predicates, GALLERY_FILE_COLUMNS); if (resultSet == nullptr) { LOGE("get nullptr created result"); ret = E_RDB; @@ -132,7 +146,7 @@ int32_t FileDataHandler::GetCreatedRecords(vector &records) createPredicates.Limit(LIMIT_SIZE); /* query */ - auto results = Query(createPredicates, createConvertor_.GetLocalColumns()); + auto results = Query(createPredicates, GALLERY_FILE_COLUMNS); if (results == nullptr) { LOGE("get nullptr created result"); return E_RDB; @@ -161,7 +175,7 @@ int32_t FileDataHandler::GetDeletedRecords(vector &records) deletePredicates.Limit(LIMIT_SIZE); /* query */ - auto results = Query(deletePredicates, deleteConvertor_.GetLocalColumns()); + auto results = Query(deletePredicates, GALLERY_FILE_COLUMNS); if (results == nullptr) { LOGE("get nullptr deleted result"); return E_RDB; @@ -190,7 +204,7 @@ int32_t FileDataHandler::GetModifiedRecords(vector &records) updatePredicates.Limit(LIMIT_SIZE); /* query */ - auto results = Query(updatePredicates, updateConvertor_.GetLocalColumns()); + auto results = Query(updatePredicates, GALLERY_FILE_COLUMNS); if (results == nullptr) { LOGE("get nullptr modified result"); return E_RDB; @@ -215,30 +229,25 @@ int32_t FileDataHandler::OnCreateRecords(const map(Media::DirtyType::TYPE_SYNCED)); - DKRecordData data; - record.GetRecordData(data); - auto iter = data.find(Media::MEDIA_DATA_DB_ID); + DKRecordDatas data; + record.GetRecordDatas(data); + auto iter = data.find(FILE_FILE_NAME); if (iter == data.end()) { LOGE("no id in record data"); continue; } - int32_t id = iter->second; + string fileName = iter->second; /* update local */ int32_t changedRows; - string whereClause = Media::MEDIA_DATA_DB_ID + " = ?"; - ret = Update(changedRows, valuesBucket, whereClause, { to_string(id) }); + string whereClause = Media::MEDIA_DATA_DB_NAME + " = ?"; + int32_t ret = Update(changedRows, valuesBucket, whereClause, { fileName }); if (ret != 0) { - LOGE("on create records update err %{public}d, id %{public}d", ret, id); + LOGE("on create records update err %{public}d, file name %{private}s", ret, fileName.c_str()); continue; } } @@ -281,11 +290,6 @@ int32_t FileDataHandler::OnModifyRecords(const map(Media::DirtyType::TYPE_SYNCED)); @@ -301,7 +305,7 @@ int32_t FileDataHandler::OnModifyRecords(const map(rdb_); + fileHandler_ = make_shared(userId, bundleName, rdb_); } void GalleryDataSyncer::Schedule() diff --git a/services/cloudsyncservice/src/data_sync/sdk_helper.cpp b/services/cloudsyncservice/src/data_sync/sdk_helper.cpp index 397e87dde..a1658fd29 100644 --- a/services/cloudsyncservice/src/data_sync/sdk_helper.cpp +++ b/services/cloudsyncservice/src/data_sync/sdk_helper.cpp @@ -46,6 +46,21 @@ SdkHelper::SdkHelper(const int32_t userId, const std::string bundleName) } } +int32_t SdkHelper::GetLock(DriveKit::DKLock &lock) +{ + auto err = database_->GetLock(lock); + if (err.HasError()) { + LOGE("create records get lock err"); + return E_CLOUD_SDK; + } + return E_OK; +} + +void SdkHelper::DeleteLock(DriveKit::DKLock &lock) +{ + database_->DeleteLock(lock); +} + int32_t SdkHelper::FetchRecords(shared_ptr context, DriveKit::DKQueryCursor cursor, function, std::shared_ptr, std::shared_ptr>, DriveKit::DKQueryCursor, @@ -98,9 +113,17 @@ int32_t SdkHelper::CreateRecords(shared_ptr context, std::shared_ptr>, const DriveKit::DKError &)> callback) { - auto err = database_->SaveRecords(context, std::move(records), DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback); - if (err != DriveKit::DKLocalErrorCode::NO_ERROR) { - LOGE("drivekit save records err %{public}d", err); + DriveKit::DKLock lock; + int32_t ret = GetLock(lock); + if (ret != E_OK) { + return ret; + } + context->SetData(&lock); + + auto errCode = database_->SaveRecords(context, move(records), + DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback); + if (errCode != DriveKit::DKLocalErrorCode::NO_ERROR) { + LOGE("drivekit save records err %{public}d", errCode); return E_CLOUD_SDK; } diff --git a/services/distributedfile.cfg b/services/distributedfile.cfg index ecabebac3..3c577e61a 100644 --- a/services/distributedfile.cfg +++ b/services/distributedfile.cfg @@ -22,7 +22,7 @@ "name": "cloudfiledaemon", "path": ["/system/bin/sa_main", "/system/profile/cloudfiledaemon.json"], "uid": "1009", - "gid": ["system", "dfs", "ddms"], + "gid": ["system", "dfs", "ddms", "user_data_rw"], "jobs" : { "on-start" : "services:cloudfiledaemon" }, @@ -30,7 +30,7 @@ "apl": "system_basic", "permission" : [ "ohos.permission.DISTRIBUTED_DATASYNC", - "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.MANAGE_LOCAL_ACCOUNTS", "ohos.permission.ACCESS_SERVICE_DM" ] }] diff --git a/utils/log/include/dfs_error.h b/utils/log/include/dfs_error.h index 01bc5cf44..fc39efd6a 100644 --- a/utils/log/include/dfs_error.h +++ b/utils/log/include/dfs_error.h @@ -53,6 +53,7 @@ enum CloudSyncServiceErrCode : ErrCode { E_PENDING, E_SCHEDULE, E_ASYNC_RUN, + E_PATH }; } // namespace OHOS::FileManagement -- Gitee From 93777ca62ff7038035050914a3081b7a6b1e1e9f Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Tue, 25 Apr 2023 02:21:26 +0000 Subject: [PATCH 2/3] add sdk lock Signed-off-by: jiahaoluo Change-Id: I9b5e248c1fcbf917d6ae82179cb8b13744125526 --- services/cloudsyncservice/BUILD.gn | 2 +- .../include/data_sync/data_syncer.h | 2 +- .../gallery_data_sync/file_data_convertor.h | 24 +++--- .../src/data_sync/data_syncer.cpp | 15 ++-- .../gallery_data_sync/file_data_convertor.cpp | 83 ++++++++++++------- .../gallery_data_sync/file_data_handler.cpp | 4 +- .../src/data_sync/sdk_helper.cpp | 9 +- .../unittests/cloudsync_sa/data_sync/BUILD.gn | 1 + test/unittests/cloudsync_sa/ipc/BUILD.gn | 1 + 9 files changed, 82 insertions(+), 59 deletions(-) diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index 2e251ab19..cd83f632e 100644 --- a/services/cloudsyncservice/BUILD.gn +++ b/services/cloudsyncservice/BUILD.gn @@ -36,7 +36,7 @@ ohos_shared_library("cloudsync_sa") { "src/data_sync/gallery_data_sync/file_data_handler.cpp", "src/data_sync/gallery_data_sync/album_data_handler.cpp", "src/data_sync/gallery_data_sync/data_convertor.cpp", - "src/data_sync/gallery_data_sync/file_data_convertor.cpp" + "src/data_sync/gallery_data_sync/file_data_convertor.cpp", ] sources = [ diff --git a/services/cloudsyncservice/include/data_sync/data_syncer.h b/services/cloudsyncservice/include/data_sync/data_syncer.h index 2190e4067..87f06f031 100644 --- a/services/cloudsyncservice/include/data_sync/data_syncer.h +++ b/services/cloudsyncservice/include/data_sync/data_syncer.h @@ -133,7 +133,7 @@ private: /* sdk */ SdkHelper sdkHelper_; - + DriveKit::DKLock lock_; int32_t errorCode_{0}; /* cursor*/ diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h index f21a563b2..55deccaff 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h @@ -32,17 +32,17 @@ public: private: /* basic */ - int32_t HandleAlbumId(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleFileName(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleHashId(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleSource(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleFileType(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleCreatedTime(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleFavorite(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); - int32_t HandleDescription(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleAlbumId(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFileName(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleHashId(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleSource(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFileType(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleCreatedTime(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleFavorite(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); + int32_t HandleDescription(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); /* properties */ - int32_t HandleProperties(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleProperties(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); int32_t HandleHeight(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); int32_t HandleRotation(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); @@ -58,7 +58,7 @@ private: int32_t HandleTimeZone(std::string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet); /* attachments */ - int32_t HandleAttachments(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet); + int32_t HandleAttachments(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); int32_t HandleContent(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet); int32_t HandleThumbnail(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet); @@ -76,9 +76,9 @@ private: /* map */ static std::unordered_map map_; + DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet)> map_; static std::unordered_map pMap_; + DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet)> pMap_; static std::unordered_map aMap_; }; diff --git a/services/cloudsyncservice/src/data_sync/data_syncer.cpp b/services/cloudsyncservice/src/data_sync/data_syncer.cpp index 66a89ddd2..ecdc22b7b 100644 --- a/services/cloudsyncservice/src/data_sync/data_syncer.cpp +++ b/services/cloudsyncservice/src/data_sync/data_syncer.cpp @@ -98,6 +98,12 @@ int32_t DataSyncer::StartSync(bool forceFlag, SyncTriggerType triggerType) /* notify sync state */ SyncStateChangedNotify(SyncType::ALL, SyncPromptState::SYNC_STATE_SYNCING); + /* lock: device-reentrant */ + int32_t ret = sdkHelper_.GetLock(lock_); + if (ret != E_OK) { + return ret; + } + /* start data sync */ Schedule(); @@ -439,12 +445,6 @@ void DataSyncer::OnCreateRecords(shared_ptr context, return; } - /* unlock */ - DKLock *lock = static_cast(context->GetData()); - if (lock != nullptr) { - sdkHelper_.DeleteLock(*lock); - } - /* push more */ ret = AsyncRun(ctx, &DataSyncer::CreateRecords); if (ret != E_OK) { @@ -564,6 +564,9 @@ void DataSyncer::CompleteAll(int32_t code, const SyncType type) { LOGI("%{private}d %{private}s completes all", userId_, bundleName_.c_str()); + /* unlock */ + sdkHelper_.DeleteLock(lock_); + if (errorCode_ == E_SYNC_FAILED_BATTERY_LOW) { code = errorCode_; } diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp index 8afcd0734..c1100626a 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp @@ -38,7 +38,7 @@ string FileDataConvertor::suffix_ = "/hmdfs/account/files"; string FileDataConvertor::sandboxPrefix_ = "/storage/media/local/files"; /* basic map */ -unordered_map FileDataConvertor::map_ = { /* basic */ { FILE_ALBUM_ID, &FileDataConvertor::HandleAlbumId }, @@ -90,7 +90,7 @@ FileDataConvertor::FileDataConvertor(int32_t userId, string &bundleName) : userI int32_t FileDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet) { /* data */ - DriveKit::DKRecordDatas data; + DriveKit::DKRecordData data; for (auto it = map_.begin(); it != map_.end(); it++) { string &key = const_cast(it->first); int32_t ret = (this->*(it->second))(key, data, resultSet); @@ -99,7 +99,7 @@ int32_t FileDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::Result return ret; } } - record.SetRecordDatas(data); + record.SetRecordData(data); /* control info */ record.SetRecordType(recordType_); @@ -109,7 +109,8 @@ int32_t FileDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::Result } /* properties */ -int32_t FileDataConvertor::HandleProperties(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleProperties(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { DriveKit::DKRecordFieldMap map; for (auto it = pMap_.begin(); it != pMap_.end(); it++) { @@ -125,7 +126,8 @@ int32_t FileDataConvertor::HandleProperties(string &key, DriveKit::DKRecordDatas } /* attachments */ -int32_t FileDataConvertor::HandleAttachments(std::string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleAttachments(std::string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { DriveKit::DKRecordFieldList attachments; for (auto it = aMap_.begin(); it != aMap_.end(); it++) { @@ -140,13 +142,15 @@ int32_t FileDataConvertor::HandleAttachments(std::string &key, DriveKit::DKRecor } /* basic */ -int32_t FileDataConvertor::HandleAlbumId(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleAlbumId(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { - data[key] = DriveKit::DKRecordField("default-album-300--1005381015-1682043649074"); + data[key] = DriveKit::DKRecordField("default-album-1"); return E_OK; } -int32_t FileDataConvertor::HandleFileName(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleFileName(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { string val; int32_t ret = GetString(MEDIA_DATA_DB_NAME, val, resultSet); @@ -157,25 +161,29 @@ int32_t FileDataConvertor::HandleFileName(string &key, DriveKit::DKRecordDatas & return E_OK; } -int32_t FileDataConvertor::HandleHashId(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleHashId(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { data[key] = DriveKit::DKRecordField("11111111"); return E_OK; } -int32_t FileDataConvertor::HandleSource(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleSource(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { data[key] = DriveKit::DKRecordField(""); return E_OK; } -int32_t FileDataConvertor::HandleFileType(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleFileType(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { data[key] = DriveKit::DKRecordField(1); return E_OK; } -int32_t FileDataConvertor::HandleCreatedTime(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleCreatedTime(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { int64_t val; int32_t ret = GetLong(MEDIA_DATA_DB_DATE_ADDED, val, resultSet); @@ -186,7 +194,8 @@ int32_t FileDataConvertor::HandleCreatedTime(string &key, DriveKit::DKRecordData return E_OK; } -int32_t FileDataConvertor::HandleFavorite(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleFavorite(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { int32_t val; int32_t ret = GetInt(MEDIA_DATA_DB_NAME, val, resultSet); @@ -197,14 +206,16 @@ int32_t FileDataConvertor::HandleFavorite(string &key, DriveKit::DKRecordDatas & return E_OK; } -int32_t FileDataConvertor::HandleDescription(string &key, DriveKit::DKRecordDatas &data, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleDescription(string &key, DriveKit::DKRecordData &data, + NativeRdb::ResultSet &resultSet) { data[key] = DriveKit::DKRecordField(""); return E_OK; } /* properties */ -int32_t FileDataConvertor::HandleHeight(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleHeight(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int32_t val; int32_t ret = GetInt(MEDIA_DATA_DB_HEIGHT, val, resultSet); @@ -215,7 +226,8 @@ int32_t FileDataConvertor::HandleHeight(string &key, DriveKit::DKRecordFieldMap return E_OK; } -int32_t FileDataConvertor::HandleRotation(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleRotation(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int32_t val; int32_t ret = GetInt(MEDIA_DATA_DB_ORIENTATION, val, resultSet); @@ -226,7 +238,8 @@ int32_t FileDataConvertor::HandleRotation(string &key, DriveKit::DKRecordFieldMa return E_OK; } -int32_t FileDataConvertor::HandleWidth(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleWidth(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int32_t val; int32_t ret = GetInt(MEDIA_DATA_DB_WIDTH, val, resultSet); @@ -237,13 +250,15 @@ int32_t FileDataConvertor::HandleWidth(string &key, DriveKit::DKRecordFieldMap & return E_OK; } -int32_t FileDataConvertor::HandlePosition(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandlePosition(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { map[key] = DriveKit::DKRecordField(""); return E_OK; } -int32_t FileDataConvertor::HandleDataModified(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleDataModified(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int64_t val; int32_t ret = GetLong(MEDIA_DATA_DB_WIDTH, val, resultSet); @@ -254,13 +269,15 @@ int32_t FileDataConvertor::HandleDataModified(string &key, DriveKit::DKRecordFie return E_OK; } -int32_t FileDataConvertor::HandleDetailTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleDetailTime(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { map[key] = DriveKit::DKRecordField(""); return E_OK; } -int32_t FileDataConvertor::HandleFileCreateTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleFileCreateTime(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int64_t val; int32_t ret = GetLong(MEDIA_DATA_DB_DATE_ADDED, val, resultSet); @@ -271,13 +288,15 @@ int32_t FileDataConvertor::HandleFileCreateTime(string &key, DriveKit::DKRecordF return E_OK; } -int32_t FileDataConvertor::HandleFirstUpdateTime(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleFirstUpdateTime(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { map[key] = DriveKit::DKRecordField(""); return E_OK; } -int32_t FileDataConvertor::HandleRelativeBucketId(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleRelativeBucketId(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { int32_t val; int32_t ret = GetInt(MEDIA_DATA_DB_BUCKET_ID, val, resultSet); @@ -288,7 +307,8 @@ int32_t FileDataConvertor::HandleRelativeBucketId(string &key, DriveKit::DKRecor return E_OK; } -int32_t FileDataConvertor::HandleSourceFileName(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleSourceFileName(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { string val; int32_t ret = GetString(MEDIA_DATA_DB_NAME, val, resultSet); @@ -299,7 +319,8 @@ int32_t FileDataConvertor::HandleSourceFileName(string &key, DriveKit::DKRecordF return E_OK; } -int32_t FileDataConvertor::HandleSourcePath(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleSourcePath(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { string val; int32_t ret = GetString(MEDIA_DATA_DB_FILE_PATH, val, resultSet); @@ -310,14 +331,16 @@ int32_t FileDataConvertor::HandleSourcePath(string &key, DriveKit::DKRecordField return E_OK; } -int32_t FileDataConvertor::HandleTimeZone(string &key, DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleTimeZone(string &key, DriveKit::DKRecordFieldMap &map, + NativeRdb::ResultSet &resultSet) { map[key] = DriveKit::DKRecordField(""); return E_OK; } /* attachments */ -int32_t FileDataConvertor::HandleContent(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleContent(DriveKit::DKRecordFieldList &list, + NativeRdb::ResultSet &resultSet) { /* path */ string val; @@ -342,7 +365,8 @@ int32_t FileDataConvertor::HandleContent(DriveKit::DKRecordFieldList &list, Nati return E_OK; } -int32_t FileDataConvertor::HandleThumbnail(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleThumbnail(DriveKit::DKRecordFieldList &list, + NativeRdb::ResultSet &resultSet) { /* tmp */ string val; @@ -367,7 +391,8 @@ int32_t FileDataConvertor::HandleThumbnail(DriveKit::DKRecordFieldList &list, Na return E_OK; } -int32_t FileDataConvertor::HandleLcd(DriveKit::DKRecordFieldList &list, NativeRdb::ResultSet &resultSet) +int32_t FileDataConvertor::HandleLcd(DriveKit::DKRecordFieldList &list, + NativeRdb::ResultSet &resultSet) { /* tmp */ string val; diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp index 876a90c12..8716467af 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp @@ -233,8 +233,8 @@ int32_t FileDataHandler::OnCreateRecords(const map(Media::DirtyType::TYPE_SYNCED)); - DKRecordDatas data; - record.GetRecordDatas(data); + DKRecordData data; + record.GetRecordData(data); auto iter = data.find(FILE_FILE_NAME); if (iter == data.end()) { LOGE("no id in record data"); diff --git a/services/cloudsyncservice/src/data_sync/sdk_helper.cpp b/services/cloudsyncservice/src/data_sync/sdk_helper.cpp index a1658fd29..235eb6257 100644 --- a/services/cloudsyncservice/src/data_sync/sdk_helper.cpp +++ b/services/cloudsyncservice/src/data_sync/sdk_helper.cpp @@ -50,7 +50,7 @@ int32_t SdkHelper::GetLock(DriveKit::DKLock &lock) { auto err = database_->GetLock(lock); if (err.HasError()) { - LOGE("create records get lock err"); + LOGE("get sdk lock err"); return E_CLOUD_SDK; } return E_OK; @@ -113,13 +113,6 @@ int32_t SdkHelper::CreateRecords(shared_ptr context, std::shared_ptr>, const DriveKit::DKError &)> callback) { - DriveKit::DKLock lock; - int32_t ret = GetLock(lock); - if (ret != E_OK) { - return ret; - } - context->SetData(&lock); - auto errCode = database_->SaveRecords(context, move(records), DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback); if (errCode != DriveKit::DKLocalErrorCode::NO_ERROR) { diff --git a/test/unittests/cloudsync_sa/data_sync/BUILD.gn b/test/unittests/cloudsync_sa/data_sync/BUILD.gn index d80b45def..6fb083488 100644 --- a/test/unittests/cloudsync_sa/data_sync/BUILD.gn +++ b/test/unittests/cloudsync_sa/data_sync/BUILD.gn @@ -27,6 +27,7 @@ ohos_unittest("data_sync_manager_test") { "${services_path}/cloudsyncservice/src/data_sync/data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/album_data_handler.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp", + "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/rdb_data_handler.cpp", diff --git a/test/unittests/cloudsync_sa/ipc/BUILD.gn b/test/unittests/cloudsync_sa/ipc/BUILD.gn index 722e6f44d..2ccb4a07a 100644 --- a/test/unittests/cloudsync_sa/ipc/BUILD.gn +++ b/test/unittests/cloudsync_sa/ipc/BUILD.gn @@ -56,6 +56,7 @@ ohos_unittest("cloud_sync_service_test") { "${services_path}/cloudsyncservice/src/data_sync/data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/album_data_handler.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/data_convertor.cpp", + "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp", "${services_path}/cloudsyncservice/src/data_sync/gallery_data_sync/gallery_data_syncer.cpp", "${services_path}/cloudsyncservice/src/data_sync/rdb_data_handler.cpp", -- Gitee From 3406fb7f107242652f7c968f2da40f8dc52754b8 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Tue, 25 Apr 2023 12:47:22 +0000 Subject: [PATCH 3/3] handle record id Signed-off-by: jiahaoluo Change-Id: I24dc7959732a5949f969d86baf29b3e299f30d17 --- .../gallery_data_sync/file_data_convertor.h | 6 +++- .../gallery_data_sync/file_data_handler.h | 8 ++--- .../gallery_data_sync/file_data_convertor.cpp | 29 ++++++++++++++++--- services/distributedfile.cfg | 2 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h index 55deccaff..f2a2f47f1 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_convertor.h @@ -25,12 +25,15 @@ namespace FileManagement { namespace CloudSync { class FileDataConvertor : public DataConvertor { public: - FileDataConvertor(int32_t userId, std::string &bundleName); + FileDataConvertor(int32_t userId, std::string &bundleName, bool isNew); ~FileDataConvertor() = default; int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet); private: + /* record id */ + int32_t FillRecordId(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet); + /* basic */ int32_t HandleAlbumId(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); int32_t HandleFileName(std::string &key, DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet); @@ -68,6 +71,7 @@ private: int32_t userId_; std::string bundleName_; static std::string recordType_; + bool isNew_; /* path */ static std::string realPrefix_; diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h index b40f3e870..88fe91f89 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h @@ -66,15 +66,15 @@ private: /* create */ int32_t createOffset_ = 0; - FileDataConvertor createConvertor_ = { userId_, bundleName_ }; + FileDataConvertor createConvertor_ = { userId_, bundleName_, true }; /* delete */ int32_t deleteOffset_ = 0; - FileDataConvertor deleteConvertor_ = { userId_, bundleName_ }; + FileDataConvertor deleteConvertor_ = { userId_, bundleName_, false }; /* update */ int32_t updateOffset_ = 0; - FileDataConvertor updateConvertor_ = { userId_, bundleName_ }; + FileDataConvertor updateConvertor_ = { userId_, bundleName_, false }; /* fetch */ @@ -84,7 +84,7 @@ private: int32_t PullRecordDelete(const DriveKit::DKRecord &record, const DriveKit::DKRecord &local); /* db result to record */ - FileDataConvertor localConvertor_ = { userId_, bundleName_ }; + FileDataConvertor localConvertor_ = { userId_, bundleName_, false }; }; } // namespace CloudSync } // namespace FileManagement diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp index c1100626a..9fe4bb91e 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_convertor.cpp @@ -82,8 +82,8 @@ unordered_map