From fdd497cf6f4d2ff4c7069c47f8b036ff1bc26ecd Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Mon, 16 Jan 2023 02:14:42 +0000 Subject: [PATCH 1/7] add data syncer and data provider Signed-off-by: jiahaoluo Change-Id: I61775487b40768fd185f68087085b0a2f13ac052 --- BUILD.gn | 1 + services/cloud_sync/BUILD.gn | 42 +++++ services/cloud_sync/gallery/BUILD.gn | 0 .../gallery/include/gallery_data_syncer.h | 31 +++ .../gallery/src/gallery_data_syncer.cpp | 25 +++ services/cloud_sync/include/data_provider.h | 44 +++++ services/cloud_sync/include/data_syncer.h | 64 +++++++ services/cloud_sync/include/drivekit.h | 31 +++ services/cloud_sync/include/sdk_helper.h | 45 +++++ services/cloud_sync/src/data_provider.cpp | 25 +++ services/cloud_sync/src/data_syncer.cpp | 178 ++++++++++++++++++ services/cloud_sync/src/sdk_helper.cpp | 48 +++++ 12 files changed, 534 insertions(+) create mode 100644 services/cloud_sync/BUILD.gn create mode 100644 services/cloud_sync/gallery/BUILD.gn create mode 100644 services/cloud_sync/gallery/include/gallery_data_syncer.h create mode 100644 services/cloud_sync/gallery/src/gallery_data_syncer.cpp create mode 100644 services/cloud_sync/include/data_provider.h create mode 100644 services/cloud_sync/include/data_syncer.h create mode 100644 services/cloud_sync/include/drivekit.h create mode 100644 services/cloud_sync/include/sdk_helper.h create mode 100644 services/cloud_sync/src/data_provider.cpp create mode 100644 services/cloud_sync/src/data_syncer.cpp create mode 100644 services/cloud_sync/src/sdk_helper.cpp diff --git a/BUILD.gn b/BUILD.gn index 7ff292bfb..4954eb29b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -21,6 +21,7 @@ group("services_target") { "${services_path}:distributedfile_sa_profile", "${services_path}/cloudsyncservice:cloudsync_sa", "${services_path}/distributedfiledaemon:libdistributedfiledaemon", + "${services_path}/cloud_sync:cloud_sync" ] } diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn new file mode 100644 index 000000000..251dd16c2 --- /dev/null +++ b/services/cloud_sync/BUILD.gn @@ -0,0 +1,42 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +ohos_shared_library("cloud_sync") { + include_dirs = [ + "include", + "gallery/include" + ] + + sources = [ + "src/data_provider.cpp", + "src/data_syncer.cpp", + "gallery/src/gallery_data_syncer.cpp" + ] + + deps = [ + "//third_party/libuv:uv" + ] + + external_deps = [ + "ability_base:want" + ] + + configs = [ "${utils_path}:compiler_configs" ] + + defines = [ "LOG_TAG=\"distributedfile_cloud\"" ] + + part_name = "dfs_service" + subsystem_name = "filemanagement" +} diff --git a/services/cloud_sync/gallery/BUILD.gn b/services/cloud_sync/gallery/BUILD.gn new file mode 100644 index 000000000..e69de29bb diff --git a/services/cloud_sync/gallery/include/gallery_data_syncer.h b/services/cloud_sync/gallery/include/gallery_data_syncer.h new file mode 100644 index 000000000..098e03109 --- /dev/null +++ b/services/cloud_sync/gallery/include/gallery_data_syncer.h @@ -0,0 +1,31 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class GalleryDataSyncer : public DataSyncer { +public: + +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // GALLERY_DATA_SYNCER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp new file mode 100644 index 000000000..df2657192 --- /dev/null +++ b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp @@ -0,0 +1,25 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace std; + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/include/data_provider.h b/services/cloud_sync/include/data_provider.h new file mode 100644 index 000000000..cdde138b9 --- /dev/null +++ b/services/cloud_sync/include/data_provider.h @@ -0,0 +1,44 @@ +/* + * 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 "drivekit.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class DataProvider { +public: + /* download */ + virtual int32_t OnFetchRecords(std::vector &records) = 0; + + /* upload */ + virtual std::vector GetCreatedRecords() = 0; + virtual std::vector GetDeletedRecords() = 0; + virtual std::vector GetModifiedRecords() = 0; + + virtual int32_t OnCreateResults(std::vector &records) = 0; + virtual int32_t OnDeleteResults(std::vector &records) = 0; + virtual int32_t OnModifyResults(std::vector &records) = 0; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/include/data_syncer.h b/services/cloud_sync/include/data_syncer.h new file mode 100644 index 000000000..b13cf4204 --- /dev/null +++ b/services/cloud_sync/include/data_syncer.h @@ -0,0 +1,64 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class DataSyncer { +public: + int32_t StartSync(); + int32_t StopSync(); + +protected: + /* download */ + int32_t Pull(std::shared_ptr provider); + int32_t Pull(std::vector &idList, std::shared_ptr provider); + + /* upload */ + int32_t Push(std::shared_ptr provider); + + /* download callback */ + int32_t OnFetchRecords(std::vector &records, std::shared_ptr provider); + + /* upload callback */ + int32_t OnCreateResults(std::vector &records, std::shared_ptr provider); + int32_t OnDeleteResults(std::vector &records, std::shared_ptr provider); + int32_t OnModifyResults(std::vector &records, std::shared_ptr provider); + + /* schedule */ + virtual void Schedule() = 0; + + /* retry */ + +private: + int32_t downloadCount_ = 1; + int32_t uploadCount_ = 3; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // DATA_SYNCER_H \ No newline at end of file diff --git a/services/cloud_sync/include/drivekit.h b/services/cloud_sync/include/drivekit.h new file mode 100644 index 000000000..c14c2c2e4 --- /dev/null +++ b/services/cloud_sync/include/drivekit.h @@ -0,0 +1,31 @@ +/* + * 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 + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class DriveKit { +public: + class Record { + + }; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // DRIVEKIT_H \ No newline at end of file diff --git a/services/cloud_sync/include/sdk_helper.h b/services/cloud_sync/include/sdk_helper.h new file mode 100644 index 000000000..97be36152 --- /dev/null +++ b/services/cloud_sync/include/sdk_helper.h @@ -0,0 +1,45 @@ +/* + * 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 Storage { +namespace DistributedFile { +class SdkHelper { +public: + /* download */ + static int32_t FetchRecords(std::function &)> callback); + + /* upload */ + static int32_t CreateRecords(std::vector &records, + std::function &)> callback); + static int32_t DeleteRecords(std::vector &records, + std::function &)> callback); + static int32_t ModifyRecords(std::vector &records, + std::function &)> callback); + + int32_t tmp_; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // SDK_HELPER_H \ No newline at end of file diff --git a/services/cloud_sync/src/data_provider.cpp b/services/cloud_sync/src/data_provider.cpp new file mode 100644 index 000000000..929f2f3eb --- /dev/null +++ b/services/cloud_sync/src/data_provider.cpp @@ -0,0 +1,25 @@ +/* + * 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 Storage { +namespace DistributedFile { + + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp new file mode 100644 index 000000000..b1314e1c7 --- /dev/null +++ b/services/cloud_sync/src/data_syncer.cpp @@ -0,0 +1,178 @@ +/* + * 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 "sdk_helper.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace std; + +int32_t DataSyncer::StartSync() +{ + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + uv_loop_close(uv_default_loop()); + + return 0; +} + +int32_t DataSyncer::StopSync() +{ + return 0; +} + +int32_t DataSyncer::Pull(shared_ptr provider) +{ + auto callback = bind(&DataSyncer::OnFetchRecords, this, placeholders::_1, provider); + /* fetch condition */ + int32_t ret = SdkHelper::FetchRecords(callback); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::Push(shared_ptr provider) +{ + /* create */ + auto records = provider->GetCreatedRecords(); + auto createCallback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); + int32_t ret = SdkHelper::CreateRecords(records, createCallback); + if (ret != 0) { + + } + + /* delete */ + records = provider->GetDeletedRecords(); + auto deleteCallback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + ret = SdkHelper::DeleteRecords(records, deleteCallback); + if (ret != 0) { + + } + + /* modify */ + records = provider->GetDeletedRecords(); + auto modifyCallback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + ret = SdkHelper::ModifyRecords(records, modifyCallback); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::OnFetchRecords(vector &records, shared_ptr provider) +{ + /* no more records */ + if (records.size() == 0) { + if (downloadCount_-- == 0) { + Schedule(); + } + } + + int32_t ret = provider->OnFetchRecords(records); + if (ret != 0) { + + } + + /* pull more */ + ret = Pull(provider); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::OnCreateResults(vector &records, shared_ptr provider) +{ + int32_t ret = provider->OnCreateResults(records); + if (ret != 0) { + /* error */ + } + + /* push more */ + records = provider->GetCreatedRecords(); + if (records.size() == 0) { + if (uploadCount_-- == 0) { + Schedule(); + } + } else { + auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); + ret = SdkHelper::CreateRecords(records, callback); + if (ret != 0) { + + } + } + + return 0; +} + +int32_t DataSyncer::OnDeleteResults(vector &records, shared_ptr provider) +{ + int32_t ret = provider->OnDeleteResults(records); + if (ret != 0) { + /* error */ + } + + /* push more */ + records = provider->GetDeletedRecords(); + if (records.size() == 0) { + if (uploadCount_-- == 0) { + Schedule(); + } + } else { + auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + ret = SdkHelper::DeleteRecords(records, callback); + if (ret != 0) { + + } + } + + return 0; +} + +int32_t DataSyncer::OnModifyResults(vector &records, shared_ptr provider) +{ + int32_t ret = provider->OnModifyResults(records); + if (ret != 0) { + /* error */ + } + + /* push more */ + records = provider->GetModifiedRecords(); + if (records.size() == 0) { + if (uploadCount_-- == 0) { + Schedule(); + } + } else { + auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + ret = SdkHelper::ModifyRecords(records, callback); + if (ret != 0) { + + } + } + + return 0; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/sdk_helper.cpp b/services/cloud_sync/src/sdk_helper.cpp new file mode 100644 index 000000000..214105e15 --- /dev/null +++ b/services/cloud_sync/src/sdk_helper.cpp @@ -0,0 +1,48 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace std; + +int32_t SdkHelper::FetchRecords(function &)> callback) +{ + return 0; +} + +int32_t SdkHelper::CreateRecords(vector &records, function &)> callback) +{ + + return 0; +} + +int32_t SdkHelper::DeleteRecords(vector &records, function &)> callback) +{ + + return 0; +} + +int32_t SdkHelper::ModifyRecords(vector &records, function &)> callback) +{ + + return 0; +} + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file -- Gitee From cfdfcceae7507abf4cd6f3869a26ccd90ebc2bd7 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Mon, 16 Jan 2023 12:59:01 +0000 Subject: [PATCH 2/7] add gallery data syncer and related data providers Signed-off-by: jiahaoluo Change-Id: Id3848c2ca1aa55f82965c60d87caf63ad5156aa4 --- services/cloud_sync/BUILD.gn | 4 +- services/cloud_sync/gallery/BUILD.gn | 37 ++++++++++++++ .../gallery/include/album_data_provider.h | 41 +++++++++++++++ .../gallery/include/file_data_provider.h | 41 +++++++++++++++ .../gallery/include/gallery_data_syncer.h | 18 +++++++ .../gallery/src/album_data_provider.cpp | 25 ++++++++++ .../gallery/src/file_data_provider.cpp | 25 ++++++++++ .../gallery/src/gallery_data_syncer.cpp | 50 +++++++++++++++++++ services/cloud_sync/src/data_syncer.cpp | 4 +- 9 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 services/cloud_sync/gallery/include/album_data_provider.h create mode 100644 services/cloud_sync/gallery/include/file_data_provider.h create mode 100644 services/cloud_sync/gallery/src/album_data_provider.cpp create mode 100644 services/cloud_sync/gallery/src/file_data_provider.cpp diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn index 251dd16c2..d38985f07 100644 --- a/services/cloud_sync/BUILD.gn +++ b/services/cloud_sync/BUILD.gn @@ -22,7 +22,9 @@ ohos_shared_library("cloud_sync") { sources = [ "src/data_provider.cpp", "src/data_syncer.cpp", - "gallery/src/gallery_data_syncer.cpp" + "gallery/src/gallery_data_syncer.cpp", + "gallery/src/file_data_provider.cpp", + "gallery/src/album_data_provider.cpp", ] deps = [ diff --git a/services/cloud_sync/gallery/BUILD.gn b/services/cloud_sync/gallery/BUILD.gn index e69de29bb..f9d102858 100644 --- a/services/cloud_sync/gallery/BUILD.gn +++ b/services/cloud_sync/gallery/BUILD.gn @@ -0,0 +1,37 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +ohos_shared_library("cloud_gallery") { + include_dirs = [ + ] + + sources = [ + ] + + deps = [ + "//third_party/libuv:uv" + ] + + external_deps = [ + "ability_base:want" + ] + + configs = [ "${utils_path}:compiler_configs" ] + + defines = [ "LOG_TAG=\"distributedfile_cloud\"" ] + + part_name = "dfs_service" + subsystem_name = "filemanagement" +} diff --git a/services/cloud_sync/gallery/include/album_data_provider.h b/services/cloud_sync/gallery/include/album_data_provider.h new file mode 100644 index 000000000..4dd421968 --- /dev/null +++ b/services/cloud_sync/gallery/include/album_data_provider.h @@ -0,0 +1,41 @@ +/* + * 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 Storage { +namespace DistributedFile { +class AlbumDataProvider : public DataProvider { +public: + /* download */ + virtual int32_t OnFetchRecords(std::vector &records) override; + + /* upload */ + virtual std::vector GetCreatedRecords() override; + virtual std::vector GetDeletedRecords() override; + virtual std::vector GetModifiedRecords() override; + + virtual int32_t OnCreateResults(std::vector &records) override; + virtual int32_t OnDeleteResults(std::vector &records) override; + virtual int32_t OnModifyResults(std::vector &records) override; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // ALBUM_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/include/file_data_provider.h b/services/cloud_sync/gallery/include/file_data_provider.h new file mode 100644 index 000000000..1361cd232 --- /dev/null +++ b/services/cloud_sync/gallery/include/file_data_provider.h @@ -0,0 +1,41 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class FileDataProvider : public DataProvider { +public: + /* download */ + virtual int32_t OnFetchRecords(std::vector &records) override; + + /* upload */ + virtual std::vector GetCreatedRecords() override; + virtual std::vector GetDeletedRecords() override; + virtual std::vector GetModifiedRecords() override; + + virtual int32_t OnCreateResults(std::vector &records) override; + virtual int32_t OnDeleteResults(std::vector &records) override; + virtual int32_t OnModifyResults(std::vector &records) override; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // FILE_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/include/gallery_data_syncer.h b/services/cloud_sync/gallery/include/gallery_data_syncer.h index 098e03109..af1896b02 100644 --- a/services/cloud_sync/gallery/include/gallery_data_syncer.h +++ b/services/cloud_sync/gallery/include/gallery_data_syncer.h @@ -23,7 +23,25 @@ namespace Storage { namespace DistributedFile { class GalleryDataSyncer : public DataSyncer { public: + virtual void Schedule() override; +private: + enum { + BEGIN, + DOWNLOADALBUM, + DOWNLOADFILE, + UPLOADALBUM, + UPLOADFILE, + END + }; + + void DownloadAlbum(); + void DownloadFile(); + void UploadAlbum(); + void UploadFile(); + void Complete(); + + int32_t stage_ = BEGIN; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/cloud_sync/gallery/src/album_data_provider.cpp b/services/cloud_sync/gallery/src/album_data_provider.cpp new file mode 100644 index 000000000..518c98a1a --- /dev/null +++ b/services/cloud_sync/gallery/src/album_data_provider.cpp @@ -0,0 +1,25 @@ +/* + * 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 Storage { +namespace DistributedFile { +using namespace std; + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/file_data_provider.cpp b/services/cloud_sync/gallery/src/file_data_provider.cpp new file mode 100644 index 000000000..c580db643 --- /dev/null +++ b/services/cloud_sync/gallery/src/file_data_provider.cpp @@ -0,0 +1,25 @@ +/* + * 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 Storage { +namespace DistributedFile { +using namespace std; + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp index df2657192..73397bb83 100644 --- a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp +++ b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp @@ -20,6 +20,56 @@ namespace Storage { namespace DistributedFile { using namespace std; +void GalleryDataSyncer::Schedule() +{ + stage_++; + + switch (stage_) { + case DOWNLOADALBUM: + DownloadAlbum(); + break; + case DOWNLOADFILE: + DownloadFile(); + break; + case UPLOADALBUM: + UploadAlbum(); + break; + case UPLOADFILE: + UploadFile(); + break; + case END: + Complete(); + break; + default: + break; + } +} + +void GalleryDataSyncer::DownloadAlbum() +{ + +} + +void GalleryDataSyncer::DownloadFile() +{ + +} + +void GalleryDataSyncer::UploadAlbum() +{ + +} + +void GalleryDataSyncer::UploadFile() +{ + +} + +void GalleryDataSyncer::Complete() +{ + +} + } // namespace DistributedFile } // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp index b1314e1c7..b22735bf6 100644 --- a/services/cloud_sync/src/data_syncer.cpp +++ b/services/cloud_sync/src/data_syncer.cpp @@ -26,9 +26,9 @@ using namespace std; int32_t DataSyncer::StartSync() { - uv_run(uv_default_loop(), UV_RUN_DEFAULT); + // uv_run(uv_default_loop(), UV_RUN_DEFAULT); - uv_loop_close(uv_default_loop()); + Schedule(); return 0; } -- Gitee From ea86a0bd61d444d9a5218cfcd51140957c3714a9 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Wed, 18 Jan 2023 06:21:24 +0000 Subject: [PATCH 3/7] add file manager Signed-off-by: jiahaoluo Change-Id: I2475d44bc191e1febff12f129fd0e948f29779c9 --- services/cloud_sync/BUILD.gn | 1 + services/cloud_sync/include/file_manager.h | 66 ++++++++++++++++++++++ services/cloud_sync/include/sdk_helper.h | 2 - services/cloud_sync/src/data_syncer.cpp | 48 ++++++++++++++++ services/cloud_sync/src/file_manager.cpp | 25 ++++++++ services/cloud_sync/src/sdk_helper.cpp | 1 - 6 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 services/cloud_sync/include/file_manager.h create mode 100644 services/cloud_sync/src/file_manager.cpp diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn index d38985f07..0dbabc41e 100644 --- a/services/cloud_sync/BUILD.gn +++ b/services/cloud_sync/BUILD.gn @@ -22,6 +22,7 @@ ohos_shared_library("cloud_sync") { sources = [ "src/data_provider.cpp", "src/data_syncer.cpp", + "src/file_manager.cpp", "gallery/src/gallery_data_syncer.cpp", "gallery/src/file_data_provider.cpp", "gallery/src/album_data_provider.cpp", diff --git a/services/cloud_sync/include/file_manager.h b/services/cloud_sync/include/file_manager.h new file mode 100644 index 000000000..baa976468 --- /dev/null +++ b/services/cloud_sync/include/file_manager.h @@ -0,0 +1,66 @@ +/* + * 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 "sdk_helper.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class File { +public: + virtual int32_t Open(); + +private: + enum { + DOWNLOAD, + UPLOAD + }; + + std::string path_; + int32_t type_; + int32_t percent_; +}; + +class FileManager { +public: + /* download */ + int32_t DownloadFile(); + int32_t CancelDownloadFile(); + int32_t OnDownloadProgress(); + int32_t OnDownloadResult(); + + /* upload */ + int32_t OnOpenFile(); + int32_t CancelUploadFile(); + int32_t OnUploadProgress(); + int32_t OnUploadResult(); + + /* query */ + int32_t GetDownloadProgress(); + int32_t GetUploadProgress(); + +private: + std::unordered_map taskMap_; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // FILE_MANAGER_H \ No newline at end of file diff --git a/services/cloud_sync/include/sdk_helper.h b/services/cloud_sync/include/sdk_helper.h index 97be36152..5b73e42a7 100644 --- a/services/cloud_sync/include/sdk_helper.h +++ b/services/cloud_sync/include/sdk_helper.h @@ -36,8 +36,6 @@ public: std::function &)> callback); static int32_t ModifyRecords(std::vector &records, std::function &)> callback); - - int32_t tmp_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp index b22735bf6..04d123878 100644 --- a/services/cloud_sync/src/data_syncer.cpp +++ b/services/cloud_sync/src/data_syncer.cpp @@ -173,6 +173,54 @@ int32_t DataSyncer::OnModifyResults(vector &records, shared_pt return 0; } + +int32_t DataSyncer::RetryCreateRecords(std::vector &records) +{ + uploadCount_++; + + /* get id list from records */ + vector idList; + + auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); + ret = SdkHelper::CreateRecords(records, callback); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::RetryDeleteRecords(std::vector &records) +{ + uploadCount_++; + + /* get id list from records */ + vector idList; + + auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + ret = SdkHelper::DeleteRecords(records, callback); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::RetryModifyRecords(std::vector &records) +{ + uploadCount_++; + + /* get id list from records */ + vector idList; + + auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + ret = SdkHelper::ModifyRecords(records, callback); + if (ret != 0) { + + } + + return 0; +} } // namespace DistributedFile } // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/file_manager.cpp b/services/cloud_sync/src/file_manager.cpp new file mode 100644 index 000000000..cbcf72ed8 --- /dev/null +++ b/services/cloud_sync/src/file_manager.cpp @@ -0,0 +1,25 @@ +/* + * 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" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace std; + +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/sdk_helper.cpp b/services/cloud_sync/src/sdk_helper.cpp index 214105e15..e6e7167a0 100644 --- a/services/cloud_sync/src/sdk_helper.cpp +++ b/services/cloud_sync/src/sdk_helper.cpp @@ -42,7 +42,6 @@ int32_t SdkHelper::ModifyRecords(vector &records, function Date: Tue, 17 Jan 2023 07:28:40 +0000 Subject: [PATCH 4/7] add error handle Signed-off-by: jiahaoluo Change-Id: Idfc9e71f387048f6d5b945a328b15bbc198b8dd7 --- services/cloud_sync/BUILD.gn | 1 + .../gallery/include/album_data_provider.h | 8 +- .../gallery/include/file_data_provider.h | 8 +- .../gallery/include/gallery_data_syncer.h | 10 +- .../gallery/src/album_data_provider.cpp | 8 +- .../gallery/src/file_data_provider.cpp | 8 +- .../gallery/src/gallery_data_syncer.cpp | 12 +- services/cloud_sync/include/data_provider.h | 8 +- services/cloud_sync/include/data_syncer.h | 32 ++-- services/cloud_sync/include/drivekit.h | 8 +- services/cloud_sync/include/file_manager.h | 8 +- services/cloud_sync/include/sdk_helper.h | 8 +- services/cloud_sync/include/task.h | 112 ++++++++++++ services/cloud_sync/src/data_provider.cpp | 8 +- services/cloud_sync/src/data_syncer.cpp | 165 ++++++++++-------- services/cloud_sync/src/file_manager.cpp | 8 +- services/cloud_sync/src/sdk_helper.cpp | 8 +- services/cloud_sync/src/task.cpp | 60 +++++++ 18 files changed, 338 insertions(+), 142 deletions(-) create mode 100644 services/cloud_sync/include/task.h create mode 100644 services/cloud_sync/src/task.cpp diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn index 0dbabc41e..fceece71c 100644 --- a/services/cloud_sync/BUILD.gn +++ b/services/cloud_sync/BUILD.gn @@ -23,6 +23,7 @@ ohos_shared_library("cloud_sync") { "src/data_provider.cpp", "src/data_syncer.cpp", "src/file_manager.cpp", + "src/task.cpp", "gallery/src/gallery_data_syncer.cpp", "gallery/src/file_data_provider.cpp", "gallery/src/album_data_provider.cpp", diff --git a/services/cloud_sync/gallery/include/album_data_provider.h b/services/cloud_sync/gallery/include/album_data_provider.h index 4dd421968..4e795edbd 100644 --- a/services/cloud_sync/gallery/include/album_data_provider.h +++ b/services/cloud_sync/gallery/include/album_data_provider.h @@ -19,8 +19,8 @@ #include "data_provider.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class AlbumDataProvider : public DataProvider { public: /* download */ @@ -35,7 +35,7 @@ public: virtual int32_t OnDeleteResults(std::vector &records) override; virtual int32_t OnModifyResults(std::vector &records) override; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // ALBUM_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/include/file_data_provider.h b/services/cloud_sync/gallery/include/file_data_provider.h index 1361cd232..3b9ba4bb0 100644 --- a/services/cloud_sync/gallery/include/file_data_provider.h +++ b/services/cloud_sync/gallery/include/file_data_provider.h @@ -19,8 +19,8 @@ #include "data_provider.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class FileDataProvider : public DataProvider { public: /* download */ @@ -35,7 +35,7 @@ public: virtual int32_t OnDeleteResults(std::vector &records) override; virtual int32_t OnModifyResults(std::vector &records) override; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // FILE_DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/include/gallery_data_syncer.h b/services/cloud_sync/gallery/include/gallery_data_syncer.h index af1896b02..ff1308e1f 100644 --- a/services/cloud_sync/gallery/include/gallery_data_syncer.h +++ b/services/cloud_sync/gallery/include/gallery_data_syncer.h @@ -19,11 +19,11 @@ #include "data_syncer.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class GalleryDataSyncer : public DataSyncer { public: - virtual void Schedule() override; + virtual int32_t Schedule() override; private: enum { @@ -43,7 +43,7 @@ private: int32_t stage_ = BEGIN; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // GALLERY_DATA_SYNCER_H \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/album_data_provider.cpp b/services/cloud_sync/gallery/src/album_data_provider.cpp index 518c98a1a..12cbfd1b3 100644 --- a/services/cloud_sync/gallery/src/album_data_provider.cpp +++ b/services/cloud_sync/gallery/src/album_data_provider.cpp @@ -16,10 +16,10 @@ #include "album_data_provider.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/file_data_provider.cpp b/services/cloud_sync/gallery/src/file_data_provider.cpp index c580db643..82d340967 100644 --- a/services/cloud_sync/gallery/src/file_data_provider.cpp +++ b/services/cloud_sync/gallery/src/file_data_provider.cpp @@ -16,10 +16,10 @@ #include "file_data_provider.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp index 73397bb83..19a04fbb0 100644 --- a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp +++ b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp @@ -16,11 +16,11 @@ #include "gallery_data_syncer.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; -void GalleryDataSyncer::Schedule() +int32_t GalleryDataSyncer::Schedule() { stage_++; @@ -43,6 +43,8 @@ void GalleryDataSyncer::Schedule() default: break; } + + return 0; } void GalleryDataSyncer::DownloadAlbum() @@ -70,6 +72,6 @@ void GalleryDataSyncer::Complete() } -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/include/data_provider.h b/services/cloud_sync/include/data_provider.h index cdde138b9..9fa4614ac 100644 --- a/services/cloud_sync/include/data_provider.h +++ b/services/cloud_sync/include/data_provider.h @@ -22,8 +22,8 @@ #include "drivekit.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class DataProvider { public: /* download */ @@ -38,7 +38,7 @@ public: virtual int32_t OnDeleteResults(std::vector &records) = 0; virtual int32_t OnModifyResults(std::vector &records) = 0; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // DATA_PROVIDER_H \ No newline at end of file diff --git a/services/cloud_sync/include/data_syncer.h b/services/cloud_sync/include/data_syncer.h index b13cf4204..93ce17de4 100644 --- a/services/cloud_sync/include/data_syncer.h +++ b/services/cloud_sync/include/data_syncer.h @@ -18,16 +18,16 @@ #include #include +#include #include -#include - #include "drivekit.h" #include "data_provider.h" +#include "task.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class DataSyncer { public: int32_t StartSync(); @@ -36,29 +36,39 @@ public: protected: /* download */ int32_t Pull(std::shared_ptr provider); - int32_t Pull(std::vector &idList, std::shared_ptr provider); /* upload */ int32_t Push(std::shared_ptr provider); /* download callback */ - int32_t OnFetchRecords(std::vector &records, std::shared_ptr provider); + // int32_t OnFetchRecords(std::vector &records, std::shared_ptr provider); + + int32_t FetchRecords(Task &task); + int32_t OnFetchRecords(Task &task, std::vector &records); /* upload callback */ int32_t OnCreateResults(std::vector &records, std::shared_ptr provider); int32_t OnDeleteResults(std::vector &records, std::shared_ptr provider); int32_t OnModifyResults(std::vector &records, std::shared_ptr provider); + int32_t OnCreateResults(std::vector &records, std::shared_ptr task); + int32_t OnDeleteResults(std::vector &records, std::shared_ptr task); + int32_t OnModifyResults(std::vector &records, std::shared_ptr task); + /* schedule */ - virtual void Schedule() = 0; + virtual bool IsSchedulable(); + virtual int32_t Schedule() = 0; + + /* task */ + int32_t CommitTask(Task &t); + int32_t CompleteTask(Task &t); /* retry */ private: - int32_t downloadCount_ = 1; - int32_t uploadCount_ = 3; + TaskManager taskManager_; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // DATA_SYNCER_H \ No newline at end of file diff --git a/services/cloud_sync/include/drivekit.h b/services/cloud_sync/include/drivekit.h index c14c2c2e4..260fd8140 100644 --- a/services/cloud_sync/include/drivekit.h +++ b/services/cloud_sync/include/drivekit.h @@ -17,15 +17,15 @@ #define DRIVEKIT_H namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class DriveKit { public: class Record { }; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // DRIVEKIT_H \ No newline at end of file diff --git a/services/cloud_sync/include/file_manager.h b/services/cloud_sync/include/file_manager.h index baa976468..ac6c7aada 100644 --- a/services/cloud_sync/include/file_manager.h +++ b/services/cloud_sync/include/file_manager.h @@ -22,8 +22,8 @@ #include "sdk_helper.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class File { public: virtual int32_t Open(); @@ -60,7 +60,7 @@ public: private: std::unordered_map taskMap_; }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // FILE_MANAGER_H \ No newline at end of file diff --git a/services/cloud_sync/include/sdk_helper.h b/services/cloud_sync/include/sdk_helper.h index 5b73e42a7..5d366f4be 100644 --- a/services/cloud_sync/include/sdk_helper.h +++ b/services/cloud_sync/include/sdk_helper.h @@ -22,8 +22,8 @@ #include "drivekit.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { class SdkHelper { public: /* download */ @@ -37,7 +37,7 @@ public: static int32_t ModifyRecords(std::vector &records, std::function &)> callback); }; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS #endif // SDK_HELPER_H \ No newline at end of file diff --git a/services/cloud_sync/include/task.h b/services/cloud_sync/include/task.h new file mode 100644 index 000000000..aedf66168 --- /dev/null +++ b/services/cloud_sync/include/task.h @@ -0,0 +1,112 @@ +/* + * 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 "drivekit.h" +#include "data_provider.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { + + +class Task { +public: + using TaskAction = std::function; + + Task(); + ~Task(); + + virtual int32_t Run() + { + return action_(*this); + } + + virtual int32_t GetId() final + { + return id_; + } + + virtual std::shared_ptr GetProvider() + { + return provider_; + } + +protected: + std::function action_; + std::shared_ptr provider_; + +private: + int32_t id_; +}; + +class ModifyRecordTask : public Task { +public: + ModifyRecordTask(std::shared_ptr &provider, TaskAction f) + { + action_ = f; + provider_ = provider; + } + +private: +}; + +class FetchRecordTask : public Task { +public: + FetchRecordTask(std::shared_ptr provider, TaskAction f) { + action_ = f; + provider_ = provider; + } + +private: +}; + +class RetryFetchRecordTask : public Task { +public: + RetryFetchRecordTask(std::shared_ptr provider, TaskAction f) + { + action_ = f; + provider_ = provider; + } + +private: +}; + +class FileTask : public Task { +public: + +private: +}; + +class TaskManager { +public: + int32_t CommitTask(Task &t); + int32_t CompleteTask(Task &t); + + bool IsIdle(); + +private: + std::list taskList_; +}; +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS +#endif // TASK_H \ No newline at end of file diff --git a/services/cloud_sync/src/data_provider.cpp b/services/cloud_sync/src/data_provider.cpp index 929f2f3eb..987989679 100644 --- a/services/cloud_sync/src/data_provider.cpp +++ b/services/cloud_sync/src/data_provider.cpp @@ -16,10 +16,10 @@ #include "data_provider.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp index 04d123878..666972fae 100644 --- a/services/cloud_sync/src/data_syncer.cpp +++ b/services/cloud_sync/src/data_syncer.cpp @@ -20,9 +20,10 @@ #include "sdk_helper.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; +using namespace placeholders; int32_t DataSyncer::StartSync() { @@ -40,9 +41,9 @@ int32_t DataSyncer::StopSync() int32_t DataSyncer::Pull(shared_ptr provider) { - auto callback = bind(&DataSyncer::OnFetchRecords, this, placeholders::_1, provider); - /* fetch condition */ - int32_t ret = SdkHelper::FetchRecords(callback); + auto cb = bind(&DataSyncer::FetchRecords, this, _1); + FetchRecordTask task(provider, cb); + int32_t ret = CommitTask(task); if (ret != 0) { } @@ -50,28 +51,15 @@ int32_t DataSyncer::Pull(shared_ptr provider) return 0; } -int32_t DataSyncer::Push(shared_ptr provider) +int32_t DataSyncer::FetchRecords(Task &task) { - /* create */ - auto records = provider->GetCreatedRecords(); - auto createCallback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); - int32_t ret = SdkHelper::CreateRecords(records, createCallback); - if (ret != 0) { - - } - - /* delete */ - records = provider->GetDeletedRecords(); - auto deleteCallback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); - ret = SdkHelper::DeleteRecords(records, deleteCallback); - if (ret != 0) { + FetchRecordTask &t = static_cast(task); + auto provider = t.GetProvider(); - } + /* get fetch parameters here */ - /* modify */ - records = provider->GetDeletedRecords(); - auto modifyCallback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); - ret = SdkHelper::ModifyRecords(records, modifyCallback); + auto cb = bind(&DataSyncer::OnFetchRecords, this, t, _1); + int32_t ret = SdkHelper::FetchRecords(cb); if (ret != 0) { } @@ -79,15 +67,24 @@ int32_t DataSyncer::Push(shared_ptr provider) return 0; } -int32_t DataSyncer::OnFetchRecords(vector &records, shared_ptr provider) +int32_t DataSyncer::OnFetchRecords(Task &task, vector &records) { + FetchRecordTask &t = static_cast(task); + /* no more records */ if (records.size() == 0) { - if (downloadCount_-- == 0) { + CompleteTask(t); + + if (IsSchedulable()) { Schedule(); } } + auto provider = t.GetProvider(); + if (provider == nullptr) { + + } + int32_t ret = provider->OnFetchRecords(records); if (ret != 0) { @@ -102,6 +99,37 @@ int32_t DataSyncer::OnFetchRecords(vector &records, shared_ptr return 0; } +int32_t DataSyncer::Push(shared_ptr provider) +{ + /* create */ + auto records = provider->GetCreatedRecords(); + // ModifyRecordTask createTask(provider); + // CommitTask(createTask); + // auto createCallback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, createTask); + // int32_t ret = SdkHelper::CreateRecords(records, createCallback); + // if (ret != 0) { + + // } + + /* delete */ + records = provider->GetDeletedRecords(); + // auto deleteCallback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + // ret = SdkHelper::DeleteRecords(records, deleteCallback); + // if (ret != 0) { + + // } + + /* modify */ + records = provider->GetDeletedRecords(); + // auto modifyCallback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + // ret = SdkHelper::ModifyRecords(records, modifyCallback); + // if (ret != 0) { + + // } + + return 0; +} + int32_t DataSyncer::OnCreateResults(vector &records, shared_ptr provider) { int32_t ret = provider->OnCreateResults(records); @@ -112,15 +140,15 @@ int32_t DataSyncer::OnCreateResults(vector &records, shared_pt /* push more */ records = provider->GetCreatedRecords(); if (records.size() == 0) { - if (uploadCount_-- == 0) { + if (IsSchedulable()) { Schedule(); } } else { - auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); - ret = SdkHelper::CreateRecords(records, callback); - if (ret != 0) { + // auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); + // ret = SdkHelper::CreateRecords(records, callback); + // if (ret != 0) { - } + // } } return 0; @@ -136,15 +164,15 @@ int32_t DataSyncer::OnDeleteResults(vector &records, shared_pt /* push more */ records = provider->GetDeletedRecords(); if (records.size() == 0) { - if (uploadCount_-- == 0) { + if (IsSchedulable()) { Schedule(); } } else { - auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); - ret = SdkHelper::DeleteRecords(records, callback); - if (ret != 0) { + // auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + // ret = SdkHelper::DeleteRecords(records, callback); + // if (ret != 0) { - } + // } } return 0; @@ -160,67 +188,50 @@ int32_t DataSyncer::OnModifyResults(vector &records, shared_pt /* push more */ records = provider->GetModifiedRecords(); if (records.size() == 0) { - if (uploadCount_-- == 0) { + if (IsSchedulable()) { Schedule(); } } else { - auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); - ret = SdkHelper::ModifyRecords(records, callback); - if (ret != 0) { + // auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + // ret = SdkHelper::ModifyRecords(records, callback); + // if (ret != 0) { - } + // } } return 0; } -int32_t DataSyncer::RetryCreateRecords(std::vector &records) +int32_t DataSyncer::OnCreateResults(std::vector &records, std::shared_ptr task) { - uploadCount_++; - - /* get id list from records */ - vector idList; - - auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); - ret = SdkHelper::CreateRecords(records, callback); - if (ret != 0) { - - } - return 0; } -int32_t DataSyncer::RetryDeleteRecords(std::vector &records) +int32_t DataSyncer::OnDeleteResults(std::vector &records, std::shared_ptr task) { - uploadCount_++; - - /* get id list from records */ - vector idList; - - auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); - ret = SdkHelper::DeleteRecords(records, callback); - if (ret != 0) { - - } - return 0; } -int32_t DataSyncer::RetryModifyRecords(std::vector &records) +int32_t DataSyncer::OnModifyResults(std::vector &records, std::shared_ptr task) { - uploadCount_++; - - /* get id list from records */ - vector idList; + return 0; +} - auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); - ret = SdkHelper::ModifyRecords(records, callback); - if (ret != 0) { +int32_t DataSyncer::CommitTask(Task &t) +{ + return taskManager_.CommitTask(t); +} - } +int32_t DataSyncer::CompleteTask(Task &t) +{ + return taskManager_.CompleteTask(t); +} - return 0; +bool DataSyncer::IsSchedulable() +{ + /* all tasks have been finished at this stage, so move to the next stage */ + return taskManager_.IsIdle(); } -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/file_manager.cpp b/services/cloud_sync/src/file_manager.cpp index cbcf72ed8..9ca4fe56c 100644 --- a/services/cloud_sync/src/file_manager.cpp +++ b/services/cloud_sync/src/file_manager.cpp @@ -16,10 +16,10 @@ #include "file_manager.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; -} // namespace DistributedFile -} // namespace Storage +} // namespace CloudSync +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/services/cloud_sync/src/sdk_helper.cpp b/services/cloud_sync/src/sdk_helper.cpp index e6e7167a0..5c627720f 100644 --- a/services/cloud_sync/src/sdk_helper.cpp +++ b/services/cloud_sync/src/sdk_helper.cpp @@ -16,8 +16,8 @@ #include "sdk_helper.h" namespace OHOS { -namespace Storage { -namespace DistributedFile { +namespace FileManagement { +namespace CloudSync { using namespace std; int32_t SdkHelper::FetchRecords(function &)> callback) @@ -42,6 +42,6 @@ int32_t SdkHelper::ModifyRecords(vector &records, function + +#include "sdk_helper.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudSync { +using namespace std; + +Task::Task() +{ +} + +Task::~Task() +{ +} + +int32_t TaskManager::CommitTask(Task &t) +{ + /* lock */ + taskList_.push_back(t); + + /* schedule */ + t.Run(); + + return 0; +} + +int32_t TaskManager::CompleteTask(Task &t) +{ + /* lock */ + /* erase task from list */ + + return 0; +} + +bool TaskManager::IsIdle() +{ + return taskList_.empty(); +} +} // namespace CloudSync +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file -- Gitee From 73889348f33bdf42b34e128e4c587557fa4c8c98 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Tue, 31 Jan 2023 12:07:59 +0000 Subject: [PATCH 5/7] add rdb handle Signed-off-by: jiahaoluo Change-Id: I83415ed838d7753bd5f12d9b7a6e130124853e84 --- services/cloud_sync/BUILD.gn | 3 +- .../gallery/include/album_data_provider.h | 50 ++++++- .../gallery/include/file_data_provider.h | 53 +++++++- .../gallery/include/gallery_data_syncer.h | 26 +++- .../gallery/src/album_data_provider.cpp | 95 +++++++++++++- .../gallery/src/file_data_provider.cpp | 93 +++++++++++++ .../gallery/src/gallery_data_syncer.cpp | 55 +++++--- services/cloud_sync/include/data_provider.h | 81 +++++++++++- services/cloud_sync/include/data_syncer.h | 31 +++-- services/cloud_sync/include/drivekit.h | 32 ++++- services/cloud_sync/include/task.h | 2 +- services/cloud_sync/src/data_provider.cpp | 124 ++++++++++++++++++ services/cloud_sync/src/data_syncer.cpp | 70 ++++++---- 13 files changed, 627 insertions(+), 88 deletions(-) diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn index fceece71c..da55ef7ce 100644 --- a/services/cloud_sync/BUILD.gn +++ b/services/cloud_sync/BUILD.gn @@ -34,7 +34,8 @@ ohos_shared_library("cloud_sync") { ] external_deps = [ - "ability_base:want" + "ability_base:want", + "relational_store:native_rdb" ] configs = [ "${utils_path}:compiler_configs" ] diff --git a/services/cloud_sync/gallery/include/album_data_provider.h b/services/cloud_sync/gallery/include/album_data_provider.h index 4e795edbd..4509e4546 100644 --- a/services/cloud_sync/gallery/include/album_data_provider.h +++ b/services/cloud_sync/gallery/include/album_data_provider.h @@ -21,19 +21,55 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { -class AlbumDataProvider : public DataProvider { +class AlbumDataProvider : public RdbProvider { public: + AlbumDataProvider(std::shared_ptr rdb); + virtual ~AlbumDataProvider() = default; + /* download */ virtual int32_t OnFetchRecords(std::vector &records) override; + virtual int32_t GetFetchCondition() override; /* upload */ - virtual std::vector GetCreatedRecords() override; - virtual std::vector GetDeletedRecords() override; - virtual std::vector GetModifiedRecords() override; + 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(std::vector &records) override; + virtual int32_t OnDeleteRecords(std::vector &records) override; + virtual int32_t OnModifyRecords(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 + }; - virtual int32_t OnCreateResults(std::vector &records) override; - virtual int32_t OnDeleteResults(std::vector &records) override; - virtual int32_t OnModifyResults(std::vector &records) override; + /* fetch */ }; } // namespace CloudSync } // namespace FileManagement diff --git a/services/cloud_sync/gallery/include/file_data_provider.h b/services/cloud_sync/gallery/include/file_data_provider.h index 3b9ba4bb0..200fe3873 100644 --- a/services/cloud_sync/gallery/include/file_data_provider.h +++ b/services/cloud_sync/gallery/include/file_data_provider.h @@ -18,22 +18,61 @@ #include "data_provider.h" +#include + namespace OHOS { namespace FileManagement { namespace CloudSync { -class FileDataProvider : public DataProvider { +class FileDataProvider : public RdbProvider { public: + FileDataProvider(std::shared_ptr rdb); + virtual ~FileDataProvider() = default; + /* download */ + virtual int32_t GetFetchCondition() override; virtual int32_t OnFetchRecords(std::vector &records) override; /* upload */ - virtual std::vector GetCreatedRecords() override; - virtual std::vector GetDeletedRecords() override; - virtual std::vector GetModifiedRecords() override; + virtual int32_t GetCreatedRecords(std::vector &records) override; + virtual int32_t GetDeletedRecords(std::vector &records) override; + virtual int32_t GetModifiedRecords(std::vector &records) override; + + /* callback */ + virtual int32_t OnCreateRecords(std::vector &records) override; + virtual int32_t OnDeleteRecords(std::vector &records) override; + virtual int32_t OnModifyRecords(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 + }; - virtual int32_t OnCreateResults(std::vector &records) override; - virtual int32_t OnDeleteResults(std::vector &records) override; - virtual int32_t OnModifyResults(std::vector &records) override; + /* fetch */ }; } // namespace CloudSync } // namespace FileManagement diff --git a/services/cloud_sync/gallery/include/gallery_data_syncer.h b/services/cloud_sync/gallery/include/gallery_data_syncer.h index ff1308e1f..f92e6644b 100644 --- a/services/cloud_sync/gallery/include/gallery_data_syncer.h +++ b/services/cloud_sync/gallery/include/gallery_data_syncer.h @@ -18,11 +18,15 @@ #include "data_syncer.h" +#include "file_data_provider.h" + namespace OHOS { namespace FileManagement { namespace CloudSync { class GalleryDataSyncer : public DataSyncer { public: + GalleryDataSyncer(); + virtual int32_t Schedule() override; private: @@ -35,13 +39,25 @@ private: END }; - void DownloadAlbum(); - void DownloadFile(); - void UploadAlbum(); - void UploadFile(); - void Complete(); + int32_t DownloadAlbum(); + int32_t DownloadFile(); + int32_t UploadAlbum(); + int32_t UploadFile(); + int32_t Complete(); 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 name_ = "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 diff --git a/services/cloud_sync/gallery/src/album_data_provider.cpp b/services/cloud_sync/gallery/src/album_data_provider.cpp index 12cbfd1b3..a4ffe677d 100644 --- a/services/cloud_sync/gallery/src/album_data_provider.cpp +++ b/services/cloud_sync/gallery/src/album_data_provider.cpp @@ -19,7 +19,100 @@ 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(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(std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::OnDeleteRecords(std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t AlbumDataProvider::OnModifyRecords(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 +} // namespace OHOS diff --git a/services/cloud_sync/gallery/src/file_data_provider.cpp b/services/cloud_sync/gallery/src/file_data_provider.cpp index 82d340967..a71ef80e1 100644 --- a/services/cloud_sync/gallery/src/file_data_provider.cpp +++ b/services/cloud_sync/gallery/src/file_data_provider.cpp @@ -19,7 +19,100 @@ 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::GetFetchCondition() +{ + return 0; +} + +int32_t FileDataProvider::OnFetchRecords(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(std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::OnDeleteRecords(std::vector &records) +{ + vector valuesBuckets; + int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); + if (ret != 0) { + + } + + return 0; +} + +int32_t FileDataProvider::OnModifyRecords(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/cloud_sync/gallery/src/gallery_data_syncer.cpp b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp index 19a04fbb0..506e7ba1b 100644 --- a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp +++ b/services/cloud_sync/gallery/src/gallery_data_syncer.cpp @@ -20,25 +20,40 @@ namespace FileManagement { namespace CloudSync { using namespace std; +GalleryDataSyncer::GalleryDataSyncer() +{ + NativeRdb::RdbStoreConfig config(path_); + config.SetName(name_); + config.SetBundleName(bundleName_); + config.SetArea(area_); + config.SetReadConSize(connectSize_); + rdb_ = InitRdb(config); + if (rdb_ == nullptr) { + + } + + fileProvider_ = make_shared(rdb_); +} + int32_t GalleryDataSyncer::Schedule() { stage_++; - switch (stage_) { - case DOWNLOADALBUM: - DownloadAlbum(); + case DOWNLOADALBUM: { + (void)DownloadAlbum(); break; + } case DOWNLOADFILE: - DownloadFile(); + (void)DownloadFile(); break; case UPLOADALBUM: - UploadAlbum(); + (void)UploadAlbum(); break; case UPLOADFILE: - UploadFile(); + (void)UploadFile(); break; case END: - Complete(); + (void)Complete(); break; default: break; @@ -47,29 +62,39 @@ int32_t GalleryDataSyncer::Schedule() return 0; } -void GalleryDataSyncer::DownloadAlbum() +int32_t GalleryDataSyncer::DownloadAlbum() { - + return 0; } -void GalleryDataSyncer::DownloadFile() +int32_t GalleryDataSyncer::DownloadFile() { + int ret = Pull(fileProvider_); + if (ret != 0) { + } + + return 0; } -void GalleryDataSyncer::UploadAlbum() +int32_t GalleryDataSyncer::UploadAlbum() { - + return 0; } -void GalleryDataSyncer::UploadFile() +int32_t GalleryDataSyncer::UploadFile() { + int ret = Push(fileProvider_); + if (ret != 0) { + } + + return 0; } -void GalleryDataSyncer::Complete() +int32_t GalleryDataSyncer::Complete() { - + return 0; } } // namespace CloudSync diff --git a/services/cloud_sync/include/data_provider.h b/services/cloud_sync/include/data_provider.h index 9fa4614ac..435f54d09 100644 --- a/services/cloud_sync/include/data_provider.h +++ b/services/cloud_sync/include/data_provider.h @@ -19,6 +19,10 @@ #include #include +#include "rdb_helper.h" +#include "values_bucket.h" +#include "result_set.h" +#include "abs_rdb_predicates.h" #include "drivekit.h" namespace OHOS { @@ -27,16 +31,81 @@ namespace CloudSync { class DataProvider { public: /* download */ + virtual int32_t GetFetchCondition() = 0; virtual int32_t OnFetchRecords(std::vector &records) = 0; /* upload */ - virtual std::vector GetCreatedRecords() = 0; - virtual std::vector GetDeletedRecords() = 0; - virtual std::vector GetModifiedRecords() = 0; + 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 OnCreateResults(std::vector &records) = 0; - virtual int32_t OnDeleteResults(std::vector &records) = 0; - virtual int32_t OnModifyResults(std::vector &records) = 0; + virtual int32_t OnCreateRecords(std::vector &records) = 0; + virtual int32_t OnDeleteRecords(std::vector &records) = 0; + virtual int32_t OnModifyRecords(std::vector &records) = 0; + + /* cursor */ + virtual int32_t SetCursor(); + virtual int32_t GetCursor(); +}; + +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 void SetConfig(const std::string &name, const std::string &path, + const std::string &bundleName, const int32_t area, const int32_t connectSize); + + /* 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 diff --git a/services/cloud_sync/include/data_syncer.h b/services/cloud_sync/include/data_syncer.h index 93ce17de4..5c6af38f3 100644 --- a/services/cloud_sync/include/data_syncer.h +++ b/services/cloud_sync/include/data_syncer.h @@ -30,30 +30,30 @@ namespace FileManagement { namespace CloudSync { class DataSyncer { public: - int32_t StartSync(); - int32_t StopSync(); + /* sync */ + virtual int32_t StartSync(); + virtual int32_t StopSync(); protected: + /* rdb init */ + std::shared_ptr InitRdb(NativeRdb::RdbStoreConfig &config); + /* download */ int32_t Pull(std::shared_ptr provider); - /* upload */ - int32_t Push(std::shared_ptr provider); - - /* download callback */ - // int32_t OnFetchRecords(std::vector &records, std::shared_ptr provider); - int32_t FetchRecords(Task &task); int32_t OnFetchRecords(Task &task, std::vector &records); - /* upload callback */ - int32_t OnCreateResults(std::vector &records, std::shared_ptr provider); - int32_t OnDeleteResults(std::vector &records, std::shared_ptr provider); - int32_t OnModifyResults(std::vector &records, std::shared_ptr provider); + /* upload */ + int32_t Push(std::shared_ptr provider); - int32_t OnCreateResults(std::vector &records, std::shared_ptr task); - int32_t OnDeleteResults(std::vector &records, std::shared_ptr task); - int32_t OnModifyResults(std::vector &records, std::shared_ptr task); + int32_t OnCreateRecords(std::vector &records, std::shared_ptr provider); + int32_t OnDeleteRecords(std::vector &records, std::shared_ptr provider); + int32_t OnModifyRecords(std::vector &records, std::shared_ptr provider); + + int32_t OnCreateRecords(std::vector &records, std::shared_ptr task); + int32_t OnDeleteRecords(std::vector &records, std::shared_ptr task); + int32_t OnModifyRecords(std::vector &records, std::shared_ptr task); /* schedule */ virtual bool IsSchedulable(); @@ -64,7 +64,6 @@ protected: int32_t CompleteTask(Task &t); /* retry */ - private: TaskManager taskManager_; }; diff --git a/services/cloud_sync/include/drivekit.h b/services/cloud_sync/include/drivekit.h index 260fd8140..1440ac113 100644 --- a/services/cloud_sync/include/drivekit.h +++ b/services/cloud_sync/include/drivekit.h @@ -16,13 +16,43 @@ #ifndef DRIVEKIT_H #define DRIVEKIT_H +#include +#include + namespace OHOS { namespace FileManagement { namespace CloudSync { class DriveKit { public: - class Record { + 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 {} }; }; } // namespace CloudSync diff --git a/services/cloud_sync/include/task.h b/services/cloud_sync/include/task.h index aedf66168..f25edb269 100644 --- a/services/cloud_sync/include/task.h +++ b/services/cloud_sync/include/task.h @@ -33,7 +33,7 @@ public: using TaskAction = std::function; Task(); - ~Task(); + virtual ~Task(); virtual int32_t Run() { diff --git a/services/cloud_sync/src/data_provider.cpp b/services/cloud_sync/src/data_provider.cpp index 987989679..7fb250b7f 100644 --- a/services/cloud_sync/src/data_provider.cpp +++ b/services/cloud_sync/src/data_provider.cpp @@ -18,7 +18,131 @@ 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 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 diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp index 666972fae..9784f1acd 100644 --- a/services/cloud_sync/src/data_syncer.cpp +++ b/services/cloud_sync/src/data_syncer.cpp @@ -25,13 +25,21 @@ namespace CloudSync { using namespace std; using namespace placeholders; -int32_t DataSyncer::StartSync() +shared_ptr DataSyncer::InitRdb(NativeRdb::RdbStoreConfig &config) { - // uv_run(uv_default_loop(), UV_RUN_DEFAULT); + int32_t err; + RdbCallback cb; + auto rdb = NativeRdb::RdbHelper::GetRdbStore(config, 0, cb, err); + if (rdb == nullptr) { - Schedule(); + } - return 0; + return rdb; +} + +int32_t DataSyncer::StartSync() +{ + return Schedule(); } int32_t DataSyncer::StopSync() @@ -102,26 +110,29 @@ int32_t DataSyncer::OnFetchRecords(Task &task, vector &records int32_t DataSyncer::Push(shared_ptr provider) { /* create */ - auto records = provider->GetCreatedRecords(); + vector creaetedRecords; + int32_t ret = provider->GetCreatedRecords(creaetedRecords); // ModifyRecordTask createTask(provider); // CommitTask(createTask); - // auto createCallback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, createTask); + // auto createCallback = bind(&DataSyncer::OnCreateRecords, this, placeholders::_1, createTask); // int32_t ret = SdkHelper::CreateRecords(records, createCallback); // if (ret != 0) { // } /* delete */ - records = provider->GetDeletedRecords(); - // auto deleteCallback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + vector deletedRecords; + ret = provider->GetDeletedRecords(deletedRecords); + // auto deleteCallback = bind(&DataSyncer::OnDeleteRecords, this, placeholders::_1, provider); // ret = SdkHelper::DeleteRecords(records, deleteCallback); // if (ret != 0) { // } /* modify */ - records = provider->GetDeletedRecords(); - // auto modifyCallback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + vector modifiedRecords; + ret = provider->GetModifiedRecords(modifiedRecords); + // auto modifyCallback = bind(&DataSyncer::OnModifyRecords, this, placeholders::_1, provider); // ret = SdkHelper::ModifyRecords(records, modifyCallback); // if (ret != 0) { @@ -130,21 +141,22 @@ int32_t DataSyncer::Push(shared_ptr provider) return 0; } -int32_t DataSyncer::OnCreateResults(vector &records, shared_ptr provider) +int32_t DataSyncer::OnCreateRecords(vector &records, shared_ptr provider) { - int32_t ret = provider->OnCreateResults(records); + int32_t ret = provider->OnCreateRecords(records); if (ret != 0) { /* error */ } /* push more */ - records = provider->GetCreatedRecords(); - if (records.size() == 0) { + vector createdRecords; + ret = provider->GetCreatedRecords(createdRecords); + if (createdRecords.size() == 0) { if (IsSchedulable()) { Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnCreateResults, this, placeholders::_1, provider); + // auto callback = bind(&DataSyncer::OnCreateRecords, this, placeholders::_1, provider); // ret = SdkHelper::CreateRecords(records, callback); // if (ret != 0) { @@ -154,21 +166,22 @@ int32_t DataSyncer::OnCreateResults(vector &records, shared_pt return 0; } -int32_t DataSyncer::OnDeleteResults(vector &records, shared_ptr provider) +int32_t DataSyncer::OnDeleteRecords(vector &records, shared_ptr provider) { - int32_t ret = provider->OnDeleteResults(records); + int32_t ret = provider->OnDeleteRecords(records); if (ret != 0) { /* error */ } /* push more */ - records = provider->GetDeletedRecords(); - if (records.size() == 0) { + vector deletedRecords; + ret = provider->GetDeletedRecords(deletedRecords); + if (deletedRecords.size() == 0) { if (IsSchedulable()) { Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnDeleteResults, this, placeholders::_1, provider); + // auto callback = bind(&DataSyncer::OnDeleteRecords, this, placeholders::_1, provider); // ret = SdkHelper::DeleteRecords(records, callback); // if (ret != 0) { @@ -178,21 +191,22 @@ int32_t DataSyncer::OnDeleteResults(vector &records, shared_pt return 0; } -int32_t DataSyncer::OnModifyResults(vector &records, shared_ptr provider) +int32_t DataSyncer::OnModifyRecords(vector &records, shared_ptr provider) { - int32_t ret = provider->OnModifyResults(records); + int32_t ret = provider->OnModifyRecords(records); if (ret != 0) { /* error */ } /* push more */ - records = provider->GetModifiedRecords(); - if (records.size() == 0) { + vector modifiedRecords; + ret = provider->GetModifiedRecords(modifiedRecords); + if (modifiedRecords.size() == 0) { if (IsSchedulable()) { Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnModifyResults, this, placeholders::_1, provider); + // auto callback = bind(&DataSyncer::OnModifyRecords, this, placeholders::_1, provider); // ret = SdkHelper::ModifyRecords(records, callback); // if (ret != 0) { @@ -202,17 +216,17 @@ int32_t DataSyncer::OnModifyResults(vector &records, shared_pt return 0; } -int32_t DataSyncer::OnCreateResults(std::vector &records, std::shared_ptr task) +int32_t DataSyncer::OnCreateRecords(std::vector &records, std::shared_ptr task) { return 0; } -int32_t DataSyncer::OnDeleteResults(std::vector &records, std::shared_ptr task) +int32_t DataSyncer::OnDeleteRecords(std::vector &records, std::shared_ptr task) { return 0; } -int32_t DataSyncer::OnModifyResults(std::vector &records, std::shared_ptr task) +int32_t DataSyncer::OnModifyRecords(std::vector &records, std::shared_ptr task) { return 0; } -- Gitee From 9f89e99d40a08a79577c4715857d381aec4762b9 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Tue, 14 Feb 2023 04:13:21 +0000 Subject: [PATCH 6/7] add task Signed-off-by: jiahaoluo Change-Id: Ifc135a0d8c39b63b50193286d29fd340ac0893a5 --- services/cloud_sync/BUILD.gn | 3 +- services/cloud_sync/include/data_syncer.h | 27 +- services/cloud_sync/include/task.h | 43 ++- services/cloud_sync/src/data_syncer.cpp | 256 ++++++++++++------ services/cloud_sync/src/file_manager.cpp | 73 ++++- services/cloud_sync/src/sdk_helper.cpp | 17 +- services/cloud_sync/src/task.cpp | 48 +++- .../include/ipc/daemon.h | 4 +- 8 files changed, 353 insertions(+), 118 deletions(-) diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn index da55ef7ce..cab1214a4 100644 --- a/services/cloud_sync/BUILD.gn +++ b/services/cloud_sync/BUILD.gn @@ -24,17 +24,16 @@ ohos_shared_library("cloud_sync") { "src/data_syncer.cpp", "src/file_manager.cpp", "src/task.cpp", + "src/sdk_helper.cpp", "gallery/src/gallery_data_syncer.cpp", "gallery/src/file_data_provider.cpp", "gallery/src/album_data_provider.cpp", ] deps = [ - "//third_party/libuv:uv" ] external_deps = [ - "ability_base:want", "relational_store:native_rdb" ] diff --git a/services/cloud_sync/include/data_syncer.h b/services/cloud_sync/include/data_syncer.h index 5c6af38f3..c1a00ad4e 100644 --- a/services/cloud_sync/include/data_syncer.h +++ b/services/cloud_sync/include/data_syncer.h @@ -41,33 +41,34 @@ protected: /* download */ int32_t Pull(std::shared_ptr provider); - int32_t FetchRecords(Task &task); - int32_t OnFetchRecords(Task &task, std::vector &records); + int32_t OnFetchRecords(std::shared_ptr task, std::vector &records); /* upload */ int32_t Push(std::shared_ptr provider); - int32_t OnCreateRecords(std::vector &records, std::shared_ptr provider); - int32_t OnDeleteRecords(std::vector &records, std::shared_ptr provider); - int32_t OnModifyRecords(std::vector &records, std::shared_ptr provider); - - int32_t OnCreateRecords(std::vector &records, std::shared_ptr task); - int32_t OnDeleteRecords(std::vector &records, std::shared_ptr task); - int32_t OnModifyRecords(std::vector &records, std::shared_ptr task); + int32_t OnCreateRecords(std::shared_ptr task, std::vector &records); + int32_t OnDeleteRecords(std::shared_ptr task, std::vector &records); + int32_t OnModifyRecords(std::shared_ptr task, std::vector &records); /* schedule */ virtual bool IsSchedulable(); virtual int32_t Schedule() = 0; /* task */ - int32_t CommitTask(Task &t); - int32_t CompleteTask(Task &t); + int32_t CommitTask(std::shared_ptr t); + int32_t CompleteTask(std::shared_ptr t); /* retry */ + private: - TaskManager taskManager_; + int32_t CreateRecords(std::shared_ptr provider); + int32_t DeleteRecords(std::shared_ptr provider); + int32_t ModifyRecords(std::shared_ptr provider); + + template + std::function WrapFunc(RET(*func)(ARGS... args)); }; } // namespace CloudSync } // namespace FileManagement } // namespace OHOS -#endif // DATA_SYNCER_H \ No newline at end of file +#endif // DATA_SYNCER_H diff --git a/services/cloud_sync/include/task.h b/services/cloud_sync/include/task.h index f25edb269..07a6c1d4a 100644 --- a/services/cloud_sync/include/task.h +++ b/services/cloud_sync/include/task.h @@ -19,6 +19,9 @@ #include #include #include +#include + +#include "thread_pool.h" #include "drivekit.h" #include "data_provider.h" @@ -27,17 +30,16 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { - -class Task { +class Task : public std::enable_shared_from_this { public: - using TaskAction = std::function; + using TaskAction = std::function)>; Task(); - virtual ~Task(); + virtual ~Task() = default; - virtual int32_t Run() + virtual void Run() { - return action_(*this); + (void)action_(shared_from_this()); } virtual int32_t GetId() final @@ -51,13 +53,21 @@ public: } protected: - std::function action_; + std::function)> action_; std::shared_ptr provider_; private: int32_t id_; }; +class GeneralTask : public Task { +public: + GeneralTask(TaskAction f) + { + action_ = f; + } +}; + class ModifyRecordTask : public Task { public: ModifyRecordTask(std::shared_ptr &provider, TaskAction f) @@ -98,15 +108,26 @@ private: class TaskManager { public: - int32_t CommitTask(Task &t); - int32_t CompleteTask(Task &t); + static std::shared_ptr GetInstance(); + virtual ~TaskManager(); + + int32_t CommitTask(std::shared_ptr t); + int32_t CompleteTask(std::shared_ptr t); bool IsIdle(); private: - std::list taskList_; + TaskManager(); + + ThreadPool pool_ = ThreadPool("TaskManager"); + + static std::mutex instanceMutex_; + static std::shared_ptr instance_; + + std::shared_mutex mutex_; + std::list> taskList_; }; } // namespace CloudSync } // namespace FileManagement } // namespace OHOS -#endif // TASK_H \ No newline at end of file +#endif // TASK_H diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp index 9784f1acd..160291665 100644 --- a/services/cloud_sync/src/data_syncer.cpp +++ b/services/cloud_sync/src/data_syncer.cpp @@ -49,8 +49,16 @@ int32_t DataSyncer::StopSync() int32_t DataSyncer::Pull(shared_ptr provider) { - auto cb = bind(&DataSyncer::FetchRecords, this, _1); - FetchRecordTask task(provider, cb); + shared_ptr task = make_shared(provider, + [this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnFetchRecords, this, t, _1); + int ret = SdkHelper::FetchRecords(callback); + if (ret != 0) { + + } + return 0; + } + ); int32_t ret = CommitTask(task); if (ret != 0) { @@ -59,25 +67,9 @@ int32_t DataSyncer::Pull(shared_ptr provider) return 0; } -int32_t DataSyncer::FetchRecords(Task &task) -{ - FetchRecordTask &t = static_cast(task); - auto provider = t.GetProvider(); - - /* get fetch parameters here */ - - auto cb = bind(&DataSyncer::OnFetchRecords, this, t, _1); - int32_t ret = SdkHelper::FetchRecords(cb); - if (ret != 0) { - - } - - return 0; -} - -int32_t DataSyncer::OnFetchRecords(Task &task, vector &records) +int32_t DataSyncer::OnFetchRecords(shared_ptr task, vector &records) { - FetchRecordTask &t = static_cast(task); + shared_ptr t = static_pointer_cast(task); /* no more records */ if (records.size() == 0) { @@ -88,7 +80,7 @@ int32_t DataSyncer::OnFetchRecords(Task &task, vector &records } } - auto provider = t.GetProvider(); + auto provider = t->GetProvider(); if (provider == nullptr) { } @@ -107,42 +99,128 @@ int32_t DataSyncer::OnFetchRecords(Task &task, vector &records return 0; } +int32_t DataSyncer::CreateRecords(shared_ptr provider) +{ + vector records; + int32_t ret = provider->GetCreatedRecords(records); + if (ret != 0) { + + } + shared_ptr task = make_shared(provider, + [&records, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnCreateRecords, this, t, _1); + int ret = SdkHelper::CreateRecords(records, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(task); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::DeleteRecords(shared_ptr provider) +{ + vector records; + int32_t ret = provider->GetDeletedRecords(records); + if (ret != 0) { + + } + shared_ptr task = make_shared(provider, + [&records, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnDeleteRecords, this, t, _1); + int ret = SdkHelper::DeleteRecords(records, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(task); + if (ret != 0) { + + } + + return 0; +} + +int32_t DataSyncer::ModifyRecords(shared_ptr provider) +{ + vector records; + int32_t ret = provider->GetModifiedRecords(records); + if (ret != 0) { + + } + shared_ptr task = make_shared(provider, + [&records, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnModifyRecords, this, t, _1); + int ret = SdkHelper::ModifyRecords(records, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(task); + if (ret != 0) { + + } + + return 0; +} + + int32_t DataSyncer::Push(shared_ptr provider) { - /* create */ - vector creaetedRecords; - int32_t ret = provider->GetCreatedRecords(creaetedRecords); - // ModifyRecordTask createTask(provider); - // CommitTask(createTask); - // auto createCallback = bind(&DataSyncer::OnCreateRecords, this, placeholders::_1, createTask); - // int32_t ret = SdkHelper::CreateRecords(records, createCallback); - // if (ret != 0) { + /* + * Bugfix: If the first callback find no more records before below tasks + * commited, syncer will directly schedule to next stage. + */ + int32_t ret = CreateRecords(provider); + if (ret != 0) { - // } + } - /* delete */ - vector deletedRecords; - ret = provider->GetDeletedRecords(deletedRecords); - // auto deleteCallback = bind(&DataSyncer::OnDeleteRecords, this, placeholders::_1, provider); - // ret = SdkHelper::DeleteRecords(records, deleteCallback); - // if (ret != 0) { + ret = DeleteRecords(provider); + if (ret != 0) { - // } + } - /* modify */ - vector modifiedRecords; - ret = provider->GetModifiedRecords(modifiedRecords); - // auto modifyCallback = bind(&DataSyncer::OnModifyRecords, this, placeholders::_1, provider); - // ret = SdkHelper::ModifyRecords(records, modifyCallback); - // if (ret != 0) { + ret = ModifyRecords(provider); + if (ret != 0) { - // } + } return 0; } -int32_t DataSyncer::OnCreateRecords(vector &records, shared_ptr provider) +template +function DataSyncer::WrapFunc(RET(*func)(ARGS... args)) +{ + /* async: lifecycle of args, need shared_ptr here */ + return [=](ARGS&... args) -> RET { + shared_ptr task = make_shared([=](shared_ptr t) -> int32_t { + return (*func)(args...); + } + ); + int ret = CommitTask(task); + if (ret != 0) { + + } + return 0; + }; +} + +int32_t DataSyncer::OnCreateRecords(shared_ptr task, vector &records) { + shared_ptr t = static_pointer_cast(task); + auto provider = t->GetProvider(); + int32_t ret = provider->OnCreateRecords(records); if (ret != 0) { /* error */ @@ -156,18 +234,30 @@ int32_t DataSyncer::OnCreateRecords(vector &records, shared_pt Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnCreateRecords, this, placeholders::_1, provider); - // ret = SdkHelper::CreateRecords(records, callback); - // if (ret != 0) { + shared_ptr newTask = make_shared(provider, + [&createdRecords, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnCreateRecords, this, t, _1); + int ret = SdkHelper::CreateRecords(createdRecords, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(newTask); + if (ret != 0) { - // } + } } return 0; } -int32_t DataSyncer::OnDeleteRecords(vector &records, shared_ptr provider) +int32_t DataSyncer::OnDeleteRecords(shared_ptr task, vector &records) { + shared_ptr t = static_pointer_cast(task); + auto provider = t->GetProvider(); + int32_t ret = provider->OnDeleteRecords(records); if (ret != 0) { /* error */ @@ -181,18 +271,30 @@ int32_t DataSyncer::OnDeleteRecords(vector &records, shared_pt Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnDeleteRecords, this, placeholders::_1, provider); - // ret = SdkHelper::DeleteRecords(records, callback); - // if (ret != 0) { + shared_ptr newTask = make_shared(provider, + [&deletedRecords, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnDeleteRecords, this, t, _1); + int ret = SdkHelper::DeleteRecords(deletedRecords, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(newTask); + if (ret != 0) { - // } + } } return 0; } -int32_t DataSyncer::OnModifyRecords(vector &records, shared_ptr provider) +int32_t DataSyncer::OnModifyRecords(shared_ptr task, vector &records) { + shared_ptr t = static_pointer_cast(task); + auto provider = t->GetProvider(); + int32_t ret = provider->OnModifyRecords(records); if (ret != 0) { /* error */ @@ -206,46 +308,40 @@ int32_t DataSyncer::OnModifyRecords(vector &records, shared_pt Schedule(); } } else { - // auto callback = bind(&DataSyncer::OnModifyRecords, this, placeholders::_1, provider); - // ret = SdkHelper::ModifyRecords(records, callback); - // if (ret != 0) { + shared_ptr newTask = make_shared(provider, + [&modifiedRecords, this](shared_ptr t) -> int32_t { + auto callback = bind(&DataSyncer::OnModifyRecords, this, t, _1); + int ret = SdkHelper::ModifyRecords(modifiedRecords, callback); + if (ret != 0) { + + } + return 0; + } + ); + ret = CommitTask(newTask); + if (ret != 0) { - // } + } } return 0; } -int32_t DataSyncer::OnCreateRecords(std::vector &records, std::shared_ptr task) -{ - return 0; -} - -int32_t DataSyncer::OnDeleteRecords(std::vector &records, std::shared_ptr task) -{ - return 0; -} - -int32_t DataSyncer::OnModifyRecords(std::vector &records, std::shared_ptr task) -{ - return 0; -} - -int32_t DataSyncer::CommitTask(Task &t) +int32_t DataSyncer::CommitTask(shared_ptr t) { - return taskManager_.CommitTask(t); + return TaskManager::GetInstance()->CommitTask(t); } -int32_t DataSyncer::CompleteTask(Task &t) +int32_t DataSyncer::CompleteTask(shared_ptr t) { - return taskManager_.CompleteTask(t); + return TaskManager::GetInstance()->CompleteTask(t); } bool DataSyncer::IsSchedulable() { /* all tasks have been finished at this stage, so move to the next stage */ - return taskManager_.IsIdle(); + return TaskManager::GetInstance()->IsIdle(); } } // namespace CloudSync } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/cloud_sync/src/file_manager.cpp b/services/cloud_sync/src/file_manager.cpp index 9ca4fe56c..0b0d30b64 100644 --- a/services/cloud_sync/src/file_manager.cpp +++ b/services/cloud_sync/src/file_manager.cpp @@ -19,7 +19,78 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { using namespace std; +FileManager::FileManager() +{ + +} +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 FileManager::DownloadFile(vector recordIds, + int32_t id, string &path, + function)> resultCallback, + function)> progressCallback) +{ + return 0; +} + +int32_t FileManager::CancelDownloadFile(int32_t id) +{ + return 0; +} + +int32_t FileManager::OnDownloadProgress(std::vector) +{ + return 0; +} + +int32_t FileManager::OnDownloadResult(std::vector) +{ + return 0; +} + +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 \ No newline at end of file +} // namespace OHOS diff --git a/services/cloud_sync/src/sdk_helper.cpp b/services/cloud_sync/src/sdk_helper.cpp index 5c627720f..86f51a703 100644 --- a/services/cloud_sync/src/sdk_helper.cpp +++ b/services/cloud_sync/src/sdk_helper.cpp @@ -14,6 +14,7 @@ */ #include "sdk_helper.h" +#include "task.h" namespace OHOS { namespace FileManagement { @@ -27,6 +28,7 @@ int32_t SdkHelper::FetchRecords(function &)> ca int32_t SdkHelper::CreateRecords(vector &records, function &)> callback) { + // DriveKit::CreateRecords(records, callback); return 0; } @@ -42,6 +44,19 @@ int32_t SdkHelper::ModifyRecords(vector &records, function recordIds, + int32_t id, string &path, + function)> resultCallback, + function)> progressCallback) +{ + return 0; +} + +int32_t SdkHelper::CancelDownloadAssets(int32_t id) +{ + return 0; +} } // namespace CloudSync } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/cloud_sync/src/task.cpp b/services/cloud_sync/src/task.cpp index 9e3ec4d9d..2a2187b8f 100644 --- a/services/cloud_sync/src/task.cpp +++ b/services/cloud_sync/src/task.cpp @@ -26,33 +26,65 @@ using namespace std; Task::Task() { + /* todo */ + id_ = 1; } -Task::~Task() +std::shared_ptr TaskManager::GetInstance() { + if (instance_ == nullptr) { + std::lock_guard guard(instanceMutex_); + + if (instance_ != nullptr) { + return instance_; + } + + instance_ = std::shared_ptr(); + } + + return instance_; +} + +TaskManager::TaskManager() { + pool_.SetMaxTaskNum(8); + pool_.Start(0); } -int32_t TaskManager::CommitTask(Task &t) +TaskManager::~TaskManager() { - /* lock */ - taskList_.push_back(t); + pool_.Stop(); +} + +int32_t TaskManager::CommitTask(shared_ptr t) +{ + unique_lock lock(mutex_); - /* schedule */ - t.Run(); + taskList_.emplace_back(t); + + pool_.AddTask(bind(&Task::Run, *t)); return 0; } -int32_t TaskManager::CompleteTask(Task &t) +int32_t TaskManager::CompleteTask(shared_ptr t) { - /* lock */ + unique_lock lock(mutex_); + /* erase task from list */ + for (auto entry = taskList_.begin(); entry != taskList_.end();) + { + if (entry->get()->GetId() == t->GetId()) + (void)taskList_.erase(entry); + else + entry++; + } return 0; } bool TaskManager::IsIdle() { + shared_lock lock(mutex_); return taskList_.empty(); } } // namespace CloudSync diff --git a/services/distributedfiledaemon/include/ipc/daemon.h b/services/distributedfiledaemon/include/ipc/daemon.h index 7843c9bd4..684f3b8e1 100644 --- a/services/distributedfiledaemon/include/ipc/daemon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -37,7 +37,7 @@ class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyab DECLARE_SYSTEM_ABILITY(Daemon); public: - explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {}; + explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreaeteRecords) {}; virtual ~Daemon() = default; void OnStart() override; @@ -64,4 +64,4 @@ private: } // namespace DistributedFile } // namespace Storage } // namespace OHOS -#endif // DAEMON_H \ No newline at end of file +#endif // DAEMON_H -- Gitee From 1403ab71ed753085cd393b72124e4cc0dd88c5f7 Mon Sep 17 00:00:00 2001 From: jiahaoluo Date: Tue, 21 Feb 2023 02:07:38 +0000 Subject: [PATCH 7/7] implement file manager Signed-off-by: jiahaoluo Change-Id: I577cb54de67df465ef43e0eec29e4ed089f5fc7f --- BUILD.gn | 1 - services/cloud_sync/BUILD.gn | 46 -- services/cloud_sync/gallery/BUILD.gn | 37 -- services/cloud_sync/src/data_syncer.cpp | 347 -------------- services/cloudsyncservice/BUILD.gn | 24 +- .../include/data_syncer}/data_provider.h | 15 +- .../include/data_syncer}/data_syncer.h | 42 +- .../include/data_syncer}/drivekit.h | 25 + .../include/data_syncer}/file_manager.h | 88 +++- .../album_data_provider.h | 8 +- .../gallery_data_syncer}/file_data_provider.h | 11 +- .../gallery_data_syncer.h | 12 +- .../include/data_syncer}/sdk_helper.h | 36 +- .../include/data_syncer}/task.h | 90 ++-- .../src/data_syncer/.data_syncer.cpp.swp | Bin 0 -> 28672 bytes .../src/data_syncer}/data_provider.cpp | 13 +- .../src/data_syncer/data_syncer.cpp | 430 ++++++++++++++++++ .../src/data_syncer}/file_manager.cpp | 134 +++++- .../album_data_provider.cpp | 8 +- .../file_data_provider.cpp | 13 +- .../gallery_data_syncer.cpp | 90 +++- .../src/data_syncer}/sdk_helper.cpp | 22 +- .../src/data_syncer}/task.cpp | 75 +-- .../include/ipc/daemon.h | 2 +- utils/log/include/dfs_error.h | 11 +- 25 files changed, 965 insertions(+), 615 deletions(-) delete mode 100644 services/cloud_sync/BUILD.gn delete mode 100644 services/cloud_sync/gallery/BUILD.gn delete mode 100644 services/cloud_sync/src/data_syncer.cpp rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/data_provider.h (87%) rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/data_syncer.h (50%) rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/drivekit.h (78%) rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/file_manager.h (35%) rename services/{cloud_sync/gallery/include => cloudsyncservice/include/data_syncer/gallery_data_syncer}/album_data_provider.h (85%) rename services/{cloud_sync/gallery/include => cloudsyncservice/include/data_syncer/gallery_data_syncer}/file_data_provider.h (83%) rename services/{cloud_sync/gallery/include => cloudsyncservice/include/data_syncer/gallery_data_syncer}/gallery_data_syncer.h (85%) rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/sdk_helper.h (39%) rename services/{cloud_sync/include => cloudsyncservice/include/data_syncer}/task.h (55%) create mode 100644 services/cloudsyncservice/src/data_syncer/.data_syncer.cpp.swp rename services/{cloud_sync/src => cloudsyncservice/src/data_syncer}/data_provider.cpp (96%) create mode 100644 services/cloudsyncservice/src/data_syncer/data_syncer.cpp rename services/{cloud_sync/src => cloudsyncservice/src/data_syncer}/file_manager.cpp (32%) rename services/{cloud_sync/gallery/src => cloudsyncservice/src/data_syncer/gallery_data_syncer}/album_data_provider.cpp (88%) rename services/{cloud_sync/gallery/src => cloudsyncservice/src/data_syncer/gallery_data_syncer}/file_data_provider.cpp (86%) rename services/{cloud_sync/gallery/src => cloudsyncservice/src/data_syncer/gallery_data_syncer}/gallery_data_syncer.cpp (52%) rename services/{cloud_sync/src => cloudsyncservice/src/data_syncer}/sdk_helper.cpp (49%) rename services/{cloud_sync/src => cloudsyncservice/src/data_syncer}/task.cpp (57%) diff --git a/BUILD.gn b/BUILD.gn index 4954eb29b..7ff292bfb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -21,7 +21,6 @@ group("services_target") { "${services_path}:distributedfile_sa_profile", "${services_path}/cloudsyncservice:cloudsync_sa", "${services_path}/distributedfiledaemon:libdistributedfiledaemon", - "${services_path}/cloud_sync:cloud_sync" ] } diff --git a/services/cloud_sync/BUILD.gn b/services/cloud_sync/BUILD.gn deleted file mode 100644 index cab1214a4..000000000 --- a/services/cloud_sync/BUILD.gn +++ /dev/null @@ -1,46 +0,0 @@ -# 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. -import("//build/ohos.gni") -import("//foundation/filemanagement/dfs_service/distributedfile.gni") - -ohos_shared_library("cloud_sync") { - include_dirs = [ - "include", - "gallery/include" - ] - - sources = [ - "src/data_provider.cpp", - "src/data_syncer.cpp", - "src/file_manager.cpp", - "src/task.cpp", - "src/sdk_helper.cpp", - "gallery/src/gallery_data_syncer.cpp", - "gallery/src/file_data_provider.cpp", - "gallery/src/album_data_provider.cpp", - ] - - deps = [ - ] - - external_deps = [ - "relational_store:native_rdb" - ] - - configs = [ "${utils_path}:compiler_configs" ] - - defines = [ "LOG_TAG=\"distributedfile_cloud\"" ] - - part_name = "dfs_service" - subsystem_name = "filemanagement" -} diff --git a/services/cloud_sync/gallery/BUILD.gn b/services/cloud_sync/gallery/BUILD.gn deleted file mode 100644 index f9d102858..000000000 --- a/services/cloud_sync/gallery/BUILD.gn +++ /dev/null @@ -1,37 +0,0 @@ -# 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. -import("//build/ohos.gni") -import("//foundation/filemanagement/dfs_service/distributedfile.gni") - -ohos_shared_library("cloud_gallery") { - include_dirs = [ - ] - - sources = [ - ] - - deps = [ - "//third_party/libuv:uv" - ] - - external_deps = [ - "ability_base:want" - ] - - configs = [ "${utils_path}:compiler_configs" ] - - defines = [ "LOG_TAG=\"distributedfile_cloud\"" ] - - part_name = "dfs_service" - subsystem_name = "filemanagement" -} diff --git a/services/cloud_sync/src/data_syncer.cpp b/services/cloud_sync/src/data_syncer.cpp deleted file mode 100644 index 160291665..000000000 --- a/services/cloud_sync/src/data_syncer.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* - * 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 "sdk_helper.h" - -namespace OHOS { -namespace FileManagement { -namespace CloudSync { -using namespace std; -using namespace placeholders; - -shared_ptr DataSyncer::InitRdb(NativeRdb::RdbStoreConfig &config) -{ - int32_t err; - RdbCallback cb; - auto rdb = NativeRdb::RdbHelper::GetRdbStore(config, 0, cb, err); - if (rdb == nullptr) { - - } - - return rdb; -} - -int32_t DataSyncer::StartSync() -{ - return Schedule(); -} - -int32_t DataSyncer::StopSync() -{ - return 0; -} - -int32_t DataSyncer::Pull(shared_ptr provider) -{ - shared_ptr task = make_shared(provider, - [this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnFetchRecords, this, t, _1); - int ret = SdkHelper::FetchRecords(callback); - if (ret != 0) { - - } - return 0; - } - ); - int32_t ret = CommitTask(task); - if (ret != 0) { - - } - - return 0; -} - -int32_t DataSyncer::OnFetchRecords(shared_ptr task, vector &records) -{ - shared_ptr t = static_pointer_cast(task); - - /* no more records */ - if (records.size() == 0) { - CompleteTask(t); - - if (IsSchedulable()) { - Schedule(); - } - } - - auto provider = t->GetProvider(); - if (provider == nullptr) { - - } - - int32_t ret = provider->OnFetchRecords(records); - if (ret != 0) { - - } - - /* pull more */ - ret = Pull(provider); - if (ret != 0) { - - } - - return 0; -} - -int32_t DataSyncer::CreateRecords(shared_ptr provider) -{ - vector records; - int32_t ret = provider->GetCreatedRecords(records); - if (ret != 0) { - - } - shared_ptr task = make_shared(provider, - [&records, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnCreateRecords, this, t, _1); - int ret = SdkHelper::CreateRecords(records, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(task); - if (ret != 0) { - - } - - return 0; -} - -int32_t DataSyncer::DeleteRecords(shared_ptr provider) -{ - vector records; - int32_t ret = provider->GetDeletedRecords(records); - if (ret != 0) { - - } - shared_ptr task = make_shared(provider, - [&records, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnDeleteRecords, this, t, _1); - int ret = SdkHelper::DeleteRecords(records, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(task); - if (ret != 0) { - - } - - return 0; -} - -int32_t DataSyncer::ModifyRecords(shared_ptr provider) -{ - vector records; - int32_t ret = provider->GetModifiedRecords(records); - if (ret != 0) { - - } - shared_ptr task = make_shared(provider, - [&records, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnModifyRecords, this, t, _1); - int ret = SdkHelper::ModifyRecords(records, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(task); - if (ret != 0) { - - } - - return 0; -} - - -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. - */ - int32_t ret = CreateRecords(provider); - if (ret != 0) { - - } - - ret = DeleteRecords(provider); - if (ret != 0) { - - } - - ret = ModifyRecords(provider); - if (ret != 0) { - - } - - return 0; -} - -template -function DataSyncer::WrapFunc(RET(*func)(ARGS... args)) -{ - /* async: lifecycle of args, need shared_ptr here */ - return [=](ARGS&... args) -> RET { - shared_ptr task = make_shared([=](shared_ptr t) -> int32_t { - return (*func)(args...); - } - ); - int ret = CommitTask(task); - if (ret != 0) { - - } - return 0; - }; -} - -int32_t DataSyncer::OnCreateRecords(shared_ptr task, vector &records) -{ - shared_ptr t = static_pointer_cast(task); - auto provider = t->GetProvider(); - - int32_t ret = provider->OnCreateRecords(records); - if (ret != 0) { - /* error */ - } - - /* push more */ - vector createdRecords; - ret = provider->GetCreatedRecords(createdRecords); - if (createdRecords.size() == 0) { - if (IsSchedulable()) { - Schedule(); - } - } else { - shared_ptr newTask = make_shared(provider, - [&createdRecords, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnCreateRecords, this, t, _1); - int ret = SdkHelper::CreateRecords(createdRecords, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(newTask); - if (ret != 0) { - - } - } - - return 0; -} - -int32_t DataSyncer::OnDeleteRecords(shared_ptr task, vector &records) -{ - shared_ptr t = static_pointer_cast(task); - auto provider = t->GetProvider(); - - int32_t ret = provider->OnDeleteRecords(records); - if (ret != 0) { - /* error */ - } - - /* push more */ - vector deletedRecords; - ret = provider->GetDeletedRecords(deletedRecords); - if (deletedRecords.size() == 0) { - if (IsSchedulable()) { - Schedule(); - } - } else { - shared_ptr newTask = make_shared(provider, - [&deletedRecords, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnDeleteRecords, this, t, _1); - int ret = SdkHelper::DeleteRecords(deletedRecords, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(newTask); - if (ret != 0) { - - } - } - - return 0; -} - -int32_t DataSyncer::OnModifyRecords(shared_ptr task, vector &records) -{ - shared_ptr t = static_pointer_cast(task); - auto provider = t->GetProvider(); - - int32_t ret = provider->OnModifyRecords(records); - if (ret != 0) { - /* error */ - } - - /* push more */ - vector modifiedRecords; - ret = provider->GetModifiedRecords(modifiedRecords); - if (modifiedRecords.size() == 0) { - if (IsSchedulable()) { - Schedule(); - } - } else { - shared_ptr newTask = make_shared(provider, - [&modifiedRecords, this](shared_ptr t) -> int32_t { - auto callback = bind(&DataSyncer::OnModifyRecords, this, t, _1); - int ret = SdkHelper::ModifyRecords(modifiedRecords, callback); - if (ret != 0) { - - } - return 0; - } - ); - ret = CommitTask(newTask); - if (ret != 0) { - - } - } - - return 0; -} - -int32_t DataSyncer::CommitTask(shared_ptr t) -{ - return TaskManager::GetInstance()->CommitTask(t); -} - -int32_t DataSyncer::CompleteTask(shared_ptr t) -{ - return TaskManager::GetInstance()->CompleteTask(t); -} - -bool DataSyncer::IsSchedulable() -{ - /* all tasks have been finished at this stage, so move to the next stage */ - return TaskManager::GetInstance()->IsIdle(); -} -} // namespace CloudSync -} // namespace FileManagement -} // namespace OHOS diff --git a/services/cloudsyncservice/BUILD.gn b/services/cloudsyncservice/BUILD.gn index 365ba8f33..0337b003a 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/cloud_sync/include/data_provider.h b/services/cloudsyncservice/include/data_syncer/data_provider.h similarity index 87% rename from services/cloud_sync/include/data_provider.h rename to services/cloudsyncservice/include/data_syncer/data_provider.h index 435f54d09..363edbcd6 100644 --- a/services/cloud_sync/include/data_provider.h +++ b/services/cloudsyncservice/include/data_syncer/data_provider.h @@ -32,20 +32,24 @@ class DataProvider { public: /* download */ virtual int32_t GetFetchCondition() = 0; - virtual int32_t OnFetchRecords(std::vector &records) = 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(std::vector &records) = 0; - virtual int32_t OnDeleteRecords(std::vector &records) = 0; - virtual int32_t OnModifyRecords(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 { @@ -84,8 +88,7 @@ protected: /* init */ RdbProvider(std::string &table, std::shared_ptr rdb) : rdb_(rdb), tableName_(table) {} - virtual void SetConfig(const std::string &name, const std::string &path, - const std::string &bundleName, const int32_t area, const int32_t connectSize); + virtual ~RdbProvider() = default; /* insert, delete, update, query */ virtual int32_t Insert(int64_t &outRowId, const NativeRdb::ValuesBucket &initialValues); diff --git a/services/cloud_sync/include/data_syncer.h b/services/cloudsyncservice/include/data_syncer/data_syncer.h similarity index 50% rename from services/cloud_sync/include/data_syncer.h rename to services/cloudsyncservice/include/data_syncer/data_syncer.h index c1a00ad4e..2b318c2d9 100644 --- a/services/cloud_sync/include/data_syncer.h +++ b/services/cloudsyncservice/include/data_syncer/data_syncer.h @@ -34,25 +34,37 @@ public: 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 InitRdb(NativeRdb::RdbStoreConfig &config); + std::shared_ptr GetRdb(NativeRdb::RdbStoreConfig &config); /* download */ int32_t Pull(std::shared_ptr provider); - int32_t OnFetchRecords(std::shared_ptr task, std::vector &records); + void OnFetchRecords(const std::shared_ptr context, const std::vector &records); /* upload */ int32_t Push(std::shared_ptr provider); - int32_t OnCreateRecords(std::shared_ptr task, std::vector &records); - int32_t OnDeleteRecords(std::shared_ptr task, std::vector &records); - int32_t OnModifyRecords(std::shared_ptr task, std::vector &records); + 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 int32_t Schedule() = 0; + 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); @@ -61,12 +73,20 @@ protected: /* retry */ private: - int32_t CreateRecords(std::shared_ptr provider); - int32_t DeleteRecords(std::shared_ptr provider); - int32_t ModifyRecords(std::shared_ptr provider); + 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); - template - std::function WrapFunc(RET(*func)(ARGS... args)); + std::string name_; + std::shared_ptr taskManager_; }; } // namespace CloudSync } // namespace FileManagement diff --git a/services/cloud_sync/include/drivekit.h b/services/cloudsyncservice/include/data_syncer/drivekit.h similarity index 78% rename from services/cloud_sync/include/drivekit.h rename to services/cloudsyncservice/include/data_syncer/drivekit.h index 1440ac113..75088a2b0 100644 --- a/services/cloud_sync/include/drivekit.h +++ b/services/cloudsyncservice/include/data_syncer/drivekit.h @@ -24,6 +24,20 @@ 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() {} @@ -54,6 +68,17 @@ 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 diff --git a/services/cloud_sync/include/file_manager.h b/services/cloudsyncservice/include/data_syncer/file_manager.h similarity index 35% rename from services/cloud_sync/include/file_manager.h rename to services/cloudsyncservice/include/data_syncer/file_manager.h index ac6c7aada..57c22e412 100644 --- a/services/cloud_sync/include/file_manager.h +++ b/services/cloudsyncservice/include/data_syncer/file_manager.h @@ -17,9 +17,18 @@ #define FILE_MANAGER_H #include +#include +#include #include -#include "sdk_helper.h" +#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 { @@ -39,13 +48,48 @@ private: int32_t percent_; }; -class FileManager { +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 DownloadFile(); - int32_t CancelDownloadFile(); - int32_t OnDownloadProgress(); - int32_t OnDownloadResult(); + 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(); @@ -54,13 +98,41 @@ public: int32_t OnUploadResult(); /* query */ + bool IsIdle(); int32_t GetDownloadProgress(); int32_t GetUploadProgress(); + /* wait */ + bool IsWaitting(); + int32_t Wait(std::shared_ptr dataSyncer); + private: - std::unordered_map taskMap_; + 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 \ No newline at end of file +#endif // FILE_MANAGER_H diff --git a/services/cloud_sync/gallery/include/album_data_provider.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h similarity index 85% rename from services/cloud_sync/gallery/include/album_data_provider.h rename to services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h index 4509e4546..7bfeaa7e4 100644 --- a/services/cloud_sync/gallery/include/album_data_provider.h +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/album_data_provider.h @@ -27,7 +27,7 @@ public: virtual ~AlbumDataProvider() = default; /* download */ - virtual int32_t OnFetchRecords(std::vector &records) override; + virtual int32_t OnFetchRecords(const std::vector &records) override; virtual int32_t GetFetchCondition() override; /* upload */ @@ -35,9 +35,9 @@ public: virtual int32_t GetDeletedRecords(std::vector &records) override; virtual int32_t GetModifiedRecords(std::vector &records) override; - virtual int32_t OnCreateRecords(std::vector &records) override; - virtual int32_t OnDeleteRecords(std::vector &records) override; - virtual int32_t OnModifyRecords(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"; diff --git a/services/cloud_sync/gallery/include/file_data_provider.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h similarity index 83% rename from services/cloud_sync/gallery/include/file_data_provider.h rename to services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h index 200fe3873..37a462ad4 100644 --- a/services/cloud_sync/gallery/include/file_data_provider.h +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/file_data_provider.h @@ -30,17 +30,20 @@ public: /* download */ virtual int32_t GetFetchCondition() override; - virtual int32_t OnFetchRecords(std::vector &records) 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(std::vector &records) override; - virtual int32_t OnDeleteRecords(std::vector &records) override; - virtual int32_t OnModifyRecords(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_ = "Files"; diff --git a/services/cloud_sync/gallery/include/gallery_data_syncer.h b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h similarity index 85% rename from services/cloud_sync/gallery/include/gallery_data_syncer.h rename to services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h index f92e6644b..96399db2c 100644 --- a/services/cloud_sync/gallery/include/gallery_data_syncer.h +++ b/services/cloudsyncservice/include/data_syncer/gallery_data_syncer/gallery_data_syncer.h @@ -23,11 +23,11 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { -class GalleryDataSyncer : public DataSyncer { +class GalleryDataSyncer : public DataSyncer, std::enable_shared_from_this { public: GalleryDataSyncer(); - virtual int32_t Schedule() override; + virtual void Schedule() override; private: enum { @@ -36,6 +36,7 @@ private: DOWNLOADFILE, UPLOADALBUM, UPLOADFILE, + WAIT, END }; @@ -43,15 +44,18 @@ private: 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 name_ = "media_library.db"; + 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; @@ -62,4 +66,4 @@ private: } // namespace CloudSync } // namespace FileManagement } // namespace OHOS -#endif // GALLERY_DATA_SYNCER_H \ No newline at end of file +#endif // GALLERY_DATA_SYNCER_H diff --git a/services/cloud_sync/include/sdk_helper.h b/services/cloudsyncservice/include/data_syncer/sdk_helper.h similarity index 39% rename from services/cloud_sync/include/sdk_helper.h rename to services/cloudsyncservice/include/data_syncer/sdk_helper.h index 5d366f4be..67c974ddb 100644 --- a/services/cloud_sync/include/sdk_helper.h +++ b/services/cloudsyncservice/include/data_syncer/sdk_helper.h @@ -24,20 +24,34 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { + class SdkHelper { public: - /* download */ - static int32_t FetchRecords(std::function &)> callback); - - /* upload */ - static int32_t CreateRecords(std::vector &records, - std::function &)> callback); - static int32_t DeleteRecords(std::vector &records, - std::function &)> callback); - static int32_t ModifyRecords(std::vector &records, - std::function &)> callback); + /* 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 \ No newline at end of file +#endif // SDK_HELPER_H diff --git a/services/cloud_sync/include/task.h b/services/cloudsyncservice/include/data_syncer/task.h similarity index 55% rename from services/cloud_sync/include/task.h rename to services/cloudsyncservice/include/data_syncer/task.h index 07a6c1d4a..9ec1a5287 100644 --- a/services/cloud_sync/include/task.h +++ b/services/cloudsyncservice/include/data_syncer/task.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "thread_pool.h" @@ -29,86 +30,52 @@ namespace OHOS { namespace FileManagement { namespace CloudSync { - -class Task : public std::enable_shared_from_this { +class TaskContext : public DriveKit::DKContext { public: - using TaskAction = std::function)>; - - Task(); - virtual ~Task() = default; - - virtual void Run() - { - (void)action_(shared_from_this()); - } - - virtual int32_t GetId() final - { - return id_; - } + TaskContext(std::shared_ptr provider) : provider_(provider) + {} - virtual std::shared_ptr GetProvider() + std::shared_ptr GetProvider() { return provider_; } -protected: - std::function)> action_; - std::shared_ptr provider_; - private: - int32_t id_; + std::shared_ptr provider_; }; -class GeneralTask : public Task { +class Task { public: - GeneralTask(TaskAction f) - { - action_ = f; - } -}; + using Action = std::function)>; -class ModifyRecordTask : public Task { -public: - ModifyRecordTask(std::shared_ptr &provider, TaskAction f) + Task(std::shared_ptr context, Action action) : context_(context), + action_(action) { - action_ = f; - provider_ = provider; + id_ = GenerateTaskId(); } + virtual ~Task() = default; -private: -}; - -class FetchRecordTask : public Task { -public: - FetchRecordTask(std::shared_ptr provider, TaskAction f) { - action_ = f; - provider_ = provider; + virtual void Run() + { + action_(context_); } -private: -}; - -class RetryFetchRecordTask : public Task { -public: - RetryFetchRecordTask(std::shared_ptr provider, TaskAction f) + int32_t GetId() { - action_ = f; - provider_ = provider; + return id_; } private: -}; - -class FileTask : public Task { -public: + int32_t GenerateTaskId(); -private: + int32_t id_; + std::shared_ptr context_; + Action action_; }; class TaskManager { public: - static std::shared_ptr GetInstance(); + TaskManager(); virtual ~TaskManager(); int32_t CommitTask(std::shared_ptr t); @@ -116,16 +83,19 @@ public: bool IsIdle(); -private: - TaskManager(); + bool StopAndWaitFor(); +private: ThreadPool pool_ = ThreadPool("TaskManager"); + const int32_t MAX_THREAD_NUM = 4; + const int32_t WAIT_FOR_SECOND = 30; + + bool stopFlag_ = false; - static std::mutex instanceMutex_; - static std::shared_ptr instance_; - std::shared_mutex mutex_; std::list> taskList_; + /* cv for shared_mutex */ + std::condition_variable_any cv_; }; } // namespace CloudSync } // namespace FileManagement 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 GIT binary patch literal 28672 zcmeI43veCPd4O+-aiGP9p`?jv49D`=z7l#En*fz4OCPLd*imqbW=F}X+C{^+9J`}rmCZ53Hb%@b z)Al=xC2zSD#tHSmLikDTSWp@ zztnd5oZRwd7cZ46FKk+%e(BupZnFKNkWD>|EkVyau_?6Rh z2lzZKl*Zxk^KEsXr-#3<3qOCi?)t3o_Zi{myfA|(pV{I0L*eIzb=Th>o{!3LN8R=C za`ur)Ad^5QflLCK1TqO^638TwNg$IzCV@->nFKNkyaovv#ayn3`)-znH~#-<{r{7* zbGf5%2sXleI3J#$o6G$R+zPv34t!-!E_VcOgHOXjxCR`sVF&ygbKu1@bGaYDop1}>3^#!XSHcA_2hM_{Y!xC5w zvtbrI^bX1oe+f6j^>7G&2ljvkOJFfv2(Pel@*nU+cp1I{N8x(-C>(@g_zlG*RYPbj%!9r+)=h&3_XSf|c16RO%;B@#J`f@Ma z4R^t<@F@^Ix(QCku|KCcT+^?*wpusTw`oOAp9#(9KAodw`^%RN`I1nr zQr!}1(qFan1;_Tyy?(1&yp{lz0UdwR*}08p^#_j%bM;0I*DMZIe0Sxb;qB=OZtGIv z{bV_2>@kOeWT_W6T_CFARCU zn{Vd!eYvO>^@YtR$|u*hh0p9#wWp&x`th;QyN4au7g>dMMCMAhbm&yUcV$QtHEOzM zd-8O(Qg)1@WsfL-)Kq1sV3bv1)UZcP)v7#4pYf|cFik$CUVU~l$5sSjd69N>w5T8_$1YkD zQbkr#8@6?AFnv@nxR7sN5w#XKjiA?8R!w(OR5HAiw%nH+cdVkS*O#a=3AJgY*3FU9 zL=RFex{8tF29aKFt=hU#XdC@Gqn;O>u}azWO;1|7p{h?QcTadrce$MZSrn{N^+r#u zWZSsoXH>Kk6`EMI>5C@4IJ8A!lP>XUjjHH*%65FKWEFIfU#gX3a}spK3B&UB9X@8# zQ=acsRLQW)O<^|?^SnT=ZP8JJ?oeCT0BO4I;tuXy?}()fO*UGsZ%0G4jR$VP_` zkGc?cHXi;I$BgBXiX%P7qxvUPo5tgSh*1WxxS3Or)Iu33jGD!2*~E6dicaeJhU=ej zDY&O$G2G(tlyN`M8L?elmDYC6sbf2XeL{I8;i^^2MxhyvBVExpQ(sbB(Svcbzc}34 z$)CuEV>$VSj9gPpc*z=RRZCk{VYpQu80Jrk$YOIPoKJ)k2U3aT6BaZ5Af0tr%F+hU z%r{7f5vK%daZr+)HJjRCardfS+p=y;U;nQ9d6H$*@1%z?5s~m)SE`llbjd!~K!EAX zQ)V+^Z|xGr5#`l?bdO+slF#Y1vX^p+z&)Slb!E!1SdvraswcnXhgUHQrmB?rJL;5~ zDtRlWT=o3oid>wyM`DOY&D6fc+4V#p2PKv0K>X&=ys;#w=eO zDo+tKO*4tb*-U)2?a3UgMGcr{@?2m`#XeWef|y`cW}a20h1G~NZo0Mzky*-^<$08X z@zgF#HamI;H}-8GR6Dx+`@6Re_O2UHef_GZZ|mCL!QQ^D{93QNx9(D#dbh5n3s@8b zmDyWyP0v#pe2ekfGK;!MMN67)xau<-n^I`8lgR{FPQzAB-2Oi9_Srtl>%+I z%aXNgURkx6N~_^XWmI@Z!5A(xhcqVSsm6$Fa*dgkWvdC736M=c^_-GF!Tg(SBqupW zeZSJ#(J?VG(QcqUqh`C~j&zWl$kFTA+}pEm>%h9UW$jDDn|3)>lzLJ*!@fb@2GgVp zze*`t8%$DL)MS{HF;7)JX$`6V(IN|5G%2rcUCyYj zTh;r~bD7^!MwJ&WXbNxAy|zX5P(arj8TD1Z(5#j%UA9bZ)B*ZxttkQZBxr9{n|<1d zDVMDOKgYWMagg|EkVznuKqi4q0+|Fd31kw;B#=qq z4J3j6s-q*ZH71)^xw>=dy)M1}e;ObDdExpW7qveAi{D>`2cQZ&U^z$(fTVqddme%Z z;ltp=rLYt_;K%s*p9P5vxD{@JL+}x}4%Wk5I19dokN*f<3;SUl`k@O}!DVnM`xCcH7zYo{LwQvnw4IOYA zLLlkl4$N1gW7_FqG< za=1I4+(mBd>h*eyfv-XD*+s797PUq8`PphLSe{v)#I@q6F*zl-;{!KwVA><0$GJuI zdP?@T*&*|-G5+9Rb`9Gr$>wR`YqoLWv%Sow=;~bXD76K5ykeJc)GX`?a$=KgUvAiO z%Bem2UN7{C$Ts8@*GalQkq2^+V&=tb-q;b7N|x{!$ZcK9Z?5wQO?d`8#K(U^l<4^R zFAa9=@uq1f-lR%~9z@Op?JABuS4nSipDlj5STOqozfmH}Vrk@qK?fqgw+^lF_tu3~ zY;lTKX)^RZhmHh_C63mh{UhI9;Pq(qjHrmgvtq}1pPkB_s0JM<5;-?X_OWjZ!zUtdN8pk7_mTuH^R2>A#i1moJ5(?{KOOFYuR|hAw04Lt2ELDR zvw-Vq}tolGQNK6Kp$XZ19TQW%) z7D>Wz#b3-`eRNw_K6Jq2+v5m_e|boE|Aqk`ucR2ws;x`)uN%biDyfoDMA{=ON41W&y!lz*Zc0xOx2eaU(tnnX(FTpqrLqBYWO>h~^hcn@utnKfI z``}*q5>&y14Ip~}KV^;oB0LY@hi|~2fCob`ALhV2U^cwKy8m&w3*=pZ--b!>K;8u~ z;Cy&S*7|T54!|T#z&KnAm%thDudMYSfVS$NZfz47w`|P^JV@22z(Xpgu_sQ zov;=<;SyL5P4FGo_&39~&*8dAQ3tmQuspMo3V z2KX)b0Cd4(I2X=<)8K9JGD>gv`J2r~>T9S(xQdNLA3_&P#u)US&bPvtEUsybLa5ReKY@oPb^v9*i8Ny@(S6MWvik=uzyNr;>MtqPIr07ycK=}f!k)(6=QYh=Yi2DlXNm4JWwRo9i|wQg zIH|nf?A<45ms8Pw0x`a|x=%`VPQ3f5;0+qIJ+WI5&JuXvBXAOgIzIJ1k@&Vodu>4% z((ge+aLdV$v@T*3DqzL7S@bs!=@K3rf6h zi0J>pbvp;A$o6DUPp0vrAm@f&nCS~1Uy;e_shF^8w_~JHQQQB#+&#&+LZf$P;`fec zcJxU6|9!0EX0gVT_5Yz@O_%#$*7^5?yc}K=!zx$_^4|YN&E~M&)Qzz^?wk)2=~C^gI|Gp z@DjT40{j5}4jzSvKw<*+!uwziTnMsHpmitIoBxj=S&Twk7kl*j#cJ8u&SDf8BK3cQ zsEw?_ETFcfkDnonQOIHxWOgzAoGZO13?~cm+_B-D=8Ya%lCBdIK>dDh+BY`ygd_BG z&EPydZeL5S7Gi-miU-Q+CW`;HORYms-)yPe>*mVSO-0}6&6cjy>uI)lb&$mInsert(outRowId, tableName_, initiavalues); @@ -130,7 +140,8 @@ int32_t RdbProvider::RecordsToValueBuckets(const std::vector & case LONG: { v.PutLong(dc.localColumns[j], data[dc.cloudColumns[j]]); break; - } case STRING: { + } + case STRING: { v.PutString(dc.localColumns[j], data[dc.cloudColumns[j]]); break; } 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 000000000..3c5de711b --- /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/cloud_sync/src/file_manager.cpp b/services/cloudsyncservice/src/data_syncer/file_manager.cpp similarity index 32% rename from services/cloud_sync/src/file_manager.cpp rename to services/cloudsyncservice/src/data_syncer/file_manager.cpp index 0b0d30b64..926e4c155 100644 --- a/services/cloud_sync/src/file_manager.cpp +++ b/services/cloudsyncservice/src/data_syncer/file_manager.cpp @@ -15,13 +15,33 @@ #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() @@ -39,29 +59,127 @@ shared_ptr FileManager::GetInstance() return instance_; } -int32_t FileManager::DownloadFile(vector recordIds, - int32_t id, string &path, - function)> resultCallback, - function)> progressCallback) +int32_t FileManagerRdbCallback::OnCreate(NativeRdb::RdbStore &rdb) { + /** + * ------------------------------------------------ + * | id | path | status | progress | app | userId | + * ------------------------------------------------ + * | 1 | /data | Upload | 0.5 | medialibrary | 100 | + * ------------------------------------------------- + */ return 0; } -int32_t FileManager::CancelDownloadFile(int32_t id) +int32_t FileManagerRdbCallback::OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion, + int32_t newVersion) { return 0; } -int32_t FileManager::OnDownloadProgress(std::vector) +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::OnDownloadResult(std::vector) +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; diff --git a/services/cloud_sync/gallery/src/album_data_provider.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp similarity index 88% rename from services/cloud_sync/gallery/src/album_data_provider.cpp rename to services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp index a4ffe677d..78fc192e2 100644 --- a/services/cloud_sync/gallery/src/album_data_provider.cpp +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/album_data_provider.cpp @@ -31,7 +31,7 @@ int32_t AlbumDataProvider::GetFetchCondition() return 0; } -int32_t AlbumDataProvider::OnFetchRecords(vector &records) +int32_t AlbumDataProvider::OnFetchRecords(const vector &records) { return 0; } @@ -81,7 +81,7 @@ int32_t AlbumDataProvider::GetModifiedRecords(vector &records) return 0; } -int32_t AlbumDataProvider::OnCreateRecords(std::vector &records) +int32_t AlbumDataProvider::OnCreateRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); @@ -92,7 +92,7 @@ int32_t AlbumDataProvider::OnCreateRecords(std::vector &record return 0; } -int32_t AlbumDataProvider::OnDeleteRecords(std::vector &records) +int32_t AlbumDataProvider::OnDeleteRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); @@ -103,7 +103,7 @@ int32_t AlbumDataProvider::OnDeleteRecords(std::vector &record return 0; } -int32_t AlbumDataProvider::OnModifyRecords(std::vector &records) +int32_t AlbumDataProvider::OnModifyRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, updateConvertor_); diff --git a/services/cloud_sync/gallery/src/file_data_provider.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp similarity index 86% rename from services/cloud_sync/gallery/src/file_data_provider.cpp rename to services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp index a71ef80e1..7ff28788b 100644 --- a/services/cloud_sync/gallery/src/file_data_provider.cpp +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/file_data_provider.cpp @@ -26,12 +26,17 @@ FileDataProvider::FileDataProvider(std::shared_ptr rdb) : { } +int32_t FileDataProvider::OnDownloadFiles() +{ + return 0; +} + int32_t FileDataProvider::GetFetchCondition() { return 0; } -int32_t FileDataProvider::OnFetchRecords(vector &records) +int32_t FileDataProvider::OnFetchRecords(const vector &records) { return 0; } @@ -81,7 +86,7 @@ int32_t FileDataProvider::GetModifiedRecords(vector &records) return 0; } -int32_t FileDataProvider::OnCreateRecords(std::vector &records) +int32_t FileDataProvider::OnCreateRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, createConvertor_); @@ -92,7 +97,7 @@ int32_t FileDataProvider::OnCreateRecords(std::vector &records return 0; } -int32_t FileDataProvider::OnDeleteRecords(std::vector &records) +int32_t FileDataProvider::OnDeleteRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, deleteConvertor_); @@ -103,7 +108,7 @@ int32_t FileDataProvider::OnDeleteRecords(std::vector &records return 0; } -int32_t FileDataProvider::OnModifyRecords(std::vector &records) +int32_t FileDataProvider::OnModifyRecords(const std::vector &records) { vector valuesBuckets; int ret = RecordsToValueBuckets(records, valuesBuckets, updateConvertor_); diff --git a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp similarity index 52% rename from services/cloud_sync/gallery/src/gallery_data_syncer.cpp rename to services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp index 506e7ba1b..fe960e487 100644 --- a/services/cloud_sync/gallery/src/gallery_data_syncer.cpp +++ b/services/cloudsyncservice/src/data_syncer/gallery_data_syncer/gallery_data_syncer.cpp @@ -15,6 +15,10 @@ #include "gallery_data_syncer.h" +#include "dfs_error.h" +#include "utils_log.h" +#include "file_manager.h" + namespace OHOS { namespace FileManagement { namespace CloudSync { @@ -22,81 +26,123 @@ 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(name_); + config.SetName(rdbName_); config.SetBundleName(bundleName_); config.SetArea(area_); config.SetReadConSize(connectSize_); - rdb_ = InitRdb(config); + rdb_ = GetRdb(config); if (rdb_ == nullptr) { - + LOGE("gallyer data syncer init rdb fail"); } fileProvider_ = make_shared(rdb_); } -int32_t GalleryDataSyncer::Schedule() +void GalleryDataSyncer::Schedule() { + LOGI("current stage is %{public}d", stage_); stage_++; + + int32_t ret = E_OK; switch (stage_) { case DOWNLOADALBUM: { - (void)DownloadAlbum(); + ret = DownloadAlbum(); + break; + } + case DOWNLOADFILE: { + ret = DownloadFile(); break; } - case DOWNLOADFILE: - (void)DownloadFile(); + case UPLOADALBUM: { + ret = UploadAlbum(); break; - case UPLOADALBUM: - (void)UploadAlbum(); + } + case UPLOADFILE: { + ret = UploadFile(); break; - case UPLOADFILE: - (void)UploadFile(); + } + case WAIT: { + ret = Wait(); break; - case END: - (void)Complete(); + } + case END: { + ret = Complete(); break; + } default: + ret = E_SCHEDULE; break; } - return 0; + 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 0; + return E_OK; } int32_t GalleryDataSyncer::DownloadFile() { int ret = Pull(fileProvider_); - if (ret != 0) { + if (ret != E_OK) { } - return 0; + return E_OK; } int32_t GalleryDataSyncer::UploadAlbum() { - return 0; + return E_OK; } int32_t GalleryDataSyncer::UploadFile() { int ret = Push(fileProvider_); - if (ret != 0) { + if (ret != E_OK) { } - return 0; + 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() { - return 0; + CompleteAll(); + return E_OK; } } // namespace CloudSync } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/cloud_sync/src/sdk_helper.cpp b/services/cloudsyncservice/src/data_syncer/sdk_helper.cpp similarity index 49% rename from services/cloud_sync/src/sdk_helper.cpp rename to services/cloudsyncservice/src/data_syncer/sdk_helper.cpp index 86f51a703..cbb111880 100644 --- a/services/cloud_sync/src/sdk_helper.cpp +++ b/services/cloudsyncservice/src/data_syncer/sdk_helper.cpp @@ -21,34 +21,40 @@ namespace FileManagement { namespace CloudSync { using namespace std; -int32_t SdkHelper::FetchRecords(function &)> callback) +int32_t SdkHelper::FetchRecords(shared_ptr context, + function context, vector &)> callback) { return 0; } -int32_t SdkHelper::CreateRecords(vector &records, function &)> callback) +int32_t SdkHelper::CreateRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) { // DriveKit::CreateRecords(records, callback); return 0; } -int32_t SdkHelper::DeleteRecords(vector &records, function &)> callback) +int32_t SdkHelper::DeleteRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) { return 0; } -int32_t SdkHelper::ModifyRecords(vector &records, function &)> callback) +int32_t SdkHelper::ModifyRecords(shared_ptr context, + vector &records, + function context, vector &)> callback) { return 0; } -int32_t SdkHelper::DownloadAssets(vector recordIds, - int32_t id, string &path, - function)> resultCallback, - function)> progressCallback) +int32_t SdkHelper::DownloadAssets(shared_ptr context, + vector recordIds, int32_t id, + function context, vector)> resultCallback) { return 0; } diff --git a/services/cloud_sync/src/task.cpp b/services/cloudsyncservice/src/data_syncer/task.cpp similarity index 57% rename from services/cloud_sync/src/task.cpp rename to services/cloudsyncservice/src/data_syncer/task.cpp index 2a2187b8f..a15c1eb3b 100644 --- a/services/cloud_sync/src/task.cpp +++ b/services/cloudsyncservice/src/data_syncer/task.cpp @@ -16,7 +16,10 @@ #include "task.h" #include +#include +#include "dfs_error.h" +#include "utils_log.h" #include "sdk_helper.h" namespace OHOS { @@ -24,29 +27,15 @@ namespace FileManagement { namespace CloudSync { using namespace std; -Task::Task() +int32_t Task::GenerateTaskId() { /* todo */ - id_ = 1; -} - -std::shared_ptr TaskManager::GetInstance() { - if (instance_ == nullptr) { - std::lock_guard guard(instanceMutex_); - - if (instance_ != nullptr) { - return instance_; - } - - instance_ = std::shared_ptr(); - } - - return instance_; + return 0; } TaskManager::TaskManager() { - pool_.SetMaxTaskNum(8); + pool_.SetMaxTaskNum(MAX_THREAD_NUM); pool_.Start(0); } @@ -59,34 +48,68 @@ int32_t TaskManager::CommitTask(shared_ptr t) { unique_lock lock(mutex_); - taskList_.emplace_back(t); + if (stopFlag_) { + LOGI("commit task fail since stop"); + return E_STOP; + } - pool_.AddTask(bind(&Task::Run, *t)); + taskList_.emplace_back(t); - return 0; + /* + * 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_); - - /* erase task from list */ - for (auto entry = taskList_.begin(); entry != taskList_.end();) - { + 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_); - return taskList_.empty(); + /* + * 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 \ No newline at end of file +} // namespace OHOS diff --git a/services/distributedfiledaemon/include/ipc/daemon.h b/services/distributedfiledaemon/include/ipc/daemon.h index 684f3b8e1..c2983f7b4 100644 --- a/services/distributedfiledaemon/include/ipc/daemon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -37,7 +37,7 @@ class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyab DECLARE_SYSTEM_ABILITY(Daemon); public: - explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreaeteRecords) {}; + explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {}; virtual ~Daemon() = default; void OnStart() override; diff --git a/utils/log/include/dfs_error.h b/utils/log/include/dfs_error.h index c97e6ad5d..a058374a5 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 -- Gitee