diff --git a/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h b/frameworks/innerkitsimpl/kvdb/include/kvdb_service.h index ab29e3be27face4a0c1187bb3271a7b000e41e2c..548490ebca6a7c5835e64eb7bbb1c536e31ea866 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 2f77e642075a517c4038da13011fda5174e27043..fb513f2351a69af4cf0487058aa3d11b51e0b082 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 6c482b5727501081fe9617db7de5df250ee2b507..92b6d9a4d3225d2f8d16e403ffc757f7ba8983ba 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 71902cb79fc8cb1e198303fceff6bd0c5dc8a471..452f757de8362a814b87d43eb7231e6ea25f622f 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 44c27a33d01f85cfa3d007ecf6342cc9eca0139a..ba96bae6115b452064f618edbc9c7bc40ce8ed52 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 2b84024d5395e7931548473070b98fd20ca634cb..61b571a643b48f88b0a55bfa62588188ac0cfb21 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/innerkitsimpl/object/include/iobject_service.h b/frameworks/innerkitsimpl/object/include/iobject_service.h index b31ecc8e2aa69d2c86cac9da50ed91054d6ccabf..f94810aeb6dbd001c2f82c4e7cc12e5ebe817585 100644 --- a/frameworks/innerkitsimpl/object/include/iobject_service.h +++ b/frameworks/innerkitsimpl/object/include/iobject_service.h @@ -20,7 +20,6 @@ #include #include "object_service.h" - namespace OHOS::DistributedObject { class IObjectService : public ObjectService, public IRemoteBroker { public: diff --git a/frameworks/innerkitsimpl/rdb/include/irdb_service.h b/frameworks/innerkitsimpl/rdb/include/irdb_service.h index f5ecc638e1dd1c470b6def4e2f40aaa8c0b7b6f8..5e0e3948dd818006c522dce706fbb88a5235419c 100644 --- a/frameworks/innerkitsimpl/rdb/include/irdb_service.h +++ b/frameworks/innerkitsimpl/rdb/include/irdb_service.h @@ -21,7 +21,6 @@ #include #include "rdb_service.h" #include "rdb_types.h" - namespace OHOS::DistributedRdb { class IRdbService : public RdbService, public IRemoteBroker { public: diff --git a/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/js_single_kv_store.h index 979c535e4dd78e38e2253341e2e6d89e7c16384a..ffa818eb30172b8ff023dbb5046f771770fb4072 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 bfb23ba2e9bd24be32ab5484c190b0843838df93..3a1939f1e2c9d8229513b52db42ae496458c2735 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 46af583c054f84b8f18856e15c598fca994c3653..f8068afd152b2cd2581d191b145eeecdd3eb2db4 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 73dac1651cecfeac518cb61fcd01b8056e4900e0..d93127925e09b05343b0daffcdbc0a58dfd13098 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 f020bbfaa897dc73e2119b7a735453f14dbcf094..6ca66eddc9e38bdd98301bfe3811657cbc19d13e 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 313d552d495f8a04bc0801d08855ab22f4be536a..abcf1d0fab88c5db3b2496c7ee7007da358f4e88 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/src/query_helper.h b/services/distributeddataservice/app/src/query_helper.h deleted file mode 100644 index d39df548dc21a4a052e0c38092d83c0ab663f2d5..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/app/src/query_helper.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef QUERY_HELPER_H -#define QUERY_HELPER_H - -#include -#include "query.h" -#include "types.h" - -namespace OHOS::DistributedKv { -class QueryHelper { -public: - static DistributedDB::Query StringToDbQuery(const std::string &query, bool &isSuccess); - -private: - static std::string deviceId_; - static bool hasPrefixKey_; - static bool hasInKeys_; - static void Handle(const std::vector &words, int &pointer, - const int &end, bool &isSuccess, DistributedDB::Query &dbQuery); - static void HandleExtra(const std::vector &words, int &pointer, - 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 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); -}; -} // namespace OHOS::DistributedKv -#endif // QUERY_HELPER_H diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index 4dd21a166540859e97d093c953f889d426b45b66..8121f155cca46cf90a7bbf9e1721cc91934d88f8 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 65974bb8a395a933a34397ef8c32f57d2922a65e..f0b0c2a0e1ae262cc5deec328731aec1bb26a14a 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", diff --git a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp index 3ed804c7e3731317d0a8799a2a87b31e79a766c2..406de4f4d70b9f6697b1eb0cfead46dd1ed8c691 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_backup_test.cpp @@ -12,19 +12,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include +#include #include -#include #include -#include "bootstrap.h" +#include +#include + #include "backup_handler.h" +#include "bootstrap.h" +#include "communication_provider.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 +46,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 +54,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/include/metadata/capability_meta_data.h b/services/distributeddataservice/framework/include/metadata/capability_meta_data.h index f2dadaf3c558b8c94ca65aa6aee8d72c918ee926..702a362a110eca69b46c64fe9806de63836425a6 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 e6c207293546adae87a32b089c98005d489ec55c..7ffdaca673c3da522156b9a43dca0b65e8c71901 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 4091a93495210d943ea4521efb512e3a30523ba1..ab81aaeb816f2668dda522b4aaa898d31c3ea4c9 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/secret_key_meta_data.cpp b/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp index 41599d383a50247bfcca9e99f3e272a602d8e458..eebe8dcf8815b5343a14c58f578ddee7c56f5b5a 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/framework/metadata/user_meta_data.cpp b/services/distributeddataservice/framework/metadata/user_meta_data.cpp index d19b9c8b1f7e38b3c9d4f0752e391466a97db096..e1452ef4cb55c4d0a1228ea11138e9eeb4d5e769 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/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index a44ebca5e7b324caa22cb9a438fdfaf704c614b4..5b067b6b81d556befaff9fa544bb95853ca6ca4c 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 d63d9eba09cf50be64ae85068bb969124da5ec7f..d0d425d27ebabf83f757745ce6606e5250401f28 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,86 @@ 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 +175,83 @@ 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 8c13705cebb06f45bbe699b83f986c2164193d95..681d27b4b6a9f51fec4e7dda758f85f0f95ffc96 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 7a86e8b733c504de60cb1be48b15293f87e06944..afe89fafb54c9ab69e1081b6112072e0ef07e29b 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 99% rename from services/distributeddataservice/app/src/kvstore_sync_manager.cpp rename to services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp index 746477abef4a39ea16c029f046b1cc3d87fd0b08..8fa819f5c78539e80bd3b3f8710c74bb4bfa382f 100644 --- a/services/distributeddataservice/app/src/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/app/src/kvstore_sync_manager.h b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h similarity index 77% rename from services/distributeddataservice/app/src/kvstore_sync_manager.h rename to services/distributeddataservice/service/kvdb/kvstore_sync_manager.h index 15b142b104110b6012047eca391461e08ae0663a..25db29e10780353740adef8e280bdd2c3cf8beb0 100644 --- a/services/distributeddataservice/app/src/kvstore_sync_manager.h +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h @@ -16,19 +16,20 @@ #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 { -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; - 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/app/src/query_helper.cpp b/services/distributeddataservice/service/kvdb/query_helper.cpp similarity index 62% rename from services/distributeddataservice/app/src/query_helper.cpp rename to services/distributeddataservice/service/kvdb/query_helper.cpp index b197b61efd19869aef0900bab788bfaf07c33ff9..088f071df0566697c2e8cb1ecb0c85e6a5829a4b 100644 --- a/services/distributeddataservice/app/src/query_helper.cpp +++ b/services/distributeddataservice/service/kvdb/query_helper.cpp @@ -12,31 +12,26 @@ * 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 "kvstore_utils.h" #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_{}; +constexpr int MAX_QUERY_COMPLEXITY = 500; // Max query complexity 500 +bool QueryHelper::hasPrefixKey_ = false; +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 +49,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 +151,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 + pointer += 4; // 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 +178,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 + pointer += 4; // 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 +203,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 + pointer += 4; // 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 +228,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 + pointer += 4; // 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 +254,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 + pointer += 4; // 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 +280,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 + pointer += 4; // 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 + pointer += 2; // 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 + pointer += 2; // 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 +333,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 +362,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 + pointer += 3; // 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 + pointer += 3; // 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 + pointer += 2; // 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 + pointer += 2; // 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 + pointer += 3; // 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 + pointer += 2; // 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 +493,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 +527,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 + pointer += 2; // 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 +564,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 +578,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 +587,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 +612,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 +632,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 +652,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 new file mode 100644 index 0000000000000000000000000000000000000000..78db7b9f5369cd24894fa5ef9a90f86dc83be2a5 --- /dev/null +++ b/services/distributeddataservice/service/kvdb/query_helper.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef QUERY_HELPER_H +#define QUERY_HELPER_H + +#include + +#include "query.h" +#include "types.h" + +namespace OHOS::DistributedKv { +class QueryHelper { +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 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, 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 diff --git a/services/distributeddataservice/service/object/object_manager.cpp b/services/distributeddataservice/service/object/object_manager.cpp index 88e389d24578835e90c80ca62d18c2171df0cc1a..9162f49e12dbbb2224aa1b2634f413d8fa8dbb14 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 {