From 3f70eb205df2eb62c225fac87470ed692bead8c8 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 16 May 2022 14:28:08 +0800 Subject: [PATCH 1/3] remove unused code Signed-off-by: Sven Wang --- .../distributeddatafwk/include/ikvstore.h | 98 --- .../include/ikvstore_data_service.h | 18 +- .../src/distributed_kv_data_manager.cpp | 44 - .../distributeddatafwk/src/ikvstore.cpp | 751 ----------------- .../src/ikvstore_data_service.cpp | 110 +-- .../distributeddatafwk/src/kvstore_client.cpp | 266 ------ .../distributeddatafwk/src/kvstore_client.h | 70 -- .../src/single_kvstore_client.cpp | 14 - .../src/single_kvstore_client.h | 3 - interfaces/innerkits/distributeddata/BUILD.gn | 2 - .../include/distributed_kv_data_manager.h | 19 - .../distributeddata/include/kvstore.h | 15 +- services/distributeddataservice/app/BUILD.gn | 1 - .../app/src/kvstore_app_manager.cpp | 204 +---- .../app/src/kvstore_app_manager.h | 10 - .../app/src/kvstore_data_service.cpp | 37 +- .../app/src/kvstore_data_service.h | 9 +- .../app/src/kvstore_impl.cpp | 794 ------------------ .../app/src/kvstore_impl.h | 140 --- .../app/src/kvstore_meta_manager.cpp | 15 - .../app/src/kvstore_meta_manager.h | 4 - .../app/src/kvstore_observer_impl.h | 1 - .../app/src/kvstore_user_manager.cpp | 31 - .../app/src/kvstore_user_manager.h | 5 - .../distributeddataservice/app/test/BUILD.gn | 6 - .../app/test/unittest/kvstore_backup_test.cpp | 1 - .../kvstore_impl_logical_isolation_test.cpp | 1 - .../kvstore_impl_physical_isolation_test.cpp | 1 - 28 files changed, 50 insertions(+), 2620 deletions(-) delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore.h delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore.cpp delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h delete mode 100644 services/distributeddataservice/app/src/kvstore_impl.cpp delete mode 100644 services/distributeddataservice/app/src/kvstore_impl.h diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore.h deleted file mode 100644 index 7c374727d..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2021 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 I_KVSTORE_H -#define I_KVSTORE_H - -#include "message_parcel.h" -#include "iremote_broker.h" -#include "ikvstore_observer.h" -#include "ikvstore_snapshot.h" -#include "iremote_proxy.h" -#include "iremote_stub.h" -#include "types.h" - -namespace OHOS { -namespace DistributedKv { -class IKvStoreImpl : public IRemoteBroker { -public: - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreImpl") - - virtual void GetKvStoreSnapshot(sptr observer, - std::function)> callback) = 0; - - virtual Status ReleaseKvStoreSnapshot(sptr iKvStoreSnapshot) = 0; - - virtual Status Put(const Key &key, const Value &value) = 0; - - virtual Status PutBatch(const std::vector &entries) = 0; - - virtual Status Delete(const Key &key) = 0; - - virtual Status DeleteBatch(const std::vector &keys) = 0; - - virtual Status Clear() = 0; - - virtual Status StartTransaction() = 0; - - virtual Status Commit() = 0; - - virtual Status Rollback() = 0; - - virtual Status SubscribeKvStore(const SubscribeType subscribeType, sptr observer) = 0; - - virtual Status UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer) = 0; -}; - -class KvStoreImplStub : public IRemoteStub { -public: - int OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) override; - -private: - int32_t GetKvStoreSnapshotOnRemote(MessageParcel &data, MessageParcel &reply); - int32_t ReleaseKvStoreSnapshotOnRemote(MessageParcel &data, MessageParcel &reply); - int32_t PutOnRemoteRequest(MessageParcel &data, MessageParcel &reply); - int32_t PutBatchOnRemoteRequest(MessageParcel &data, MessageParcel &reply); - int32_t DeleteOnRemoteRequest(MessageParcel &data, MessageParcel &reply); - int32_t DeleteBatchOnRemoteRequest(MessageParcel &data, MessageParcel &reply); - int32_t SubscribeKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); - int32_t UnSubscribeKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); -}; - -class KvStoreImplProxy : public IRemoteProxy { -public: - explicit KvStoreImplProxy(const sptr &impl); - ~KvStoreImplProxy() = default; - virtual void GetKvStoreSnapshot(sptr observer, - std::function)> callback); - virtual Status ReleaseKvStoreSnapshot(sptr iKvStoreSnapshot); - virtual Status Put(const Key &key, const Value &value); - virtual Status PutBatch(const std::vector &entries); - virtual Status Delete(const Key &key); - virtual Status DeleteBatch(const std::vector &keys); - virtual Status Clear(); - virtual Status StartTransaction(); - virtual Status Commit(); - virtual Status Rollback(); - virtual Status SubscribeKvStore(const SubscribeType subscribeType, sptr observer); - virtual Status UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer); -private: - static inline BrokerDelegator delegator_; -}; -} // namespace DistributedKv -} // namespace OHOS - -#endif // I_KVSTORE_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h index 2789ddf9b..fee31eaf4 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h @@ -17,7 +17,6 @@ #define I_KV_STORE_DATA_SERVICE_H #include "iremote_broker.h" -#include "ikvstore.h" #include "ikvstore_client_death_observer.h" #include "ikvstore_observer.h" #include "ikvstore_single.h" @@ -49,7 +48,6 @@ struct OptionsIpc { class IKvStoreDataService : public IRemoteBroker { public: enum { - GETKVSTORE, GETALLKVSTOREID, CLOSEKVSTORE, CLOSEALLKVSTORE, @@ -62,15 +60,13 @@ public: STARTWATCHDEVICECHANGE, STOPWATCHDEVICECHANGE, GET_RDB_SERVICE, + GET_KVDB_SERVICE, SERVICE_CMD_LAST, DATAUSAGESTART = 20, DATAUSAGEEND = 40, }; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreDataService"); - /* create and open kv store instance. */ - virtual Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) = 0; virtual Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::function)> callback) = 0; @@ -98,6 +94,7 @@ public: DeviceFilterStrategy strategy) = 0; virtual Status StopWatchDeviceChange(sptr observer) = 0; virtual sptr GetRdbService() = 0; + virtual sptr GetKVdbService() = 0; }; class KvStoreDataServiceStub : public IRemoteStub { @@ -106,7 +103,6 @@ public: MessageParcel &reply, MessageOption &option) override; private: - int32_t GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); int32_t GetAllKvStoreIdOnRemote(MessageParcel &data, MessageParcel &reply); int32_t CloseKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); int32_t CloseAllKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); @@ -119,10 +115,10 @@ private: int32_t StopWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply); int32_t GetSingleKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); int32_t GetRdbServiceOnRemote(MessageParcel& data, MessageParcel& reply); + int32_t GetKVdbServiceOnRemote(MessageParcel& data, MessageParcel& reply); using RequestHandler = int32_t(KvStoreDataServiceStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandler HANDLERS[SERVICE_CMD_LAST] = { - [GETKVSTORE] = &KvStoreDataServiceStub::GetKvStoreOnRemote, [GETALLKVSTOREID] = &KvStoreDataServiceStub::GetAllKvStoreIdOnRemote, [CLOSEKVSTORE] = &KvStoreDataServiceStub::CloseKvStoreOnRemote, [CLOSEALLKVSTORE] = &KvStoreDataServiceStub::CloseAllKvStoreOnRemote, @@ -135,6 +131,7 @@ private: [STARTWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StartWatchDeviceChangeOnRemote, [STOPWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StopWatchDeviceChangeOnRemote, [GET_RDB_SERVICE] = &KvStoreDataServiceStub::GetRdbServiceOnRemote, + [GET_KVDB_SERVICE] = &KvStoreDataServiceStub::GetKVdbServiceOnRemote, }; }; @@ -142,11 +139,6 @@ class KvStoreDataServiceProxy : public IRemoteProxy { public: explicit KvStoreDataServiceProxy(const sptr &impl); ~KvStoreDataServiceProxy() = default; - - /* create and open kv store instance. */ - virtual Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback); - virtual Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::function)> callback); @@ -172,7 +164,7 @@ public: virtual Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy); virtual Status StopWatchDeviceChange(sptr observer); virtual sptr GetRdbService(); - + virtual sptr GetKVdbService(); private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp index 6c99823fb..c04019108 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp @@ -18,7 +18,6 @@ #include "distributed_kv_data_manager.h" #include "constant.h" #include "ikvstore_data_service.h" -#include "kvstore_client.h" #include "kvstore_service_death_notifier.h" #include "log_print.h" #include "refbase.h" @@ -35,49 +34,6 @@ DistributedKvDataManager::DistributedKvDataManager() DistributedKvDataManager::~DistributedKvDataManager() {} -Status DistributedKvDataManager::GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::shared_ptr &kvStore) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - kvStore = nullptr; - std::string storeIdTmp = Constant::TrimCopy(storeId.storeId); - if (storeIdTmp.size() == 0 || storeIdTmp.size() > Constant::MAX_STORE_ID_LENGTH) { - ZLOGE("invalid storeId."); - return Status::INVALID_ARGUMENT; - } - - KvStoreServiceDeathNotifier::SetAppId(appId); - sptr kvDataServiceProxy = KvStoreServiceDeathNotifier::GetDistributedKvDataService(); - Status status = Status::SERVER_UNAVAILABLE; - if (kvDataServiceProxy == nullptr) { - ZLOGE("proxy is nullptr."); - return status; - } - - ZLOGD("call proxy."); - sptr proxyTmp; - status = kvDataServiceProxy->GetKvStore(options, appId, storeId, - [&](sptr proxy) { proxyTmp = std::move(proxy); }); - if (status == Status::RECOVER_SUCCESS) { - ZLOGE("proxy recover success: %d", static_cast(status)); - kvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); - return status; - } - - if (status != Status::SUCCESS) { - ZLOGE("proxy return error: %d", static_cast(status)); - return status; - } - - if (proxyTmp == nullptr) { - ZLOGE("proxy return nullptr."); - return status; - } - kvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); - return status; -} - Status DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::shared_ptr &singleKvStore) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore.cpp deleted file mode 100644 index a20455456..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore.cpp +++ /dev/null @@ -1,751 +0,0 @@ -/* - * Copyright (c) 2021 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 "KvStoreImplProxy" - -#include "ikvstore.h" -#include -#include -#include "message_parcel.h" -#include "constant.h" -#include "log_print.h" - -namespace OHOS { -namespace DistributedKv { -enum { - GETKVSTORESNAPSHOT, - RELEASEKVSTORESNAPSHOT, - PUT, - PUTBATCH, - DELETE, - DELETEBATCH, - CLEAR, - STARTTRANSACTION, - COMMIT, - ROLLBACK, - SUBSCRIBEKVSTORE, - UNSUBSCRIBEKVSTORE, -}; - -KvStoreImplProxy::KvStoreImplProxy(const sptr &impl) : IRemoteProxy(impl) -{} - -void KvStoreImplProxy::GetKvStoreSnapshot(sptr observer, - std::function)> callback) -{ - if (observer == nullptr) { - callback(Status::INVALID_ARGUMENT, nullptr); - return; - } - MessageParcel data; - MessageParcel reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return; - } - if (!data.WriteRemoteObject(observer->AsObject().GetRefPtr())) { - ZLOGW("get snapshot fail."); - callback(Status::IPC_ERROR, nullptr); - return; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(GETKVSTORESNAPSHOT, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - callback(Status::IPC_ERROR, nullptr); - return; - } - Status status = static_cast(reply.ReadInt32()); - if (status == Status::SUCCESS) { - sptr remote = reply.ReadRemoteObject(); - if (remote == nullptr) { - callback(status, nullptr); - return; - } - sptr kvstoreImplProxy = iface_cast(remote); - callback(status, std::move(kvstoreImplProxy)); - } else { - callback(status, nullptr); - } -} - -Status KvStoreImplProxy::ReleaseKvStoreSnapshot(sptr kvStoreSnapshotPtr) -{ - if (kvStoreSnapshotPtr == nullptr) { - ZLOGW("input snapshot ptr is null"); - return Status::INVALID_ARGUMENT; - } - MessageParcel data; - MessageParcel reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.WriteRemoteObject(kvStoreSnapshotPtr->AsObject().GetRefPtr())) { - ZLOGW("write input snapshot ptr failed."); - return Status::IPC_ERROR; - } - - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(RELEASEKVSTORESNAPSHOT, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::Put(const Key &key, const Value &value) -{ - ZLOGD("proxy put"); - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("write capacity failed."); - return Status::IPC_ERROR; - } - - int bufferSize = key.RawSize() + value.RawSize(); - if (!data.WriteInt32(bufferSize)) { - ZLOGW("write size failed."); - return Status::IPC_ERROR; - } - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - if (!data.WriteParcelable(&key) || !data.WriteParcelable(&value)) { - ZLOGW("write key or value failed."); - return Status::IPC_ERROR; - } - - MessageOption mo { MessageOption::TF_SYNC }; - int error = Remote()->SendRequest(PUT, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest failed with error code %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); - } - ZLOGI("putting large data."); - std::unique_ptr buffer( - new(std::nothrow) uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - return Status::ERROR; - } - int bufferLeftSize = bufferSize; - uint8_t *cursor = buffer.get(); - if (!key.WriteToBuffer(cursor, bufferLeftSize) || - !value.WriteToBuffer(cursor, bufferLeftSize) || - !data.WriteRawData(buffer.get(), bufferSize)) { - ZLOGW("write failed"); - return Status::ERROR; - } - // Parcel before IPC: - // buffer: options | bufferSize - // rawdata: keySize | key | ValueSize | value - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(PUT, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::PutBatch(const std::vector &entries) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("set capacity failed."); - return Status::IPC_ERROR; - } - if (!data.WriteInt32(entries.size())) { - ZLOGW("write entries size failed."); - return Status::IPC_ERROR; - } - - int64_t bufferSize = 0; - for (const auto &item : entries) { - if (item.key.Size() > Constant::MAX_KEY_LENGTH || item.value.Size() > Constant::MAX_VALUE_LENGTH) { - return Status::INVALID_ARGUMENT; - } - bufferSize += item.key.RawSize() + item.value.RawSize(); - } - if (!data.WriteInt32(bufferSize)) { - ZLOGW("write buffer size failed."); - return Status::IPC_ERROR; - } - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - for (const auto &item : entries) { - if (!data.WriteParcelable(&item)) { - ZLOGW("write parcel failed."); - return Status::IPC_ERROR; - } - } - MessageOption mo { MessageOption::TF_SYNC }; - if (Remote()->SendRequest(PUTBATCH, data, reply, mo) != 0) { - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); - } - ZLOGI("putting large data."); - if (bufferSize > static_cast(reply.GetRawDataCapacity())) { - ZLOGW("batch size larger than Messageparcel limit.(%" PRIu64")", bufferSize); - return Status::INVALID_ARGUMENT; - } - std::unique_ptr buffer(new uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - return Status::ERROR; - } - int bufferLeftSize = bufferSize; - uint8_t *cursor = buffer.get(); - for (const auto &item : entries) { - if (!item.key.WriteToBuffer(cursor, bufferLeftSize) || - !item.value.WriteToBuffer(cursor, bufferLeftSize)) { - ZLOGW("write item failed."); - } - } - if (!data.WriteRawData(buffer.get(), bufferSize)) { - ZLOGW("write failed"); - return Status::ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(PUTBATCH, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::Delete(const Key &key) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.WriteParcelable(&key)) { - ZLOGW("write key failed."); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(DELETE, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::DeleteBatch(const std::vector &keys) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("set capacity failed."); - return Status::IPC_ERROR; - } - if (!data.WriteInt32(keys.size())) { - ZLOGW("write keys size failed."); - return Status::IPC_ERROR; - } - for (const auto &item : keys) { - if (item.Size() > Constant::MAX_KEY_LENGTH) { - ZLOGW("Delete key size larger than key size limit"); - return Status::INVALID_ARGUMENT; - } - if (!data.WriteParcelable(&item)) { - ZLOGW("write parcel failed"); - return Status::IPC_ERROR; - } - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(DELETEBATCH, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::Clear() -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(CLEAR, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::StartTransaction() -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(STARTTRANSACTION, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::Commit() -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(COMMIT, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::Rollback() -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(ROLLBACK, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::SubscribeKvStore(const SubscribeType subscribeType, sptr observer) -{ - if (observer == nullptr) { - ZLOGW("observer is invalid."); - return Status::INVALID_ARGUMENT; - } - MessageParcel data; - MessageParcel reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.WriteInt32(static_cast(subscribeType)) || - !data.WriteRemoteObject(observer->AsObject().GetRefPtr())) { - ZLOGW("subscribe type failed."); - return Status::IPC_ERROR; - } - - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(SUBSCRIBEKVSTORE, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -Status KvStoreImplProxy::UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer) -{ - if (observer == nullptr) { - return Status::INVALID_ARGUMENT; - } - - MessageParcel data; - MessageParcel reply; - if (!data.WriteInterfaceToken(KvStoreImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!data.WriteInt32(static_cast(subscribeType)) || - !data.WriteRemoteObject(observer->AsObject().GetRefPtr())) { - ZLOGW("unsubscribe type failed."); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(UNSUBSCRIBEKVSTORE, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest returned %d", error); - return Status::IPC_ERROR; - } - return static_cast(reply.ReadInt32()); -} - -int32_t KvStoreImplStub::GetKvStoreSnapshotOnRemote(MessageParcel &data, MessageParcel &reply) -{ - sptr remote = data.ReadRemoteObject(); - if (remote == nullptr) { - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write obj failed."); - return -1; - } - return 0; - } - sptr kvStoreObserverProxy = iface_cast(remote); - sptr proxyTmp; - Status statusTmp; - GetKvStoreSnapshot(kvStoreObserverProxy, [&](Status status, sptr proxy) { - statusTmp = status; - proxyTmp = std::move(proxy); - }); - if (!reply.WriteInt32(static_cast(statusTmp))) { - ZLOGW("write get snapshot result failed."); - return -1; - } - if (statusTmp == Status::SUCCESS && proxyTmp != nullptr) { - if (!reply.WriteRemoteObject(proxyTmp->AsObject().GetRefPtr())) { - ZLOGW("write strong failed."); - return -1; - } - } - return 0; -} -int32_t KvStoreImplStub::ReleaseKvStoreSnapshotOnRemote(MessageParcel &data, MessageParcel &reply) -{ - sptr remote = data.ReadRemoteObject(); - if (remote == nullptr) { - ZLOGW("kvstoreSnapshotProxy nullptr after ipc"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - return -1; - } - return 0; - } - sptr kvStoreSnapshotProxy = iface_cast(remote); - Status status = ReleaseKvStoreSnapshot(kvStoreSnapshotProxy); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write release snapshot failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::PutOnRemoteRequest(MessageParcel &data, MessageParcel &reply) -{ - if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("write capacity failed"); - return -1; - } - const int bufferSize = data.ReadInt32(); - ZLOGD("bufferSize %d", bufferSize); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - sptr key = data.ReadParcelable(); - sptr value = data.ReadParcelable(); - if (key == nullptr || value == nullptr) { - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write key or val status failed."); - return -1; - } - return 0; - } - Status status = Put(*key, *value); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write ret status failed."); - return -1; - } - return 0; - } - // this memory is managed by MassageParcel, DO NOT free here - const uint8_t *buffer = reinterpret_cast(data.ReadRawData(bufferSize)); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - ZLOGW("write buffer status failed."); - return -1; - } - return 0; - } - int bufferLeftSize = bufferSize; - const uint8_t *cursor = buffer; - Key key; - Value value; - if (!key.ReadFromBuffer(cursor, bufferLeftSize) || !value.ReadFromBuffer(cursor, bufferLeftSize)) { - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - ZLOGW("read key or value error."); - return -1; - } - } - Status status = Put(key, value); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write ret status failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::PutBatchOnRemoteRequest(MessageParcel &data, MessageParcel &reply) -{ - if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("set batch size failed."); - return -1; - } - int len = data.ReadInt32(); - if (len < 0) { - ZLOGW("invalid status. len %d", len); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write putbatch failed."); - return -1; - } - return 0; - } - const int bufferSize = data.ReadInt32(); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - std::vector entries; - for (int i = 0; i < len; i++) { - sptr entry = data.ReadParcelable(); - if (entry == nullptr) { - ZLOGW("putbatch got null entry pointer"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - ZLOGW("write putbatch failed."); - return -1; - } - return 0; - } - entries.push_back(*entry); - } - Status status = PutBatch(entries); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write putbatch failed."); - return -1; - } - return 0; - } - // this memory is managed by MassageParcel, DO NOT free here - const uint8_t *buffer = reinterpret_cast(data.ReadRawData(bufferSize)); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - ZLOGW("write putbatch big failed."); - return -1; - } - return 0; - } - int bufferLeftSize = bufferSize; - const uint8_t *cursor = buffer; - std::vector entries; - Entry entry; - for (int i = 0; i < len; i++) { - bool success = entry.key.ReadFromBuffer(cursor, bufferLeftSize); - success = success && entry.value.ReadFromBuffer(cursor, bufferLeftSize); - entries.push_back(std::move(entry)); - if (!success) { - ZLOGW("get key or value failed"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - ZLOGW("write putbatch big failed."); - return -1; - } - return 0; - } - } - Status status = PutBatch(entries); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write putbatch big failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::DeleteOnRemoteRequest(MessageParcel &data, MessageParcel &reply) -{ - sptr key = data.ReadParcelable(); - if (key == nullptr) { - ZLOGW("key nullptr after ipc"); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write delete failed."); - return -1; - } - return 0; - } - Status status = Delete(*key); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write delete failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::DeleteBatchOnRemoteRequest(MessageParcel &data, MessageParcel &reply) -{ - int len = data.ReadInt32(); - if (len < 0) { - ZLOGW("len %d invalid after ipc", len); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write delete failed."); - return -1; - } - return 0; - } - std::vector keys; - for (int i = 0; i < len; i++) { - sptr key = data.ReadParcelable(); - if (key == nullptr) { - ZLOGW("key nullptr"); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - ZLOGW("write delete failed."); - return -1; - } - return 0; - } - keys.push_back(*key); - } - Status status = DeleteBatch(keys); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write delete failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::SubscribeKvStoreOnRemote(MessageParcel &data, MessageParcel &reply) -{ - int32_t type = data.ReadInt32(); - if (type < 0) { - return -1; - } - SubscribeType subscribeType = static_cast(type); - sptr remote = data.ReadRemoteObject(); - if (remote == nullptr) { - ZLOGW("kvStoreObserverProxy is null"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - return -1; - } - return 0; - } - sptr kvStoreObserverProxy = iface_cast(remote); - Status status = SubscribeKvStore(subscribeType, std::move(kvStoreObserverProxy)); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write subscribe status failed."); - return -1; - } - return 0; -} -int32_t KvStoreImplStub::UnSubscribeKvStoreOnRemote(MessageParcel &data, MessageParcel &reply) -{ - int32_t type = data.ReadInt32(); - if (type < 0) { - return -1; - } - SubscribeType subscribeType = static_cast(type); - sptr remote = data.ReadRemoteObject(); - if (remote == nullptr) { - ZLOGW("unsubscribe Proxy is null"); - if (!reply.WriteInt32(static_cast(Status::IPC_ERROR))) { - return -1; - } - return 0; - } - sptr kvStoreObserverProxy = iface_cast(remote); - Status status = UnSubscribeKvStore(subscribeType, std::move(kvStoreObserverProxy)); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write unsubscribe status failed."); - return -1; - } - return 0; -} - -int32_t KvStoreImplStub::OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) -{ - ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid()); - std::u16string descriptor = KvStoreImplStub::GetDescriptor(); - std::u16string remoteDescriptor = data.ReadInterfaceToken(); - if (descriptor != remoteDescriptor) { - ZLOGE("local descriptor is not equal to remote"); - return -1; - } - switch (code) { - case GETKVSTORESNAPSHOT: { - return GetKvStoreSnapshotOnRemote(data, reply); - } - case RELEASEKVSTORESNAPSHOT: { - return ReleaseKvStoreSnapshotOnRemote(data, reply); - } - case PUT: { - return PutOnRemoteRequest(data, reply); - } - case PUTBATCH: { - return PutBatchOnRemoteRequest(data, reply); - } - case DELETE: { - return DeleteOnRemoteRequest(data, reply); - } - case DELETEBATCH: { - return DeleteBatchOnRemoteRequest(data, reply); - } - case CLEAR: { - Status status = Clear(); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write clear failed."); - return -1; - } - return 0; - } - case STARTTRANSACTION: { - Status status = StartTransaction(); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write transaction failed."); - return -1; - } - return 0; - } - case COMMIT: { - Status status = Commit(); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write commit failed."); - return -1; - } - return 0; - } - case ROLLBACK: { - Status status = Rollback(); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGW("write rollback failed."); - return -1; - } - return 0; - } - case SUBSCRIBEKVSTORE: { - return SubscribeKvStoreOnRemote(data, reply); - } - case UNSUBSCRIBEKVSTORE: { - return UnSubscribeKvStoreOnRemote(data, reply); - } - default: { - MessageOption mo { MessageOption::TF_SYNC }; - return IPCObjectStub::OnRemoteRequest(code, data, reply, mo); - } - } -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp index 96d59797a..620f36ab2 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp @@ -34,56 +34,6 @@ KvStoreDataServiceProxy::KvStoreDataServiceProxy(const sptr &impl ZLOGI("init data service proxy."); } -Status KvStoreDataServiceProxy::GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) -{ - ZLOGI("%s %s", appId.appId.c_str(), storeId.storeId.c_str()); - MessageParcel data; - MessageParcel reply; - - if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - - // Passing a struct with an std::string field is a potential security exploit. - OptionsIpc optionsIpc; - optionsIpc.createIfMissing = options.createIfMissing; - optionsIpc.encrypt = options.encrypt; - optionsIpc.persistent = options.persistent; - optionsIpc.backup = options.backup; - optionsIpc.autoSync = options.autoSync; - optionsIpc.securityLevel = options.securityLevel; - optionsIpc.syncPolicy = options.syncPolicy; - optionsIpc.kvStoreType = options.kvStoreType; - optionsIpc.syncable = options.syncable; - optionsIpc.dataOwnership = true; // set default value - - if (!data.WriteBuffer(&optionsIpc, sizeof(optionsIpc)) || - !data.WriteString(appId.appId) || - !data.WriteString(storeId.storeId)) { - ZLOGW("failed to write parcel."); - return Status::IPC_ERROR; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(GETKVSTORE, data, reply, mo); - if (error != 0) { - ZLOGW("failed to write parcel."); - return Status::IPC_ERROR; - } - Status ret = static_cast(reply.ReadInt32()); - if (ret == Status::SUCCESS) { - sptr remote = reply.ReadRemoteObject(); - if (remote != nullptr) { - sptr kvstoreImplProxy = iface_cast(remote); - callback(std::move(kvstoreImplProxy)); - } - } else { - callback(nullptr); - } - return ret; -} - Status KvStoreDataServiceProxy::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::function)> callback) { @@ -417,46 +367,30 @@ sptr KvStoreDataServiceProxy::GetRdbService() return remoteObject; } -int32_t KvStoreDataServiceStub::GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply) +sptr KvStoreDataServiceProxy::GetKVdbService() { - const OptionsIpc *optionIpcPtr = reinterpret_cast(data.ReadBuffer(sizeof(OptionsIpc))); - if (optionIpcPtr == nullptr) { - ZLOGW("optionPtr is nullptr"); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - return 0; - } - OptionsIpc optionsIpc = *optionIpcPtr; - Options options; - options.createIfMissing = optionsIpc.createIfMissing; - options.encrypt = optionsIpc.encrypt; - options.persistent = optionsIpc.persistent; - options.backup = optionsIpc.backup; - options.autoSync = optionsIpc.autoSync; - options.securityLevel = optionsIpc.securityLevel; - options.syncPolicy = optionsIpc.syncPolicy; - options.kvStoreType = optionsIpc.kvStoreType; - options.syncable = optionsIpc.syncable; - options.dataOwnership = optionsIpc.dataOwnership; - AppId appId = { Constant::TrimCopy(data.ReadString())}; - StoreId storeId = { Constant::TrimCopy(data.ReadString())}; - sptr proxyTmp; - Status status = GetKvStore(options, appId, storeId, - [&proxyTmp](sptr proxy) { proxyTmp = std::move(proxy); }); - if (!reply.WriteInt32(static_cast(status))) { - return -1; + ZLOGI("enter"); + MessageParcel data; + if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return nullptr; } - if (proxyTmp == nullptr) { - ZLOGW("proxy is null."); - return 0; + + MessageParcel reply; + MessageOption mo { MessageOption::TF_SYNC }; + int32_t error = Remote()->SendRequest(GET_KVDB_SERVICE, data, reply, mo); + if (error != 0) { + ZLOGE("SendRequest returned %{public}d", error); + return nullptr; } - if (status == Status::SUCCESS && !reply.WriteRemoteObject(proxyTmp->AsObject().GetRefPtr())) { - ZLOGW("write ipc failed."); - return -1; + auto remoteObject = reply.ReadRemoteObject(); + if (remoteObject == nullptr) { + ZLOGE("remote object is nullptr"); + return nullptr; } - return 0; + return remoteObject; } + int32_t KvStoreDataServiceStub::GetAllKvStoreIdOnRemote(MessageParcel &data, MessageParcel &reply) { AppId appId = { Constant::TrimCopy(data.ReadString())}; @@ -646,6 +580,12 @@ int32_t KvStoreDataServiceStub::GetRdbServiceOnRemote(MessageParcel &data, Messa return 0; } +int32_t KvStoreDataServiceStub::GetKVdbServiceOnRemote(MessageParcel &data, MessageParcel &reply) +{ + reply.WriteRemoteObject(GetKVdbService()); + return 0; +} + int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp deleted file mode 100644 index 2acfdca1b..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright (c) 2021 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 "KvStoreClient" - -#include "kvstore_client.h" -#include "constant.h" -#include "dds_trace.h" -#include "kvstore_observer_client.h" -#include "kvstore_snapshot_client.h" -#include "log_print.h" - -namespace OHOS { -namespace DistributedKv { -KvStoreClient::KvStoreClient(sptr kvStoreProxy, const std::string &storeId) - : kvStoreProxy_(std::move(kvStoreProxy)), storeId_(storeId) -{ - ZLOGI("construct"); -} - -KvStoreClient::~KvStoreClient() -{ - ZLOGI("destruct"); -} - -StoreId KvStoreClient::GetStoreId() const -{ - return { storeId_ }; -} -Status KvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, - std::shared_ptr &snapshot) const -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - snapshot = nullptr; - if (kvStoreProxy_ == nullptr) { - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; - } - - sptr kvStoreObserverClient = new (std::nothrow) KvStoreObserverClient(GetStoreId(), - SubscribeType::SUBSCRIBE_TYPE_ALL, observer, KvStoreType::MULTI_VERSION); - if (kvStoreObserverClient == nullptr) { - ZLOGE("new kvStoreObserverClient failed"); - return Status::ERROR; - } - - sptr snapshotProxyTmp; - Status statusTmp = Status::SERVER_UNAVAILABLE; - auto snapshotCallbackFunction = [&](Status status, sptr snapshotProxy) { - statusTmp = status; - snapshotProxyTmp = snapshotProxy; - }; - kvStoreProxy_->GetKvStoreSnapshot(kvStoreObserverClient, snapshotCallbackFunction); - if (statusTmp != Status::SUCCESS) { - ZLOGE("return error: %d.", static_cast(statusTmp)); - return statusTmp; - } - - if (snapshotProxyTmp == nullptr) { - ZLOGE("snapshotProxyTmp is nullptr."); - return statusTmp; - } - - ZLOGD("success."); - snapshot = std::make_shared(std::move(snapshotProxyTmp)); - return statusTmp; -} - -Status KvStoreClient::ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (kvStoreProxy_ == nullptr) { - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; - } - if (snapshot == nullptr) { - ZLOGE("kvstoresnapshot is nullptr."); - return Status::INVALID_ARGUMENT; - } - - KvStoreSnapshotClient *kvStoreSnapshotClient = - reinterpret_cast(snapshot.get()); - sptr snapshotProxyTmp = kvStoreSnapshotClient->GetkvStoreSnapshotProxy(); - Status status = kvStoreProxy_->ReleaseKvStoreSnapshot(std::move(snapshotProxyTmp)); - snapshot = nullptr; - ZLOGI("return: %d.", static_cast(status)); - return status; -} - -Status KvStoreClient::Put(const Key &key, const Value &value) -{ - ZLOGD("key: %zu value: %zu.", key.Size(), value.Size()); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - std::vector keyData = Constant::TrimCopy>(key.Data()); - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH || - value.Size() > Constant::MAX_VALUE_LENGTH) { - ZLOGE("invalid key or value."); - return Status::INVALID_ARGUMENT; - } - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Put(key, value); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::PutBatch(const std::vector &entries) -{ - ZLOGI("entry size: %zu", entries.size()); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - if (entries.size() > Constant::MAX_BATCH_SIZE) { - ZLOGE("batch size must less than 128."); - return Status::INVALID_ARGUMENT; - } - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->PutBatch(entries); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::Delete(const Key &key) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - std::vector keyData = Constant::TrimCopy>(key.Data()); - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Delete(key); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::DeleteBatch(const std::vector &keys) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - if (keys.size() > Constant::MAX_BATCH_SIZE) { - ZLOGE("batch size must less than 128."); - return Status::INVALID_ARGUMENT; - } - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->DeleteBatch(keys); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::Clear() -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Clear(); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::StartTransaction() -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->StartTransaction(); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::Commit() -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Commit(); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::Rollback() -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Rollback(); - } - ZLOGE("kvstore proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -Status KvStoreClient::SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (observer == nullptr) { - ZLOGW("return INVALID_ARGUMENT."); - return Status::INVALID_ARGUMENT; - } - std::lock_guard lck(observerMapMutex_); - // change this to map.contains() after c++20 - if (registeredObservers_.count(observer.get()) == 1) { - ZLOGW("return STORE_ALREADY_SUBSCRIBE."); - return Status::STORE_ALREADY_SUBSCRIBE; - } - - // remove storeId after remove SubscribeKvStore function in manager. currently reserve for convenience. - sptr ipcObserver = - new (std::nothrow) KvStoreObserverClient(GetStoreId(), subscribeType, observer, KvStoreType::MULTI_VERSION); - if (ipcObserver == nullptr) { - ZLOGW("new KvStoreObserverClient failed"); - return Status::ERROR; - } - Status status = kvStoreProxy_->SubscribeKvStore(subscribeType, ipcObserver); - if (status == Status::SUCCESS) { - registeredObservers_.emplace(observer.get(), ipcObserver); - } - return status; -} - -Status KvStoreClient::UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (observer == nullptr) { - ZLOGW("return INVALID_ARGUMENT."); - return Status::INVALID_ARGUMENT; - } - std::lock_guard lck(observerMapMutex_); - auto it = registeredObservers_.find(observer.get()); - if (it == registeredObservers_.end()) { - ZLOGW("return STORE_NOT_SUBSCRIBE."); - return Status::STORE_NOT_SUBSCRIBE; - } - Status status = kvStoreProxy_->UnSubscribeKvStore(subscribeType, it->second); - if (status == Status::SUCCESS) { - registeredObservers_.erase(it); - } - return status; -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h deleted file mode 100644 index 2c8a0abc1..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2021 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 KVSTORE_CLIENT_H -#define KVSTORE_CLIENT_H - -#include -#include "ikvstore.h" -#include "kvstore.h" -#include "kvstore_service_death_notifier.h" -#include "kvstore_snapshot.h" -#include "types.h" - -namespace OHOS { -namespace DistributedKv { -class KvStoreClient final : public KvStore { -public: - explicit KvStoreClient(sptr kvStoreProxy, const std::string &storeId); - - ~KvStoreClient(); - - StoreId GetStoreId() const override; - - Status GetKvStoreSnapshot(std::shared_ptr observer, - std::shared_ptr &snapshot) const override; - - Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) override; - - Status Put(const Key &key, const Value &value) override; - - Status PutBatch(const std::vector &entries) override; - - Status Delete(const Key &key) override; - - Status DeleteBatch(const std::vector &keys) override; - - Status Clear() override; - - Status StartTransaction() override; - - Status Commit() override; - - Status Rollback() override; - - Status SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) override; - - Status UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) override; - -private: - sptr kvStoreProxy_; - std::map> registeredObservers_; - std::mutex observerMapMutex_; - std::string storeId_; -}; -} // namespace DistributedKv -} // namespace OHOS - -#endif // KVSTORE_CLIENT_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index ec5d41195..226ad26bc 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -529,20 +529,6 @@ Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId); } -Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, - std::shared_ptr &snapshot) const -{ - (void) observer; - (void) snapshot; - return Status::NOT_SUPPORT; -} - -Status SingleKvStoreClient::ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) -{ - (void) snapshot; - return Status::NOT_SUPPORT; -} - Status SingleKvStoreClient::Clear() { return Status::NOT_SUPPORT; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index fd0afa572..92f3d8130 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -93,9 +93,6 @@ public: Status SubscribeWithQuery(const std::vector &devices, const DataQuery &query) override; Status UnsubscribeWithQuery(const std::vector &devices, const DataQuery &query) override; - Status GetKvStoreSnapshot(std::shared_ptr observer, - std::shared_ptr &snapshot) const override; - Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) override; Status Clear() override; protected: diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 906b7d5c2..ff9467439 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -52,7 +52,6 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/device_status_change_listener_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/idevice_status_change_listener.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_client_death_observer.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp", @@ -61,7 +60,6 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_snapshot.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client_death_observer.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_resultset_client.cpp", diff --git a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h index 685114257..d2456a462 100644 --- a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h +++ b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h @@ -32,25 +32,6 @@ public: API_EXPORT ~DistributedKvDataManager(); - // Open kvstore instance with the given storeId, creating it if needed. - // It is allowed to open the same kvstore concurrently - // multiple times, but only one KvStoreImpl will be created. - // Parameters: - // options: the config of the kvstore, including encrypt, - // create if needed and whether need sync between devices. - // appId: the name of the application. - // :storeId: the name of the kvstore. - // callback: including status and KvStore instance returned by this call. - // callback will return: - // if Options.createIfMissing is false and kvstore has not been created before, - // STORE_NOT_FOUND and nullptr, - // if storeId is not valid, INVALID_ARGUMENT and nullptr, - // if appId has no permission, PERMISSION_DENIED and nullptr, - // otherwise, SUCCESS and the unipue_ptr of kvstore, which client can use to operate kvstore, will be returned. - [[deprecated]] - API_EXPORT Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::shared_ptr &kvStore); - // Open kvstore instance with the given storeId, creating it if needed. // It is allowed to open the same kvstore concurrently // multiple times, but only one KvStoreImpl will be created. diff --git a/interfaces/innerkits/distributeddata/include/kvstore.h b/interfaces/innerkits/distributeddata/include/kvstore.h index 54fdb54ad..92380d88e 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore.h +++ b/interfaces/innerkits/distributeddata/include/kvstore.h @@ -35,20 +35,6 @@ public: // Get kvstore name of this kvstore instance. virtual StoreId GetStoreId() const = 0; - // Creates a snapshot of the kvstore, allowing the client app to read a - // consistent data of the content of the kvstore. - // If observer is provided, it will receive notifications for changes of the - // kvstore newer than the resulting snapshot. - // observer: observer for subscribe. - // snapshot: [output] the KvStoreSnapshot instance. - [[deprecated]] - virtual Status GetKvStoreSnapshot(std::shared_ptr observer, - std::shared_ptr &snapshot) const = 0; - - // Release snapshot created by calling GetKvStoreSnapshot. - [[deprecated]] - virtual Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) = 0; - // Mutation operations. // Key level operations. // Mutations are bundled together into atomic commits. If a transaction is in @@ -79,6 +65,7 @@ public: // clear all entries in the kvstore. // after this call, IsClear function in ChangeNotification in subscription return true. + [[deprecated]] virtual Status Clear() = 0; // start transaction. diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index ad9d17231..63097769a 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -85,7 +85,6 @@ ohos_shared_library("distributeddataservice") { "src/kvstore_app_manager.cpp", "src/kvstore_data_service.cpp", "src/kvstore_device_listener.cpp", - "src/kvstore_impl.cpp", "src/kvstore_meta_manager.cpp", "src/kvstore_observer_impl.cpp", "src/kvstore_resultset_impl.cpp", diff --git a/services/distributeddataservice/app/src/kvstore_app_manager.cpp b/services/distributeddataservice/app/src/kvstore_app_manager.cpp index 10dc91b56..f2e3fc5b5 100644 --- a/services/distributeddataservice/app/src/kvstore_app_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_app_manager.cpp @@ -29,7 +29,6 @@ #include "constant.h" #include "device_kvstore_impl.h" #include "directory_utils.h" -#include "ikvstore.h" #include "kv_store_delegate.h" #include "kvstore_app_accessor.h" #include "kvstore_utils.h" @@ -54,9 +53,6 @@ KvStoreAppManager::KvStoreAppManager(const std::string &bundleName, pid_t uid, u KvStoreAppManager::~KvStoreAppManager() { ZLOGD("begin."); - stores_[PATH_DE].clear(); - stores_[PATH_CE].clear(); - { std::lock_guard guard(delegateMutex_); delete delegateManagers_[PATH_DE]; @@ -94,77 +90,6 @@ Status KvStoreAppManager::ConvertErrorStatus(DistributedDB::DBStatus dbStatus, b return Status::SUCCESS; } -Status KvStoreAppManager::GetKvStore(const Options &options, const StoreMetaData &metaData, - const std::vector &cipherKey, sptr &kvStore) -{ - ZLOGI("begin"); - kvStore = nullptr; - PathType type = ConvertPathType(metaData); - auto *delegateManager = GetDelegateManager(type); - if (delegateManager == nullptr) { - ZLOGE("delegateManagers[%d] is nullptr.", type); - return Status::ILLEGAL_STATE; - } - - if (!flowCtrl_.IsTokenEnough()) { - ZLOGE("flow control denied"); - return Status::EXCEED_MAX_ACCESS_RATE; - } - - std::lock_guard lg(storeMutex_); - auto it = stores_[type].find(metaData.storeId); - if (it != stores_[type].end()) { - kvStore = it->second; - ZLOGI("find store in map refcount: %d.", kvStore->GetSptrRefCount()); - kvStore->IncreaseOpenCount(); - return Status::SUCCESS; - } - - if ((GetTotalKvStoreNum()) >= static_cast(Constant::MAX_OPEN_KVSTORES)) { - ZLOGE("limit %d KvStores can be opened.", Constant::MAX_OPEN_KVSTORES); - return Status::ERROR; - } - - DistributedDB::KvStoreDelegate::Option dbOption; - auto status = InitDbOption(options, cipherKey, dbOption); - if (status != Status::SUCCESS) { - ZLOGE("InitDbOption failed."); - return status; - } - - DistributedDB::KvStoreDelegate *storeDelegate = nullptr; - DistributedDB::DBStatus dbStatusTmp; - delegateManager->GetKvStore(metaData.storeId, dbOption, - [&storeDelegate, &dbStatusTmp](DistributedDB::DBStatus dbStatus, DistributedDB::KvStoreDelegate *delegate) { - storeDelegate = delegate; - dbStatusTmp = dbStatus; - }); - - if (storeDelegate == nullptr) { - ZLOGE("storeDelegate is nullptr, status:%d.", static_cast(dbStatusTmp)); - return ConvertErrorStatus(dbStatusTmp, options.createIfMissing); - } - - ZLOGD("get delegate"); - kvStore = new (std::nothrow)KvStoreImpl(options, metaData.user, metaData.bundleName, - metaData.appId, metaData.storeId, GetDbDir(metaData), storeDelegate); - if (kvStore == nullptr) { - delegateManager->CloseKvStore(storeDelegate); - kvStore = nullptr; - return Status::ERROR; - } - auto result = stores_[type].emplace(metaData.storeId, kvStore); - if (!result.second) { - ZLOGE("emplace failed."); - delegateManager->CloseKvStore(storeDelegate); - kvStore = nullptr; - return Status::ERROR; - } - - ZLOGD("after emplace refcount: %d", kvStore->GetSptrRefCount()); - return Status::SUCCESS; -} - Status KvStoreAppManager::GetKvStore(const Options &options, const StoreMetaData &metaData, const std::vector &cipherKey, sptr &kvStore) { @@ -342,38 +267,6 @@ Status KvStoreAppManager::DeleteAllKvStore() return Status::SUCCESS; } -Status KvStoreAppManager::MigrateAllKvStore(const std::string &harmonyAccountId) -{ - ZLOGI("begin"); - std::lock_guard lg(storeMutex_); - userId_ = harmonyAccountId; - ZLOGI("path de migration begin."); - Status statusDE = MigrateAllKvStore(harmonyAccountId, PATH_DE); - ZLOGI("path ce migration begin."); - Status statusCE = MigrateAllKvStore(harmonyAccountId, PATH_CE); - return (statusCE != Status::SUCCESS) ? statusCE : statusDE; -} - -Status KvStoreAppManager::InitDbOption(const Options &options, const std::vector &cipherKey, - DistributedDB::KvStoreDelegate::Option &dbOption) -{ - DistributedDB::CipherPassword password; - auto status = password.SetValue(cipherKey.data(), cipherKey.size()); - if (status != DistributedDB::CipherPassword::ErrorCode::OK) { - ZLOGE("Failed to set the passwd."); - return Status::DB_ERROR; - } - dbOption.createIfNecessary = options.createIfMissing; - dbOption.localOnly = false; - dbOption.isEncryptedDb = options.encrypt; - if (options.encrypt) { - dbOption.cipher = DistributedDB::CipherType::AES_256_GCM; - dbOption.passwd = password; - } - dbOption.createDirByStoreIdOnly = options.dataOwnership; - return Status::SUCCESS; -} - Status KvStoreAppManager::InitNbDbOption(const Options &options, const std::vector &cipherKey, DistributedDB::KvStoreNbDelegate::Option &dbOption) { @@ -494,21 +387,6 @@ Status KvStoreAppManager::CloseKvStore(const std::string &storeId, PathType type return Status::ILLEGAL_STATE; } - auto it = stores_[type].find(storeId); - if (it != stores_[type].end()) { - ZLOGD("find store and close delegate."); - InnerStatus status = it->second->Close(delegateManager); - if (status == InnerStatus::SUCCESS) { - stores_[type].erase(it); - return Status::SUCCESS; - } - if (status == InnerStatus::DECREASE_REFCOUNT) { - return Status::SUCCESS; - } - ZLOGE("delegate close error: %d.", static_cast(status)); - return Status::DB_ERROR; - } - auto itSingle = singleStores_[type].find(storeId); if (itSingle != singleStores_[type].end()) { ZLOGD("find single store and close delegate."); @@ -535,17 +413,6 @@ Status KvStoreAppManager::CloseAllKvStore(PathType type) return Status::ILLEGAL_STATE; } - for (auto it = stores_[type].begin(); it != stores_[type].end(); it = stores_[type].erase(it)) { - KvStoreImpl *currentStore = it->second.GetRefPtr(); - ZLOGI("close kvstore, refcount %d.", it->second->GetSptrRefCount()); - Status status = currentStore->ForceClose(delegateManager); - if (status != Status::SUCCESS) { - ZLOGE("delegate close error: %d.", static_cast(status)); - return Status::DB_ERROR; - } - } - stores_[type].clear(); - for (auto it = singleStores_[type].begin(); it != singleStores_[type].end(); it = singleStores_[type].erase(it)) { SingleKvStoreImpl *currentStore = it->second.GetRefPtr(); ZLOGI("close kvstore, refcount %d.", it->second->GetSptrRefCount()); @@ -567,15 +434,6 @@ Status KvStoreAppManager::DeleteKvStore(const std::string &storeId, PathType typ return Status::ILLEGAL_STATE; } std::lock_guard lg(storeMutex_); - auto it = stores_[type].find(storeId); - if (it != stores_[type].end()) { - Status status = it->second->ForceClose(delegateManager); - if (status != Status::SUCCESS) { - return Status::DB_ERROR; - } - stores_[type].erase(it); - } - auto itSingle = singleStores_[type].find(storeId); if (itSingle != singleStores_[type].end()) { Status status = itSingle->second->ForceClose(delegateManager); @@ -586,7 +444,7 @@ Status KvStoreAppManager::DeleteKvStore(const std::string &storeId, PathType typ } DistributedDB::DBStatus status = delegateManager->DeleteKvStore(storeId); - if (singleStores_[type].empty() && stores_[type].empty()) { + if (singleStores_[type].empty()) { SwitchDelegateManager(type, nullptr); delete delegateManager; } @@ -601,24 +459,6 @@ Status KvStoreAppManager::DeleteAllKvStore(PathType type) return Status::ILLEGAL_STATE; } - for (auto it = stores_[type].begin(); it != stores_[type].end(); it = stores_[type].erase(it)) { - std::string storeId = it->first; - KvStoreImpl *currentStore = it->second.GetRefPtr(); - Status status = currentStore->ForceClose(delegateManager); - if (status != Status::SUCCESS) { - ZLOGE("delegate delete close failed error: %d.", static_cast(status)); - return Status::DB_ERROR; - } - - ZLOGI("delete kvstore, refcount %d.", it->second->GetSptrRefCount()); - DistributedDB::DBStatus dbStatus = delegateManager->DeleteKvStore(storeId); - if (dbStatus != DistributedDB::DBStatus::OK) { - ZLOGE("delegate delete error: %d.", static_cast(dbStatus)); - return Status::DB_ERROR; - } - } - stores_[type].clear(); - for (auto it = singleStores_[type].begin(); it != singleStores_[type].end(); it = singleStores_[type].erase(it)) { std::string storeId = it->first; SingleKvStoreImpl *currentStore = it->second.GetRefPtr(); @@ -641,49 +481,9 @@ Status KvStoreAppManager::DeleteAllKvStore(PathType type) return Status::SUCCESS; } -Status KvStoreAppManager::MigrateAllKvStore(const std::string &harmonyAccountId, PathType type) -{ - auto *delegateManager = GetDelegateManager(type); - if (delegateManager == nullptr) { - ZLOGE("delegateManager is nullptr."); - return Status::ILLEGAL_STATE; - } - - std::string dirPath = GetDataStoragePath(deviceAccountId_, bundleName_, type); - DistributedDB::KvStoreDelegateManager *newDelegateManager = nullptr; - Status status = Status::SUCCESS; - ZLOGI("KvStore migration begin."); - for (auto &it : stores_[type]) { - sptr impl = it.second; - if (impl->MigrateKvStore(harmonyAccountId, dirPath, delegateManager, newDelegateManager) != Status::SUCCESS) { - status = Status::MIGRATION_KVSTORE_FAILED; - ZLOGE("migrate kvstore for appId-%s failed.", bundleName_.c_str()); - // skip this failed, continue to migrate other kvstore. - } - } - - ZLOGI("SingleKvStore migration begin."); - for (auto &it : singleStores_[type]) { - sptr impl = it.second; - if (impl->MigrateKvStore(harmonyAccountId, dirPath, delegateManager, newDelegateManager) != Status::SUCCESS) { - status = Status::MIGRATION_KVSTORE_FAILED; - ZLOGE("migrate single kvstore for appId-%s failed.", bundleName_.c_str()); - // skip this failed, continue to migrate other kvstore. - } - } - - if (newDelegateManager != nullptr) { - delegateManager = SwitchDelegateManager(type, newDelegateManager); - delete delegateManager; - } - return status; -} - size_t KvStoreAppManager::GetTotalKvStoreNum() const { - size_t total = stores_[PATH_DE].size(); - total += stores_[PATH_CE].size(); - total += singleStores_[PATH_DE].size(); + size_t total = singleStores_[PATH_DE].size(); total += singleStores_[PATH_CE].size(); return int(total); }; diff --git a/services/distributeddataservice/app/src/kvstore_app_manager.h b/services/distributeddataservice/app/src/kvstore_app_manager.h index 4900b0a1f..f1495629a 100644 --- a/services/distributeddataservice/app/src/kvstore_app_manager.h +++ b/services/distributeddataservice/app/src/kvstore_app_manager.h @@ -21,7 +21,6 @@ #include #include "flowctrl_manager/kvstore_flowctrl_manager.h" #include "kv_store_delegate_manager.h" -#include "kvstore_impl.h" #include "kv_store_nb_delegate.h" #include "kvstore_meta_manager.h" #include "metadata/store_meta_data.h" @@ -44,9 +43,6 @@ public: virtual ~KvStoreAppManager(); - Status GetKvStore(const Options &options, const StoreMetaData &metaData, const std::vector &cipherKey, - sptr &kvStore); - Status GetKvStore(const Options &options, const StoreMetaData &metaData, const std::vector &cipherKey, sptr &kvStore); @@ -58,11 +54,6 @@ public: Status DeleteAllKvStore(); - Status MigrateAllKvStore(const std::string &harmonyAccountId); - - static Status InitDbOption(const Options &options, const std::vector &cipherKey, - DistributedDB::KvStoreDelegate::Option &dbOption); - static Status InitNbDbOption(const Options &options, const std::vector &cipherKey, DistributedDB::KvStoreNbDelegate::Option &dbOption); @@ -94,7 +85,6 @@ private: Status DeleteAllKvStore(PathType type); Status MigrateAllKvStore(const std::string &harmonyAccountId, PathType type); std::mutex storeMutex_ {}; - std::map> stores_[PATH_TYPE_MAX] {}; std::map> singleStores_[PATH_TYPE_MAX] {}; std::string userId_ {}; std::string bundleName_ {}; diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 665d89b13..9384644a2 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -137,18 +137,6 @@ void KvStoreDataService::Initialize() deviceInnerListener_.get(), { "innerListener" }); } -Status KvStoreDataService::GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - if (!appId.IsValid() || !storeId.IsValid() || options.kvStoreType != KvStoreType::MULTI_VERSION) { - ZLOGE("invalid argument type."); - return Status::INVALID_ARGUMENT; - } - return Status::NOT_SUPPORT; -} - Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::function)> callback) { @@ -730,7 +718,6 @@ void KvStoreDataService::OnStart() return; } } - CreateRdbService(); StartService(); } @@ -1067,11 +1054,11 @@ void KvStoreDataService::AccountEventChanged(const AccountEventInfo &eventInfo) } std::initializer_list dirList = {Constant::ROOT_PATH_DE, "/", Constant::SERVICE_NAME, "/", eventInfo.deviceAccountId}; - std::string deviceAccountKvStoreDataDir = Constant::Concatenate(dirList); - ForceRemoveDirectory(deviceAccountKvStoreDataDir); + std::string userDir = Constant::Concatenate(dirList); + ForceRemoveDirectory(userDir); dirList = {Constant::ROOT_PATH_CE, "/", Constant::SERVICE_NAME, "/", eventInfo.deviceAccountId}; - deviceAccountKvStoreDataDir = Constant::Concatenate(dirList); - ForceRemoveDirectory(deviceAccountKvStoreDataDir); + userDir = Constant::Concatenate(dirList); + ForceRemoveDirectory(userDir); g_kvStoreAccountEventStatus = 0; break; } @@ -1198,17 +1185,21 @@ bool KvStoreDataService::CheckSyncActivation( return true; } -void KvStoreDataService::CreateRdbService() +sptr KvStoreDataService::GetRdbService() { - rdbService_ = new(std::nothrow) DistributedRdb::RdbServiceImpl(); - if (rdbService_ != nullptr) { - ZLOGI("create rdb service success"); + if (rdbService_ == nullptr) { + std::lock_guard lockGuard(mutex_); + if (rdbService_ == nullptr) { + rdbService_ = new (std::nothrow) DistributedRdb::RdbServiceImpl(); + } + return rdbService_ == nullptr ? nullptr : rdbService_->AsObject().GetRefPtr(); } + return rdbService_->AsObject().GetRefPtr(); } -sptr KvStoreDataService::GetRdbService() +sptr KvStoreDataService::GetKVdbService() { - return rdbService_->AsObject().GetRefPtr(); + return sptr(); } bool DbMetaCallbackDelegateMgr::GetKvStoreDiskSize(const std::string &storeId, uint64_t &size) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 982d3c7c3..711602674 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -26,7 +26,6 @@ #include "device_change_listener_impl.h" #include "ikvstore_data_service.h" #include "kvstore_device_listener.h" -#include "kvstore_impl.h" #include "kvstore_user_manager.h" #include "metadata/store_meta_data.h" #include "reporter.h" @@ -54,9 +53,6 @@ public: explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); virtual ~KvStoreDataService(); - Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) override; - Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, std::function)> callback) override; @@ -77,7 +73,7 @@ public: Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy) override; Status StopWatchDeviceChange(sptr observer) override; sptr GetRdbService() override; - + sptr GetKVdbService() override; void OnDump() override; int Dump(int fd, const std::vector &args) override; @@ -164,7 +160,6 @@ private: bool CheckSyncActivation(const std::string &userId, const std::string &appId, const std::string &storeId); bool CheckOptions(const Options &options, const std::vector &metaKey) const; - void CreateRdbService(); bool IsStoreOpened(const std::string &userId, const std::string &appId, const std::string &storeId); static Status FillStoreParam( const Options &options, const AppId &appId, const StoreId &storeId, StoreMetaData &metaData); @@ -182,7 +177,9 @@ private: std::shared_ptr deviceListener_; std::shared_ptr security_; + std::mutex mutex_; sptr rdbService_; + sptr kvdbService_; std::shared_ptr deviceInnerListener_; }; diff --git a/services/distributeddataservice/app/src/kvstore_impl.cpp b/services/distributeddataservice/app/src/kvstore_impl.cpp deleted file mode 100644 index 1509a2add..000000000 --- a/services/distributeddataservice/app/src/kvstore_impl.cpp +++ /dev/null @@ -1,794 +0,0 @@ -/* - * Copyright (c) 2021 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 "KvStoreImpl" - -#include "kvstore_impl.h" -#include -#include -#include -#include "backup_handler.h" -#include "checker/checker_manager.h" -#include "constant.h" -#include "dds_trace.h" -#include "device_kvstore_impl.h" -#include "kvstore_account_observer.h" -#include "kvstore_data_service.h" -#include "kvstore_meta_manager.h" -#include "kvstore_utils.h" -#include "log_print.h" -#include "metadata/meta_data_manager.h" -#include "permission_validator.h" -#include "reporter.h" - -namespace OHOS { -namespace DistributedKv { -using namespace DistributedData; -KvStoreImpl::KvStoreImpl(const Options &options, const std::string &userId, const std::string &bundleName, - const std::string &appId, const std::string &storeId, const std::string &directory, - DistributedDB::KvStoreDelegate *delegate) - : options_(options), deviceAccountId_(userId), bundleName_(bundleName), storeId_(storeId), appId_(appId), - storePath_(Constant::Concatenate({ directory, storeId })), kvStoreDelegate_(delegate), storeObserverMutex_(), - observerSet_(), openCount_(1) -{ - ZLOGI("construct"); -} - -std::string KvStoreImpl::GetStoreId() -{ - return storeId_; -} - -void KvStoreImpl::GetKvStoreSnapshot(sptr observer, - std::function)> callback) -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - callback(Status::DB_ERROR, nullptr); - return; - } - - DistributedDB::KvStoreSnapshotDelegate *retSnapshotKvStore = nullptr; - DistributedDB::DBStatus retSnapshotStatus; - auto snapshotCallbackFunction = [&](DistributedDB::DBStatus status, - DistributedDB::KvStoreSnapshotDelegate *snapshot) { - retSnapshotStatus = status; - retSnapshotKvStore = snapshot; - }; - std::lock_guard lg(storeSnapshotMutex_); - KvStoreObserverImpl *kvStoreObserverImpl = nullptr; - if (observer == nullptr) { - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreDelegate_->GetKvStoreSnapshot(nullptr, snapshotCallbackFunction); - } - } else { - kvStoreObserverImpl = - new (std::nothrow) KvStoreObserverImpl(SubscribeType::SUBSCRIBE_TYPE_ALL, observer); - if (kvStoreObserverImpl == nullptr) { - ZLOGW("new KvStoreObserverImpl failed"); - callback(Status::ERROR, nullptr); - return; - } - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreDelegate_->GetKvStoreSnapshot(kvStoreObserverImpl, snapshotCallbackFunction); - } - } - - if (retSnapshotStatus != DistributedDB::DBStatus::OK || retSnapshotKvStore == nullptr) { - ZLOGE("delegate return nullptr."); - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - if (retSnapshotStatus == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGE("GetKvStoreSnapshot failed, distributeddb need recover."); - IMPORT_DATABASE(bundleName_); - } else { - callback(Status::DB_ERROR, nullptr); - } - return; - } - - ZLOGD("get delegate"); - KvStoreSnapshotImpl *snapshot = new (std::nothrow) KvStoreSnapshotImpl(retSnapshotKvStore, kvStoreObserverImpl); - if (snapshot == nullptr) { - ZLOGW("new KvStoreSnapshotImpl failed"); - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - callback(Status::ERROR, nullptr); - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreDelegate_->ReleaseKvStoreSnapshot(retSnapshotKvStore); - } - return; - } - sptr kvStoreSnapshotImpl = snapshot; - callback(Status::SUCCESS, kvStoreSnapshotImpl); - snapshotMap_.emplace(snapshot, std::move(kvStoreSnapshotImpl)); - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); -} - -Status KvStoreImpl::ReleaseKvStoreSnapshot(sptr iKvStoreSnapshot) -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is nullptr."); - return Status::DB_ERROR; - } - - if (iKvStoreSnapshot == nullptr) { - ZLOGE("snapshot is nullptr."); - return Status::ERROR; - } - - std::lock_guard lg(storeSnapshotMutex_); - Status status = static_cast(iKvStoreSnapshot.GetRefPtr())->Release(kvStoreDelegate_); - if (status == Status::SUCCESS) { - auto it = snapshotMap_.find(static_cast(iKvStoreSnapshot.GetRefPtr())); - if (it != snapshotMap_.end()) { - snapshotMap_.erase(it); - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - } else { - FaultMsg msg = {FaultType::RUNTIME_FAULT, "user", __FUNCTION__, Fault::RF_RELEASE_SNAPSHOT}; - Reporter::GetInstance()->ServiceFault()->Report(msg); - } - return status; -} - -Status KvStoreImpl::Put(const Key &key, const Value &value) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::vector keyData = Constant::TrimCopy>(key.Data()); - - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - DistributedDB::Key dbKey = keyData; - DistributedDB::Value dbValue = value.Data(); - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->Put(dbKey, dbValue); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("Put failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate put failed."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::PutBatch(const std::vector &entries) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - // temporary transform. - std::vector dbEntries; - for (auto &entry : entries) { - DistributedDB::Entry dbEntry; - - std::vector keyData = Constant::TrimCopy>(entry.key.Data()); - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - - dbEntry.key = keyData; - dbEntry.value = entry.value.Data(); - dbEntries.push_back(dbEntry); - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->PutBatch(dbEntries); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("PutBatch failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate PutBatch failed."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::Delete(const Key &key) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::vector keyData = Constant::TrimCopy>(key.Data()); - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - DistributedDB::Key dbKey = keyData; - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->Delete(dbKey); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("Delete failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate Delete failed."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::DeleteBatch(const std::vector &keys) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - // temporary transform. - std::vector dbKeys; - for (auto &key : keys) { - std::vector keyData = Constant::TrimCopy>(key.Data()); - if (keyData.size() == 0 || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - - DistributedDB::Key keyTmp = keyData; - dbKeys.push_back(keyTmp); - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->DeleteBatch(dbKeys); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("DeleteBatch failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate DeleteBatch failed."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::Clear() -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->Clear(); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("Clear failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate Clear failed."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::StartTransaction() -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->StartTransaction(); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("StartTransaction failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::Commit() -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->Commit(); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("Commit failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -Status KvStoreImpl::Rollback() -{ - ZLOGI("begin."); - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - DistributedDB::DBStatus status; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - status = kvStoreDelegate_->Rollback(); - } - if (status == DistributedDB::DBStatus::INVALID_PASSWD_OR_CORRUPTED_DB) { - ZLOGI("Rollback failed, distributeddb need recover."); - return IMPORT_DATABASE(bundleName_); - } - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error."); - return Status::DB_ERROR; - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -InnerStatus KvStoreImpl::Close(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGW("start Close"); - if (openCount_ > 1) { - openCount_--; - return InnerStatus::DECREASE_REFCOUNT; - } - Status status = ForceClose(kvStoreDelegateManager); - if (status == Status::SUCCESS) { - return InnerStatus::SUCCESS; - } - return InnerStatus::ERROR; -} - -Status KvStoreImpl::ForceClose(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGI("start ForceClose, current openCount is %d.", openCount_); - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr || kvStoreDelegateManager == nullptr) { - ZLOGW("close got nullptr"); - return Status::INVALID_ARGUMENT; - } - ZLOGI("ForceClose start to clean observer"); - std::lock_guard observerSetLockGuard(storeObserverMutex_); - for (auto observer = observerSet_.begin(); observer != observerSet_.end();) { - DistributedDB::DBStatus dbStatus = kvStoreDelegate_->UnRegisterObserver(*observer); - if (dbStatus == DistributedDB::DBStatus::OK) { - delete *observer; - observer = observerSet_.erase(observer); - } else { - ZLOGW("Force close kvstore failed during UnRegisterObserver, status %d.", dbStatus); - return Status::ERROR; - } - } - ZLOGI("ForceClose start to clean snapshot"); - std::lock_guard snapshotMapLockGuard(storeSnapshotMutex_); - for (auto snapshotPair = snapshotMap_.begin(); snapshotPair != snapshotMap_.end();) { - auto *snapshotImpl = static_cast(snapshotPair->second.GetRefPtr()); - if (snapshotImpl != nullptr) { - auto status = snapshotImpl->Release(kvStoreDelegate_); - if (status != Status::SUCCESS) { - ZLOGW("Force close kvstore failed during release snapshot, errCode %d", status); - return status; - } - } - snapshotPair = snapshotMap_.erase(snapshotPair); - } - DistributedDB::DBStatus status = kvStoreDelegateManager->CloseKvStore(kvStoreDelegate_); - if (status == DistributedDB::DBStatus::OK) { - kvStoreDelegate_ = nullptr; - ZLOGI("end ForceClose."); - return Status::SUCCESS; - } - ZLOGI("ForceClose close failed with error code %d.", status); - return Status::ERROR; -} - -Status KvStoreImpl::MigrateKvStore(const std::string &harmonyAccountId, - const std::string &kvStoreDataDir, - DistributedDB::KvStoreDelegateManager *oldDelegateMgr, - DistributedDB::KvStoreDelegateManager *&newDelegateMgr) -{ - ZLOGI("begin."); - std::unique_lock lock(storeDelegateMutex_); - if (oldDelegateMgr == nullptr) { - ZLOGW("kvStore delegate manager is nullptr."); - return Status::INVALID_ARGUMENT; - } - - ZLOGI("create new KvStore."); - std::vector secretKey; // expected get secret key from meta kvstore successful when encrypt flag is true. - std::unique_ptr, void(*)(std::vector*)> cleanGuard( - &secretKey, [](std::vector *ptr) { ptr->assign(ptr->size(), 0); }); - auto metaSecretKey = KvStoreMetaManager::GetMetaKey(deviceAccountId_, "default", bundleName_, storeId_, "KEY"); - if (options_.encrypt) { - bool outdated = false; // ignore outdated flag during rebuild kvstore. - KvStoreMetaManager::GetInstance().GetSecretKeyFromMeta(metaSecretKey, secretKey, outdated); - if (secretKey.empty()) { - ZLOGE("Get secret key from meta kvstore failed."); - return Status::CRYPT_ERROR; - } - } - - DistributedDB::KvStoreDelegate::Option dbOption; - Status status = KvStoreAppManager::InitDbOption(options_, secretKey, dbOption); - if (status != Status::SUCCESS) { - ZLOGE("InitDbOption failed."); - return status; - } - if (newDelegateMgr == nullptr) { - if (appId_.empty()) { - ZLOGE("Get appId by bundle name failed."); - return Status::MIGRATION_KVSTORE_FAILED; - } - newDelegateMgr = new (std::nothrow) DistributedDB::KvStoreDelegateManager(appId_, harmonyAccountId); - if (newDelegateMgr == nullptr) { - ZLOGE("new KvStoreDelegateManager failed."); - return Status::MIGRATION_KVSTORE_FAILED; - } - DistributedDB::KvStoreConfig kvStoreConfig {kvStoreDataDir}; - newDelegateMgr->SetKvStoreConfig(kvStoreConfig); - } - - DistributedDB::DBStatus dbStatus = DistributedDB::DBStatus::OK; - DistributedDB::KvStoreDelegate *kvStoreDelegate = nullptr; // new KvStoreDelegate get from distributed DB. - newDelegateMgr->GetKvStore(storeId_, dbOption, - [&](DistributedDB::DBStatus result, DistributedDB::KvStoreDelegate *delegate) { - kvStoreDelegate = delegate; - dbStatus = result; - }); - - if (kvStoreDelegate == nullptr) { - ZLOGE("storeDelegate is nullptr, dbStatusTmp: %d", static_cast(dbStatus)); - return Status::DB_ERROR; - } - - status = RebuildKvStoreObserver(kvStoreDelegate); - if (status != Status::SUCCESS) { - ZLOGI("rebuild KvStore observer failed, errCode %d.", static_cast(status)); - // skip this failed, continue to do other rebuild process. - } - - status = RebuildKvStoreSnapshot(kvStoreDelegate); - if (status != Status::SUCCESS) { - ZLOGI("rebuild KvStore snapshot failed, errCode %d.", static_cast(status)); - // skip this failed, continue to do close kvstore process. - } - - ZLOGI("close old KvStore."); - dbStatus = oldDelegateMgr->CloseKvStore(kvStoreDelegate_); - if (dbStatus != DistributedDB::DBStatus::OK) { - ZLOGI("rebuild KvStore failed during close KvStore, errCode %d.", static_cast(dbStatus)); - newDelegateMgr->CloseKvStore(kvStoreDelegate); - return Status::DB_ERROR; - } - - ZLOGI("update kvstore delegate."); - kvStoreDelegate_ = kvStoreDelegate; - return Status::SUCCESS; -} - -Status KvStoreImpl::RebuildKvStoreObserver(DistributedDB::KvStoreDelegate *kvStoreDelegate) -{ - ZLOGI("rebuild observer."); - if (kvStoreDelegate_ == nullptr || kvStoreDelegate == nullptr) { - return Status::INVALID_ARGUMENT; - } - std::lock_guard observerSetLockGuard(storeObserverMutex_); - Status status = Status::SUCCESS; - DistributedDB::DBStatus dbStatus; - for (const auto observer : observerSet_) { - dbStatus = kvStoreDelegate_->UnRegisterObserver(observer); - if (dbStatus != DistributedDB::OK) { - status = Status::DB_ERROR; - ZLOGW("rebuild observer failed during UnRegisterObserver, status %d.", static_cast(dbStatus)); - continue; - } - - dbStatus = kvStoreDelegate->RegisterObserver(observer); - if (dbStatus != DistributedDB::OK) { - status = Status::DB_ERROR; - ZLOGW("rebuild observer failed during RegisterObserver, status %d.", static_cast(dbStatus)); - continue; - } - } - return status; -} - -Status KvStoreImpl::RebuildKvStoreSnapshot(DistributedDB::KvStoreDelegate *kvStoreDelegate) -{ - ZLOGI("rebuild snapshot."); - if (kvStoreDelegate_ == nullptr || kvStoreDelegate == nullptr) { - return Status::INVALID_ARGUMENT; - } - std::lock_guard snapshotMapLockGuard(storeSnapshotMutex_); - Status retStatus = Status::SUCCESS; - for (const auto &snapshotPair : snapshotMap_) { - auto *snapshot = static_cast(snapshotPair.second.GetRefPtr()); - if (snapshot == nullptr) { - continue; - } - Status status = snapshot->Release(kvStoreDelegate_); - if (status != Status::SUCCESS) { - retStatus = status; - ZLOGW("rebuild snapshot failed during release snapshot, errCode %d", static_cast(status)); - continue; - } - - status = snapshot->MigrateKvStore(kvStoreDelegate); - if (status != Status::SUCCESS) { - retStatus = status; - ZLOGW("rebuild snapshot failed during migrate snapshot, errCode %d", static_cast(status)); - continue; - } - } - return retStatus; -} - -void KvStoreImpl::IncreaseOpenCount() -{ - openCount_++; -} - -/* subscribe kv store */ -Status KvStoreImpl::SubscribeKvStore(const SubscribeType subscribeType, sptr observer) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGI("begin."); - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - if (observer == nullptr) { - ZLOGE("observer is nullptr."); - return Status::INVALID_ARGUMENT; - } - - std::lock_guard lg(storeObserverMutex_); - KvStoreObserverImpl *kvStoreObserverImpl = new (std::nothrow) KvStoreObserverImpl(subscribeType, observer); - if (kvStoreObserverImpl == nullptr) { - ZLOGE("kvStoreObserverImpl is nullptr."); - return Status::ERROR; - } - - if (observerSet_.find(kvStoreObserverImpl) != observerSet_.end()) { - ZLOGI("already subscribed."); - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - return Status::STORE_ALREADY_SUBSCRIBE; - } - - DistributedDB::DBStatus status = kvStoreDelegate_->RegisterObserver(kvStoreObserverImpl); - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error."); - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - return Status::DB_ERROR; - } - - auto it = observerSet_.insert(kvStoreObserverImpl); - ZLOGI("set size: %zu.", observerSet_.size()); - if (!(it.second)) { - status = kvStoreDelegate_->UnRegisterObserver(kvStoreObserverImpl); - ZLOGI("insert failed set size: %zu status: %d.", observerSet_.size(), static_cast(status)); - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - return Status::STORE_ALREADY_SUBSCRIBE; - } else { - ZLOGI("insert success set size: %zu.", observerSet_.size()); - } - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - return Status::SUCCESS; -} - -/* unsubscribe kv store */ -Status KvStoreImpl::UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGI("begin."); - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); - Status status = Status::DB_ERROR; - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return status; - } - - if (observer == nullptr) { - ZLOGE("observer is nullptr."); - return Status::INVALID_ARGUMENT; - } - - std::lock_guard lg(storeObserverMutex_); - KvStoreObserverImpl *kvStoreObserverImpl = new (std::nothrow) KvStoreObserverImpl(subscribeType, observer); - if (kvStoreObserverImpl == nullptr) { - ZLOGE("kvStoreObserverImpl is nullptr."); - return Status::ERROR; - } - - ZLOGI("set size: %zu.", observerSet_.size()); - auto it = observerSet_.find(kvStoreObserverImpl); - if (it != observerSet_.end()) { - DistributedDB::DBStatus dbStatus; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - dbStatus = kvStoreDelegate_->UnRegisterObserver(*it); - } - if (dbStatus == DistributedDB::DBStatus::OK) { - delete *it; - observerSet_.erase(it); - status = Status::SUCCESS; - } - ZLOGE("delegate return status: %d.", static_cast(dbStatus)); - } else { - ZLOGW("No existing observer to unsubscribe. Return success."); - status = Status::SUCCESS; - } - - delete kvStoreObserverImpl; - kvStoreObserverImpl = nullptr; - Reporter::GetInstance()->VisitStatistic()->Report({bundleName_, __FUNCTION__}); - ZLOGI("return status: %d", static_cast(status)); - return status; -} - -Status KvStoreImpl::ReKey(const std::vector &key) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGI("begin"); - std::shared_lock lock(storeDelegateMutex_); - if (kvStoreDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - DistributedDB::CipherPassword password; - auto status = password.SetValue(key.data(), key.size()); - if (status != DistributedDB::CipherPassword::ErrorCode::OK) { - ZLOGE("Failed to set the passwd."); - return Status::DB_ERROR; - } - DistributedDB::DBStatus dbStatus; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - dbStatus = kvStoreDelegate_->Rekey(password); - } - if (dbStatus == DistributedDB::DBStatus::OK) { - ZLOGI("succeed"); - return Status::SUCCESS; - } - return Status::ERROR; -} - -const std::string KvStoreImpl::GetStorePath() -{ - return storePath_; -} - -KvStoreImpl::~KvStoreImpl() -{ - ZLOGI("destruct"); -} - -bool KvStoreImpl::Import(const std::string &bundleName) const -{ - ZLOGI("KvStoreImpl Import start"); - StoreMetaData metaData; - metaData.user = deviceAccountId_; - metaData.bundleName = bundleName; - metaData.storeId = storeId_; - metaData.deviceId = DeviceKvStoreImpl::GetLocalDeviceId(); - MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); - std::shared_lock lock(storeDelegateMutex_); - return std::make_unique()->MultiKvStoreRecover(metaData, kvStoreDelegate_); -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_impl.h b/services/distributeddataservice/app/src/kvstore_impl.h deleted file mode 100644 index 8ff6bf311..000000000 --- a/services/distributeddataservice/app/src/kvstore_impl.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (c) 2021 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 KVSTORE_IMPL_H -#define KVSTORE_IMPL_H - -#include -#include -#include -#include -#include "ikvstore.h" -#include "ikvstore_observer.h" -#include "ikvstore_snapshot.h" -#include "kv_store_delegate.h" -#include "kv_store_delegate_manager.h" -#include "kvstore_observer_impl.h" -#include "kvstore_snapshot_impl.h" -#include "types.h" -#include "inner_types.h" - -namespace OHOS { -namespace DistributedKv { -#define IMPORT_DATABASE(bundleName) (Import(bundleName) ? Status::RECOVER_SUCCESS : Status::RECOVER_FAILED) - -struct KvStoreObserverImplPtrCompare { - bool operator()(const KvStoreObserverImpl *lhs, const KvStoreObserverImpl *rhs) const - { - if (lhs == rhs || rhs == nullptr) { - return false; - } - if (lhs == nullptr) { - return true; - } - return lhs->GetKvStoreObserverProxy()->AsObject().GetRefPtr() < - rhs->GetKvStoreObserverProxy()->AsObject().GetRefPtr(); - } -}; - -class KvStoreImpl : public KvStoreImplStub { -public: - KvStoreImpl(const Options &options, const std::string &userId, const std::string &bundleName, - const std::string &appId, const std::string &storeId, const std::string &directory, - DistributedDB::KvStoreDelegate *delegate); - - std::string GetStoreId(); - - void GetKvStoreSnapshot(sptr observer, - std::function)> callback) override; - - Status ReleaseKvStoreSnapshot(sptr iKvStoreSnapshot) override; - - Status Put(const Key &key, const Value &value) override; - - Status PutBatch(const std::vector &entries) override; - - Status Delete(const Key &key) override; - - Status DeleteBatch(const std::vector &keys) override; - - Status Clear() override; - - Status StartTransaction() override; - - Status Commit() override; - - Status Rollback() override; - - /* subscribe kv store */ - Status SubscribeKvStore(const SubscribeType subscribeType, sptr observer) override; - - /* unsubscribe kv store */ - Status UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer) override; - - virtual const std::string GetStorePath(); - - virtual ~KvStoreImpl(); - - InnerStatus Close(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager); - - Status ForceClose(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager); - - Status MigrateKvStore(const std::string &harmonyAccountId, - const std::string &kvStoreDataDir, - DistributedDB::KvStoreDelegateManager *oldDelegateMgr, - DistributedDB::KvStoreDelegateManager *&newDelegateMgr); - - void IncreaseOpenCount(); - - Status ReKey(const std::vector &key); - - bool Import(const std::string &bundleName) const; - -private: - Status RebuildKvStoreObserver(DistributedDB::KvStoreDelegate *kvStoreDelegate); - - Status RebuildKvStoreSnapshot(DistributedDB::KvStoreDelegate *kvStoreDelegate); - - // kvstore options - const Options options_; - - // device account id - std::string deviceAccountId_; - - // appId get from PMS. - const std::string bundleName_; - - // kvstore name. - const std::string storeId_; - - const std::string appId_; - - // kvstore absolute path in distributeddatamgr. - const std::string storePath_; - - // distributeddb is responsible for free kvStoreDelegate_, - // by calling CloseKvStore in KvStoreAppManager, - // can not free it in KvStoreImpl's destructor. - mutable std::shared_mutex storeDelegateMutex_ {}; - DistributedDB::KvStoreDelegate *kvStoreDelegate_; - std::mutex storeObserverMutex_; - std::set observerSet_; - std::mutex storeSnapshotMutex_; - std::map> snapshotMap_; - int openCount_; -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // KVSTORE_IMPL_H diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index aab0ff386..80f2fab4a 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -692,21 +692,6 @@ Status KvStoreMetaManager::RecoverSecretKeyFromFile(const std::string &secretKey return Status::SUCCESS; } -void KvStoreMetaManager::ReKey(const std::string &userId, const std::string &bundleName, const std::string &storeId, - int32_t pathType, sptr store) -{ - if (store == nullptr) { - return; - } - std::vector key = Crypto::Random(KEY_SIZE); - WriteSecretKeyToMeta(GetMetaKey(userId, "default", bundleName, storeId, "KEY"), key); - Status status = store->ReKey(key); - if (status == Status::SUCCESS) { - WriteSecretKeyToFile(GetSecretKeyFile(userId, bundleName, storeId, pathType), key); - } - key.assign(key.size(), 0); -} - void KvStoreMetaManager::ReKey(const std::string &userId, const std::string &bundleName, const std::string &storeId, int32_t pathType, sptr store) { diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.h b/services/distributeddataservice/app/src/kvstore_meta_manager.h index 156b61be5..5f7a86a69 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -22,7 +22,6 @@ #include "kv_store_delegate.h" #include "kv_store_delegate_manager.h" #include "kv_store_task.h" -#include "kvstore_impl.h" #include "single_kvstore_impl.h" #include "system_ability.h" #include "types.h" @@ -211,9 +210,6 @@ public: RecoverSecretKeyFromFile(const std::string &secretKeyFile, const std::vector &metaSecretKey, std::vector &key, bool &outdated); - void ReKey(const std::string &userId, const std::string &bundleName, const std::string &storeId, int32_t pathType, - sptr store); - void ReKey(const std::string &userId, const std::string &bundleName, const std::string &storeId, int32_t pathType, sptr store); diff --git a/services/distributeddataservice/app/src/kvstore_observer_impl.h b/services/distributeddataservice/app/src/kvstore_observer_impl.h index 55c9d792a..595f93a5a 100644 --- a/services/distributeddataservice/app/src/kvstore_observer_impl.h +++ b/services/distributeddataservice/app/src/kvstore_observer_impl.h @@ -16,7 +16,6 @@ #ifndef KVSTORE_OBSERVER_IMPL_H #define KVSTORE_OBSERVER_IMPL_H -#include "ikvstore.h" #include "ikvstore_observer.h" #include "kv_store_delegate.h" #include "types.h" diff --git a/services/distributeddataservice/app/src/kvstore_user_manager.cpp b/services/distributeddataservice/app/src/kvstore_user_manager.cpp index 1bafba123..25b51952c 100644 --- a/services/distributeddataservice/app/src/kvstore_user_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_user_manager.cpp @@ -103,37 +103,6 @@ void KvStoreUserManager::DeleteAllKvStore() appMap_.clear(); } -// Migrate all KvStore DB delegate object when harmony account changed. -Status KvStoreUserManager::MigrateAllKvStore(const std::string &harmonyAccountId) -{ - ZLOGI("begin."); - std::lock_guard lg(appMutex_); - Status status = Status::SUCCESS; - for (auto &it : appMap_) { - status = (it.second).MigrateAllKvStore(harmonyAccountId); - if (status != Status::SUCCESS) { - ZLOGE("migrate all kvstore for app-%s failed, status:%d.", - it.first.c_str(), static_cast(status)); - status = Status::MIGRATION_KVSTORE_FAILED; - } - } - return status; -} - -std::string KvStoreUserManager::GetDbDir(const StoreMetaData &metaData) -{ - ZLOGI("begin."); - if (metaData.storeType == KvStoreType::MULTI_VERSION) { - return "default"; - } - std::lock_guard lg(appMutex_); - auto it = appMap_.find(metaData.bundleName); - if (it != appMap_.end()) { - return (it->second).GetDbDir(metaData); - } - return ""; -} - void KvStoreUserManager::Dump(int fd) const { const std::string prefix(4, ' '); diff --git a/services/distributeddataservice/app/src/kvstore_user_manager.h b/services/distributeddataservice/app/src/kvstore_user_manager.h index 2897b59c5..c6a369370 100644 --- a/services/distributeddataservice/app/src/kvstore_user_manager.h +++ b/services/distributeddataservice/app/src/kvstore_user_manager.h @@ -19,7 +19,6 @@ #include #include #include "kvstore_app_manager.h" -#include "kvstore_impl.h" #include "types.h" #include "metadata/store_meta_data.h" @@ -62,10 +61,6 @@ public: void DeleteAllKvStore(); - Status MigrateAllKvStore(const std::string &harmonyAccountId); - - std::string GetDbDir(const StoreMetaData &metaData); - void Dump(int fd) const; bool IsStoreOpened(const std::string &appId, const std::string &storeId); diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 9b0c8a1a8..088d48741 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -68,7 +68,6 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", @@ -142,7 +141,6 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", @@ -216,7 +214,6 @@ ohos_unittest("KvStoreDataServiceTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", @@ -290,7 +287,6 @@ ohos_unittest("KvStoreBackupTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", @@ -400,7 +396,6 @@ ohos_unittest("KvStoreSyncManagerTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", @@ -472,7 +467,6 @@ ohos_unittest("KvStoreUninstallerTest") { "../src/kvstore_app_manager.cpp", "../src/kvstore_data_service.cpp", "../src/kvstore_device_listener.cpp", - "../src/kvstore_impl.cpp", "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", diff --git a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp index cf7507c19..3ed804c7e 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp @@ -18,7 +18,6 @@ #include #include #include "bootstrap.h" -#include "kvstore_impl.h" #include "backup_handler.h" #include "kvstore_data_service.h" #include "log_print.h" diff --git a/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp index b71dcf16b..177436029 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp @@ -17,7 +17,6 @@ #include #include "kvstore_data_service.h" #include "kvstore_meta_manager.h" -#include "kvstore_impl.h" #include "refbase.h" #include "types.h" #include "bootstrap.h" diff --git a/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp index eba7a42c8..bd8c8833f 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp @@ -18,7 +18,6 @@ #include "constant.h" #include "kvstore_data_service.h" #include "kvstore_meta_manager.h" -#include "kvstore_impl.h" #include "refbase.h" #include "types.h" #include "bootstrap.h" -- Gitee From 544e7c2eb5e2977279367d70ca2647eb83dad3a7 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 16 May 2022 17:37:40 +0800 Subject: [PATCH 2/3] remove un used code Signed-off-by: Sven Wang --- .../distributeddata/include/kvstore.h | 5 --- .../innerkits/distributeddata/include/types.h | 3 +- .../app/src/backup_handler.cpp | 32 ------------------- .../app/src/backup_handler.h | 1 - .../app/src/kvstore_data_service.cpp | 17 +++------- .../app/src/kvstore_data_service.h | 1 - .../app/src/kvstore_meta_manager.cpp | 21 +----------- 7 files changed, 7 insertions(+), 73 deletions(-) diff --git a/interfaces/innerkits/distributeddata/include/kvstore.h b/interfaces/innerkits/distributeddata/include/kvstore.h index 92380d88e..e138eec4f 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore.h +++ b/interfaces/innerkits/distributeddata/include/kvstore.h @@ -63,11 +63,6 @@ public: // keys memory size should not be greater than IPC transport limit, and can not be empty. virtual Status DeleteBatch(const std::vector &keys) = 0; - // clear all entries in the kvstore. - // after this call, IsClear function in ChangeNotification in subscription return true. - [[deprecated]] - virtual Status Clear() = 0; - // start transaction. // all changes to this kvstore will be in a same transaction and will not change the store until Commit() or // Rollback() is called. diff --git a/interfaces/innerkits/distributeddata/include/types.h b/interfaces/innerkits/distributeddata/include/types.h index 4ccef3b20..074d908bb 100644 --- a/interfaces/innerkits/distributeddata/include/types.h +++ b/interfaces/innerkits/distributeddata/include/types.h @@ -243,8 +243,7 @@ struct Options { inline bool IsValidType() const { - return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION - || kvStoreType == KvStoreType::MULTI_VERSION; + return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION; } }; diff --git a/services/distributeddataservice/app/src/backup_handler.cpp b/services/distributeddataservice/app/src/backup_handler.cpp index 1269c9ce9..73dac1651 100644 --- a/services/distributeddataservice/app/src/backup_handler.cpp +++ b/services/distributeddataservice/app/src/backup_handler.cpp @@ -224,38 +224,6 @@ bool BackupHandler::SingleKvStoreRecover(StoreMetaData &metaData, DistributedDB: return false; } -bool BackupHandler::MultiKvStoreRecover(StoreMetaData &metaData, DistributedDB::KvStoreDelegate *delegate) -{ - ZLOGI("start."); - if (delegate == nullptr) { - ZLOGE("MultiKvStoreRecover failed, delegate is null."); - return false; - } - auto pathType = KvStoreAppManager::ConvertPathType(metaData); - if (!BackupHandler::FileExists(BackupHandler::GetBackupPath(metaData.user, pathType))) { - ZLOGE("MultiKvStoreRecover failed, backupDir_ file is not exist."); - return false; - } - - ZLOGI("MultiKvStoreRecover start."); - DistributedDB::CipherPassword password; - if (!GetPassword(metaData, password)) { - ZLOGE("Set secret key failed."); - return false; - } - - std::string backupName = Constant::Concatenate({ metaData.account, "_", metaData.appId, "_", metaData.storeId }); - auto backupFullName = Constant::Concatenate( - { BackupHandler::GetBackupPath(metaData.user, pathType), "/", GetHashedBackupName(backupName) }); - DistributedDB::DBStatus dbStatus = delegate->Import(backupFullName, password); - if (dbStatus == DistributedDB::DBStatus::OK) { - ZLOGI("MultiKvStoreRecover success."); - return true; - } - ZLOGI("MultiKvStoreRecover failed."); - return false; -} - std::string BackupHandler::backupDirCe_; std::string BackupHandler::backupDirDe_; const std::string &BackupHandler::GetBackupPath(const std::string &deviceAccountId, int pathType) diff --git a/services/distributeddataservice/app/src/backup_handler.h b/services/distributeddataservice/app/src/backup_handler.h index 35f0e38f5..760cc5737 100644 --- a/services/distributeddataservice/app/src/backup_handler.h +++ b/services/distributeddataservice/app/src/backup_handler.h @@ -37,7 +37,6 @@ public: void BackSchedule(); void SingleKvStoreBackup(const StoreMetaData &metaData); bool SingleKvStoreRecover(StoreMetaData &metaData, DistributedDB::KvStoreNbDelegate *delegate); - bool MultiKvStoreRecover(StoreMetaData &metaData, DistributedDB::KvStoreDelegate *delegate); static const std::string &GetBackupPath(const std::string &deviceAccountId, int pathType); static bool RenameFile(const std::string &oldPath, const std::string &newPath); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 9384644a2..978953627 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -193,8 +193,7 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId Status KvStoreDataService::FillStoreParam( const Options &options, const AppId &appId, const StoreId &storeId, StoreMetaData &metaData) { - if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() - || options.kvStoreType == KvStoreType::MULTI_VERSION) { + if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType()) { ZLOGE("invalid argument type."); return Status::INVALID_ARGUMENT; } @@ -236,15 +235,9 @@ Status KvStoreDataService::GetSecretKey(const Options &options, const StoreMetaD std::vector metaSecretKey; std::string secretKeyFile; - if (options.kvStoreType == KvStoreType::MULTI_VERSION) { - metaSecretKey = KvStoreMetaManager::GetMetaKey(metaData.user, "default", bundleName, storeIdTmp, "KEY"); - secretKeyFile = KvStoreMetaManager::GetSecretKeyFile( - metaData.user, bundleName, storeIdTmp, KvStoreAppManager::ConvertPathType(metaData)); - } else { - metaSecretKey = KvStoreMetaManager::GetMetaKey(metaData.user, "default", bundleName, storeIdTmp, "SINGLE_KEY"); - secretKeyFile = KvStoreMetaManager::GetSecretSingleKeyFile( - metaData.user, bundleName, storeIdTmp, KvStoreAppManager::ConvertPathType(metaData)); - } + metaSecretKey = KvStoreMetaManager::GetMetaKey(metaData.user, "default", bundleName, storeIdTmp, "SINGLE_KEY"); + secretKeyFile = KvStoreMetaManager::GetSecretSingleKeyFile( + metaData.user, bundleName, storeIdTmp, KvStoreAppManager::ConvertPathType(metaData)); bool outdated = false; Status alreadyCreated = KvStoreMetaManager::GetInstance().CheckUpdateServiceMeta(metaSecretKey, CHECK_EXIST_LOCAL); @@ -854,7 +847,7 @@ bool KvStoreDataService::ResolveAutoLaunchParamByIdentifier( void KvStoreDataService::ResolveAutoLaunchCompatible(const MetaData &meta, const std::string &identifier) { ZLOGI("AutoLaunch:peer device is old tuple, begin to open store"); - if (meta.kvStoreType >= KvStoreType::MULTI_VERSION) { + if (meta.kvStoreType > KvStoreType::SINGLE_VERSION) { ZLOGW("no longer support multi or higher version store type"); return; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 711602674..de35601f6 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -179,7 +179,6 @@ private: std::shared_ptr security_; std::mutex mutex_; sptr rdbService_; - sptr kvdbService_; std::shared_ptr deviceInnerListener_; }; diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 80f2fab4a..f842aba2e 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -270,20 +270,6 @@ std::vector KvStoreMetaManager::GetMetaKey(const std::string &deviceAcc return SecretMetaRow::GetKeyFor(originKey); } -std::string KvStoreMetaManager::GetSecretKeyFile(const std::string &userId, const std::string &appId, - const std::string &storeId, int pathType) -{ - std::string hashedStoreId; - DistributedDB::DBStatus result = DistributedDB::KvStoreDelegateManager::GetDatabaseDir(storeId, hashedStoreId); - if (DistributedDB::OK != result) { - ZLOGE("get data base directory by kvstore store id failed, result = %d.", result); - return ""; - } - std::string miscPath = (pathType == KvStoreAppManager::PATH_DE) ? Constant::ROOT_PATH_DE : Constant::ROOT_PATH_CE; - return miscPath + "/" + Constant::SERVICE_NAME + "/" + userId + "/" + Constant::GetDefaultHarmonyAccountName() - + "/" + appId + "/" + hashedStoreId + ".mul.key"; -} - std::string KvStoreMetaManager::GetSecretSingleKeyFile(const std::string &userId, const std::string &appId, const std::string &storeId, int pathType) { @@ -606,12 +592,7 @@ Status KvStoreMetaManager::RemoveSecretKey(pid_t uid, const std::string &bundleN } for (int32_t pathType = KvStoreAppManager::PATH_DE; pathType < KvStoreAppManager::PATH_TYPE_MAX; ++pathType) { - std::string keyFile = GetSecretKeyFile(userId, bundleName, storeId, pathType); - if (!RemoveFile(keyFile)) { - ZLOGW("remove secret key file %{public}s fail.", keyFile.c_str()); - status = Status::DB_ERROR; - } - keyFile = GetSecretSingleKeyFile(userId, bundleName, storeId, pathType); + std::string keyFile = GetSecretSingleKeyFile(userId, bundleName, storeId, pathType); if (!RemoveFile(keyFile)) { ZLOGW("remove secretKeyFile Single fail."); status = Status::DB_ERROR; -- Gitee From 55c93f94378221a6c6f2e5f5fd4cae0066731764 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 16 May 2022 22:00:17 +0800 Subject: [PATCH 3/3] remove unused code Signed-off-by: Sven Wang --- .../distributeddatafwk/src/single_kvstore_client.cpp | 5 ----- .../distributeddatafwk/src/single_kvstore_client.h | 1 - 2 files changed, 6 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 226ad26bc..1d5a1e442 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -528,9 +528,4 @@ Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector syncCallbackClient_->AddSyncCallback(syncObserver_, sequenceId); return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId); } - -Status SingleKvStoreClient::Clear() -{ - return Status::NOT_SUPPORT; -} } // namespace OHOS::DistributedKv diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 92f3d8130..e3962c80e 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -93,7 +93,6 @@ public: Status SubscribeWithQuery(const std::vector &devices, const DataQuery &query) override; Status UnsubscribeWithQuery(const std::vector &devices, const DataQuery &query) override; - Status Clear() override; protected: Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam) override; -- Gitee