From d80aa7d177efd18cb566847f395eb173ba9ed825 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 7 Jun 2023 11:21:35 +0800 Subject: [PATCH] update Signed-off-by: zuojiangjiang --- .../service/rdb/rdb_cloud.cpp | 44 ++++--------- .../service/rdb/rdb_cloud.h | 2 - .../service/rdb/rdb_general_store.cpp | 3 +- .../service/rdb/rdb_util.cpp | 61 +++++++++++++++++++ .../service/rdb/rdb_util.h | 34 +++++++++++ 5 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_util.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_util.h diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp index 746ea246..89764786 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "RdbCloud" #include "rdb_cloud.h" #include "log_print.h" +#include "rdb_util.h" #include "value_proxy.h" namespace OHOS::DistributedRdb { @@ -34,7 +35,7 @@ DBStatus RdbCloud::BatchInsert( if (error == GeneralError::E_OK) { extend = ValueProxy::Convert(std::move(extends)); } - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } DBStatus RdbCloud::BatchUpdate( @@ -42,13 +43,13 @@ DBStatus RdbCloud::BatchUpdate( { auto error = cloudDB_->BatchUpdate( tableName, ValueProxy::Convert(std::move(record)), ValueProxy::Convert(std::move(extend))); - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } DBStatus RdbCloud::BatchDelete(const std::string &tableName, std::vector &extend) { auto error = cloudDB_->BatchDelete(tableName, ValueProxy::Convert(std::move(extend))); - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } DBStatus RdbCloud::Query(const std::string &tableName, DBVBucket &extend, std::vector &data) @@ -56,7 +57,7 @@ DBStatus RdbCloud::Query(const std::string &tableName, DBVBucket &extend, std::v auto cursor = cloudDB_->Query(tableName, ValueProxy::Convert(std::move(extend))); if (cursor == nullptr) { ZLOGE("cursor is null, table:%{public}s, extend:%{public}zu", tableName.c_str(), extend.size()); - return ConvertStatus(static_cast(E_ERROR)); + return RdbUtil::ConvertStatus(static_cast(E_ERROR)); } int32_t count = cursor->GetCount(); data.reserve(count); @@ -75,53 +76,30 @@ DBStatus RdbCloud::Query(const std::string &tableName, DBVBucket &extend, std::v ZLOGD("query end, table:%{public}s", tableName.c_str()); return DBStatus::QUERY_END; } - return ConvertStatus(static_cast(err)); + return RdbUtil::ConvertStatus(static_cast(err)); } std::pair RdbCloud::Lock() { auto error = cloudDB_->Lock(); - return std::make_pair(ConvertStatus(static_cast(error)), cloudDB_->AliveTime() * 1000); // int64_t <-> uint32_t, s <-> ms + return std::make_pair(RdbUtil::ConvertStatus(static_cast(error)), cloudDB_->AliveTime() * 1000); // int64_t <-> uint32_t, s <-> ms } DBStatus RdbCloud::UnLock() { auto error = cloudDB_->Unlock(); - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } DBStatus RdbCloud::HeartBeat() { auto error = cloudDB_->Heartbeat(); - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } DBStatus RdbCloud::Close() { auto error = cloudDB_->Close(); - return ConvertStatus(static_cast(error)); + return RdbUtil::ConvertStatus(static_cast(error)); } - -DBStatus RdbCloud::ConvertStatus(DistributedData::GeneralError error) -{ - switch (error) { - case GeneralError::E_OK: - return DBStatus::OK; - case GeneralError::E_BUSY: - return DBStatus::BUSY; - case GeneralError::E_INVALID_ARGS: - return DBStatus::INVALID_ARGS; - case GeneralError::E_NOT_SUPPORT: - return DBStatus::NOT_SUPPORT; - case GeneralError::E_ERROR: // fallthrough - case GeneralError::E_NOT_INIT: - case GeneralError::E_ALREADY_CONSUMED: - case GeneralError::E_ALREADY_CLOSED: - return DBStatus::CLOUD_ERROR; - default: - ZLOGE("unknown error:0x%{public}x", error); - break; - } - return DBStatus::CLOUD_ERROR; -} -} // namespace OHOS::DistributedRdb \ No newline at end of file +} // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h index 30e4ee59..df4df17f 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h @@ -18,7 +18,6 @@ #include "cloud/cloud_db.h" #include "cloud/cloud_store_types.h" #include "cloud/icloud_db.h" -#include "error/general_error.h" namespace OHOS::DistributedRdb { class RdbCloud : public DistributedDB::ICloudDb { @@ -38,7 +37,6 @@ public: DBStatus UnLock() override; DBStatus HeartBeat() override; DBStatus Close() override; - DBStatus ConvertStatus(DistributedData::GeneralError error); private: std::shared_ptr cloudDB_; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 87049d11..d057a60e 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -26,6 +26,7 @@ #include "rdb_helper.h" #include "rdb_query.h" #include "rdb_syncer.h" +#include "rdb_util.h" #include "relational_store_manager.h" #include "value_proxy.h" namespace OHOS::DistributedRdb { @@ -198,7 +199,7 @@ int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &qu } else { dbQuery = rdbQuery->query_; } - auto dbMode = DistributedDB::SyncMode(mode); + auto dbMode = RdbUtil::ConvertMode(static_cast(mode)); auto status = (mode < CLOUD_BEGIN) ? delegate_->Sync(devices, dbMode, dbQuery, GetDBBriefCB(std::move(async)), wait) : delegate_->Sync(devices, dbMode, dbQuery, GetDBProcessCB(std::move(async)), wait); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.cpp new file mode 100644 index 00000000..02eabaee --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "RdbUtil" +#include "rdb_util.h" +#include "log_print.h" + +namespace OHOS::DistributedRdb { +using namespace DistributedDB; +using namespace DistributedData; +RdbUtil::DBStatus RdbUtil::ConvertStatus(DistributedData::GeneralError error) +{ + switch (error) { + case GeneralError::E_OK: + return DBStatus::OK; + case GeneralError::E_BUSY: + return DBStatus::BUSY; + case GeneralError::E_INVALID_ARGS: + return DBStatus::INVALID_ARGS; + case GeneralError::E_NOT_SUPPORT: + return DBStatus::NOT_SUPPORT; + case GeneralError::E_ERROR: // fallthrough + case GeneralError::E_NOT_INIT: + case GeneralError::E_ALREADY_CONSUMED: + case GeneralError::E_ALREADY_CLOSED: + return DBStatus::CLOUD_ERROR; + default: + ZLOGE("unknown error:0x%{public}x", error); + break; + } + return DBStatus::CLOUD_ERROR; +} + +RdbUtil::DBSyncMode RdbUtil::ConvertMode(SyncMode mode) +{ + switch (mode) { + case SyncMode::CLOUD_TIME_FIRST: + return DBSyncMode::SYNC_MODE_CLOUD_MERGE; + case SyncMode::CLOUD_NATIVE_FIRST: + return DBSyncMode::SYNC_MODE_CLOUD_FORCE_PUSH; + case SyncMode::CLOUD_ClOUD_FIRST: + return DBSyncMode::SYNC_MODE_CLOUD_FORCE_PULL; + default: + ZLOGE("unknown mode:0x%{public}x", mode); + break; + } + return DBSyncMode::SYNC_MODE_CLOUD_MERGE; +} +} // OHOS::DistributedRdb \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.h new file mode 100644 index 00000000..60e84c19 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_util.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_UTIL_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_UTIL_H +#include +#include "error/general_error.h" +#include "store/general_store.h" +#include "store_types.h" + +namespace OHOS::DistributedRdb { +class RdbUtil final { +public: + using DBStatus = DistributedDB::DBStatus; + using DBSyncMode = DistributedDB::SyncMode; + using SyncMode = DistributedData::GeneralStore::SyncMode; + + static DBStatus ConvertStatus(DistributedData::GeneralError error); + static DBSyncMode ConvertMode(SyncMode mode); +}; +} // OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_UTIL_H -- Gitee