diff --git a/bundle.json b/bundle.json index 9ed5012f5a0135890adbefb35f52bf5db0a9ee96..6983df085d3c631feb4f626ed1e42d4f4cb418c0 100644 --- a/bundle.json +++ b/bundle.json @@ -109,7 +109,8 @@ "visibility.h", "data_query.h", "device_status_change_listener.h", - "store_errno.h" + "store_errno.h", + "kvdbstore_datashare_native.h" ], "header_base": "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include" } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h b/frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h new file mode 100644 index 0000000000000000000000000000000000000000..be5e7c58d33a0aaf2730735989b54046ce4f1c5a --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KVDBSTORE_RESULT_SET_H +#define KVDBSTORE_RESULT_SET_H + +#include "datashare_abstract_result_set.h" +#include "kvstore_result_set.h" +#include "single_kvstore.h" +#include "datashare_errno.h" + +namespace OHOS { +namespace DistributedKv { +using namespace DataShare; +class KvDBStoreResultSet : public DataShareAbstractResultSet +{ +public: + KvDBStoreResultSet(std::shared_ptr kvResultSet); + + ~KvDBStoreResultSet() = default; + + int GetRowCount(int &count) override; + + int GetAllColumnOrKeyName(std::vector &columnOrKeyNames) override; + + bool OnGo(int oldRowIndex, int newRowIndex, const std::shared_ptr &writer) override; + +private: + int Count(); + + bool FillBlock(int startRowIndex, const std::shared_ptr &writer); + + static constexpr int INVALID_COUNT = -1; + + int resultRowCount {INVALID_COUNT}; + + std::shared_ptr kvResultSet_; +}; +} // namespace DistributedKv +} // namespace OHOS +#endif // KVDBSTORE_RESULT_SET_H + diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h new file mode 100644 index 0000000000000000000000000000000000000000..a70f3593c3de639afb22ea4f1ddead0780443acd --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef KVSTORE_PREDICATE_H +#define KVSTORE_PREDICATE_H + +#include +#include "data_query.h" +#include "types.h" +#include "datashare_predicates.h" + +namespace OHOS { +namespace DistributedKv { +using namespace DataShare; + +class KvStorePredicate +{ +public: + KvStorePredicate(const DataSharePredicates &predicates); + + ~KvStorePredicate() = default; + + Status ToQuery(DataQuery &query); + +private: + Status InKeys(const OperationItem &oper, DataQuery &query); + + Status KeyPrefix(const OperationItem &oper, DataQuery &query); + + using QueryHandler = Status (KvStorePredicate::*)(const OperationItem &, DataQuery &); + + static const std::map HANDLERS; + + DataSharePredicates predicates_; +}; +}// namespace DistributedKv +}//namespace + +#endif // KVSTORE_PREDICATE_H + + diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc875b7e409b48b97b256ae6cb0f578b2f314de1 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "kvDBDataShareNative" + + +#include "kvdbstore_result_set.h" +#include "kvstore_predicates.h" +#include "kvdbstore_datashare_native.h" +#include "log_print.h" +#include "data_query.h" + +namespace OHOS { +namespace DistributedKv { + +kvDBDataShareNative::kvDBDataShareNative(std::shared_ptr kvStoreClient) + :kvStoreClient_(kvStoreClient) {}; + +std::shared_ptr kvDBDataShareNative::GetDataShareResult(const DataSharePredicates &predicate) +{ + if(kvStoreClient_ == nullptr) + { + ZLOGE("kvStoreClient_ nullptr"); + return nullptr; + } + + DataQuery query; + auto kvPredicates = std::make_shared(predicate); + Status status = kvPredicates->ToQuery(query); + if(status != Status::SUCCESS) + { + ZLOGE("ToQuery failed: %{public}d", status); + return nullptr; + } + + std::shared_ptr resultSet; + status = kvStoreClient_->GetResultSetWithQuery(query, resultSet); + if (status != Status::SUCCESS) + { + ZLOGE("GetResultSetWithQuery failed: %{public}d", status); + return nullptr; + } + + return std::make_shared(resultSet); +} +} // namespace DistributedKv +} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d3d46ebdccabe631fd9021845ce859a5ed94d42 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "KvDBStoreResultSet" + +#include "constant.h" +#include "log_print.h" +#include "kvdbstore_result_set.h" + +namespace OHOS { +namespace DistributedKv { +KvDBStoreResultSet::KvDBStoreResultSet(std::shared_ptr kvResultSet) + :kvResultSet_(kvResultSet) {}; + +int KvDBStoreResultSet::GetRowCount(int &count) +{ + count = Count(); + return count == INVALID_COUNT ? E_ERROR : E_OK; +} + +int KvDBStoreResultSet::GetAllColumnOrKeyName(std::vector &columnOrKeyNames) +{ + columnOrKeyNames.clear(); + columnOrKeyNames.emplace_back("key", "value"); + return E_OK; +} + +bool KvDBStoreResultSet::FillBlock(int startRowIndex, const std::shared_ptr &writer) +{ + if (kvResultSet_ == nullptr) + { + ZLOGE("kvResultSet_ nullptr"); + return false; + } + + bool isMoved = kvResultSet_->MoveToPosition(startRowIndex); + if (!isMoved) + { + ZLOGE("MoveToPosition failed"); + return false; + } + + Entry entry; + Status status = kvResultSet_->GetEntry(entry); + if (status != Status::SUCCESS) + { + ZLOGE("GetEntry failed %{public}d", status); + return false; + } + + int clearStatus = writer->Clear(); + if(clearStatus != E_OK){ + ZLOGE("clear writer failed: %{public}d", clearStatus); + return false; + } + + if ((entry.key.Size() + entry.value.Size()) > (writer->Size() - writer->GetUsedBytes())) + { + ZLOGE("not enought memory; entry size: %{public}d, writer size: %{public}d", + entry.key.Size() + entry.value.Size(), writer->Size() - writer->GetUsedBytes()); + return false; + } + // uint8_t + int keyStatus = writer->WriteBlob((uint32_t)startRowIndex, 0, &entry.key.Data(), entry.key.Size()); + if(keyStatus != E_OK) + { + ZLOGE("WriteBlob key error: %{public}d", keyStatus); + return false; + } + + int valueStatus = writer->WriteBlob((uint32_t)startRowIndex, 1, &entry.value.Data(), entry.value.Size()); + if(valueStatus != E_OK) + { + ZLOGE("WriteBlob value error: %{public}d", valueStatus); + return false; + } + return true; +} + +int KvDBStoreResultSet::Count() +{ + if (kvResultSet_ == nullptr) + { + ZLOGE("kvResultSet_ nullptr"); + return INVALID_COUNT; + } + if (resultRowCount != INVALID_COUNT) + { + return resultRowCount; + } + int count = kvResultSet_->GetCount(); + if (count < 0) + { + ZLOGE("kvResultSet count invalid: %{public}d", count); + return INVALID_COUNT; + } + resultRowCount = count; + return count; +} + +bool KvDBStoreResultSet::OnGo(int oldRowIndex, int newRowIndex, const std::shared_ptr &writer) +{ + if (writer == nullptr || newRowIndex >= Count()) + { + ZLOGE("invalid writer: {public} or newRowIndex: %{public}d", newRowIndex); + return false; + } + return FillBlock(newRowIndex, writer); +} +} // namespace DistributedKv +} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e28b0ad247148aa3db0a1020689d19146380e19d --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "KvStorePredicate" + +#include "kvstore_predicates.h" +#include "log_print.h" +#include "datashare_errno.h" + +namespace OHOS { +namespace DistributedKv { + +const std::map KvStorePredicate::HANDLERS = { + {IN_KEY, &KvStorePredicate::InKeys}, + {KEY_PREFIX, &KvStorePredicate::KeyPrefix} +}; + +KvStorePredicate::KvStorePredicate(const DataSharePredicates &predicates) + :predicates_(predicates){}; + +Status KvStorePredicate::ToQuery(DataQuery &query) +{ + std::list operationList = predicates_.GetOperationList(); + for(const auto &oper : operationList) + { + auto it = HANDLERS.find(oper.operation); + if(it == HANDLERS.end()) + { + ZLOGE("This feature is not supported"); + return Status::NOT_SUPPORT; + } + Status status = (this->*(it->second))(oper, query); + if(status != Status::SUCCESS){ + ZLOGE("ToQuery called failed"); + return status; + } + } + return Status::SUCCESS; +} + +Status KvStorePredicate::InKeys(const OperationItem &oper, DataQuery &query) +{ + std::vector keys; + int status = oper.para1.GetStringVector(keys); + if(status != E_OK){ + ZLOGE("GetStringVector failed"); + return Status::ERROR; + } + query.InKeys(keys); + return Status::SUCCESS; +} + +Status KvStorePredicate::KeyPrefix(const OperationItem &oper, DataQuery &query) +{ + std::string prefix; + int status = oper.para1.GetString(prefix); + if(status != E_OK){ + ZLOGE("KeyPrefix failed"); + return Status::ERROR; + } + query.KeyPrefix(prefix); + return Status::SUCCESS; +} +}// namespace DistributedKv +}// namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 712faae1f6dce8ae54ca31d4d015a9c56eb4f43f..9e9050639fa1bc305d6425bc5eca80ec5e5f4427 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -31,6 +31,9 @@ config("distributeddatafwk_config") { "//utils/system/safwk/native/include", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", + "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share/provider/include", + "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share/common/include", + "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common/include" ] } @@ -71,6 +74,9 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp", "include/types.h", ] @@ -89,6 +95,8 @@ ohos_shared_library("distributeddata_inner") { "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//utils/native/base:utils", + "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common:datashare_common" + # "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share:datashare_common" ] external_deps = [ "hiviewdfx_hilog_native:libhilog", diff --git a/interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h b/interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h new file mode 100644 index 0000000000000000000000000000000000000000..64177e6766e9a5f6e5e2dba3f2df0c97c8ba52d0 --- /dev/null +++ b/interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KVDBSTORE_DATASHARE_NATIVE_H +#define KVDBSTORE_DATASHARE_NATIVE_H + +#include "types.h" +#include "single_kvstore.h" +#include "datashare_predicates.h" +#include "data_query.h" +#include "datashare_abstract_result_set.h" + +namespace OHOS { +namespace DistributedKv { +using namespace DataShare; +class kvDBDataShareNative { + +public: + kvDBDataShareNative(std::shared_ptr kvStoreClient); + + ~kvDBDataShareNative() = default; + + std::shared_ptr GetDataShareResult(const DataSharePredicates &predicate); + +private: + std::shared_ptr kvStoreClient_ = nullptr; + +}; +} // namespace DistributedKv +} // namespace OHOS +#endif // KVDBSTORE_DATASHARE_NATIVE_H \ No newline at end of file