From 6e8b8321cea49d30c22d727385be1709878a3060 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 09:36:04 +0800 Subject: [PATCH 1/6] add service implement Signed-off-by: Sven Wang --- .../innerkitsimpl/kvdb/include/kvdb_service.h | 9 +- .../kvdb/include/kvdb_service_client.h | 8 +- .../kvdb/include/single_store_impl.h | 9 +- .../kvdb/src/kvdb_service_client.cpp | 35 ++-- .../kvdb/src/single_store_impl.cpp | 36 ++-- .../innerkitsimpl/kvdb/src/store_factory.cpp | 4 +- .../include/js_single_kv_store.h | 2 - .../adapter/utils/src/kvstore_utils.cpp | 8 +- services/distributeddataservice/app/BUILD.gn | 2 - .../app/src/backup_handler.cpp | 3 +- .../app/src/kvstore_data_service.cpp | 24 ++- .../app/src/kvstore_data_service.h | 10 +- .../app/test/unittest/kvstore_backup_test.cpp | 37 +++-- .../metadata/secret_key_meta_data.cpp | 13 +- .../distributeddataservice/service/BUILD.gn | 2 + .../service/kvdb/kvdb_service_impl.cpp | 157 ++++++++++++++++-- .../service/kvdb/kvdb_service_impl.h | 21 ++- .../service/kvdb/kvdb_service_stub.cpp | 44 +++-- .../kvdb}/kvstore_sync_manager.cpp | 0 .../kvdb}/kvstore_sync_manager.h | 2 +- .../src => service/kvdb}/query_helper.cpp | 0 .../{app/src => service/kvdb}/query_helper.h | 2 +- .../service/object/object_manager.cpp | 1 - 23 files changed, 308 insertions(+), 121 deletions(-) rename services/distributeddataservice/{app/src => service/kvdb}/kvstore_sync_manager.cpp (100%) rename services/distributeddataservice/{app/src => service/kvdb}/kvstore_sync_manager.h (98%) rename services/distributeddataservice/{app/src => service/kvdb}/query_helper.cpp (100%) rename services/distributeddataservice/{app/src => service/kvdb}/query_helper.h (98%) diff --git a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h index ab29e3be2..548490ebc 100644 --- a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h +++ b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h @@ -16,7 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SERVICE_H #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SERVICE_H #include - +#include #include "ikvstore_observer.h" #include "ikvstore_sync_callback.h" #include "iremote_broker.h" @@ -29,7 +29,7 @@ class API_EXPORT KVDBService : public IRemoteBroker { public: using SingleKVStore = SingleKvStore; struct SyncInfo { - uint64_t seqId = -1; + uint64_t seqId = std::numeric_limits::max(); int32_t mode = PUSH_PULL; uint32_t delay = 0; std::vector devices; @@ -47,9 +47,8 @@ public: const AppId &appId, const StoreId &storeId, const Options &options, const std::vector &password) = 0; virtual Status Delete(const AppId &appId, const StoreId &storeId) = 0; virtual Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) = 0; - virtual Status RegisterSyncCallback( - const AppId &appId, const StoreId &storeId, sptr callback) = 0; - virtual Status UnregisterSyncCallback(const AppId &appId, const StoreId &storeId) = 0; + virtual Status RegisterSyncCallback(const AppId &appId, sptr callback) = 0; + virtual Status UnregisterSyncCallback(const AppId &appId) = 0; virtual Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) = 0; virtual Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) = 0; virtual Status EnableCapability(const AppId &appId, const StoreId &storeId) = 0; diff --git a/frameworks/innerkitsimpl/kvdb/include/kvdb_service_client.h b/frameworks/innerkitsimpl/kvdb/include/kvdb_service_client.h index 2f77e6420..fb513f235 100644 --- a/frameworks/innerkitsimpl/kvdb/include/kvdb_service_client.h +++ b/frameworks/innerkitsimpl/kvdb/include/kvdb_service_client.h @@ -31,9 +31,8 @@ public: const std::vector &password) override; Status Delete(const AppId &appId, const StoreId &storeId) override; Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; - Status RegisterSyncCallback( - const AppId &appId, const StoreId &storeId, sptr callback) override; - Status UnregisterSyncCallback(const AppId &appId, const StoreId &storeId) override; + Status RegisterSyncCallback(const AppId &appId, sptr callback) override; + Status UnregisterSyncCallback(const AppId &appIdd) override; Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) override; Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) override; Status EnableCapability(const AppId &appId, const StoreId &storeId) override; @@ -61,7 +60,8 @@ private: static std::shared_ptr instance_; static std::atomic_bool isWatched_; sptr remote_; - ConcurrentMap> syncAgents_; + std::mutex agentMtx_; + sptr syncAgent_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SERVICE_CLIENT_H diff --git a/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h b/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h index 6c482b572..92b6d9a4d 100644 --- a/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h +++ b/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h @@ -20,7 +20,6 @@ #include "concurrent_map.h" #include "kv_store_nb_delegate.h" #include "kvdb_service.h" -#include "kvstore_sync_callback_client.h" #include "observer_bridge.h" #include "single_kvstore.h" #include "sync_observer.h" @@ -37,7 +36,7 @@ public: using DBQuery = DistributedDB::Query; using SyncInfo = KVDBService::SyncInfo; using Convert = std::function; - SingleStoreImpl(const AppId &appId, std::shared_ptr dbStore); + SingleStoreImpl(std::shared_ptr dbStore, const AppId &appId, const Options &options); ~SingleStoreImpl() = default; StoreId GetStoreId() const override; Status Put(const Key &key, const Value &value) override; @@ -87,15 +86,15 @@ private: Status GetResultSet(const DistributedDB::Query &query, std::shared_ptr &resultSet) const; Status GetEntries(const DistributedDB::Query &query, std::vector &entries) const; Status DoSync(const SyncInfo &syncInfo, std::shared_ptr observer); + void DoAutoSync(); std::function BridgeReleaser(); + std::shared_ptr dbStore_ = nullptr; std::string appId_; std::string storeId_; + bool autoSync_ = false; mutable std::shared_mutex rwMutex_; - mutable std::mutex mutex_; - std::shared_ptr dbStore_ = nullptr; std::shared_ptr syncObserver_ = nullptr; - sptr syncCallback_ = nullptr; ConcurrentMap>> observers_; }; } // namespace OHOS::DistributedKv diff --git a/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp b/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp index 71902cb79..452f757de 100644 --- a/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp +++ b/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp @@ -153,25 +153,23 @@ Status KVDBServiceClient::Sync(const AppId &appId, const StoreId &storeId, const return static_cast(status); } -Status KVDBServiceClient::RegisterSyncCallback( - const AppId &appId, const StoreId &storeId, sptr callback) +Status KVDBServiceClient::RegisterSyncCallback(const AppId &appId, sptr callback) { MessageParcel reply; - int32_t status = IPC_SEND(TRANS_REGISTER_CALLBACK, reply, appId, storeId, callback->AsObject().GetRefPtr()); + int32_t status = IPC_SEND(TRANS_REGISTER_CALLBACK, reply, appId, StoreId(), callback->AsObject().GetRefPtr()); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, callback:0x%{public}x", status, - appId.appId.c_str(), storeId.storeId.c_str(), StoreUtil::Anonymous(callback.GetRefPtr())); + ZLOGE("status:0x%{public}x, appId:%{public}s, callback:0x%{public}x", status, appId.appId.c_str(), + StoreUtil::Anonymous(callback.GetRefPtr())); } return static_cast(status); } -Status KVDBServiceClient::UnregisterSyncCallback(const AppId &appId, const StoreId &storeId) +Status KVDBServiceClient::UnregisterSyncCallback(const AppId &appId) { MessageParcel reply; - int32_t status = IPC_SEND(TRANS_UNREGISTER_CALLBACK, reply, appId, storeId); + int32_t status = IPC_SEND(TRANS_UNREGISTER_CALLBACK, reply, appId, StoreId()); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + ZLOGE("status:0x%{public}x, appId:%{public}s", status, appId.appId.c_str()); } return static_cast(status); } @@ -282,17 +280,16 @@ Status KVDBServiceClient::Unsubscribe(const AppId &appId, const StoreId &storeId sptr KVDBServiceClient::GetSyncAgent(const AppId &appId) { - auto it = syncAgents_.Find(appId); - if (it.first && it.second == nullptr) { - syncAgents_.Erase(appId); + std::lock_guard lockGuard(agentMtx_); + if (syncAgent_ != nullptr) { + return syncAgent_; } - syncAgents_.ComputeIfAbsent(appId.appId, [this](const std::string &key) { - sptr agent = new (std::nothrow) KvStoreSyncCallbackClient(); - RegisterSyncCallback({ key }, { "" }, agent); - return agent; - }); - - return syncAgents_[appId]; + sptr syncAgent = new (std::nothrow) KvStoreSyncCallbackClient(); + auto status = RegisterSyncCallback(appId, syncAgent); + if (status == SUCCESS) { + syncAgent_ = std::move(syncAgent); + } + return syncAgent_; } } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp b/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp index 44c27a33d..ba96bae61 100644 --- a/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp +++ b/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp @@ -23,8 +23,8 @@ #include "store_result_set.h" #include "store_util.h" namespace OHOS::DistributedKv { -SingleStoreImpl::SingleStoreImpl(const AppId &appId, std::shared_ptr dbStore) - : appId_(appId.appId), dbStore_(std::move(dbStore)) +SingleStoreImpl::SingleStoreImpl(std::shared_ptr dbStore, const AppId &appId, const Options &options) + : dbStore_(std::move(dbStore)), appId_(appId.appId), autoSync_(options.autoSync) { storeId_ = dbStore_->GetStoreId(); syncObserver_ = std::make_shared(); @@ -56,7 +56,7 @@ Status SingleStoreImpl::Put(const Key &key, const Value &value) ZLOGE("status:0x%{public}x, key:%{public}s, value size:%{public}zu", status, StoreUtil::Anonymous(key.ToString()).c_str(), value.Size()); } - // do auto sync process + DoAutoSync(); return status; } @@ -86,7 +86,7 @@ Status SingleStoreImpl::PutBatch(const std::vector &entries) if (status != SUCCESS) { ZLOGE("status:0x%{public}x, entries size:%{public}zu", status, entries.size()); } - // do auto sync process + DoAutoSync(); return status; } @@ -110,7 +110,7 @@ Status SingleStoreImpl::Delete(const Key &key) if (status != SUCCESS) { ZLOGE("status:0x%{public}x, key:%{public}s", status, StoreUtil::Anonymous(key.ToString()).c_str()); } - // do auto sync process + DoAutoSync(); return status; } @@ -138,7 +138,7 @@ Status SingleStoreImpl::DeleteBatch(const std::vector &keys) if (status != SUCCESS) { ZLOGE("status:0x%{public}x, keys size:%{public}zu", status, keys.size()); } - // do auto sync process + DoAutoSync(); return status; } @@ -559,15 +559,6 @@ Status SingleStoreImpl::Close() { observers_.Clear(); syncObserver_->Clean(); - std::shared_ptr service; - { - std::lock_guard lock(mutex_); - service = (syncCallback_ != nullptr) ? KVDBServiceClient::GetInstance() : nullptr; - syncCallback_ = nullptr; - } - if (service != nullptr) { - service->UnregisterSyncCallback({ appId_ }, { storeId_ }); - } std::unique_lock lock(rwMutex_); dbStore_ = nullptr; return SUCCESS; @@ -688,7 +679,22 @@ Status SingleStoreImpl::DoSync(const SyncInfo &syncInfo, std::shared_ptrAddSyncCallback(observer, syncInfo.seqId); return service->Sync({ appId_ }, { storeId_ }, syncInfo); } + +void SingleStoreImpl::DoAutoSync() +{ + if (!autoSync_) { + return; + } + + SyncInfo syncInfo; + auto service = KVDBServiceClient::GetInstance(); + if (service == nullptr) { + return; + } + service->Sync({ appId_ }, { storeId_ }, syncInfo); +} } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp b/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp index 2b84024d5..61b571a64 100644 --- a/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp +++ b/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp @@ -56,9 +56,9 @@ std::shared_ptr StoreFactory::GetOrOpenStore( auto release = [dbManager](auto *store) { dbManager->CloseKvStore(store); }; auto dbStore = std::shared_ptr(store, release); if (options.kvStoreType == DEVICE_COLLABORATION) { - kvStore = std::make_shared(appId, dbStore); + kvStore = std::make_shared(dbStore, appId, options); } else { - kvStore = std::make_shared(appId, dbStore); + kvStore = std::make_shared(dbStore, appId, options); } stores[dbStore->GetStoreId()] = kvStore; }); diff --git a/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h index 979c535e4..ffa818eb3 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h @@ -35,8 +35,6 @@ private: static napi_value GetResultSize(napi_env env, napi_callback_info info); static napi_value RemoveDeviceData(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); - static napi_value OnEvent(napi_env env, napi_callback_info info); - static napi_value OffEvent(napi_env env, napi_callback_info info); static napi_value SetSyncParam(napi_env env, napi_callback_info info); static napi_value GetSecurityLevel(napi_env env, napi_callback_info info); }; diff --git a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp index bfb23ba2e..3a1939f1e 100644 --- a/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp +++ b/services/distributeddataservice/adapter/utils/src/kvstore_utils.cpp @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define LOG_TAG "KvStoreUtils" #include "kvstore_utils.h" +#include #include "permission_validator.h" namespace OHOS { @@ -41,7 +41,11 @@ std::string KvStoreUtils::ToBeAnonymous(const std::string &name) uint64_t KvStoreUtils::GenerateSequenceId() { - return ++sequenceId_; + uint64_t seqId = ++sequenceId_; + if (seqId == std::numeric_limits::max()) { + return ++sequenceId_; + } + return seqId; } } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 46af583c0..f8068afd1 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -92,9 +92,7 @@ ohos_shared_library("distributeddataservice") { "src/kvstore_meta_manager.cpp", "src/kvstore_observer_impl.cpp", "src/kvstore_resultset_impl.cpp", - "src/kvstore_sync_manager.cpp", "src/kvstore_user_manager.cpp", - "src/query_helper.cpp", "src/security/security.cpp", "src/security/sensitive.cpp", "src/session_manager/route_head_handler_impl.cpp", diff --git a/services/distributeddataservice/app/src/backup_handler.cpp b/services/distributeddataservice/app/src/backup_handler.cpp index 73dac1651..d93127925 100644 --- a/services/distributeddataservice/app/src/backup_handler.cpp +++ b/services/distributeddataservice/app/src/backup_handler.cpp @@ -178,8 +178,7 @@ bool BackupHandler::GetPassword(const StoreMetaData &metaData, DistributedDB::Ci return true; } - std::string key = SecretKeyMetaData::GetKey({ metaData.user, "default", metaData.bundleName, metaData.storeId, - metaData.storeType == SINGLE_VERSION ? "SINGLE_KEY" : "KEY" }); + std::string key = SecretKeyMetaData::GetKey({ metaData.user, "default", metaData.bundleName, metaData.storeId }); SecretKeyMetaData secretKey; MetaDataManager::GetInstance().LoadMeta(key, secretKey, true); std::vector decryptKey; diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index f020bbfaa..6ca66eddc 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -670,13 +670,13 @@ Status KvStoreDataService::RegisterClientDeathObserver(const AppId &appId, sptr< std::lock_guard lg(clientDeathObserverMutex_); clientDeathObserverMap_.erase(info.tokenId); auto it = clientDeathObserverMap_.emplace(std::piecewise_construct, std::forward_as_tuple(info.tokenId), - std::forward_as_tuple(appId, info.uid, info.tokenId, *this, std::move(observer))); + std::forward_as_tuple(appId, *this, std::move(observer))); ZLOGI("bundleName:%{public}s, uid:%{public}d, pid:%{public}d inserted:%{public}s.", appId.appId.c_str(), info.uid, IPCSkeleton::GetCallingPid(), it.second ? "success" : "failed"); return it.second ? Status::SUCCESS : Status::ERROR; } -Status KvStoreDataService::AppExit(const AppId &appId, pid_t uid, uint32_t token) +Status KvStoreDataService::AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId) { ZLOGI("AppExit"); // memory of parameter appId locates in a member of clientDeathObserverMap_ and will be freed after @@ -1028,12 +1028,14 @@ void KvStoreDataService::OnStop() } KvStoreDataService::KvStoreClientDeathObserverImpl::KvStoreClientDeathObserverImpl( - const AppId &appId, pid_t uid, uint32_t token, KvStoreDataService &service, sptr observer) - : appId_(appId), uid_(uid), token_(token), dataService_(service), observerProxy_(std::move(observer)), - deathRecipient_(new KvStoreDeathRecipient(*this)) + const AppId &appId, KvStoreDataService &service, sptr observer) + : appId_(appId), dataService_(service), observerProxy_(std::move(observer)), + deathRecipient_(new KvStoreDeathRecipient(*this)) { ZLOGI("KvStoreClientDeathObserverImpl"); - + uid_ = IPCSkeleton::GetCallingUid(); + pid_ = IPCSkeleton::GetCallingPid(); + token_ = IPCSkeleton::GetCallingTokenID(); if (observerProxy_ != nullptr) { ZLOGI("add death recipient"); observerProxy_->AddDeathRecipient(deathRecipient_); @@ -1049,12 +1051,20 @@ KvStoreDataService::KvStoreClientDeathObserverImpl::~KvStoreClientDeathObserverI ZLOGI("remove death recipient"); observerProxy_->RemoveDeathRecipient(deathRecipient_); } + sptr kvdbService; + { + std::lock_guard lockGuard(dataService_.mutex_); + kvdbService = dataService_.kvdbService_; + } + if (kvdbService) { + kvdbService->AppExit(uid_, pid_, token_, appId_); + } } void KvStoreDataService::KvStoreClientDeathObserverImpl::NotifyClientDie() { ZLOGI("appId: %{public}s uid:%{public}d tokenId:%{public}u", appId_.appId.c_str(), uid_, token_); - dataService_.AppExit(appId_, uid_, token_); + dataService_.AppExit(uid_, pid_, token_, appId_); } KvStoreDataService::KvStoreClientDeathObserverImpl::KvStoreDeathRecipient::KvStoreDeathRecipient( diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 313d552d4..abcf1d0fa 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -112,8 +112,7 @@ public: private: class KvStoreClientDeathObserverImpl { public: - KvStoreClientDeathObserverImpl(const AppId &appId, pid_t uid, uint32_t token, - KvStoreDataService &service, sptr observer); + KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr observer); virtual ~KvStoreClientDeathObserverImpl(); @@ -128,9 +127,10 @@ private: KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_; }; void NotifyClientDie(); - AppId appId_; pid_t uid_; + pid_t pid_; uint32_t token_; + AppId appId_; KvStoreDataService &dataService_; sptr observerProxy_; sptr deathRecipient_; @@ -162,7 +162,7 @@ private: Status GetSingleKvStoreFailDo(Status status, const Options &options, const StoreMetaData &metaData, SecretKeyPara &secKeyParas, KvStoreUserManager &kvUserManager, sptr &kvStore); - Status AppExit(const AppId &appId, pid_t uid, uint32_t token); + Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId); bool CheckPermissions(const std::string &userId, const std::string &appId, const std::string &storeId, const std::string &deviceId, uint8_t flag) const; @@ -195,7 +195,7 @@ private: std::shared_ptr security_; std::mutex mutex_; sptr rdbService_; - sptr kvdbService_; + sptr kvdbService_; sptr objectService_; std::shared_ptr deviceInnerListener_; }; diff --git a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp index 3ed804c7e..79c9feb9f 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp @@ -12,19 +12,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + #include +#include #include -#include #include -#include -#include "bootstrap.h" + #include "backup_handler.h" +#include "bootstrap.h" +#include "communication_provider.h" +#include "gtest/gtest.h" +#include "kv_store_delegate_manager.h" #include "kvstore_data_service.h" #include "log_print.h" -#include "single_kvstore_impl.h" #include "metadata/meta_data_manager.h" -#include "communication_provider.h" -#include "gtest/gtest.h" +#include "process_communicator_impl.h" +#include "session_manager/route_head_handler_impl.h" +#include "single_kvstore_impl.h" using namespace testing::ext; using namespace OHOS::DistributedKv; using namespace OHOS; @@ -44,12 +49,6 @@ protected: }; void KvStoreBackupTest::SetUpTestCase(void) -{} - -void KvStoreBackupTest::TearDownTestCase(void) -{} - -void KvStoreBackupTest::SetUp(void) { const std::string backupDirCe = "/data/misc_ce/0/mdds/0/default/backup"; unlink(backupDirCe.c_str()); @@ -58,12 +57,22 @@ void KvStoreBackupTest::SetUp(void) const std::string backupDirDe = "/data/misc_de/0/mdds/0/default/backup"; unlink(backupDirDe.c_str()); mkdir(backupDirDe.c_str(), KvStoreBackupTest::DEFAULT_DIR_MODE); - KvStoreMetaManager::GetInstance().InitMetaParameter(); - KvStoreMetaManager::GetInstance().InitMetaListener(); Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); + KvStoreMetaManager::GetInstance().InitMetaParameter(); + KvStoreMetaManager::GetInstance().InitMetaListener(); + DistributedDB::KvStoreDelegateManager::SetProcessLabel("KvStoreBackupTest", "default"); + auto communicator = std::make_shared(RouteHeadHandlerImpl::Create); + (void)DistributedDB::KvStoreDelegateManager::SetProcessCommunicator(communicator); +} + +void KvStoreBackupTest::TearDownTestCase(void) +{} + +void KvStoreBackupTest::SetUp(void) +{ deviceId_ = CommunicationProvider::GetInstance().GetLocalDevice().uuid; } diff --git a/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp b/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp index 41599d383..eebe8dcf8 100644 --- a/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp @@ -41,16 +41,19 @@ bool SecretKeyMetaData::Unmarshal(const json &node) std::string SecretKeyMetaData::GetKey(const std::initializer_list &fields) { - std::string prefix = KEY_PREFIX; - for (const auto &field : fields) { - prefix.append(Constant::KEY_SEPARATOR).append(field); - } + std::string prefix = GetPrefix(fields); + prefix.append("SINGLE_KEY"); return prefix; } std::string SecretKeyMetaData::GetPrefix(const std::initializer_list &fields) { - return GetKey(fields).append(Constant::KEY_SEPARATOR); + std::string prefix = KEY_PREFIX; + for (const auto &field : fields) { + prefix.append(Constant::KEY_SEPARATOR).append(field); + } + prefix.append(Constant::KEY_SEPARATOR); + return prefix; } } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index a44ebca5e..5b067b6b8 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -51,6 +51,8 @@ ohos_shared_library("distributeddatasvc") { "kvdb/executor_factory.cpp", "kvdb/kvdb_service_impl.cpp", "kvdb/kvdb_service_stub.cpp", + "kvdb/kvstore_sync_manager.cpp", + "kvdb/query_helper.cpp", "kvdb/user_delegate.cpp", "object/object_manager.cpp", "object/object_service_impl.cpp", diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index d63d9eba0..b1286f3be 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -15,14 +15,21 @@ #define LOG_TAG "KVDBServiceImpl" #include "kvdb_service_impl.h" +#include "accesstoken_kit.h" #include "account/account_delegate.h" +#include "checker/checker_manager.h" #include "communication_provider.h" +#include "directory_manager.h" #include "ipc_skeleton.h" +#include "kvstore_sync_manager.h" +#include "log_print.h" #include "metadata/meta_data_manager.h" -#include "metadata/store_meta_data.h" +#include "metadata/secret_key_meta_data.h" +#include "utils/converter.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using namespace OHOS::AppDistributedKv; +using namespace OHOS::Security::AccessToken; KVDBServiceImpl::KVDBServiceImpl() { } @@ -46,7 +53,15 @@ Status KVDBServiceImpl::GetStoreIds(const AppId &appId, std::vector &st Status KVDBServiceImpl::Delete(const AppId &appId, const StoreId &storeId) { - return NOT_SUPPORT; + StoreMetaData metaData = GetStoreMetaData(appId, storeId); + if (metaData.instanceId < 0) { + return ILLEGAL_STATE; + } + + MetaDataManager::GetInstance().DelMeta(metaData.GetKey()); + auto key = SecretKeyMetaData::GetKey({ metaData.user, "default", metaData.bundleName, metaData.storeId }); + MetaDataManager::GetInstance().DelMeta(key, true); + return SUCCESS; } Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) @@ -54,41 +69,85 @@ Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const S return NOT_SUPPORT; } -Status KVDBServiceImpl::RegisterSyncCallback( - const AppId &appId, const StoreId &storeId, sptr callback) +Status KVDBServiceImpl::RegisterSyncCallback(const AppId &appId, sptr callback) { - return NOT_SUPPORT; + syncAgents_.InsertOrAssign(IPCSkeleton::GetCallingTokenID(), std::pair{ IPCSkeleton::GetCallingPid(), callback }); + return SUCCESS; } -Status KVDBServiceImpl::UnregisterSyncCallback(const AppId &appId, const StoreId &storeId) +Status KVDBServiceImpl::UnregisterSyncCallback(const AppId &appId) { - return NOT_SUPPORT; + syncAgents_.ComputeIfPresent( + IPCSkeleton::GetCallingTokenID(), [](const auto &key, std::pair> &value) { + return !(value.first == IPCSkeleton::GetCallingPid()); + }); + return SUCCESS; } Status KVDBServiceImpl::SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) { - return NOT_SUPPORT; + if (syncParam.allowedDelayMs > 0 && syncParam.allowedDelayMs < KvStoreSyncManager::SYNC_MIN_DELAY_MS) { + return Status::INVALID_ARGUMENT; + } + if (syncParam.allowedDelayMs > KvStoreSyncManager::SYNC_MAX_DELAY_MS) { + return Status::INVALID_ARGUMENT; + } + delayTimes_.Compute( + IPCSkeleton::GetCallingTokenID(), [&storeId, &syncParam](auto &key, std::map &values) { + values[storeId] = syncParam.allowedDelayMs; + return !values.empty(); + }); + return SUCCESS; } Status KVDBServiceImpl::GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) { - return NOT_SUPPORT; + delayTimes_.ComputeIfPresent(IPCSkeleton::GetCallingTokenID(), [&storeId, &syncParam](auto &key, std::map &values) { + auto it = values.find(storeId); + if (it != values.end()) { + syncParam.allowedDelayMs = it->second; + } + return !values.empty(); + }); + return SUCCESS; } Status KVDBServiceImpl::EnableCapability(const AppId &appId, const StoreId &storeId) { - return NOT_SUPPORT; + StrategyMeta strategyMeta = GetStrategyMeta(appId, storeId); + if (strategyMeta.instanceId < 0) { + return ILLEGAL_STATE; + } + MetaDataManager::GetInstance().LoadMeta(strategyMeta.GetKey(), strategyMeta); + strategyMeta.capabilityEnabled = true; + MetaDataManager::GetInstance().SaveMeta(strategyMeta.GetKey(), strategyMeta); + return SUCCESS; } Status KVDBServiceImpl::DisableCapability(const AppId &appId, const StoreId &storeId) { - return NOT_SUPPORT; + StrategyMeta strategyMeta = GetStrategyMeta(appId, storeId); + if (strategyMeta.instanceId < 0) { + return ILLEGAL_STATE; + } + MetaDataManager::GetInstance().LoadMeta(strategyMeta.GetKey(), strategyMeta); + strategyMeta.capabilityEnabled = false; + MetaDataManager::GetInstance().SaveMeta(strategyMeta.GetKey(), strategyMeta); + return SUCCESS; } Status KVDBServiceImpl::SetCapability(const AppId &appId, const StoreId &storeId, const std::vector &local, const std::vector &remote) { - return NOT_SUPPORT; + StrategyMeta strategyMeta = GetStrategyMeta(appId, storeId); + if (strategyMeta.instanceId < 0) { + return ILLEGAL_STATE; + } + MetaDataManager::GetInstance().LoadMeta(strategyMeta.GetKey(), strategyMeta); + strategyMeta.capabilityRange.localLabel = local; + strategyMeta.capabilityRange.remoteLabel = remote; + MetaDataManager::GetInstance().SaveMeta(strategyMeta.GetKey(), strategyMeta); + return SUCCESS; } Status KVDBServiceImpl::AddSubscribeInfo( @@ -115,12 +174,84 @@ Status KVDBServiceImpl::Unsubscribe(const AppId &appId, const StoreId &storeId, Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) { - return NOT_SUPPORT; + return SUCCESS; } Status KVDBServiceImpl::AfterCreate( const AppId &appId, const StoreId &storeId, const Options &options, const std::vector &password) { + if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType()) { + return INVALID_ARGUMENT; + } + StoreMetaData metaData = GetStoreMetaData(appId, storeId); + StoreMetaData oldMeta; + MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), oldMeta); + if (oldMeta == metaData) { + return SUCCESS; + } + AddOptions(options, metaData); + return NOT_SUPPORT; } + +Status KVDBServiceImpl::AppExit(pid_t uid, pid_t pid, uint32_t tokenId, const AppId &appId) +{ + dataObservers_.ComputeIfPresent(tokenId, [pid](const auto &, auto &value) { return !(value.first == pid); }); + syncAgents_.ComputeIfPresent(tokenId, [pid](const auto &, auto &value) { return !(value.first == pid); }); + delayTimes_.Erase(tokenId); + return SUCCESS; +} + +void KVDBServiceImpl::AddOptions(const Options &options, StoreMetaData &metaData) +{ + metaData.securityLevel = options.securityLevel; + metaData.storeType = options.kvStoreType; + metaData.isBackup = options.backup; + metaData.isEncrypt = options.encrypt; + metaData.isAutoSync = options.autoSync; + metaData.appId = CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(metaData)); + metaData.user = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(metaData.uid); + metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); + metaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData); +} + +StoreMetaData KVDBServiceImpl::GetStoreMetaData(const AppId &appId, const StoreId &storeId) +{ + StoreMetaData metaData; + metaData.deviceId = AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().uuid; + metaData.uid = IPCSkeleton::GetCallingUid(); + metaData.tokenId = IPCSkeleton::GetCallingTokenID(); + metaData.bundleName = appId.appId; + metaData.storeId = storeId.storeId; + metaData.user = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(metaData.uid); + metaData.instanceId = GetInstIndex(metaData.tokenId, appId); + return metaData; +} + +StrategyMeta KVDBServiceImpl::GetStrategyMeta(const AppId &appId, const StoreId &storeId) +{ + auto deviceId = AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().uuid; + auto userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(IPCSkeleton::GetCallingUid()); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + StrategyMeta strategyMeta(deviceId, userId, appId.appId, storeId.storeId); + strategyMeta.instanceId = GetInstIndex(tokenId, appId); + return strategyMeta; +} + +int32_t KVDBServiceImpl::GetInstIndex(uint32_t tokenId, const AppId &appId) +{ + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + return 0; + } + + HapTokenInfo tokenInfo; + tokenInfo.instIndex = -1; + int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (errCode != RET_SUCCESS) { + ZLOGE("GetHapTokenInfo error:%{public}d, appId:%{public}s", errCode, appId.appId.c_str()); + return -1; + } + return tokenInfo.instIndex; +} + } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 8c13705ce..681d27b4b 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -15,7 +15,10 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_KVDB_SERVICE_IMPL_H #define OHOS_DISTRIBUTED_DATA_SERVICES_KVDB_SERVICE_IMPL_H +#include "concurrent_map.h" #include "kvdb_service_stub.h" +#include "metadata/store_meta_data.h" +#include "metadata/strategy_meta_data.h" namespace OHOS::DistributedKv { class API_EXPORT KVDBServiceImpl final : public KVDBServiceStub { public: @@ -27,9 +30,8 @@ public: const std::vector &password) override; Status Delete(const AppId &appId, const StoreId &storeId) override; Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; - Status RegisterSyncCallback( - const AppId &appId, const StoreId &storeId, sptr callback) override; - Status UnregisterSyncCallback(const AppId &appId, const StoreId &storeId) override; + Status RegisterSyncCallback(const AppId &appId, sptr callback) override; + Status UnregisterSyncCallback(const AppId &appId) override; Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) override; Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) override; Status EnableCapability(const AppId &appId, const StoreId &storeId) override; @@ -42,6 +44,19 @@ public: const std::string &query) override; Status Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; + Status AppExit(pid_t uid, pid_t pid, uint32_t tokenId, const AppId &appId); + +private: + using StoreMetaData = OHOS::DistributedData::StoreMetaData; + using StrategyMeta = OHOS::DistributedData::StrategyMeta; + void AddOptions(const Options &options, StoreMetaData &metaData); + StoreMetaData GetStoreMetaData(const AppId &appId, const StoreId &storeId); + StrategyMeta GetStrategyMeta(const AppId &appId, const StoreId &storeId); + int32_t GetInstIndex(uint32_t tokenId, const AppId &appId); + + ConcurrentMap>> syncAgents_; + ConcurrentMap>> dataObservers_; + ConcurrentMap> delayTimes_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_SERVICES_KVDB_SERVICE_IMPL_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 7a86e8b73..8c2899733 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -15,6 +15,7 @@ #define LOG_TAG "KVDBServiceStub" #include "kvdb_service_stub.h" +#include "checker/checker_manager.h" #include "ipc_skeleton.h" #include "itypes_util.h" #include "log_print.h" @@ -42,7 +43,7 @@ const KVDBServiceStub::Handler KVDBServiceStub::HANDLERS[TRANS_BUTT] = { int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { - ZLOGD("code:%{public}u, callingPid:%{public}u", code, IPCSkeleton::GetCallingPid()); + ZLOGI("code:%{public}u, callingPid:%{public}u", code, IPCSkeleton::GetCallingPid()); std::u16string local = KVDBServiceStub::GetDescriptor(); std::u16string remote = data.ReadInterfaceToken(); if (local != remote) { @@ -50,18 +51,35 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return -1; } - if (TRANS_HEAD <= code && code < TRANS_BUTT && HANDLERS[code] != nullptr) { - AppId appId; - StoreId storeId; - if (!ITypesUtil::Unmarshal(data, appId, storeId)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); - return IPC_STUB_INVALID_DATA_ERR; - } - appId.appId = Constant::TrimCopy(appId.appId); - storeId.storeId = Constant::TrimCopy(storeId.storeId); + if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] != nullptr) { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + AppId appId; + StoreId storeId; + if (!ITypesUtil::Unmarshal(data, appId, storeId)) { + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + appId.appId = Constant::TrimCopy(appId.appId); + storeId.storeId = Constant::TrimCopy(storeId.storeId); + + CheckerManager::StoreInfo info; + info.uid = IPCSkeleton::GetCallingUid(); + info.tokenId = IPCSkeleton::GetCallingTokenID(); + info.bundleName = appId.appId; + info.storeId = storeId.storeId; + if (CheckerManager::GetInstance().IsValid(info)) { return (this->*HANDLERS[code])(appId, storeId, data, reply); } - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + ZLOGE("PERMISSION_DENIED uid:%{public}d, appId:%{public}s, storeId:%{public}s", info.uid, info.bundleName.c_str(), + info.storeId.c_str()); + + if (!ITypesUtil::Marshal(reply, PERMISSION_DENIED)) { + ZLOGE("Marshal PERMISSION_DENIED code£º%{public}u, appId:%{public}s storeId:%{public}s", code, + appId.appId.c_str(), storeId.storeId.c_str()); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return ERR_NONE; } int32_t KVDBServiceStub::OnGetStoreIds( @@ -148,7 +166,7 @@ int32_t KVDBServiceStub::OnRegisterCallback( return IPC_STUB_INVALID_DATA_ERR; } auto syncCallback = (remoteObj == nullptr) ? nullptr : iface_cast(remoteObj); - int32_t status = RegisterSyncCallback(appId, storeId, syncCallback); + int32_t status = RegisterSyncCallback(appId, syncCallback); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), storeId.storeId.c_str()); @@ -160,7 +178,7 @@ int32_t KVDBServiceStub::OnRegisterCallback( int32_t KVDBServiceStub::OnUnregisterCallback( const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) { - int32_t status = UnregisterSyncCallback(appId, storeId); + int32_t status = UnregisterSyncCallback(appId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), storeId.storeId.c_str()); diff --git a/services/distributeddataservice/app/src/kvstore_sync_manager.cpp b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp similarity index 100% rename from services/distributeddataservice/app/src/kvstore_sync_manager.cpp rename to services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp diff --git a/services/distributeddataservice/app/src/kvstore_sync_manager.h b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h similarity index 98% rename from services/distributeddataservice/app/src/kvstore_sync_manager.h rename to services/distributeddataservice/service/kvdb/kvstore_sync_manager.h index 15b142b10..004ce6493 100644 --- a/services/distributeddataservice/app/src/kvstore_sync_manager.h +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h @@ -24,7 +24,7 @@ namespace OHOS { namespace DistributedKv { -class KvStoreSyncManager { +class API_EXPORT KvStoreSyncManager { public: static constexpr uint32_t SYNC_DEFAULT_DELAY_MS = 1000; static constexpr uint32_t SYNC_MIN_DELAY_MS = 100; diff --git a/services/distributeddataservice/app/src/query_helper.cpp b/services/distributeddataservice/service/kvdb/query_helper.cpp similarity index 100% rename from services/distributeddataservice/app/src/query_helper.cpp rename to services/distributeddataservice/service/kvdb/query_helper.cpp diff --git a/services/distributeddataservice/app/src/query_helper.h b/services/distributeddataservice/service/kvdb/query_helper.h similarity index 98% rename from services/distributeddataservice/app/src/query_helper.h rename to services/distributeddataservice/service/kvdb/query_helper.h index d39df548d..6b4223b68 100644 --- a/services/distributeddataservice/app/src/query_helper.h +++ b/services/distributeddataservice/service/kvdb/query_helper.h @@ -23,7 +23,7 @@ namespace OHOS::DistributedKv { class QueryHelper { public: - static DistributedDB::Query StringToDbQuery(const std::string &query, bool &isSuccess); + API_EXPORT static DistributedDB::Query StringToDbQuery(const std::string &query, bool &isSuccess); private: static std::string deviceId_; diff --git a/services/distributeddataservice/service/object/object_manager.cpp b/services/distributeddataservice/service/object/object_manager.cpp index 88e389d24..9162f49e1 100644 --- a/services/distributeddataservice/service/object/object_manager.cpp +++ b/services/distributeddataservice/service/object/object_manager.cpp @@ -21,7 +21,6 @@ #include "checker/checker_manager.h" #include "kvstore_utils.h" #include "log_print.h" -#include "query_helper.h" namespace OHOS { namespace DistributedObject { -- Gitee From 3f41f756c36bde1f022e28de2b72fe6443256edc Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 10:09:24 +0800 Subject: [PATCH 2/6] remove const object with need to constructor Signed-off-by: Sven Wang --- .../framework/include/metadata/capability_meta_data.h | 6 +++--- .../framework/include/metadata/user_meta_data.h | 2 +- .../framework/metadata/capability_meta_data.cpp | 4 +++- .../framework/metadata/user_meta_data.cpp | 2 +- .../service/kvdb/kvdb_service_stub.cpp | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/capability_meta_data.h b/services/distributeddataservice/framework/include/metadata/capability_meta_data.h index f2dadaf3c..702a362a1 100644 --- a/services/distributeddataservice/framework/include/metadata/capability_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/capability_meta_data.h @@ -20,8 +20,8 @@ namespace OHOS::DistributedData { class API_EXPORT CapMetaData final : public Serializable { public: - const static int32_t CURRENT_VERSION = 1; - const static int32_t INVALID_VERSION = -1; + static constexpr int32_t CURRENT_VERSION = 1; + static constexpr int32_t INVALID_VERSION = -1; int32_t version = INVALID_VERSION; API_EXPORT bool Marshal(json &node) const override; @@ -30,7 +30,7 @@ public: class CapMetaRow { public: - static const std::string KEY_PREFIX; + static constexpr const char *KEY_PREFIX = "CapabilityMeta"; API_EXPORT static std::vector GetKeyFor(const std::string &key); }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/framework/include/metadata/user_meta_data.h b/services/distributeddataservice/framework/include/metadata/user_meta_data.h index e6c207293..7ffdaca67 100644 --- a/services/distributeddataservice/framework/include/metadata/user_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/user_meta_data.h @@ -42,7 +42,7 @@ public: class UserMetaRow { public: - API_EXPORT static const std::string KEY_PREFIX; + API_EXPORT static constexpr const char *KEY_PREFIX = "UserMeta"; API_EXPORT static std::string GetKeyFor(const std::string &key); }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/framework/metadata/capability_meta_data.cpp b/services/distributeddataservice/framework/metadata/capability_meta_data.cpp index 4091a9349..ab81aaeb8 100644 --- a/services/distributeddataservice/framework/metadata/capability_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/capability_meta_data.cpp @@ -17,6 +17,9 @@ #include "utils/constant.h" namespace OHOS::DistributedData { +constexpr int32_t CapMetaData::CURRENT_VERSION; +constexpr int32_t CapMetaData::INVALID_VERSION; +constexpr const char *CapMetaRow::KEY_PREFIX; bool CapMetaData::Marshal(json &node) const { bool ret = true; @@ -31,7 +34,6 @@ bool CapMetaData::Unmarshal(const json &node) return ret; } -const std::string CapMetaRow::KEY_PREFIX = "CapabilityMeta"; std::vector CapMetaRow::GetKeyFor(const std::string &key) { std::string str = Constant::Concatenate({ KEY_PREFIX, Constant::KEY_SEPARATOR, key }); diff --git a/services/distributeddataservice/framework/metadata/user_meta_data.cpp b/services/distributeddataservice/framework/metadata/user_meta_data.cpp index d19b9c8b1..e1452ef4c 100644 --- a/services/distributeddataservice/framework/metadata/user_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/user_meta_data.cpp @@ -17,6 +17,7 @@ #include "utils/constant.h" namespace OHOS::DistributedData { +constexpr const char *UserMetaRow::KEY_PREFIX; bool UserMetaData::Marshal(json &node) const { bool ret = true; @@ -54,7 +55,6 @@ UserStatus::UserStatus(int id, bool isActive) : id(id), isActive(isActive) { } -const std::string UserMetaRow::KEY_PREFIX = "UserMeta"; std::string UserMetaRow::GetKeyFor(const std::string &key) { std::string str = Constant::Concatenate({ KEY_PREFIX, Constant::KEY_SEPARATOR, key }); diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 8c2899733..afe89fafb 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -75,7 +75,7 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message info.storeId.c_str()); if (!ITypesUtil::Marshal(reply, PERMISSION_DENIED)) { - ZLOGE("Marshal PERMISSION_DENIED code£º%{public}u, appId:%{public}s storeId:%{public}s", code, + ZLOGE("Marshal PERMISSION_DENIED code:%{public}u, appId:%{public}s storeId:%{public}s", code, appId.appId.c_str(), storeId.storeId.c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } -- Gitee From 41d8b9a976d1e8815ed34c211e74d08ea19d8803 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 17:32:51 +0800 Subject: [PATCH 3/6] update Signed-off-by: Sven Wang --- frameworks/innerkitsimpl/kvdb/include/kvdb_service.h | 10 +++++++++- .../innerkitsimpl/object/include/iobject_service.h | 10 +++++++++- frameworks/innerkitsimpl/rdb/include/irdb_service.h | 11 +++++++++-- .../app/src/uninstaller/BUILD.gn | 1 + services/distributeddataservice/app/test/BUILD.gn | 12 ------------ 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h index 548490ebc..0b66e0fda 100644 --- a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h +++ b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h @@ -25,6 +25,14 @@ #include "types.h" #include "visibility.h" namespace OHOS::DistributedKv { +#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN +#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ + static inline const std::u16string &GetDescriptor() \ + { \ + static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ + return metaDescriptor_; \ + } +#endif class API_EXPORT KVDBService : public IRemoteBroker { public: using SingleKVStore = SingleKvStore; @@ -35,7 +43,7 @@ public: std::vector devices; std::string query; }; - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.KVDBService"); + DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedKv.KVDBService"); API_EXPORT KVDBService() = default; diff --git a/frameworks/innerkitsimpl/object/include/iobject_service.h b/frameworks/innerkitsimpl/object/include/iobject_service.h index b31ecc8e2..070010b72 100644 --- a/frameworks/innerkitsimpl/object/include/iobject_service.h +++ b/frameworks/innerkitsimpl/object/include/iobject_service.h @@ -20,6 +20,14 @@ #include #include "object_service.h" +#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN +#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ + static inline const std::u16string &GetDescriptor() \ + { \ + static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ + return metaDescriptor_; \ + } +#endif namespace OHOS::DistributedObject { class IObjectService : public ObjectService, public IRemoteBroker { @@ -30,7 +38,7 @@ public: OBJECTSTORE_RETRIEVE, OBJECTSTORE_SERVICE_CMD_MAX }; - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedObject.IObjectService"); + DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedObject.IObjectService"); }; } // namespace OHOS::DistributedRdb #endif diff --git a/frameworks/innerkitsimpl/rdb/include/irdb_service.h b/frameworks/innerkitsimpl/rdb/include/irdb_service.h index f5ecc638e..d59559f30 100644 --- a/frameworks/innerkitsimpl/rdb/include/irdb_service.h +++ b/frameworks/innerkitsimpl/rdb/include/irdb_service.h @@ -21,8 +21,15 @@ #include #include "rdb_service.h" #include "rdb_types.h" - namespace OHOS::DistributedRdb { +#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN +#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ + static inline const std::u16string &GetDescriptor() \ + { \ + static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ + return metaDescriptor_; \ + } +#endif class IRdbService : public RdbService, public IRemoteBroker { public: enum { @@ -36,7 +43,7 @@ public: RDB_SERVICE_CMD_MAX }; - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); + DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedRdb.IRdbService"); virtual int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) = 0; diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index 4dd21a166..8121f155c 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -25,6 +25,7 @@ ohos_static_library("distributeddata_uninstaller_static") { "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/framework/include", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/service/kvdb", "//third_party/json/single_include", "//utils/native/base/include", "//base/security/huks/interfaces/innerkits/huks_standard/main/include", diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 65974bb8a..f0b0c2a0e 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -74,9 +74,7 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", @@ -139,9 +137,7 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", @@ -204,9 +200,7 @@ ohos_unittest("KvStoreDataServiceTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", @@ -270,9 +264,7 @@ ohos_unittest("KvStoreBackupTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", @@ -373,9 +365,7 @@ ohos_unittest("KvStoreSyncManagerTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", @@ -437,9 +427,7 @@ ohos_unittest("KvStoreUninstallerTest") { "../src/kvstore_meta_manager.cpp", "../src/kvstore_observer_impl.cpp", "../src/kvstore_resultset_impl.cpp", - "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", - "../src/query_helper.cpp", "../src/security/security.cpp", "../src/security/sensitive.cpp", "../src/session_manager/route_head_handler_impl.cpp", -- Gitee From d4e41f18340b37349f28f4268b32aa67f6da1e3d Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 20:57:08 +0800 Subject: [PATCH 4/6] fixed code style Signed-off-by: Sven Wang --- .../service/kvdb/query_helper.cpp | 370 ++++++++---------- .../service/kvdb/query_helper.h | 88 ++--- 2 files changed, 206 insertions(+), 252 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/query_helper.cpp b/services/distributeddataservice/service/kvdb/query_helper.cpp index b197b61ef..a7e7db0a7 100644 --- a/services/distributeddataservice/service/kvdb/query_helper.cpp +++ b/services/distributeddataservice/service/kvdb/query_helper.cpp @@ -20,8 +20,8 @@ #include #include -#include "kvstore_utils.h" #include "data_query.h" +#include "kvstore_utils.h" #include "log_print.h" #include "types.h" @@ -29,14 +29,14 @@ namespace OHOS::DistributedKv { 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_{ }; +constexpr int MAX_QUERY_COMPLEXITY = 500; // Max query complexity 500 +bool QueryHelper::hasPrefixKey_{}; std::string QueryHelper::deviceId_{}; DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool &isSuccess) { ZLOGI("query string length:%{public}zu", query.length()); - DistributedDB::Query dbQuery = DistributedDB::Query::Select(); + DBQuery dbQuery = DBQuery::Select(); if (query.empty()) { ZLOGI("Query string is empty."); isSuccess = true; @@ -54,98 +54,95 @@ DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool std::regex regex(" "); std::vector words( std::sregex_token_iterator(inputTrim.begin(), inputTrim.end(), regex, -1), // regex split string by space - std::sregex_token_iterator() - ); + std::sregex_token_iterator()); - int pointer = 0; // Read pointer starts at 0 + int pointer = 0; // Read pointer starts at 0 int end = words.size() - 1; // Read pointer ends at size - 1 - int count = 0; // Counts how many keywords has been handled + int count = 0; // Counts how many keywords has been handled while (pointer <= end && count <= MAX_QUERY_COMPLEXITY) { count++; std::string keyword = words.at(pointer); if (keyword == DataQuery::EQUAL_TO) { - HandleEqualTo(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleEqualTo(words, pointer, end, dbQuery); } else if (keyword == DataQuery::NOT_EQUAL_TO) { - HandleNotEqualTo(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleNotEqualTo(words, pointer, end, dbQuery); } else if (keyword == DataQuery::GREATER_THAN) { - HandleGreaterThan(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleGreaterThan(words, pointer, end, dbQuery); } else if (keyword == DataQuery::LESS_THAN) { - HandleLessThan(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleLessThan(words, pointer, end, dbQuery); } else if (keyword == DataQuery::GREATER_THAN_OR_EQUAL_TO) { - HandleGreaterThanOrEqualTo(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleGreaterThanOrEqualTo(words, pointer, end, dbQuery); } else if (keyword == DataQuery::LESS_THAN_OR_EQUAL_TO) { - HandleLessThanOrEqualTo(words, pointer, end, isSuccess, dbQuery); + isSuccess = HandleLessThanOrEqualTo(words, pointer, end, dbQuery); } else { - Handle(words, pointer, end, isSuccess, dbQuery); + isSuccess = Handle(words, pointer, end, dbQuery); } if (!isSuccess) { ZLOGE("Invalid params."); - return DistributedDB::Query::Select(); + return DBQuery::Select(); } } return dbQuery; } -void QueryHelper::Handle(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::Handle(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ std::string keyword = words.at(pointer); if (keyword == DataQuery::IS_NULL) { - HandleIsNull(words, pointer, end, isSuccess, dbQuery); + return HandleIsNull(words, pointer, end, dbQuery); } else if (keyword == DataQuery::IN) { - HandleIn(words, pointer, end, isSuccess, dbQuery); + return HandleIn(words, pointer, end, dbQuery); } else if (keyword == DataQuery::NOT_IN) { - HandleNotIn(words, pointer, end, isSuccess, dbQuery); + return HandleNotIn(words, pointer, end, dbQuery); } else if (keyword == DataQuery::LIKE) { - HandleLike(words, pointer, end, isSuccess, dbQuery); + return HandleLike(words, pointer, end, dbQuery); } else if (keyword == DataQuery::NOT_LIKE) { - HandleNotLike(words, pointer, end, isSuccess, dbQuery); + return HandleNotLike(words, pointer, end, dbQuery); } else if (keyword == DataQuery::AND) { - HandleAnd(words, pointer, end, isSuccess, dbQuery); + return HandleAnd(words, pointer, end, dbQuery); } else if (keyword == DataQuery::OR) { - HandleOr(words, pointer, end, isSuccess, dbQuery); + return HandleOr(words, pointer, end, dbQuery); } else if (keyword == DataQuery::ORDER_BY_ASC) { - HandleOrderByAsc(words, pointer, end, isSuccess, dbQuery); + return HandleOrderByAsc(words, pointer, end, dbQuery); } else if (keyword == DataQuery::ORDER_BY_DESC) { - HandleOrderByDesc(words, pointer, end, isSuccess, dbQuery); + return HandleOrderByDesc(words, pointer, end, dbQuery); } else if (keyword == DataQuery::LIMIT) { - HandleLimit(words, pointer, end, isSuccess, dbQuery); + return HandleLimit(words, pointer, end, dbQuery); } else { - HandleExtra(words, pointer, end, isSuccess, dbQuery); + return HandleExtra(words, pointer, end, dbQuery); } } -void QueryHelper::HandleExtra(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleExtra(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ std::string keyword = words.at(pointer); if (keyword == DataQuery::BEGIN_GROUP) { - HandleBeginGroup(words, pointer, end, isSuccess, dbQuery); + return HandleBeginGroup(words, pointer, end, dbQuery); } else if (keyword == DataQuery::END_GROUP) { - HandleEndGroup(words, pointer, end, isSuccess, dbQuery); + return HandleEndGroup(words, pointer, end, dbQuery); } else if (keyword == DataQuery::KEY_PREFIX) { - HandleKeyPrefix(words, pointer, end, isSuccess, dbQuery); + return HandleKeyPrefix(words, pointer, end, dbQuery); } else if (keyword == DataQuery::IS_NOT_NULL) { - HandleIsNotNull(words, pointer, end, isSuccess, dbQuery); + return HandleIsNotNull(words, pointer, end, dbQuery); } else if (keyword == DataQuery::DEVICE_ID) { - HandleDeviceId(words, pointer, end, isSuccess, dbQuery); + return HandleDeviceId(words, pointer, end, dbQuery); } else if (keyword == DataQuery::SUGGEST_INDEX) { - HandleSetSuggestIndex(words, pointer, end, isSuccess, dbQuery); + return HandleSetSuggestIndex(words, pointer, end, dbQuery); } else if (keyword == DataQuery::IN_KEYS) { - HandleInKeys(words, pointer, end, isSuccess, dbQuery); - } else { - ZLOGE("Invalid keyword."); - isSuccess = false; + return HandleInKeys(words, pointer, end, dbQuery); } + ZLOGE("Invalid keyword."); + return false; } -void QueryHelper::HandleEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleEqualTo(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("EqualTo not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.EqualTo(StringToString(fieldName), StringToInt(fieldValue)); @@ -159,22 +156,20 @@ void QueryHelper::HandleEqualTo(const std::vector &words, int &poin dbQuery.EqualTo(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("EqualTo wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleNotEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleNotEqualTo(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("NotEqualTo not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.NotEqualTo(StringToString(fieldName), StringToInt(fieldValue)); @@ -188,22 +183,20 @@ void QueryHelper::HandleNotEqualTo(const std::vector &words, int &p dbQuery.NotEqualTo(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("NotEqualTo wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleGreaterThan(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleGreaterThan(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("GreaterThan not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.GreaterThan(StringToString(fieldName), StringToInt(fieldValue)); @@ -215,22 +208,20 @@ void QueryHelper::HandleGreaterThan(const std::vector &words, int & dbQuery.GreaterThan(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("GreaterThan wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleLessThan(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleLessThan(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("LessThan not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.LessThan(StringToString(fieldName), StringToInt(fieldValue)); @@ -242,22 +233,21 @@ void QueryHelper::HandleLessThan(const std::vector &words, int &poi dbQuery.LessThan(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("LessThan wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleGreaterThanOrEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleGreaterThanOrEqualTo( + const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("GreaterThanOrEqualTo not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.GreaterThanOrEqualTo(StringToString(fieldName), StringToInt(fieldValue)); @@ -269,22 +259,21 @@ void QueryHelper::HandleGreaterThanOrEqualTo(const std::vector &wor dbQuery.GreaterThanOrEqualTo(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("GreaterThanOrEqualTo wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleLessThanOrEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleLessThanOrEqualTo( + const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 3 > end) { // This keyword has 3 following params ZLOGE("LessThanOrEqualTo not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldType = words.at(pointer + 1); // fieldType - const std::string &fieldName = words.at(pointer + 2); // fieldName + const std::string &fieldType = words.at(pointer + 1); // fieldType + const std::string &fieldName = words.at(pointer + 2); // fieldName const std::string &fieldValue = words.at(pointer + 3); // fieldValue if (fieldType == DataQuery::TYPE_INTEGER) { dbQuery.LessThanOrEqualTo(StringToString(fieldName), StringToInt(fieldValue)); @@ -296,49 +285,45 @@ void QueryHelper::HandleLessThanOrEqualTo(const std::vector &words, dbQuery.LessThanOrEqualTo(StringToString(fieldName), StringToString(fieldValue)); } else { ZLOGE("LessThanOrEqualTo wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer += 4; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleIsNull(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleIsNull(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("IsNull not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.IsNull(StringToString(fieldName)); - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleIsNotNull(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleIsNotNull(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("IsNotNull not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.IsNotNull(StringToString(fieldName)); - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleIn(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleIn(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 4 > end || words.at(pointer + 3) != DataQuery::START_IN) { // This keyword has at least 4 params ZLOGE("In not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldType = words.at(pointer + 1); // fieldType const std::string &fieldName = words.at(pointer + 2); // fieldName - int elementPointer = pointer + 4; // first fieldValue, or END if list is empty + int elementPointer = pointer + 4; // first fieldValue, or END if list is empty if (fieldType == DataQuery::TYPE_INTEGER) { const std::vector intValueList = GetIntegerList(words, elementPointer, end); dbQuery.In(StringToString(fieldName), intValueList); @@ -353,23 +338,21 @@ void QueryHelper::HandleIn(const std::vector &words, int &pointer, dbQuery.In(StringToString(fieldName), stringValueList); } else { ZLOGE("In wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer = elementPointer + 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleNotIn(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleNotIn(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 4 > end || words.at(pointer + 3) != DataQuery::START_IN) { // This keyword has at least 4 params ZLOGE("NotIn not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldType = words.at(pointer + 1); // fieldType const std::string &fieldName = words.at(pointer + 2); // fieldName - int elementPointer = pointer + 4; // first fieldValue, or END if list is empty + int elementPointer = pointer + 4; // first fieldValue, or END if list is empty if (fieldType == DataQuery::TYPE_INTEGER) { const std::vector intValueList = GetIntegerList(words, elementPointer, end); dbQuery.NotIn(StringToString(fieldName), intValueList); @@ -384,134 +367,126 @@ void QueryHelper::HandleNotIn(const std::vector &words, int &pointe dbQuery.NotIn(StringToString(fieldName), stringValueList); } else { ZLOGE("NotIn wrong type."); - isSuccess = false; - return; + return false; } - isSuccess = true; pointer = elementPointer + 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleLike(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleLike(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 2 > end) { // This keyword has 2 following params ZLOGE("Like not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldName = words.at(pointer + 1); // fieldName + const std::string &fieldName = words.at(pointer + 1); // fieldName const std::string &fieldValue = words.at(pointer + 2); // fieldValue dbQuery.Like(StringToString(fieldName), StringToString(fieldValue)); - isSuccess = true; pointer += 3; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleNotLike(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleNotLike(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 2 > end) { // This keyword has 2 following params ZLOGE("NotLike not enough params."); - isSuccess = false; - return; + return false; } - const std::string &fieldName = words.at(pointer + 1); // fieldName + const std::string &fieldName = words.at(pointer + 1); // fieldName const std::string &fieldValue = words.at(pointer + 2); // fieldValue dbQuery.NotLike(StringToString(fieldName), StringToString(fieldValue)); - isSuccess = true; pointer += 3; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleAnd(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleAnd(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ dbQuery.And(); - isSuccess = true; pointer += 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleOr(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleOr(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ dbQuery.Or(); - isSuccess = true; pointer += 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleOrderByAsc(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleOrderByAsc(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("OrderByAsc not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.OrderBy(StringToString(fieldName), true); - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleOrderByDesc(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleOrderByDesc(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("OrderByDesc not enough params."); - isSuccess = false; - return; + return false; } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.OrderBy(StringToString(fieldName), false); - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleLimit(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleLimit(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 2 > end) { // This keyword has 2 following params ZLOGE("Limit not enough params."); - isSuccess = false; - return; + return false; } const int number = StringToInt(words.at(pointer + 1)); // number const int offset = StringToInt(words.at(pointer + 2)); // offset dbQuery.Limit(number, offset); - isSuccess = true; pointer += 3; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleBeginGroup(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleBeginGroup(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ dbQuery.BeginGroup(); - isSuccess = true; pointer += 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleEndGroup(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleEndGroup(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ dbQuery.EndGroup(); - isSuccess = true; pointer += 1; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleKeyPrefix(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleKeyPrefix(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("KeyPrefix not enough params."); - isSuccess = false; - return; + return false; } const std::string &prefix = deviceId_ + StringToString(words.at(pointer + 1)); // prefix const std::vector prefixVector(prefix.begin(), prefix.end()); dbQuery.PrefixKey(prefixVector); - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -void QueryHelper::HandleInKeys(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleInKeys(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ // pointer points at keyword "IN_KEYS", (pointer + 1) points at keyword "START_IN" 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; + return false; } - int inkeyOffSet = startInOffSet + 1; // inkeyOffSet points at the first inkey value + 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) { @@ -523,30 +498,28 @@ void QueryHelper::HandleInKeys(const std::vector &words, int &point int size = inDbKeys.size(); ZLOGI("size of inKeys=%{public}d", size); dbQuery.InKeys(inDbKeys); - isSuccess = true; int endOffSet = inkeyOffSet; pointer = endOffSet + 1; // endOffSet points at keyword "END", Pointer goes to next keyword + return true; } -void QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleSetSuggestIndex(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + QUERY_SKIP_SIZE > end) { ZLOGE("HandleSetSuggestIndex not enough params."); - isSuccess = false; - return; + return false; } std::string index = StringToString(words.at(pointer + QUERY_SKIP_SIZE)); dbQuery.SuggestIndex(index); - isSuccess = true; pointer += QUERY_WORD_SIZE; + return true; } -void QueryHelper::HandleDeviceId(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery) { +bool QueryHelper::HandleDeviceId(const std::vector &words, int &pointer, int end, DBQuery &dbQuery) +{ if (pointer + 1 > end) { // This keyword has 1 following params ZLOGE("DeviceId not enough params."); - isSuccess = false; - return; + return false; } deviceId_ = StringToString(words.at(pointer + 1)); // deviceId ZLOGI("query devId string length:%zu", deviceId_.length()); @@ -559,29 +532,33 @@ void QueryHelper::HandleDeviceId(const std::vector &words, int &poi } else { ZLOGD("Join deviceId with user specified prefixkey later."); } - isSuccess = true; pointer += 2; // Pointer goes to next keyword + return true; } -int QueryHelper::StringToInt(const std::string &word) { +int QueryHelper::StringToInt(const std::string &word) +{ int result; std::istringstream(word) >> result; return result; } -int64_t QueryHelper::StringToLong(const std::string &word) { +int64_t QueryHelper::StringToLong(const std::string &word) +{ int64_t result; std::istringstream(word) >> result; return result; } -double QueryHelper::StringToDouble(const std::string &word) { +double QueryHelper::StringToDouble(const std::string &word) +{ double result; std::istringstream(word) >> result; return result; } -bool QueryHelper::StringToBoolean(const std::string &word) { +bool QueryHelper::StringToBoolean(const std::string &word) +{ if (word == DataQuery::VALUE_TRUE) { return true; } else if (word == DataQuery::VALUE_FALSE) { @@ -592,7 +569,8 @@ bool QueryHelper::StringToBoolean(const std::string &word) { } } -std::string QueryHelper::StringToString(const std::string &word) { +std::string QueryHelper::StringToString(const std::string &word) +{ std::string result = word; if (result.compare(DataQuery::EMPTY_STRING) == 0) { result = ""; @@ -605,7 +583,7 @@ std::string QueryHelper::StringToString(const std::string &word) { break; } result.replace(index, 2, DataQuery::SPACE); // 2 chars to be replaced - index += 1; // replaced with 1 char, keep searching the remaining string + index += 1; // replaced with 1 char, keep searching the remaining string } index = 0; // search from the beginning of the string while (true) { @@ -614,13 +592,13 @@ std::string QueryHelper::StringToString(const std::string &word) { break; } result.replace(index, 3, DataQuery::SPECIAL); // 3 chars to be replaced - index += 1; // replaced with 1 char, keep searching the remaining string + index += 1; // replaced with 1 char, keep searching the remaining string } return result; } -std::vector QueryHelper::GetIntegerList(const std::vector &words, - int &elementPointer, const int &end) { +std::vector QueryHelper::GetIntegerList(const std::vector &words, int &elementPointer, int end) +{ std::vector valueList; bool isEndFound = false; while (elementPointer <= end) { @@ -639,8 +617,8 @@ std::vector QueryHelper::GetIntegerList(const std::vector &wor } } -std::vector QueryHelper::GetLongList(const std::vector &words, - int &elementPointer, const int &end) { +std::vector QueryHelper::GetLongList(const std::vector &words, int &elementPointer, int end) +{ std::vector valueList; bool isEndFound = false; while (elementPointer <= end) { @@ -659,8 +637,8 @@ std::vector QueryHelper::GetLongList(const std::vector &wo } } -std::vector QueryHelper::GetDoubleList(const std::vector &words, - int &elementPointer, const int &end) { +std::vector QueryHelper::GetDoubleList(const std::vector &words, int &elementPointer, int end) +{ std::vector valueList; bool isEndFound = false; while (elementPointer <= end) { @@ -679,8 +657,8 @@ std::vector QueryHelper::GetDoubleList(const std::vector &w } } -std::vector QueryHelper::GetStringList(const std::vector &words, - int &elementPointer, const int &end) { +std::vector QueryHelper::GetStringList(const std::vector &words, int &elementPointer, int end) +{ std::vector valueList; bool isEndFound = false; while (elementPointer <= end) { diff --git a/services/distributeddataservice/service/kvdb/query_helper.h b/services/distributeddataservice/service/kvdb/query_helper.h index 6b4223b68..78db7b9f5 100644 --- a/services/distributeddataservice/service/kvdb/query_helper.h +++ b/services/distributeddataservice/service/kvdb/query_helper.h @@ -17,6 +17,7 @@ #define QUERY_HELPER_H #include + #include "query.h" #include "types.h" @@ -26,70 +27,45 @@ public: API_EXPORT static DistributedDB::Query StringToDbQuery(const std::string &query, bool &isSuccess); private: + using DBQuery = DistributedDB::Query; 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, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleNotEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleGreaterThan(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleLessThan(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleGreaterThanOrEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleLessThanOrEqualTo(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleIsNull(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleIsNotNull(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleIn(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleNotIn(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleLike(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleNotLike(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleAnd(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleOr(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleOrderByAsc(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleOrderByDesc(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleLimit(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleBeginGroup(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleEndGroup(const std::vector &words, int &pointer, - 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, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); + static bool Handle(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleExtra(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleEqualTo(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleNotEqualTo(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleGreaterThan(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleLessThan(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleGreaterThanOrEqualTo( + const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleLessThanOrEqualTo(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleIsNull(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleIsNotNull(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleIn(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleNotIn(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleLike(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleNotLike(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleAnd(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleOr(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleOrderByAsc(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleOrderByDesc(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleLimit(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleBeginGroup(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleEndGroup(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleKeyPrefix(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleInKeys(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleSetSuggestIndex(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); + static bool HandleDeviceId(const std::vector &words, int &pointer, int end, DBQuery &dbQuery); static int StringToInt(const std::string &word); static int64_t StringToLong(const std::string &word); static double StringToDouble(const std::string &word); static bool StringToBoolean(const std::string &word); static std::string StringToString(const std::string &word); - static std::vector GetIntegerList(const std::vector &words, int &elementPointer, const int &end); - static std::vector GetLongList(const std::vector &words, int &elementPointer, const int &end); - static std::vector GetDoubleList(const std::vector &words, - int &elementPointer, const int &end); - static std::vector GetStringList(const std::vector &words, - int &elementPointer, const int &end); + static std::vector GetIntegerList(const std::vector &words, int &elementPointer, int end); + static std::vector GetLongList(const std::vector &words, int &elementPointer, int end); + static std::vector GetDoubleList(const std::vector &words, int &elementPointer, int end); + static std::vector GetStringList(const std::vector &words, int &elementPointer, int end); }; } // namespace OHOS::DistributedKv #endif // QUERY_HELPER_H -- Gitee From 65c06651aa78d2b8713500da890794c740eecdb5 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 21:00:52 +0800 Subject: [PATCH 5/6] fixed code style Signed-off-by: Sven Wang --- .../service/kvdb/kvdb_service_impl.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index b1286f3be..d0d425d27 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -102,13 +102,14 @@ Status KVDBServiceImpl::SetSyncParam(const AppId &appId, const StoreId &storeId, Status KVDBServiceImpl::GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) { - delayTimes_.ComputeIfPresent(IPCSkeleton::GetCallingTokenID(), [&storeId, &syncParam](auto &key, std::map &values) { - auto it = values.find(storeId); - if (it != values.end()) { - syncParam.allowedDelayMs = it->second; - } - return !values.empty(); - }); + delayTimes_.ComputeIfPresent( + IPCSkeleton::GetCallingTokenID(), [&storeId, &syncParam](auto &key, std::map &values) { + auto it = values.find(storeId); + if (it != values.end()) { + syncParam.allowedDelayMs = it->second; + } + return !values.empty(); + }); return SUCCESS; } @@ -253,5 +254,4 @@ int32_t KVDBServiceImpl::GetInstIndex(uint32_t tokenId, const AppId &appId) } return tokenInfo.instIndex; } - } // namespace OHOS::DistributedKv \ No newline at end of file -- Gitee From 55c06a9a53ad55d31f21fdfa519f8db52c010e9f Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 8 Jun 2022 22:11:13 +0800 Subject: [PATCH 6/6] fixed the code style Signed-off-by: Sven Wang --- .../innerkitsimpl/kvdb/include/kvdb_service.h | 10 +---- .../object/include/iobject_service.h | 11 +---- .../innerkitsimpl/rdb/include/irdb_service.h | 10 +---- .../app/test/unittest/kvstore_backup_test.cpp | 5 +-- .../service/kvdb/kvstore_sync_manager.cpp | 2 - .../service/kvdb/kvstore_sync_manager.h | 43 +++++++++---------- .../service/kvdb/query_helper.cpp | 39 ++++++++--------- 7 files changed, 42 insertions(+), 78 deletions(-) diff --git a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h index 0b66e0fda..548490ebc 100644 --- a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h +++ b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h @@ -25,14 +25,6 @@ #include "types.h" #include "visibility.h" namespace OHOS::DistributedKv { -#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN -#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ - static inline const std::u16string &GetDescriptor() \ - { \ - static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ - return metaDescriptor_; \ - } -#endif class API_EXPORT KVDBService : public IRemoteBroker { public: using SingleKVStore = SingleKvStore; @@ -43,7 +35,7 @@ public: std::vector devices; std::string query; }; - DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedKv.KVDBService"); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.KVDBService"); API_EXPORT KVDBService() = default; diff --git a/frameworks/innerkitsimpl/object/include/iobject_service.h b/frameworks/innerkitsimpl/object/include/iobject_service.h index 070010b72..f94810aeb 100644 --- a/frameworks/innerkitsimpl/object/include/iobject_service.h +++ b/frameworks/innerkitsimpl/object/include/iobject_service.h @@ -20,15 +20,6 @@ #include #include "object_service.h" -#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN -#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ - static inline const std::u16string &GetDescriptor() \ - { \ - static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ - return metaDescriptor_; \ - } -#endif - namespace OHOS::DistributedObject { class IObjectService : public ObjectService, public IRemoteBroker { public: @@ -38,7 +29,7 @@ public: OBJECTSTORE_RETRIEVE, OBJECTSTORE_SERVICE_CMD_MAX }; - DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedObject.IObjectService"); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedObject.IObjectService"); }; } // namespace OHOS::DistributedRdb #endif diff --git a/frameworks/innerkitsimpl/rdb/include/irdb_service.h b/frameworks/innerkitsimpl/rdb/include/irdb_service.h index d59559f30..5e0e3948d 100644 --- a/frameworks/innerkitsimpl/rdb/include/irdb_service.h +++ b/frameworks/innerkitsimpl/rdb/include/irdb_service.h @@ -22,14 +22,6 @@ #include "rdb_service.h" #include "rdb_types.h" namespace OHOS::DistributedRdb { -#ifndef DECLARE_INTERFACE_DESCRIPTOR_IN -#define DECLARE_INTERFACE_DESCRIPTOR_IN(DESCRIPTOR) \ - static inline const std::u16string &GetDescriptor() \ - { \ - static std::u16string metaDescriptor_ = { DESCRIPTOR }; \ - return metaDescriptor_; \ - } -#endif class IRdbService : public RdbService, public IRemoteBroker { public: enum { @@ -43,7 +35,7 @@ public: RDB_SERVICE_CMD_MAX }; - DECLARE_INTERFACE_DESCRIPTOR_IN(u"OHOS.DistributedRdb.IRdbService"); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); virtual int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) = 0; diff --git a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp index 79c9feb9f..406de4f4d 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp @@ -12,9 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include - -#include #include #include #include @@ -22,10 +21,8 @@ #include "backup_handler.h" #include "bootstrap.h" #include "communication_provider.h" -#include "gtest/gtest.h" #include "kv_store_delegate_manager.h" #include "kvstore_data_service.h" -#include "log_print.h" #include "metadata/meta_data_manager.h" #include "process_communicator_impl.h" #include "session_manager/route_head_handler_impl.h" diff --git a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp index 746477abe..8fa819f5c 100644 --- a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp @@ -12,9 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define LOG_TAG "KvSyncManager" - #include "kvstore_sync_manager.h" #include "log_print.h" diff --git a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h index 004ce6493..25db29e10 100644 --- a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h @@ -16,11 +16,12 @@ #ifndef KVSTORE_SYNC_MANAGER_H #define KVSTORE_SYNC_MANAGER_H -#include #include -#include "types.h" +#include + #include "kv_scheduler.h" #include "kv_store_nb_delegate.h" +#include "types.h" namespace OHOS { namespace DistributedKv { @@ -28,7 +29,7 @@ class API_EXPORT KvStoreSyncManager { public: static constexpr uint32_t SYNC_DEFAULT_DELAY_MS = 1000; static constexpr uint32_t SYNC_MIN_DELAY_MS = 100; - static constexpr uint32_t SYNC_MAX_DELAY_MS = 1000 * 3600 * 24; // 24hours + static constexpr uint32_t SYNC_MAX_DELAY_MS = 1000 * 3600 * 24; // 24hours static constexpr uint32_t SYNC_RETRY_MAX_COUNT = 3; static KvStoreSyncManager *GetInstance() { @@ -40,16 +41,15 @@ public: using SyncFunc = std::function; struct KvSyncOperation { - uintptr_t syncId{ 0 }; - uint32_t opSeq{ 0 }; - uint32_t delayMs{ 0 }; - SyncFunc syncFunc{}; - SyncEnd syncEnd{}; - TimePoint beginTime{}; + uintptr_t syncId = 0; + uint32_t opSeq = 0; + uint32_t delayMs = 0; + SyncFunc syncFunc; + SyncEnd syncEnd; + TimePoint beginTime; }; using OpPred = std::function; - Status AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc, - const SyncEnd &syncEnd); + Status AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc, const SyncEnd &syncEnd); Status RemoveSyncOperation(uintptr_t syncId); private: @@ -68,16 +68,15 @@ private: static constexpr uint32_t REALTIME_PRIOR_SYNCING_MS = 300; static constexpr uint32_t DELAY_TIME_RANGE_DIVISOR = 4; - mutable std::mutex syncOpsMutex_{}; - std::list realtimeSyncingOps_{}; - std::list delaySyncingOps_{}; - std::multimap scheduleSyncOps_{}; + mutable std::mutex syncOpsMutex_; + std::list realtimeSyncingOps_; + std::list delaySyncingOps_; + std::multimap scheduleSyncOps_; - KvScheduler syncScheduler_{}; - TimePoint nextScheduleTime_{}; - std::atomic_uint32_t syncOpSeq_{ 0 }; + KvScheduler syncScheduler_; + TimePoint nextScheduleTime_; + std::atomic_uint32_t syncOpSeq_ = 0; }; -} // namespace DistributedKv -} // namespace OHOS - -#endif // KVSTORE_SYNC_MANAGER_H +} // namespace DistributedKv +} // namespace OHOS +#endif // KVSTORE_SYNC_MANAGER_H diff --git a/services/distributeddataservice/service/kvdb/query_helper.cpp b/services/distributeddataservice/service/kvdb/query_helper.cpp index a7e7db0a7..088f071df 100644 --- a/services/distributeddataservice/service/kvdb/query_helper.cpp +++ b/services/distributeddataservice/service/kvdb/query_helper.cpp @@ -12,26 +12,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define LOG_TAG "QueryHelper" - #include "query_helper.h" - #include #include - #include "data_query.h" #include "kvstore_utils.h" #include "log_print.h" #include "types.h" - namespace OHOS::DistributedKv { 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_{}; -std::string QueryHelper::deviceId_{}; +bool QueryHelper::hasPrefixKey_ = false; +std::string QueryHelper::deviceId_; DistributedDB::Query QueryHelper::StringToDbQuery(const std::string &query, bool &isSuccess) { @@ -158,7 +153,7 @@ bool QueryHelper::HandleEqualTo(const std::vector &words, int &poin ZLOGE("EqualTo wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -185,7 +180,7 @@ bool QueryHelper::HandleNotEqualTo(const std::vector &words, int &p ZLOGE("NotEqualTo wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -210,7 +205,7 @@ bool QueryHelper::HandleGreaterThan(const std::vector &words, int & ZLOGE("GreaterThan wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -235,7 +230,7 @@ bool QueryHelper::HandleLessThan(const std::vector &words, int &poi ZLOGE("LessThan wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -261,7 +256,7 @@ bool QueryHelper::HandleGreaterThanOrEqualTo( ZLOGE("GreaterThanOrEqualTo wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -287,7 +282,7 @@ bool QueryHelper::HandleLessThanOrEqualTo( ZLOGE("LessThanOrEqualTo wrong type."); return false; } - pointer += 4; // Pointer goes to next keyword + pointer += 4; // 4 Pointer goes to next keyword return true; } @@ -299,7 +294,7 @@ bool QueryHelper::HandleIsNull(const std::vector &words, int &point } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.IsNull(StringToString(fieldName)); - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } @@ -311,7 +306,7 @@ bool QueryHelper::HandleIsNotNull(const std::vector &words, int &po } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.IsNotNull(StringToString(fieldName)); - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } @@ -382,7 +377,7 @@ bool QueryHelper::HandleLike(const std::vector &words, int &pointer const std::string &fieldName = words.at(pointer + 1); // fieldName const std::string &fieldValue = words.at(pointer + 2); // fieldValue dbQuery.Like(StringToString(fieldName), StringToString(fieldValue)); - pointer += 3; // Pointer goes to next keyword + pointer += 3; // 3 Pointer goes to next keyword return true; } @@ -395,7 +390,7 @@ bool QueryHelper::HandleNotLike(const std::vector &words, int &poin const std::string &fieldName = words.at(pointer + 1); // fieldName const std::string &fieldValue = words.at(pointer + 2); // fieldValue dbQuery.NotLike(StringToString(fieldName), StringToString(fieldValue)); - pointer += 3; // Pointer goes to next keyword + pointer += 3; // 3 Pointer goes to next keyword return true; } @@ -421,7 +416,7 @@ bool QueryHelper::HandleOrderByAsc(const std::vector &words, int &p } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.OrderBy(StringToString(fieldName), true); - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } @@ -433,7 +428,7 @@ bool QueryHelper::HandleOrderByDesc(const std::vector &words, int & } const std::string &fieldName = words.at(pointer + 1); // fieldName dbQuery.OrderBy(StringToString(fieldName), false); - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } @@ -446,7 +441,7 @@ bool QueryHelper::HandleLimit(const std::vector &words, int &pointe const int number = StringToInt(words.at(pointer + 1)); // number const int offset = StringToInt(words.at(pointer + 2)); // offset dbQuery.Limit(number, offset); - pointer += 3; // Pointer goes to next keyword + pointer += 3; // 3 Pointer goes to next keyword return true; } @@ -473,7 +468,7 @@ bool QueryHelper::HandleKeyPrefix(const std::vector &words, int &po const std::string &prefix = deviceId_ + StringToString(words.at(pointer + 1)); // prefix const std::vector prefixVector(prefix.begin(), prefix.end()); dbQuery.PrefixKey(prefixVector); - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } @@ -532,7 +527,7 @@ bool QueryHelper::HandleDeviceId(const std::vector &words, int &poi } else { ZLOGD("Join deviceId with user specified prefixkey later."); } - pointer += 2; // Pointer goes to next keyword + pointer += 2; // 2 Pointer goes to next keyword return true; } -- Gitee