From 49ae01cc93bdc3f358a48be0c34c951f64aa1bf3 Mon Sep 17 00:00:00 2001 From: lott14 Date: Wed, 18 Sep 2024 17:48:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Restore=E5=90=8E=E9=87=8D=E6=96=B0=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=95=B0=E6=8D=AE=E5=8F=98=E5=8C=96=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lott14 --- frameworks/native/rdb/include/delay_notify.h | 5 ++-- .../native/rdb/include/rdb_service_proxy.h | 3 ++- .../native/rdb/include/rdb_store_impl.h | 1 + .../native/rdb/include/rdb_types_util.h | 5 ++++ frameworks/native/rdb/src/delay_notify.cpp | 25 +++++++++++++++---- .../native/rdb/src/rdb_service_proxy.cpp | 4 +-- frameworks/native/rdb/src/rdb_store_impl.cpp | 14 +++++++++++ frameworks/native/rdb/src/rdb_types_util.cpp | 11 ++++++++ .../inner_api/rdb/include/rdb_service.h | 2 +- interfaces/inner_api/rdb/include/rdb_types.h | 5 ++++ 10 files changed, 64 insertions(+), 11 deletions(-) diff --git a/frameworks/native/rdb/include/delay_notify.h b/frameworks/native/rdb/include/delay_notify.h index 9ecd74d19..f26f0e073 100644 --- a/frameworks/native/rdb/include/delay_notify.h +++ b/frameworks/native/rdb/include/delay_notify.h @@ -26,13 +26,13 @@ namespace OHOS::NativeRdb { class DelayNotify { public: - using Task = std::function; + using Task = std::function; using Time = std::chrono::steady_clock::time_point; DelayNotify(); ~DelayNotify(); void SetExecutorPool(std::shared_ptr pool); void SetTask(Task task); - void UpdateNotify(const DistributedRdb::RdbChangedData &changedData); + void UpdateNotify(const DistributedRdb::RdbChangedData &changedData, bool isFull = false); void SetAutoSyncInterval(uint32_t autoSyncInterval); void Pause(); void Resume(); @@ -41,6 +41,7 @@ private: static constexpr uint32_t MAX_NOTIFY_INTERVAL = 5000; static constexpr uint32_t SERVICE_INTERVAL = 10000; bool isInitialized_ = false; + bool isFull_ = false; Time lastTimePoint_; std::atomic_int32_t pauseCount_; ExecutorPool::TaskId delaySyncTaskId_ = ExecutorPool::INVALID_TASK_ID; diff --git a/frameworks/native/rdb/include/rdb_service_proxy.h b/frameworks/native/rdb/include/rdb_service_proxy.h index d4ffacc28..daadd0cb6 100644 --- a/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/frameworks/native/rdb/include/rdb_service_proxy.h @@ -72,7 +72,7 @@ public: int32_t Delete(const RdbSyncerParam ¶m) override; int32_t NotifyDataChange(const RdbSyncerParam& param, const RdbChangedData &clientChangedData, - uint32_t delay = 0) override; + const RdbNotifyConfig &rdbNotifyConfig) override; int32_t SetSearchable(const RdbSyncerParam& param, bool isSearchable) override; @@ -82,6 +82,7 @@ public: int32_t Disable(const RdbSyncerParam& param) override; int32_t Enable(const RdbSyncerParam& param) override; + int32_t GetPassword(const RdbSyncerParam& param, std::vector &key) override; std::pair LockCloudContainer(const RdbSyncerParam& param) override; diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index 49d5316be..b401af2a0 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -264,6 +264,7 @@ private: std::pair ExecuteEntry(const std::string& sql, const std::vector& bindArgs, int64_t trxId); int GetDestPath(const std::string &backupPath, std::string &destPath); + void NotifyDataChange(); void ReportDbRestoreSuccessEvent(); static constexpr char SCHEME_RDB[] = "rdb://"; diff --git a/frameworks/native/rdb/include/rdb_types_util.h b/frameworks/native/rdb/include/rdb_types_util.h index 6c231bdba..2788a592c 100644 --- a/frameworks/native/rdb/include/rdb_types_util.h +++ b/frameworks/native/rdb/include/rdb_types_util.h @@ -26,6 +26,7 @@ namespace OHOS::ITypesUtil { using SubOption = DistributedRdb::SubscribeOption; using SyncerParam = DistributedRdb::RdbSyncerParam; +using NotifyConfig = DistributedRdb::RdbNotifyConfig; using Option = DistributedRdb::RdbService::Option; using RdbPredicates = DistributedRdb::PredicatesMemo; using RdbOperation = DistributedRdb::RdbPredicateOperation; @@ -49,6 +50,10 @@ API_EXPORT bool Marshalling(const SyncerParam &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(SyncerParam &output, MessageParcel &data); template<> +API_EXPORT bool Marshalling(const NotifyConfig &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(NotifyConfig &output, MessageParcel &data); +template<> API_EXPORT bool Marshalling(const Option &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(Option &output, MessageParcel &data); diff --git a/frameworks/native/rdb/src/delay_notify.cpp b/frameworks/native/rdb/src/delay_notify.cpp index 07977926e..785bcb70a 100644 --- a/frameworks/native/rdb/src/delay_notify.cpp +++ b/frameworks/native/rdb/src/delay_notify.cpp @@ -30,14 +30,17 @@ DelayNotify::~DelayNotify() pool_->Remove(delaySyncTaskId_); } if (task_ != nullptr && changedData_.tableData.size() > 0) { - auto errCode = task_(changedData_, 0); + DistributedRdb::RdbNotifyConfig rdbNotifyConfig; + rdbNotifyConfig.delay_ = 0; + rdbNotifyConfig.isFull_ = isFull_; + auto errCode = task_(changedData_, rdbNotifyConfig); if (errCode != 0) { LOG_ERROR("NotifyDataChange is failed, err is %{public}d.", errCode); } } } -void DelayNotify::UpdateNotify(const DistributedRdb::RdbChangedData &changedData) +void DelayNotify::UpdateNotify(const DistributedRdb::RdbChangedData &changedData, bool isFull) { LOG_DEBUG("Update changed data."); { @@ -51,6 +54,7 @@ void DelayNotify::UpdateNotify(const DistributedRdb::RdbChangedData &changedData changedData_.tableData.insert_or_assign(k, v); } } + isFull_ |= isFull; } StartTimer(); } @@ -72,9 +76,11 @@ void DelayNotify::StartTimer() { DistributedRdb::RdbChangedData changedData; bool needExecTask = false; + bool isFull = false; { std::lock_guard lock(mutex_); changedData.tableData = changedData_.tableData; + isFull = isFull_; if (pool_ == nullptr) { return; } @@ -106,7 +112,10 @@ void DelayNotify::StartTimer() } if (needExecTask) { - task_(changedData, SERVICE_INTERVAL); + DistributedRdb::RdbNotifyConfig rdbNotifyConfig; + rdbNotifyConfig.delay_ = SERVICE_INTERVAL; + rdbNotifyConfig.isFull_ = isFull; + task_(changedData, rdbNotifyConfig); } } @@ -122,14 +131,20 @@ void DelayNotify::ExecuteTask() { LOG_DEBUG("Notify data change."); DistributedRdb::RdbChangedData changedData; + bool isFull = false; { std::lock_guard lock(mutex_); changedData.tableData = std::move(changedData_.tableData); + isFull = isFull_; RestoreDefaultSyncInterval(); StopTimer(); + isFull_ = false; } - if (task_ != nullptr && changedData.tableData.size() > 0) { - int errCode = task_(changedData, 0); + if (task_ != nullptr && (changedData.tableData.size() > 0 || isFull)) { + DistributedRdb::RdbNotifyConfig rdbNotifyConfig; + rdbNotifyConfig.delay_ = 0; + rdbNotifyConfig.isFull_ = isFull; + int errCode = task_(changedData, rdbNotifyConfig); if (errCode != 0) { LOG_ERROR("NotifyDataChange is failed, err is %{public}d.", errCode); std::lock_guard lock(mutex_); diff --git a/frameworks/native/rdb/src/rdb_service_proxy.cpp b/frameworks/native/rdb/src/rdb_service_proxy.cpp index d1d6b5007..c1edcdce6 100644 --- a/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -512,11 +512,11 @@ int32_t RdbServiceProxy::SetSearchable(const RdbSyncerParam& param, bool isSearc } int32_t RdbServiceProxy::NotifyDataChange(const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, - uint32_t delay) + const RdbNotifyConfig &rdbNotifyConfig) { MessageParcel reply; int32_t status = IPC_SEND(static_cast(RdbServiceCode::RDB_SERVICE_CMD_NOTIFY_DATA_CHANGE), - reply, param, rdbChangedData, delay); + reply, param, rdbChangedData, rdbNotifyConfig); if (status != RDB_OK) { LOG_ERROR("RdbServiceProxy NotifyDataChange fail, status:%{public}d, " "bundleName:%{public}s, storeName:%{public}s", diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 4ad90f711..a45366a34 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -68,6 +68,7 @@ namespace OHOS::NativeRdb { using namespace OHOS::Rdb; using namespace std::chrono; using SqlStatistic = DistributedRdb::SqlStatistic; +using RdbNotifyConfig = DistributedRdb::RdbNotifyConfig; #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) using RdbMgr = DistributedRdb::RdbManagerImpl; #endif @@ -805,6 +806,18 @@ std::shared_ptr RdbStoreImpl::QuerySql(const std::string &sq } return std::make_shared(connectionPool_, path_, sql, bindArgs); } + +void RdbStoreImpl::NotifyDataChange() +{ + int errCode = RegisterDataChangeCallback(); + if (errCode != E_OK) { + LOG_ERROR("RegisterDataChangeCallback is failed, err is %{public}d.", errCode); + } + DistributedRdb::RdbChangedData rdbChangedData; + if (delayNotifier_ != nullptr) { + delayNotifier_->UpdateNotify(rdbChangedData, true); + } +} #endif #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM) @@ -1773,6 +1786,7 @@ int RdbStoreImpl::Restore(const std::string &backupPath, const std::vectorChangeDbFileForRestore(path_, destPath, newKey); #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) + NotifyDataChange(); SecurityPolicy::SetSecurityLabel(config_); if (service != nullptr) { service->Enable(syncerParam_); diff --git a/frameworks/native/rdb/src/rdb_types_util.cpp b/frameworks/native/rdb/src/rdb_types_util.cpp index c4fc4986d..45c7169c2 100644 --- a/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/frameworks/native/rdb/src/rdb_types_util.cpp @@ -30,6 +30,17 @@ bool Unmarshalling(SyncerParam &output, MessageParcel &data) output.isSearchable_, output.haMode_); } +template<> +bool Marshalling(const NotifyConfig &input, MessageParcel &data) +{ + return ITypesUtil::Marshal(data, input.delay_, input.isFull_); +} +template<> +bool Unmarshalling(NotifyConfig &output, MessageParcel &data) +{ + return ITypesUtil::Unmarshal(data, output.delay_, output.isFull_); +} + template<> bool Marshalling(const Option &input, MessageParcel &data) { diff --git a/interfaces/inner_api/rdb/include/rdb_service.h b/interfaces/inner_api/rdb/include/rdb_service.h index a5e77aefb..f80043f25 100644 --- a/interfaces/inner_api/rdb/include/rdb_service.h +++ b/interfaces/inner_api/rdb/include/rdb_service.h @@ -74,7 +74,7 @@ public: const RdbSyncerParam ¶m, const PredicatesMemo &predicates, const std::vector &columns) = 0; virtual int32_t NotifyDataChange( - const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, uint32_t delay = 0) = 0; + const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, const RdbNotifyConfig &rdbNotifyConfig) = 0; virtual int32_t SetSearchable(const RdbSyncerParam& param, bool isSearchable) = 0; diff --git a/interfaces/inner_api/rdb/include/rdb_types.h b/interfaces/inner_api/rdb/include/rdb_types.h index b448d7de4..39b91e6c9 100644 --- a/interfaces/inner_api/rdb/include/rdb_types.h +++ b/interfaces/inner_api/rdb/include/rdb_types.h @@ -56,6 +56,11 @@ struct RdbSyncerParam { }; }; +struct RdbNotifyConfig { + uint32_t delay_ = 0; + bool isFull_ = false; +}; + enum SyncMode { PUSH, PULL, From 4e41452ef0e95e1bc92596976e1c0f834b3447cb Mon Sep 17 00:00:00 2001 From: lott14 Date: Wed, 18 Sep 2024 09:56:52 +0000 Subject: [PATCH 2/3] update frameworks/native/rdb/include/rdb_service_proxy.h. Signed-off-by: lott14 --- frameworks/native/rdb/include/rdb_service_proxy.h | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/native/rdb/include/rdb_service_proxy.h b/frameworks/native/rdb/include/rdb_service_proxy.h index daadd0cb6..ffe2e5252 100644 --- a/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/frameworks/native/rdb/include/rdb_service_proxy.h @@ -82,7 +82,6 @@ public: int32_t Disable(const RdbSyncerParam& param) override; int32_t Enable(const RdbSyncerParam& param) override; - int32_t GetPassword(const RdbSyncerParam& param, std::vector &key) override; std::pair LockCloudContainer(const RdbSyncerParam& param) override; From ee55f3f5d705400e439ed7e25f6d3203fcd8a7ca Mon Sep 17 00:00:00 2001 From: lott14 Date: Wed, 18 Sep 2024 09:59:46 +0000 Subject: [PATCH 3/3] update frameworks/native/rdb/src/rdb_store_impl.cpp. Signed-off-by: lott14 --- frameworks/native/rdb/src/rdb_store_impl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index a45366a34..d423e633c 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -2272,7 +2272,7 @@ void RdbStoreImpl::InitDelayNotifier() } delayNotifier_->SetExecutorPool(pool_); delayNotifier_->SetTask([param = syncerParam_] - (const DistributedRdb::RdbChangedData& rdbChangedData, uint32_t delay) -> int { + (const DistributedRdb::RdbChangedData& rdbChangedData, const RdbNotifyConfig& rdbNotifyConfig) -> int { auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(param); if (errCode == E_NOT_SUPPORT) { return errCode; @@ -2281,7 +2281,7 @@ void RdbStoreImpl::InitDelayNotifier() LOG_ERROR("GetRdbService is failed, err is %{public}d.", errCode); return errCode; } - return service->NotifyDataChange(param, rdbChangedData, delay); + return service->NotifyDataChange(param, rdbChangedData, rdbNotifyConfig); }); }