diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_observer.h index cf05a666816af0aa045b538b2dab6f8fbfab56e5..30a8e9f313f584bbeea4c55583261c7d994d03bf 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_observer.h @@ -19,7 +19,6 @@ #include "change_notification.h" #include "iremote_broker.h" #include "ikvstore_observer.h" -#include "ikvstore_snapshot.h" #include "iremote_proxy.h" #include "iremote_stub.h" @@ -28,7 +27,7 @@ namespace DistributedKv { class IKvStoreObserver : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreObserver"); - virtual void OnChange(const ChangeNotification &changeNotification, sptr snapshot) = 0; + virtual void OnChange(const ChangeNotification &changeNotification) = 0; }; class KvStoreObserverStub : public IRemoteStub { @@ -41,7 +40,7 @@ class KvStoreObserverProxy : public IRemoteProxy { public: explicit KvStoreObserverProxy(const sptr &impl); ~KvStoreObserverProxy() = default; - void OnChange(const ChangeNotification &changeNotification, sptr snapshot) override; + void OnChange(const ChangeNotification &changeNotification) override; private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_snapshot.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_snapshot.h deleted file mode 100644 index a031c52758880158440a9c238a8fdee709c632d6..0000000000000000000000000000000000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_snapshot.h +++ /dev/null @@ -1,80 +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_SNAPSHOT_H -#define I_KVSTORE_SNAPSHOT_H - -#include "message_parcel.h" -#include "iremote_broker.h" -#include "iremote_proxy.h" -#include "iremote_stub.h" -#include "types.h" - -namespace OHOS { -namespace DistributedKv { -class IKvStoreSnapshotImpl : public IRemoteBroker { -public: - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreSnapshotImpl") - virtual void GetEntries( - const Key &prefixKey, const Key &nextKey, - std::function &, - const OHOS::DistributedKv::Key &)> - callback) = 0; - - virtual void GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) = 0; - - virtual Status Get(const Key &key, Value &value) = 0; -}; - -class KvStoreSnapshotImplStub : public IRemoteStub { -public: - int OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) override; - -private: - int32_t GetEntriesOnRemote(MessageParcel &data, MessageParcel &reply); - int32_t GetKeysRemote(MessageParcel &data, MessageParcel &reply); - int32_t GetRemote(MessageParcel &data, MessageParcel &reply); - int32_t GetTotalEntriesSize(std::vector entryList); - int32_t WriteEntriesParcelable(MessageParcel &reply, Status statusTmp, - std::vector entryList, int bufferSize, Key nxtKey); - int32_t GetTotalkeysSize(std::vector keyList); - int32_t WritekeysParcelable(MessageParcel &reply, Status statusTmp, - std::vector keyList, int bufferSize, Key nxtKey); -}; - -class KvStoreSnapshotImplProxy : public IRemoteProxy { -public: - explicit KvStoreSnapshotImplProxy(const sptr &impl); - virtual void GetEntries( - const Key &prefixKey, const Key &nextKey, - std::function &, - const OHOS::DistributedKv::Key &)> - callback); - - virtual void GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback); - - virtual Status Get(const Key &key, Value &value); - - virtual ~KvStoreSnapshotImplProxy(); -private: - static inline BrokerDelegator delegator_; -}; -} // namespace DistributedKv -} // namespace OHOS - -#endif // I_KVSTORE_SNAPSHOT_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp index 107bd3c56e0b4a40d6d84b1fc64fa394c8e61e3b..d297d760bac172c67a0db2a7aa6986af6bd64fb3 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp @@ -84,7 +84,7 @@ bool WriteListToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, cons return true; } -void KvStoreObserverProxy::OnChange(const ChangeNotification &changeNotification, sptr snapshot) +void KvStoreObserverProxy::OnChange(const ChangeNotification &changeNotification) { MessageParcel data; MessageParcel reply; @@ -118,11 +118,6 @@ void KvStoreObserverProxy::OnChange(const ChangeNotification &changeNotification } } - if (snapshot != nullptr && !data.WriteRemoteObject(snapshot->AsObject().GetRefPtr())) { - ZLOGE("write strong parcel failed."); - return; - } - MessageOption mo { MessageOption::TF_WAIT_TIME }; int error = Remote()->SendRequest(ONCHANGE, data, reply, mo); if (error != 0) { @@ -189,13 +184,7 @@ int32_t KvStoreObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, ZLOGE("changeNotification is nullptr"); return errorResult; } - sptr remote = data.ReadRemoteObject(); - if (remote != nullptr) { - sptr kvStoreSnapshotProxy = iface_cast(remote); - OnChange(*changeNotification, std::move(kvStoreSnapshotProxy)); - } else { - OnChange(*changeNotification, nullptr); - } + OnChange(*changeNotification); } else { std::vector insertEntries; bool result = ReadListFromBuf(data, insertEntries); @@ -222,14 +211,7 @@ int32_t KvStoreObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, bool isClear = data.ReadBool(); ChangeNotification change(std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), deviceId, isClear); - sptr remote = data.ReadRemoteObject(); - if (remote != nullptr) { - sptr kvStoreSnapshotProxy = iface_cast(remote); - OnChange(change, std::move(kvStoreSnapshotProxy)); - } else { - ZLOGD("read kvstoreSnapshot is nullptr."); - OnChange(change, nullptr); - } + OnChange(change); } return 0; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_snapshot.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_snapshot.cpp deleted file mode 100644 index 82adafee72135b67d3dfdc4a0c7038cb57685124..0000000000000000000000000000000000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_snapshot.cpp +++ /dev/null @@ -1,543 +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 "KvStoreSnapshotImplProxy" - -#include "ikvstore_snapshot.h" -#include -#include "constant.h" -#include "log_print.h" -#include "message_parcel.h" - -namespace OHOS { -namespace DistributedKv { -enum { - GETENTRIES, - GETKEYS, - GET, -}; - -KvStoreSnapshotImplProxy::KvStoreSnapshotImplProxy(const sptr &impl) - : IRemoteProxy(impl) -{} - -KvStoreSnapshotImplProxy::~KvStoreSnapshotImplProxy() -{} - -void KvStoreSnapshotImplProxy::GetEntries(const Key &prefixKey, const Key &nextKey, - std::function &entries, const Key &key)> callback) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreSnapshotImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return; - } - std::vector entries; - if (!reply.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - ZLOGW("set max capacity failed"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - if (!data.WriteParcelable(&prefixKey)) { - ZLOGW("write prefix failed"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - if (!data.WriteParcelable(&nextKey)) { - ZLOGW("write nextkey failed"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - MessageOption mo { MessageOption::TF_SYNC }; - // struct of returned reply: - // buffer: | status | entryLength | rawdatasize | (sptr)nextkey | - // rawData: ( | keyLen | key | valueLen | value | ){entryLength} - int32_t error = Remote()->SendRequest(GETENTRIES, data, reply, mo); - if (error != 0) { - ZLOGW("Transact failed"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - - Status status = static_cast(reply.ReadInt32()); - if (status != Status::SUCCESS) { - ZLOGW("status not success, which is %d", static_cast(status)); - callback(status, entries, Key()); - return; - } - int replyEntryCount = reply.ReadInt32(); - int bufferSize = reply.ReadInt32(); - sptr keyTmp = reply.ReadParcelable(); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - for (int i = 0; i < replyEntryCount; i++) { - sptr entry = reply.ReadParcelable(); - if (entry == nullptr) { - ZLOGW("entry is nullptr"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - entries.push_back(*entry); - } - } else { - ZLOGI("getting large entry set"); - // this memory is managed by MassageParcel, DO NOT free here - const uint8_t *buffer = reinterpret_cast(reply.ReadRawData(bufferSize)); - if (replyEntryCount < 0 || bufferSize < 0 || buffer == nullptr) { - ZLOGW("replyEntryCount(%d) or bufferSize(%d) less than 0, or buffer is nullptr", replyEntryCount, - bufferSize); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - const uint8_t *rawDataCursor = buffer; - int bufferLeftSize = bufferSize; - entries = std::vector(replyEntryCount); - for (auto &entry : entries) { - if (!entry.key.ReadFromBuffer(rawDataCursor, bufferLeftSize) || - !entry.value.ReadFromBuffer(rawDataCursor, bufferLeftSize)) { - ZLOGW("read key or value from buffer failed"); - callback(Status::IPC_ERROR, entries, Key()); - return; - } - } - } - if (keyTmp != nullptr) { - callback(status, entries, *keyTmp); - } else { - callback(status, entries, Key()); - } -} - -void KvStoreSnapshotImplProxy::GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &keys, const Key &key)> callback) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreSnapshotImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return; - } - std::vector keyList; - if (!reply.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - if (!data.WriteParcelable(&prefixKey)) { - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - if (!data.WriteParcelable(&nextKey)) { - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(GETKEYS, data, reply, mo); - if (error != 0) { - ZLOGW("Transact failed"); - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - - Status status = static_cast(reply.ReadInt32()); - if (status != Status::SUCCESS) { - ZLOGW("status not success, which is %d", static_cast(status)); - callback(status, keyList, Key()); - return; - } - int replyKeyCount = reply.ReadInt32(); - int bufferSize = reply.ReadInt32(); - sptr keyTmp = reply.ReadParcelable(); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - for (int i = 0; i < replyKeyCount; i++) { - sptr keyInner = reply.ReadParcelable(); - if (keyInner == nullptr) { - ZLOGW("keyInner is nullptr"); - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - keyList.push_back(*keyInner); - } - } else { - ZLOGI("getting large key set"); - // this memory is managed by MassageParcel, DO NOT free here - const uint8_t *buffer = reinterpret_cast(reply.ReadRawData(bufferSize)); - if (replyKeyCount < 0 || bufferSize < 0 || buffer == nullptr) { - ZLOGW("replyKeyCount(%d) or bufferSize(%d) less than 0, or buffer is nullptr", replyKeyCount, bufferSize); - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - const uint8_t *rawDataCursor = buffer; - int bufferLeftSize = bufferSize; - keyList = std::vector(replyKeyCount); - for (auto &key : keyList) { - if (!key.ReadFromBuffer(rawDataCursor, bufferLeftSize)) { - ZLOGW("read key from buffer failed"); - callback(Status::IPC_ERROR, keyList, Key()); - return; - } - } - } - if (keyTmp != nullptr) { - callback(status, keyList, *keyTmp); - } else { - callback(status, keyList, Key()); - } -} - -Status KvStoreSnapshotImplProxy::Get(const Key &key, Value &value) -{ - MessageParcel data, reply; - if (!data.WriteInterfaceToken(KvStoreSnapshotImplProxy::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return Status::IPC_ERROR; - } - if (!reply.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - return Status::IPC_ERROR; - } - if (!data.WriteParcelable(&key)) { - return Status::IPC_ERROR; - } - - MessageOption mo { MessageOption::TF_SYNC }; - ZLOGI("start"); - int32_t error = Remote()->SendRequest(GET, data, reply, mo); - if (error != 0) { - ZLOGW("SendRequest failed, error is %d", error); - return Status::IPC_ERROR; - } - Status status = static_cast(reply.ReadInt32()); - if (status != Status::SUCCESS) { - ZLOGW("status not success(%d)", static_cast(status)); - return status; - } - - int bufferSize = reply.ReadInt32(); - if (bufferSize < 0) { - ZLOGW("bufferSize < 0(%d)", bufferSize); - return Status::ERROR; - } - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - sptr valueTmp = reply.ReadParcelable(); - if (valueTmp != nullptr) { - value = *valueTmp; - } - return status; - } - ZLOGI("getting big data"); - // this memory is managed by MassageParcel, DO NOT free here - const uint8_t *buffer = reinterpret_cast(reply.ReadRawData(bufferSize)); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - return Status::IPC_ERROR; - } - if (!value.ReadFromBuffer(buffer, bufferSize)) { - ZLOGW("read value from buffer failed"); - return Status::IPC_ERROR; - } - return Status::SUCCESS; -} - -int32_t KvStoreSnapshotImplStub::GetTotalEntriesSize(std::vector entryList) -{ - int bufferSize = 0; - for (const auto &item : entryList) { - bufferSize += item.key.RawSize() + item.value.RawSize(); - } - return bufferSize; -} -int32_t KvStoreSnapshotImplStub::WriteEntriesParcelable(MessageParcel &reply, Status statusTmp, - std::vector entryList, int bufferSize, Key nxtKey) -{ - if (!reply.WriteInt32(static_cast(statusTmp)) || - !reply.WriteInt32(entryList.size()) || - !reply.WriteInt32(bufferSize) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write entry to parcel failed."); - return -1; - } - for (const auto &item : entryList) { - if (!reply.WriteParcelable(&item)) { - ZLOGW("write item to parcel failed."); - return -1; - } - } - return 0; -} -int32_t KvStoreSnapshotImplStub::GetTotalkeysSize(std::vector keyList) -{ - int bufferSize = 0; - for (const auto &key : keyList) { - bufferSize += key.RawSize(); - } - return bufferSize; -} -int32_t KvStoreSnapshotImplStub::WritekeysParcelable(MessageParcel &reply, Status statusTmp, - std::vector keyList, int bufferSize, Key nxtKey) -{ - if (!reply.WriteInt32(static_cast(statusTmp)) || - !reply.WriteInt32(keyList.size()) || - !reply.WriteInt32(bufferSize) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write buffer size failed."); - return -1; - } - for (const auto &item : keyList) { - if (!reply.WriteParcelable(&item)) { - ZLOGW("write item failed."); - return -1; - } - } - return 0; -} - -int32_t KvStoreSnapshotImplStub::GetEntriesOnRemote(MessageParcel &data, MessageParcel &reply) -{ - // struct of returned reply: - // buffer: | status | entryLength | rawdatasize | (sptr)nextkey | - // rawData: ( | keyLen | key | valueLen | value | ){entryLength} - sptr keyPrefix = data.ReadParcelable(); - if (keyPrefix == nullptr) { - ZLOGW("keyPrefix is null. return."); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - return 0; - } - sptr nextKey = data.ReadParcelable(); - if (nextKey == nullptr) { - ZLOGW("nextKey is null. return."); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - return 0; - } - std::vector entryList; - Key nxtKey; - Status statusTmp; - GetEntries(*keyPrefix, *nextKey, [&](Status status, const std::vector &entries, const Key &key) { - statusTmp = status; - entryList = std::move(entries); - nxtKey = key; - }); - int bufferSize = GetTotalEntriesSize(entryList); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - return WriteEntriesParcelable(reply, statusTmp, entryList, bufferSize, nxtKey); - } - ZLOGI("getting large entry set"); - if (bufferSize > static_cast(reply.GetRawDataCapacity())) { - ZLOGW("bufferSize %d larger than message parcel limit", bufferSize); - if (!reply.WriteInt32(static_cast(Status::ERROR))) { - return -1; - } - return 0; - } - std::unique_ptr buffer( - new uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - if (!reply.WriteInt32(static_cast(Status::ERROR))) { - return -1; - } - return 0; - } - - if (!reply.WriteInt32(static_cast(statusTmp)) || - !reply.WriteInt32(entryList.size()) || - !reply.WriteInt32(bufferSize) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write entries failed."); - return -1; - } - int bufferLeftSize = bufferSize; - uint8_t *cursor = buffer.get(); - for (const auto &item : entryList) { - if (!item.key.WriteToBuffer(cursor, bufferLeftSize) || - !item.value.WriteToBuffer(cursor, bufferLeftSize)) { - ZLOGW("write to buffer failed"); - return -1; - } - } - if (!reply.WriteRawData(buffer.get(), bufferSize)) { - ZLOGW("write rawData failed"); - return -1; - } - return 0; -} -int32_t KvStoreSnapshotImplStub::GetKeysRemote(MessageParcel &data, MessageParcel &reply) -{ - // struct of returned reply: - // buffer: | status | keyListLength | rawdatasize | (sptr)nextkey | - // rawData: ( | keyLen | key | ){keyListLength} - sptr keyPrefix = data.ReadParcelable(); - sptr nextKey = data.ReadParcelable(); - if (keyPrefix == nullptr) { - ZLOGW("keyPrefix is null"); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - if (!reply.WriteParcelable(nullptr)) { - return -1; - } - return 0; - } - if (nextKey == nullptr) { - ZLOGW("nextKey is null. return."); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - if (!reply.WriteParcelable(nullptr)) { - return -1; - } - return 0; - } - std::vector keyList; - Key nxtKey; - Status statusTmp; - GetKeys(*keyPrefix, *nextKey, [&](Status status, const std::vector &keys, const Key &key) { - statusTmp = status; - keyList = std::move(keys); - nxtKey = key; - }); - int bufferSize = GetTotalkeysSize(keyList); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - return WritekeysParcelable(reply, statusTmp, keyList, bufferSize, nxtKey); - } - ZLOGI("getting large key set"); - if (bufferSize > static_cast(reply.GetRawDataCapacity())) { - ZLOGW("bufferSize %d larger than message parcel limit", bufferSize); - if (!reply.WriteInt32(static_cast(Status::ERROR)) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write status failed."); - return -1; - } - return 0; - } - std::unique_ptr buffer( - new uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); - if (buffer == nullptr) { - ZLOGW("alloc memory failed(buffer is null). perhaps low on memory."); - if (!reply.WriteInt32(static_cast(Status::ERROR)) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write nxtkey failed."); - return -1; - } - return 0; - } - if (!reply.WriteInt32(static_cast(statusTmp)) || - !reply.WriteInt32(keyList.size()) || - !reply.WriteInt32(bufferSize) || - !reply.WriteParcelable(&nxtKey)) { - ZLOGW("write meta failed."); - return -1; - } - - int bufferLeftSize = bufferSize; - uint8_t *cursor = buffer.get(); - for (const auto &key : keyList) { - if (!key.WriteToBuffer(cursor, bufferLeftSize)) { - ZLOGW("write to buffer failed."); - return -1; - } - } - if (!reply.WriteRawData(buffer.get(), bufferSize)) { - ZLOGW("write rawData failed"); - return -1; - } - return 0; -} -int32_t KvStoreSnapshotImplStub::GetRemote(MessageParcel &data, MessageParcel &reply) -{ - sptr key = data.ReadParcelable(); - if (key == nullptr) { - ZLOGW("key is null"); - if (!reply.WriteInt32(static_cast(Status::INVALID_ARGUMENT))) { - return -1; - } - if (!reply.WriteInt32(0)) { - return -1; - } - return 0; - } - Value value; - Status status = Get(*key, value); - - int bufferSize = value.RawSize(); - if (bufferSize < Constant::SWITCH_RAW_DATA_SIZE) { - if (!reply.WriteInt32(static_cast(status)) || - !reply.WriteInt32(bufferSize) || - !reply.WriteParcelable(&value)) { - ZLOGW("write meta failed."); - return -1; - } - return 0; - } - ZLOGI("getting large entry"); - std::unique_ptr buffer( - new uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); - if (buffer == nullptr) { - ZLOGW("buffer is null"); - if (!reply.WriteInt32(static_cast(Status::ILLEGAL_STATE)) || - !reply.WriteInt32(0)) { - ZLOGW("write state failed."); - return -1; - } - return 0; - } - int bufferLeftSize = bufferSize; - if (!reply.WriteInt32(static_cast(status)) || - !reply.WriteInt32(bufferSize)) { - ZLOGW("write bufferSize failed."); - return -1; - } - - uint8_t *cursor = buffer.get(); - if (!value.WriteToBuffer(cursor, bufferLeftSize) || - !reply.WriteRawData(buffer.get(), bufferSize)) { - ZLOGW("write rawData failed."); - return -1; - } - return 0; -} - -int32_t KvStoreSnapshotImplStub::OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) -{ - ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid()); - std::u16string descriptor = KvStoreSnapshotImplStub::GetDescriptor(); - std::u16string remoteDescriptor = data.ReadInterfaceToken(); - if (descriptor != remoteDescriptor) { - ZLOGE("local descriptor is not equal to remote"); - return -1; - } - if (!reply.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) { - return -1; - } - switch (code) { - case GETENTRIES: { - return GetEntriesOnRemote(data, reply); - } - case GETKEYS: { - return GetKeysRemote(data, reply); - } - case GET: { - return GetRemote(data, reply); - } - default: - if (!reply.WriteInt32((int32_t)Status::ERROR)) { - return -1; - } - return 0; - } -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp index 201e612abc1e4590529a82ee3a6f8ea79562a8c4..5dbb60864e79770848881c69ba607227df87da58 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp @@ -16,7 +16,6 @@ #define LOG_TAG "KvStoreObserverClient" #include "kvstore_observer_client.h" -#include "kvstore_snapshot_client.h" #include "log_print.h" namespace OHOS { @@ -33,17 +32,13 @@ KvStoreObserverClient::~KvStoreObserverClient() ZLOGI("end"); } -void KvStoreObserverClient::OnChange(const ChangeNotification &changeNotification, sptr snapshot) +void KvStoreObserverClient::OnChange(const ChangeNotification &changeNotification) { ZLOGI("start"); if (kvStoreObserver_ != nullptr) { if (type_ == KvStoreType::SINGLE_VERSION) { ZLOGI("SINGLE_VERSION start"); kvStoreObserver_->OnChange(changeNotification); - } else { - ZLOGI("MULTI_VERSION start"); - kvStoreObserver_->OnChange(changeNotification, - std::make_unique(std::move(snapshot))); } } } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.h index 1ba2a076712a27baabf14569989a5010687d535c..addcd074a2b5dfeb81661019e5d468b01754f8f9 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.h @@ -19,7 +19,6 @@ #include #include "change_notification.h" #include "ikvstore_observer.h" -#include "ikvstore_snapshot.h" #include "kvstore_observer.h" #include "refbase.h" @@ -32,7 +31,7 @@ public: ~KvStoreObserverClient(); - void OnChange(const ChangeNotification &changeNotification, sptr snapshot) override; + void OnChange(const ChangeNotification &changeNotification) override; const StoreId &GetStoreId() const; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp deleted file mode 100644 index 1c29f0b0d1a7cff6d76bf9f3b1f7c2953115bd75..0000000000000000000000000000000000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp +++ /dev/null @@ -1,181 +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 "KvStoreSnapshotClient" - -#include "kvstore_snapshot_client.h" -#include "constant.h" -#include "dds_trace.h" -#include "log_print.h" - -namespace OHOS { -namespace DistributedKv { -KvStoreSnapshotClient::KvStoreSnapshotClient() : kvStoreSnapshotProxy_(nullptr) -{} - -KvStoreSnapshotClient::KvStoreSnapshotClient(sptr kvStoreSnapshotProxy) - : kvStoreSnapshotProxy_(std::move(kvStoreSnapshotProxy)) -{ - ZLOGI("construct"); -} - -KvStoreSnapshotClient::~KvStoreSnapshotClient() -{ - ZLOGI("destruct"); -} - -Status KvStoreSnapshotClient::GetEntries(const Key &prefixKey, Key &nextKey, std::vector &entries) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid prefixKey."); - return Status::INVALID_ARGUMENT; - } - keyData = Constant::TrimCopy>(nextKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid nextKey."); - return Status::INVALID_ARGUMENT; - } - Status status = Status::SERVER_UNAVAILABLE; - if (kvStoreSnapshotProxy_ != nullptr) { - kvStoreSnapshotProxy_->GetEntries(prefixKey, nextKey, - [&status, &entries, &nextKey](Status stat, auto &result, const auto &next) { - status = stat; - entries = std::move(result); - nextKey = next; - }); - } else { - ZLOGE("snapshot proxy is nullptr."); - } - return status; -} - -Status KvStoreSnapshotClient::GetEntries(const Key &prefixKey, std::vector &entries) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - - std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid prefixKey."); - return Status::INVALID_ARGUMENT; - } - if (kvStoreSnapshotProxy_ == nullptr) { - ZLOGE("snapshot proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; - } - Key startKey(""); - Status status = Status::ERROR; - do { - kvStoreSnapshotProxy_->GetEntries(prefixKey, startKey, - [&status, &entries, &startKey](Status stat, auto &result, Key next) { - status = stat; - if (stat != Status::SUCCESS) { - return; - } - startKey = result.empty() ? Key("") : next; - entries.insert(entries.end(), result.begin(), result.end()); - }); - } while (status == Status::SUCCESS && startKey.ToString() != ""); - if (status != Status::SUCCESS) { - ZLOGW("Error occurs during GetEntries."); - entries.clear(); - } - return status; -} - -Status KvStoreSnapshotClient::GetKeys(const Key &prefixKey, Key &nextKey, std::vector &keys) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid prefixKey."); - return Status::INVALID_ARGUMENT; - } - keyData = Constant::TrimCopy>(nextKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid nextKey."); - return Status::INVALID_ARGUMENT; - } - Status status = Status::SERVER_UNAVAILABLE; - if (kvStoreSnapshotProxy_ != nullptr) { - kvStoreSnapshotProxy_->GetKeys(prefixKey, nextKey, - [&status, &keys, &nextKey](Status stat, auto &result, const auto &next) { - status = stat; - keys = std::move(result); - nextKey = next; - }); - } else { - ZLOGE("snapshot proxy is nullptr."); - } - return status; -} - -Status KvStoreSnapshotClient::GetKeys(const Key &prefixKey, std::vector &entries) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); - if (keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid prefixKey."); - return Status::INVALID_ARGUMENT; - } - if (kvStoreSnapshotProxy_ == nullptr) { - ZLOGE("snapshot proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; - } - Key startKey(""); - Status status = Status::ERROR; - do { - kvStoreSnapshotProxy_->GetKeys(prefixKey, startKey, - [&status, &entries, &startKey](Status stat, auto &result, Key next) { - status = stat; - if (stat != Status::SUCCESS) { - return; - } - startKey = result.empty() ? Key("") : next; - entries.insert(entries.end(), result.begin(), result.end()); - }); - } while (status == Status::SUCCESS && startKey.ToString() != ""); - if (status != Status::SUCCESS) { - ZLOGW("Error occurs during GetKeys."); - entries.clear(); - } - return status; -} - -Status KvStoreSnapshotClient::Get(const Key &key, Value &value) -{ - 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 (kvStoreSnapshotProxy_ != nullptr) { - return kvStoreSnapshotProxy_->Get(key, value); - } - ZLOGE("snapshot proxy is nullptr."); - return Status::SERVER_UNAVAILABLE; -} - -sptr KvStoreSnapshotClient::GetkvStoreSnapshotProxy() -{ - return kvStoreSnapshotProxy_; -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.h deleted file mode 100644 index 3bb3263308f7f4aa1b84f7fc1d91980f158c97e5..0000000000000000000000000000000000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.h +++ /dev/null @@ -1,51 +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_SNAPSHOT_CLIENT_H -#define KVSTORE_SNAPSHOT_CLIENT_H - -#include "ikvstore_snapshot.h" -#include "kvstore_service_death_notifier.h" -#include "kvstore_snapshot.h" -#include "types.h" - -namespace OHOS { -namespace DistributedKv { -class KvStoreSnapshotClient final : public KvStoreSnapshot { -public: - KvStoreSnapshotClient(); - - explicit KvStoreSnapshotClient(sptr kvStoreSnapshotProxy); - - ~KvStoreSnapshotClient(); - - Status GetEntries(const Key &prefixKey, Key &nextKey, std::vector &entries) override; - - Status GetEntries(const Key &prefixKey, std::vector &entries) override; - - Status GetKeys(const Key &prefixKey, Key &nextKey, std::vector &keys) override; - - Status GetKeys(const Key &prefixKey, std::vector &entries) override; - - Status Get(const Key &key, Value &value) override; - - sptr GetkvStoreSnapshotProxy(); -private: - // use shared_ptr here to free pointer when reference count is 0. - sptr kvStoreSnapshotProxy_; -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // KVSTORE_SNAPSHOT_CLIENT_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 5402033d6383e05b0854cc7af064d66211b7242c..59e15a42e1243585615a0c42eb0abe356faea14e 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -38,8 +38,8 @@ void KvStoreSyncCallbackClient::SyncCompleted(const std::map callback, - uint64_t sequenceId) +void KvStoreSyncCallbackClient::AddSyncCallback( + const std::shared_ptr callback, uint64_t sequenceId) { if (callback == nullptr) { ZLOGE("callback is nullptr"); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index c738b58bb4b1249d8a073551a91687e90d284f73..301dda0a575945bf830f1367a6e33220d6eb7da6 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -30,8 +30,7 @@ public: void SyncCompleted(const std::map &results, uint64_t sequenceId) override; - void AddSyncCallback(const std::shared_ptr callback, - uint64_t sequenceId); + void AddSyncCallback(const std::shared_ptr callback, uint64_t sequenceId); void DeleteSyncCallback(uint64_t sequenceId); private: diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index e3962c80ef6ca55ba0202403829e28a61ee353d0..384cdf7abf9ee18f5aaa98304ce7f977b6aa2c44 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -95,7 +95,7 @@ public: Status UnsubscribeWithQuery(const std::vector &devices, const DataQuery &query) override; protected: - Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam) override; + Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam); private: sptr kvStoreProxy_; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp index 69d55662ef387e04347a432e2feaafd8dba5bc90..76551654c8257891e33c6dc616bcf3944fed8524 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp @@ -714,37 +714,3 @@ HWTEST_F(DistributedKvDataManagerTest, UnRegisterKvStoreServiceDeathRecipient001 std::shared_ptr kvStoreDeathRecipientPtr = std::make_shared(); manager.UnRegisterKvStoreServiceDeathRecipient(kvStoreDeathRecipientPtr); } - -class DeviceListenerImpl : public DeviceStatusChangeListener { -public: - void OnDeviceChanged(const DeviceInfo &info, const DeviceChangeType &type) const override - { - } - DeviceFilterStrategy GetFilterStrategy() const override - { - return DeviceFilterStrategy::NO_FILTER; - } -}; -/** -* @tc.name: GetDevice001 -* @tc.desc: Get device id. -* @tc.type: FUNC -* @tc.require: SR000DOH1R AR000DPSGU -* @tc.author: hongbo -*/ -HWTEST_F(DistributedKvDataManagerTest, GetDevice001, TestSize.Level1) -{ - ZLOGI("GetDevice001 begin."); - DeviceInfo info; - Status status = manager.GetLocalDevice(info); - EXPECT_EQ(Status::SUCCESS, status) << "expected getLocalDevice true"; - EXPECT_TRUE(info.deviceId.size() > 0) << "expected deviceId exist"; - - std::vector infos; - status = manager.GetDeviceList(infos, DeviceFilterStrategy::FILTER); - auto listener = std::make_shared(); - status = manager.StartWatchDeviceChange(listener); - EXPECT_EQ(Status::SUCCESS, status) << "expected StartWatchDeviceChange true"; - status = manager.StopWatchDeviceChange(listener); - EXPECT_EQ(Status::SUCCESS, status) << "expected StopWatchDeviceChange true"; -} diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp index 8dd8fa6efe561daee4788eefd7be3fd035556a55..9804c2ad854ccaa94c42625885940837e1f45b25 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp @@ -99,9 +99,6 @@ public: KvStoreObserverUnitTest(KvStoreObserverUnitTest &&) = delete; KvStoreObserverUnitTest &operator=(KvStoreObserverUnitTest &&) = delete; - // callback function will be called when the db data is changed. - void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot); - void OnChange(const ChangeNotification &changeNotification); // reset the callCount_ to zero. @@ -122,8 +119,7 @@ KvStoreObserverUnitTest::KvStoreObserverUnitTest() isClear_ = false; } -void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotification, - std::shared_ptr snapshot) +void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotification) { ZLOGD("begin."); callCount_++; @@ -134,11 +130,6 @@ void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotificat isClear_ = changeNotification.IsClear(); } -void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotification) -{ - OnChange(changeNotification, nullptr); -} - void KvStoreObserverUnitTest::ResetToZero() { callCount_ = 0; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp index 641e45ce340395ff199491e4bf4ecf3e668040e4..e8bb85013c8e3e07af479ea3260f97032ad0cfc3 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -88,9 +88,6 @@ public: KvStoreObserverTestImpl(KvStoreObserverTestImpl &&) = delete; KvStoreObserverTestImpl &operator=(KvStoreObserverTestImpl &&) = delete; - // callback function will be called when the db data is changed. - void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot); - void OnChange(const ChangeNotification &changeNotification); // reset the callCount_ to zero. @@ -126,11 +123,6 @@ void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotificat isClear_ = changeNotification.IsClear(); } -void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotification, - std::shared_ptr snapshot) -{} - - KvStoreObserverTestImpl::KvStoreObserverTestImpl() { callCount_ = 0; diff --git a/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h index 9560698ada1f321e454124ac11b52f797ae0beed..dc5a4851900f49afef4321da9d36dce7bd36c741 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h @@ -62,8 +62,6 @@ private: public: DataObserver(std::shared_ptr uvQueue, napi_value callback) : JSObserver(uvQueue, callback) {}; virtual ~DataObserver() = default; - void OnChange(const DistributedKv::ChangeNotification& notification, - std::shared_ptr snapshot) override; void OnChange(const DistributedKv::ChangeNotification& notification) override; }; diff --git a/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp index 2762606e842229ae7b6fb536e89fc19e17575822..13bc8d7be0393faf12556d159f76729a874005b1 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp @@ -612,14 +612,6 @@ void JsKVStore::SetUvQueue(std::shared_ptr uvQueue) uvQueue_ = uvQueue; } -void JsKVStore::DataObserver::OnChange(const ChangeNotification ¬ification, - std::shared_ptr snapshot) -{ - ZLOGD("data change insert:%{public}zu, update:%{public}zu, delete:%{public}zu", - notification.GetInsertEntries().size(), notification.GetUpdateEntries().size(), - notification.GetDeleteEntries().size()); -} - void JsKVStore::DataObserver::OnChange(const ChangeNotification& notification) { ZLOGD("data change insert:%{public}zu, update:%{public}zu, delete:%{public}zu", diff --git a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h index d2456a4628e97b66c0e6a3fe4a34c275e57c5396..3b505ed2d0d897743086b9a68e0e551266edf551 100644 --- a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h +++ b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h @@ -106,6 +106,7 @@ public: // observer: callback for device status change event. // Return: // Status of this subscribe operation. + [[deprecated]] API_EXPORT Status StartWatchDeviceChange(std::shared_ptr observer); // Unsubscribe device status change, like online or offline. @@ -115,6 +116,7 @@ public: // observer: callback for device status change event. // Return: // Status of this unsubscribe operation. + [[deprecated]] API_EXPORT Status StopWatchDeviceChange(std::shared_ptr observer); // Get all connected devices. @@ -124,6 +126,7 @@ public: // deviceInfoList: list of all connected device will be returned by this parameter. // Return: // Status of this get device list operation. + [[deprecated]] API_EXPORT Status GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy); // Get device. @@ -132,6 +135,7 @@ public: // localDevice: DeviceInfo will be returned by this parameter. // Return: // Status of this get device operation. + [[deprecated]] API_EXPORT Status GetLocalDevice(DeviceInfo &localDevice); }; } // namespace DistributedKv diff --git a/interfaces/innerkits/distributeddata/include/kvstore.h b/interfaces/innerkits/distributeddata/include/kvstore.h index e138eec4feada04666b9a276492b6ad3e6994f03..52c854e3459059008f4d5540c52d2c06fec1d88f 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore.h +++ b/interfaces/innerkits/distributeddata/include/kvstore.h @@ -17,7 +17,6 @@ #define KVSTORE_H #include "kvstore_observer.h" -#include "kvstore_snapshot.h" #include "types.h" namespace OHOS { diff --git a/interfaces/innerkits/distributeddata/include/kvstore_observer.h b/interfaces/innerkits/distributeddata/include/kvstore_observer.h index a0013e06394de14199f39b78c8aeff754d648a9e..8be7f0e12c87b22e72bac1897963d0acdb650938 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore_observer.h +++ b/interfaces/innerkits/distributeddata/include/kvstore_observer.h @@ -18,7 +18,6 @@ #include #include "change_notification.h" -#include "kvstore_snapshot.h" namespace OHOS { namespace DistributedKv { @@ -29,9 +28,6 @@ public: API_EXPORT virtual ~KvStoreObserver() {} - // client override this function to receive change notification. - virtual void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot) = 0; - // client override this function to receive change notification. API_EXPORT virtual void OnChange(const ChangeNotification &changeNotification) {} }; diff --git a/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h b/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h deleted file mode 100644 index 64a8e97a6779ac5b28a6588a4e7c13e2a16b0a75..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h +++ /dev/null @@ -1,89 +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_SNAPSHOT_H -#define KVSTORE_SNAPSHOT_H - -#include "types.h" - -namespace OHOS { -namespace DistributedKv { -class KvStoreSnapshot { -public: - API_EXPORT KvStoreSnapshot() = default; - - API_EXPORT virtual ~KvStoreSnapshot() {} - - // Deprecated. use the GetEntries interface without nextKey as parameter instead. - // Get a list of entries from kvstore by keyPrefix, - // key length must be less than 1024, - // GetEntries will return all entries whose Key.StartsWith(keyPrefix) is true, - // if keyPrefix is empty, all entries in the kvstore will be returned. - // if data size is larger than 800k, data may be transported by several times. each time callback will give you the - // first key of the not-transported part. You can use this key as nextKey to get next part of data. When you get an - // empty nextKey, It means all data has been transported. - // parameters: - // prefixKey: prefix key to search - // nextKey: The first key to start in this search. - // callback: all entries satisfied prefixKey, status of this call and the first key of the next part of data. - [[deprecated]] - virtual Status GetEntries(const Key &prefixKey, Key &nextKey, std::vector &entries) = 0; - - // Get a list of entries from kvstore by keyPrefix, - // key length must be less than 1024, - // GetEntries will return all entries whose Key.StartsWith(keyPrefix) is true, - // if keyPrefix is empty, all entries in the kvstore will be returned. - // if some entry in the return set large then 750k, GetEntries may only return entries before this entry. you need - // to use GetKeys interface to get all keys, then use Get interface to get each entry. - // parameters: - // prefixKey: prefix key to search - // callback: all entries satisfies prefixKey, and Stauts for this call. - virtual Status GetEntries(const Key &prefixKey, std::vector &entries) = 0; - - // Deprecated. use the GetKeys interface without nextKey as parameter instead. - // Get a list of keys from kvstore by keyPrefix, - // key length must be less than 1024, - // GetKeys will return all keys whose Key.StartsWith(keyPrefix) is true, - // if keyPrefix is empty, all keys in the kvstore will be returned. - // if data size is larger than 800k, data may be transported by several times. each time callback will give you the - // first key of the not-transported part. You can use this key as nextKey to get next part of data. When you get an - // empty nextKey, It means all data has been transported. - // parameters: - // prefixKey: prefix key to search - // nextKey: The first key to start in this search. - // callback: all keys satisfies prefixKey, status of this call and the first key of the next part of data. - [[deprecated]] - virtual Status GetKeys(const Key &prefixKey, Key &nextKey, std::vector &entries) = 0; - - // Get a list of keys from kvstore by keyPrefix, - // key length must be less than 1024, - // GetKeys will return all keys whose Key.StartsWith(keyPrefix) is true, - // if keyPrefix is empty, all keys in the kvstore will be returned. - // parameters: - // prefixKey: prefix key to search - // callback: all keys satisfies prefixKey, and Stauts for this call. - virtual Status GetKeys(const Key &prefixKey, std::vector &entries) = 0; - - // Get value by key from kvstore, key length must be less than 256 and can not be empty. - // if key not found in kvstore, KEY_NOT_FOUND will be returned. - // otherwise, SUCCESS will be returned and value can be retrieved from the second parameter. - // parameters: - // key: key specified by client, - // value: value stored in kvstore, or empty and KEY_NOT_FOUND returned. - virtual Status Get(const Key &key, Value &value) = 0; -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // KVSTORE_SNAPSHOT_H diff --git a/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp b/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp index efd61259b1d98dee9150d453b970f25d5d13d7af..aa718a099491bde31a42304e00e45ee4dcd43432 100644 --- a/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp +++ b/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp @@ -46,7 +46,7 @@ void DeviceKvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData } ChangeNotification change(std::move(inserts), std::move(updates), std::move(deleteds), deviceId, false); if (observerProxy_ != nullptr) { - observerProxy_->OnChange(change, nullptr); + observerProxy_->OnChange(change); } } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 978953627f3955b3e40e2ebf526da53412099333..f1a9350ebf363988b0aa66b8bc88d5b8f3d38aea 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -696,11 +696,12 @@ void KvStoreDataService::OnStart() } ZLOGE("GetLocalDeviceId failed, retry count:%{public}d", static_cast(retry)); } - Initialize(); + ZLOGI("Bootstrap configs and plugins."); Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); + Initialize(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr != nullptr) { ZLOGI("samgr exist."); diff --git a/services/distributeddataservice/app/src/kvstore_observer_impl.cpp b/services/distributeddataservice/app/src/kvstore_observer_impl.cpp index a1b0cf1af809033e82bc658cbeaf3aa2171bb106..a8bba4d019a96948d4a6286fee636e41a7b5909d 100644 --- a/services/distributeddataservice/app/src/kvstore_observer_impl.cpp +++ b/services/distributeddataservice/app/src/kvstore_observer_impl.cpp @@ -80,7 +80,7 @@ void KvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData &data ChangeNotification change(std::move(inserts), std::move(updates), std::move(deleteds), std::string(), false); ZLOGI("call proxy OnChange"); - observerProxy_->OnChange(change, nullptr); + observerProxy_->OnChange(change); } SubscribeType KvStoreObserverImpl::GetSubscribeType() const diff --git a/services/distributeddataservice/app/src/kvstore_snapshot_impl.cpp b/services/distributeddataservice/app/src/kvstore_snapshot_impl.cpp deleted file mode 100644 index 783a0c0a063aec597e42423eb04ac12843bfba97..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/app/src/kvstore_snapshot_impl.cpp +++ /dev/null @@ -1,374 +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 "KvStoreSnapshotImpl" - -#include "kvstore_snapshot_impl.h" -#include "constant.h" -#include "log_print.h" -#include "dds_trace.h" - -namespace OHOS { -namespace DistributedKv { -KvStoreSnapshotImpl::KvStoreSnapshotImpl(DistributedDB::KvStoreSnapshotDelegate *kvStoreSnapshotDelegate, - KvStoreObserverImpl *kvStoreObserverImpl) - : kvStoreSnapshotDelegate_(kvStoreSnapshotDelegate), kvStoreObserverImpl_(kvStoreObserverImpl) -{ - ZLOGI("construct"); -} - -KvStoreSnapshotImpl::~KvStoreSnapshotImpl() -{ - ZLOGI("destruct"); - if (kvStoreObserverImpl_ != nullptr) { - delete kvStoreObserverImpl_; - } -} - -Status KvStoreSnapshotImpl::Get(const Key &key, Value &value) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - std::vector keyData = Constant::TrimCopy>(key.Data()); - - if (keyData.empty() || keyData.size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - return Status::INVALID_ARGUMENT; - } - std::shared_lock lock(snapshotDelegateMutex_); - if (kvStoreSnapshotDelegate_ == nullptr) { - ZLOGE("delegate is null."); - return Status::DB_ERROR; - } - - DistributedDB::Value retValue; - DistributedDB::DBStatus retValueStatus; - - auto valueCallbackFunction = [&](DistributedDB::DBStatus status, DistributedDB::Value valueTmp) { - retValueStatus = status; - retValue = valueTmp; - }; - - DistributedDB::Key dbKey = keyData; - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreSnapshotDelegate_->Get(dbKey, valueCallbackFunction); - } - if (retValueStatus != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error: %d.", static_cast(retValueStatus)); - if (retValueStatus == DistributedDB::DBStatus::NOT_FOUND) { - return Status::KEY_NOT_FOUND; - } - return Status::DB_ERROR; - } - - Value valueOut(retValue); - value = valueOut; - return Status::SUCCESS; -} - -void KvStoreSnapshotImpl::GetEntries(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - std::vector retEntries; - Key trimmedPrefix = Key(Constant::TrimCopy>(prefixKey.Data())); - Key trimmedNext = Key(Constant::TrimCopy>(nextKey.Data())); - // handling parameter errors - if (trimmedPrefix.Size() > Constant::MAX_KEY_LENGTH || trimmedNext.Size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - callback(Status::INVALID_ARGUMENT, retEntries, nextKey); - return; - } - - std::shared_lock lock(snapshotDelegateMutex_); - if (kvStoreSnapshotDelegate_ == nullptr) { - ZLOGE("delegate is null."); - callback(Status::DB_ERROR, retEntries, nextKey); - return; - } - // search the buffer to find if this search has already been buffered. - std::lock_guard entryLock(entriesMutex_); - auto restPair = batchEntries_.begin(); - for (; restPair != batchEntries_.end(); restPair++) { - // firstly compare prefixKey - if (restPair->first == trimmedPrefix.ToString()) { - // secondly compare nextKey - if (restPair->second.front().key.ToString() == trimmedNext.ToString()) { - break; - } - } - } - - if (restPair != batchEntries_.end()) { - // buffer of this search has been found. read and remove returned entries from buffer. - auto &restList = restPair->second; - size_t retSize = 0; - // compute if add next entry to retEntries will let retEntries size exceeds IPC limit. - while (restList.size() > 0 && restList.front().value.Size() + retSize < SOFT_LIMIT) { - retSize += restList.front().key.Size() + IPC_WRITE_AMPLIFICATION + - restList.front().value.Size() + IPC_WRITE_AMPLIFICATION; - retEntries.push_back(restList.front()); - restList.pop_front(); - } - if (restList.size() > 0 && retEntries.size() > 0) { - callback(Status::SUCCESS, retEntries, restList.front().key); - return; - } - batchEntries_.erase(restPair); - if (restList.size() == 0) { - callback(Status::SUCCESS, retEntries, Key("")); - } else { - callback(Status::ILLEGAL_STATE, retEntries, Key("")); - } - return; - } - // holding entriesMutex until GetEntriesFromDelegate() finish. GetEntriesFromDelegate will change batchEntries_ too. - GetEntriesFromDelegateLocked(trimmedPrefix, trimmedNext, callback); -} - -void KvStoreSnapshotImpl::GetEntriesFromDelegateLocked(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - std::vector retEntries; - DistributedDB::DBStatus retValueStatus; - size_t retSize = 0; - Key nextStart; - // the returned entries can be separated into three part: - // part1: the already returned part. this part should be discarded. - // part2: entries to be returned this time. size of this part depends on ipc limit. - // part3: entries that cannot be returned due to ipc limit. part3 should be buffered to batchEntries_. - auto valueCallbackFunction = [&](DistributedDB::DBStatus status, const std::vector &entries) { - ZLOGD("delegate return entry size: %zu", entries.size()); - retValueStatus = status; - auto entry = entries.begin(); - // deal with part1: skip already returned entries. - if (nextKey.Size() != 0) { - while (!(Key(entry->key) == nextKey)) { - entry++; - } - if (entry == entries.end()) { - ZLOGE("search reach end before find nextkey"); - return; - } - } - // deal with part2: put entries to retEntries until put next entry will cause retEntries exceeds its size limit. - for (; entry != entries.end() && entry->value.size() + retSize < SOFT_LIMIT; entry++) { - Entry entryTmp; - entryTmp.key = Key(entry->key); - entryTmp.value = Value(entry->value); - retEntries.push_back(entryTmp); - retSize += entryTmp.value.Size() + IPC_WRITE_AMPLIFICATION + entryTmp.key.Size() + IPC_WRITE_AMPLIFICATION; - } - // all returned entries has been put to retEntries so there will not be a part 3. - if (entry == entries.end() || retEntries.size() == 0) { - nextStart = ""; - return; - } - // deal with part3: - if (batchEntries_.size() >= BUFFER_SIZE) { - // buffer is full. firstly remove the oldest buffer(the last element in batchEntries_). - batchEntries_.pop_back(); - } - nextStart = entry->key; - // secondly move the rest entries to buffer - std::list buffer; - for (; entry != entries.end(); entry++) { - Entry entryTmp; - entryTmp.key = Key(entry->key); - entryTmp.value = Value(entry->value); - buffer.push_back(entryTmp); - } - // thirdly put buffer to the front of batchEntries_, use prefixKey as its key - batchEntries_.push_front(std::make_pair(prefixKey.ToString(), std::move(buffer))); - }; // end of valueCallbackFunction - - std::shared_lock lock(snapshotDelegateMutex_); - DistributedDB::Key dbKey = prefixKey.Data(); - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreSnapshotDelegate_->GetEntries(dbKey, valueCallbackFunction); - } - if (retValueStatus == DistributedDB::DBStatus::NOT_FOUND) { - callback(Status::KEY_NOT_FOUND, retEntries, nextStart); - return; - } - if (retValueStatus != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error: %d.", static_cast(retValueStatus)); - callback(Status::DB_ERROR, retEntries, nextStart); - return; - } - ZLOGD("retEntries size: %zu : %zu.", retEntries.size(), retSize); - callback(Status::SUCCESS, retEntries, nextStart); -} - -void KvStoreSnapshotImpl::GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - ZLOGI("begin."); - std::vector retKeys; - Key trimmedPrefix = Key(Constant::TrimCopy>(prefixKey.Data())); - Key trimmedNext = Key(Constant::TrimCopy>(nextKey.Data())); - if (trimmedPrefix.Size() > Constant::MAX_KEY_LENGTH || trimmedNext.Size() > Constant::MAX_KEY_LENGTH) { - ZLOGE("invalid key."); - callback(Status::INVALID_ARGUMENT, retKeys, nextKey); - return; - } - - std::shared_lock lock(snapshotDelegateMutex_); - if (kvStoreSnapshotDelegate_ == nullptr) { - ZLOGE("delegate is null."); - callback(Status::DB_ERROR, retKeys, nextKey); - return; - } - - std::lock_guard keyLock(keysMutex_); - // search the buffer to find if this search has been buffered - auto restPair = batchKeys_.begin(); - for (; restPair != batchKeys_.end(); restPair++) { - if (restPair->first == trimmedPrefix.ToString()) { - if (restPair->second.front() == trimmedNext) { - break; - } - } - } - - // buffer of this search has been found - if (restPair != batchKeys_.end()) { - auto restList = restPair->second; - size_t retSize = 0; - while (restList.size() > 0 && retSize < SOFT_LIMIT) { - retSize += restList.front().Size() + IPC_WRITE_AMPLIFICATION; - retKeys.push_back(restList.front()); - restList.pop_front(); - } - if (restList.size() > 0) { - callback(Status::SUCCESS, retKeys, restList.front()); - } else { - callback(Status::SUCCESS, retKeys, Key("")); - batchKeys_.erase(restPair); - } - return; - } - - // holding keysMutex until GetKeysFromDelegate() finish. GetKeysFromDelegate will change batchkeys_ too. - GetKeysFromDelegateLocked(trimmedPrefix, trimmedNext, callback); -} - -void KvStoreSnapshotImpl::GetKeysFromDelegateLocked(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - // just the same as GetEntriesFromDelegate - std::vector retKeys; - DistributedDB::DBStatus retValueStatus; - size_t retSize = 0; - Key nextStart; - auto valueCallbackFunction = [&](DistributedDB::DBStatus status, const std::vector &entries) { - ZLOGD("delegate return entry size: %zu", entries.size()); - retValueStatus = status; - auto entry = entries.begin(); - if (nextKey.Size() != 0) { - while (!(Key(entry->key) == nextKey)) { - entry++; - } - } - for (; entry != entries.end() && entry->key.size() + retSize < SOFT_LIMIT; entry++) { - retKeys.push_back(Key(entry->key)); - retSize += entry->key.size() + IPC_WRITE_AMPLIFICATION; - } - - if (entry == entries.end()) { - nextStart = ""; - return; - } - if (batchKeys_.size() >= BUFFER_SIZE) { - batchKeys_.pop_back(); - } - std::list buffer; - nextStart = Key(entry->key); - for (; entry != entries.end(); entry++) { - buffer.push_back(Key(entry->key)); - } - batchKeys_.push_front(std::make_pair(prefixKey.ToString(), std::move(buffer))); - }; - - std::shared_lock lock(snapshotDelegateMutex_); - DistributedDB::Key dbKey = prefixKey.Data(); - { - DdsTrace trace(std::string(LOG_TAG "Delegate::") + std::string(__FUNCTION__)); - kvStoreSnapshotDelegate_->GetEntries(dbKey, valueCallbackFunction); - } - if (retValueStatus == DistributedDB::DBStatus::NOT_FOUND) { - callback(Status::KEY_NOT_FOUND, retKeys, nextStart); - return; - } - if (retValueStatus != DistributedDB::DBStatus::OK) { - ZLOGE("delegate return error: %d.", static_cast(retValueStatus)); - callback(Status::DB_ERROR, retKeys, nextStart); - return; - } - ZLOGD("retKeys size: %zu : %zu.", retKeys.size(), retSize); - callback(Status::SUCCESS, retKeys, nextStart); -} - -Status KvStoreSnapshotImpl::Release(DistributedDB::KvStoreDelegate *kvStoreDelegate) -{ - DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - ZLOGI("Releasing KvStoreSnapshot."); - if (kvStoreDelegate == nullptr) { - return Status::INVALID_ARGUMENT; - } - std::shared_lock lock(snapshotDelegateMutex_); - DistributedDB::DBStatus status = kvStoreDelegate->ReleaseKvStoreSnapshot(kvStoreSnapshotDelegate_); - if (status != DistributedDB::DBStatus::OK) { - ZLOGE("Error occurs during Releasing KvStoreSnapshot, error code %d.", status); - return Status::DB_ERROR; - } - kvStoreSnapshotDelegate_ = nullptr; - return Status::SUCCESS; -} - -Status KvStoreSnapshotImpl::MigrateKvStore(DistributedDB::KvStoreDelegate *kvStoreDelegate) -{ - if (kvStoreDelegate == nullptr) { - return Status::INVALID_ARGUMENT; - } - ZLOGI("begin."); - DistributedDB::KvStoreSnapshotDelegate *snapshotDelegate = nullptr; - DistributedDB::DBStatus dbStatus; - auto snapshotCallbackFunction = [&](DistributedDB::DBStatus status, - DistributedDB::KvStoreSnapshotDelegate *snapshot) { - dbStatus = status; - snapshotDelegate = snapshot; - }; - std::unique_lock lock(snapshotDelegateMutex_); - kvStoreDelegate->GetKvStoreSnapshot(kvStoreObserverImpl_, snapshotCallbackFunction); - if (dbStatus != DistributedDB::DBStatus::OK || snapshotDelegate == nullptr) { - ZLOGE("delegate return nullptr or errcode, dbStatus:%d.", static_cast(dbStatus)); - return Status::DB_ERROR; - } - - kvStoreSnapshotDelegate_ = snapshotDelegate; - return Status::SUCCESS; -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_snapshot_impl.h b/services/distributeddataservice/app/src/kvstore_snapshot_impl.h deleted file mode 100644 index 74f385f57561049ea8729d98ba6c37c8e611abe3..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/app/src/kvstore_snapshot_impl.h +++ /dev/null @@ -1,84 +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_SNAPSHOT_IMPL_H -#define KVSTORE_SNAPSHOT_IMPL_H - -#include -#include -#include -#include -#include -#include -#include "kv_store_delegate.h" -#include "ikvstore_snapshot.h" -#include "kv_store_snapshot_delegate.h" -#include "types.h" -#include "kvstore_observer_impl.h" - -namespace OHOS { -namespace DistributedKv { -class KvStoreSnapshotImpl : public KvStoreSnapshotImplStub { -public: - explicit KvStoreSnapshotImpl(DistributedDB::KvStoreSnapshotDelegate *kvStoreSnapshotDelegate, - KvStoreObserverImpl *kvStoreObserverImpl); - - virtual ~KvStoreSnapshotImpl(); - - Status Get(const Key &key, Value &value) override; - - void GetEntries(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) override; - - void GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) override; - - Status Release(DistributedDB::KvStoreDelegate *kvStoreDelegate); - - Status MigrateKvStore(DistributedDB::KvStoreDelegate *kvStoreDelegate); - -private: - // distributeddb is responsible for free kvStoreSnapshotDelegate_, - // by calling ReleaseKvStoreSnapshot in kvstore, - // can not free it in KvStoreSnapshotImpl's destructor. - mutable std::shared_mutex snapshotDelegateMutex_{}; - DistributedDB::KvStoreSnapshotDelegate *kvStoreSnapshotDelegate_; - KvStoreObserverImpl *kvStoreObserverImpl_; - // write amplification of each write parcel operation. currently zero. - static constexpr unsigned int IPC_WRITE_AMPLIFICATION = 0; - - // max size of returned entries or keys. size of the key and IPC_WRITE_AMPLIFICATION of the last element is ignored. - // IPC limit is 819200 currently. SOFT_LIMIT should be smaller than IPC limit. - static constexpr unsigned int SOFT_LIMIT = 750000; - - // max size of batchEntries_ and batchKeys_ - static constexpr unsigned int BUFFER_SIZE = 3; - - // temporarily storage entries of an unfinished search. - std::list>> batchEntries_; - std::mutex entriesMutex_; - void GetEntriesFromDelegateLocked(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback); - - // temporarily storage keys of an unfinished search. - std::list>> batchKeys_; - std::mutex keysMutex_; - void GetKeysFromDelegateLocked(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback); -}; -} // namespace DistributedKv -} // namespace OHOS - -#endif // KVSTORE_SNAPSHOT_IMPL_H diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index d8aaf12eee8bd1af0dfd4a5ed77efd4e640a9d05..0b691049ea3706abf3866cc7b41dfa571985751a 100644 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -1071,150 +1071,6 @@ Status SingleKvStoreImpl::ForceClose(DistributedDB::KvStoreDelegateManager *kvSt return Status::ERROR; } -Status SingleKvStoreImpl::MigrateKvStore(const std::string &harmonyAccountId, - const std::string &kvStoreDataDir, - DistributedDB::KvStoreDelegateManager *oldDelegateMgr, - DistributedDB::KvStoreDelegateManager *&newDelegateMgr) -{ - ZLOGI("begin."); - std::unique_lock lock(storeNbDelegateMutex_); - 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); }); - bool outdated = false; // ignore outdated flag during rebuild kvstore. - auto metaSecretKey = KvStoreMetaManager::GetMetaKey(deviceAccountId_, "default", bundleName_, storeId_, - "SINGLE_KEY"); - if (options_.encrypt) { - KvStoreMetaManager::GetInstance().GetSecretKeyFromMeta(metaSecretKey, secretKey, outdated); - if (secretKey.empty()) { - ZLOGE("Get secret key from meta kvstore failed."); - return Status::CRYPT_ERROR; - } - } - - DistributedDB::DBStatus dbStatus; - DistributedDB::KvStoreNbDelegate::Option dbOption; - Status status = KvStoreAppManager::InitNbDbOption(options_, secretKey, dbOption); - if (status != Status::SUCCESS) { - ZLOGE("InitNbDbOption 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; - kvStoreConfig.dataDir = kvStoreDataDir; - newDelegateMgr->SetKvStoreConfig(kvStoreConfig); - } - DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate = nullptr; // new KvStoreNbDelegate get from distributed DB. - newDelegateMgr->GetKvStore( - storeId_, dbOption, - [&](DistributedDB::DBStatus status, DistributedDB::KvStoreNbDelegate *delegate) { - kvStoreNbDelegate = delegate; - dbStatus = status; - }); - if (kvStoreNbDelegate == nullptr) { - ZLOGE("storeDelegate is nullptr, dbStatusTmp: %d", static_cast(dbStatus)); - return Status::DB_ERROR; - } - - if (options_.autoSync) { - bool autoSync = true; - auto data = static_cast(&autoSync); - auto pragmaStatus = kvStoreNbDelegate->Pragma(DistributedDB::PragmaCmd::AUTO_SYNC, data); - if (pragmaStatus != DistributedDB::DBStatus::OK) { - ZLOGE("pragmaStatus: %d", static_cast(pragmaStatus)); - } - } - - status = RebuildKvStoreObserver(kvStoreNbDelegate); - if (status != Status::SUCCESS) { - ZLOGI("rebuild KvStore observer failed, errCode %d.", static_cast(status)); - // skip this failed, continue to do other rebuild process. - } - - status = RebuildKvStoreResultSet(); - if (status != Status::SUCCESS) { - ZLOGI("rebuild KvStore resultset failed, errCode %d.", static_cast(status)); - // skip this failed, continue to do close kvstore process. - } - - ZLOGI("close old KvStore."); - dbStatus = oldDelegateMgr->CloseKvStore(kvStoreNbDelegate_); - if (dbStatus != DistributedDB::DBStatus::OK) { - ZLOGI("rebuild KvStore failed during close KvStore, errCode %d.", static_cast(status)); - newDelegateMgr->CloseKvStore(kvStoreNbDelegate); - return Status::DB_ERROR; - } - - ZLOGI("update kvstore delegate."); - kvStoreNbDelegate_ = kvStoreNbDelegate; - return Status::SUCCESS; -} - -Status SingleKvStoreImpl::RebuildKvStoreObserver(DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate) -{ - ZLOGI("rebuild observer."); - if (kvStoreNbDelegate_ == nullptr || kvStoreNbDelegate == nullptr) { - ZLOGI("RebuildKvStoreObserver illlegal."); - return Status::ILLEGAL_STATE; - } - std::lock_guard observerMapLockGuard(observerMapMutex_); - Status status = Status::SUCCESS; - DistributedDB::DBStatus dbStatus; - DistributedDB::Key emptyKey; - for (const auto &observerPair : observerMap_) { - dbStatus = kvStoreNbDelegate_->UnRegisterObserver(observerPair.second); - if (dbStatus != DistributedDB::OK) { - status = Status::DB_ERROR; - ZLOGW("rebuild observer failed during UnRegisterObserver, status %d.", static_cast(dbStatus)); - continue; - } - dbStatus = kvStoreNbDelegate->RegisterObserver(emptyKey, - static_cast(ConvertToDbObserverMode(observerPair.second->GetSubscribeType())), - observerPair.second); - if (dbStatus != DistributedDB::OK) { - status = Status::DB_ERROR; - ZLOGW("rebuild observer failed during RegisterObserver, status %d.", static_cast(dbStatus)); - continue; - } - } - return status; -} - -Status SingleKvStoreImpl::RebuildKvStoreResultSet() -{ - if (kvStoreNbDelegate_ == nullptr) { - return Status::INVALID_ARGUMENT; - } - ZLOGI("rebuild resultset"); - std::lock_guard lg(storeResultSetMutex_); - Status retStatus = Status::SUCCESS; - for (const auto &resultSetPair : storeResultSetMap_) { - Status status = (resultSetPair.second)->MigrateKvStore(kvStoreNbDelegate_); - if (status != Status::SUCCESS) { - retStatus = status; - ZLOGW("rebuild resultset failed, errCode %d", static_cast(status)); - continue; - } - } - return retStatus; -} - Status SingleKvStoreImpl::ReKey(const std::vector &key) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index 279fc94486b17d7e9ae2314accb2fb1a5e38dac8..3763fac2a50ea59b4a7d6b250fa4d2153ef92d6e 100644 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -61,9 +61,6 @@ public: Status ReKey(const std::vector &key); 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 PutBatch(const std::vector &entries) override; Status DeleteBatch(const std::vector &keys) override; @@ -100,8 +97,6 @@ private: uint64_t sequenceId); Status DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId); - Status RebuildKvStoreObserver(DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate); - Status RebuildKvStoreResultSet(); int ConvertToDbObserverMode(SubscribeType subscribeType) const; DistributedDB::SyncMode ConvertToDbSyncMode(SyncMode syncMode) const; Status DoSubscribe(const std::vector &deviceIds, @@ -120,8 +115,6 @@ private: // kvstore options. const Options options_; - // kvstore cipherKey. - const std::vector cipherKey_; // deviceAccount id get from service std::string deviceAccountId_; // appId get from PMS. @@ -136,7 +129,6 @@ private: // for top-app, 0 means synchronization immediately. for others, 0 means 1000ms. uint32_t defaultSyncDelayMs_{ 0 }; std::atomic_uint32_t waitingSyncCount_{ 0 }; - std::atomic_uint32_t waitingAutoSyncCount_{ 0 }; std::atomic_uint32_t syncRetries_{ 0 }; std::vector lastSyncDeviceIds_{ }; SyncMode lastSyncMode_{ SyncMode::PULL };