diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index 365ba8f33283bba5f605bec6fec3ef438a1ca372..0337b003a4c048b49bb991c1b5864c0dc3b64bdf 100644 --- a/services/cloudsyncservice/BUILD.gn +++ b/services/cloudsyncservice/BUILD.gn @@ -15,6 +15,20 @@ import("//build/ohos.gni") import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_shared_library("cloudsync_sa") { + data_syncer = [ + "src/data_syncer/data_provider.cpp", + "src/data_syncer/data_syncer.cpp", + "src/data_syncer/file_manager.cpp", + "src/data_syncer/task.cpp", + "src/data_syncer/sdk_helper.cpp", + ] + + gallery_data_syncer = [ + "src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp", + "src/data_syncer/gallery_data_syncer/file_data_provider.cpp", + "src/data_syncer/gallery_data_syncer/album_data_provider.cpp", + ] + sources = [ "src/ipc/cloud_sync_callback_manager.cpp", "src/ipc/cloud_sync_callback_proxy.cpp", @@ -26,18 +40,26 @@ ohos_shared_library("cloudsync_sa") { #"src/datasync/data_syncer.cpp" ] + sources += data_syncer + sources += gallery_data_syncer + defines = [ "LOG_DOMAIN=0xD004307", "LOG_TAG=\"CLOUDSYNC_SA\"", ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "include/data_syncer", + "include/data_syncer/gallery_data_syncer" + ] deps = [ "${utils_path}:libdistributedfileutils" ] external_deps = [ "dfs_service:cloudsync_kit_inner", "ipc:ipc_core", + "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/services/cloudsyncservice/include/data_syncer/data_provider.h b/services/cloudsyncservice/include/data_syncer/data_provider.h new file mode 100644 index 0000000000000000000000000000000000000000..363edbcd68f7e1a5c14e49606c22cd9acf37bc70 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/data_provider.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 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 DATA_PROVIDER_H +#define DATA_PROVIDER_H + +#include +#include + +#include "rdb_helper.h" +#include "values_bucket.h" +#include "result_set.h" +#include "abs_rdb_predicates.h" +#include "drivekit.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class DataProvider { +public: + /* download */ + virtual int32_t GetFetchCondition() = 0; + virtual int32_t OnFetchRecords(const std::vector &records) = 0; + + /* upload */ + virtual int32_t GetCreatedRecords(std::vector &records) = 0; + virtual int32_t GetDeletedRecords(std::vector &records) = 0; + virtual int32_t GetModifiedRecords(std::vector &records) = 0; + + virtual int32_t OnCreateRecords(const std::vector &records) = 0; + virtual int32_t OnDeleteRecords(const std::vector &records) = 0; + virtual int32_t OnModifyRecords(const std::vector &records) = 0; + + /* cursor */ + virtual int32_t SetCursor(); + virtual int32_t GetCursor(); + + /* file */ + virtual int32_t DownloadFiles(); + virtual int32_t OnDownloadFiles(); +}; + +class RdbCallback : public NativeRdb::RdbOpenCallback { +public: + virtual int32_t OnCreate(NativeRdb::RdbStore &r) override + { + return 0; + } + + virtual int32_t OnUpgrade(NativeRdb::RdbStore &r, int32_t oldVersion, + int32_t newVersion) override + { + return 0; + } +}; + +class RdbProvider : public DataProvider { +protected: + enum DataType { + INT, + LONG, + STRING, + DOUBLE, + BOOL, + BLOB + }; + + struct DataConvertor { + const std::vector localColumns; + const std::vector cloudColumns; + const std::vector types; + const int size; + }; + +protected: + /* init */ + RdbProvider(std::string &table, std::shared_ptr rdb) : + rdb_(rdb), tableName_(table) {} + virtual ~RdbProvider() = default; + + /* insert, delete, update, query */ + virtual int32_t Insert(int64_t &outRowId, const NativeRdb::ValuesBucket &initialValues); + virtual int32_t Update(int &changedRows, const NativeRdb::ValuesBucket &values, + const std::string &whereClause = "", + const std::vector &whereArgs = std::vector()); + virtual int32_t Delete(int &deletedRows, const std::string &whereClause = "", + const std::vector &whereArgs = std::vector()); + virtual std::unique_ptr Query( + const NativeRdb::AbsRdbPredicates &predicates,const std::vector columns); + + /* convert */ + int32_t ResultSetToRecords(const std::unique_ptr resultSet, + std::vector &records, const DataConvertor &dc); + int32_t RecordsToValueBuckets(const std::vector &records, + std::vector &valueBuckets, const DataConvertor &dc); + +private: + std::shared_ptr rdb_; + std::string tableName_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/data_syncer/data_syncer.h b/services/cloudsyncservice/include/data_syncer/data_syncer.h new file mode 100644 index 0000000000000000000000000000000000000000..2b318c2d9bb7df1c5269aafd918fe7cd3322f074 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/data_syncer.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022 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 DATA_SYNCER_H +#define DATA_SYNCER_H + +#include +#include +#include +#include + +#include "drivekit.h" +#include "data_provider.h" +#include "task.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class DataSyncer { +public: + /* sync */ + virtual int32_t StartSync(); + virtual int32_t StopSync(); + + /* wait */ + int32_t OnWait(); + +protected: + /* init */ + int32_t Init(const std::string &name); + + /* rdb init */ + std::shared_ptr GetRdb(NativeRdb::RdbStoreConfig &config); + + /* download */ + int32_t Pull(std::shared_ptr provider); + + void OnFetchRecords(const std::shared_ptr context, const std::vector &records); + + /* upload */ + int32_t Push(std::shared_ptr provider); + + void OnCreateRecords(const std::shared_ptr context, const std::vector &records); + void OnDeleteRecords(const std::shared_ptr context, const std::vector &records); + void OnModifyRecords(const std::shared_ptr context, const std::vector &records); + + /* schedule */ + virtual bool IsSchedulable(); + virtual void Schedule() = 0; + + virtual void Abort(int32_t reason); + + virtual void CompletePull(); + virtual void CompletePush(); + virtual void CompleteAll(); + + /* task */ + int32_t CommitTask(std::shared_ptr t); + int32_t CompleteTask(std::shared_ptr t); + + /* retry */ + +private: + void PullRecords(std::shared_ptr context); + + void CreateRecords(std::shared_ptr context); + void DeleteRecords(std::shared_ptr context); + void ModifyRecords(std::shared_ptr context); + + int32_t AsyncRun(std::shared_ptr context, + void(DataSyncer::*f)(std::shared_ptr context)); + + template + std::function AsyncCallback(RET(T::*f)(ARGS...), T *t); + + std::string name_; + std::shared_ptr taskManager_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // DATA_SYNCER_H diff --git a/services/cloudsyncservice/include/data_syncer/drivekit.h b/services/cloudsyncservice/include/data_syncer/drivekit.h new file mode 100644 index 0000000000000000000000000000000000000000..75088a2b0293e37b0d963667cb40de41f33f3d30 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/drivekit.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2022 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 DRIVEKIT_H +#define DRIVEKIT_H + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class DriveKit { +public: + class DKContext { + public: + virtual ~DKContext() = default; + + void SetData(void *data) { data_ = data; } + void *GetData() { return data_; } + private: + void *data_; + }; + + enum class DKLocalErrorCode { + NO_ERROR = 0, + }; + + class DKRecordField { + public: + DKRecordField() {} + + explicit DKRecordField(int32_t val) {} + explicit DKRecordField(int64_t val) {} + explicit DKRecordField(double val) {} + explicit DKRecordField(bool val) {} + explicit DKRecordField(std::string &val) {} + + DKRecordField(const DKRecordField &f) {} + DKRecordField &operator=(const DKRecordField &f) + { + return *this; + } + + operator int () const { return 0; } + operator int64_t () const { return 0; } + operator std::string () const { return ""; } + }; + + using DKRecordType = std::string; + using DKFieldKey = std::string; + using DKRecordDatas = std::map; + + class Record { + public: + void SetRecordDatas(DKRecordDatas &fields) {} + void GetRecordDatas(DKRecordDatas &fields) const {} + }; + + using DKRecordId = std::string; + class DKAsset { + }; + class DKDownloadAsset { + public: + DKRecordType recordType; + DKRecordId recordId; + DKFieldKey fieldKey; + DKAsset asset; + }; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // DRIVEKIT_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/data_syncer/file_manager.h b/services/cloudsyncservice/include/data_syncer/file_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..57c22e412e6d9341812c3c89f2f66ceae8037506 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/file_manager.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2022 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 FILE_MANAGER_H +#define FILE_MANAGER_H + +#include +#include +#include +#include + +#include "rdb_helper.h" +#include "values_bucket.h" +#include "result_set.h" +#include "abs_rdb_predicates.h" + +#include "drivekit.h" +#include "data_provider.h" +#include "data_syncer.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class File { +public: + virtual int32_t Open(); + +private: + enum { + DOWNLOAD, + UPLOAD + }; + + std::string path_; + int32_t type_; + int32_t percent_; +}; + +class FileManagerRdbCallback : public NativeRdb::RdbOpenCallback { +public: + virtual int32_t OnCreate(NativeRdb::RdbStore &r) override; + virtual int32_t OnUpgrade(NativeRdb::RdbStore &r, int32_t oldVersion, + int32_t newVersion) override; +}; + +class FileManagerContext : public DriveKit::DKContext { +public: + FileManagerContext(int32_t id, std::shared_ptr provider) : + id_(id), provider_(provider) + {} + + int32_t GetId() + { + return id_; + } + + std::shared_ptr GetProvider() + { + return provider_; + } + +private: + int32_t id_; + std::shared_ptr provider_; +}; + +class FileManager final { +public: + static std::shared_ptr GetInstance(); + + /* download */ + int32_t DownloadFiles(std::vector assets, + std::shared_ptr provider); + + int32_t CancelDownloadFiles(std::vector assets); + int32_t CancelDownloadFiles(); + + int32_t OnDownloadProgress(std::vector); + int32_t OnDownloadResult(std::shared_ptr context, + std::vector); + + /* upload */ + int32_t OnOpenFile(); + int32_t CancelUploadFile(); + int32_t OnUploadProgress(); + int32_t OnUploadResult(); + + /* query */ + bool IsIdle(); + int32_t GetDownloadProgress(); + int32_t GetUploadProgress(); + + /* wait */ + bool IsWaitting(); + int32_t Wait(std::shared_ptr dataSyncer); + +private: + FileManager(); + + /* data syncer */ + std::shared_ptr dataSyncer_; + + /* congestion control */ + int32_t GetTaskId(); + + /* instance */ + static std::mutex instanceMutex_; + static std::shared_ptr instance_; + + /* download queue */ + std::list runningQueue_; + std::list pendingQueue_; + static std::shared_mutex queueMutex_; + + /* rdb */ + std::shared_ptr rdb_; + const std::string path_ = "/data/app/el2/100/database/com.ohos.medialibrary.medialibrarydata/rdb"; + const std::string name_ = "media_library.db"; + const std::string bundleName_ = "com.ohos.medialibrary.medialibrarydata"; + const int32_t area_ = 1; + const int32_t connectSize_ = 10; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // FILE_MANAGER_H diff --git a/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h new file mode 100644 index 0000000000000000000000000000000000000000..7bfeaa7e465146cd8c96126336749a86428a5d0b --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 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 ALBUM_DATA_PROVIDER_H +#define ALBUM_DATA_PROVIDER_H + +#include "data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class AlbumDataProvider : public RdbProvider { +public: + AlbumDataProvider(std::shared_ptr rdb); + virtual ~AlbumDataProvider() = default; + + /* download */ + virtual int32_t OnFetchRecords(const std::vector &records) override; + virtual int32_t GetFetchCondition() override; + + /* upload */ + virtual int32_t GetCreatedRecords(std::vector &records) override; + virtual int32_t GetDeletedRecords(std::vector &records) override; + virtual int32_t GetModifiedRecords(std::vector &records) override; + + virtual int32_t OnCreateRecords(const std::vector &records) override; + virtual int32_t OnDeleteRecords(const std::vector &records) override; + virtual int32_t OnModifyRecords(const std::vector &records) override; + +private: + const std::string tableName_ = "albums"; + + /* create */ + const NativeRdb::AbsRdbPredicates *createPredicates_; + const DataConvertor createConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* delete */ + const NativeRdb::AbsRdbPredicates *deletePredicates_; + const DataConvertor deleteConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* update */ + const NativeRdb::AbsRdbPredicates *updatePredicates_; + const DataConvertor updateConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* fetch */ +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // ALBUM_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h new file mode 100644 index 0000000000000000000000000000000000000000..37a462ad4b7f3df656d7a8492a91510d2980d0ce --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 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 FILE_DATA_PROVIDER_H +#define FILE_DATA_PROVIDER_H + +#include "data_provider.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class FileDataProvider : public RdbProvider { +public: + FileDataProvider(std::shared_ptr rdb); + virtual ~FileDataProvider() = default; + + /* download */ + virtual int32_t GetFetchCondition() override; + virtual int32_t OnFetchRecords(const std::vector &records) override; + + /* upload */ + virtual int32_t GetCreatedRecords(std::vector &records) override; + virtual int32_t GetDeletedRecords(std::vector &records) override; + virtual int32_t GetModifiedRecords(std::vector &records) override; + + /* file */ + virtual int32_t OnDownloadFiles() override; + + /* callback */ + virtual int32_t OnCreateRecords(const std::vector &records) override; + virtual int32_t OnDeleteRecords(const std::vector &records) override; + virtual int32_t OnModifyRecords(const std::vector &records) override; + +private: + const std::string tableName_ = "Files"; + + /* create */ + const NativeRdb::AbsRdbPredicates *createPredicates_; + const DataConvertor createConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* delete */ + const NativeRdb::AbsRdbPredicates *deletePredicates_; + const DataConvertor deleteConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* update */ + const NativeRdb::AbsRdbPredicates *updatePredicates_; + const DataConvertor updateConvertor_ = { + { "file_id", "data", "size" }, + { "id", "path", "size" }, + { INT, STRING, INT }, + 3 + }; + + /* fetch */ +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // FILE_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h new file mode 100644 index 0000000000000000000000000000000000000000..96399db2c5d4ef9baefbe1a20a30041072d716e0 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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 GALLERY_DATA_SYNCER_H +#define GALLERY_DATA_SYNCER_H + +#include "data_syncer.h" + +#include "file_data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class GalleryDataSyncer : public DataSyncer, std::enable_shared_from_this { +public: + GalleryDataSyncer(); + + virtual void Schedule() override; + +private: + enum { + BEGIN, + DOWNLOADALBUM, + DOWNLOADFILE, + UPLOADALBUM, + UPLOADFILE, + WAIT, + END + }; + + int32_t DownloadAlbum(); + int32_t DownloadFile(); + int32_t UploadAlbum(); + int32_t UploadFile(); + int32_t Wait(); + int32_t Complete(); + + int32_t OnWait(); + + int32_t stage_ = BEGIN; + + /* rdb */ + std::shared_ptr rdb_; + + const std::string path_ = "/data/app/el2/100/database/com.ohos.medialibrary.medialibrarydata/rdb"; + const std::string rdbName_ = "media_library.db"; + const std::string bundleName_ = "com.ohos.medialibrary.medialibrarydata"; + const int32_t area_ = 1; + const int32_t connectSize_ = 10; + + /* provider */ + std::shared_ptr fileProvider_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // GALLERY_DATA_SYNCER_H diff --git a/services/cloudsyncservice/include/data_syncer/sdk_helper.h b/services/cloudsyncservice/include/data_syncer/sdk_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..67c974ddb82788cf70914fd1eaa620af4e2684be --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/sdk_helper.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 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 SDK_HELPER_H +#define SDK_HELPER_H + +#include +#include + +#include "drivekit.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { + +class SdkHelper { +public: + /* record download */ + static int32_t FetchRecords(std::shared_ptr context, + std::function context, std::vector &)> callback); + + /* record upload */ + static int32_t CreateRecords(std::shared_ptr context, + std::vector &records, + std::function context, std::vector &)> callback); + + static int32_t DeleteRecords(std::shared_ptr context, + std::vector &records, + std::function context, std::vector &)> callback); + + static int32_t ModifyRecords(std::shared_ptr context, + std::vector &records, + std::function context, std::vector &)> callback); + + /* asset download */ + static int32_t DownloadAssets(std::shared_ptr context, + std::vector recordIds, int32_t id, + std::function context, std::vector)> resultCallback); + + static int32_t CancelDownloadAssets(int32_t id); +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // SDK_HELPER_H diff --git a/services/cloudsyncservice/include/data_syncer/task.h b/services/cloudsyncservice/include/data_syncer/task.h new file mode 100644 index 0000000000000000000000000000000000000000..9ec1a5287eb82d05789f75dd5ef4bcecab518048 --- /dev/null +++ b/services/cloudsyncservice/include/data_syncer/task.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022 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 TASK_H +#define TASK_H + +#include +#include +#include +#include +#include + +#include "thread_pool.h" + +#include "drivekit.h" +#include "data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +class TaskContext : public DriveKit::DKContext { +public: + TaskContext(std::shared_ptr provider) : provider_(provider) + {} + + std::shared_ptr GetProvider() + { + return provider_; + } + +private: + std::shared_ptr provider_; +}; + +class Task { +public: + using Action = std::function)>; + + Task(std::shared_ptr context, Action action) : context_(context), + action_(action) + { + id_ = GenerateTaskId(); + } + virtual ~Task() = default; + + virtual void Run() + { + action_(context_); + } + + int32_t GetId() + { + return id_; + } + +private: + int32_t GenerateTaskId(); + + int32_t id_; + std::shared_ptr context_; + Action action_; +}; + +class TaskManager { +public: + TaskManager(); + virtual ~TaskManager(); + + int32_t CommitTask(std::shared_ptr t); + int32_t CompleteTask(std::shared_ptr t); + + bool IsIdle(); + + bool StopAndWaitFor(); + +private: + ThreadPool pool_ = ThreadPool("TaskManager"); + const int32_t MAX_THREAD_NUM = 4; + const int32_t WAIT_FOR_SECOND = 30; + + bool stopFlag_ = false; + + std::shared_mutex mutex_; + std::list> taskList_; + /* cv for shared_mutex */ + std::condition_variable_any cv_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // TASK_H diff --git a/services/cloudsyncservice/src/data_syncer/.data_syncer.cpp.swp b/services/cloudsyncservice/src/data_syncer/.data_syncer.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..3289155fedfef2beb450b009dc7abcde5c55b3db Binary files /dev/null and b/services/cloudsyncservice/src/data_syncer/.data_syncer.cpp.swp differ diff --git a/services/cloudsyncservice/src/data_syncer/data_provider.cpp b/services/cloudsyncservice/src/data_syncer/data_provider.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74d25d81fc4c8333c57dd279ef9ce4b7ea76e76b --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/data_provider.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2022 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 "data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace NativeRdb; + +int32_t DataProvider::SetCursor() +{ + return 0; +} + +int32_t DataProvider::GetCursor() +{ + return 0; +} + +int32_t DataProvider::DownloadFiles() +{ + return 0; +} + +int32_t DataProvider::OnDownloadFiles() +{ + return 0; +} + +int32_t RdbProvider::Insert(int64_t &outRowId, const ValuesBucket &initiavalues) +{ + return rdb_->Insert(outRowId, tableName_, initiavalues); +} + +int32_t RdbProvider::Update(int &changedRows, const ValuesBucket &values, + const string &whereClause, const vector &whereArgs) +{ + return rdb_->Update(changedRows, tableName_, values, whereClause, whereArgs); +} + +int32_t RdbProvider::Delete(int &deletedRows, const string &whereClause, const vector &whereArgs) +{ + return rdb_->Delete(deletedRows, tableName_, whereClause, whereArgs); +} + +unique_ptr RdbProvider::Query( + const NativeRdb::AbsRdbPredicates &predicates, const std::vector columns) +{ + return rdb_->Query(predicates, columns); +} + +int32_t RdbProvider::ResultSetToRecords(const unique_ptr resultSet, + std::vector &records, const DataConvertor &dc) +{ + /* resize to avoid repeatedly alloc and copy */ + int32_t rowCount = 0; + int ret = resultSet->GetRowCount(rowCount); + if (ret != 0) { + + } + records.resize(rowCount); + + /* iterate */ + while (resultSet->GoToNextRow() == 0) { + DriveKit::Record record; + DriveKit::DKRecordDatas data; + for (int i = 0; i < dc.size; i++) { + DataType type = dc.types[i]; + switch (type) { + case INT: { + int32_t val; + int32_t err = resultSet->GetInt(i, val); + if (err != 0) { + + } + data[dc.cloudColumns[i]] = DriveKit::DKRecordField(val); + break; + } + case LONG: { + int64_t val; + int32_t err = resultSet->GetLong(i, val); + if (err != 0) { + + } + data[dc.cloudColumns[i]] = DriveKit::DKRecordField(val); + break; + } + case STRING: { + string val; + int32_t err = resultSet->GetString(i, val); + if (err != 0) { + + } + data[dc.cloudColumns[i]] = DriveKit::DKRecordField(val); + break; + } + default: + break; + } + } + record.SetRecordDatas(data); + records.emplace_back(record); + } + + return 0; +} + +int32_t RdbProvider::RecordsToValueBuckets(const std::vector &records, + vector &valueBuckets, const DataConvertor &dc) +{ + /* resize to avoid repeatedly alloc and copy */ + valueBuckets.resize(records.size()); + + /* iterate records */ + for (uint32_t i = 0; i < records.size(); i++) { + DriveKit::DKRecordDatas data; + records[i].GetRecordDatas(data); + NativeRdb::ValuesBucket v; + for (int32_t j = 0; j < dc.size; j++) { + DataType type = dc.types[j]; + switch (type) { + case INT: { + v.PutInt(dc.localColumns[j], data[dc.cloudColumns[j]]); + break; + } + case LONG: { + v.PutLong(dc.localColumns[j], data[dc.cloudColumns[j]]); + break; + } + case STRING: { + v.PutString(dc.localColumns[j], data[dc.cloudColumns[j]]); + break; + } + default: + break; + } + } + valueBuckets[i] = v; + } + + return 0; +} + +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/services/cloudsyncservice/src/data_syncer/data_syncer.cpp b/services/cloudsyncservice/src/data_syncer/data_syncer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c5de711b6b29ce420d049037311184148c287ee --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/data_syncer.cpp @@ -0,0 +1,430 @@ +/* + * Copyright (c) 2022 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 "data_syncer.h" + +#include + +#include "dfs_error.h" +#include "utils_log.h" +#include "sdk_helper.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace placeholders; + +int32_t DataSyncer::Init(const string &name) +{ + name_ = name; + + taskManager_ = make_shared(); + if (taskManager_ == nullptr) { + LOGE("init task manager err"); + return E_MEMORY; + } + + return E_OK; +} + +shared_ptr DataSyncer::GetRdb(NativeRdb::RdbStoreConfig &config) +{ + int32_t err; + RdbCallback cb; + auto rdb = NativeRdb::RdbHelper::GetRdbStore(config, 0, cb, err); + if (rdb == nullptr) { + LOGE("get rdb store err %{public}d", err); + } + + return rdb; +} + +int32_t DataSyncer::StartSync() +{ + LOGI("data syncer %{public}s starts sync", name_.c_str()); + + Schedule(); + + return E_OK; +} + +int32_t DataSyncer::StopSync() +{ + LOGI("data syncer %{public}s stops sync", name_.c_str()); + return E_OK; +} + +void DataSyncer::Abort(int32_t reason) +{ + LOGI("data syncer %{public}s aborts, the reason is %{public}d", name_.c_str(), + reason); + + /* stop all the tasks and wait for tasks' termination */ + if (!taskManager_->StopAndWaitFor()) { + LOGE("wait for tasks stop fail"); + } + + /* + * call the syncer manager's callback for notification + */ +} + +void DataSyncer::CompletePull() +{ + LOGI("data syncer %{public}s completes pull", name_.c_str()); + + /* + * call syncer manager callback + */ +} + +void DataSyncer::CompletePush() +{ + LOGI("data syncer %{public}s completes push", name_.c_str()); + + /* + * call syncer manager callback + */ +} + +void DataSyncer::CompleteAll() +{ + LOGI("data syncer %{public}s completes all", name_.c_str()); + + /* + * call syncer manager callback + */ +} + +void DataSyncer::PullRecords(shared_ptr context) +{ + /* get query condition */ + /* auto condition = provider->GetCondition(); */ + + auto callback = AsyncCallback(&DataSyncer::OnFetchRecords, this); + int32_t ret = SdkHelper::FetchRecords(context, callback); + if (ret != E_OK) { + LOGE("sdk fetch records err %{public}d", ret); + /* + * Records from the cloud should be all pulled down before + * uploading the local change , so the sycner would just + * abort here. + */ + Abort(ret); + } +} + +int32_t DataSyncer::Pull(shared_ptr provider) +{ + LOGI("data syncer %{public}s pull begin", name_.c_str()); + + shared_ptr context = make_shared(provider); + int32_t ret = AsyncRun(context, &DataSyncer::PullRecords); + if (ret != E_OK) { + LOGE("asyn run pull records err %{public}d", ret); + return ret; + } + + return E_OK; +} + +void DataSyncer::OnFetchRecords(const shared_ptr context, + const vector &records) +{ + auto ctx = static_pointer_cast(context); + + /* no more records */ + if (records.size() == 0) { + if (IsSchedulable()) { + Schedule(); + } + return; + } + + /* update local */ + auto provider = ctx->GetProvider(); + if (provider == nullptr) { + LOGE("context get provider err"); + Abort(E_CONTEXT); + return; + } + int32_t ret = provider->OnFetchRecords(records); + if (ret != 0) { + LOGE("provider on fetch records err %{public}d", ret); + Abort(ret); + return; + } + + /* pull more */ + ret = Pull(provider); + if (ret != 0) { + LOGE("pull more records err %{public}d", ret); + Abort(ret); + } +} + +void DataSyncer::CreateRecords(shared_ptr context) +{ + auto provider = context->GetProvider(); + if (provider == nullptr) { + LOGE("context get provider err"); + return; + } + + /* query local */ + vector records; + int32_t ret = provider->GetCreatedRecords(records); + if (ret != 0) { + LOGE("provider get created records err %{public}d", ret); + return; + } + + /* no need upload */ + if (records.size() == 0) { + if (IsSchedulable()) { + Schedule(); + return; + } + } + + /* upload */ + auto callback = AsyncCallback(&DataSyncer::OnCreateRecords, this); + ret = SdkHelper::CreateRecords(context, records, callback); + if (ret != 0) { + LOGE("sdk create records err %{public}d", ret); + return; + } +} + +void DataSyncer::DeleteRecords(shared_ptr context) +{ + auto provider = context->GetProvider(); + if (provider == nullptr) { + LOGE("context get provider err"); + return; + } + + /* query local */ + vector records; + int32_t ret = provider->GetDeletedRecords(records); + if (ret != 0) { + LOGE("provider get deleted records err %{public}d", ret); + return; + } + + /* no need upload */ + if (records.size() == 0) { + if (IsSchedulable()) { + Schedule(); + return; + } + } + + /* upload */ + auto callback = AsyncCallback(&DataSyncer::OnDeleteRecords, this); + ret = SdkHelper::DeleteRecords(context, records, callback); + if (ret != 0) { + LOGE("sdk delete records err %{public}d", ret); + } +} + +void DataSyncer::ModifyRecords(shared_ptr context) +{ + auto provider = context->GetProvider(); + if (provider == nullptr) { + LOGE("context get provider err"); + return; + } + + /* query local */ + vector records; + int32_t ret = provider->GetModifiedRecords(records); + if (ret != 0) { + LOGE("provider get modified records err %{public}d", ret); + return; + } + + /* no need upload */ + if (records.size() == 0) { + if (IsSchedulable()) { + Schedule(); + return; + } + } + + /* upload */ + auto callback = AsyncCallback(&DataSyncer::OnModifyRecords, this); + ret = SdkHelper::ModifyRecords(context, records, callback); + if (ret != 0) { + LOGE("sdk modify records err %{public}d", ret); + } +} + +int32_t DataSyncer::Push(shared_ptr provider) +{ + /* + * Bugfix: If the first callback find no more records before + * below tasks commited, syncer will directly schedule to next stage. + * + * One possible solution: commit dummy task. + */ + shared_ptr context = make_shared(provider); + if (context == nullptr) { + LOGE("alloc context fail"); + return E_MEMORY; + } + + int32_t ret = AsyncRun(context, &DataSyncer::CreateRecords); + if (ret != 0) { + LOGE("async run create records err %{public}d", ret); + } + + ret = AsyncRun(context, &DataSyncer::DeleteRecords); + if (ret != 0) { + LOGE("async run delete records err %{public}d", ret); + } + + ret = AsyncRun(context, &DataSyncer::ModifyRecords); + if (ret != 0) { + LOGE("async run modify records err %{public}d", ret); + } + + return E_OK; +} + +int32_t DataSyncer::AsyncRun(std::shared_ptr context, + void(DataSyncer::*f)(std::shared_ptr)) +{ + shared_ptr task = make_shared(context, + [this, f](shared_ptr ctx) { + (this->*f)(ctx); + } + ); + int32_t ret = CommitTask(task); + if (ret != 0) { + + } + + return 0; +} + +template +function DataSyncer::AsyncCallback(RET(T::*f)(ARGS...), T *t) +{ + /* + * ARGS... args: + * async execute requires value-copy or shared_ptr like + * input parameters, but no reference for lifecycle + * consideration. + * In addition, [=] requires the wrapped function with + * const parameters. + */ + return [=](ARGS... args) -> RET { + shared_ptr task = make_shared(nullptr, + [=](shared_ptr) { + (t->*f)(args...); + } + ); + int32_t ret = CommitTask(task); + if (ret != 0) { + + } + }; +} + +void DataSyncer::OnCreateRecords(const std::shared_ptr context, + const vector &records) +{ + auto ctx = static_pointer_cast(context); + auto provider = ctx->GetProvider(); + + /* update local */ + int32_t ret = provider->OnCreateRecords(records); + if (ret != 0) { + + } + + /* push more */ + ret = AsyncRun(ctx, &DataSyncer::CreateRecords); + if (ret != 0) { + + } +} + +void DataSyncer::OnDeleteRecords(const std::shared_ptr context, + const vector &records) +{ + auto ctx = static_pointer_cast(context); + auto provider = ctx->GetProvider(); + + /* update local */ + int32_t ret = provider->OnDeleteRecords(records); + if (ret != 0) { + } + + /* push more */ + ret = AsyncRun(ctx, &DataSyncer::DeleteRecords); + if (ret != 0) { + + } +} + +void DataSyncer::OnModifyRecords(const std::shared_ptr context, + const vector &records) +{ + auto ctx = static_pointer_cast(context); + auto provider = ctx->GetProvider(); + + /* update local */ + int32_t ret = provider->OnModifyRecords(records); + if (ret != 0) { + } + + /* push more */ + ret = AsyncRun(ctx, &DataSyncer::ModifyRecords); + if (ret != 0) { + + } +} + +int32_t DataSyncer::OnWait() +{ + /* notify data syncer manager */ + return 0; +} + +int32_t DataSyncer::CommitTask(shared_ptr t) +{ + return taskManager_->CommitTask(t); +} + +int32_t DataSyncer::CompleteTask(shared_ptr t) +{ + return taskManager_->CompleteTask(t); +} + +bool DataSyncer::IsSchedulable() +{ + /* + * Make a check. + * Is it the time to transfer to the next stage? + * More precisely, no more tasks pending. + */ + return taskManager_->IsIdle(); +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_syncer/file_manager.cpp b/services/cloudsyncservice/src/data_syncer/file_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..926e4c155aa57b21bbe1d086a28264af9740e1a5 --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/file_manager.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2022 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_manager.h" + +#include "sdk_helper.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace placeholders; + +shared_ptr FileManager::instance_ = nullptr; +mutex FileManager::instanceMutex_; +shared_mutex FileManager::queueMutex_; + +FileManager::FileManager() +{ + int32_t err; + FileManagerRdbCallback cb; + + NativeRdb::RdbStoreConfig config(path_); + config.SetName(name_); + config.SetBundleName(bundleName_); + config.SetArea(area_); + config.SetReadConSize(connectSize_); + + rdb_ = NativeRdb::RdbHelper::GetRdbStore(config, 0, cb, err); + if (rdb_ == nullptr) { + + } +} + +shared_ptr FileManager::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard guard(instanceMutex_); + + if (instance_ != nullptr) { + return instance_; + } + + instance_ = std::shared_ptr(); + } + + return instance_; +} + +int32_t FileManagerRdbCallback::OnCreate(NativeRdb::RdbStore &rdb) +{ + /** + * ------------------------------------------------ + * | id | path | status | progress | app | userId | + * ------------------------------------------------ + * | 1 | /data | Upload | 0.5 | medialibrary | 100 | + * ------------------------------------------------- + */ + return 0; +} + +int32_t FileManagerRdbCallback::OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion, + int32_t newVersion) +{ + return 0; +} + +int32_t FileManager::GetTaskId() +{ + /* todo */ + return 0; +} + +int32_t FileManager::DownloadFiles(vector assets, + shared_ptr provider) +{ + unique_lock lock(queueMutex_); + int32_t id = GetTaskId(); + runningQueue_.emplace_back(id); + lock.unlock(); + + shared_ptr context = make_shared( + id, provider + ); + int32_t ret = SdkHelper::DownloadAssets(context, assets, id, + bind(&FileManager::OnDownloadResult, this, _1, _2)); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileManager::CancelDownloadFiles(vector assets) +{ + return 0; +} + +int32_t FileManager::CancelDownloadFiles() +{ + unique_lock lock(queueMutex_); + + /* clear pending */ + pendingQueue_.clear(); + + /* cancel running */ + for (auto entry = runningQueue_.begin(); entry != runningQueue_.end(); entry++) { + int32_t ret = SdkHelper::CancelDownloadAssets(*entry); + if (ret != 0) { + + } + } + + return 0; +} + +int32_t FileManager::OnDownloadProgress(vector) +{ + return 0; +} + +int32_t FileManager::OnDownloadResult(shared_ptr context, + vector assets) +{ + auto ctx = static_pointer_cast(context); + + /* provider callback */ + int32_t ret = ctx->GetProvider()->OnDownloadFiles(); + if (ret != 0) { + + } + + /* task finished */ + unique_lock lock(queueMutex_); + for (auto entry = runningQueue_.begin(); entry != runningQueue_.end(); entry++) { + if (*entry == ctx->GetId()) { + runningQueue_.erase(entry); + break; + } + } + + /* data sycner callback */ + if (runningQueue_.empty()) { + if (IsWaitting()) { + lock.unlock(); + dataSyncer_->OnWait(); + } + } + + return 0; +} + +bool FileManager::IsWaitting() +{ + return dataSyncer_ != nullptr; +} + +int32_t FileManager::Wait(shared_ptr dataSyncer) +{ + dataSyncer_ = dataSyncer; + + return 0; +} + +bool FileManager::IsIdle() +{ + shared_lock lock(queueMutex_); + return runningQueue_.empty(); +} + +int32_t FileManager::OnOpenFile() +{ + return 0; +} + +int32_t FileManager::CancelUploadFile() +{ + return 0; +} + +int32_t FileManager::OnUploadProgress() +{ + return 0; +} + +int32_t FileManager::OnUploadResult() +{ + return 0; +} + +int32_t FileManager::GetDownloadProgress() +{ + return 0; +} + +int32_t FileManager::GetUploadProgress() +{ + return 0; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78fc192e228d2dcc5c9ffc8a78ac078f41522490 --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022 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 "album_data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace NativeRdb; + +AlbumDataProvider::AlbumDataProvider(std::shared_ptr rdb) : + RdbProvider(const_cast(tableName_), rdb) +{ +} + +int32_t AlbumDataProvider::GetFetchCondition() +{ + return 0; +} + +int32_t AlbumDataProvider::OnFetchRecords(const vector &records) +{ + return 0; +} + +int32_t AlbumDataProvider::GetCreatedRecords(vector &records) +{ + auto results = Query(*createPredicates_, createConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::GetDeletedRecords(vector &records) +{ + auto results = Query(*deletePredicates_, deleteConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::GetModifiedRecords(vector &records) +{ + auto results = Query(*updatePredicates_, updateConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, updateConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::OnCreateRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::OnDeleteRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::OnModifyRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, updateConvertor_); + if (ret != 0) { + + } + + return 0; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ff28788b4bcb31e223bbed8fddc4a812c70abe7 --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 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_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; +using namespace NativeRdb; + +FileDataProvider::FileDataProvider(std::shared_ptr rdb) : + RdbProvider(const_cast(tableName_), rdb) +{ +} + +int32_t FileDataProvider::OnDownloadFiles() +{ + return 0; +} + +int32_t FileDataProvider::GetFetchCondition() +{ + return 0; +} + +int32_t FileDataProvider::OnFetchRecords(const vector &records) +{ + return 0; +} + +int32_t FileDataProvider::GetCreatedRecords(vector &records) +{ + auto results = Query(*createPredicates_, createConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::GetDeletedRecords(vector &records) +{ + auto results = Query(*deletePredicates_, deleteConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::GetModifiedRecords(vector &records) +{ + auto results = Query(*updatePredicates_, updateConvertor_.localColumns); + if (results == nullptr) { + + } + + int ret = ResultSetToRecords(move(results), records, updateConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::OnCreateRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::OnDeleteRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::OnModifyRecords(const std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, updateConvertor_); + if (ret != 0) { + + } + + return 0; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe960e4874bacdc929c08895dfd87b03e54eaf6d --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2022 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 "gallery_data_syncer.h" + +#include "dfs_error.h" +#include "utils_log.h" +#include "file_manager.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; + +GalleryDataSyncer::GalleryDataSyncer() +{ + int32_t ret = Init("GalleryDataSyncer"); + if (ret != E_OK) { + LOGE("data syncer init err %{public}d", ret); + } + + NativeRdb::RdbStoreConfig config(path_); + config.SetName(rdbName_); + config.SetBundleName(bundleName_); + config.SetArea(area_); + config.SetReadConSize(connectSize_); + rdb_ = GetRdb(config); + if (rdb_ == nullptr) { + LOGE("gallyer data syncer init rdb fail"); + } + + fileProvider_ = make_shared(rdb_); +} + +void GalleryDataSyncer::Schedule() +{ + LOGI("current stage is %{public}d", stage_); + stage_++; + + int32_t ret = E_OK; + switch (stage_) { + case DOWNLOADALBUM: { + ret = DownloadAlbum(); + break; + } + case DOWNLOADFILE: { + ret = DownloadFile(); + break; + } + case UPLOADALBUM: { + ret = UploadAlbum(); + break; + } + case UPLOADFILE: { + ret = UploadFile(); + break; + } + case WAIT: { + ret = Wait(); + break; + } + case END: { + ret = Complete(); + break; + } + default: + ret = E_SCHEDULE; + break; + } + + if (ret != E_OK) { + LOGE("schedule fail %{public}d", ret); + Abort(ret); + } + + LOGI("schedule to the stage %{public}d", stage_); +} + +int32_t GalleryDataSyncer::DownloadAlbum() +{ + return E_OK; +} + +int32_t GalleryDataSyncer::DownloadFile() +{ + int ret = Pull(fileProvider_); + if (ret != E_OK) { + + } + + return E_OK; +} + +int32_t GalleryDataSyncer::UploadAlbum() +{ + return E_OK; +} + +int32_t GalleryDataSyncer::UploadFile() +{ + int ret = Push(fileProvider_); + if (ret != E_OK) { + + } + + return E_OK; +} + +int32_t GalleryDataSyncer::Wait() +{ + int32_t ret = FileManager::GetInstance()->Wait(shared_from_this()); + if (ret != E_OK) { + + } + + return E_OK; +} + +int32_t GalleryDataSyncer::OnWait() +{ + /* notify data syncer manager */ + + Schedule(); + + return E_OK; +} + +int32_t GalleryDataSyncer::Complete() +{ + CompleteAll(); + return E_OK; +} + +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_syncer/sdk_helper.cpp b/services/cloudsyncservice/src/data_syncer/sdk_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbb1118803225587a4f4443aaf2073aa48c2058c --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/sdk_helper.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 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 "sdk_helper.h" +#include "task.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; + +int32_t SdkHelper::FetchRecords(shared_ptr context, + function context, vector &)> callback) +{ + return 0; +} + +int32_t SdkHelper::CreateRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) +{ + // DriveKit::CreateRecords(records, callback); + + return 0; +} + +int32_t SdkHelper::DeleteRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) +{ + + return 0; +} + +int32_t SdkHelper::ModifyRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) +{ + + return 0; +} + +int32_t SdkHelper::DownloadAssets(shared_ptr context, + vector recordIds, int32_t id, + function context, vector)> resultCallback) +{ + return 0; +} + +int32_t SdkHelper::CancelDownloadAssets(int32_t id) +{ + return 0; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/cloudsyncservice/src/data_syncer/task.cpp b/services/cloudsyncservice/src/data_syncer/task.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a15c1eb3b1b8f172bb4e8c6244dc9f53ce3d3c77 --- /dev/null +++ b/services/cloudsyncservice/src/data_syncer/task.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2022 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 "task.h" + +#include +#include + +#include "dfs_error.h" +#include "utils_log.h" +#include "sdk_helper.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; + +int32_t Task::GenerateTaskId() +{ + /* todo */ + return 0; +} + +TaskManager::TaskManager() +{ + pool_.SetMaxTaskNum(MAX_THREAD_NUM); + pool_.Start(0); +} + +TaskManager::~TaskManager() +{ + pool_.Stop(); +} + +int32_t TaskManager::CommitTask(shared_ptr t) +{ + unique_lock lock(mutex_); + + if (stopFlag_) { + LOGI("commit task fail since stop"); + return E_STOP; + } + + taskList_.emplace_back(t); + + /* + * Task t is held by the task list in the + * TaskMangaer. + * After the wrapped func execution, Task t + * would be released. + */ + pool_.AddTask([t, this]() { + t->Run(); + this->CompleteTask(t); + }); + + return E_OK; +} + +int32_t TaskManager::CompleteTask(shared_ptr t) +{ + unique_lock lock(mutex_); + for (auto entry = taskList_.begin(); entry != taskList_.end();) { + if (entry->get()->GetId() == t->GetId()) + (void)taskList_.erase(entry); + else + entry++; + } + + if (stopFlag_ && taskList_.empty()) { + cv_.notify_all(); + } + + return 0; +} + +bool TaskManager::StopAndWaitFor() +{ + unique_lock lock(mutex_); + LOGI("task manager stop"); + + stopFlag_ = true; + + return cv_.wait_for(lock, chrono::seconds(WAIT_FOR_SECOND), [this] { + return taskList_.empty(); + }); +} + + +bool TaskManager::IsIdle() +{ + shared_lock lock(mutex_); + /* + * This method is called when one task finds it suitable + * to schedule to the next stage, yet before the task + * being removed from task list. + * If no more tasks on the way, it equals to 1. + */ + return taskList_.size() == 1; +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS diff --git a/services/distributedfiledaemon/include/ipc/daemon.h b/services/distributedfiledaemon/include/ipc/daemon.h index 7843c9bd4c9cc5638bc7d95ef6b51eff26a91dae..c2983f7b4277d96a2491f892c14c194a3f113866 100644 --- a/services/distributedfiledaemon/include/ipc/daemon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -64,4 +64,4 @@ private: } // namespace DistributedFile } // namespace Storage } // namespace OHOS -#endif // DAEMON_H \ No newline at end of file +#endif // DAEMON_H diff --git a/utils/log/include/dfs_error.h b/utils/log/include/dfs_error.h index c97e6ad5d5b4ee1425923bbb18cdcf0bc0c5f7ee..a058374a5091cf49a98a5e1d39818a29025ad742 100644 --- a/utils/log/include/dfs_error.h +++ b/utils/log/include/dfs_error.h @@ -37,7 +37,16 @@ enum CloudSyncServiceErrCode : ErrCode { E_SERVICE_DESCRIPTOR_IS_EMPTY, E_PERMISSION_DENIED, E_GET_TOKEN_INFO_ERROR, + + /* data syncer */ + E_CLOUD_SDK, + E_RDB, + E_CONTEXT, + E_MEMORY, + E_STOP, + E_SCHEDULE, + E_ASYNC_RUN, }; } // namespace OHOS::FileManagement -#endif // OHOS_FILEMGMT_DFS_ERROR_H \ No newline at end of file +#endif // OHOS_FILEMGMT_DFS_ERROR_H