From d31f8bc9634ca3636542d9faaff08a8eebc553c8 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 10 Feb 2022 21:46:21 +0800 Subject: [PATCH 01/32] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93Query=E6=94=AF=E6=8C=81InKeys=E8=B0=93?= =?UTF-8?q?=E8=AF=8D&&SyncWithCondition=E6=94=AF=E6=8C=81SyncCallback?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/ikvstore_sync_callback.h | 4 +- .../distributeddatafwk/src/data_query.cpp | 23 ++++++ .../src/ikvstore_single.cpp | 1 + .../src/ikvstore_sync_callback.cpp | 12 ++- .../src/kvstore_sync_callback_client.cpp | 52 ++++++++++-- .../src/kvstore_sync_callback_client.h | 14 +++- .../src/single_kvstore_client.cpp | 24 +++++- .../src/single_kvstore_client.h | 2 +- .../distributeddata/include/data_query.h | 10 +++ .../distributeddata/include/single_kvstore.h | 4 +- .../src/communication_provider_impl.cpp | 2 +- .../app/src/query_helper.cpp | 63 ++++++++++++++- .../app/src/query_helper.h | 7 ++ .../app/src/single_kvstore_impl.cpp | 81 ++++++++----------- .../app/src/single_kvstore_impl.h | 3 +- 15 files changed, 233 insertions(+), 69 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h index 285509698..ecd2cb61f 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h @@ -28,7 +28,7 @@ namespace DistributedKv { class IKvStoreSyncCallback : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreSyncCallback"); - virtual void SyncCompleted(const std::map &results) = 0; + virtual void SyncCompleted(const std::map &results, const std::string &label) = 0; }; class KvStoreSyncCallbackStub : public IRemoteStub { @@ -41,7 +41,7 @@ class KvStoreSyncCallbackProxy : public IRemoteProxy { public: explicit KvStoreSyncCallbackProxy(const sptr &impl); ~KvStoreSyncCallbackProxy() = default; - void SyncCompleted(const std::map &results) override; + void SyncCompleted(const std::map &results, const std::string &label) override; private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp index 6ff0a512d..58ee95f4c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp @@ -56,6 +56,7 @@ const std::string DataQuery::TYPE_BOOLEAN = "BOOL"; const std::string DataQuery::VALUE_TRUE = "true"; const std::string DataQuery::VALUE_FALSE = "false"; const std::string DataQuery::SUGGEST_INDEX = "^SUGGEST_INDEX"; +const std::string DataQuery::IN_KEYS = "^IN_KEYS"; constexpr int MAX_QUERY_LENGTH = 5 * 1024; // Max query string length 5k DataQuery::DataQuery() @@ -537,6 +538,28 @@ DataQuery& DataQuery::SetSuggestIndex(const std::string &index) return *this; } +DataQuery& DataQuery::InKeys(const std::vector &keys) +{ + if (keys.empty()) { + ZLOGE("Invalid number param"); + return *this; + } + str_.append(SPACE); + str_.append(IN_KEYS); + str_.append(SPACE); + str_.append(START_IN); + str_.append(SPACE); + for (std::string key : keys) { + if (ValidateField(key)) { + EscapeSpace(key); + str_.append(key); + str_.append(SPACE); + } + } + str_.append(END_IN); + return *this; +} + std::string DataQuery::ToString() const { if (str_.length() > MAX_QUERY_LENGTH) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp index a2fd8e0f5..ff9cfef42 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp @@ -487,6 +487,7 @@ Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncM } return static_cast(reply.ReadInt32()); } + Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query) { MessageParcel data; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp index c2ede3608..c1c5907ee 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp @@ -18,8 +18,11 @@ #include "ikvstore_sync_callback.h" #include #include +#include #include "log_print.h" #include "message_parcel.h" +#include "message_option.h" +#include "types.h" namespace OHOS { namespace DistributedKv { @@ -31,7 +34,7 @@ KvStoreSyncCallbackProxy::KvStoreSyncCallbackProxy(const sptr &im : IRemoteProxy(impl) {} -void KvStoreSyncCallbackProxy::SyncCompleted(const std::map &results) +void KvStoreSyncCallbackProxy::SyncCompleted(const std::map &results, const std::string &label) { MessageParcel data; MessageParcel reply; @@ -50,6 +53,10 @@ void KvStoreSyncCallbackProxy::SyncCompleted(const std::map return; } } + if (!data.WriteString(label)) { + ZLOGW("write label error."); + return; + } MessageOption mo { MessageOption::TF_SYNC }; int error = Remote()->SendRequest(SYNCCOMPLETED, data, reply, mo); if (error != 0) { @@ -78,7 +85,8 @@ int32_t KvStoreSyncCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &d results.insert(std::pair(data.ReadString(), static_cast(data.ReadInt32()))); } - SyncCompleted(results); + std::string label = data.ReadString(); + SyncCompleted(results, label); return 0; } default: diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 9800daadb..bae23a46d 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -15,22 +15,60 @@ #define LOG_TAG "KvStoreSyncCallbackClient" +#include +#include +#include "log_print.h" #include "kvstore_sync_callback_client.h" namespace OHOS { namespace DistributedKv { +std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; +const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSyncCallbackLabel"); + +KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; + KvStoreSyncCallbackClient::KvStoreSyncCallbackClient(std::shared_ptr kvStoreSyncCallback) - : kvStoreSyncCallback_(kvStoreSyncCallback) -{} +{ + if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) == kvStoreSyncCallbackInfo_.end()) { + AddKvStoreSyncCallback(std::move(kvStoreSyncCallback), CommonSyncCallbackLabel); + } +} + +KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; + +void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &label) +{ + if (label.empty() && kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { + kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]->SyncCompleted(results); + } else if (kvStoreSyncCallbackInfo_.find(label) != kvStoreSyncCallbackInfo_.end()) { + ZLOGI("label = %{public}s", label.c_str()); + kvStoreSyncCallbackInfo_[label]->SyncCompleted(results); + } +} -KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() -{} +void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, + const std::string &label) +{ + std::mutex mtx; + if(kvStoreSyncCallbackInfo_.find(label) == kvStoreSyncCallbackInfo_.end()) { + mtx.lock(); + kvStoreSyncCallbackInfo_.insert({label, kvStoreSyncCallback}); + mtx.unlock(); + } +} -void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results) +std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCallback() { - if (kvStoreSyncCallback_ != nullptr) { - kvStoreSyncCallback_->SyncCompleted(results); + if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { + return kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]; + } else { + return nullptr; } } + +std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() +{ + return CommonSyncCallbackLabel; +} } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index c8ba5fa86..0c50bca6f 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -24,16 +24,24 @@ namespace DistributedKv { class KvStoreSyncCallbackClient : public KvStoreSyncCallbackStub { public: + explicit KvStoreSyncCallbackClient(); + explicit KvStoreSyncCallbackClient(std::shared_ptr kvStoreSyncCallback); virtual ~KvStoreSyncCallbackClient(); - void SyncCompleted(const std::map &results) override; + void SyncCompleted(const std::map &results, const std::string &label) override; + + void AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, + const std::string &label); + std::shared_ptr GetCommonSyncCallback(); + + std::string GetCommonSyncCallbackLabel(); private: - std::shared_ptr kvStoreSyncCallback_; + static const std::string CommonSyncCallbackLabel; + static std::map> kvStoreSyncCallbackInfo_; }; - } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 6ee6c46bd..b0c06a4bf 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -449,8 +449,8 @@ Status SingleKvStoreClient::GetSecurityLevel(SecurityLevel &securityLevel) const return Status::SERVER_UNAVAILABLE; } -Status SingleKvStoreClient::SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query) +Status SingleKvStoreClient::SyncWithCondition(const std::vector &deviceIds, SyncMode mode, + const DataQuery &query, std::shared_ptr syncCallback) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); @@ -460,6 +460,26 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } + sptr ipcCallback; + if (syncCallback == nullptr) { + ipcCallback = new KvStoreSyncCallbackClient(); + if (ipcCallback->GetCommonSyncCallback() != nullptr) { + syncCallback = ipcCallback->GetCommonSyncCallback(); + ipcCallback = new (std::nothrow) KvStoreSyncCallbackClient(syncCallback); + } + } else { + ipcCallback = new (std::nothrow) KvStoreSyncCallbackClient(syncCallback); + for (const std::string &deviceId : deviceIds) { + std::string label = deviceId + query.ToString(); + ZLOGI("label = %{public}s", label.c_str()); + ipcCallback->AddKvStoreSyncCallback(syncCallback, label); + } + } + auto status = kvStoreProxy_->RegisterSyncCallback(ipcCallback); + if (status != Status::SUCCESS) { + ZLOGE("RegisterSyncCallback is not success."); + return status; + } return kvStoreProxy_->Sync(deviceIds, mode, query.ToString()); } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 2a15f3c26..2558be285 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -85,7 +85,7 @@ public: Status GetSecurityLevel(SecurityLevel &securityLevel) const override; Status SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query) override; + const DataQuery &query, std::shared_ptr syncCallback) override; Status SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; Status UnSubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; diff --git a/interfaces/innerkits/distributeddata/include/data_query.h b/interfaces/innerkits/distributeddata/include/data_query.h index dc8a36f86..cebd86dd6 100755 --- a/interfaces/innerkits/distributeddata/include/data_query.h +++ b/interfaces/innerkits/distributeddata/include/data_query.h @@ -399,6 +399,13 @@ public: // This Query. KVSTORE_API DataQuery& SetSuggestIndex(const std::string &index); + // Select results with many keys. + // Parameters: + // keys: the vector of keys for query + // Return: + // This Query. + KVSTORE_API DataQuery& InKeys(const std::vector &keys); + // Get string representation // Return: // String representation of this query. @@ -511,6 +518,9 @@ public: // suggested index static const std::string SUGGEST_INDEX; + + //in keys + static const std::string IN_KEYS; private: std::string str_; diff --git a/interfaces/innerkits/distributeddata/include/single_kvstore.h b/interfaces/innerkits/distributeddata/include/single_kvstore.h index 312f7f1c8..a42c16a12 100755 --- a/interfaces/innerkits/distributeddata/include/single_kvstore.h +++ b/interfaces/innerkits/distributeddata/include/single_kvstore.h @@ -185,7 +185,7 @@ public: * Status of this Sync operation. */ KVSTORE_API virtual Status SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query) = 0; + const DataQuery &query, std::shared_ptr syncCallback) = 0; /* * Subscribe store with other devices consistently Synchronize the data which is satisfied with the condition. @@ -196,7 +196,7 @@ public: * Status of this Subscribe operation. */ KVSTORE_API virtual Status SubscribeWithQuery(const std::vector &deviceIds, - const DataQuery &query) = 0; + const DataQuery &query) = 0; /* * UnSubscribe store with other devices which is satisfied with the condition. diff --git a/services/distributeddataservice/adapter/communicator/src/communication_provider_impl.cpp b/services/distributeddataservice/adapter/communicator/src/communication_provider_impl.cpp index 259fdb2b0..54d2fb31f 100755 --- a/services/distributeddataservice/adapter/communicator/src/communication_provider_impl.cpp +++ b/services/distributeddataservice/adapter/communicator/src/communication_provider_impl.cpp @@ -108,7 +108,7 @@ std::vector CommunicationProviderImpl::GetRemoteNodesBasicInfo() con std::string CommunicationProviderImpl::ToNodeId(const std::string &id) const { - std::string ret = appDeviceHandler_.ToNodeID(id, ""); + std::string ret = appDeviceHandler_.ToNodeID("", id); if (ret.empty()) { ZLOGD("toNodeId failed."); } diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index fa0494240..e7ba59a13 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -22,6 +22,8 @@ #include "kvstore_utils.h" #include "data_query.h" #include "log_print.h" +#include +#include "types.h" namespace OHOS::DistributedKv { constexpr int QUERY_SKIP_SIZE = 1; @@ -29,11 +31,12 @@ constexpr int QUERY_WORD_SIZE = 2; constexpr int MAX_QUERY_LENGTH = 5 * 1024; // Max query string length 5k constexpr int MAX_QUERY_COMPLEXITY = 500; // Max query complexity 500 bool QueryHelper::hasPrefixKey_{}; +bool QueryHelper::hasInKeys_{}; std::string QueryHelper::deviceId_{}; DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool &isSuccess) { - ZLOGI("query string length:%zu", query.length()); + ZLOGI("query string length:%{public}zu", query.length()); DistributedDB::Query dbQuery = DistributedDB::Query::Select(); if (query.size() == 0) { ZLOGI("Query string is empty."); @@ -47,6 +50,7 @@ DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool } deviceId_.clear(); hasPrefixKey_ = (query.find(DataQuery::KEY_PREFIX) != std::string::npos); + hasInKeys_ = (query.find(DataQuery::IN_KEYS) != std::string::npos); size_t pos = query.find_first_not_of(DataQuery::SPACE); std::string inputTrim = (pos == std::string::npos) ? "" : query.substr(pos); std::regex regex(" "); @@ -127,6 +131,8 @@ void QueryHelper::HandleExtra(const std::vector &words, int &pointe HandleDeviceId(words, pointer, end, isSuccess, dbQuery); } else if (keyword == DataQuery::SUGGEST_INDEX) { HandleSetSuggestIndex(words, pointer, end, isSuccess, dbQuery); + } else if (keyword == DataQuery::IN_KEYS) { + HandleInKeys(words, pointer, end, isSuccess, dbQuery); } else { ZLOGE("Invalid keyword."); isSuccess = false; @@ -497,6 +503,29 @@ void QueryHelper::HandleKeyPrefix(const std::vector &words, int &po pointer += 2; // Pointer goes to next keyword } +void QueryHelper::HandleInKeys(const std::vector &words, int &pointer, + const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { + if (pointer + 2 > end || words.at(pointer + 1) != DataQuery::START_IN) { // This keyword has at least 2 params + ZLOGE("In not enough params."); + isSuccess = false; + return; + } + int elementPointer = pointer + 2; + const std::vector inKeys = GetInKeyList(words, elementPointer, end); + std::set> inDbKeys; + for(const std::string &inKey : inKeys) { + ZLOGI("inKey=%{public}s", inKey.c_str()); + std::vector dbKey; + dbKey.assign(inKey.begin(), inKey.end()); + inDbKeys.insert(dbKey); + } + int size = inDbKeys.size(); + ZLOGI("size of inKeys=%{public}d", size); + dbQuery.InKeys(inDbKeys); + isSuccess = true; + pointer = elementPointer + 1; // Pointer goes to next keyword +} + void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { if (pointer + QUERY_SKIP_SIZE > end) { @@ -528,6 +557,16 @@ void QueryHelper::HandleDeviceId(const std::vector &words, int &poi } else { ZLOGD("Join deviceId with user specified prefixkey later."); } + if (!hasInKeys_) { + ZLOGD("DeviceId as the only inkey."); + std::set> inDbKeys; + std::vector dbKey; + dbKey.assign(deviceId_.begin(), deviceId_.begin()); + inDbKeys.insert(dbKey); + dbQuery.InKeys(inDbKeys); + } else { + ZLOGD("Join deviceId with user specified inkeys later."); + } isSuccess = true; pointer += 2; // Pointer goes to next keyword } @@ -667,4 +706,26 @@ std::vector QueryHelper::GetStringList(const std::vector(); } } + +std::vector QueryHelper::GetInKeyList(const std::vector &words, + int &elementPointer, const int &end) { + std::vector values; + bool isEndFound = false; + while (elementPointer <= end) { + if (words.at(elementPointer) == DataQuery::END_IN) { + isEndFound = true; + break; + } + std::string inKeyStr = deviceId_ + StringToString(words.at(elementPointer)); + values.push_back(inKeyStr); + ZLOGI("value=%{public}s", inKeyStr.c_str()); + elementPointer++; + } + if (isEndFound) { + return values; + } else { + ZLOGE("GetStringList failed."); + return std::vector(); + } +} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/query_helper.h b/services/distributeddataservice/app/src/query_helper.h index 158b25b55..241b24db8 100755 --- a/services/distributeddataservice/app/src/query_helper.h +++ b/services/distributeddataservice/app/src/query_helper.h @@ -17,6 +17,8 @@ #define QUERY_HELPER_H #include "query.h" +#include +#include "types.h" namespace OHOS::DistributedKv { class QueryHelper { @@ -25,6 +27,7 @@ public: private: static std::string deviceId_; static bool hasPrefixKey_; + static bool hasInKeys_; static void Handle(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); static void HandleExtra(const std::vector &words, int &pointer, @@ -69,6 +72,8 @@ private: const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); static void HandleKeyPrefix(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); + static void HandleInKeys(const std::vector &words, int &pointer, + const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); static void HandleSetSuggestIndex(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); static void HandleDeviceId(const std::vector &words, int &pointer, @@ -84,6 +89,8 @@ private: int &elementPointer, const int &end); static std::vector GetStringList(const std::vector &words, int &elementPointer, const int &end); + static std::vector GetInKeyList(const std::vector &words, + int &elementPointer, const int &end); }; } // namespace OHOS::DistributedKv #endif // QUERY_HELPER_H diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 4389691c4..4f2f1edf7 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -297,19 +297,19 @@ int SingleKvStoreImpl::ConvertToDbObserverMode(const SubscribeType subscribeType return dbObserverMode; } - // Convert KvStore sync mode to DistributeDB sync mode. - DistributedDB::SyncMode SingleKvStoreImpl::ConvertToDbSyncMode(SyncMode syncMode) const - { - DistributedDB::SyncMode dbSyncMode; - if (syncMode == SyncMode::PUSH) { - dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - } else if (syncMode == SyncMode::PULL) { - dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PULL_ONLY; - } else { - dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_PULL; - } - return dbSyncMode; +// Convert KvStore sync mode to DistributeDB sync mode. +DistributedDB::SyncMode SingleKvStoreImpl::ConvertToDbSyncMode(SyncMode syncMode) const +{ + DistributedDB::SyncMode dbSyncMode; + if (syncMode == SyncMode::PUSH) { + dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; + } else if (syncMode == SyncMode::PULL) { + dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PULL_ONLY; + } else { + dbSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_PULL; } + return dbSyncMode; +} Status SingleKvStoreImpl::UnSubscribeKvStore(const SubscribeType subscribeType, sptr observer) { @@ -457,34 +457,11 @@ Status SingleKvStoreImpl::GetEntriesWithQuery(const std::string &query, std::vec } return Status::SUCCESS; } - switch (status) { - case DistributedDB::DBStatus::BUSY: - case DistributedDB::DBStatus::DB_ERROR: { - return Status::DB_ERROR; - } - case DistributedDB::DBStatus::INVALID_ARGS: { - return Status::INVALID_ARGUMENT; - } - case DistributedDB::DBStatus::INVALID_QUERY_FORMAT: { - return Status::INVALID_QUERY_FORMAT; - } - case DistributedDB::DBStatus::INVALID_QUERY_FIELD: { - return Status::INVALID_QUERY_FIELD; - } - case DistributedDB::DBStatus::NOT_SUPPORT: { - return Status::NOT_SUPPORT; - } - case DistributedDB::DBStatus::NOT_FOUND: { - ZLOGI("DB return NOT_FOUND, no matching result. Return success with empty list."); - return Status::SUCCESS; - } - case DistributedDB::DBStatus::EKEYREVOKED_ERROR: // fallthrough - case DistributedDB::DBStatus::SECURITY_OPTION_CHECK_ERROR: - return Status::SECURITY_LEVEL_ERROR; - default: { - return Status::ERROR; - } + if (status == DistributedDB::DBStatus::NOT_FOUND) { + ZLOGI("DB return NOT_FOUND, no matching result. Return success with empty list."); + return Status::SUCCESS; } + return ConvertDbStatus(status); } void SingleKvStoreImpl::GetResultSet(const Key &prefixKey, @@ -774,9 +751,10 @@ Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, Syn { ZLOGD("start."); waitingSyncCount_++; + const std::string query; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query)); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, @@ -786,7 +764,7 @@ Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, Syn waitingSyncCount_++; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoQuerySync, this, deviceIds, mode, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query)); } uint32_t SingleKvStoreImpl::GetSyncDelayTime(uint32_t allowedDelayMs) const @@ -814,7 +792,8 @@ Status SingleKvStoreImpl::RemoveAllSyncOperation() return KvStoreSyncManager::GetInstance()->RemoveSyncOperation(reinterpret_cast(this)); } -void SingleKvStoreImpl::DoSyncComplete(const std::map &devicesSyncResult) +void SingleKvStoreImpl::DoSyncComplete(const std::map &devicesSyncResult, + const std::string &query) { DdsTrace trace(std::string("DdsTrace " LOG_TAG "::") + std::string(__FUNCTION__)); std::map resultMap; @@ -824,7 +803,16 @@ void SingleKvStoreImpl::DoSyncComplete(const std::mapSyncCompleted(resultMap); + for (const auto &deviceSyncResult : devicesSyncResult) { + auto deviceId = deviceSyncResult.first; + ZLOGI("deviceId = %{public}s", deviceId.c_str()); + // map UUID to nodeId + std::string deviceNodeId = KvStoreUtils::GetProviderInstance().ToNodeId(deviceId); + ZLOGI("deviceNodeId = %{public}s", deviceNodeId.c_str()); + std::string label = deviceNodeId + query; + ZLOGI("label = %{public}s", label.c_str()); + syncCallback_->SyncCompleted(resultMap, label); + } } } @@ -868,8 +856,7 @@ Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, if (status == DistributedDB::DBStatus::BUSY) { if (syncRetries_ < KvStoreSyncManager::SYNC_RETRY_MAX_COUNT) { syncRetries_++; - auto addStatus = AddSync(deviceIds, mode, query, - KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS); + auto addStatus = AddSync(deviceIds, mode, query, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS); if (addStatus == Status::SUCCESS) { return addStatus; } @@ -992,7 +979,7 @@ Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector & ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "")); } Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, @@ -1001,7 +988,7 @@ Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoUnSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "")); } Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &deviceIds, diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index 2e2eaae5b..e104479c3 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -88,7 +88,8 @@ private: Status AddSync(const std::vector &deviceIds, SyncMode mode, const std::string &query, uint32_t delayMs); Status RemoveAllSyncOperation(); - void DoSyncComplete(const std::map &devicesSyncResult); + void DoSyncComplete(const std::map &devicesSyncResult, + const std::string &query); Status DoSync(const std::vector &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd); Status DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); -- Gitee From 365b2b64fae3023c9f07ed6f909081bda1f3c23b Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 10:52:00 +0800 Subject: [PATCH 02/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A3=80=E6=9F=A5inkey?= =?UTF-8?q?s=E6=98=AF=E5=90=A6=E6=98=AF=E6=97=A0=E6=95=88=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp index 58ee95f4c..bfe6a425c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp @@ -540,10 +540,13 @@ DataQuery& DataQuery::SetSuggestIndex(const std::string &index) DataQuery& DataQuery::InKeys(const std::vector &keys) { +<<<<<<< HEAD if (keys.empty()) { ZLOGE("Invalid number param"); return *this; } +======= +>>>>>>> 672b78e5d6bb76451eb6d1126117c72da6d9890b str_.append(SPACE); str_.append(IN_KEYS); str_.append(SPACE); -- Gitee From 6c7d9453c6ed6e236a413de9bc2c96ea6c3da6e9 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 11:16:56 +0800 Subject: [PATCH 03/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9inkeys?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=98=AF=E6=97=A0=E6=95=88=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp index bfe6a425c..58ee95f4c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp @@ -540,13 +540,10 @@ DataQuery& DataQuery::SetSuggestIndex(const std::string &index) DataQuery& DataQuery::InKeys(const std::vector &keys) { -<<<<<<< HEAD if (keys.empty()) { ZLOGE("Invalid number param"); return *this; } -======= ->>>>>>> 672b78e5d6bb76451eb6d1126117c72da6d9890b str_.append(SPACE); str_.append(IN_KEYS); str_.append(SPACE); -- Gitee From 691154427479801f18d576cd7c6966411c8ae2f8 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 14:05:04 +0800 Subject: [PATCH 04/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9synccallback,=E4=BF=AE?= =?UTF-8?q?=E6=94=B9registersynccallback=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/ikvstore_sync_callback.cpp | 6 +++++- .../src/kvstore_sync_callback_client.cpp | 7 ------- .../src/kvstore_sync_callback_client.h | 2 -- .../src/single_kvstore_client.cpp | 20 +++++-------------- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp index c1c5907ee..e46175bbf 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp @@ -85,7 +85,11 @@ int32_t KvStoreSyncCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &d results.insert(std::pair(data.ReadString(), static_cast(data.ReadInt32()))); } - std::string label = data.ReadString(); + const std::string &label = data.ReadString(); + if (label.empty()) { + ZLOGE("get label error"); + return -1; + } SyncCompleted(results, label); return 0; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index bae23a46d..04bbc875c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -27,13 +27,6 @@ const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSync KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; -KvStoreSyncCallbackClient::KvStoreSyncCallbackClient(std::shared_ptr kvStoreSyncCallback) -{ - if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) == kvStoreSyncCallbackInfo_.end()) { - AddKvStoreSyncCallback(std::move(kvStoreSyncCallback), CommonSyncCallbackLabel); - } -} - KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &label) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 0c50bca6f..ab954f311 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -26,8 +26,6 @@ class KvStoreSyncCallbackClient : public KvStoreSyncCallbackStub { public: explicit KvStoreSyncCallbackClient(); - explicit KvStoreSyncCallbackClient(std::shared_ptr kvStoreSyncCallback); - virtual ~KvStoreSyncCallbackClient(); void SyncCompleted(const std::map &results, const std::string &label) override; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index b0c06a4bf..97845fe04 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -305,12 +305,9 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = - new (std::nothrow) KvStoreSyncCallbackClient(callback); - if (ipcCallback == nullptr) { - ZLOGW("new KvStoreSyncCallbackClient failed"); - return Status::ERROR; - } + sptr ipcCallback = new KvStoreSyncCallbackClient(); + const std::string &label = ipcCallback->GetCommonSyncCallbackLabel(); + ipcCallback->AddKvStoreSyncCallback(callback, label); return kvStoreProxy_->RegisterSyncCallback(ipcCallback); } @@ -460,15 +457,8 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback; - if (syncCallback == nullptr) { - ipcCallback = new KvStoreSyncCallbackClient(); - if (ipcCallback->GetCommonSyncCallback() != nullptr) { - syncCallback = ipcCallback->GetCommonSyncCallback(); - ipcCallback = new (std::nothrow) KvStoreSyncCallbackClient(syncCallback); - } - } else { - ipcCallback = new (std::nothrow) KvStoreSyncCallbackClient(syncCallback); + sptr ipcCallback = new KvStoreSyncCallbackClient(); + if (syncCallback != nullptr) { for (const std::string &deviceId : deviceIds) { std::string label = deviceId + query.ToString(); ZLOGI("label = %{public}s", label.c_str()); -- Gitee From 579551adf574b902cc5efe403426d2222ce10e99 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 15:54:02 +0800 Subject: [PATCH 05/32] =?UTF-8?q?=E5=B0=86dosynccomplete=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E4=B8=B2=E6=94=B9=E4=B8=BA=E7=89=B9=E6=AE=8A=E6=A0=87?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/single_kvstore_client.cpp | 8 +++---- .../app/src/single_kvstore_impl.cpp | 22 +++++++++---------- .../app/src/single_kvstore_impl.h | 1 + 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 97845fe04..8ca4bdfbf 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -459,11 +459,9 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de } sptr ipcCallback = new KvStoreSyncCallbackClient(); if (syncCallback != nullptr) { - for (const std::string &deviceId : deviceIds) { - std::string label = deviceId + query.ToString(); - ZLOGI("label = %{public}s", label.c_str()); - ipcCallback->AddKvStoreSyncCallback(syncCallback, label); - } + std::string label = query.ToString(); + ZLOGI("label = %{public}s", label.c_str()); + ipcCallback->AddKvStoreSyncCallback(syncCallback, label); } auto status = kvStoreProxy_->RegisterSyncCallback(ipcCallback); if (status != Status::SUCCESS) { diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 4f2f1edf7..31120bf6e 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -33,6 +33,7 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; +const std::string SingleKvStoreImpl::DEFAULT_QUERY("default_query"); static bool TaskIsBackground(pid_t pid) { std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup", std::ios::in); @@ -751,10 +752,9 @@ Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, Syn { ZLOGD("start."); waitingSyncCount_++; - const std::string query; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, @@ -803,13 +803,13 @@ void SingleKvStoreImpl::DoSyncComplete(const std::mapSyncCompleted(resultMap, label); + } else { + label = "CommonSyncCallbackLabel"; ZLOGI("label = %{public}s", label.c_str()); syncCallback_->SyncCompleted(resultMap, label); } @@ -979,7 +979,7 @@ Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector & ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "")); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); } Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, @@ -988,7 +988,7 @@ Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoUnSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "")); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); } Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &deviceIds, diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index e104479c3..eeb208c42 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -153,6 +153,7 @@ private: KvStoreFlowCtrlManager flowCtrl_; static constexpr int BURST_CAPACITY = 1000; static constexpr int SUSTAINED_CAPACITY = 10000; + static const std::string DEFAULT_QUERY; }; } // namespace OHOS::DistributedKv #endif // SINGLE_KVSTORE_IMPL_H -- Gitee From 2a64b2c7f21a2bf199bad3e984537e1f47697a84 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 15:58:44 +0800 Subject: [PATCH 06/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9KvStoreSyncCallbackClie?= =?UTF-8?q?nt::SyncCompleted=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../distributeddatafwk/src/kvstore_sync_callback_client.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 04bbc875c..6316cb613 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -31,9 +31,7 @@ KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &label) { - if (label.empty() && kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]->SyncCompleted(results); - } else if (kvStoreSyncCallbackInfo_.find(label) != kvStoreSyncCallbackInfo_.end()) { + if (kvStoreSyncCallbackInfo_.find(label) != kvStoreSyncCallbackInfo_.end()) { ZLOGI("label = %{public}s", label.c_str()); kvStoreSyncCallbackInfo_[label]->SyncCompleted(results); } -- Gitee From 2fdc2fc288ef107d0f700ed333de9f9e044c710c Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 16:18:20 +0800 Subject: [PATCH 07/32] =?UTF-8?q?KvStoreSyncCallbackClient=E4=B8=AD?= =?UTF-8?q?=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 8 +++----- .../distributeddatafwk/src/kvstore_sync_callback_client.h | 2 ++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 6316cb613..00dc9a022 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -15,8 +15,6 @@ #define LOG_TAG "KvStoreSyncCallbackClient" -#include -#include #include "log_print.h" #include "kvstore_sync_callback_client.h" @@ -31,6 +29,7 @@ KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &label) { + std::lock_guard lg(syncCallbackMutex_); if (kvStoreSyncCallbackInfo_.find(label) != kvStoreSyncCallbackInfo_.end()) { ZLOGI("label = %{public}s", label.c_str()); kvStoreSyncCallbackInfo_[label]->SyncCompleted(results); @@ -40,16 +39,15 @@ void KvStoreSyncCallbackClient::SyncCompleted(const std::map kvStoreSyncCallback, const std::string &label) { - std::mutex mtx; + std::lock_guard lg(syncCallbackMutex_); if(kvStoreSyncCallbackInfo_.find(label) == kvStoreSyncCallbackInfo_.end()) { - mtx.lock(); kvStoreSyncCallbackInfo_.insert({label, kvStoreSyncCallback}); - mtx.unlock(); } } std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCallback() { + std::lock_guard lg(syncCallbackMutex_); if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { return kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]; } else { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index ab954f311..1c681b1bc 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -16,6 +16,7 @@ #ifndef KVSTORE_SYNC_CALLBACK_CLIENT_H #define KVSTORE_SYNC_CALLBACK_CLIENT_H +#include #include "ikvstore_sync_callback.h" #include "kvstore_sync_callback.h" @@ -39,6 +40,7 @@ public: private: static const std::string CommonSyncCallbackLabel; static std::map> kvStoreSyncCallbackInfo_; + std::mutex syncCallbackMutex_; }; } // namespace DistributedKv } // namespace OHOS -- Gitee From d352261f365ea3114dbf872c9d2af87f28a820a8 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 16:51:58 +0800 Subject: [PATCH 08/32] =?UTF-8?q?=E5=9C=A8unregisterSyncCallback=E4=B8=AD?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=B0=86common=E6=A0=87=E7=AD=BE=E4=BB=8Emap?= =?UTF-8?q?=E4=B8=AD=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 8 ++++++++ .../distributeddatafwk/src/kvstore_sync_callback_client.h | 2 ++ .../distributeddatafwk/src/single_kvstore_client.cpp | 2 ++ 3 files changed, 12 insertions(+) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 00dc9a022..99451a3a5 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -55,6 +55,14 @@ std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCal } } +void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback() +{ + std::lock_guard lg(syncCallbackMutex_); + if(kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { + kvStoreSyncCallbackInfo_.erase(CommonSyncCallbackLabel); + } +} + std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() { return CommonSyncCallbackLabel; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 1c681b1bc..4eb448d43 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -36,6 +36,8 @@ public: std::shared_ptr GetCommonSyncCallback(); + void DeleteCommonKvStoreSyncCallback(); + std::string GetCommonSyncCallbackLabel(); private: static const std::string CommonSyncCallbackLabel; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 8ca4bdfbf..706de5b76 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -314,6 +314,8 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = new KvStoreSyncCallbackClient(); + ipcCallback->DeleteCommonKvStoreSyncCallback(); return kvStoreProxy_->UnRegisterSyncCallback(); } -- Gitee From aefd74e413b676cfacf080a1079feb114b489350 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 11 Feb 2022 18:25:11 +0800 Subject: [PATCH 09/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E4=BE=8B?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=8F=96=E6=B6=88?= =?UTF-8?q?synccallback=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../distributeddatafwk/src/kvstore_sync_callback_client.cpp | 3 ++- .../distributeddatafwk/src/kvstore_sync_callback_client.h | 6 ++++++ .../distributeddatafwk/src/single_kvstore_client.cpp | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 99451a3a5..f91b08a91 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -21,6 +21,7 @@ namespace OHOS { namespace DistributedKv { std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; +sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSyncCallbackLabel"); KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; @@ -68,4 +69,4 @@ std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() return CommonSyncCallbackLabel; } } // namespace DistributedKv -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 4eb448d43..1d438143a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -39,10 +39,16 @@ public: void DeleteCommonKvStoreSyncCallback(); std::string GetCommonSyncCallbackLabel(); + + static sptr GetInstance() + { + return pInstance_; + } private: static const std::string CommonSyncCallbackLabel; static std::map> kvStoreSyncCallbackInfo_; std::mutex syncCallbackMutex_; + static sptr pInstance_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 706de5b76..452add7bf 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -305,7 +305,7 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = new KvStoreSyncCallbackClient(); + sptr ipcCallback = GetInstance(); const std::string &label = ipcCallback->GetCommonSyncCallbackLabel(); ipcCallback->AddKvStoreSyncCallback(callback, label); return kvStoreProxy_->RegisterSyncCallback(ipcCallback); @@ -314,7 +314,7 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = new KvStoreSyncCallbackClient(); + sptr ipcCallback = GetInstance(); ipcCallback->DeleteCommonKvStoreSyncCallback(); return kvStoreProxy_->UnRegisterSyncCallback(); } -- Gitee From 87768c09a6b67e0e4a1a607c8018c475b5d985cd Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 12:06:42 +0800 Subject: [PATCH 10/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86commonSyncCall?= =?UTF-8?q?backLabel=E7=9A=84=E8=AE=BE=E7=BD=AE=E5=8F=8Asynccallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/ikvstore_single.h | 6 ++- .../include/ikvstore_sync_callback.h | 1 + .../src/ikvstore_single.cpp | 10 ++++- .../src/kvstore_sync_callback_client.cpp | 4 +- .../src/kvstore_sync_callback_client.h | 2 +- .../src/single_kvstore_client.cpp | 14 ++++--- .../src/single_kvstore_client.h | 4 +- .../distributeddata/include/single_kvstore.h | 2 +- .../app/src/single_kvstore_impl.cpp | 41 ++++++++----------- .../app/src/single_kvstore_impl.h | 9 ++-- 10 files changed, 51 insertions(+), 42 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h index 996697f7d..0ad1884f7 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h @@ -89,7 +89,8 @@ public: virtual Status SetCapabilityRange(const std::vector &localLabels, const std::vector &remoteSupportLabels) = 0; virtual Status GetSecurityLevel(SecurityLevel &securityLevel) = 0; - virtual Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query) = 0; + virtual Status Sync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const std::string &syncLabel) = 0; virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query) = 0; virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query) = 0; }; @@ -179,7 +180,8 @@ public: virtual Status CloseResultSet(sptr resultSet); virtual Status GetCountWithQuery(const std::string &query, int &result); virtual Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs); - virtual Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query); + virtual Status Sync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const std::string &syncLabel); virtual Status RemoveDeviceData(const std::string &device); virtual Status RegisterSyncCallback(sptr callback); virtual Status UnRegisterSyncCallback(); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h index ecd2cb61f..62ab68946 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h @@ -29,6 +29,7 @@ class IKvStoreSyncCallback : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreSyncCallback"); virtual void SyncCompleted(const std::map &results, const std::string &label) = 0; + const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; }; class KvStoreSyncCallbackStub : public IRemoteStub { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp index ff9cfef42..e7d205ed3 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp @@ -488,7 +488,8 @@ Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncM return static_cast(reply.ReadInt32()); } -Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query) +Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const std::string &syncLabel) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -505,6 +506,10 @@ Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncM ZLOGE("write query fail"); return Status::IPC_ERROR; } + if (!data.WriteString(syncLabel)) { + ZLOGE("write label fail"); + return Status::IPC_ERROR; + } MessageParcel reply; int32_t error = Remote()->SendRequest(SYNC_WITH_CONDITION, data, reply, mo); if (error != 0) { @@ -1594,7 +1599,8 @@ int SingleKvStoreStub::OnSyncRequest(MessageParcel &data, MessageParcel &reply) } auto mode = static_cast(data.ReadInt32()); auto query = data.ReadString(); - Status status = Sync(devices, mode, query); + auto synclabel = data.ReadString(); + Status status = Sync(devices, mode, query, synclabel); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index f91b08a91..deeea0162 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -20,9 +20,9 @@ namespace OHOS { namespace DistributedKv { -std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); -const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSyncCallbackLabel"); +std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; +//const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSyncCallbackLabel"); KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 1d438143a..78a71d1f1 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -45,7 +45,7 @@ public: return pInstance_; } private: - static const std::string CommonSyncCallbackLabel; +// static const std::string CommonSyncCallbackLabel; static std::map> kvStoreSyncCallbackInfo_; std::mutex syncCallbackMutex_; static sptr pInstance_; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 452add7bf..90e94eb3a 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -305,8 +305,9 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = GetInstance(); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); const std::string &label = ipcCallback->GetCommonSyncCallbackLabel(); + ZLOGI("label = %{public}s", label.c_str()); ipcCallback->AddKvStoreSyncCallback(callback, label); return kvStoreProxy_->RegisterSyncCallback(ipcCallback); } @@ -314,7 +315,7 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = GetInstance(); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); ipcCallback->DeleteCommonKvStoreSyncCallback(); return kvStoreProxy_->UnRegisterSyncCallback(); } @@ -459,18 +460,21 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback = new KvStoreSyncCallbackClient(); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + std::string label; if (syncCallback != nullptr) { - std::string label = query.ToString(); + label = query.ToString(); ZLOGI("label = %{public}s", label.c_str()); ipcCallback->AddKvStoreSyncCallback(syncCallback, label); + } else { + label = ipcCallback->GetCommonSyncCallbackLabel(); } auto status = kvStoreProxy_->RegisterSyncCallback(ipcCallback); if (status != Status::SUCCESS) { ZLOGE("RegisterSyncCallback is not success."); return status; } - return kvStoreProxy_->Sync(deviceIds, mode, query.ToString()); + return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), label); } Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 2558be285..c0c729458 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -84,8 +84,8 @@ public: const std::vector &remoteSupportLabels) const override; Status GetSecurityLevel(SecurityLevel &securityLevel) const override; - Status SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query, std::shared_ptr syncCallback) override; + Status SyncWithCondition(const std::vector &deviceIds, SyncMode mode, const DataQuery &query, + std::shared_ptr syncCallback = nullptr) override; Status SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; Status UnSubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; diff --git a/interfaces/innerkits/distributeddata/include/single_kvstore.h b/interfaces/innerkits/distributeddata/include/single_kvstore.h index a42c16a12..a2ec86aeb 100755 --- a/interfaces/innerkits/distributeddata/include/single_kvstore.h +++ b/interfaces/innerkits/distributeddata/include/single_kvstore.h @@ -185,7 +185,7 @@ public: * Status of this Sync operation. */ KVSTORE_API virtual Status SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query, std::shared_ptr syncCallback) = 0; + const DataQuery &query, std::shared_ptr syncCallback = nullptr) = 0; /* * Subscribe store with other devices consistently Synchronize the data which is satisfied with the condition. diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 31120bf6e..4af16ad84 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -735,7 +735,8 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo return AddSync(deviceIds, mode, delayMs); } -Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query) +Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const std::string &syncLabel) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -744,27 +745,27 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddSync(deviceIds, mode, query, delayMs); + return AddSync(deviceIds, mode, query, delayMs, syncLabel); } -Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, - uint32_t delayMs) +Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs) { ZLOGD("start."); waitingSyncCount_++; + const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, uint32_t delayMs) + const std::string &query, uint32_t delayMs, const std::string &syncLabel) { ZLOGD("start."); waitingSyncCount_++; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoQuerySync, this, deviceIds, mode, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query)); + std::bind(&SingleKvStoreImpl::DoQuerySync, this, deviceIds, mode, query, std::placeholders::_1, syncLabel), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query, syncLabel)); } uint32_t SingleKvStoreImpl::GetSyncDelayTime(uint32_t allowedDelayMs) const @@ -793,7 +794,7 @@ Status SingleKvStoreImpl::RemoveAllSyncOperation() } void SingleKvStoreImpl::DoSyncComplete(const std::map &devicesSyncResult, - const std::string &query) + const std::string &query, const std::string &syncLabel) { DdsTrace trace(std::string("DdsTrace " LOG_TAG "::") + std::string(__FUNCTION__)); std::map resultMap; @@ -803,21 +804,13 @@ void SingleKvStoreImpl::DoSyncComplete(const std::mapSyncCompleted(resultMap, label); - } else { - label = "CommonSyncCallbackLabel"; - ZLOGI("label = %{public}s", label.c_str()); - syncCallback_->SyncCompleted(resultMap, label); - } + ZLOGI("syncLabel = %{public}s", syncLabel.c_str()); + syncCallback_->SyncCompleted(resultMap, syncLabel); } } Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd) + const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -856,7 +849,7 @@ Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, if (status == DistributedDB::DBStatus::BUSY) { if (syncRetries_ < KvStoreSyncManager::SYNC_RETRY_MAX_COUNT) { syncRetries_++; - auto addStatus = AddSync(deviceIds, mode, query, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS); + auto addStatus = AddSync(deviceIds, mode, query, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, syncLabel); if (addStatus == Status::SUCCESS) { return addStatus; } @@ -977,18 +970,20 @@ Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector & const std::string &query, uint32_t delayMs) { ZLOGD("start."); + const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); } Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, uint32_t delayMs) { ZLOGD("start."); + const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoUnSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); } Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &deviceIds, diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index eeb208c42..ed2dc6469 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -52,7 +52,8 @@ public: Status GetCountWithQuery(const std::string &query, int &result) override; Status CloseResultSet(sptr resultSet) override; Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) override; - Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query) override; + Status Sync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const std::string &syncLabel) override; Status RemoveDeviceData(const std::string &device) override; Status RegisterSyncCallback(sptr callback) override; Status UnRegisterSyncCallback() override; @@ -86,13 +87,13 @@ private: uint32_t GetSyncDelayTime(uint32_t allowedDelayMs) const; Status AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs); Status AddSync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, uint32_t delayMs); + const std::string &query, uint32_t delayMs, const std::string &syncLabel); Status RemoveAllSyncOperation(); void DoSyncComplete(const std::map &devicesSyncResult, - const std::string &query); + const std::string &query, const std::string &syncLabel); Status DoSync(const std::vector &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd); Status DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, - const KvStoreSyncManager::SyncEnd &syncEnd); + const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel); Status AddAutoSync(); Status DoAutoSync(const KvStoreSyncManager::SyncEnd &); Status RebuildKvStoreObserver(DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate); -- Gitee From 718c905dacec403f9b37af6611f4729094f671a0 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 12:12:22 +0800 Subject: [PATCH 11/32] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../distributeddatafwk/src/kvstore_sync_callback_client.h | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 78a71d1f1..131862b63 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -45,7 +45,6 @@ public: return pInstance_; } private: -// static const std::string CommonSyncCallbackLabel; static std::map> kvStoreSyncCallbackInfo_; std::mutex syncCallbackMutex_; static sptr pInstance_; -- Gitee From cda66829a22d15a75e308218c7e91c3744079931 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 17:42:50 +0800 Subject: [PATCH 12/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/ikvstore_single.h | 18 +++++--- .../src/ikvstore_single.cpp | 29 +++++++++---- .../src/kvstore_sync_callback_client.cpp | 9 ++-- .../src/single_kvstore_client.cpp | 12 ++++-- .../distributeddata/include/data_query.h | 2 +- .../app/src/query_helper.cpp | 6 +-- .../app/src/query_helper.h | 2 +- .../app/src/single_kvstore_impl.cpp | 43 +++++++++---------- .../app/src/single_kvstore_impl.h | 18 ++++---- 9 files changed, 81 insertions(+), 58 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h index 0ad1884f7..3853c0a45 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h @@ -75,7 +75,8 @@ public: std::function)> callback) = 0; virtual Status CloseResultSet(sptr resultSet) = 0; virtual Status GetCountWithQuery(const std::string &query, int &result) = 0; - virtual Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) = 0; + virtual Status Sync(const std::vector &deviceIds, SyncMode mode, + uint32_t allowedDelayMs, const std::string &syncLabel) = 0; virtual Status RemoveDeviceData(const std::string &device) = 0; virtual Status RegisterSyncCallback(sptr callback) = 0; virtual Status UnRegisterSyncCallback() = 0; @@ -91,8 +92,10 @@ public: virtual Status GetSecurityLevel(SecurityLevel &securityLevel) = 0; virtual Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const std::string &syncLabel) = 0; - virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query) = 0; - virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query) = 0; + virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel) = 0; + virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel) = 0; }; class SingleKvStoreStub : public IRemoteStub { @@ -179,7 +182,8 @@ public: std::function)> callback); virtual Status CloseResultSet(sptr resultSet); virtual Status GetCountWithQuery(const std::string &query, int &result); - virtual Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs); + virtual Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, + const std::string &syncLabel); virtual Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const std::string &syncLabel); virtual Status RemoveDeviceData(const std::string &device); @@ -195,8 +199,10 @@ public: virtual Status SetCapabilityRange(const std::vector &localLabels, const std::vector &remoteSupportLabels); virtual Status GetSecurityLevel(SecurityLevel &securityLevel); - virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query); - virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query); + virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel); + virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel); private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp index e7d205ed3..02bb9bc2d 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp @@ -462,7 +462,7 @@ Status SingleKvStoreProxy::CloseResultSet(sptr resultSetPtr) } Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, - uint32_t allowedDelayMs) + uint32_t allowedDelayMs, const std::string &syncLabel) { MessageParcel data; MessageParcel reply; @@ -854,7 +854,8 @@ Status SingleKvStoreProxy::GetSecurityLevel(SecurityLevel &securityLevel) return status; } -Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &deviceIds, const std::string &query) +Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -869,6 +870,10 @@ Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &de ZLOGE("write query fail"); return Status::IPC_ERROR; } + if (!data.WriteString(syncLabel)) { + ZLOGE("write query fail"); + return Status::IPC_ERROR; + } MessageParcel reply; MessageOption mo { MessageOption::TF_SYNC }; int32_t error = Remote()->SendRequest(SUBSCRIBE_WITH_QUERY, data, reply, mo); @@ -879,7 +884,8 @@ Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &de return static_cast(reply.ReadInt32()); } -Status SingleKvStoreProxy::UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query) +Status SingleKvStoreProxy::UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + const std::string &syncLabel) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -894,6 +900,10 @@ Status SingleKvStoreProxy::UnSubscribeWithQuery(const std::vector & ZLOGE("write query fail"); return Status::IPC_ERROR; } + if (!data.WriteString(syncLabel)) { + ZLOGE("write query fail"); + return Status::IPC_ERROR; + } MessageParcel reply; MessageOption mo { MessageOption::TF_SYNC }; int32_t error = Remote()->SendRequest(UNSUBSCRIBE_WITH_QUERY, data, reply, mo); @@ -1224,7 +1234,8 @@ int SingleKvStoreStub::SyncOnRemote(MessageParcel &data, MessageParcel &reply) } auto mode = static_cast(data.ReadInt32()); auto allowedDelayMs = static_cast(data.ReadInt32()); - Status status = Sync(devices, mode, allowedDelayMs); + auto syncLabel = data.ReadString(); + Status status = Sync(devices, mode, allowedDelayMs, syncLabel); if (!reply.WriteInt32(static_cast(status))) { ZLOGW("write sync status fail"); return -1; @@ -1599,8 +1610,8 @@ int SingleKvStoreStub::OnSyncRequest(MessageParcel &data, MessageParcel &reply) } auto mode = static_cast(data.ReadInt32()); auto query = data.ReadString(); - auto synclabel = data.ReadString(); - Status status = Sync(devices, mode, query, synclabel); + auto syncLabel = data.ReadString(); + Status status = Sync(devices, mode, query, syncLabel); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; @@ -1638,7 +1649,8 @@ int SingleKvStoreStub::OnSubscribeWithQueryRequest(MessageParcel &data, MessageP return 0; } auto query = data.ReadString(); - Status status = SubscribeWithQuery(devices, query); + auto syncLabel = data.ReadString(); + Status status = SubscribeWithQuery(devices, query, syncLabel); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; @@ -1658,7 +1670,8 @@ int SingleKvStoreStub::OnUnSubscribeWithQueryRequest(MessageParcel &data, Messag return 0; } auto query = data.ReadString(); - Status status = UnSubscribeWithQuery(devices, query); + auto syncLabel = data.ReadString(); + Status status = UnSubscribeWithQuery(devices, query, syncLabel); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index deeea0162..7d52f14ef 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -22,7 +22,6 @@ namespace OHOS { namespace DistributedKv { sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; -//const std::string KvStoreSyncCallbackClient::CommonSyncCallbackLabel("CommonSyncCallbackLabel"); KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; @@ -41,8 +40,8 @@ void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr lg(syncCallbackMutex_); - if(kvStoreSyncCallbackInfo_.find(label) == kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_.insert({label, kvStoreSyncCallback}); + if (kvStoreSyncCallbackInfo_.find(label) == kvStoreSyncCallbackInfo_.end()) { + kvStoreSyncCallbackInfo_.insert( {label, kvStoreSyncCallback} ); } } @@ -59,7 +58,7 @@ std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCal void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback() { std::lock_guard lg(syncCallbackMutex_); - if(kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { + if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { kvStoreSyncCallbackInfo_.erase(CommonSyncCallbackLabel); } } @@ -69,4 +68,4 @@ std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() return CommonSyncCallbackLabel; } } // namespace DistributedKv -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 90e94eb3a..03a3bfcb7 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -176,7 +176,9 @@ Status SingleKvStoreClient::Sync(const std::vector &deviceIds, Sync ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + std::string label = ipcCallback->GetCommonSyncCallbackLabel(); + return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, label); } Status SingleKvStoreClient::RemoveDeviceData(const std::string &device) @@ -487,7 +489,9 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& d ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->SubscribeWithQuery(deviceIds, query.ToString()); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + std::string label = ipcCallback->GetCommonSyncCallbackLabel(); + return kvStoreProxy_->SubscribeWithQuery(deviceIds, query.ToString(), label); } Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) @@ -500,7 +504,9 @@ Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->UnSubscribeWithQuery(deviceIds, query.ToString()); + sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + std::string label = ipcCallback->GetCommonSyncCallbackLabel(); + return kvStoreProxy_->UnSubscribeWithQuery(deviceIds, query.ToString(), label); } Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, diff --git a/interfaces/innerkits/distributeddata/include/data_query.h b/interfaces/innerkits/distributeddata/include/data_query.h index cebd86dd6..0e3a122ce 100755 --- a/interfaces/innerkits/distributeddata/include/data_query.h +++ b/interfaces/innerkits/distributeddata/include/data_query.h @@ -519,7 +519,7 @@ public: // suggested index static const std::string SUGGEST_INDEX; - //in keys + // in keys static const std::string IN_KEYS; private: std::string str_; diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index e7ba59a13..bc398b560 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -515,7 +515,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point std::set> inDbKeys; for(const std::string &inKey : inKeys) { ZLOGI("inKey=%{public}s", inKey.c_str()); - std::vector dbKey; + std::vector dbKey; dbKey.assign(inKey.begin(), inKey.end()); inDbKeys.insert(dbKey); } @@ -559,7 +559,7 @@ void QueryHelper::HandleDeviceId(const std::vector &words, int &poi } if (!hasInKeys_) { ZLOGD("DeviceId as the only inkey."); - std::set> inDbKeys; + std::set> inDbKeys; std::vector dbKey; dbKey.assign(deviceId_.begin(), deviceId_.begin()); inDbKeys.insert(dbKey); @@ -718,7 +718,7 @@ std::vector QueryHelper::GetInKeyList(const std::vector +#include "query.h" #include "types.h" namespace OHOS::DistributedKv { diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 4af16ad84..8f311e956 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -33,7 +33,6 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; -const std::string SingleKvStoreImpl::DEFAULT_QUERY("default_query"); static bool TaskIsBackground(pid_t pid) { std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup", std::ios::in); @@ -713,7 +712,7 @@ Status SingleKvStoreImpl::RemoveDeviceData(const std::string &device) return Status::ERROR; } -Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) +Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, const std::string &syncLabel) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -732,7 +731,7 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo lastSyncMode_ = mode; lastSyncDelayMs_ = delayMs; } - return AddSync(deviceIds, mode, delayMs); + return AddSync(deviceIds, mode, delayMs, syncLabel); } Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, @@ -748,14 +747,14 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo return AddSync(deviceIds, mode, query, delayMs, syncLabel); } -Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs) +Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs, + const std::string &syncLabel) { ZLOGD("start."); waitingSyncCount_++; - const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); + std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1, syncLabel), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, @@ -809,8 +808,8 @@ void SingleKvStoreImpl::DoSyncComplete(const std::map &deviceIds, SyncMode mode, - const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) +Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, + const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -859,7 +858,7 @@ Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, } Status SingleKvStoreImpl::DoSync(const std::vector &deviceIds, SyncMode mode, - const KvStoreSyncManager::SyncEnd &syncEnd) + const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -884,7 +883,7 @@ Status SingleKvStoreImpl::DoSync(const std::vector &deviceIds, Sync if (status == DistributedDB::DBStatus::BUSY) { if (syncRetries_ < KvStoreSyncManager::SYNC_RETRY_MAX_COUNT) { syncRetries_++; - auto addStatus = AddSync(deviceUuids, mode, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS); + auto addStatus = AddSync(deviceUuids, mode, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, syncLabel); if (addStatus == Status::SUCCESS) { return addStatus; } @@ -966,28 +965,26 @@ Status SingleKvStoreImpl::DoUnSubscribeWithQuery(const std::vector return ConvertDbStatus(status); } -Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs) +Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + uint32_t delayMs, const std::string &syncLabel) { ZLOGD("start."); - const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); } -Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs) +Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, + uint32_t delayMs, const std::string &syncLabel) { ZLOGD("start."); - const std::string &syncLabel = syncCallback_->CommonSyncCallbackLabel; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, std::bind(&SingleKvStoreImpl::DoUnSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, DEFAULT_QUERY, syncLabel)); + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); } Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &deviceIds, - const std::string &query) + const std::string &query, const std::string &syncLabel) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -996,11 +993,11 @@ Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &dev return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddSubscribeWithQuery(deviceIds, query, delayMs); + return AddSubscribeWithQuery(deviceIds, query, delayMs, syncLabel); } Status SingleKvStoreImpl::UnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query) + const std::string &query, const std::string &syncLabel) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -1009,7 +1006,7 @@ Status SingleKvStoreImpl::UnSubscribeWithQuery(const std::vector &d return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddUnSubscribeWithQuery(deviceIds, query, delayMs); + return AddUnSubscribeWithQuery(deviceIds, query, delayMs, syncLabel); } InnerStatus SingleKvStoreImpl::Close(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager) diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index ed2dc6469..f992cadd3 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -51,7 +51,8 @@ public: std::function)> callback) override; Status GetCountWithQuery(const std::string &query, int &result) override; Status CloseResultSet(sptr resultSet) override; - Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) override; + Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, + const std::string &syncLabel) override; Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const std::string &syncLabel) override; Status RemoveDeviceData(const std::string &device) override; @@ -85,13 +86,15 @@ protected: private: Status ConvertDbStatus(DistributedDB::DBStatus dbStatus); uint32_t GetSyncDelayTime(uint32_t allowedDelayMs) const; - Status AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs); + Status AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs, + const std::string &syncLabel); Status AddSync(const std::vector &deviceIds, SyncMode mode, const std::string &query, uint32_t delayMs, const std::string &syncLabel); Status RemoveAllSyncOperation(); void DoSyncComplete(const std::map &devicesSyncResult, const std::string &query, const std::string &syncLabel); - Status DoSync(const std::vector &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd); + Status DoSync(const std::vector &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd, + const std::string &syncLabel); Status DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel); Status AddAutoSync(); @@ -103,15 +106,15 @@ private: Status DoSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); Status AddSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs); + const std::string &query, uint32_t delayMs, const std::string &syncLabel); Status SubscribeWithQuery(const std::vector &deviceIds, - const std::string &query) override; + const std::string &query, const std::string &syncLabel) override; Status DoUnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); Status AddUnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs); + const std::string &query, uint32_t delayMs, const std::string &syncLabel); Status UnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query) override; + const std::string &query, const std::string &syncLabel) override; std::vector MapNodeIdToUuids(const std::vector &deviceIds); // kvstore options. @@ -154,7 +157,6 @@ private: KvStoreFlowCtrlManager flowCtrl_; static constexpr int BURST_CAPACITY = 1000; static constexpr int SUSTAINED_CAPACITY = 10000; - static const std::string DEFAULT_QUERY; }; } // namespace OHOS::DistributedKv #endif // SINGLE_KVSTORE_IMPL_H -- Gitee From de49b9d9b2fd710746f7a9eeaf6e6630211747c2 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 18:05:27 +0800 Subject: [PATCH 13/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/ikvstore_sync_callback.h | 1 - .../src/kvstore_sync_callback_client.cpp | 23 +++---- .../src/kvstore_sync_callback_client.h | 11 ++-- .../app/src/query_helper.cpp | 64 +++++++++---------- .../app/src/query_helper.h | 4 +- 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h index 62ab68946..ecd2cb61f 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h @@ -29,7 +29,6 @@ class IKvStoreSyncCallback : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreSyncCallback"); virtual void SyncCompleted(const std::map &results, const std::string &label) = 0; - const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; }; class KvStoreSyncCallbackStub : public IRemoteStub { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 7d52f14ef..7e87243f1 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -20,7 +20,8 @@ namespace OHOS { namespace DistributedKv { -sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); +//sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); +std::mutex KvStoreSyncCallbackClient::syncCallbackMutex_; std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; @@ -45,15 +46,15 @@ void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCallback() -{ - std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { - return kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]; - } else { - return nullptr; - } -} +//std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCallback() +//{ +// std::lock_guard lg(syncCallbackMutex_); +// if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { +// return kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]; +// } else { +// return nullptr; +// } +//} void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback() { @@ -68,4 +69,4 @@ std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() return CommonSyncCallbackLabel; } } // namespace DistributedKv -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 131862b63..2fe5354cf 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -25,8 +25,6 @@ namespace DistributedKv { class KvStoreSyncCallbackClient : public KvStoreSyncCallbackStub { public: - explicit KvStoreSyncCallbackClient(); - virtual ~KvStoreSyncCallbackClient(); void SyncCompleted(const std::map &results, const std::string &label) override; @@ -34,7 +32,7 @@ public: void AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, const std::string &label); - std::shared_ptr GetCommonSyncCallback(); +// std::shared_ptr GetCommonSyncCallback(); void DeleteCommonKvStoreSyncCallback(); @@ -42,12 +40,15 @@ public: static sptr GetInstance() { + static sptr pInstance_; return pInstance_; } private: + KvStoreSyncCallbackClient(); static std::map> kvStoreSyncCallbackInfo_; - std::mutex syncCallbackMutex_; - static sptr pInstance_; + static std::mutex syncCallbackMutex_; + const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; +// static sptr pInstance_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index bc398b560..90f83aed4 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -511,7 +511,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point return; } int elementPointer = pointer + 2; - const std::vector inKeys = GetInKeyList(words, elementPointer, end); + const std::vector inKeys = GetStringList(words, elementPointer, end); std::set> inDbKeys; for(const std::string &inKey : inKeys) { ZLOGI("inKey=%{public}s", inKey.c_str()); @@ -557,16 +557,16 @@ void QueryHelper::HandleDeviceId(const std::vector &words, int &poi } else { ZLOGD("Join deviceId with user specified prefixkey later."); } - if (!hasInKeys_) { - ZLOGD("DeviceId as the only inkey."); - std::set> inDbKeys; - std::vector dbKey; - dbKey.assign(deviceId_.begin(), deviceId_.begin()); - inDbKeys.insert(dbKey); - dbQuery.InKeys(inDbKeys); - } else { - ZLOGD("Join deviceId with user specified inkeys later."); - } +// if (!hasInKeys_) { +// ZLOGD("DeviceId as the only inkey."); +// std::set> inDbKeys; +// std::vector dbKey; +// dbKey.assign(deviceId_.begin(), deviceId_.begin()); +// inDbKeys.insert(dbKey); +// dbQuery.InKeys(inDbKeys); +// } else { +// ZLOGD("Join deviceId with user specified inkeys later."); +// } isSuccess = true; pointer += 2; // Pointer goes to next keyword } @@ -707,25 +707,25 @@ std::vector QueryHelper::GetStringList(const std::vector QueryHelper::GetInKeyList(const std::vector &words, - int &elementPointer, const int &end) { - std::vector values; - bool isEndFound = false; - while (elementPointer <= end) { - if (words.at(elementPointer) == DataQuery::END_IN) { - isEndFound = true; - break; - } - std::string inKeyStr = deviceId_ + StringToString(words.at(elementPointer)); - values.push_back(inKeyStr); - ZLOGI("value=%{public}s", inKeyStr.c_str()); - elementPointer++; - } - if (isEndFound) { - return values; - } else { - ZLOGE("GetStringList failed."); - return std::vector(); - } -} +//std::vector QueryHelper::GetInKeyList(const std::vector &words, +// int &elementPointer, const int &end) { +// std::vector values; +// bool isEndFound = false; +// while (elementPointer <= end) { +// if (words.at(elementPointer) == DataQuery::END_IN) { +// isEndFound = true; +// break; +// } +// std::string inKeyStr = deviceId_ + StringToString(words.at(elementPointer)); +// values.push_back(inKeyStr); +// ZLOGI("value=%{public}s", inKeyStr.c_str()); +// elementPointer++; +// } +// if (isEndFound) { +// return values; +// } else { +// ZLOGE("GetStringList failed."); +// return std::vector(); +// } +//} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/query_helper.h b/services/distributeddataservice/app/src/query_helper.h index ebd6473a9..c9d562d03 100755 --- a/services/distributeddataservice/app/src/query_helper.h +++ b/services/distributeddataservice/app/src/query_helper.h @@ -89,8 +89,8 @@ private: int &elementPointer, const int &end); static std::vector GetStringList(const std::vector &words, int &elementPointer, const int &end); - static std::vector GetInKeyList(const std::vector &words, - int &elementPointer, const int &end); +// static std::vector GetInKeyList(const std::vector &words, +// int &elementPointer, const int &end); }; } // namespace OHOS::DistributedKv #endif // QUERY_HELPER_H -- Gitee From f02906254e0d2ee74cd65544d00d9103f495ef94 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 18:55:05 +0800 Subject: [PATCH 14/32] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E6=8E=89=E7=9A=84=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 10 ------ .../src/kvstore_sync_callback_client.h | 2 -- .../app/src/query_helper.cpp | 32 ------------------- 3 files changed, 44 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 7e87243f1..04945a938 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -46,16 +46,6 @@ void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr KvStoreSyncCallbackClient::GetCommonSyncCallback() -//{ -// std::lock_guard lg(syncCallbackMutex_); -// if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { -// return kvStoreSyncCallbackInfo_[CommonSyncCallbackLabel]; -// } else { -// return nullptr; -// } -//} - void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback() { std::lock_guard lg(syncCallbackMutex_); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 2fe5354cf..365c12b5b 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -32,8 +32,6 @@ public: void AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, const std::string &label); -// std::shared_ptr GetCommonSyncCallback(); - void DeleteCommonKvStoreSyncCallback(); std::string GetCommonSyncCallbackLabel(); diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index 90f83aed4..a296f401b 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -557,16 +557,6 @@ void QueryHelper::HandleDeviceId(const std::vector &words, int &poi } else { ZLOGD("Join deviceId with user specified prefixkey later."); } -// if (!hasInKeys_) { -// ZLOGD("DeviceId as the only inkey."); -// std::set> inDbKeys; -// std::vector dbKey; -// dbKey.assign(deviceId_.begin(), deviceId_.begin()); -// inDbKeys.insert(dbKey); -// dbQuery.InKeys(inDbKeys); -// } else { -// ZLOGD("Join deviceId with user specified inkeys later."); -// } isSuccess = true; pointer += 2; // Pointer goes to next keyword } @@ -706,26 +696,4 @@ std::vector QueryHelper::GetStringList(const std::vector(); } } - -//std::vector QueryHelper::GetInKeyList(const std::vector &words, -// int &elementPointer, const int &end) { -// std::vector values; -// bool isEndFound = false; -// while (elementPointer <= end) { -// if (words.at(elementPointer) == DataQuery::END_IN) { -// isEndFound = true; -// break; -// } -// std::string inKeyStr = deviceId_ + StringToString(words.at(elementPointer)); -// values.push_back(inKeyStr); -// ZLOGI("value=%{public}s", inKeyStr.c_str()); -// elementPointer++; -// } -// if (isEndFound) { -// return values; -// } else { -// ZLOGE("GetStringList failed."); -// return std::vector(); -// } -//} } // namespace OHOS::DistributedKv -- Gitee From be66aaa6c37ab25525973841be6ef40a76a66250 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 12 Feb 2022 19:48:35 +0800 Subject: [PATCH 15/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 22 +++++++------- .../src/kvstore_sync_callback_client.h | 14 ++++----- .../src/single_kvstore_client.cpp | 29 +++++++------------ .../src/single_kvstore_client.h | 1 + 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 04945a938..21572348e 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -24,33 +24,31 @@ namespace DistributedKv { std::mutex KvStoreSyncCallbackClient::syncCallbackMutex_; std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; -KvStoreSyncCallbackClient::KvStoreSyncCallbackClient() = default; - KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; -void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &label) +void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &syncLabel) { std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(label) != kvStoreSyncCallbackInfo_.end()) { - ZLOGI("label = %{public}s", label.c_str()); - kvStoreSyncCallbackInfo_[label]->SyncCompleted(results); + if (kvStoreSyncCallbackInfo_.find(syncLabel) != kvStoreSyncCallbackInfo_.end()) { + ZLOGI("label = %{public}s", syncLabel.c_str()); + kvStoreSyncCallbackInfo_[syncLabel]->SyncCompleted(results); } } void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, - const std::string &label) + const std::string &syncLabel) { std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(label) == kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_.insert( {label, kvStoreSyncCallback} ); + if (kvStoreSyncCallbackInfo_.find(syncLabel) == kvStoreSyncCallbackInfo_.end()) { + kvStoreSyncCallbackInfo_.insert( {syncLabel, kvStoreSyncCallback} ); } } -void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback() +void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback(const std::string &syncLabel) { std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(CommonSyncCallbackLabel) != kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_.erase(CommonSyncCallbackLabel); + if (kvStoreSyncCallbackInfo_.find(syncLabel) != kvStoreSyncCallbackInfo_.end()) { + kvStoreSyncCallbackInfo_.erase(syncLabel); } } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 365c12b5b..bd4ab686a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -27,26 +27,24 @@ class KvStoreSyncCallbackClient : public KvStoreSyncCallbackStub { public: virtual ~KvStoreSyncCallbackClient(); - void SyncCompleted(const std::map &results, const std::string &label) override; + void SyncCompleted(const std::map &results, const std::string &syncLabel) override; void AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, - const std::string &label); + const std::string &syncLabel); - void DeleteCommonKvStoreSyncCallback(); + void DeleteCommonKvStoreSyncCallback(const std::string &syncLabel); std::string GetCommonSyncCallbackLabel(); - static sptr GetInstance() + static KvStoreSyncCallbackClient& GetInstance() { - static sptr pInstance_; + static KvStoreSyncCallbackClient pInstance_; return pInstance_; } private: - KvStoreSyncCallbackClient(); + KvStoreSyncCallbackClient() = default; static std::map> kvStoreSyncCallbackInfo_; static std::mutex syncCallbackMutex_; - const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; -// static sptr pInstance_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 03a3bfcb7..be355e3d4 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -176,9 +176,7 @@ Status SingleKvStoreClient::Sync(const std::vector &deviceIds, Sync ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - std::string label = ipcCallback->GetCommonSyncCallbackLabel(); - return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, label); + return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, CommonSyncCallbackLabel); } Status SingleKvStoreClient::RemoveDeviceData(const std::string &device) @@ -306,12 +304,9 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - const std::string &label = ipcCallback->GetCommonSyncCallbackLabel(); - ZLOGI("label = %{public}s", label.c_str()); - ipcCallback->AddKvStoreSyncCallback(callback, label); - return kvStoreProxy_->RegisterSyncCallback(ipcCallback); + const KvStoreSyncCallbackClient &ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + ipcCallback.AddKvStoreSyncCallback(callback, CommonSyncCallbackLabel); + return kvStoreProxy_->RegisterSyncCallback(&ipcCallback); } Status SingleKvStoreClient::UnRegisterSyncCallback() @@ -462,16 +457,16 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); + const KvStoreSyncCallbackClient &ipcCallback = KvStoreSyncCallbackClient::GetInstance(); std::string label; if (syncCallback != nullptr) { label = query.ToString(); ZLOGI("label = %{public}s", label.c_str()); - ipcCallback->AddKvStoreSyncCallback(syncCallback, label); + ipcCallback.AddKvStoreSyncCallback(syncCallback, label); } else { - label = ipcCallback->GetCommonSyncCallbackLabel(); + label = CommonSyncCallbackLabel; } - auto status = kvStoreProxy_->RegisterSyncCallback(ipcCallback); + auto status = kvStoreProxy_->RegisterSyncCallback(&ipcCallback); if (status != Status::SUCCESS) { ZLOGE("RegisterSyncCallback is not success."); return status; @@ -489,9 +484,7 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& d ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - std::string label = ipcCallback->GetCommonSyncCallbackLabel(); - return kvStoreProxy_->SubscribeWithQuery(deviceIds, query.ToString(), label); + return kvStoreProxy_->SubscribeWithQuery(deviceIds, query.ToString(), CommonSyncCallbackLabel); } Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) @@ -504,9 +497,7 @@ Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - std::string label = ipcCallback->GetCommonSyncCallbackLabel(); - return kvStoreProxy_->UnSubscribeWithQuery(deviceIds, query.ToString(), label); + return kvStoreProxy_->UnSubscribeWithQuery(deviceIds, query.ToString(), CommonSyncCallbackLabel); } Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index c0c729458..5170f04ef 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -97,6 +97,7 @@ protected: Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam) override; private: + const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; sptr kvStoreProxy_; std::map> registeredObservers_; std::mutex observerMapMutex_; -- Gitee From 76923a65ab4d55b12703851617346a73eae36ebe Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sun, 13 Feb 2022 01:44:24 +0800 Subject: [PATCH 16/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/ikvstore_single.h | 36 +++++----- .../include/ikvstore_sync_callback.h | 4 +- .../distributeddatafwk/src/data_query.cpp | 6 ++ .../src/ikvstore_single.cpp | 42 +++++------ .../src/ikvstore_sync_callback.cpp | 12 ++-- .../src/kvstore_sync_callback_client.cpp | 37 ++++------ .../src/kvstore_sync_callback_client.h | 22 ++---- .../src/single_kvstore_client.cpp | 70 ++++++++++++------- .../src/single_kvstore_client.h | 14 +++- .../distributeddatafwk/src/sync_observer.cpp | 28 ++++++++ .../distributeddatafwk/src/sync_observer.h | 31 ++++++++ .../unittest/single_kvstore_client_test.cpp | 4 +- interfaces/innerkits/distributeddata/BUILD.gn | 1 + .../distributeddata/include/data_query.h | 2 + .../distributeddata/include/single_kvstore.h | 7 +- .../app/src/single_kvstore_impl.cpp | 66 ++++++++--------- .../app/src/single_kvstore_impl.h | 34 ++++----- 17 files changed, 243 insertions(+), 173 deletions(-) create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h index 3853c0a45..ecb004a6b 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h @@ -57,8 +57,8 @@ public: SETCAPABILITYRANGE, SETSECURITLEVEL, SYNC_WITH_CONDITION, - SUBSCRIBE_WITH_QUERY, - UNSUBSCRIBE_WITH_QUERY, + SUBSCRIBE, + UNSUBSCRIBE, SINGLE_CMD_LAST, }; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.ISingleKvStore") @@ -76,7 +76,7 @@ public: virtual Status CloseResultSet(sptr resultSet) = 0; virtual Status GetCountWithQuery(const std::string &query, int &result) = 0; virtual Status Sync(const std::vector &deviceIds, SyncMode mode, - uint32_t allowedDelayMs, const std::string &syncLabel) = 0; + uint32_t allowedDelayMs, uint64_t sequenceId) = 0; virtual Status RemoveDeviceData(const std::string &device) = 0; virtual Status RegisterSyncCallback(sptr callback) = 0; virtual Status UnRegisterSyncCallback() = 0; @@ -91,11 +91,11 @@ public: const std::vector &remoteSupportLabels) = 0; virtual Status GetSecurityLevel(SecurityLevel &securityLevel) = 0; virtual Status Sync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const std::string &syncLabel) = 0; - virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel) = 0; - virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel) = 0; + const std::string &query, uint64_t sequenceId) = 0; + virtual Status Subscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId) = 0; + virtual Status UnSubscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId) = 0; }; class SingleKvStoreStub : public IRemoteStub { @@ -129,8 +129,8 @@ private: int OnSecurityLevelRequest(MessageParcel &data, MessageParcel &reply); int OnSyncRequest(MessageParcel &data, MessageParcel &reply); - int OnSubscribeWithQueryRequest(MessageParcel &data, MessageParcel &reply); - int OnUnSubscribeWithQueryRequest(MessageParcel &data, MessageParcel &reply); + int OnSubscribeRequest(MessageParcel &data, MessageParcel &reply); + int OnUnSubscribeRequest(MessageParcel &data, MessageParcel &reply); int WriteEntriesParcelable(MessageParcel &reply, Status status, std::vector entries, int bufferSize); int GetTotalEntriesSize(std::vector entries); @@ -161,8 +161,8 @@ private: [SETCAPABILITYRANGE] = &SingleKvStoreStub::OnCapabilityRangeRequest, [SETSECURITLEVEL] = &SingleKvStoreStub::OnSecurityLevelRequest, [SYNC_WITH_CONDITION] = &SingleKvStoreStub::OnSyncRequest, - [SUBSCRIBE_WITH_QUERY] = &SingleKvStoreStub::OnSubscribeWithQueryRequest, - [UNSUBSCRIBE_WITH_QUERY] = &SingleKvStoreStub::OnUnSubscribeWithQueryRequest, + [SUBSCRIBE] = &SingleKvStoreStub::OnSubscribeRequest, + [UNSUBSCRIBE] = &SingleKvStoreStub::OnUnSubscribeRequest, }; }; @@ -183,9 +183,9 @@ public: virtual Status CloseResultSet(sptr resultSet); virtual Status GetCountWithQuery(const std::string &query, int &result); virtual Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, - const std::string &syncLabel); + uint64_t sequenceId); virtual Status Sync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const std::string &syncLabel); + const std::string &query, uint64_t sequenceId); virtual Status RemoveDeviceData(const std::string &device); virtual Status RegisterSyncCallback(sptr callback); virtual Status UnRegisterSyncCallback(); @@ -199,10 +199,10 @@ public: virtual Status SetCapabilityRange(const std::vector &localLabels, const std::vector &remoteSupportLabels); virtual Status GetSecurityLevel(SecurityLevel &securityLevel); - virtual Status SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel); - virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel); + virtual Status Subscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId); + virtual Status UnSubscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId); private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h index ecd2cb61f..343760a8c 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_sync_callback.h @@ -28,7 +28,7 @@ namespace DistributedKv { class IKvStoreSyncCallback : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreSyncCallback"); - virtual void SyncCompleted(const std::map &results, const std::string &label) = 0; + virtual void SyncCompleted(const std::map &results, uint64_t sequenceId) = 0; }; class KvStoreSyncCallbackStub : public IRemoteStub { @@ -41,7 +41,7 @@ class KvStoreSyncCallbackProxy : public IRemoteProxy { public: explicit KvStoreSyncCallbackProxy(const sptr &impl); ~KvStoreSyncCallbackProxy() = default; - void SyncCompleted(const std::map &results, const std::string &label) override; + void SyncCompleted(const std::map &results, uint64_t sequenceId) override; private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp index 58ee95f4c..c8fd4ea18 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp @@ -65,6 +65,7 @@ DataQuery::DataQuery() DataQuery& DataQuery::Reset() { str_ = ""; + inkeysFlag_ = false; return *this; } @@ -544,6 +545,11 @@ DataQuery& DataQuery::InKeys(const std::vector &keys) ZLOGE("Invalid number param"); return *this; } + if (inkeysFlag_) { + ZLOGE("cannot set inkeys more than once"); + return *this; + } + inkeysFlag_ = true; str_.append(SPACE); str_.append(IN_KEYS); str_.append(SPACE); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp index 02bb9bc2d..b1cace25c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp @@ -462,7 +462,7 @@ Status SingleKvStoreProxy::CloseResultSet(sptr resultSetPtr) } Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, - uint32_t allowedDelayMs, const std::string &syncLabel) + uint32_t allowedDelayMs, uint64_t sequenceId) { MessageParcel data; MessageParcel reply; @@ -489,7 +489,7 @@ Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncM } Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const std::string &syncLabel) + const std::string &query, uint64_t sequenceId) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -506,7 +506,7 @@ Status SingleKvStoreProxy::Sync(const std::vector &deviceIds, SyncM ZLOGE("write query fail"); return Status::IPC_ERROR; } - if (!data.WriteString(syncLabel)) { + if (!data.WriteUint64(sequenceId)) { ZLOGE("write label fail"); return Status::IPC_ERROR; } @@ -854,8 +854,8 @@ Status SingleKvStoreProxy::GetSecurityLevel(SecurityLevel &securityLevel) return status; } -Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel) +Status SingleKvStoreProxy::Subscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -870,13 +870,13 @@ Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &de ZLOGE("write query fail"); return Status::IPC_ERROR; } - if (!data.WriteString(syncLabel)) { + if (!data.WriteUint64(sequenceId)) { ZLOGE("write query fail"); return Status::IPC_ERROR; } MessageParcel reply; MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(SUBSCRIBE_WITH_QUERY, data, reply, mo); + int32_t error = Remote()->SendRequest(SUBSCRIBE, data, reply, mo); if (error != 0) { ZLOGE("SendRequest returned %d", error); return Status::IPC_ERROR; @@ -884,8 +884,8 @@ Status SingleKvStoreProxy::SubscribeWithQuery(const std::vector &de return static_cast(reply.ReadInt32()); } -Status SingleKvStoreProxy::UnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - const std::string &syncLabel) +Status SingleKvStoreProxy::UnSubscribe(const std::vector &deviceIds, const std::string &query, + uint64_t sequenceId) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -900,13 +900,13 @@ Status SingleKvStoreProxy::UnSubscribeWithQuery(const std::vector & ZLOGE("write query fail"); return Status::IPC_ERROR; } - if (!data.WriteString(syncLabel)) { + if (!data.WriteUint64(sequenceId)) { ZLOGE("write query fail"); return Status::IPC_ERROR; } MessageParcel reply; MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(UNSUBSCRIBE_WITH_QUERY, data, reply, mo); + int32_t error = Remote()->SendRequest(UNSUBSCRIBE, data, reply, mo); if (error != 0) { ZLOGE("SendRequest returned %d", error); return Status::IPC_ERROR; @@ -1234,8 +1234,8 @@ int SingleKvStoreStub::SyncOnRemote(MessageParcel &data, MessageParcel &reply) } auto mode = static_cast(data.ReadInt32()); auto allowedDelayMs = static_cast(data.ReadInt32()); - auto syncLabel = data.ReadString(); - Status status = Sync(devices, mode, allowedDelayMs, syncLabel); + auto sequenceId = data.ReadUint64(); + Status status = Sync(devices, mode, allowedDelayMs, sequenceId); if (!reply.WriteInt32(static_cast(status))) { ZLOGW("write sync status fail"); return -1; @@ -1610,8 +1610,8 @@ int SingleKvStoreStub::OnSyncRequest(MessageParcel &data, MessageParcel &reply) } auto mode = static_cast(data.ReadInt32()); auto query = data.ReadString(); - auto syncLabel = data.ReadString(); - Status status = Sync(devices, mode, query, syncLabel); + auto sequenceId = data.ReadUint64(); + Status status = Sync(devices, mode, query, sequenceId); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; @@ -1637,7 +1637,7 @@ int SingleKvStoreStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Messa } } -int SingleKvStoreStub::OnSubscribeWithQueryRequest(MessageParcel &data, MessageParcel &reply) +int SingleKvStoreStub::OnSubscribeRequest(MessageParcel &data, MessageParcel &reply) { std::vector devices; if (!data.ReadStringVector(&devices) || devices.empty()) { @@ -1649,8 +1649,8 @@ int SingleKvStoreStub::OnSubscribeWithQueryRequest(MessageParcel &data, MessageP return 0; } auto query = data.ReadString(); - auto syncLabel = data.ReadString(); - Status status = SubscribeWithQuery(devices, query, syncLabel); + auto sequenceId = data.ReadUint64(); + Status status = Subscribe(devices, query, sequenceId); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; @@ -1658,7 +1658,7 @@ int SingleKvStoreStub::OnSubscribeWithQueryRequest(MessageParcel &data, MessageP return 0; } -int SingleKvStoreStub::OnUnSubscribeWithQueryRequest(MessageParcel &data, MessageParcel &reply) +int SingleKvStoreStub::OnUnSubscribeRequest(MessageParcel &data, MessageParcel &reply) { std::vector devices; if (!data.ReadStringVector(&devices) || devices.empty()) { @@ -1670,8 +1670,8 @@ int SingleKvStoreStub::OnUnSubscribeWithQueryRequest(MessageParcel &data, Messag return 0; } auto query = data.ReadString(); - auto syncLabel = data.ReadString(); - Status status = UnSubscribeWithQuery(devices, query, syncLabel); + auto sequenceId = data.ReadUint64(); + Status status = UnSubscribe(devices, query, sequenceId); if (!reply.WriteInt32(static_cast(status))) { ZLOGE("write sync status fail"); return -1; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp index e46175bbf..863547b13 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp @@ -34,7 +34,7 @@ KvStoreSyncCallbackProxy::KvStoreSyncCallbackProxy(const sptr &im : IRemoteProxy(impl) {} -void KvStoreSyncCallbackProxy::SyncCompleted(const std::map &results, const std::string &label) +void KvStoreSyncCallbackProxy::SyncCompleted(const std::map &results, uint64_t sequenceId) { MessageParcel data; MessageParcel reply; @@ -53,7 +53,7 @@ void KvStoreSyncCallbackProxy::SyncCompleted(const std::map return; } } - if (!data.WriteString(label)) { + if (!data.WriteUint64(sequenceId)) { ZLOGW("write label error."); return; } @@ -85,12 +85,8 @@ int32_t KvStoreSyncCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &d results.insert(std::pair(data.ReadString(), static_cast(data.ReadInt32()))); } - const std::string &label = data.ReadString(); - if (label.empty()) { - ZLOGE("get label error"); - return -1; - } - SyncCompleted(results, label); + uint64_t sequenceId = data.ReadUint64(); + SyncCompleted(results, sequenceId); return 0; } default: diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 21572348e..91ffb64a4 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -15,46 +15,35 @@ #define LOG_TAG "KvStoreSyncCallbackClient" +#include #include "log_print.h" #include "kvstore_sync_callback_client.h" namespace OHOS { namespace DistributedKv { -//sptr KvStoreSyncCallbackClient::pInstance_ = new KvStoreSyncCallbackClient(); -std::mutex KvStoreSyncCallbackClient::syncCallbackMutex_; -std::map> KvStoreSyncCallbackClient::kvStoreSyncCallbackInfo_; - KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; - -void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, const std::string &syncLabel) +void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { - std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(syncLabel) != kvStoreSyncCallbackInfo_.end()) { - ZLOGI("label = %{public}s", syncLabel.c_str()); - kvStoreSyncCallbackInfo_[syncLabel]->SyncCompleted(results); + if (SyncCallbackInfo_.find(sequenceId) != SyncCallbackInfo_.end()) { + ZLOGI("label = %{public}llu", sequenceId); + SyncCallbackInfo_[sequenceId]->SyncCompleted(results); + DeleteSyncCallback(sequenceId); } } -void KvStoreSyncCallbackClient::AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, - const std::string &syncLabel) +void KvStoreSyncCallbackClient::AddSyncCallback(const std::shared_ptr SyncCallback, + uint64_t sequenceId) { - std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(syncLabel) == kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_.insert( {syncLabel, kvStoreSyncCallback} ); + if (SyncCallbackInfo_.find(sequenceId) == SyncCallbackInfo_.end()) { + SyncCallbackInfo_.insert( {sequenceId, SyncCallback} ); } } -void KvStoreSyncCallbackClient::DeleteCommonKvStoreSyncCallback(const std::string &syncLabel) +void KvStoreSyncCallbackClient::DeleteSyncCallback(uint64_t sequenceId) { - std::lock_guard lg(syncCallbackMutex_); - if (kvStoreSyncCallbackInfo_.find(syncLabel) != kvStoreSyncCallbackInfo_.end()) { - kvStoreSyncCallbackInfo_.erase(syncLabel); + if (SyncCallbackInfo_.find(sequenceId) != SyncCallbackInfo_.end()) { + SyncCallbackInfo_.erase(sequenceId); } } - -std::string KvStoreSyncCallbackClient::GetCommonSyncCallbackLabel() -{ - return CommonSyncCallbackLabel; -} } // namespace DistributedKv } // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index bd4ab686a..25ada18f0 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -17,6 +17,7 @@ #define KVSTORE_SYNC_CALLBACK_CLIENT_H #include +#include "concurrent_map.h" #include "ikvstore_sync_callback.h" #include "kvstore_sync_callback.h" @@ -25,26 +26,17 @@ namespace DistributedKv { class KvStoreSyncCallbackClient : public KvStoreSyncCallbackStub { public: + KvStoreSyncCallbackClient() = default; virtual ~KvStoreSyncCallbackClient(); - void SyncCompleted(const std::map &results, const std::string &syncLabel) override; - - void AddKvStoreSyncCallback(const std::shared_ptr kvStoreSyncCallback, - const std::string &syncLabel); + void SyncCompleted(const std::map &results, uint64_t sequenceId) override; - void DeleteCommonKvStoreSyncCallback(const std::string &syncLabel); + void AddSyncCallback(const std::shared_ptr SyncCallback, + uint64_t sequenceId); - std::string GetCommonSyncCallbackLabel(); - - static KvStoreSyncCallbackClient& GetInstance() - { - static KvStoreSyncCallbackClient pInstance_; - return pInstance_; - } + void DeleteSyncCallback(uint64_t sequenceId); private: - KvStoreSyncCallbackClient() = default; - static std::map> kvStoreSyncCallbackInfo_; - static std::mutex syncCallbackMutex_; + std::map> SyncCallbackInfo_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index be355e3d4..9f22749b9 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -25,8 +25,9 @@ namespace OHOS::DistributedKv { SingleKvStoreClient::SingleKvStoreClient(sptr kvStoreProxy, const std::string &storeId) - : kvStoreProxy_(kvStoreProxy), storeId_(storeId) -{} + :kvStoreProxy_(kvStoreProxy), storeId_(storeId), syncObserver_(std::make_shared()) +{ +} StoreId SingleKvStoreClient::GetStoreId() const { @@ -167,7 +168,6 @@ Status SingleKvStoreClient::GetCountWithQuery(const DataQuery &query, int &resul Status SingleKvStoreClient::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), true); - if (kvStoreProxy_ == nullptr) { ZLOGE("kvstore proxy is nullptr."); return Status::SERVER_UNAVAILABLE; @@ -176,7 +176,10 @@ Status SingleKvStoreClient::Sync(const std::vector &deviceIds, Sync ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, CommonSyncCallbackLabel); + ++sequenceId_; + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + RegisterCallback(); + return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, sequenceId_); } Status SingleKvStoreClient::RemoveDeviceData(const std::string &device) @@ -304,17 +307,32 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptrRegisterSyncCallback(&ipcCallback); + syncObserver_->Add(callback); + return Status::SUCCESS; +} + +Status SingleKvStoreClient::RegisterCallback() +{ + { + std::lock_guard lg(registerCallbackMutex_); + if (isRegisterSyncCallback_) { + return Status::SUCCESS; + } + isRegisterSyncCallback_ = true; + } + auto status = kvStoreProxy_->RegisterSyncCallback(&syncCallbackClient_); + if (status != Status::SUCCESS) { + ZLOGE("RegisterSyncCallback is not success."); + return status; + } + return Status::SUCCESS; } Status SingleKvStoreClient::UnRegisterSyncCallback() { ZLOGI("begin."); - sptr ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - ipcCallback->DeleteCommonKvStoreSyncCallback(); - return kvStoreProxy_->UnRegisterSyncCallback(); + syncObserver_->Clean(); + return Status::SUCCESS; } Status SingleKvStoreClient::PutBatch(const std::vector &entries) @@ -447,7 +465,7 @@ Status SingleKvStoreClient::GetSecurityLevel(SecurityLevel &securityLevel) const } Status SingleKvStoreClient::SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query, std::shared_ptr syncCallback) + const DataQuery &query, std::shared_ptr callback) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); @@ -457,21 +475,14 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - const KvStoreSyncCallbackClient &ipcCallback = KvStoreSyncCallbackClient::GetInstance(); - std::string label; - if (syncCallback != nullptr) { - label = query.ToString(); - ZLOGI("label = %{public}s", label.c_str()); - ipcCallback.AddKvStoreSyncCallback(syncCallback, label); + ++sequenceId_; + if (callback != nullptr) { + syncCallbackClient_.AddSyncCallback(callback, sequenceId_); + RegisterCallback(); } else { - label = CommonSyncCallbackLabel; - } - auto status = kvStoreProxy_->RegisterSyncCallback(&ipcCallback); - if (status != Status::SUCCESS) { - ZLOGE("RegisterSyncCallback is not success."); - return status; + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); } - return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), label); + return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), sequenceId_); } Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) @@ -484,10 +495,13 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& d ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->SubscribeWithQuery(deviceIds, query.ToString(), CommonSyncCallbackLabel); + ++sequenceId_; + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + RegisterCallback(); + return kvStoreProxy_->Subscribe(deviceIds, query.ToString(), sequenceId_); } -Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) +Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector& deviceIds, const DataQuery &query) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); @@ -497,7 +511,9 @@ Status SingleKvStoreClient::UnSubscribeWithQuery(const std::vector& ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - return kvStoreProxy_->UnSubscribeWithQuery(deviceIds, query.ToString(), CommonSyncCallbackLabel); + ++sequenceId_; + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId_); } Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 5170f04ef..0cbdf32df 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -16,9 +16,12 @@ #ifndef DISTRIBUTEDDATAMGR2_SINGLE_KVSTORE_CLIENT_H #define DISTRIBUTEDDATAMGR2_SINGLE_KVSTORE_CLIENT_H +#include #include "data_query.h" #include "ikvstore_single.h" #include "single_kvstore.h" +#include "kvstore_sync_callback_client.h" +#include "sync_observer.h" namespace OHOS::DistributedKv { class SingleKvStoreClient : public SingleKvStore { @@ -64,6 +67,8 @@ public: Status RegisterSyncCallback(std::shared_ptr callback) override; + Status RegisterCallback(); + Status UnRegisterSyncCallback() override; Status PutBatch(const std::vector &entries) override; @@ -88,7 +93,7 @@ public: std::shared_ptr syncCallback = nullptr) override; Status SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; - Status UnSubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; + Status UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) override; Status GetKvStoreSnapshot(std::shared_ptr observer, std::shared_ptr &snapshot) const override; Status ReleaseKvStoreSnapshot(std::shared_ptr &snapshot) override; @@ -97,11 +102,16 @@ protected: Status Control(KvControlCmd cmd, const KvParam &inputParam, KvParam &outputParam) override; private: - const std::string CommonSyncCallbackLabel = "CommonSyncCallbackLabel"; sptr kvStoreProxy_; std::map> registeredObservers_; std::mutex observerMapMutex_; std::string storeId_; + KvStoreSyncCallbackClient syncCallbackClient_; + std::atomic sequenceId_; +// std::vector> syncCallbacks_; + std::shared_ptr syncObserver_; + bool isRegisterSyncCallback_ = false; + std::mutex registerCallbackMutex_; }; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR2_SINGLE_KVSTORE_CLIENT_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp new file mode 100644 index 000000000..568e9172d --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp @@ -0,0 +1,28 @@ +// +// Created by Sincer on 2022/2/12. +// + +#include "sync_observer.h" + +namespace OHOS::DistributedKv { +SyncObserver::SyncObserver(const std::vector > &callbacks) + :callbacks_(callbacks) +{}; + +bool SyncObserver::Add(const std::shared_ptr callback) { + callbacks_.push_back(callback); + return true; +} + +bool SyncObserver::Clean() { + callbacks_.clear(); + return true; +} + +void SyncObserver::SyncCompleted(const std::map &results) +{ + for (auto &callback : callbacks_) { + callback->SyncCompleted(results); + } +} +} diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h new file mode 100644 index 000000000..b7d80ad74 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h @@ -0,0 +1,31 @@ +// +// Created by Sincer on 2022/2/12. +// + +#ifndef DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H +#define DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H + +#include +#include +#include "kvstore_sync_callback.h" + +namespace OHOS::DistributedKv { +class SyncObserver : public KvStoreSyncCallback { +public: + SyncObserver(const std::vector> &callbacks); + + SyncObserver() = default; + + virtual ~SyncObserver() = default; + + bool Add(const std::shared_ptr callback); + + bool Clean(); + + void SyncCompleted(const std::map &results) override; + +private: + std::vector > callbacks_; +}; +} +#endif //DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H 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 08a1a52e4..ed1a1aadb 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -1084,7 +1084,7 @@ HWTEST_F(SingleKvStoreClientTest, SubscribeWithQuery001, TestSize.Level1) std::vector deviceIds = {"invalid_device_id1", "invalid_device_id2"}; DataQuery dataQuery; dataQuery.KeyPrefix("name"); - auto syncStatus = singleKvStorePtr->SubscribeWithQuery(deviceIds, dataQuery); + auto syncStatus = singleKvStorePtr->Subscribe(deviceIds, dataQuery); EXPECT_NE(syncStatus, Status::SUCCESS) << "sync device should not return success"; } @@ -1101,7 +1101,7 @@ HWTEST_F(SingleKvStoreClientTest, UnSubscribeWithQuery001, TestSize.Level1) std::vector deviceIds = {"invalid_device_id1", "invalid_device_id2"}; DataQuery dataQuery; dataQuery.KeyPrefix("name"); - auto unSubscribeStatus = singleKvStorePtr->UnSubscribeWithQuery(deviceIds, dataQuery); + auto unSubscribeStatus = singleKvStorePtr->UnSubscribe(deviceIds, dataQuery); EXPECT_NE(unSubscribeStatus, Status::SUCCESS) << "sync device should not return success"; } diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 9e3943606..c29c0717c 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -72,6 +72,7 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp", "include/types.h", ] diff --git a/interfaces/innerkits/distributeddata/include/data_query.h b/interfaces/innerkits/distributeddata/include/data_query.h index 0e3a122ce..f99592bde 100755 --- a/interfaces/innerkits/distributeddata/include/data_query.h +++ b/interfaces/innerkits/distributeddata/include/data_query.h @@ -524,6 +524,8 @@ public: private: std::string str_; + bool inkeysFlag_; + template void AppendCommon(const std::string &keyword, const std::string &fieldType, std::string &field, const T &value); diff --git a/interfaces/innerkits/distributeddata/include/single_kvstore.h b/interfaces/innerkits/distributeddata/include/single_kvstore.h index a2ec86aeb..f0db828c0 100755 --- a/interfaces/innerkits/distributeddata/include/single_kvstore.h +++ b/interfaces/innerkits/distributeddata/include/single_kvstore.h @@ -195,8 +195,7 @@ public: * Return: * Status of this Subscribe operation. */ - KVSTORE_API virtual Status SubscribeWithQuery(const std::vector &deviceIds, - const DataQuery &query) = 0; + KVSTORE_API virtual Status SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) = 0; /* * UnSubscribe store with other devices which is satisfied with the condition. @@ -206,8 +205,8 @@ public: * Return: * Status of this UnSubscribe operation. */ - KVSTORE_API virtual Status UnSubscribeWithQuery(const std::vector &deviceIds, - const DataQuery &query) = 0; + KVSTORE_API virtual Status UnsubscribeWithQuery(const std::vector &deviceIds, + const DataQuery &query) = 0; protected: // control this store. diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 8f311e956..e094abb02 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -712,7 +712,7 @@ Status SingleKvStoreImpl::RemoveDeviceData(const std::string &device) return Status::ERROR; } -Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, const std::string &syncLabel) +Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -731,11 +731,11 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo lastSyncMode_ = mode; lastSyncDelayMs_ = delayMs; } - return AddSync(deviceIds, mode, delayMs, syncLabel); + return AddSync(deviceIds, mode, delayMs, sequenceId); } Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const std::string &syncLabel) + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -744,27 +744,27 @@ Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMo return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddSync(deviceIds, mode, query, delayMs, syncLabel); + return AddSync(deviceIds, mode, query, delayMs, sequenceId); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs, - const std::string &syncLabel) + uint64_t sequenceId) { ZLOGD("start."); waitingSyncCount_++; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1, syncLabel), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); + std::bind(&SingleKvStoreImpl::DoSync, this, deviceIds, mode, std::placeholders::_1, sequenceId), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", sequenceId)); } Status SingleKvStoreImpl::AddSync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, uint32_t delayMs, const std::string &syncLabel) + const std::string &query, uint32_t delayMs, uint64_t sequenceId) { ZLOGD("start."); waitingSyncCount_++; return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoQuerySync, this, deviceIds, mode, query, std::placeholders::_1, syncLabel), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query, syncLabel)); + std::bind(&SingleKvStoreImpl::DoQuerySync, this, deviceIds, mode, query, std::placeholders::_1, sequenceId), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, query, sequenceId)); } uint32_t SingleKvStoreImpl::GetSyncDelayTime(uint32_t allowedDelayMs) const @@ -793,7 +793,7 @@ Status SingleKvStoreImpl::RemoveAllSyncOperation() } void SingleKvStoreImpl::DoSyncComplete(const std::map &devicesSyncResult, - const std::string &query, const std::string &syncLabel) + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string("DdsTrace " LOG_TAG "::") + std::string(__FUNCTION__)); std::map resultMap; @@ -803,13 +803,13 @@ void SingleKvStoreImpl::DoSyncComplete(const std::mapSyncCompleted(resultMap, syncLabel); + ZLOGI("sequenceId = %{public}llu", sequenceId); + syncCallback_->SyncCompleted(resultMap, sequenceId); } } Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, - const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) + const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -848,7 +848,7 @@ Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, if (status == DistributedDB::DBStatus::BUSY) { if (syncRetries_ < KvStoreSyncManager::SYNC_RETRY_MAX_COUNT) { syncRetries_++; - auto addStatus = AddSync(deviceIds, mode, query, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, syncLabel); + auto addStatus = AddSync(deviceIds, mode, query, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, sequenceId); if (addStatus == Status::SUCCESS) { return addStatus; } @@ -858,7 +858,7 @@ Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, } Status SingleKvStoreImpl::DoSync(const std::vector &deviceIds, SyncMode mode, - const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel) + const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -883,7 +883,7 @@ Status SingleKvStoreImpl::DoSync(const std::vector &deviceIds, Sync if (status == DistributedDB::DBStatus::BUSY) { if (syncRetries_ < KvStoreSyncManager::SYNC_RETRY_MAX_COUNT) { syncRetries_++; - auto addStatus = AddSync(deviceUuids, mode, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, syncLabel); + auto addStatus = AddSync(deviceUuids, mode, KvStoreSyncManager::SYNC_DEFAULT_DELAY_MS, sequenceId); if (addStatus == Status::SUCCESS) { return addStatus; } @@ -904,7 +904,7 @@ std::vector SingleKvStoreImpl::MapNodeIdToUuids(const std::vector &deviceIds, +Status SingleKvStoreImpl::DoSubscribe(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd) { ZLOGD("start."); @@ -935,7 +935,7 @@ Status SingleKvStoreImpl::DoSubscribeWithQuery(const std::vector &d return ConvertDbStatus(status); } -Status SingleKvStoreImpl::DoUnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, +Status SingleKvStoreImpl::DoUnSubscribe(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd) { ZLOGD("start."); @@ -965,26 +965,26 @@ Status SingleKvStoreImpl::DoUnSubscribeWithQuery(const std::vector return ConvertDbStatus(status); } -Status SingleKvStoreImpl::AddSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - uint32_t delayMs, const std::string &syncLabel) +Status SingleKvStoreImpl::AddSubscribe(const std::vector &deviceIds, const std::string &query, + uint32_t delayMs, uint64_t sequenceId) { ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); + std::bind(&SingleKvStoreImpl::DoSubscribe, this, deviceIds, query, std::placeholders::_1), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", sequenceId)); } -Status SingleKvStoreImpl::AddUnSubscribeWithQuery(const std::vector &deviceIds, const std::string &query, - uint32_t delayMs, const std::string &syncLabel) +Status SingleKvStoreImpl::AddUnSubscribe(const std::vector &deviceIds, const std::string &query, + uint32_t delayMs, uint64_t sequenceId) { ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, - std::bind(&SingleKvStoreImpl::DoUnSubscribeWithQuery, this, deviceIds, query, std::placeholders::_1), - std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", syncLabel)); + std::bind(&SingleKvStoreImpl::DoUnSubscribe, this, deviceIds, query, std::placeholders::_1), + std::bind(&SingleKvStoreImpl::DoSyncComplete, this, std::placeholders::_1, "", sequenceId)); } -Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, const std::string &syncLabel) +Status SingleKvStoreImpl::Subscribe(const std::vector &deviceIds, + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -993,11 +993,11 @@ Status SingleKvStoreImpl::SubscribeWithQuery(const std::vector &dev return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddSubscribeWithQuery(deviceIds, query, delayMs, syncLabel); + return AddSubscribe(deviceIds, query, delayMs, sequenceId); } -Status SingleKvStoreImpl::UnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, const std::string &syncLabel) +Status SingleKvStoreImpl::UnSubscribe(const std::vector &deviceIds, + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -1006,7 +1006,7 @@ Status SingleKvStoreImpl::UnSubscribeWithQuery(const std::vector &d return Status::EXCEED_MAX_ACCESS_RATE; } uint32_t delayMs = GetSyncDelayTime(0); - return AddUnSubscribeWithQuery(deviceIds, query, delayMs, syncLabel); + return AddUnSubscribe(deviceIds, query, delayMs, sequenceId); } InnerStatus SingleKvStoreImpl::Close(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager) diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index f992cadd3..467835d75 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -52,9 +52,9 @@ public: Status GetCountWithQuery(const std::string &query, int &result) override; Status CloseResultSet(sptr resultSet) override; Status Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, - const std::string &syncLabel) override; + uint64_t sequenceId) override; Status Sync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, const std::string &syncLabel) override; + const std::string &query, uint64_t sequenceId) override; Status RemoveDeviceData(const std::string &device) override; Status RegisterSyncCallback(sptr callback) override; Status UnRegisterSyncCallback() override; @@ -87,34 +87,34 @@ private: Status ConvertDbStatus(DistributedDB::DBStatus dbStatus); uint32_t GetSyncDelayTime(uint32_t allowedDelayMs) const; Status AddSync(const std::vector &deviceIds, SyncMode mode, uint32_t delayMs, - const std::string &syncLabel); + uint64_t sequenceId); Status AddSync(const std::vector &deviceIds, SyncMode mode, - const std::string &query, uint32_t delayMs, const std::string &syncLabel); + const std::string &query, uint32_t delayMs, uint64_t sequenceId); Status RemoveAllSyncOperation(); void DoSyncComplete(const std::map &devicesSyncResult, - const std::string &query, const std::string &syncLabel); + const std::string &query, uint64_t sequenceId); Status DoSync(const std::vector &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd, - const std::string &syncLabel); + uint64_t sequenceId); Status DoQuerySync(const std::vector &deviceIds, SyncMode mode, const std::string &query, - const KvStoreSyncManager::SyncEnd &syncEnd, const std::string &syncLabel); + const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId); Status AddAutoSync(); Status DoAutoSync(const KvStoreSyncManager::SyncEnd &); Status RebuildKvStoreObserver(DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate); Status RebuildKvStoreResultSet(); int ConvertToDbObserverMode(SubscribeType subscribeType) const; DistributedDB::SyncMode ConvertToDbSyncMode(SyncMode syncMode) const; - Status DoSubscribeWithQuery(const std::vector &deviceIds, + Status DoSubscribe(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); - Status AddSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs, const std::string &syncLabel); - Status SubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, const std::string &syncLabel) override; - Status DoUnSubscribeWithQuery(const std::vector &deviceIds, + Status AddSubscribe(const std::vector &deviceIds, + const std::string &query, uint32_t delayMs, uint64_t sequenceId); + Status Subscribe(const std::vector &deviceIds, + const std::string &query, uint64_t sequenceId) override; + Status DoUnSubscribe(const std::vector &deviceIds, const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); - Status AddUnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs, const std::string &syncLabel); - Status UnSubscribeWithQuery(const std::vector &deviceIds, - const std::string &query, const std::string &syncLabel) override; + Status AddUnSubscribe(const std::vector &deviceIds, + const std::string &query, uint32_t delayMs, uint64_t sequenceId); + Status UnSubscribe(const std::vector &deviceIds, + const std::string &query, uint64_t sequenceId) override; std::vector MapNodeIdToUuids(const std::vector &deviceIds); // kvstore options. -- Gitee From 1e121c2041ce617f63b4cb397787a826eba51f33 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 10:11:58 +0800 Subject: [PATCH 17/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9inkeys=20and=20synccall?= =?UTF-8?q?back?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../distributeddatafwk/include/ikvstore_single.h | 4 ++-- .../src/kvstore_sync_callback_client.cpp | 10 +++++----- .../src/kvstore_sync_callback_client.h | 2 +- .../distributeddatafwk/src/single_kvstore_client.h | 1 - .../test/unittest/single_kvstore_client_test.cpp | 4 ++-- .../distributeddataservice/app/src/query_helper.cpp | 5 +++-- services/distributeddataservice/app/src/query_helper.h | 2 -- .../app/src/single_kvstore_impl.cpp | 6 +++--- 8 files changed, 16 insertions(+), 18 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h index ecb004a6b..b7ec4012b 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h @@ -200,9 +200,9 @@ public: const std::vector &remoteSupportLabels); virtual Status GetSecurityLevel(SecurityLevel &securityLevel); virtual Status Subscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId); + uint64_t sequenceId); virtual Status UnSubscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId); + uint64_t sequenceId); private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 91ffb64a4..b10d2ea52 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -24,7 +24,7 @@ namespace DistributedKv { KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { - if (SyncCallbackInfo_.find(sequenceId) != SyncCallbackInfo_.end()) { + if (SyncCallbackInfo_.Contains(sequenceId)) { ZLOGI("label = %{public}llu", sequenceId); SyncCallbackInfo_[sequenceId]->SyncCompleted(results); DeleteSyncCallback(sequenceId); @@ -34,15 +34,15 @@ void KvStoreSyncCallbackClient::SyncCompleted(const std::map SyncCallback, uint64_t sequenceId) { - if (SyncCallbackInfo_.find(sequenceId) == SyncCallbackInfo_.end()) { - SyncCallbackInfo_.insert( {sequenceId, SyncCallback} ); + if (!SyncCallbackInfo_.Contains(sequenceId)) { + SyncCallbackInfo_.Insert(sequenceId, SyncCallback); } } void KvStoreSyncCallbackClient::DeleteSyncCallback(uint64_t sequenceId) { - if (SyncCallbackInfo_.find(sequenceId) != SyncCallbackInfo_.end()) { - SyncCallbackInfo_.erase(sequenceId); + if (SyncCallbackInfo_.Contains(sequenceId)) { + SyncCallbackInfo_.Erase(sequenceId); } } } // namespace DistributedKv diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index 25ada18f0..d68c8357d 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -36,7 +36,7 @@ public: void DeleteSyncCallback(uint64_t sequenceId); private: - std::map> SyncCallbackInfo_; + ConcurrentMap> SyncCallbackInfo_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 0cbdf32df..b8ed94d6e 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -108,7 +108,6 @@ private: std::string storeId_; KvStoreSyncCallbackClient syncCallbackClient_; std::atomic sequenceId_; -// std::vector> syncCallbacks_; std::shared_ptr syncObserver_; bool isRegisterSyncCallback_ = false; std::mutex registerCallbackMutex_; 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 ed1a1aadb..08a1a52e4 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -1084,7 +1084,7 @@ HWTEST_F(SingleKvStoreClientTest, SubscribeWithQuery001, TestSize.Level1) std::vector deviceIds = {"invalid_device_id1", "invalid_device_id2"}; DataQuery dataQuery; dataQuery.KeyPrefix("name"); - auto syncStatus = singleKvStorePtr->Subscribe(deviceIds, dataQuery); + auto syncStatus = singleKvStorePtr->SubscribeWithQuery(deviceIds, dataQuery); EXPECT_NE(syncStatus, Status::SUCCESS) << "sync device should not return success"; } @@ -1101,7 +1101,7 @@ HWTEST_F(SingleKvStoreClientTest, UnSubscribeWithQuery001, TestSize.Level1) std::vector deviceIds = {"invalid_device_id1", "invalid_device_id2"}; DataQuery dataQuery; dataQuery.KeyPrefix("name"); - auto unSubscribeStatus = singleKvStorePtr->UnSubscribe(deviceIds, dataQuery); + auto unSubscribeStatus = singleKvStorePtr->UnSubscribeWithQuery(deviceIds, dataQuery); EXPECT_NE(unSubscribeStatus, Status::SUCCESS) << "sync device should not return success"; } diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index a296f401b..47cf67220 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -505,12 +505,13 @@ void QueryHelper::HandleKeyPrefix(const std::vector &words, int &po void QueryHelper::HandleInKeys(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { + // pointer points at keyword "IN_KEYS", (pointer + 1) points at keyword "START_IN" if (pointer + 2 > end || words.at(pointer + 1) != DataQuery::START_IN) { // This keyword has at least 2 params ZLOGE("In not enough params."); isSuccess = false; return; } - int elementPointer = pointer + 2; + int elementPointer = pointer + 2; // elementPointer points at the first inkey value const std::vector inKeys = GetStringList(words, elementPointer, end); std::set> inDbKeys; for(const std::string &inKey : inKeys) { @@ -523,7 +524,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point ZLOGI("size of inKeys=%{public}d", size); dbQuery.InKeys(inDbKeys); isSuccess = true; - pointer = elementPointer + 1; // Pointer goes to next keyword + pointer = elementPointer + 1; // elementPointer points at keyword "END", Pointer goes to next keyword } void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, diff --git a/services/distributeddataservice/app/src/query_helper.h b/services/distributeddataservice/app/src/query_helper.h index c9d562d03..c87bea95a 100755 --- a/services/distributeddataservice/app/src/query_helper.h +++ b/services/distributeddataservice/app/src/query_helper.h @@ -89,8 +89,6 @@ private: int &elementPointer, const int &end); static std::vector GetStringList(const std::vector &words, int &elementPointer, const int &end); -// static std::vector GetInKeyList(const std::vector &words, -// int &elementPointer, const int &end); }; } // namespace OHOS::DistributedKv #endif // QUERY_HELPER_H diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index e094abb02..7c7226052 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -966,7 +966,7 @@ Status SingleKvStoreImpl::DoUnSubscribe(const std::vector &deviceId } Status SingleKvStoreImpl::AddSubscribe(const std::vector &deviceIds, const std::string &query, - uint32_t delayMs, uint64_t sequenceId) + uint32_t delayMs, uint64_t sequenceId) { ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, @@ -975,7 +975,7 @@ Status SingleKvStoreImpl::AddSubscribe(const std::vector &deviceIds } Status SingleKvStoreImpl::AddUnSubscribe(const std::vector &deviceIds, const std::string &query, - uint32_t delayMs, uint64_t sequenceId) + uint32_t delayMs, uint64_t sequenceId) { ZLOGD("start."); return KvStoreSyncManager::GetInstance()->AddSyncOperation(reinterpret_cast(this), delayMs, @@ -997,7 +997,7 @@ Status SingleKvStoreImpl::Subscribe(const std::vector &deviceIds, } Status SingleKvStoreImpl::UnSubscribe(const std::vector &deviceIds, - const std::string &query, uint64_t sequenceId) + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); -- Gitee From 85a9c3096be7c5f81897502353d8b601cda290e0 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 10:24:47 +0800 Subject: [PATCH 18/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9inkeys=E5=92=8Csynccall?= =?UTF-8?q?back=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../distributeddatafwk/include/ikvstore_single.h | 4 ++-- .../distributeddatafwk/src/ikvstore_single.cpp | 4 ++-- .../distributeddatafwk/src/single_kvstore_client.cpp | 4 ++-- .../app/src/single_kvstore_impl.h | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h index b7ec4012b..2d333cb63 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_single.h @@ -93,9 +93,9 @@ public: virtual Status Sync(const std::vector &deviceIds, SyncMode mode, const std::string &query, uint64_t sequenceId) = 0; virtual Status Subscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId) = 0; + uint64_t sequenceId) = 0; virtual Status UnSubscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId) = 0; + uint64_t sequenceId) = 0; }; class SingleKvStoreStub : public IRemoteStub { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp index b1cace25c..5a15c0a14 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp @@ -855,7 +855,7 @@ Status SingleKvStoreProxy::GetSecurityLevel(SecurityLevel &securityLevel) } Status SingleKvStoreProxy::Subscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId) + uint64_t sequenceId) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { @@ -885,7 +885,7 @@ Status SingleKvStoreProxy::Subscribe(const std::vector &deviceIds, } Status SingleKvStoreProxy::UnSubscribe(const std::vector &deviceIds, const std::string &query, - uint64_t sequenceId) + uint64_t sequenceId) { MessageParcel data; if (!data.WriteInterfaceToken(SingleKvStoreProxy::GetDescriptor())) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index 9f22749b9..d2c79addd 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -485,7 +485,7 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), sequenceId_); } -Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& deviceIds, const DataQuery& query) +Status SingleKvStoreClient::SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); @@ -501,7 +501,7 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector& d return kvStoreProxy_->Subscribe(deviceIds, query.ToString(), sequenceId_); } -Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector& deviceIds, const DataQuery &query) +Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.h b/services/distributeddataservice/app/src/single_kvstore_impl.h index 467835d75..e1c2d5179 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.h +++ b/services/distributeddataservice/app/src/single_kvstore_impl.h @@ -104,17 +104,17 @@ private: int ConvertToDbObserverMode(SubscribeType subscribeType) const; DistributedDB::SyncMode ConvertToDbSyncMode(SyncMode syncMode) const; Status DoSubscribe(const std::vector &deviceIds, - const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); + const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); Status AddSubscribe(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs, uint64_t sequenceId); + const std::string &query, uint32_t delayMs, uint64_t sequenceId); Status Subscribe(const std::vector &deviceIds, - const std::string &query, uint64_t sequenceId) override; + const std::string &query, uint64_t sequenceId) override; Status DoUnSubscribe(const std::vector &deviceIds, - const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); + const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); Status AddUnSubscribe(const std::vector &deviceIds, - const std::string &query, uint32_t delayMs, uint64_t sequenceId); + const std::string &query, uint32_t delayMs, uint64_t sequenceId); Status UnSubscribe(const std::vector &deviceIds, - const std::string &query, uint64_t sequenceId) override; + const std::string &query, uint64_t sequenceId) override; std::vector MapNodeIdToUuids(const std::vector &deviceIds); // kvstore options. -- Gitee From 64173532e9e01b97b3ec8562b14c98a66dac902f Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 10:57:36 +0800 Subject: [PATCH 19/32] inkeys Signed-off-by: Hollokin --- .../innerkitsimpl/distributeddatafwk/src/sync_observer.cpp | 4 ++-- .../innerkitsimpl/distributeddatafwk/src/sync_observer.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp index 568e9172d..9513fe7dd 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp @@ -5,7 +5,7 @@ #include "sync_observer.h" namespace OHOS::DistributedKv { -SyncObserver::SyncObserver(const std::vector > &callbacks) +SyncObserver::SyncObserver(const std::vector> &callbacks) :callbacks_(callbacks) {}; @@ -19,7 +19,7 @@ bool SyncObserver::Clean() { return true; } -void SyncObserver::SyncCompleted(const std::map &results) +void SyncObserver::SyncCompleted(const std::map &results) { for (auto &callback : callbacks_) { callback->SyncCompleted(results); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h index b7d80ad74..c174475e6 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h @@ -22,10 +22,10 @@ public: bool Clean(); - void SyncCompleted(const std::map &results) override; + void SyncCompleted(const std::map &results) override; private: - std::vector > callbacks_; + std::vector> callbacks_; }; } #endif //DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H -- Gitee From 0232c4631e81a341d43d6f92c965f75f5c429477 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 13:44:32 +0800 Subject: [PATCH 20/32] synccallback Signed-off-by: Hollokin --- .../src/single_kvstore_client.cpp | 36 ++++++++++++------- .../src/single_kvstore_client.h | 10 +++++- .../app/src/query_helper.cpp | 6 ++-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index d2c79addd..d2d036077 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -176,10 +176,10 @@ Status SingleKvStoreClient::Sync(const std::vector &deviceIds, Sync ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - ++sequenceId_; - syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); RegisterCallback(); - return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, sequenceId_); + return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, sequenceId); } Status SingleKvStoreClient::RemoveDeviceData(const std::string &device) @@ -475,14 +475,14 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - ++sequenceId_; + uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); if (callback != nullptr) { - syncCallbackClient_.AddSyncCallback(callback, sequenceId_); + syncCallbackClient_.AddSyncCallback(callback, sequenceId); RegisterCallback(); } else { - syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); } - return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), sequenceId_); + return kvStoreProxy_->Sync(deviceIds, mode, query.ToString(), sequenceId); } Status SingleKvStoreClient::SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) @@ -495,10 +495,10 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector &d ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - ++sequenceId_; - syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); + uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); RegisterCallback(); - return kvStoreProxy_->Subscribe(deviceIds, query.ToString(), sequenceId_); + return kvStoreProxy_->Subscribe(deviceIds, query.ToString(), sequenceId); } Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) @@ -511,9 +511,9 @@ Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - ++sequenceId_; - syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId_); - return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId_); + uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); + return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId); } Status SingleKvStoreClient::GetKvStoreSnapshot(std::shared_ptr observer, @@ -531,4 +531,14 @@ Status SingleKvStoreClient::Clear() { return Status::NOT_SUPPORT; } + +std::atomic UUIDGenerate::sequenceId_ = 0; + +UUIDGenerate::UUIDGenerate() {} + +uint64_t UUIDGenerate::GenenrateSequenceId() +{ + ++sequenceId_; + return sequenceId_; +} } // namespace OHOS::DistributedKv diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index b8ed94d6e..78ad777b4 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -107,10 +107,18 @@ private: std::mutex observerMapMutex_; std::string storeId_; KvStoreSyncCallbackClient syncCallbackClient_; - std::atomic sequenceId_; +// std::atomic sequenceId_; std::shared_ptr syncObserver_; bool isRegisterSyncCallback_ = false; std::mutex registerCallbackMutex_; }; + +class UUIDGenerate{ +public: + explicit UUIDGenerate(); + static uint64_t GenenrateSequenceId(); +private: + static std::atomic sequenceId_; +}; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR2_SINGLE_KVSTORE_CLIENT_H diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index 47cf67220..fa948eea9 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -511,8 +511,8 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point isSuccess = false; return; } - int elementPointer = pointer + 2; // elementPointer points at the first inkey value - const std::vector inKeys = GetStringList(words, elementPointer, end); + int inkeyPointer = pointer + 2; // inkeyOffset points at the first inkey value + const std::vector inKeys = GetStringList(words, inkeyPointer, end); std::set> inDbKeys; for(const std::string &inKey : inKeys) { ZLOGI("inKey=%{public}s", inKey.c_str()); @@ -524,7 +524,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point ZLOGI("size of inKeys=%{public}d", size); dbQuery.InKeys(inDbKeys); isSuccess = true; - pointer = elementPointer + 1; // elementPointer points at keyword "END", Pointer goes to next keyword + pointer = inkeyPointer + 1; // inkeyOffset points at keyword "END", Pointer goes to next keyword } void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, -- Gitee From 9f91da371609dea408c6cf38a0c49f3d31d20ed7 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 13:48:40 +0800 Subject: [PATCH 21/32] synccallback Signed-off-by: Hollokin --- .../innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 78ad777b4..1ddf240a5 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -107,7 +107,6 @@ private: std::mutex observerMapMutex_; std::string storeId_; KvStoreSyncCallbackClient syncCallbackClient_; -// std::atomic sequenceId_; std::shared_ptr syncObserver_; bool isRegisterSyncCallback_ = false; std::mutex registerCallbackMutex_; -- Gitee From 54a61950a716efa65ee78e1604712e3d86139c42 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 13:51:40 +0800 Subject: [PATCH 22/32] inkeys Signed-off-by: Hollokin --- services/distributeddataservice/app/src/query_helper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index fa948eea9..7ca9afc38 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -511,7 +511,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point isSuccess = false; return; } - int inkeyPointer = pointer + 2; // inkeyOffset points at the first inkey value + int inkeyPointer = pointer + 2; // inkeyPointer points at the first inkey value const std::vector inKeys = GetStringList(words, inkeyPointer, end); std::set> inDbKeys; for(const std::string &inKey : inKeys) { @@ -524,7 +524,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point ZLOGI("size of inKeys=%{public}d", size); dbQuery.InKeys(inDbKeys); isSuccess = true; - pointer = inkeyPointer + 1; // inkeyOffset points at keyword "END", Pointer goes to next keyword + pointer = inkeyPointer + 1; // inkeyPointer points at keyword "END", Pointer goes to next keyword } void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, -- Gitee From a9662b7a6a7da83e8d3dbac6d36a960dea16be1f Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 16:13:29 +0800 Subject: [PATCH 23/32] inkeys and synccallback Signed-off-by: Hollokin --- .../src/single_kvstore_client.cpp | 32 +++++++------------ .../src/single_kvstore_client.h | 8 ----- .../distributeddatafwk/src/sync_observer.cpp | 17 ++++++++-- .../distributeddatafwk/src/sync_observer.h | 17 ++++++++-- interfaces/innerkits/distributeddata/BUILD.gn | 1 + .../adapter/include/utils/kvstore_utils.h | 4 +++ .../adapter/utils/src/kvstore_utils.cpp | 7 ++++ .../app/src/query_helper.cpp | 12 ++++--- 8 files changed, 59 insertions(+), 39 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index d2d036077..dfdb87d83 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -22,6 +22,7 @@ #include "kvstore_resultset_client.h" #include "kvstore_sync_callback_client.h" #include "log_print.h" +#include "kvstore_utils.h" namespace OHOS::DistributedKv { SingleKvStoreClient::SingleKvStoreClient(sptr kvStoreProxy, const std::string &storeId) @@ -176,7 +177,7 @@ Status SingleKvStoreClient::Sync(const std::vector &deviceIds, Sync ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + uint64_t sequenceId = KvStoreUtils::GenerateSequenceId(); syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); RegisterCallback(); return kvStoreProxy_->Sync(deviceIds, mode, allowedDelayMs, sequenceId); @@ -313,18 +314,19 @@ Status SingleKvStoreClient::RegisterSyncCallback(std::shared_ptrRegisterSyncCallback(&syncCallbackClient_); if (status != Status::SUCCESS) { ZLOGE("RegisterSyncCallback is not success."); return status; } + isRegisterSyncCallback_ = true; return Status::SUCCESS; } @@ -475,7 +477,7 @@ Status SingleKvStoreClient::SyncWithCondition(const std::vector &de ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + uint64_t sequenceId = KvStoreUtils::GenerateSequenceId(); if (callback != nullptr) { syncCallbackClient_.AddSyncCallback(callback, sequenceId); RegisterCallback(); @@ -495,7 +497,7 @@ Status SingleKvStoreClient::SubscribeWithQuery(const std::vector &d ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + uint64_t sequenceId = KvStoreUtils::GenerateSequenceId(); syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); RegisterCallback(); return kvStoreProxy_->Subscribe(deviceIds, query.ToString(), sequenceId); @@ -511,7 +513,7 @@ Status SingleKvStoreClient::UnsubscribeWithQuery(const std::vector ZLOGW("deviceIds is empty."); return Status::INVALID_ARGUMENT; } - uint64_t sequenceId = UUIDGenerate::GenenrateSequenceId(); + uint64_t sequenceId = KvStoreUtils::GenerateSequenceId(); syncCallbackClient_.AddSyncCallback(syncObserver_, sequenceId); return kvStoreProxy_->UnSubscribe(deviceIds, query.ToString(), sequenceId); } @@ -531,14 +533,4 @@ Status SingleKvStoreClient::Clear() { return Status::NOT_SUPPORT; } - -std::atomic UUIDGenerate::sequenceId_ = 0; - -UUIDGenerate::UUIDGenerate() {} - -uint64_t UUIDGenerate::GenenrateSequenceId() -{ - ++sequenceId_; - return sequenceId_; -} } // namespace OHOS::DistributedKv diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h index 1ddf240a5..9a6db3f04 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.h @@ -111,13 +111,5 @@ private: bool isRegisterSyncCallback_ = false; std::mutex registerCallbackMutex_; }; - -class UUIDGenerate{ -public: - explicit UUIDGenerate(); - static uint64_t GenenrateSequenceId(); -private: - static std::atomic sequenceId_; -}; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR2_SINGLE_KVSTORE_CLIENT_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp index 9513fe7dd..23683c4eb 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp @@ -1,6 +1,17 @@ -// -// Created by Sincer on 2022/2/12. -// +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include "sync_observer.h" diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h index c174475e6..c20c3cd4a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h @@ -1,6 +1,17 @@ -// -// Created by Sincer on 2022/2/12. -// +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H #define DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index c29c0717c..b53e0e84e 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -92,6 +92,7 @@ ohos_shared_library("distributeddata_inner") { deps = [ "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//utils/native/base:utils", ] external_deps = [ diff --git a/services/distributeddataservice/adapter/include/utils/kvstore_utils.h b/services/distributeddataservice/adapter/include/utils/kvstore_utils.h index bfa9ab9a1..65f8fb165 100755 --- a/services/distributeddataservice/adapter/include/utils/kvstore_utils.h +++ b/services/distributeddataservice/adapter/include/utils/kvstore_utils.h @@ -17,6 +17,7 @@ #define KVSTORE_UTILS_H #include +#include #include "visibility.h" #include "communication_provider.h" @@ -30,9 +31,12 @@ public: KVSTORE_API static std::string ToBeAnonymous(const std::string &name); KVSTORE_API static AppDistributedKv::CommunicationProvider &GetProviderInstance(); + + KVSTORE_API static uint64_t GenerateSequenceId(); private: static constexpr int MAIN_USER_ID = 0; static constexpr int SYSTEM_UID = 1000; + static std::atomic sequenceId_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp index 89dc7333e..f32bd0d0e 100755 --- a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp +++ b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp @@ -25,6 +25,7 @@ constexpr int32_t END_SIZE = 3; constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3; constexpr const char *REPLACE_CHAIN = "***"; constexpr const char *DEFAULT_ANONYMOUS = "******"; +std::atomic KvStoreUtils::sequenceId_{0}; std::string KvStoreUtils::ToBeAnonymous(const std::string &name) { if (name.length() <= HEAD_SIZE) { @@ -46,5 +47,11 @@ AppDistributedKv::CommunicationProvider &KvStoreUtils::GetProviderInstance() return *(AppDistributedKv::CommunicationProvider::MakeCommunicationProvider().get()); #endif } + +uint64_t KvStoreUtils::GenerateSequenceId() +{ + ++sequenceId_; + return sequenceId_; +} } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index 7ca9afc38..a174c2d1e 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -50,7 +50,6 @@ DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool } deviceId_.clear(); hasPrefixKey_ = (query.find(DataQuery::KEY_PREFIX) != std::string::npos); - hasInKeys_ = (query.find(DataQuery::IN_KEYS) != std::string::npos); size_t pos = query.find_first_not_of(DataQuery::SPACE); std::string inputTrim = (pos == std::string::npos) ? "" : query.substr(pos); std::regex regex(" "); @@ -506,13 +505,15 @@ void QueryHelper::HandleKeyPrefix(const std::vector &words, int &po void QueryHelper::HandleInKeys(const std::vector &words, int &pointer, const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { // pointer points at keyword "IN_KEYS", (pointer + 1) points at keyword "START_IN" - if (pointer + 2 > end || words.at(pointer + 1) != DataQuery::START_IN) { // This keyword has at least 2 params + int startInOffSet = pointer + 1; + int queryLen = end - pointer; + if (queryLen < 2 || words.at(startInOffSet) != DataQuery::START_IN) { // This keyword has at least 2 params ZLOGE("In not enough params."); isSuccess = false; return; } - int inkeyPointer = pointer + 2; // inkeyPointer points at the first inkey value - const std::vector inKeys = GetStringList(words, inkeyPointer, end); + int inkeyOffSet = startInOffSet + 1; // inkeyOffSet points at the first inkey value + const std::vector inKeys = GetStringList(words, inkeyOffSet, end); std::set> inDbKeys; for(const std::string &inKey : inKeys) { ZLOGI("inKey=%{public}s", inKey.c_str()); @@ -524,7 +525,8 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point ZLOGI("size of inKeys=%{public}d", size); dbQuery.InKeys(inDbKeys); isSuccess = true; - pointer = inkeyPointer + 1; // inkeyPointer points at keyword "END", Pointer goes to next keyword + int endOffSet = inkeyOffSet; + pointer = endOffSet + 1; // endOffSet points at keyword "END", Pointer goes to next keyword } void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, -- Gitee From b64510c8b24f961199b496996e2c73d0b72b4d23 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 16:42:26 +0800 Subject: [PATCH 24/32] synccallback and inkeys Signed-off-by: Hollokin --- .../src/single_kvstore_client.cpp | 4 ++-- .../distributeddatafwk/src/sync_observer.cpp | 6 ++++-- .../distributeddatafwk/src/sync_observer.h | 4 ++-- .../distributeddata/include/single_kvstore.h | 3 ++- .../adapter/utils/src/kvstore_utils.cpp | 2 +- .../distributeddataservice/app/src/query_helper.cpp | 6 ++---- .../app/src/single_kvstore_impl.cpp | 13 +++++++------ 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp index dfdb87d83..e8e1cbf9a 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp @@ -466,8 +466,8 @@ Status SingleKvStoreClient::GetSecurityLevel(SecurityLevel &securityLevel) const return Status::SERVER_UNAVAILABLE; } -Status SingleKvStoreClient::SyncWithCondition(const std::vector &deviceIds, SyncMode mode, - const DataQuery &query, std::shared_ptr callback) +Status SingleKvStoreClient::SyncWithCondition(const std::vector &deviceIds, SyncMode mode, + const DataQuery &query, std::shared_ptr callback) { if (kvStoreProxy_ == nullptr) { ZLOGE("singleKvstore proxy is nullptr."); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp index 23683c4eb..608383ff8 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp @@ -20,12 +20,14 @@ SyncObserver::SyncObserver(const std::vector callback) { +bool SyncObserver::Add(const std::shared_ptr callback) +{ callbacks_.push_back(callback); return true; } -bool SyncObserver::Clean() { +bool SyncObserver::Clean() +{ callbacks_.clear(); return true; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h index c20c3cd4a..c30c43a3b 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h @@ -23,7 +23,7 @@ namespace OHOS::DistributedKv { class SyncObserver : public KvStoreSyncCallback { public: - SyncObserver(const std::vector> &callbacks); + explicit SyncObserver(const std::vector> &callbacks); SyncObserver() = default; @@ -39,4 +39,4 @@ private: std::vector> callbacks_; }; } -#endif //DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H +# endif //DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H diff --git a/interfaces/innerkits/distributeddata/include/single_kvstore.h b/interfaces/innerkits/distributeddata/include/single_kvstore.h index f0db828c0..e816575c3 100755 --- a/interfaces/innerkits/distributeddata/include/single_kvstore.h +++ b/interfaces/innerkits/distributeddata/include/single_kvstore.h @@ -195,7 +195,8 @@ public: * Return: * Status of this Subscribe operation. */ - KVSTORE_API virtual Status SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) = 0; + KVSTORE_API virtual Status SubscribeWithQuery(const std::vector &deviceIds, + const DataQuery &query) = 0; /* * UnSubscribe store with other devices which is satisfied with the condition. diff --git a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp index f32bd0d0e..a1d6eabb1 100755 --- a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp +++ b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp @@ -25,7 +25,7 @@ constexpr int32_t END_SIZE = 3; constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3; constexpr const char *REPLACE_CHAIN = "***"; constexpr const char *DEFAULT_ANONYMOUS = "******"; -std::atomic KvStoreUtils::sequenceId_{0}; +std::atomic KvStoreUtils::sequenceId_{ 0 }; std::string KvStoreUtils::ToBeAnonymous(const std::string &name) { if (name.length() <= HEAD_SIZE) { diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index a174c2d1e..29eb58252 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -15,15 +15,14 @@ #define LOG_TAG "QueryHelper" -#include "query_helper.h" #include #include #include #include "kvstore_utils.h" #include "data_query.h" #include "log_print.h" -#include #include "types.h" +#include "query_helper.h" namespace OHOS::DistributedKv { constexpr int QUERY_SKIP_SIZE = 1; @@ -31,7 +30,6 @@ constexpr int QUERY_WORD_SIZE = 2; constexpr int MAX_QUERY_LENGTH = 5 * 1024; // Max query string length 5k constexpr int MAX_QUERY_COMPLEXITY = 500; // Max query complexity 500 bool QueryHelper::hasPrefixKey_{}; -bool QueryHelper::hasInKeys_{}; std::string QueryHelper::deviceId_{}; DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool &isSuccess) @@ -515,7 +513,7 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point int inkeyOffSet = startInOffSet + 1; // inkeyOffSet points at the first inkey value const std::vector inKeys = GetStringList(words, inkeyOffSet, end); std::set> inDbKeys; - for(const std::string &inKey : inKeys) { + for (const std::string &inKey : inKeys) { ZLOGI("inKey=%{public}s", inKey.c_str()); std::vector dbKey; dbKey.assign(inKey.begin(), inKey.end()); diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 7c7226052..4e46b5649 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -712,7 +712,8 @@ Status SingleKvStoreImpl::RemoveDeviceData(const std::string &device) return Status::ERROR; } -Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs, uint64_t sequenceId) +Status SingleKvStoreImpl::Sync(const std::vector &deviceIds, SyncMode mode, + uint32_t allowedDelayMs, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); @@ -808,8 +809,8 @@ void SingleKvStoreImpl::DoSyncComplete(const std::map &deviceIds, SyncMode mode, const std::string &query, - const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId) +Status SingleKvStoreImpl::DoQuerySync(const std::vector &deviceIds, SyncMode mode, + const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -905,7 +906,7 @@ std::vector SingleKvStoreImpl::MapNodeIdToUuids(const std::vector &deviceIds, - const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd) + const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -936,7 +937,7 @@ Status SingleKvStoreImpl::DoSubscribe(const std::vector &deviceIds, } Status SingleKvStoreImpl::DoUnSubscribe(const std::vector &deviceIds, const std::string &query, - const KvStoreSyncManager::SyncEnd &syncEnd) + const KvStoreSyncManager::SyncEnd &syncEnd) { ZLOGD("start."); std::vector deviceUuids = MapNodeIdToUuids(deviceIds); @@ -984,7 +985,7 @@ Status SingleKvStoreImpl::AddUnSubscribe(const std::vector &deviceI } Status SingleKvStoreImpl::Subscribe(const std::vector &deviceIds, - const std::string &query, uint64_t sequenceId) + const std::string &query, uint64_t sequenceId) { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGD("start."); -- Gitee From 0c194157f4a8a9e9c13ae327bcfacb84a9f15fdb Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 16:45:55 +0800 Subject: [PATCH 25/32] UnsubscribeWithQuery Signed-off-by: Hollokin --- .../test/unittest/single_kvstore_client_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 08a1a52e4..846d227d7 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -1101,7 +1101,7 @@ HWTEST_F(SingleKvStoreClientTest, UnSubscribeWithQuery001, TestSize.Level1) std::vector deviceIds = {"invalid_device_id1", "invalid_device_id2"}; DataQuery dataQuery; dataQuery.KeyPrefix("name"); - auto unSubscribeStatus = singleKvStorePtr->UnSubscribeWithQuery(deviceIds, dataQuery); + auto unSubscribeStatus = singleKvStorePtr->UnsubscribeWithQuery(deviceIds, dataQuery); EXPECT_NE(unSubscribeStatus, Status::SUCCESS) << "sync device should not return success"; } -- Gitee From c1ea8421e35c6759e67ebd3b739532f0e6ea4cfb Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 16:57:58 +0800 Subject: [PATCH 26/32] synccallback Signed-off-by: Hollokin --- frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h | 2 +- services/distributeddataservice/app/src/query_helper.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h index c30c43a3b..02051b805 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.h @@ -39,4 +39,4 @@ private: std::vector> callbacks_; }; } -# endif //DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H +#endif // DISTRIBUTEDDATAMGR_DATAMGR_SYNC_OBSERVER_H diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/app/src/query_helper.cpp index 29eb58252..cb56bf8a5 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/app/src/query_helper.cpp @@ -29,7 +29,7 @@ constexpr int QUERY_SKIP_SIZE = 1; constexpr int QUERY_WORD_SIZE = 2; constexpr int MAX_QUERY_LENGTH = 5 * 1024; // Max query string length 5k constexpr int MAX_QUERY_COMPLEXITY = 500; // Max query complexity 500 -bool QueryHelper::hasPrefixKey_{}; +bool QueryHelper::hasPrefixKey_{ }; std::string QueryHelper::deviceId_{}; DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool &isSuccess) -- Gitee From cf4ae9818acfcc995f8c3f2f6ef9639ac13f0f03 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 17:31:11 +0800 Subject: [PATCH 27/32] inkeys Signed-off-by: Hollokin --- .../distributeddatafwk/src/kvstore_sync_callback_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index b10d2ea52..efc853a04 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -25,7 +25,7 @@ KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { if (SyncCallbackInfo_.Contains(sequenceId)) { - ZLOGI("label = %{public}llu", sequenceId); + ZLOGI("label = %{public}lu", sequenceId); SyncCallbackInfo_[sequenceId]->SyncCompleted(results); DeleteSyncCallback(sequenceId); } -- Gitee From 118828721e085ed442fd7ac8283a0392b3b893c7 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 17:48:33 +0800 Subject: [PATCH 28/32] synccallback Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 1 - .../single_kvstore_client_query_test.cpp | 16 ++++++++++++++++ .../app/src/single_kvstore_impl.cpp | 1 - 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index efc853a04..af266b53c 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -25,7 +25,6 @@ KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { if (SyncCallbackInfo_.Contains(sequenceId)) { - ZLOGI("label = %{public}lu", sequenceId); SyncCallbackInfo_[sequenceId]->SyncCompleted(results); DeleteSyncCallback(sequenceId); } 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 3e4f83c94..e3b769ac3 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 @@ -589,3 +589,19 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC018, TestSize.Level1) EXPECT_TRUE(query.ToString().length() > 0); ZLOGD("TestQueryC018 end"); } + +/** +* @tc.name: TestQueryC019 +* @tc.desc: Query InKeys. +* @tc.type: FUNC +* @tc.require: AR000GOHO7 +* @tc.author: taoyuxin +*/ +HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC019, TestSize.Level1) +{ +ZLOGD("TestQueryC018 start"); +DataQuery query; +query.InKeys({"test_field_name"}); +EXPECT_TRUE(query.ToString().length() > 0); +ZLOGD("TestQueryC019 end"); +} diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index 4e46b5649..984fe07bc 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -804,7 +804,6 @@ void SingleKvStoreImpl::DoSyncComplete(const std::mapSyncCompleted(resultMap, sequenceId); } } -- Gitee From b2860d3f6ad54734758690d28414224b9c527b2d Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 17:52:08 +0800 Subject: [PATCH 29/32] inkeys Signed-off-by: Hollokin --- .../test/unittest/single_kvstore_client_query_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e3b769ac3..2c8941f7d 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 @@ -599,7 +599,7 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC018, TestSize.Level1) */ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC019, TestSize.Level1) { -ZLOGD("TestQueryC018 start"); +ZLOGD("TestQueryC019 start"); DataQuery query; query.InKeys({"test_field_name"}); EXPECT_TRUE(query.ToString().length() > 0); -- Gitee From 362e3833ceb23afb896a1010b91d91e428347497 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 17:54:34 +0800 Subject: [PATCH 30/32] inkeys Signed-off-by: Hollokin --- .../test/unittest/single_kvstore_client_query_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2c8941f7d..ac54d4ee7 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 @@ -601,7 +601,7 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC019, TestSize.Level1) { ZLOGD("TestQueryC019 start"); DataQuery query; -query.InKeys({"test_field_name"}); +query.InKeys( {"test_field_name"} ); EXPECT_TRUE(query.ToString().length() > 0); ZLOGD("TestQueryC019 end"); } -- Gitee From c32951d2d52370d8c45b977b87d81e7cd670cfd2 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 14 Feb 2022 21:17:10 +0800 Subject: [PATCH 31/32] synccallback Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 16 ++++++++-------- .../src/kvstore_sync_callback_client.h | 4 ++-- .../adapter/utils/src/kvstore_utils.cpp | 3 +-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index af266b53c..6ced29a72 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -15,6 +15,7 @@ #define LOG_TAG "KvStoreSyncCallbackClient" +#include #include #include "log_print.h" #include "kvstore_sync_callback_client.h" @@ -24,25 +25,24 @@ namespace DistributedKv { KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { - if (SyncCallbackInfo_.Contains(sequenceId)) { - SyncCallbackInfo_[sequenceId]->SyncCompleted(results); + if (syncCallbackInfo_.Contains(sequenceId)) { + syncCallbackInfo_[sequenceId]->SyncCompleted(results); DeleteSyncCallback(sequenceId); } } -void KvStoreSyncCallbackClient::AddSyncCallback(const std::shared_ptr SyncCallback, +void KvStoreSyncCallbackClient::AddSyncCallback(const std::shared_ptr callback, uint64_t sequenceId) { - if (!SyncCallbackInfo_.Contains(sequenceId)) { - SyncCallbackInfo_.Insert(sequenceId, SyncCallback); + auto inserted = syncCallbackInfo_.Insert(sequenceId, callback); + if (!inserted) { + ZLOGE("The sequeuceId %{public}" PRIu64 "is repeat!", sequenceId); } } void KvStoreSyncCallbackClient::DeleteSyncCallback(uint64_t sequenceId) { - if (SyncCallbackInfo_.Contains(sequenceId)) { - SyncCallbackInfo_.Erase(sequenceId); - } + syncCallbackInfo_.Erase(sequenceId); } } // namespace DistributedKv } // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h index d68c8357d..52ac38e68 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.h @@ -31,12 +31,12 @@ public: void SyncCompleted(const std::map &results, uint64_t sequenceId) override; - void AddSyncCallback(const std::shared_ptr SyncCallback, + void AddSyncCallback(const std::shared_ptr callback, uint64_t sequenceId); void DeleteSyncCallback(uint64_t sequenceId); private: - ConcurrentMap> SyncCallbackInfo_; + ConcurrentMap> syncCallbackInfo_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp index a1d6eabb1..097aaf043 100755 --- a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp +++ b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp @@ -50,8 +50,7 @@ AppDistributedKv::CommunicationProvider &KvStoreUtils::GetProviderInstance() uint64_t KvStoreUtils::GenerateSequenceId() { - ++sequenceId_; - return sequenceId_; + return ++sequenceId_; } } // namespace DistributedKv } // namespace OHOS -- Gitee From 844b7421c0acb363ef3ff710bcd96d0f4a5054f0 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Tue, 15 Feb 2022 09:39:07 +0800 Subject: [PATCH 32/32] synccallback Signed-off-by: Hollokin --- .../src/kvstore_sync_callback_client.cpp | 5 +++-- .../test/unittest/single_kvstore_client_query_test.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp index 6ced29a72..a0d112be6 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp @@ -25,8 +25,9 @@ namespace DistributedKv { KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient() = default; void KvStoreSyncCallbackClient::SyncCompleted(const std::map &results, uint64_t sequenceId) { - if (syncCallbackInfo_.Contains(sequenceId)) { - syncCallbackInfo_[sequenceId]->SyncCompleted(results); + auto finded = syncCallbackInfo_.Find(sequenceId); + if (finded.first) { + finded.second->SyncCompleted(results); DeleteSyncCallback(sequenceId); } } 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 ac54d4ee7..ae6fff015 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 @@ -599,9 +599,9 @@ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC018, TestSize.Level1) */ HWTEST_F(SingleKvStoreClientQueryTest, TestQueryC019, TestSize.Level1) { -ZLOGD("TestQueryC019 start"); -DataQuery query; -query.InKeys( {"test_field_name"} ); -EXPECT_TRUE(query.ToString().length() > 0); -ZLOGD("TestQueryC019 end"); + ZLOGD("TestQueryC019 start"); + DataQuery query; + query.InKeys( {"test_field_name"} ); + EXPECT_TRUE(query.ToString().length() > 0); + ZLOGD("TestQueryC019 end"); } -- Gitee