diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp index 746ea2468d3ee9f0d9a35853b64c2317078d91f7..897647861290c0cbcd1027f50c30a0fc2f58e202 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 30e4ee59245234a35ae56a97c3fd444cd210849c..df4df17f6508a5ab7b4d2828c63a9f99ab62dd28 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 87049d11679196bd3dcebae0ca346c0fed1c2a8b..d057a60eece4acade55e217eb4d1bd1ba890f5fc 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 0000000000000000000000000000000000000000..02eabaeec13ed6e82b7049ff6d7944c03e34f455 --- /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 0000000000000000000000000000000000000000..60e84c1961dd1d4722098a99cd593b725d0bc34b --- /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