diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp index a20d8151d1e816e2ba67b6daf0b45ab58b9106c8..bc9051a3bc7f79eef3a95c7098a434a06dffc21b 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp @@ -21,39 +21,40 @@ namespace OHOS { namespace DistributedKv { -ChangeNotification::ChangeNotification(const std::list &insertEntries, const std::list &updateEntries, - const std::list &deleteEntries, const std::string &deviceId, - const bool isClear) - : insertEntries_(insertEntries), updateEntries_(updateEntries), deleteEntries_(deleteEntries), - deviceId_(deviceId), isClear_(isClear) -{} +ChangeNotification::ChangeNotification(std::vector &&insertEntries, std::vector &&updateEntries, + std::vector &&deleteEntries, const std::string &deviceId, bool isClear) + : insertEntries_(std::move(insertEntries)), updateEntries_(std::move(updateEntries)), + deleteEntries_(std::move(deleteEntries)), deviceId_(deviceId), isClear_(isClear) +{ + +} ChangeNotification::~ChangeNotification() {} -const std::list &ChangeNotification::GetInsertEntries() const +const std::vector &ChangeNotification::GetInsertEntries() const { - return this->insertEntries_; + return insertEntries_; } -const std::list &ChangeNotification::GetUpdateEntries() const +const std::vector &ChangeNotification::GetUpdateEntries() const { - return this->updateEntries_; + return updateEntries_; } -const std::list &ChangeNotification::GetDeleteEntries() const +const std::vector &ChangeNotification::GetDeleteEntries() const { - return this->deleteEntries_; + return deleteEntries_; } const std::string &ChangeNotification::GetDeviceId() const { - return this->deviceId_; + return deviceId_; } bool ChangeNotification::IsClear() const { - return this->isClear_; + return isClear_; } bool ChangeNotification::Marshalling(Parcel &parcel) const @@ -101,9 +102,9 @@ bool ChangeNotification::Marshalling(Parcel &parcel) const ChangeNotification *ChangeNotification::Unmarshalling(Parcel &parcel) { - std::list insertEntries; - std::list updateEntries; - std::list deleteEntries; + std::vector insertEntries; + std::vector updateEntries; + std::vector deleteEntries; int lenInsert = parcel.ReadInt32(); if (lenInsert < 0) { @@ -151,9 +152,8 @@ ChangeNotification *ChangeNotification::Unmarshalling(Parcel &parcel) } std::string deviceId = parcel.ReadString(); bool isClear = parcel.ReadBool(); - ChangeNotification *changeNotification = - new ChangeNotification(insertEntries, updateEntries, deleteEntries, deviceId, isClear); - return changeNotification; + + return new ChangeNotification(std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), deviceId, isClear); } } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp index 1ac47434e5a32825222739180180274b0f3c12f8..90a49511f7f2301d34831e6d61383761fa1bd34a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/distributed_kv_data_manager.cpp @@ -35,16 +35,16 @@ DistributedKvDataManager::DistributedKvDataManager() DistributedKvDataManager::~DistributedKvDataManager() {} -void DistributedKvDataManager::GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) +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) { - callback(Status::INVALID_ARGUMENT, nullptr); ZLOGE("invalid storeId."); - return; + return Status::INVALID_ARGUMENT; } KvStoreServiceDeathNotifier::SetAppId(appId); @@ -52,8 +52,7 @@ void DistributedKvDataManager::GetKvStore(const Options &options, const AppId &a Status status = Status::SERVER_UNAVAILABLE; if (kvDataServiceProxy == nullptr) { ZLOGE("proxy is nullptr."); - callback(status, nullptr); - return; + return status; } ZLOGD("call proxy."); @@ -62,35 +61,33 @@ void DistributedKvDataManager::GetKvStore(const Options &options, const AppId &a [&](sptr proxy) { proxyTmp = std::move(proxy); }); if (status == Status::RECOVER_SUCCESS) { ZLOGE("proxy recover success: %d", static_cast(status)); - callback(status, std::make_unique(std::move(proxyTmp), storeIdTmp)); - return; + kvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); + return status; } if (status != Status::SUCCESS) { ZLOGE("proxy return error: %d", static_cast(status)); - callback(status, nullptr); - return; + return status; } if (proxyTmp == nullptr) { ZLOGE("proxy return nullptr."); - callback(status, nullptr); - return; + return status; } - - callback(status, std::make_unique(std::move(proxyTmp), storeIdTmp)); + kvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); + return status; } -void DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback) +Status DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, + std::shared_ptr &singleKvStore) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); + singleKvStore = nullptr; std::string storeIdTmp = Constant::TrimCopy(storeId.storeId); if (storeIdTmp.size() == 0 || storeIdTmp.size() > Constant::MAX_STORE_ID_LENGTH) { - callback(Status::INVALID_ARGUMENT, nullptr); ZLOGE("invalid storeId."); - return; + return Status::INVALID_ARGUMENT; } KvStoreServiceDeathNotifier::SetAppId(appId); @@ -98,8 +95,7 @@ void DistributedKvDataManager::GetSingleKvStore(const Options &options, const Ap Status status = Status::SERVER_UNAVAILABLE; if (kvDataServiceProxy == nullptr) { ZLOGE("proxy is nullptr."); - callback(status, nullptr); - return; + return status; } ZLOGD("call proxy."); @@ -108,27 +104,24 @@ void DistributedKvDataManager::GetSingleKvStore(const Options &options, const Ap [&](sptr proxy) { proxyTmp = std::move(proxy); }); if (status == Status::RECOVER_SUCCESS) { ZLOGE("proxy recover success: %d", static_cast(status)); - callback(status, std::make_unique(std::move(proxyTmp), storeIdTmp)); - return; + singleKvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); + return status; } if (status != Status::SUCCESS) { ZLOGE("proxy return error: %d", static_cast(status)); - callback(status, nullptr); - return; + return status; } if (proxyTmp == nullptr) { ZLOGE("proxy return nullptr."); - callback(status, nullptr); - return; + return status; } - callback(status, std::make_unique(std::move(proxyTmp), storeIdTmp)); + singleKvStore = std::make_shared(std::move(proxyTmp), storeIdTmp); + return status; } - -void DistributedKvDataManager::GetAllKvStoreId(const AppId &appId, - std::function &)> callback) +Status DistributedKvDataManager::GetAllKvStoreId(const AppId &appId, std::vector &storeIds) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); @@ -136,16 +129,19 @@ void DistributedKvDataManager::GetAllKvStoreId(const AppId &appId, sptr kvDataServiceProxy = KvStoreServiceDeathNotifier::GetDistributedKvDataService(); if (kvDataServiceProxy == nullptr) { ZLOGE("proxy is nullptr."); - std::vector storeIds; - callback(Status::SERVER_UNAVAILABLE, storeIds); - return; + return Status::SERVER_UNAVAILABLE; } - kvDataServiceProxy->GetAllKvStoreId(appId, callback); + Status status; + kvDataServiceProxy->GetAllKvStoreId(appId, [&status, &storeIds](auto statusTmp, auto &ids) { + status = statusTmp; + storeIds = std::move(ids); + }); + return status; } Status DistributedKvDataManager::CloseKvStore(const AppId &appId, const StoreId &storeId, - std::unique_ptr kvStorePtr) + std::shared_ptr kvStorePtr) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); @@ -164,7 +160,7 @@ Status DistributedKvDataManager::CloseKvStore(const AppId &appId, const StoreId return Status::SERVER_UNAVAILABLE; } -Status DistributedKvDataManager::CloseKvStore(const AppId &appId, std::unique_ptr kvStorePtr) +Status DistributedKvDataManager::CloseKvStore(const AppId &appId, std::shared_ptr kvStorePtr) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp index 53ed766b9af9287c28f090dda473c84ba4a41826..834982f7eef82fa4c1780db3283b36e70caf0392 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_observer.cpp @@ -32,7 +32,7 @@ enum { KvStoreObserverProxy::KvStoreObserverProxy(const sptr &impl) : IRemoteProxy(impl) {} -int64_t GetBufferSize(const std::list &entries) +int64_t GetBufferSize(const std::vector &entries) { int64_t bufferSize = 0; for (const auto &item : entries) { @@ -41,7 +41,7 @@ int64_t GetBufferSize(const std::list &entries) return bufferSize; } -bool WriteEntryToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, const std::list &list) +bool WriteEntryToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, const std::vector &list) { std::unique_ptr buffer(new uint8_t[bufferSize], [](uint8_t *ptr) { delete[] ptr; }); if (buffer == nullptr) { @@ -64,7 +64,7 @@ bool WriteEntryToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, con return true; } -bool WriteListToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, const std::list &list) +bool WriteListToParcelByBuf(MessageParcel &data, const int64_t &bufferSize, const std::vector &list) { if (!data.WriteInt32(list.size()) || !data.WriteInt32(bufferSize)) { @@ -129,7 +129,7 @@ void KvStoreObserverProxy::OnChange(const ChangeNotification &changeNotification } } -bool ReadFromBuff(MessageParcel &data, const int &len, const int &bufferSize, std::list &entries) +bool ReadFromBuff(MessageParcel &data, const int &len, const int &bufferSize, std::vector &entries) { const uint8_t *buffer = reinterpret_cast(data.ReadRawData(bufferSize)); if (buffer == nullptr) { @@ -150,7 +150,7 @@ bool ReadFromBuff(MessageParcel &data, const int &len, const int &bufferSize, st return true; } -bool ReadListFromBuf(MessageParcel &data, std::list &entries) +bool ReadListFromBuf(MessageParcel &data, std::vector &entries) { int len = data.ReadInt32(); if (len < 0) { @@ -196,21 +196,21 @@ int32_t KvStoreObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, OnChange(*changeNotification, nullptr); } } else { - std::list insertEntries; + std::vector insertEntries; bool result = ReadListFromBuf(data, insertEntries); if (!result) { ZLOGE("read insertList from buff filed"); return errorResult; } - std::list updateEntries; + std::vector updateEntries; result = ReadListFromBuf(data, updateEntries); if (!result) { ZLOGE("read updateList from buff filed"); return errorResult; } - std::list deleteEntries; + std::vector deleteEntries; result = ReadListFromBuf(data, deleteEntries); if (!result) { ZLOGE("read deleteList from buff filed"); @@ -219,7 +219,7 @@ int32_t KvStoreObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, std::string deviceId = data.ReadString(); bool isClear = data.ReadBool(); - ChangeNotification changeNotification(insertEntries, updateEntries, deleteEntries, deviceId, isClear); + ChangeNotification changeNotification(std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), deviceId, isClear); sptr remote = data.ReadRemoteObject(); if (remote != nullptr) { sptr kvStoreSnapshotProxy = iface_cast(remote); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp index ac0e8e71c583f284c9cb45db0d91b01ef78ec956..4737df8ae00e411b52820dfd67966e42b613219e 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp @@ -41,23 +41,23 @@ StoreId KvStoreClient::GetStoreId() const storeId.storeId = storeId_; return storeId; } - -void KvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, - std::function)> callback) const +Status KvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, + std::shared_ptr &snapshot) const { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - Status statusTmp = Status::SERVER_UNAVAILABLE; + + snapshot = nullptr; if (kvStoreProxy_ == nullptr) { ZLOGE("kvstore proxy is nullptr."); - callback(statusTmp, nullptr); - return; + return Status::SERVER_UNAVAILABLE; } sptr kvStoreObserverClient = new KvStoreObserverClient(GetStoreId(), SubscribeType::SUBSCRIBE_TYPE_ALL, observer, KvStoreType::MULTI_VERSION); sptr snapshotProxyTmp; + Status statusTmp = Status::SERVER_UNAVAILABLE; auto snapshotCallbackFunction = [&](Status status, sptr snapshotProxy) { statusTmp = status; snapshotProxyTmp = snapshotProxy; @@ -65,21 +65,20 @@ void KvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer kvStoreProxy_->GetKvStoreSnapshot(kvStoreObserverClient, snapshotCallbackFunction); if (statusTmp != Status::SUCCESS) { ZLOGE("return error: %d.", static_cast(statusTmp)); - callback(statusTmp, nullptr); - return; + return statusTmp; } if (snapshotProxyTmp == nullptr) { ZLOGE("snapshotProxyTmp is nullptr."); - callback(statusTmp, nullptr); - return; + return statusTmp; } ZLOGD("success."); - callback(statusTmp, std::make_unique(std::move(snapshotProxyTmp))); + snapshot = std::make_shared(std::move(snapshotProxyTmp)); + return statusTmp; } -Status KvStoreClient::ReleaseKvStoreSnapshot(std::unique_ptr kvStoreSnapshotPtr) +Status KvStoreClient::ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); @@ -87,17 +86,16 @@ Status KvStoreClient::ReleaseKvStoreSnapshot(std::unique_ptr kv ZLOGE("kvstore proxy is nullptr."); return Status::SERVER_UNAVAILABLE; } - if (kvStoreSnapshotPtr == nullptr) { + if (snapshot == nullptr) { ZLOGE("kvstoresnapshot is nullptr."); return Status::INVALID_ARGUMENT; } KvStoreSnapshotClient *kvStoreSnapshotClient = - reinterpret_cast(kvStoreSnapshotPtr.release()); + reinterpret_cast(snapshot.get()); sptr snapshotProxyTmp = kvStoreSnapshotClient->GetkvStoreSnapshotProxy(); Status status = kvStoreProxy_->ReleaseKvStoreSnapshot(std::move(snapshotProxyTmp)); - delete kvStoreSnapshotClient; - kvStoreSnapshotClient = nullptr; + snapshot = nullptr; ZLOGI("return: %d.", static_cast(status)); return status; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h index 01d81a615a7ed8af294c7acb33da41d82253dde5..bc964eb2455f27877a5ea8ec6391a5c92ba49f0d 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.h @@ -34,10 +34,10 @@ public: StoreId GetStoreId() const override; - void GetKvStoreSnapshot(std::shared_ptr observer, - std::function)> callback) const override; + Status GetKvStoreSnapshot(std::shared_ptr observer, + std::shared_ptr &snapshot) const override; - Status ReleaseKvStoreSnapshot(std::unique_ptr kvStoreSnapshotPtr) override; + Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) override; Status Put(const Key &key, const Value &value) override; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp index 0a64baff0e6a474a5b7f17687d40c5575f69cc36..75e31030cdbd8c6129670da540b4b87919dd59e6 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp @@ -36,115 +36,104 @@ KvStoreSnapshotClient::~KvStoreSnapshotClient() ZLOGI("destruct"); } -void KvStoreSnapshotClient::GetEntries(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) +Status KvStoreSnapshotClient::GetEntries(const Key &prefixKey, Key &nextKey, std::vector &entries) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - std::vector entries; std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid prefixKey."); - callback(Status::INVALID_ARGUMENT, entries, nextKey); - return; + return Status::INVALID_ARGUMENT; } keyData = Constant::TrimCopy>(nextKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid nextKey."); - callback(Status::INVALID_ARGUMENT, entries, nextKey); - return; + return Status::INVALID_ARGUMENT; } + Status status = Status::SERVER_UNAVAILABLE; if (kvStoreSnapshotProxy_ != nullptr) { - kvStoreSnapshotProxy_->GetEntries(prefixKey, nextKey, callback); - return; + kvStoreSnapshotProxy_->GetEntries(prefixKey, nextKey, [&](Status stat, auto &result, const auto &next) { + status = stat; + entries = result; + nextKey = next; + }); + } else { + ZLOGE("snapshot proxy is nullptr."); } - - ZLOGE("snapshot proxy is nullptr."); - callback(Status::SERVER_UNAVAILABLE, entries, nextKey); + return status; } -void KvStoreSnapshotClient::GetEntries(const Key &prefixKey, std::function &)> callback) +Status KvStoreSnapshotClient::GetEntries(const Key &prefixKey, std::vector &entries) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - std::vector allEntries; std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid prefixKey."); - callback(Status::INVALID_ARGUMENT, allEntries); - return; + return Status::INVALID_ARGUMENT; } if (kvStoreSnapshotProxy_ == nullptr) { ZLOGE("snapshot proxy is nullptr."); - callback(Status::SERVER_UNAVAILABLE, allEntries); - return; + return Status::SERVER_UNAVAILABLE; } Key startKey = ""; Status allStatus = Status::ERROR; do { kvStoreSnapshotProxy_->GetEntries(prefixKey, startKey, - [&allStatus, &allEntries, &startKey](Status stat, std::vector &entries, Key next) { + [&allStatus, &entries, &startKey](Status stat, auto &result, Key next) { allStatus = stat; if (stat != Status::SUCCESS) { return; } - for (const auto &entry : entries) { - allEntries.push_back(entry); - } - startKey = next; - if (entries.empty()) { - startKey = ""; - } + entries = std::move(result); + startKey = entries.empty() ? "" : next; }); } while (allStatus == Status::SUCCESS && startKey.ToString() != ""); if (allStatus != Status::SUCCESS) { ZLOGW("Error occurs during GetEntries."); - allEntries.clear(); + entries.clear(); } - callback(allStatus, allEntries); + return allStatus; } -void KvStoreSnapshotClient::GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) +Status KvStoreSnapshotClient::GetKeys(const Key &prefixKey, Key &nextKey, std::vector &keys) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - std::vector keys; std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid prefixKey."); - callback(Status::INVALID_ARGUMENT, keys, nextKey); - return; + return Status::INVALID_ARGUMENT; } keyData = Constant::TrimCopy>(nextKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid nextKey."); - callback(Status::INVALID_ARGUMENT, keys, nextKey); - return; + return Status::INVALID_ARGUMENT; } + Status status = Status::SERVER_UNAVAILABLE; if (kvStoreSnapshotProxy_ != nullptr) { - kvStoreSnapshotProxy_->GetKeys(prefixKey, nextKey, callback); - return; + kvStoreSnapshotProxy_->GetKeys(prefixKey, nextKey, [&](Status stat, auto &result, const auto &next) { + status = stat; + keys = std::move(result); + nextKey = next; + }); + } else { + ZLOGE("snapshot proxy is nullptr."); } - ZLOGE("snapshot proxy is nullptr."); - callback(Status::SERVER_UNAVAILABLE, keys, nextKey); + return status; } -void KvStoreSnapshotClient::GetKeys(const Key &prefixKey, std::function &)> callback) +Status KvStoreSnapshotClient::GetKeys(const Key &prefixKey, std::vector &entries) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - std::vector allKeys; std::vector keyData = Constant::TrimCopy>(prefixKey.Data()); if (keyData.size() > Constant::MAX_KEY_LENGTH) { ZLOGE("invalid prefixKey."); - callback(Status::INVALID_ARGUMENT, allKeys); - return; + return Status::INVALID_ARGUMENT; } if (kvStoreSnapshotProxy_ == nullptr) { ZLOGE("snapshot proxy is nullptr."); - callback(Status::SERVER_UNAVAILABLE, allKeys); - return; + return Status::SERVER_UNAVAILABLE; } Key startKey = ""; Status allStatus = Status::ERROR; @@ -155,7 +144,7 @@ void KvStoreSnapshotClient::GetKeys(const Key &prefixKey, std::function &, const Key &)> callback) override; + Status GetEntries(const Key &prefixKey, Key &nextKey, std::vector &entries) override; - void GetEntries(const Key &prefixKey, std::function &)> callback) override; + Status GetEntries(const Key &prefixKey, std::vector &entries) override; - void GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) override; + Status GetKeys(const Key &prefixKey, Key &nextKey, std::vector &keys) override; - void GetKeys(const Key &prefixKey, std::function &)> callback) override; + Status GetKeys(const Key &prefixKey, std::vector &entries) override; Status Get(const Key &key, Value &value) override; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index b89be66eee43fb7f8dd81fd65227672bd76da7e7..46b4cb8c10a1fc7656fc9001e7aad1e864b07138 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -64,16 +64,14 @@ Status SingleKvStoreClient::GetEntriesWithQuery(const DataQuery &query, std::vec return GetEntriesWithQuery(query.ToString(), entries); } -void SingleKvStoreClient::GetResultSet(const Key &prefixKey, - std::function)> callback) const +Status SingleKvStoreClient::GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - + resultSet = nullptr; Status statusTmp = Status::SERVER_UNAVAILABLE; if (kvStoreProxy_ == nullptr) { ZLOGE("kvstore proxy is nullptr."); - callback(statusTmp, nullptr); - return; + return statusTmp; } sptr resultSetTmp; auto callFun = [&](Status status, sptr proxy) { @@ -83,28 +81,26 @@ void SingleKvStoreClient::GetResultSet(const Key &prefixKey, kvStoreProxy_->GetResultSet(prefixKey, callFun); if (statusTmp != Status::SUCCESS) { ZLOGE("return error: %d.", static_cast(statusTmp)); - callback(statusTmp, nullptr); - return; + return statusTmp; } if (resultSetTmp == nullptr) { ZLOGE("resultSetTmp is nullptr."); - callback(statusTmp, nullptr); - return; + return statusTmp; } - callback(statusTmp, std::make_unique(std::move(resultSetTmp))); + resultSet = std::make_shared(std::move(resultSetTmp)); + return statusTmp; } -void SingleKvStoreClient::GetResultSetWithQuery(const std::string &query, - std::function)> callback) const +Status SingleKvStoreClient::GetResultSetWithQuery(const std::string &query, std::shared_ptr &resultSet) const { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); + resultSet = nullptr; Status statusTmp = Status::SERVER_UNAVAILABLE; if (kvStoreProxy_ == nullptr) { ZLOGE("kvstore proxy is nullptr."); - callback(statusTmp, nullptr); - return; + return statusTmp; } ZLOGD("Cpp client GetResultSetWithQuery"); @@ -116,30 +112,28 @@ void SingleKvStoreClient::GetResultSetWithQuery(const std::string &query, kvStoreProxy_->GetResultSetWithQuery(query, callFun); if (statusTmp != Status::SUCCESS) { ZLOGE("return error: %d.", static_cast(statusTmp)); - callback(statusTmp, nullptr); - return; + return statusTmp; } if (resultSetTmp == nullptr) { ZLOGE("resultSetTmp is nullptr."); - callback(statusTmp, nullptr); - return; + return statusTmp; } - callback(statusTmp, std::make_unique(std::move(resultSetTmp))); ZLOGE("GetResultSetWithQuery"); + resultSet = std::make_shared(std::move(resultSetTmp)); + return statusTmp; } -void SingleKvStoreClient::GetResultSetWithQuery(const DataQuery &query, - std::function)> callback) const +Status SingleKvStoreClient::GetResultSetWithQuery(const DataQuery &query, std::shared_ptr &resultSet) const { - GetResultSetWithQuery(query.ToString(), callback); + return GetResultSetWithQuery(query.ToString(), resultSet); } -Status SingleKvStoreClient::CloseResultSet(std::unique_ptr resultSet) +Status SingleKvStoreClient::CloseResultSet(std::shared_ptr &resultSet) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - - if (resultSet == nullptr) { + auto resultSetTmp = std::move(resultSet); + if (resultSetTmp == nullptr) { ZLOGE("resultSet is nullptr."); return Status::INVALID_ARGUMENT; } @@ -147,7 +141,7 @@ Status SingleKvStoreClient::CloseResultSet(std::unique_ptr res ZLOGE("kvstore proxy is nullptr."); return Status::SERVER_UNAVAILABLE; } - auto resultSetClient = reinterpret_cast(resultSet.release()); + auto resultSetClient = reinterpret_cast(resultSetTmp.get()); return kvStoreProxy_->CloseResultSet(resultSetClient->GetKvStoreResultSetProxy()); } @@ -393,30 +387,31 @@ Status SingleKvStoreClient::Rollback() Status SingleKvStoreClient::SetSyncParam(const KvSyncParam &syncParam) { KvParam input(TransferTypeToByteArray(syncParam)); - sptr output = nullptr; + KvParam output; return Control(KvControlCmd::SET_SYNC_PARAM, input, output); } Status SingleKvStoreClient::GetSyncParam(KvSyncParam &syncParam) { KvParam inputEmpty; - sptr output = nullptr; + KvParam output; Status ret = Control(KvControlCmd::GET_SYNC_PARAM, inputEmpty, output); if (ret != Status::SUCCESS) { return ret; } - if ((output != nullptr) && (output->Size() == sizeof(syncParam))) { - syncParam = TransferByteArrayToType(output->Data()); + if (output.Size() == sizeof(syncParam)) { + syncParam = TransferByteArrayToType(output.Data()); return Status::SUCCESS; } return Status::ERROR; } -Status SingleKvStoreClient::Control(KvControlCmd cmd, const KvParam &inputParam, sptr &output) +Status SingleKvStoreClient::Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &output) { ZLOGI("begin."); if (kvStoreProxy_ != nullptr) { - return kvStoreProxy_->Control(cmd, inputParam, output); + sptr kvParam; + return kvStoreProxy_->Control(cmd, inputParam, kvParam); } ZLOGE("singleKvstore proxy is nullptr."); return Status::SERVER_UNAVAILABLE; @@ -448,4 +443,17 @@ Status SingleKvStoreClient::GetSecurityLevel(SecurityLevel &securityLevel) const ZLOGE("singleKvstore proxy is nullptr."); return Status::SERVER_UNAVAILABLE; } +Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, + std::shared_ptr &snapshot) const +{ + return Status::NOT_SUPPORT; +} +Status SingleKvStoreClient::ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) +{ + return Status::NOT_SUPPORT; +} +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 ab8034d8efa6951d9ddb3bed40e2c7f170490395..633649f9af3ed4f787e17e0e554682bc95cd6930 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -36,16 +36,13 @@ public: Status GetEntriesWithQuery(const DataQuery &query, std::vector &entries) const override; - void GetResultSet(const Key &prefixKey, - std::function)> callback) const override; + Status GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const override; - void GetResultSetWithQuery(const std::string &query, - std::function)> callback) const override; + Status GetResultSetWithQuery(const std::string &query, std::shared_ptr &resultSet) const override; - void GetResultSetWithQuery(const DataQuery &query, - std::function)> callback) const override; + Status GetResultSetWithQuery(const DataQuery &query, std::shared_ptr &resultSet) const override; - Status CloseResultSet(std::unique_ptr resultSet) override; + Status CloseResultSet(std::shared_ptr &resultSet) override; Status GetCountWithQuery(const std::string &query, int &result) const override; @@ -87,8 +84,12 @@ public: const std::vector &remoteSupportLabels) const override; Status GetSecurityLevel(SecurityLevel &securityLevel) const override; + Status GetKvStoreSnapshot(std::shared_ptr observer, + std::shared_ptr &snapshot) const override; + Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) override; + Status Clear() override; protected: - Status Control(KvControlCmd cmd, const KvParam &inputParam, sptr &outputParam) override; + Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam) override; private: sptr kvStoreProxy_; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_encrypt_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_encrypt_test.cpp index 87ee17a4ac0fed5fff374ea46c7606a4ef127366..d779501cf8cc4295323dec9b910a64479d171669 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_encrypt_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_encrypt_test.cpp @@ -106,31 +106,26 @@ void DistributedKvDataManagerEncryptTest::TearDown(void) HWTEST_F(DistributedKvDataManagerEncryptTest, kvstore_ddm_createEncryptedStore_001, TestSize.Level1) { ZLOGI("kvstore_ddm_createEncryptedStore_001 begin."); - std::unique_ptr kvStorePtr; - manager.GetKvStore(createEnc, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr; + Status status = manager.GetKvStore(createEnc, appId, storeId, kvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr, nullptr); Key key = "age"; Value value = "18"; - Status status = kvStorePtr->Put(key, value); + status = kvStorePtr->Put(key, value); EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data return wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr snapshot; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); - - ASSERT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; + status = kvStorePtr->GetKvStoreSnapshot(nullptr,snapshot); + EXPECT_EQ(Status::SUCCESS, status) << "KvStore GetKvStoreSnapshot data return wrong status"; + ASSERT_NE(nullptr, snapshot) << "snapshot is nullptr"; // get value from kvstore. Value valueRet; - Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); + Status statusRet = snapshot->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusRet) << "KvStoreSnapshot get data return wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(snapshot); } \ No newline at end of file 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 342ac787899a56d210a871769d46f3f572bb58b9..aedfebcecc4f349d8e59fc9e418598524ddeb0b3 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/distributed_kv_data_manager_test.cpp @@ -132,18 +132,14 @@ void DistributedKvDataManagerTest::TearDown(void) HWTEST_F(DistributedKvDataManagerTest, GetKvStore001, TestSize.Level1) { ZLOGI("GetKvStore001 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(notExistKvStorePtr, nullptr); - std::unique_ptr existKvStorePtr; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr, nullptr); } @@ -157,11 +153,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore002, TestSize.Level1) { ZLOGI("GetKvStore002 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(notExistKvStorePtr, nullptr); manager.CloseKvStore(appId, storeId64); manager.DeleteKvStore(appId, storeId64); @@ -178,11 +172,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore002, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore003, TestSize.Level1) { ZLOGI("GetKvStore003 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - EXPECT_EQ(status, Status::STORE_NOT_FOUND); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(noCreate, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::STORE_NOT_FOUND); EXPECT_EQ(notExistKvStorePtr, nullptr); } @@ -196,11 +188,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore003, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore004, TestSize.Level1) { ZLOGI("GetKvStore004 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeIdEmpty, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::INVALID_ARGUMENT); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeIdEmpty, notExistKvStorePtr); + ASSERT_EQ(status, Status::INVALID_ARGUMENT); EXPECT_EQ(notExistKvStorePtr, nullptr); } @@ -214,11 +204,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore004, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore005, TestSize.Level1) { ZLOGI("GetKvStore005 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(noCreate, appId, storeIdEmpty, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::INVALID_ARGUMENT); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(noCreate, appId, storeIdEmpty, notExistKvStorePtr); + ASSERT_EQ(status, Status::INVALID_ARGUMENT); EXPECT_EQ(notExistKvStorePtr, nullptr); } @@ -232,11 +220,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore005, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore006, TestSize.Level1) { ZLOGI("GetKvStore006 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId65, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::INVALID_ARGUMENT); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId65, notExistKvStorePtr); + ASSERT_EQ(status, Status::INVALID_ARGUMENT); EXPECT_EQ(notExistKvStorePtr, nullptr); } @@ -250,11 +236,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore006, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetKvStore007, TestSize.Level1) { ZLOGI("GetKvStore007 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(noCreate, appId, storeId65, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::INVALID_ARGUMENT); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(noCreate, appId, storeId65, notExistKvStorePtr); + ASSERT_EQ(status, Status::INVALID_ARGUMENT); EXPECT_EQ(notExistKvStorePtr, nullptr); } @@ -268,11 +252,10 @@ HWTEST_F(DistributedKvDataManagerTest, GetKvStore007, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore001, TestSize.Level1) { ZLOGI("GetAllKvStore001 begin."); - std::vector idList; - manager.GetAllKvStoreId(appId, [&](Status status, std::vector &idList) { - EXPECT_EQ(status, Status::SUCCESS); - EXPECT_EQ(idList.size(), (unsigned long)0); - }); + std::vector storeIds; + Status status = manager.GetAllKvStoreId(appId, storeIds); + EXPECT_EQ(status, Status::SUCCESS); + EXPECT_EQ(storeIds.size(), (unsigned long)0); } /** @@ -291,42 +274,38 @@ HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore002, TestSize.Level1) id2.storeId = "id2"; StoreId id3; id3.storeId = "id3"; - manager.GetKvStore(create, appId, id1, [&](Status status, std::unique_ptr kvStore) { - ASSERT_NE(kvStore, nullptr); - ASSERT_EQ(status, Status::SUCCESS); - }); - manager.GetKvStore(create, appId, id2, [&](Status status, std::unique_ptr kvStore) { - ASSERT_NE(kvStore, nullptr); - ASSERT_EQ(status, Status::SUCCESS); - }); - manager.GetKvStore(create, appId, id3, [&](Status status, std::unique_ptr kvStore) { - ASSERT_NE(kvStore, nullptr); - ASSERT_EQ(status, Status::SUCCESS); - }); - - std::vector idList; - manager.GetAllKvStoreId(appId, [&](Status status, std::vector &idList) { - EXPECT_EQ(status, Status::SUCCESS); - bool haveId1 = false; - bool haveId2 = false; - bool haveId3 = false; - for (StoreId id : idList) { - if (id.storeId == "id1") { - haveId1 = true; - } else if (id.storeId == "id2") { - haveId2 = true; - } else if (id.storeId == "id3") { - haveId3 = true; - } else { - ZLOGI("got an unknown storeId."); - EXPECT_TRUE(false); - } + std::shared_ptr kvStore; + Status status = manager.GetKvStore(create, appId, id1, kvStore); + ASSERT_NE(kvStore, nullptr); + ASSERT_EQ(status, Status::SUCCESS); + status = manager.GetKvStore(create, appId, id2, kvStore); + ASSERT_NE(kvStore, nullptr); + ASSERT_EQ(status, Status::SUCCESS); + status = manager.GetKvStore(create, appId, id3, kvStore); + ASSERT_NE(kvStore, nullptr); + ASSERT_EQ(status, Status::SUCCESS); + std::vector storeIds; + status = manager.GetAllKvStoreId(appId, storeIds); + EXPECT_EQ(status, Status::SUCCESS); + bool haveId1 = false; + bool haveId2 = false; + bool haveId3 = false; + for (StoreId &id : storeIds) { + if (id.storeId == "id1") { + haveId1 = true; + } else if (id.storeId == "id2") { + haveId2 = true; + } else if (id.storeId == "id3") { + haveId3 = true; + } else { + ZLOGI("got an unknown storeId."); + EXPECT_TRUE(false); } - EXPECT_TRUE(haveId1); - EXPECT_TRUE(haveId2); - EXPECT_TRUE(haveId3); - EXPECT_EQ(idList.size(), (unsigned long)3); - }); + } + EXPECT_TRUE(haveId1); + EXPECT_TRUE(haveId2); + EXPECT_TRUE(haveId3); + EXPECT_EQ(storeIds.size(), (unsigned long)3); } /** @@ -339,11 +318,9 @@ HWTEST_F(DistributedKvDataManagerTest, GetAllKvStore002, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseKvStore001, TestSize.Level1) { ZLOGI("CloseKvStore001 begin."); - std::unique_ptr kvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -360,11 +337,9 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStore001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseKvStore002, TestSize.Level1) { ZLOGI("CloseKvStore002 begin."); - std::unique_ptr kvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr, nullptr); manager.CloseKvStore(appId, storeId64); @@ -424,18 +399,14 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStore005, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti001, TestSize.Level1) { ZLOGI("CloseKvStoreMulti001 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(notExistKvStorePtr, nullptr); - std::unique_ptr existKvStorePtr; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr; + manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -455,25 +426,19 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti002, TestSize.Level1) { ZLOGI("CloseKvStoreMulti002 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(notExistKvStorePtr, nullptr); - std::unique_ptr existKvStorePtr1; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr1; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr1, nullptr); - std::unique_ptr existKvStorePtr2; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr2; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr2, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -499,25 +464,19 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti002, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti003, TestSize.Level1) { ZLOGI("CloseKvStoreMulti003 begin."); - std::unique_ptr notExistKvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - notExistKvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr notExistKvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, notExistKvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(notExistKvStorePtr, nullptr); - std::unique_ptr existKvStorePtr1; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr1; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr1, nullptr); - std::unique_ptr existKvStorePtr2; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr2; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr2, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -528,21 +487,16 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti003, TestSize.Level1) Key keyInt = "math_score_int"; Value valueInt = Value(TransferTypeToByteArray(-100)); - Status status = existKvStorePtr2->Put(keyInt, valueInt); + status = existKvStorePtr2->Put(keyInt, valueInt); EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data return wrong status"; - std::unique_ptr kvStoreSnapshotPtr; - existKvStorePtr2->GetKvStoreSnapshot(nullptr, - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + std::shared_ptr kvStoreSnapshotPtr; + existKvStorePtr2->GetKvStoreSnapshot(nullptr,kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - std::unique_ptr existKvStorePtr3; - manager.GetKvStore(noCreate, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - existKvStorePtr3 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr existKvStorePtr3; + status = manager.GetKvStore(noCreate, appId, storeId64, existKvStorePtr3); + ASSERT_EQ(status, Status::SUCCESS); EXPECT_NE(existKvStorePtr3, nullptr); stat = manager.CloseKvStore(appId, storeId64); @@ -567,18 +521,14 @@ HWTEST_F(DistributedKvDataManagerTest, CloseKvStoreMulti003, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore001, TestSize.Level1) { ZLOGI("CloseAllKvStore001 begin."); - std::unique_ptr kvStorePtr1; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr1; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr1, nullptr); - std::unique_ptr kvStorePtr2; - manager.GetKvStore(create, appId, storeIdTest, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr2; + status = manager.GetKvStore(create, appId, storeIdTest, kvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr2, nullptr); Status stat = manager.CloseAllKvStore(appId); @@ -595,18 +545,14 @@ HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore002, TestSize.Level1) { ZLOGI("CloseAllKvStore002 begin."); - std::unique_ptr kvStorePtr1; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr1; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr1, nullptr); - std::unique_ptr kvStorePtr2; - manager.GetKvStore(create, appId, storeIdTest, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr2; + status = manager.GetKvStore(create, appId, storeIdTest, kvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr2, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -626,11 +572,9 @@ HWTEST_F(DistributedKvDataManagerTest, CloseAllKvStore002, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore001, TestSize.Level1) { ZLOGI("DeleteKvStore001 begin."); - std::unique_ptr kvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); @@ -650,11 +594,9 @@ HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore002, TestSize.Level1) { ZLOGI("DeleteKvStore002 begin."); - std::unique_ptr kvStorePtr; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr, nullptr); // first close it if opened, and then delete it. @@ -715,17 +657,13 @@ HWTEST_F(DistributedKvDataManagerTest, DeleteKvStore005, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore001, TestSize.Level1) { ZLOGI("DeleteAllKvStore001 begin."); - std::unique_ptr kvStorePtr1; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr1; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr1, nullptr); - std::unique_ptr kvStorePtr2; - manager.GetKvStore(create, appId, storeIdTest, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr2; + status = manager.GetKvStore(create, appId, storeIdTest, kvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr2, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); EXPECT_EQ(stat, Status::SUCCESS); @@ -746,17 +684,13 @@ HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore001, TestSize.Level1) HWTEST_F(DistributedKvDataManagerTest, DeleteAllKvStore002, TestSize.Level1) { ZLOGI("DeleteAllKvStore002 begin."); - std::unique_ptr kvStorePtr1; - manager.GetKvStore(create, appId, storeId64, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr1 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr1; + Status status = manager.GetKvStore(create, appId, storeId64, kvStorePtr1); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr1, nullptr); - std::unique_ptr kvStorePtr2; - manager.GetKvStore(create, appId, storeIdTest, [&](Status status, std::unique_ptr kvStore) { - kvStorePtr2 = std::move(kvStore); - ASSERT_EQ(status, Status::SUCCESS); - }); + std::shared_ptr kvStorePtr2; + status = manager.GetKvStore(create, appId, storeIdTest, kvStorePtr2); + ASSERT_EQ(status, Status::SUCCESS); ASSERT_NE(kvStorePtr2, nullptr); Status stat = manager.CloseKvStore(appId, storeId64); EXPECT_EQ(stat, Status::SUCCESS); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_client_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_client_test.cpp index 99097e61609fafcde57b41b7cb701cae2c082206..80b63adab32a916e8b6e28135d535f9abb031078 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_client_test.cpp @@ -34,15 +34,15 @@ public: void SetUp(); void TearDown(); - static std::unique_ptr kvStorePtr; // declare kvstore instance. - static std::unique_ptr kvStoreSnapshotPtr; // declare kvstore instance. + static std::shared_ptr kvStorePtr; // declare kvstore instance. + static std::shared_ptr kvStoreSnapshotPtr; // declare kvstore instance. static Status statusGetKvStore; static Status statusGetSnapshot; static int MAX_VALUE_SIZE; }; -std::unique_ptr KvStoreClientTest::kvStorePtr = nullptr; -std::unique_ptr KvStoreClientTest::kvStoreSnapshotPtr = nullptr; +std::shared_ptr KvStoreClientTest::kvStorePtr = nullptr; +std::shared_ptr KvStoreClientTest::kvStoreSnapshotPtr = nullptr; Status KvStoreClientTest::statusGetKvStore = Status::ERROR; Status KvStoreClientTest::statusGetSnapshot = Status::ERROR; int KvStoreClientTest::MAX_VALUE_SIZE = 4 * 1024 * 1024; @@ -65,17 +65,10 @@ void KvStoreClientTest::SetUpTestCase(void) manager.DeleteAllKvStore(appId); // [create and] open and initialize kvstore instance. - manager.GetKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - kvStorePtr = std::move(kvStore); - }); + statusGetKvStore = manager.GetKvStore(options, appId, storeId, kvStorePtr); // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - statusGetSnapshot = status; - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + statusGetSnapshot = kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); } void KvStoreClientTest::TearDownTestCase(void) @@ -103,13 +96,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmGetKvStoreSnapshot001, TestSize.Level2) Key key = "name"; Value value = "test"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get values from KvStore before putting them into KvStore. @@ -117,21 +107,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmGetKvStoreSnapshot001, TestSize.Level2) Status statusRet1 = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet1) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ("", valueRet.ToString()) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); Status status = kvStorePtr->Put(key, value); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get values from KvStore after putting them into KvStore. Status statusRet2 = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusRet2) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -154,13 +141,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut001, TestSize.Level2) Value valueInt = Value(TransferTypeToByteArray(scoreInt)); Status status = kvStorePtr->Put(keyInt, valueInt); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get int value from kvstore. @@ -168,7 +152,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut001, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(keyInt, valueRetInt); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(valueInt, valueRetInt) << "valueInt and valueRetInt are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -190,13 +174,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut002, TestSize.Level2) Value valueFloat = Value(TransferTypeToByteArray(scoreFloat)); Status status = kvStorePtr->Put(keyFloat, valueFloat); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get float value from kvstore. @@ -204,7 +185,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut002, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(keyFloat, valueRetFloat); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(valueFloat, valueRetFloat) << "valueFloat and valueRetFloat are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -227,13 +208,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut003, TestSize.Level2) Value valueDouble = Value(TransferTypeToByteArray(scoreDouble)); Status status = kvStorePtr->Put(keyDouble, valueDouble); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get double value from kvstore. @@ -241,7 +219,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut003, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(keyDouble, valueRetDouble); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(valueDouble, valueRetDouble) << "valueDouble and valueRetDouble are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -264,13 +242,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut004, TestSize.Level2) Value valueUInt = Value(TransferTypeToByteArray(scoreUInt)); Status status = kvStorePtr->Put(keyUInt, valueUInt); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get unsigned int value from kvstore. @@ -278,7 +253,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut004, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(keyUInt, valueRetUInt); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(valueUInt, valueRetUInt) << "valueUInt and valueRetUInt are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -301,13 +276,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut005, TestSize.Level2) Value valueInt64 = Value(TransferTypeToByteArray(scoreInt64)); Status status = kvStorePtr->Put(keyInt64, valueInt64); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get long int value from kvstore. @@ -315,7 +287,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut005, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(keyInt64, valueRetint64); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(valueInt64, valueRetint64) << "valueInt64 and valueRetint64 are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -336,13 +308,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut006, TestSize.Level2) Value value = "{\"class\":20, \"age\":18, \"gradle\":\"good\"}"; Status status = kvStorePtr->Put(key, value); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get json value from kvstore. @@ -350,7 +319,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut006, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -372,13 +341,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut007, TestSize.Level2) Value value = Value(str); Status status = kvStorePtr->Put(key, value); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get string value from kvstore. @@ -386,7 +352,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut007, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -408,13 +374,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut008, TestSize.Level2) Value value = Value(str); Status status = kvStorePtr->Put(key, value); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get string value from kvstore. @@ -422,7 +385,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut008, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -448,13 +411,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut009, TestSize.Level2) Value value2 = Value(str2); Status status2 = kvStorePtr->Put(key, value2); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status2) << "KvStore put data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get string value from kvstore. @@ -462,7 +422,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut009, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(value2, valueRet) << "value2 and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -575,10 +535,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut014, TestSize.Level2) EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data failed, wrong status"; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get values greater than MAX_VALUE_SIZE from KvStore. @@ -586,7 +543,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPut014, TestSize.Level2) Status statusTmp = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStoreSnapshot get large data failed, wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -610,13 +567,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDelete001, TestSize.Level2) Status status2 = kvStorePtr->Delete(key); // delete data EXPECT_EQ(Status::SUCCESS, status2) << "KvStore delete data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. @@ -624,7 +578,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDelete001, TestSize.Level2) Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ("", valueRet.ToString()) << "valueRet EQ fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -645,13 +599,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDelete002, TestSize.Level2) EXPECT_EQ(Status::SUCCESS, status1) << "KvStore delete data failed, wrong status"; Status status2 = kvStorePtr->Delete(key); // delete data which not exist EXPECT_EQ(Status::SUCCESS, status2) << "KvStore delete data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. @@ -659,7 +610,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDelete002, TestSize.Level2) Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ("", valueRet.ToString()) << "valueRet EQ fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -705,13 +656,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmClear001, TestSize.Level2) Status status3 = kvStorePtr->Clear(); // clear data EXPECT_EQ(Status::SUCCESS, status3) << "KvStore clear data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Value valueRet1; @@ -723,7 +671,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmClear001, TestSize.Level2) Status statusRet2 = kvStoreSnapshotPtr->Get(key2, valueRet2); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet2) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ("", valueRet2.ToString()) << "valueRet EQ fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -776,13 +724,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch001, TestSize.Level2) Status status = kvStorePtr->PutBatch(entries); EXPECT_EQ(Status::SUCCESS, status) << "KvStore putbatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. @@ -800,7 +745,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch001, TestSize.Level2) Status statusRet3 = kvStoreSnapshotPtr->Get(entry3.key, valueRet3); EXPECT_EQ(Status::SUCCESS, statusRet3) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(entry3.value, valueRet3) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -846,13 +791,10 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch002, TestSize.Level2) Status status = kvStorePtr->PutBatch(entriesAfter); EXPECT_EQ(Status::SUCCESS, status) << "KvStore putbatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. @@ -870,7 +812,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch002, TestSize.Level2) Status statusRet3 = kvStoreSnapshotPtr->Get(entry6.key, valueRet3); EXPECT_EQ(Status::SUCCESS, statusRet3) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(entry6.value, valueRet3) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -937,7 +879,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch004, TestSize.Level2) EXPECT_EQ(Status::INVALID_ARGUMENT, status) << "KvStore putbatch data failed, wrong status"; } -std::string Generate1025KeyLen() +static std::string Generate1025KeyLen() { // Generate key and the length is more than 1024; std::string str("prefix"); @@ -1018,10 +960,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch006, TestSize.Level2) EXPECT_EQ(Status::SUCCESS, status) << "KvStore putbatch data failed, wrong status"; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. @@ -1039,7 +978,7 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmPutBatch006, TestSize.Level2) Status statusRet3 = kvStoreSnapshotPtr->Get(entry3.key, valueRet3); EXPECT_EQ(Status::SUCCESS, statusRet3) << "KvStoreSnapshot get data failed, wrong status"; EXPECT_EQ(entry3.value, valueRet3) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -1081,24 +1020,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDeleteBatch001, TestSize.Level2) Status status2 = kvStorePtr->DeleteBatch(keys); EXPECT_EQ(Status::SUCCESS, status2) << "KvStore deletebatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Key keyPrefixStudent = "student_name_"; std::vector students; Key token; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - students = std::move(entries); - }); + kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(0, static_cast(students.size())) << "KvStore is not empty, deletebatch fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -1141,24 +1074,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDeleteBatch002, TestSize.Level2) Status status2 = kvStorePtr->DeleteBatch(keys); EXPECT_EQ(Status::SUCCESS, status2) << "KvStore deletebatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Key keyPrefixStudent = "student_name_"; std::vector students; Key token; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - students = std::move(entries); - }); + kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(0, static_cast(students.size())) << "KvStore is not empty, deletebatch fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -1199,24 +1126,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDeleteBatch003, TestSize.Level2) Status status2 = kvStorePtr->DeleteBatch(keys); EXPECT_EQ(Status::INVALID_ARGUMENT, status2) << "KvStore deletebatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Key keyPrefixStudent = "student_name_"; std::vector students; Key token; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - students = std::move(entries); - }); + kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "invalid argument, deletebatch fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -1257,24 +1178,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDeleteBatch004, TestSize.Level2) Status status2 = kvStorePtr->DeleteBatch(keys); EXPECT_EQ(Status::INVALID_ARGUMENT, status2) << "KvStore deletebatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Key keyPrefixStudent = "student_name_"; std::vector students; Key token; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - students = std::move(entries); - }); + kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "invalid argument, deletebatch fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -1316,24 +1231,18 @@ HWTEST_F(KvStoreClientTest, KvStoreDdmDeleteBatch005, TestSize.Level2) Status status2 = kvStorePtr->DeleteBatch(keys); EXPECT_EQ(Status::INVALID_ARGUMENT, status2) << "KvStore deletebatch data failed, wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; Key keyPrefixStudent = "student_name_"; std::vector students; Key token; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - students = std::move(entries); - }); + kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "invalid argument, deletebatch fail"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_snapshot_client_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_snapshot_client_test.cpp index 35d8ac2b773b8ac1d3a8a18dc9c55a8eff410083..1431eb139307f11d1d4dba035de1ed0ba1037a36 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_snapshot_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/kvstore_snapshot_client_test.cpp @@ -34,13 +34,13 @@ public: void SetUp(); void TearDown(); - static std::unique_ptr kvStorePtr; // declare kv store instance. + static std::shared_ptr kvStorePtr; // declare kv store instance. static Status statusGetKvStore; static DistributedKvDataManager manager; }; DistributedKvDataManager KvStoreSnapshotClientTest::manager; -std::unique_ptr KvStoreSnapshotClientTest::kvStorePtr = nullptr; +std::shared_ptr KvStoreSnapshotClientTest::kvStorePtr = nullptr; Status KvStoreSnapshotClientTest::statusGetKvStore = Status::ERROR; void KvStoreSnapshotClientTest::SetUpTestCase(void) @@ -60,10 +60,7 @@ void KvStoreSnapshotClientTest::SetUpTestCase(void) manager.CloseAllKvStore(appId); manager.DeleteAllKvStore(appId); // [create and] open and initialize kvstore instance. - manager.GetKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - kvStorePtr = std::move(kvStore); - }); + Status status = manager.GetKvStore(options, appId, storeId, kvStorePtr); } void KvStoreSnapshotClientTest::TearDownTestCase(void) @@ -102,21 +99,18 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGet001, TestSize.Level1) Status status = kvStorePtr->Put(key, value); // insert or update key-value EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data return wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr snapshot; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); - - EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; + status = kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */snapshot); + EXPECT_EQ(Status::SUCCESS, status) << "KvStore GetKvStoreSnapshot return wrong status"; + EXPECT_NE(nullptr, snapshot) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. Value valueRet; - Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); + Status statusRet = snapshot->Get(key, valueRet); EXPECT_EQ(Status::SUCCESS, statusRet) << "KvStoreSnapshot get data return wrong status"; EXPECT_EQ(value, valueRet) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(snapshot); } /** @@ -140,21 +134,19 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGet002, TestSize.Level1) Status status2 = kvStorePtr->Delete(key); // delete data EXPECT_EQ(Status::SUCCESS, status2) << "KvStore delete data return wrong status"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr snapshot; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + status1 = kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */snapshot); + EXPECT_EQ(Status::SUCCESS, status1) << "KvStore GetKvStoreSnapshot return wrong status"; + EXPECT_NE(nullptr, snapshot) << "kvStoreSnapshotPtr is nullptr"; - EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. Value valueRet; - Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); + Status statusRet = snapshot->Get(key, valueRet); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet); EXPECT_EQ(Status::KEY_NOT_FOUND, statusRet) << "KvStoreSnapshot get data return wrong status"; EXPECT_EQ("", valueRet.ToString()) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(snapshot); } /** @@ -169,23 +161,20 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGet003, TestSize.Level1) ZLOGI("KvStoreSnapshotDdmGet003 begin."); EXPECT_EQ(Status::SUCCESS, statusGetKvStore) << "statusGetKvStore return wrong status"; EXPECT_NE(nullptr, kvStorePtr) << "kvStorePtr is nullptr"; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr snapshot; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); - - EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; + Status status = kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */snapshot); + EXPECT_EQ(Status::SUCCESS, status) << "GetKvStoreSnapshot return wrong status"; + EXPECT_NE(nullptr, snapshot) << "kvStoreSnapshotPtr is nullptr"; // get value from kvstore. Key key; Value valueRet; - Status statusRet = kvStoreSnapshotPtr->Get(key, valueRet); + Status statusRet = snapshot->Get(key, valueRet); EXPECT_EQ(Status::INVALID_ARGUMENT, statusRet); EXPECT_EQ(Status::INVALID_ARGUMENT, statusRet) << "KvStoreSnapshot get data return wrong status"; EXPECT_EQ("", valueRet.ToString()) << "value and valueRet are not equal"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(snapshot); } /** @@ -223,22 +212,15 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGetEntries001, TestSize.Le std::vector students; Key token; Status statusTmp = Status::ERROR; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - statusTmp = std::move(status); - students = std::move(entries); - }); + statusTmp = kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "GetEntries failed"; EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStore GetEntries data return wrong status"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -276,22 +258,15 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGetEntries002, TestSize.Le std::vector students; Key token; Status statusTmp = Status::ERROR; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - statusTmp = std::move(status); - students = std::move(entries); - }); + statusTmp = kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(0, static_cast(students.size())) << "GetEntries fail"; EXPECT_EQ(Status::KEY_NOT_FOUND, statusTmp) << "KvStore GetEntries data return wrong status"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -329,22 +304,15 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGetEntries003, TestSize.Le std::vector students; Key token; Status statusTmp = Status::ERROR; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - statusTmp = std::move(status); - students = std::move(entries); - }); + statusTmp = kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "GetEntries fail"; EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStore GetEntries data return wrong status"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** @@ -382,25 +350,18 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGetEntries004, TestSize.Le std::vector students; Key token; Status statusTmp = Status::ERROR; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - statusTmp = std::move(status); - students = std::move(entries); - }); + statusTmp = kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(3, static_cast(students.size())) << "GetEntries fail"; EXPECT_EQ(Status::SUCCESS, statusTmp) << "KvStore GetEntries data return wrong status"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } -std::string Generate1025KeyLen() +static std::string Generate1025KeyLen() { // Generate key and the length is more than 1024; std::string str("prefix"); @@ -445,22 +406,15 @@ HWTEST_F(KvStoreSnapshotClientTest, KvStoreSnapshotDdmGetEntries005, TestSize.Le std::vector students; Key token; Status statusTmp = Status::ERROR; - std::unique_ptr kvStoreSnapshotPtr; + std::shared_ptr kvStoreSnapshotPtr; // [create and] open and initialize kvstore snapshot instance. - kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ - [&](Status status, std::unique_ptr kvStoreSnapshot) { - kvStoreSnapshotPtr = std::move(kvStoreSnapshot); - }); + kvStorePtr->GetKvStoreSnapshot(nullptr, /* (KvStoreObserver ) */ kvStoreSnapshotPtr); EXPECT_NE(nullptr, kvStoreSnapshotPtr) << "kvStoreSnapshotPtr is nullptr"; - kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, - [&](Status status, std::vector &entries) { - statusTmp = std::move(status); - students = std::move(entries); - }); + statusTmp = kvStoreSnapshotPtr->GetEntries(keyPrefixStudent, students); EXPECT_EQ(0, static_cast(students.size())) << "invalid argument, GetEntries fail"; EXPECT_EQ(Status::INVALID_ARGUMENT, statusTmp) << "KvStore GetEntries data return wrong status"; - kvStorePtr->ReleaseKvStoreSnapshot(std::move(kvStoreSnapshotPtr)); + kvStorePtr->ReleaseKvStoreSnapshot(kvStoreSnapshotPtr); } /** 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 96e1189374c52270669ac70a240296eea026c6ee..e6dc58d283a7980d083c79496344b3356e668672 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp @@ -37,13 +37,13 @@ public: void TearDown(); static DistributedKvDataManager manager; - static std::unique_ptr kvStorePtr; // declare kvstore instance. + static std::shared_ptr kvStorePtr; // declare kvstore instance. static Status statusGetKvStore; static AppId appId; static StoreId storeId; static int usleepTime; }; -std::unique_ptr LocalSubscribeStoreTest::kvStorePtr = nullptr; +std::shared_ptr LocalSubscribeStoreTest::kvStorePtr = nullptr; Status LocalSubscribeStoreTest::statusGetKvStore = Status::ERROR; DistributedKvDataManager LocalSubscribeStoreTest::manager; AppId LocalSubscribeStoreTest::appId; @@ -61,10 +61,7 @@ void LocalSubscribeStoreTest::SetUpTestCase(void) storeId.storeId = "student"; // define kvstore(database) name manager.DeleteKvStore(appId, storeId); // [create and] open and initialize kvstore instance. - manager.GetKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - kvStorePtr = std::move(kvStore); - }); + statusGetKvStore = manager.GetKvStore(options, appId, storeId, kvStorePtr); } void LocalSubscribeStoreTest::TearDownTestCase(void) @@ -92,7 +89,7 @@ public: KvStoreObserverUnitTest &operator=(KvStoreObserverUnitTest &&) = delete; // callback function will be called when the db data is changed. - void OnChange(const ChangeNotification &changeNotification, std::unique_ptr snapshot); + void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot); void OnChange(const ChangeNotification &changeNotification); @@ -115,23 +112,23 @@ KvStoreObserverUnitTest::KvStoreObserverUnitTest() } void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotification, - std::unique_ptr snapshot) + std::shared_ptr snapshot) { ZLOGD("begin."); callCount_++; - const std::list insert = changeNotification.GetInsertEntries(); + const auto &insert = changeNotification.GetInsertEntries(); insertEntries_.clear(); for (const auto &entry : insert) { insertEntries_.push_back(entry); } - const std::list update = changeNotification.GetUpdateEntries(); + const auto &update = changeNotification.GetUpdateEntries(); updateEntries_.clear(); for (const auto &entry : update) { updateEntries_.push_back(entry); } - const std::list del = changeNotification.GetDeleteEntries(); + const auto &del = changeNotification.GetDeleteEntries(); deleteEntries_.clear(); for (const auto &entry : del) { deleteEntries_.push_back(entry); @@ -2321,12 +2318,12 @@ HWTEST_F(LocalSubscribeStoreTest, ChangeNotificationMarshalling001, TestSize.Lev insert.value = "insert_value"; update.value = "update_value"; del.value = "delete_value"; - std::list insertEntries, updateEntries, deleteEntries; - insertEntries.push_back(insert); - updateEntries.push_back(update); - deleteEntries.push_back(del); + std::vector inserts, updates, deleteds; + inserts.push_back(insert); + updates.push_back(update); + deleteds.push_back(del); - ChangeNotification changeIn(insertEntries, updateEntries, deleteEntries, std::string(), false); + ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), std::string(), false); OHOS::Parcel parcel; changeIn.Marshalling(parcel); ChangeNotification *changeOut = ChangeNotification::Unmarshalling(parcel); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_query_test.cpp b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_query_test.cpp index f6ec23cd0da9ae4cffe6b3330bac9fc38824ccb9..d8dc3aeb37ad40038c5d7596ceffc48c7e754b1e 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_query_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_query_test.cpp @@ -37,7 +37,7 @@ public: void TearDown(); - static std::unique_ptr singleKvStorePtr; + static std::shared_ptr singleKvStorePtr; static Status statusGetKvStore; }; @@ -48,7 +48,7 @@ const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\"," "\"name\":\"INTEGER, NOT NULL\"" "}," "\"SCHEMA_INDEXES\":[\"$.name\"]}"; -std::unique_ptr SingleKvStoreClientQueryTest::singleKvStorePtr = nullptr; +std::shared_ptr SingleKvStoreClientQueryTest::singleKvStorePtr = nullptr; Status SingleKvStoreClientQueryTest::statusGetKvStore = Status::ERROR; void SingleKvStoreClientQueryTest::SetUpTestCase(void) @@ -438,10 +438,7 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestSingleKvStoreQueryC001, TestSize.Leve options.schema = VALID_SCHEMA_STRICT_DEFINE; AppId appId = { "SingleKvStoreClientQueryTestAppId1" }; StoreId storeId = { "SingleKvStoreClientQueryTestStoreId1" }; - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - singleKvStorePtr = std::move(kvStore); - }); + statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStorePtr); EXPECT_NE(singleKvStorePtr, nullptr) << "kvStorePtr is null."; singleKvStorePtr->Put("test_key_1", "{\"name\":1}"); singleKvStorePtr->Put("test_key_2", "{\"name\":2}"); @@ -458,21 +455,17 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestSingleKvStoreQueryC001, TestSize.Leve ASSERT_EQ(status2, Status::SUCCESS); EXPECT_TRUE(results2.size() == 2); - std::unique_ptr callback1; - singleKvStorePtr->GetResultSetWithQuery(query.ToString(), [&](Status status3, std::unique_ptr call) { - ASSERT_EQ(status3, Status::SUCCESS); - callback1 = std::move(call); - EXPECT_TRUE(callback1->GetCount() == 2); - }); - auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(std::move(callback1)); + std::shared_ptr resultSet; + Status status3 = singleKvStorePtr->GetResultSetWithQuery(query.ToString(), resultSet); + ASSERT_EQ(status3, Status::SUCCESS); + EXPECT_TRUE(resultSet->GetCount() == 2); + auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(resultSet); ASSERT_EQ(closeResultSetStatus, Status::SUCCESS); - std::unique_ptr callback2; - singleKvStorePtr->GetResultSetWithQuery(query, [&](Status status4, std::unique_ptr call) { - ASSERT_EQ(status4, Status::SUCCESS); - callback2 = std::move(call); - EXPECT_TRUE(callback2->GetCount() == 2); - }); - closeResultSetStatus = singleKvStorePtr->CloseResultSet(std::move(callback2)); + Status status4 = singleKvStorePtr->GetResultSetWithQuery(query, resultSet); + ASSERT_EQ(status4, Status::SUCCESS); + EXPECT_TRUE(resultSet->GetCount() == 2); + + closeResultSetStatus = singleKvStorePtr->CloseResultSet(resultSet); ASSERT_EQ(closeResultSetStatus, Status::SUCCESS); int resultSize1; @@ -513,10 +506,7 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestSingleKvStoreQueryC002, TestSize.Leve options.schema = VALID_SCHEMA_STRICT_DEFINE; AppId appId = { "SingleKvStoreClientQueryTestAppId2" }; StoreId storeId = { "SingleKvStoreClientQueryTestStoreId2" }; - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - singleKvStorePtr = std::move(kvStore); - }); + statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStorePtr); EXPECT_NE(singleKvStorePtr, nullptr) << "kvStorePtr is null."; singleKvStorePtr->Put("test_key_1", "{\"name\":1}"); singleKvStorePtr->Put("test_key_2", "{\"name\":2}"); @@ -535,21 +525,17 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestSingleKvStoreQueryC002, TestSize.Leve ASSERT_EQ(status2, Status::SUCCESS); EXPECT_TRUE(results2.size() == 1); - std::unique_ptr callback1; - singleKvStorePtr->GetResultSetWithQuery(query.ToString(), [&](Status status3, std::unique_ptr call) { - ASSERT_EQ(status3, Status::SUCCESS); - callback1 = std::move(call); - EXPECT_TRUE(callback1->GetCount() == 1); - }); - auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(std::move(callback1)); + std::shared_ptr resultSet; + Status status3 = singleKvStorePtr->GetResultSetWithQuery(query.ToString(), resultSet); + ASSERT_EQ(status3, Status::SUCCESS); + EXPECT_TRUE(resultSet->GetCount() == 1); + auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(resultSet); ASSERT_EQ(closeResultSetStatus, Status::SUCCESS); - std::unique_ptr callback2; - singleKvStorePtr->GetResultSetWithQuery(query, [&](Status status4, std::unique_ptr call) { - ASSERT_EQ(status4, Status::SUCCESS); - callback2 = std::move(call); - EXPECT_TRUE(callback2->GetCount() == 1); - }); - closeResultSetStatus = singleKvStorePtr->CloseResultSet(std::move(callback2)); + Status status4 = singleKvStorePtr->GetResultSetWithQuery(query, resultSet); + ASSERT_EQ(status4, Status::SUCCESS); + EXPECT_TRUE(resultSet->GetCount() == 1); + + closeResultSetStatus = singleKvStorePtr->CloseResultSet(resultSet); ASSERT_EQ(closeResultSetStatus, Status::SUCCESS); int resultSize1; 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 8e0fcb9497bb26f3f61b1b31c06f92be695e082a..57bc52de7eb09b2f519ec22e7991de3747ec2d5d 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -34,7 +34,7 @@ public: void TearDown(); - static std::unique_ptr singleKvStorePtr; // declare kvstore instance. + static std::shared_ptr singleKvStorePtr; // declare kvstore instance. static Status statusGetKvStore; static int MAX_VALUE_SIZE; }; @@ -47,7 +47,7 @@ const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\"," "}," "\"SCHEMA_INDEXES\":[\"$.age\"]}"; -std::unique_ptr SingleKvStoreClientTest::singleKvStorePtr = nullptr; +std::shared_ptr SingleKvStoreClientTest::singleKvStorePtr = nullptr; Status SingleKvStoreClientTest::statusGetKvStore = Status::ERROR; int SingleKvStoreClientTest::MAX_VALUE_SIZE = 4 * 1024 * 1024; // max value size is 4M. @@ -61,10 +61,7 @@ void SingleKvStoreClientTest::SetUpTestCase(void) StoreId storeId = { "student_single" }; // define kvstore(database) name. // [create and] open and initialize kvstore instance. - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - statusGetKvStore = status; - singleKvStorePtr = std::move(kvStore); - }); + statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStorePtr); } void SingleKvStoreClientTest::TearDownTestCase(void) @@ -92,7 +89,7 @@ public: KvStoreObserverTestImpl &operator=(KvStoreObserverTestImpl &&) = delete; // callback function will be called when the db data is changed. - void OnChange(const ChangeNotification &changeNotification, std::unique_ptr snapshot); + void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot); void OnChange(const ChangeNotification &changeNotification); @@ -108,19 +105,19 @@ private: void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotification) { callCount_++; - const std::list insert = changeNotification.GetInsertEntries(); + const auto &insert = changeNotification.GetInsertEntries(); insertEntries_.clear(); for (const auto &entry : insert) { insertEntries_.push_back(entry); } - const std::list update = changeNotification.GetUpdateEntries(); + const auto &update = changeNotification.GetUpdateEntries(); updateEntries_.clear(); for (const auto &entry : update) { updateEntries_.push_back(entry); } - const std::list del = changeNotification.GetDeleteEntries(); + const auto &del = changeNotification.GetDeleteEntries(); deleteEntries_.clear(); for (const auto &entry : del) { deleteEntries_.push_back(entry); @@ -130,7 +127,7 @@ void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotificat } void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotification, - std::unique_ptr snapshot) + std::shared_ptr snapshot) {} @@ -236,30 +233,28 @@ HWTEST_F(SingleKvStoreClientTest, GetEntriesAndResultSet001 ,TestSize.Level1) singleKvStorePtr->GetEntries({prefix}, results); EXPECT_EQ(results.size(), sum) << "entries size is not equal 10."; - std::unique_ptr callback; - singleKvStorePtr->GetResultSet({prefix}, [&](Status status, std::unique_ptr call) { - EXPECT_EQ(status, Status::SUCCESS); - callback = std::move(call); - }); - EXPECT_EQ(callback->GetCount(), sum_1) << "resultSet size is not equal 10."; - callback->IsFirst(); - callback->IsAfterLast(); - callback->IsBeforeFirst(); - callback->MoveToPosition(1); - callback->IsLast(); - callback->MoveToPrevious(); - callback->MoveToNext(); - callback->MoveToLast(); - callback->MoveToFirst(); - callback->GetPosition(); + std::shared_ptr resultSet; + Status status = singleKvStorePtr->GetResultSet({prefix}, resultSet); + EXPECT_EQ(status, Status::SUCCESS); + EXPECT_EQ(resultSet->GetCount(), sum_1) << "resultSet size is not equal 10."; + resultSet->IsFirst(); + resultSet->IsAfterLast(); + resultSet->IsBeforeFirst(); + resultSet->MoveToPosition(1); + resultSet->IsLast(); + resultSet->MoveToPrevious(); + resultSet->MoveToNext(); + resultSet->MoveToLast(); + resultSet->MoveToFirst(); + resultSet->GetPosition(); Entry entry; - callback->GetEntry(entry); + resultSet->GetEntry(entry); for (unsigned long i = 0; i < sum; i++) { singleKvStorePtr->Delete({prefix + std::to_string(i)}); } - auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(std::move(callback)); + auto closeResultSetStatus = singleKvStorePtr->CloseResultSet(resultSet); EXPECT_EQ(closeResultSetStatus, Status::SUCCESS) << "close resultSet failed."; } @@ -361,16 +356,14 @@ HWTEST_F(SingleKvStoreClientTest, SyncData001 ,TestSize.Level1) */ HWTEST_F(SingleKvStoreClientTest, TestSchemaStoreC001 ,TestSize.Level1) { - std::unique_ptr schemaSingleKvStorePtr; + std::shared_ptr schemaSingleKvStorePtr; DistributedKvDataManager manager; Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true, .kvStoreType = KvStoreType::SINGLE_VERSION }; options.schema = VALID_SCHEMA_STRICT_DEFINE; AppId appId = { "schema_app_id" }; StoreId storeId = { "schema_store_id" }; - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - schemaSingleKvStorePtr = std::move(kvStore); - }); + Status status = manager.GetSingleKvStore(options, appId, storeId, schemaSingleKvStorePtr); ASSERT_NE(schemaSingleKvStorePtr, nullptr) << "kvStorePtr is null."; auto result = schemaSingleKvStorePtr->GetStoreId(); EXPECT_EQ(result.storeId, "schema_store_id"); @@ -1020,15 +1013,13 @@ HWTEST_F(SingleKvStoreClientTest, SingleKvStoreTransaction002, TestSize.Level2) */ HWTEST_F(SingleKvStoreClientTest, SingleKvStoreDeviceSync001 ,TestSize.Level1) { - std::unique_ptr schemaSingleKvStorePtr; + std::shared_ptr schemaSingleKvStorePtr; DistributedKvDataManager manager; Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true, .kvStoreType = KvStoreType::SINGLE_VERSION}; AppId appId = { "schema_app_id001" }; StoreId storeId = { "schema_store_id001" }; - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - schemaSingleKvStorePtr = std::move(kvStore); - }); + manager.GetSingleKvStore(options, appId, storeId, schemaSingleKvStorePtr); ASSERT_NE(schemaSingleKvStorePtr, nullptr) << "kvStorePtr is null."; auto result = schemaSingleKvStorePtr->GetStoreId(); EXPECT_EQ(result.storeId, "schema_store_id001"); @@ -1046,15 +1037,13 @@ HWTEST_F(SingleKvStoreClientTest, SingleKvStoreDeviceSync001 ,TestSize.Level1) */ HWTEST_F(SingleKvStoreClientTest, SingleKvStoreDeviceSync002 ,TestSize.Level1) { - std::unique_ptr schemaSingleKvStorePtr; + std::shared_ptr schemaSingleKvStorePtr; DistributedKvDataManager manager; Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true, .kvStoreType = KvStoreType::SINGLE_VERSION}; AppId appId = { "schema_app_id002" }; StoreId storeId = { "schema_store_id002" }; - manager.GetSingleKvStore(options, appId, storeId, [&](Status status, std::unique_ptr kvStore) { - schemaSingleKvStorePtr = std::move(kvStore); - }); + manager.GetSingleKvStore(options, appId, storeId, schemaSingleKvStorePtr); ASSERT_NE(schemaSingleKvStorePtr, nullptr) << "kvStorePtr is null."; auto result = schemaSingleKvStorePtr->GetStoreId(); EXPECT_EQ(result.storeId, "schema_store_id002"); diff --git a/frameworks/jskitsimpl/distributeddata/include/js_util.h b/frameworks/jskitsimpl/distributeddata/include/js_util.h index 5e457ffbc2340e6a0ee386489a30fadd59786e78..c47682a4aa9af7b89741cb6b73269bcef5f422c8 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_util.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_util.h @@ -47,6 +47,7 @@ public: static std::vector Convert2StringArray(napi_env env, napi_value jsValue); private: static napi_value GetJSEntries(napi_env env, const std::list &entries); + static napi_value GetJSEntries(napi_env env, const std::vector &entries); enum ValueType : uint8_t { /** Indicates that the value type is string. */ STRING = 0, diff --git a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h index 9b3123791a58113d5ca2dad1f0680ae86b544ed2..f9a2737513cbf51b7c71d0f8713868884ed279de 100644 --- a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h @@ -27,8 +27,8 @@ class SingleKVStore final { public: SingleKVStore() = default; ~SingleKVStore(); - SingleKVStore &operator=(std::unique_ptr &&singleKvStore); - bool operator==(const std::unique_ptr &singleKvStore); + SingleKVStore &operator=(std::shared_ptr &&singleKvStore); + bool operator==(const std::shared_ptr &singleKvStore); static napi_value GetCtor(napi_env env); static napi_value OnEvent(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); @@ -74,7 +74,7 @@ private: static napi_status OnSyncComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); static std::map eventHandlers_; - std::unique_ptr kvStore_ = nullptr; + std::shared_ptr kvStore_ = nullptr; std::shared_ptr syncObserver_ = nullptr; std::shared_ptr dataObserver_[SUBSCRIBE_ALL + 1]; }; @@ -84,7 +84,7 @@ public: DataObserver(napi_env env, napi_value callback); virtual ~DataObserver(); void OnChange(const DistributedKv::ChangeNotification ¬ification, - std::unique_ptr snapshot) override; + std::shared_ptr snapshot) override; void OnChange(const DistributedKv::ChangeNotification ¬ification) override; private: struct EventDataWorker { @@ -92,8 +92,7 @@ private: const DistributedKv::ChangeNotification data; EventDataWorker(const DataObserver * const & observerIn, const DistributedKv::ChangeNotification &dataIn) : observer(observerIn), - data(dataIn.GetInsertEntries(), dataIn.GetUpdateEntries(), - dataIn.GetDeleteEntries(), dataIn.GetDeviceId(), false) {} + data(dataIn) {} }; napi_ref callback_ = nullptr; napi_env env_; diff --git a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index 902dcdd6638fc104d42330e425c9d714b1059d17..3e2f913d1be87fc3dcea8878259f56ef69dabb48 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_util.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_util.cpp @@ -278,6 +278,21 @@ napi_value JSUtil::GetJSEntries(napi_env env, const std::list &entries) +{ + napi_value jsValue = nullptr; + napi_create_array_with_length(env, entries.size(), &jsValue); + int index = 0; + for (const auto &data : entries) { + napi_value entry = nullptr; + napi_create_object(env, &entry); + napi_set_named_property(env, entry, "key", Convert2JSString(env, data.key.Data())); + napi_set_named_property(env, entry, "value", Convert2JSValue(env, data.value.Data())); + napi_set_element(env, jsValue, index++, entry); + } + return jsValue; +} + napi_value JSUtil::Convert2JSString(napi_env env, const std::string &cString) { napi_value jsValue = nullptr; diff --git a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp index 949a68adc410d17b4c4edc36f60c8447b1dbbd27..520b731d69e57c4c938f13792dd6cccc54ba6cb6 100644 --- a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp @@ -100,11 +100,11 @@ napi_value KVManager::GetKVStore(napi_env env, napi_callback_info info) return status; }; auto exec = [ctxInfo](AsyncCall::Context *ctx) { + std::shared_ptr kvStore; ctxInfo->proxy->kvDataManager_.GetSingleKvStore( ctxInfo->options, {ctxInfo->proxy->bundleName_}, {ctxInfo->storeId}, - [ctxInfo](Status, std::unique_ptr kvStore) { - *(ctxInfo->kvStore) = std::move(kvStore); - }); + kvStore); + *(ctxInfo->kvStore) = std::move(kvStore); }; auto context = std::make_shared(input, output); // getKVStore(storeId: string, options: Options, callback: AsyncCallback): void; diff --git a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp index 7bc1175c9d386176354b328c914879a88f1af4a0..64692d9588123d98e002f761370185cba1f0ceef 100644 --- a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp @@ -256,7 +256,7 @@ napi_status SingleKVStore::OnSyncComplete(napi_env env, size_t argc, napi_value return napi_ok; } -SingleKVStore &SingleKVStore::operator=(std::unique_ptr &&singleKvStore) +SingleKVStore &SingleKVStore::operator=(std::shared_ptr &&singleKvStore) { if (kvStore_ == singleKvStore) { return *this; @@ -265,7 +265,7 @@ SingleKVStore &SingleKVStore::operator=(std::unique_ptr &singleKvStore) +bool SingleKVStore::operator==(const std::shared_ptr &singleKvStore) { return kvStore_ == singleKvStore; } @@ -282,7 +282,7 @@ DataObserver::~DataObserver() napi_delete_reference(env_, callback_); } -void DataObserver::OnChange(const ChangeNotification ¬ification, std::unique_ptr snapshot) +void 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(), diff --git a/interfaces/innerkits/distributeddata/include/change_notification.h b/interfaces/innerkits/distributeddata/include/change_notification.h index 5ae6cb332e3a2741df598c637d52e85b1de390f7..3f02ba6bcb181d9facc22036b46430b56dc6667d 100644 --- a/interfaces/innerkits/distributeddata/include/change_notification.h +++ b/interfaces/innerkits/distributeddata/include/change_notification.h @@ -25,22 +25,22 @@ namespace DistributedKv { class ChangeNotification final : public virtual Parcelable { public: // Constructor of ChangeNotification. - ChangeNotification(const std::list &insertEntries, - const std::list &updateEntries, - const std::list &deleteEntries, + ChangeNotification(std::vector &&insertEntries, + std::vector &&updateEntries, + std::vector &&deleteEntries, const std::string &deviceId, - const bool isClear); + bool isClear); KVSTORE_API ~ChangeNotification(); // Get all inserted entries in this change. - KVSTORE_API const std::list &GetInsertEntries() const; + KVSTORE_API const std::vector &GetInsertEntries() const; // Get all updated entries in this changing. - KVSTORE_API const std::list &GetUpdateEntries() const; + KVSTORE_API const std::vector &GetUpdateEntries() const; // Get all deleted entries in this changing. - KVSTORE_API const std::list &GetDeleteEntries() const; + KVSTORE_API const std::vector &GetDeleteEntries() const; // Get the device ID. KVSTORE_API const std::string &GetDeviceId() const; @@ -57,11 +57,11 @@ public: // Unmarshall the given parcel from this parcelable object. KVSTORE_API static ChangeNotification *Unmarshalling(Parcel &parcel); private: - std::list insertEntries_; + std::vector insertEntries_; - std::list updateEntries_; + std::vector updateEntries_; - std::list deleteEntries_; + std::vector deleteEntries_; std::string deviceId_; diff --git a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h index 09ebd8e8a4b28c68ca49d0878f2e6b778ed682ab..8ae68a6d6739e629b7a5cfb1d5dc9b5d29856ac1 100644 --- a/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h +++ b/interfaces/innerkits/distributeddata/include/distributed_kv_data_manager.h @@ -16,6 +16,7 @@ #ifndef DISTRIBUTED_KV_DATA_MANAGER_H #define DISTRIBUTED_KV_DATA_MANAGER_H +#include #include "kvstore.h" #include "kvstore_death_recipient.h" #include "kvstore_observer.h" @@ -48,8 +49,8 @@ public: // 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. - KVSTORE_API void GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback); + KVSTORE_API 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 @@ -66,11 +67,11 @@ public: // 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. - KVSTORE_API void GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, - std::function)> callback); + KVSTORE_API Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, + std::shared_ptr &singleKvStore); // get all existed kvstore names. - KVSTORE_API void GetAllKvStoreId(const AppId &appId, std::function &)> callback); + KVSTORE_API Status GetAllKvStoreId(const AppId &appId, std::vector &storeIds); // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. // Disconnect kvstore instance from kvstoreimpl with the given storeId, @@ -83,7 +84,7 @@ public: // appId: the name of the application. // storeId: the name of the kvstore. KVSTORE_API - Status CloseKvStore(const AppId &appId, const StoreId &storeId, std::unique_ptr kvStorePtr = nullptr); + Status CloseKvStore(const AppId &appId, const StoreId &storeId, std::shared_ptr kvStorePtr = nullptr); // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. // @@ -97,7 +98,7 @@ public: // appId: the name of the application. // kvStorePtr: the pointer of the kvstore. KVSTORE_API - Status CloseKvStore(const AppId &appId, std::unique_ptr kvStorePtr); + Status CloseKvStore(const AppId &appId, std::shared_ptr kvStorePtr); // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. // close all opened kvstores for this appId. diff --git a/interfaces/innerkits/distributeddata/include/kvstore.h b/interfaces/innerkits/distributeddata/include/kvstore.h index 21eacdcd0c6324fd49f306e29a0f4eabc6c49f00..d1008a8b3d09e8bf14122a047aab399b3794eb68 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore.h +++ b/interfaces/innerkits/distributeddata/include/kvstore.h @@ -44,12 +44,11 @@ public: // Parameters: // observer: observer for subscribe. // callback: including status and KvStoreSnapshot instance returned by this call. - KVSTORE_API - virtual void GetKvStoreSnapshot(std::shared_ptr observer, - std::function)> callback) const = 0; + KVSTORE_API virtual Status GetKvStoreSnapshot(std::shared_ptr observer, + std::shared_ptr &snapshot) const = 0; // Release snapshot created by calling GetKvStoreSnapshot. - KVSTORE_API virtual Status ReleaseKvStoreSnapshot(std::unique_ptr kvStoreSnapshotPtr) = 0; + KVSTORE_API virtual Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) = 0; // Mutation operations. // Key level operations. diff --git a/interfaces/innerkits/distributeddata/include/kvstore_observer.h b/interfaces/innerkits/distributeddata/include/kvstore_observer.h index 8efc299cea2098914a51fe406f4498144ff6ff71..c50059b97d6ff573c62640785a241c132c8967cb 100644 --- a/interfaces/innerkits/distributeddata/include/kvstore_observer.h +++ b/interfaces/innerkits/distributeddata/include/kvstore_observer.h @@ -32,7 +32,7 @@ public: // client override this function to receive change notification. KVSTORE_API - virtual void OnChange(const ChangeNotification &changeNotification, std::unique_ptr snapshot) = 0; + virtual void OnChange(const ChangeNotification &changeNotification, std::shared_ptr snapshot) = 0; // client override this function to receive change notification. KVSTORE_API diff --git a/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h b/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h index 3116c4990251b84f16be26b1e2c03bd5402a1b87..fb477f95cfb979b9ed371c5cc90c9e3279283929 100755 --- a/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h +++ b/interfaces/innerkits/distributeddata/include/kvstore_snapshot.h @@ -40,8 +40,7 @@ public: // nextKey: The first key to start in this search. // callback: all entries satisfied perfixKey, status of this call and the first key of the next part of data. [[deprecated]] - KVSTORE_API virtual void GetEntries(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) = 0; + KVSTORE_API 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, @@ -52,8 +51,7 @@ public: // parameters: // prefixKey: perfix key to search // callback: all entries satisfies perfixKey, and Stauts for this call. - KVSTORE_API - virtual void GetEntries(const Key &prefixKey, std::function &)> callback) = 0; + KVSTORE_API 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, @@ -68,8 +66,7 @@ public: // nextKey: The first key to start in this search. // callback: all keys satisfies perfixKey, status of this call and the first key of the next part of data. [[deprecated]] - KVSTORE_API virtual void GetKeys(const Key &prefixKey, const Key &nextKey, - std::function &, const Key &)> callback) = 0; + KVSTORE_API 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, @@ -79,7 +76,7 @@ public: // prefixKey: perfix key to search // callback: all keys satisfies perfixKey, and Stauts for this call. KVSTORE_API - virtual void GetKeys(const Key &prefixKey, std::function &)> callback) = 0; + 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. diff --git a/interfaces/innerkits/distributeddata/include/single_kvstore.h b/interfaces/innerkits/distributeddata/include/single_kvstore.h index 1a2161fba98616cd49ffd73bea0a7a2d43b2acb7..304549ed760571ba11107f3f1decae3df926f8f3 100755 --- a/interfaces/innerkits/distributeddata/include/single_kvstore.h +++ b/interfaces/innerkits/distributeddata/include/single_kvstore.h @@ -28,7 +28,7 @@ namespace OHOS { namespace DistributedKv { // This is a public interface. Implementation of this class is in AppKvStoreImpl. // This class provides put, delete, search, sync and subscribe functions of a key-value store. -class SingleKvStore { +class SingleKvStore : virtual public KvStore { public: KVSTORE_API SingleKvStore() = default; @@ -49,7 +49,7 @@ public: // entries: entries will be returned in this parameter. // Return: // Status of this GetEntries operation. - virtual Status GetEntriesWithQuery(const std::string &query, std::vector &entries) const = 0; + KVSTORE_API virtual Status GetEntriesWithQuery(const std::string &query, std::vector &entries) const = 0; // Get all entries in this store by query. // Parameters: @@ -65,8 +65,7 @@ public: // resultSet: resultSet will be returned in this parameter. // Return: // Status of this GetResultSet operation. - KVSTORE_API virtual void GetResultSet(const Key &prefixKey, - std::function)> callback) const = 0; + KVSTORE_API virtual Status GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const = 0; // Get ResultSet in this store by Query. // Parameters: @@ -74,8 +73,7 @@ public: // resultSet: resultSet will be returned in this parameter. // Return: // Status of this GetResultSet operation. - virtual void GetResultSetWithQuery(const std::string &query, - std::function)> callback) const = 0; + KVSTORE_API virtual Status GetResultSetWithQuery(const std::string &query, std::shared_ptr &resultSet) const = 0; // Get ResultSet in this store by Query. // Parameters: @@ -83,15 +81,14 @@ public: // resultSet: resultSet will be returned in this parameter. // Return: // Status of this GetResultSet operation. - KVSTORE_API virtual void GetResultSetWithQuery(const DataQuery &query, - std::function)> callback) const = 0; + KVSTORE_API virtual Status GetResultSetWithQuery(const DataQuery &query, std::shared_ptr &resultSet) const = 0; // Close the ResultSet returned by GetResultSet. // Parameters: // resultSet: resultSet will be returned in this parameter. // Return: // Status of this CloseResultSet operation. - KVSTORE_API virtual Status CloseResultSet(std::unique_ptr resultSet) = 0; + KVSTORE_API virtual Status CloseResultSet(std::shared_ptr &resultSet) = 0; // Get the number of result by query. // Parameters: @@ -128,24 +125,6 @@ public: // Status of this remove operation. KVSTORE_API virtual Status RemoveDeviceData(const std::string &device) = 0; - // Get id of this SingleKvStore. - KVSTORE_API virtual StoreId GetStoreId() const = 0; - - // Delete an entry by its key. - // Parameters: - // key: key of the entry to be deleted. - // Return: - // Status of this delete operation. - KVSTORE_API virtual Status Delete(const Key &key) = 0; - - // Write a pair of key and value to this store. - // Parameters: - // key: key of this entry. Should be less than 1024 bytes. key will be trimmed before store. - // value: value of this entry. Should be less than (4 * 1024 * 1024) bytes. - // Return: - // Status of this put operation. - KVSTORE_API virtual Status Put(const Key &key, const Value &value) = 0; - // Get value from AppKvStore by its key. // Parameters: // key: key of this entry. @@ -154,29 +133,6 @@ public: // Status of this get operation. KVSTORE_API virtual Status Get(const Key &key, Value &value) = 0; - // subscribe change of this kvstore to a client-defined observer. observer->OnChange method will be called when store - // changes. - // Parameters: - // subscribeType: SUBSCRIBE_TYPE_LOCAL means local changes of syncable kv store, - // : SUBSCRIBE_TYPE_REMOTE means synced data changes from remote devices, - // : SUBSCRIBE_TYPE_ALL means both local changes and synced data changes. - // observer: observer to subscribe changes. - // Return: - // Status of this subscribe operation. - KVSTORE_API - virtual Status SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) = 0; - - // un-subscribe change of this kvstore to a client-defined observer. - // Parameters: - // subscribeType: SUBSCRIBE_TYPE_LOCAL means local changes of syncable kv store, - // : SUBSCRIBE_TYPE_REMOTE means synced data changes from remote devices, - // : SUBSCRIBE_TYPE_ALL means both local changes and synced data changes. - // observer: observer to subscribe changes. - // Return: - // Status of this subscribe operation. - KVSTORE_API - virtual Status UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) = 0; - // register message for sync operation. // Parameters: // callback: callback to register. @@ -193,34 +149,6 @@ public: KVSTORE_API virtual Status UnRegisterSyncCallback() = 0; - // see Put, PutBatch put a list of entries to kvstore, - // all entries will be put in a transaction, - // if entries contains invalid entry, PutBatch will all fail. - // entries's size should be less than 128 and memory size must be less than IPC transport limit. - KVSTORE_API virtual Status PutBatch(const std::vector &entries) = 0; - - // delete a list of entries in the kvstore, - // delete key not exist still return success, - // key length should not be greater than 256, and can not be empty. - // if keys contains invalid key, all delete will fail. - // keys memory size should not be greater than IPC transport limit, and can not be empty. - KVSTORE_API virtual Status DeleteBatch(const std::vector &keys) = 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. - // before this transaction is committed or rollbacked, all attemption to close this store will fail. - KVSTORE_API virtual Status StartTransaction() = 0; - - // commit current transaction. all changes to this store will be done after calling this method. - // any calling of this method outside a transaction will fail. - KVSTORE_API virtual Status Commit() = 0; - - // rollback current transaction. - // all changes to this store during this transaction will be rollback after calling this method. - // any calling of this method outside a transaction will fail. - KVSTORE_API virtual Status Rollback() = 0; - // set synchronization parameters of this store. // Parameters: // syncParam: sync policy parameter. @@ -248,7 +176,7 @@ protected: // output: output data, nullptr if no data is returned. // Return: // Status of this control operation. - KVSTORE_API virtual Status Control(KvControlCmd cmd, const KvParam &inputParam, sptr &output) = 0; + KVSTORE_API virtual Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &output) = 0; }; } // namespace AppDistributedKv } // namespace OHOS diff --git a/interfaces/innerkits/distributeddata/include/types.h b/interfaces/innerkits/distributeddata/include/types.h index b44a413aa422c696a4b74c8d9c060bddf19a3cb6..a6b71ad3a73a4eea90096e1557a33abe0d435cb7 100755 --- a/interfaces/innerkits/distributeddata/include/types.h +++ b/interfaces/innerkits/distributeddata/include/types.h @@ -40,6 +40,23 @@ struct UserId { // app identifier from Bms struct AppId { std::string appId; + inline bool IsValid() const { + if (appId.empty() || appId.size() > MAX_APP_ID_LEN) { + return false; + } + int count = 0; + auto iter = std::find_if_not(appId.begin(), appId.end(), + [&count](char c) { + count = (c == SEPARATOR_CHAR) ? (count + 1) : (count >= SEPARATOR_COUNT ? count : 0); + return (std::isprint(c) && c != '/'); + }); + + return (iter == appId.end()) && (count < SEPARATOR_COUNT); + } +private: + static constexpr int MAX_APP_ID_LEN = 256; + static constexpr int SEPARATOR_COUNT = 3; + static constexpr char SEPARATOR_CHAR = '#'; }; // kvstore name set by client by calling GetKvStore, @@ -47,6 +64,16 @@ struct AppId { // and can not be empty and all space. struct StoreId { std::string storeId; + inline bool IsValid() const { + if (storeId.empty() || storeId.size() > MAX_STORE_ID_LEN) { + return false; + } + auto iter = std::find_if_not(storeId.begin(), storeId.end(), + [](char c) { return (std::isdigit(c) || std::isalpha(c) || c == '_'); }); + return (iter == storeId.end()); + } +private: + static constexpr int MAX_STORE_ID_LEN = 128; }; struct KvStoreTuple { @@ -244,6 +271,12 @@ struct Options { bool syncable = true; // let bms delete first std::string schema = ""; bool dataOwnership = true; // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT + + inline bool IsValidType() const + { + return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION || + kvStoreType == KvStoreType::DEVICE_COLLABORATION; + } }; template diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 21c564e14fb2584467e56e4b32e9df7c9058e7a7..529866b57bde657da6a149181f8a19ef0bd64dcf 100755 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -25,6 +25,7 @@ #include "reporter.h" #include "session.h" #include "softbus_bus_center.h" +#include "securec.h" #ifdef LOG_TAG #undef LOG_TAG #endif diff --git a/services/distributeddataservice/adapter/include/broadcaster/broadcast_sender.h b/services/distributeddataservice/adapter/include/broadcaster/broadcast_sender.h index cefe50f797dfebe2a90a85f2dd965fdfa5e3d32e..4e3b39e2f9e61bbc17ac1fd41e40b2b74b0df3d1 100644 --- a/services/distributeddataservice/adapter/include/broadcaster/broadcast_sender.h +++ b/services/distributeddataservice/adapter/include/broadcaster/broadcast_sender.h @@ -18,6 +18,7 @@ #include #include +#include #include "visibility.h" namespace OHOS::DistributedKv { diff --git a/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp b/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp index 85305dbb43a822c1ff12cb699ef36240e8cdc8ef..a8cd69e2d63750fa9983bdb62361a8a67a3e3281 100644 --- a/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp +++ b/services/distributeddataservice/app/src/device_kvstore_observer_impl.cpp @@ -35,17 +35,17 @@ void DeviceKvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData std::list updateList = data.GetEntriesUpdated(); std::list deletedList = data.GetEntriesDeleted(); - std::list insertListTmp; - std::list updateListTmp; - std::list deletedListTmp; + std::vector inserts; + std::vector updates; + std::vector deleteds; std::string deviceId; - Transfer(insertList, insertListTmp, deviceId); - Transfer(updateList, updateListTmp, deviceId); - Transfer(deletedList, deletedListTmp, deviceId); + Transfer(insertList, inserts, deviceId); + Transfer(updateList, updates, deviceId); + Transfer(deletedList, deleteds, deviceId); if (deviceId.empty()) { ZLOGE("Did NOT find any valid deviceId"); } - ChangeNotification changeNotification(insertListTmp, updateListTmp, deletedListTmp, deviceId, false); + ChangeNotification changeNotification(std::move(inserts), std::move(updates), std::move(deleteds), deviceId, false); if (observerProxy_ != nullptr) { observerProxy_->OnChange(changeNotification, nullptr); } @@ -55,7 +55,7 @@ void DeviceKvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData } } -void DeviceKvStoreObserverImpl::Transfer(const std::list &input, std::list &output, +void DeviceKvStoreObserverImpl::Transfer(const std::list &input, std::vector &output, std::string &deviceId) { if (localDeviceId_.empty()) { diff --git a/services/distributeddataservice/app/src/device_kvstore_observer_impl.h b/services/distributeddataservice/app/src/device_kvstore_observer_impl.h index e292b14cad4f39c9b8d55f5917102a8053cc8086..bd9b1fd22a55f8c1d27cbf2d578429b57ca057a2 100644 --- a/services/distributeddataservice/app/src/device_kvstore_observer_impl.h +++ b/services/distributeddataservice/app/src/device_kvstore_observer_impl.h @@ -25,7 +25,7 @@ public: ~DeviceKvStoreObserverImpl() override; void OnChange(const DistributedDB::KvStoreChangedData &data) override; private: - void Transfer(const std::list &input, std::list &output, std::string &deviceId); + void Transfer(const std::list &input, std::vector &output, std::string &deviceId); bool deviceSync_; std::string localDeviceId_; sptr observerProxy_; diff --git a/services/distributeddataservice/app/src/kvstore_account_observer.cpp b/services/distributeddataservice/app/src/kvstore_account_observer.cpp index 4572d07dd3db2791a5f01c0864c887a68768c0bc..629c8d3862322ec6f6e38440f4d438417aaa25f9 100755 --- a/services/distributeddataservice/app/src/kvstore_account_observer.cpp +++ b/services/distributeddataservice/app/src/kvstore_account_observer.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "KvStoreAccountObserver" #include "kvstore_account_observer.h" +#include "kvstore_data_service.h" #include "log_print.h" #include namespace OHOS { diff --git a/services/distributeddataservice/app/src/kvstore_account_observer.h b/services/distributeddataservice/app/src/kvstore_account_observer.h index 782167c325a8aceb3f6211c0f40a303f6cad3212..eb4de43bd7bc6fefa5dff8fd4ce8cfcd33c791a0 100755 --- a/services/distributeddataservice/app/src/kvstore_account_observer.h +++ b/services/distributeddataservice/app/src/kvstore_account_observer.h @@ -16,7 +16,6 @@ #ifndef KVSTORE_ACCOUNT_OBSERVER_H #define KVSTORE_ACCOUNT_OBSERVER_H -#include "kvstore_data_service.h" #include "account_delegate.h" #include @@ -30,7 +29,7 @@ do { \ ZLOGW("system is busy with processing account event."); \ } \ } while (0) - +class KvStoreDataService; class KvStoreAccountObserver : public AccountDelegate::Observer { public: explicit KvStoreAccountObserver(KvStoreDataService &kvStoreDataService) diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 46960e3642e730abeb703e113e3eb965d2462432..880d41f4e107c83959d872377985bc0672657fed 100755 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -292,7 +292,7 @@ Status KvStoreMetaManager::CheckUpdateServiceMeta(const std::vector &me Status KvStoreMetaManager::GenerateRootKey() { ZLOGI("GenerateRootKey."); - struct HksBlob rootKeyName = { vecRootKeyAlias_.size(), &(vecRootKeyAlias_[0]) }; + struct HksBlob rootKeyName = { static_cast(vecRootKeyAlias_.size()), &(vecRootKeyAlias_[0]) }; struct HksParamSet *paramSet = nullptr; int32_t ret = HksInitParamSet(¶mSet); if (ret != HKS_SUCCESS) { @@ -368,10 +368,10 @@ Status KvStoreMetaManager::CheckRootKeyExist() std::vector KvStoreMetaManager::EncryptWorkKey(const std::vector &key) { - struct HksBlob blobAad = { vecAad_.size(), &(vecAad_[0]) }; - struct HksBlob blobNonce = { vecNonce_.size(), &(vecNonce_[0]) }; - struct HksBlob rootKeyName = { vecRootKeyAlias_.size(), &(vecRootKeyAlias_[0]) }; - struct HksBlob plainKey = { key.size(), const_cast(&(key[0])) }; + struct HksBlob blobAad = { static_cast(vecAad_.size()), &(vecAad_[0]) }; + struct HksBlob blobNonce = { static_cast(vecNonce_.size()), &(vecNonce_[0]) }; + struct HksBlob rootKeyName = { static_cast(vecRootKeyAlias_.size()), &(vecRootKeyAlias_[0]) }; + struct HksBlob plainKey = { static_cast(key.size()), const_cast(&(key[0])) }; uint8_t cipherBuf[256] = { 0 }; struct HksBlob encryptedKey = { sizeof(cipherBuf), cipherBuf }; std::vector encryptedKeyVec; @@ -420,10 +420,10 @@ std::vector KvStoreMetaManager::EncryptWorkKey(const std::vector &encryptedKey, std::vector &key) { - struct HksBlob blobAad = { vecAad_.size(), &(vecAad_[0]) }; - struct HksBlob blobNonce = { vecNonce_.size(), &(vecNonce_[0]) }; - struct HksBlob rootKeyName = { vecRootKeyAlias_.size(), &(vecRootKeyAlias_[0]) }; - struct HksBlob encryptedKeyBlob = { encryptedKey.size(), const_cast(&(encryptedKey[0])) }; + struct HksBlob blobAad = { static_cast(vecAad_.size()), &(vecAad_[0]) }; + struct HksBlob blobNonce = { static_cast(vecNonce_.size()), &(vecNonce_[0]) }; + struct HksBlob rootKeyName = { static_cast(vecRootKeyAlias_.size()), &(vecRootKeyAlias_[0]) }; + struct HksBlob encryptedKeyBlob = { static_cast(encryptedKey.size()), const_cast(&(encryptedKey[0])) }; uint8_t plainBuf[256] = { 0 }; struct HksBlob plainKeyBlob = { sizeof(plainBuf), plainBuf }; struct HksParamSet *decryptParamSet = nullptr; diff --git a/services/distributeddataservice/app/src/kvstore_observer_impl.cpp b/services/distributeddataservice/app/src/kvstore_observer_impl.cpp index 4ff122f74359db66c33b476f68090d88918bba71..8ae07060cb46befad6220a2bebe2ec723c4ec6d1 100644 --- a/services/distributeddataservice/app/src/kvstore_observer_impl.cpp +++ b/services/distributeddataservice/app/src/kvstore_observer_impl.cpp @@ -47,9 +47,9 @@ void KvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData &data std::list updateList = data.GetEntriesUpdated(); std::list deletedList = data.GetEntriesDeleted(); - std::list insertListTmp; - std::list updateListTmp; - std::list deletedListTmp; + std::vector inserts; + std::vector updates; + std::vector deleteds; for (const auto &entry : insertList) { Key key(entry.key); @@ -57,7 +57,7 @@ void KvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData &data Entry tmpEntry; tmpEntry.key = key; tmpEntry.value = value; - insertListTmp.push_back(tmpEntry); + inserts.push_back(tmpEntry); } for (const auto &entry : updateList) { @@ -66,7 +66,7 @@ void KvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData &data Entry tmpEntry; tmpEntry.key = key; tmpEntry.value = value; - updateListTmp.push_back(tmpEntry); + updates.push_back(tmpEntry); } for (const auto &entry : deletedList) { @@ -75,10 +75,10 @@ void KvStoreObserverImpl::OnChange(const DistributedDB::KvStoreChangedData &data Entry tmpEntry; tmpEntry.key = key; tmpEntry.value = value; - deletedListTmp.push_back(tmpEntry); + deleteds.push_back(tmpEntry); } - ChangeNotification changeNotification(insertListTmp, updateListTmp, deletedListTmp, std::string(), false); + ChangeNotification changeNotification(std::move(inserts), std::move(updates), std::move(deleteds), std::string(), false); ZLOGI("call proxy OnChange"); observerProxy_->OnChange(changeNotification, nullptr); } diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller.h b/services/distributeddataservice/app/src/uninstaller/uninstaller.h index 8895d473edcefaaac70eda8e871de0842dcce27d..92d2dfccdadf97e3c29251f0e935df98222397c4 100755 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller.h @@ -19,10 +19,11 @@ #include #include -#include "kvstore_data_service.h" #include "visibility.h" namespace OHOS::DistributedKv { +class KvStoreDataService; +enum class Status; class Uninstaller { public: KVSTORE_API virtual Status Init(KvStoreDataService *kvStoreDataService) = 0; diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index b433152b1b15290e56950a6fdaab6c203148eca1..0a05df79b7794f1433bec3e70391a98501da7c80 100755 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -17,6 +17,7 @@ #define DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H #include "common_event_subscriber.h" +#include "kvstore_data_service.h" #include "uninstaller.h" namespace OHOS::DistributedKv { diff --git a/services/distributeddataservice/app/test/moduletest/distributeddata_account_event_test.cpp b/services/distributeddataservice/app/test/moduletest/distributeddata_account_event_test.cpp index 34578d2ef037951a013676ba275a1ec704eec482..a5a6a04c50151d0f758807887700a40cea02c43e 100644 --- a/services/distributeddataservice/app/test/moduletest/distributeddata_account_event_test.cpp +++ b/services/distributeddataservice/app/test/moduletest/distributeddata_account_event_test.cpp @@ -41,7 +41,7 @@ using namespace testing::ext; using namespace OHOS::DistributedKv; using namespace OHOS; -using namespace Notification; +using namespace OHOS::EventFwk; static const int SYSTEM_USER_ID = 1000; @@ -69,33 +69,36 @@ void DistributedDataAccountEventTest::TearDownTestCase() void DistributedDataAccountEventTest::HarmonyAccountLogin() { - sptr intent = new AAFwk::Intent(); - intent->SetAction(CommonEventSupport::COMMON_EVENT_HWID_LOGIN); - sptr event = new CommonEventData(intent); - sptr publishInfo = new CommonEventPublishInfo(); - auto err = CommonEventManager::GetInstance().PublishCommonEventData(event, publishInfo, nullptr); + Want want; + want.SetAction(CommonEventSupport::COMMON_EVENT_HWID_LOGIN); + CommonEventData commonEventData(want); + CommonEventData event(want); + CommonEventPublishInfo publishInfo; + auto err = CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr); EXPECT_EQ(ERR_OK, err); sleep(WAIT_TIME_FOR_ACCOUNT_OPERATION); } void DistributedDataAccountEventTest::HarmonyAccountLogout() { - sptr intent = new AAFwk::Intent(); - intent->SetAction(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT); - sptr event = new CommonEventData(intent); - sptr publishInfo = new CommonEventPublishInfo(); - auto err = CommonEventManager::GetInstance().PublishCommonEventData(event, publishInfo, nullptr); + Want want; + want.SetAction(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT); + CommonEventData commonEventData(want); + CommonEventData event(want); + CommonEventPublishInfo publishInfo; + auto err = CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr); EXPECT_EQ(ERR_OK, err); sleep(WAIT_TIME_FOR_ACCOUNT_OPERATION); } void DistributedDataAccountEventTest::HarmonyAccountDelete() { - sptr intent = new AAFwk::Intent(); - intent->SetAction(CommonEventSupport::COMMON_EVENT_HWID_TOKEN_INVALID); - sptr event = new CommonEventData(intent); - sptr publishInfo = new CommonEventPublishInfo(); - auto err = CommonEventManager::GetInstance().PublishCommonEventData(event, publishInfo, nullptr); + Want want; + want.SetAction(CommonEventSupport::COMMON_EVENT_HWID_TOKEN_INVALID); + CommonEventData commonEventData(want); + CommonEventData event(want); + CommonEventPublishInfo publishInfo; + auto err = CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr); EXPECT_EQ(ERR_OK, err); sleep(WAIT_TIME_FOR_ACCOUNT_OPERATION); } diff --git a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp index 8f80565414edcf179b202ed285b35e823069cafd..dead40eca2ba67697896abbdfd990c82489adfea 100755 --- a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp @@ -70,8 +70,8 @@ void KvStoreBackupTest::TearDown(void) */ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest001, TestSize.Level1) { - Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true, .backup = true, - .kvStoreType = KvStoreType::SINGLE_VERSION, .dataOwnership = true }; + Options options = { .createIfMissing = true, .encrypt = false, .backup = true, .autoSync = true, + .kvStoreType = KvStoreType::SINGLE_VERSION, .dataOwnership = true }; AppId appId = { "backup1" }; StoreId storeId = { "store1" }; KvStoreDataService kvDataService; @@ -92,8 +92,8 @@ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest001, TestSize.Level1) */ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest002, TestSize.Level1) { - Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true, .backup = true, - .kvStoreType = KvStoreType::SINGLE_VERSION, .dataOwnership = true }; + Options options = { .createIfMissing = true, .encrypt = false, .backup = true, .autoSync = true, + .kvStoreType = KvStoreType::SINGLE_VERSION, .dataOwnership = true }; AppId appId = { "backup2" }; StoreId storeId = { "store2" }; @@ -144,8 +144,8 @@ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest002, TestSize.Level1) */ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest003, TestSize.Level1) { - Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true, .backup = true, - .kvStoreType = KvStoreType::MULTI_VERSION, .dataOwnership = true }; + Options options = { .createIfMissing = true, .encrypt = false, .backup = true, .autoSync = true, + .kvStoreType = KvStoreType::MULTI_VERSION, .dataOwnership = true }; AppId appId = { "backup3" }; StoreId storeId = { "store3" }; @@ -207,7 +207,7 @@ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest003, TestSize.Level1) */ HWTEST_F(KvStoreBackupTest, KvStoreBackupTest004, TestSize.Level1) { - Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true, .backup = true, + Options options = { .createIfMissing = true, .encrypt = false, .backup = true, .autoSync = true, .kvStoreType = KvStoreType::SINGLE_VERSION, .dataOwnership = true }; AppId appId = { "backup4" }; StoreId storeId = { "store4" }; 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 e2ec2a8a2d60e780d50d60e27e767be1e257ce2a..2a25bdaa55eb7d3be1804a76ccfa6c5875ad18c1 100755 --- a/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_impl_logical_isolation_test.cpp @@ -25,6 +25,7 @@ using namespace testing::ext; using namespace OHOS::DistributedKv; using namespace OHOS; +namespace { sptr g_kvStoreDataService; Options g_defaultOptions; AppId g_appId; @@ -33,6 +34,7 @@ AppId g_appId1; StoreId g_storeId1; AppId g_appId2; StoreId g_storeId2; +} class KvStoreImplLogicalIsolationTest : public testing::Test { public: 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 8648bb3043d03377c241dfabaf3e74d18c82dcb3..2d737c9145b285a176fb0f8394d0aa89b1bdebf1 100755 --- a/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_impl_physical_isolation_test.cpp @@ -26,6 +26,7 @@ using namespace testing::ext; using namespace OHOS::DistributedKv; using namespace OHOS; +namespace { sptr g_kvStoreDataService; Options g_defaultOptions; AppId g_appId; @@ -34,6 +35,7 @@ AppId g_appId1; StoreId g_storeId1; AppId g_appId2; StoreId g_storeId2; +} class KvStoreImplPhysicalIsolationTest : public testing::Test { public: