From 4d63b6c5ca5cd486d2f51c6fae3f1709e4805e38 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Fri, 21 Jan 2022 18:59:37 +0800 Subject: [PATCH 1/7] refactor distributed rdb code Signed-off-by: wuchunbo --- frameworks/common/include/concurrent_map.h | 244 +++++++++++++++ frameworks/common/include/itypes_util.h | 164 ++++++++++ frameworks/common/src/itypes_util.cpp | 292 ++++++++++++++++++ .../include/ikvstore_data_service.h | 9 +- .../src/ikvstore_data_service.cpp | 4 +- .../distributeddatafwk/src/rdb/rdb_parcel.cpp | 84 ----- .../innerkitsimpl/rdb/include/rdb_manager.h | 38 +++ .../irdb_store.h => rdb/include/rdb_syncer.h} | 23 +- .../rdb_parcel.h => rdb/include/rdb_types.h} | 48 ++- .../include/rdb => rdb/src}/irdb_service.h | 17 +- .../innerkitsimpl/rdb/src/irdb_syncer.h | 36 +++ .../src}/rdb_client_death_recipient.cpp | 2 +- .../src}/rdb_client_death_recipient.h | 4 +- .../innerkitsimpl/rdb/src/rdb_manager.cpp | 34 ++ .../rdb/src/rdb_manager_impl.cpp | 176 +++++++++++ .../innerkitsimpl/rdb/src/rdb_manager_impl.h | 68 ++++ .../innerkitsimpl/rdb/src/rdb_service.h | 28 +- .../src/rdb => rdb/src}/rdb_service_proxy.cpp | 14 +- .../rdb => rdb/src}/rdb_service_proxy.h | 9 +- .../src/rdb => rdb/src}/rdb_service_stub.cpp | 19 +- .../rdb => rdb/src}/rdb_service_stub.h | 15 +- .../src/rdb_syncer_proxy.cpp} | 16 +- .../src/rdb_syncer_proxy.h} | 14 +- .../src/rdb_syncer_stub.cpp} | 16 +- .../src/rdb_syncer_stub.h} | 16 +- interfaces/innerkits/distributeddata/BUILD.gn | 26 +- .../adapter/account/BUILD.gn | 1 + .../account/src/account_delegate_impl.cpp | 11 +- .../test/unittest/concurrent_map_test.cpp | 72 ----- .../adapter/include/autils/concurrent_map.h | 101 ------ .../adapter/include/log/log_print.h | 4 + services/distributeddataservice/app/BUILD.gn | 14 +- .../app/src/kvstore_data_service.cpp | 7 +- .../app/src/kvstore_data_service.h | 10 +- .../distributeddataservice/app/test/BUILD.gn | 24 +- .../test/unittest/kvstore_data_service.cpp | 4 +- .../app/test/unittest/rdb_service_impl.h | 28 ++ .../rdb_device_syncer.cpp} | 38 +-- .../rdb_device_syncer.h} | 26 +- .../rdb_service_impl.cpp} | 91 ++---- .../rdb_service.h => rdb/rdb_service_impl.h} | 20 +- .../rdb_syncer_factory.cpp} | 41 ++- services/rdb/rdb_syncer_factory.h | 76 +++++ .../rdb_store.cpp => rdb/rdb_syncer_impl.cpp} | 26 +- .../rdb/rdb_store.h => rdb/rdb_syncer_impl.h} | 24 +- 45 files changed, 1452 insertions(+), 582 deletions(-) create mode 100644 frameworks/common/include/concurrent_map.h create mode 100644 frameworks/common/include/itypes_util.h create mode 100644 frameworks/common/src/itypes_util.cpp delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp create mode 100644 frameworks/innerkitsimpl/rdb/include/rdb_manager.h rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb/irdb_store.h => rdb/include/rdb_syncer.h} (60%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb/rdb_parcel.h => rdb/include/rdb_types.h} (58%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb => rdb/src}/irdb_service.h (68%) create mode 100644 frameworks/innerkitsimpl/rdb/src/irdb_syncer.h rename frameworks/innerkitsimpl/{distributeddatafwk/src/rdb => rdb/src}/rdb_client_death_recipient.cpp (97%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb => rdb/src}/rdb_client_death_recipient.h (92%) create mode 100644 frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp create mode 100644 frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp create mode 100644 frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h rename services/distributeddataservice/app/src/rdb/rdb_store_factory.h => frameworks/innerkitsimpl/rdb/src/rdb_service.h (59%) rename frameworks/innerkitsimpl/{distributeddatafwk/src/rdb => rdb/src}/rdb_service_proxy.cpp (80%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb => rdb/src}/rdb_service_proxy.h (83%) rename frameworks/innerkitsimpl/{distributeddatafwk/src/rdb => rdb/src}/rdb_service_stub.cpp (84%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb => rdb/src}/rdb_service_stub.h (73%) rename frameworks/innerkitsimpl/{distributeddatafwk/src/rdb/rdb_store_proxy.cpp => rdb/src/rdb_syncer_proxy.cpp} (70%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb/rdb_store_proxy.h => rdb/src/rdb_syncer_proxy.h} (69%) rename frameworks/innerkitsimpl/{distributeddatafwk/src/rdb/rdb_store_stub.cpp => rdb/src/rdb_syncer_stub.cpp} (72%) rename frameworks/innerkitsimpl/{distributeddatafwk/include/rdb/rdb_store_stub.h => rdb/src/rdb_syncer_stub.h} (68%) delete mode 100755 services/distributeddataservice/adapter/autils/test/unittest/concurrent_map_test.cpp delete mode 100644 services/distributeddataservice/adapter/include/autils/concurrent_map.h create mode 100644 services/distributeddataservice/app/test/unittest/rdb_service_impl.h rename services/{distributeddataservice/app/src/rdb/rdb_device_store.cpp => rdb/rdb_device_syncer.cpp} (73%) rename services/{distributeddataservice/app/src/rdb/rdb_device_store.h => rdb/rdb_device_syncer.h} (70%) rename services/{distributeddataservice/app/src/rdb/rdb_service.cpp => rdb/rdb_service_impl.cpp} (48%) rename services/{distributeddataservice/app/src/rdb/rdb_service.h => rdb/rdb_service_impl.h} (77%) rename services/{distributeddataservice/app/src/rdb/rdb_store_factory.cpp => rdb/rdb_syncer_factory.cpp} (50%) create mode 100644 services/rdb/rdb_syncer_factory.h rename services/{distributeddataservice/app/src/rdb/rdb_store.cpp => rdb/rdb_syncer_impl.cpp} (69%) rename services/{distributeddataservice/app/src/rdb/rdb_store.h => rdb/rdb_syncer_impl.h} (69%) diff --git a/frameworks/common/include/concurrent_map.h b/frameworks/common/include/concurrent_map.h new file mode 100644 index 000000000..55967f1d6 --- /dev/null +++ b/frameworks/common/include/concurrent_map.h @@ -0,0 +1,244 @@ +/* + * 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 DISTRIBUTED_DATA_FRAMEWORKS_COMMON_CONCURRENT_MAP_H +#define DISTRIBUTED_DATA_FRAMEWORKS_COMMON_CONCURRENT_MAP_H + +#include +#include +#include + +namespace OHOS { +template +class ConcurrentMap { +public: + using key_type = typename std::map::key_type; + using mapped_type = typename std::map::mapped_type; + using value_type = typename std::map::value_type; + using size_type = typename std::map::size_type; + using reference = typename std::map::reference; + using const_reference = typename std::map::const_reference; + using Predicate = std::function; + + ConcurrentMap() = default; + ~ConcurrentMap() = default; + ConcurrentMap(const ConcurrentMap &other) + { + operator=(std::move(other)); + } + + ConcurrentMap &operator=(const ConcurrentMap &other) noexcept + { + if (this == &other) { + return *this; + } + auto tmp = other.Clone(); + std::lock_guard lock(mutex_); + entries_ = std::move(tmp); + return *this; + } + + ConcurrentMap(ConcurrentMap &&other) noexcept + { + operator=(std::move(other)); + } + + ConcurrentMap &operator=(ConcurrentMap &&other) noexcept + { + if (this == &other) { + return *this; + } + auto tmp = other.Steal(); + std::lock_guard lock(mutex_); + entries_ = std::move(tmp); + return *this; + } + + template + bool Emplace(_Args &&...__args) noexcept + { + std::lock_guard lock(mutex_); + auto it = entries_.emplace(std::forward<_Args>(__args)...); + return it->second; + } + + std::pair Find(const key_type &key) const noexcept + { + std::lock_guard lock(mutex_); + auto it = entries_.find(key); + if (it == entries_.end()) { + return std::pair{ false, mapped_type() }; + } + + return std::pair{ true, it->second }; + } + + bool Contains(const key_type& key) const noexcept + { + std::lock_guard lock(mutex_); + return (entries_.find(key) != entries_.end()); + } + + bool Insert(const key_type &key, const mapped_type &value) noexcept + { + std::lock_guard lock(mutex_); + auto it = entries_.insert(value_type{key, value}); + return it.second; + } + + size_type Erase(const key_type &key) noexcept + { + std::lock_guard lock(mutex_); + return entries_.erase(key); + } + + size_type EraseAllIf(const Predicate& predicate) noexcept + { + size_type count = 0; + if (predicate == nullptr) { + return 0; + } + for (auto it = entries_.begin(); it != entries_.end();) { + if (predicate(it->first, it->second)) { + entries_.erase(it++); + count++; + continue; + } + it++; + } + return count; + } + + void Clear() noexcept + { + std::lock_guard lock(mutex_); + return entries_.clear(); + } + + bool Empty() const noexcept + { + std::lock_guard lock(mutex_); + return entries_.empty(); + } + + size_type Size() const noexcept + { + std::lock_guard lock(mutex_); + return entries_.size(); + } + + void ClearIf(std::function &action) noexcept + { + if (action == nullptr) { + return; + } + std::lock_guard lock(mutex_); + for (auto &[key, value] : entries_) { + if (action(key, value)) { + break; + } + } + return entries_.size(); + } + + mapped_type &operator[](const key_type &key) noexcept + { + std::lock_guard lock(mutex_); + return entries_[key]; + } + + const mapped_type &operator[](const key_type &key) const noexcept + { + std::lock_guard lock(mutex_); + return entries_[key]; + } + + void ForEach(const std::function &action) + { + if (action == nullptr) { + return; + } + std::lock_guard lock(mutex_); + for (auto &[key, value] : entries_) { + if (action(key, value)) { + break; + } + } + } + + bool Compute(const key_type &key, const std::function &action) + { + if (action == nullptr) { + return false; + } + std::lock_guard lock(mutex_); + auto it = entries_.find(key); + if (it == entries_.end()) { + auto result = entries_.emplace(key, mapped_type()); + it = result.second ? result.first : entries_.end(); + } + if (it == entries_.end()) { + return false; + } + action(it->first, it->second); + return true; + } + + bool ComputeIfPresent(const key_type &key, const std::function &action) + { + if (action == nullptr) { + return false; + } + std::lock_guard lock(mutex_); + auto it = entries_.find(key); + if (it == entries_.end()) { + return false; + } + action(key, it->second); + return true; + } + + mapped_type ComputeIfAbsent(const key_type &key, const std::function &action) + { + if (action == nullptr) { + return mapped_type(); + } + std::lock_guard lock(mutex_); + auto it = entries_.find(key); + if (it != entries_.end()) { + return it->second; + } + mapped_type value = action(key); + entries_.insert(value_type{key, value}); + return value; + } +private: + std::map Steal() noexcept + { + std::lock_guard lock(mutex_); + return std::move(entries_); + } + + std::map Clone() const noexcept + { + std::lock_guard lock(mutex_); + return entries_; + } +private: + mutable std::recursive_mutex mutex_; + std::map entries_; +}; +} +#endif diff --git a/frameworks/common/include/itypes_util.h b/frameworks/common/include/itypes_util.h new file mode 100644 index 000000000..ded6a2671 --- /dev/null +++ b/frameworks/common/include/itypes_util.h @@ -0,0 +1,164 @@ +/* + * 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 DISTRIBUTED_DATA_FRAMEWORKS_COMMON_ITYPES_UTIL_H +#define DISTRIBUTED_DATA_FRAMEWORKS_COMMON_ITYPES_UTIL_H + +#include +#include "types.h" +#include "change_notification.h" +#include "message_parcel.h" +#include "rdb_types.h" + +namespace OHOS::DistributedKv { +class ITypesUtil final { +public: + static bool Marshalling(const Blob &blob, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, Blob &output); + + static bool Marshalling(const std::vector &blobs, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, std::vector &output); + + static bool Marshalling(const Entry &entry, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, Entry &output); + + static bool Marshalling(const std::vector &entry, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, std::vector &output); + + static bool Marshalling(const DeviceInfo &entry, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, DeviceInfo &output); + + static bool Marshalling(const std::vector &input, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, std::vector &output); + + static bool Marshalling(const ChangeNotification ¬ification, MessageParcel &parcel); + static bool Unmarshalling(MessageParcel &parcel, ChangeNotification &output); + + static bool Marshalling(const DistributedRdb::RdbSyncerParam& param, MessageParcel& parcel); + static bool UnMarshalling(MessageParcel& parcel, DistributedRdb::RdbSyncerParam& param); + + static int64_t GetTotalSize(const std::vector &entries); + static int64_t GetTotalSize(const std::vector &entries); + + template + static Status MarshalToBuffer(const T &input, int size, MessageParcel &data) + { + std::unique_ptr buffer = std::make_unique(size); + if (!data.WriteBool(buffer != nullptr)) { + return Status::IPC_ERROR; + } + if (buffer == nullptr) { + return Status::ILLEGAL_STATE; + } + uint8_t *cursor = buffer.get(); + if (!input.WriteToBuffer(cursor, size)) { + return Status::IPC_ERROR; + } + return data.WriteRawData(buffer.get(), size) ? Status::SUCCESS : Status::IPC_ERROR; + } + + template + static Status MarshalToBuffer(const std::vector &input, int size, MessageParcel &data) + { + std::unique_ptr buffer = std::make_unique(size); + if (!data.WriteBool(buffer != nullptr)) { + return Status::IPC_ERROR; + } + if (buffer == nullptr) { + return Status::ILLEGAL_STATE; + } + uint8_t *cursor = buffer.get(); + for (const auto &entry : input) { + if (!entry.WriteToBuffer(cursor, size)) { + return Status::IPC_ERROR; + } + } + if (!data.WriteInt32(input.size())) { + return Status::IPC_ERROR; + } + return data.WriteRawData(buffer.get(), size) ? Status::SUCCESS : Status::IPC_ERROR; + } + + template + static Status UnmarshalFromBuffer(MessageParcel &data, int size, T &output) + { + if (size < 0) { + return Status::INVALID_ARGUMENT; + } + if (!data.ReadBool()) { + return Status::ILLEGAL_STATE; + } + const uint8_t *buffer = reinterpret_cast(data.ReadRawData(size)); + if (buffer == nullptr) { + return Status::INVALID_ARGUMENT; + } + return output.ReadFromBuffer(buffer, size) ? Status::SUCCESS : Status::IPC_ERROR; + } + + template + static Status UnmarshalFromBuffer(MessageParcel &data, int size, std::vector &output) + { + if (size < 0) { + return Status::INVALID_ARGUMENT; + } + if (!data.ReadBool()) { + return Status::ILLEGAL_STATE; + } + int count = data.ReadInt32(); + const uint8_t *buffer = reinterpret_cast(data.ReadRawData(size)); + if (count < 0 || buffer == nullptr) { + return Status::INVALID_ARGUMENT; + } + + output.resize(count); + for (auto &entry : output) { + if (!entry.ReadFromBuffer(buffer, size)) { + output.clear(); + return Status::IPC_ERROR; + } + } + return Status::SUCCESS; } +private: + template + class VectorParcel : public MessageParcel { + public: + bool Writer(const T &entry) { return ITypesUtil::Marshalling(entry, *this); } + bool Reader(T &entry) { return ITypesUtil::Unmarshalling(*this, entry); } + }; + + template + static bool ReadVector(Parcel &parcel, std::vector &val, bool (Parcel::*read)(T &)); + template + static bool WriteVector(Parcel &parcel, const std::vector &val, bool (Parcel::*writer)(const T &)); + template using Reader = bool (Parcel::*)(T &); + template using Writer = bool (Parcel::*)(const T &); + template + static Writer GetParcelWriter() + { + return static_cast>(&VectorParcel::Writer); + } + template + static Reader GetParcelReader() + { + return static_cast>(&VectorParcel::Reader); + } + template + static std::vector Convert2Vector(const std::list &entries); + template + static std::list Convert2List(std::vector &&entries); +}; +} +#endif + diff --git a/frameworks/common/src/itypes_util.cpp b/frameworks/common/src/itypes_util.cpp new file mode 100644 index 000000000..fc5d9330c --- /dev/null +++ b/frameworks/common/src/itypes_util.cpp @@ -0,0 +1,292 @@ +/* + * 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 "ITypesUtil" + +#include "itypes_util.h" +#include +#include "autils/constant.h" +#include + +namespace OHOS::DistributedKv { +bool ITypesUtil::Marshalling(const Blob &blob, MessageParcel &data) +{ + return data.WriteUInt8Vector(blob.Data()); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, Blob &output) +{ + std::vector blob; + bool result = data.ReadUInt8Vector(&blob); + output = blob; + return result; +} + +bool ITypesUtil::Marshalling(const std::vector &blobs, MessageParcel &data) +{ + return WriteVector(data, blobs, ITypesUtil::GetParcelWriter()); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, std::vector &output) +{ + return ReadVector(data, output, ITypesUtil::GetParcelReader()); +} + +bool ITypesUtil::Marshalling(const std::vector &entry, MessageParcel &data) +{ + return WriteVector(data, entry, ITypesUtil::GetParcelWriter()); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, std::vector &output) +{ + return ReadVector(data, output, ITypesUtil::GetParcelReader()); +} + +bool ITypesUtil::Marshalling(const Entry &entry, MessageParcel &data) +{ + if (!Marshalling(entry.key, data)) { + return false; + } + return Marshalling(entry.value, data); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, Entry &output) +{ + if (!Unmarshalling(data, output.key)) { + return false; + } + return Unmarshalling(data, output.value); +} + +bool ITypesUtil::Marshalling(const DeviceInfo &entry, MessageParcel &data) +{ + if (!data.WriteString(entry.deviceId)) { + return false; + } + if (!data.WriteString(entry.deviceName)) { + return false; + } + return data.WriteString(entry.deviceType); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, DeviceInfo &output) +{ + if (!data.ReadString(output.deviceId)) { + return false; + } + if (!data.ReadString(output.deviceName)) { + return false; + } + return data.ReadString(output.deviceType); +} +bool ITypesUtil::Marshalling(const std::vector &input, MessageParcel &data) +{ + return WriteVector(data, input, ITypesUtil::GetParcelWriter()); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, std::vector &output) +{ + return ReadVector(data, output, ITypesUtil::GetParcelReader()); +} + +bool ITypesUtil::Marshalling(const ChangeNotification ¬ification, MessageParcel &parcel) +{ + if (!Marshalling(notification.GetInsertEntries(), parcel)) { + return false; + } + + if (!Marshalling(notification.GetUpdateEntries(), parcel)) { + return false; + } + + if (!Marshalling(notification.GetDeleteEntries(), parcel)) { + return false; + } + if (!parcel.WriteString(notification.GetDeviceId())) { + ZLOGE("WriteString deviceId_ failed."); + return false; + } + + return parcel.WriteBool(notification.IsClear()); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &parcel, ChangeNotification &output) +{ + std::vector insertEntries; + if (!Unmarshalling(parcel, insertEntries)) { + return false; + } + std::vector updateEntries; + if (!Unmarshalling(parcel, updateEntries)) { + return false; + } + std::vector deleteEntries; + if (!Unmarshalling(parcel, deleteEntries)) { + return false; + } + std::string deviceId; + if (!parcel.ReadString(deviceId)) { + ZLOGE("WriteString deviceId_ failed."); + return false; + } + bool isClear; + if (!parcel.ReadBool(isClear)) { + ZLOGE("WriteString deviceId_ failed."); + return false; + } + output = ChangeNotification(std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), deviceId, + isClear); + return true; +} + +bool ITypesUtil::Marshalling(const DistributedRdb::RdbSyncerParam& param, MessageParcel& parcel) +{ + if (!parcel.WriteString(param.bundleName_)) { + ZLOGE("RdbStoreParam write bundle name failed"); + return false; + } + if (!parcel.WriteString(param.path_)) { + ZLOGE("RdbStoreParam write directory failed"); + return false; + } + if (!parcel.WriteString(param.storeName_)) { + ZLOGE("RdbStoreParam write store name failed"); + return false; + } + if (!parcel.WriteInt32(param.type_)) { + ZLOGE("RdbStoreParam write type failed"); + return false; + } + if (!parcel.WriteBool(param.isAutoSync_)) { + ZLOGE("RdbStoreParam write auto sync failed"); + return false; + } + return true; +} +bool ITypesUtil::UnMarshalling(MessageParcel& parcel, DistributedRdb::RdbSyncerParam& param) +{ + if (!parcel.ReadString(param.bundleName_)) { + ZLOGE("RdbStoreParam read bundle name failed"); + return false; + } + if (!parcel.ReadString(param.path_)) { + ZLOGE("RdbStoreParam read directory failed"); + return false; + } + if (!parcel.ReadString(param.storeName_)) { + ZLOGE("RdbStoreParam read store name failed"); + return false; + } + if (!parcel.ReadInt32(param.type_)) { + ZLOGE("RdbStoreParam read type failed"); + return false; + } + if (!parcel.ReadBool(param.isAutoSync_)) { + ZLOGE("RdbStoreParam read auto sync failed"); + return false; + } + return true; +} + +template +std::vector ITypesUtil::Convert2Vector(const std::list &entries) +{ + std::vector vector(entries.size()); + int i = 0; + for (const auto &entry : entries) { + vector[i++] = entry; + } + return vector; +} + +template +std::list ITypesUtil::Convert2List(std::vector &&entries) +{ + std::list result; + for (auto &entry : entries) { + result.push_back(std::move(entry)); + } + return result; +} +int64_t ITypesUtil::GetTotalSize(const std::vector &entries) +{ + int64_t bufferSize = 1; + for (const auto &item : entries) { + if (item.key.Size() > Constant::MAX_KEY_LENGTH || item.value.Size() > Constant::MAX_VALUE_LENGTH) { + return -bufferSize; + } + bufferSize += item.key.RawSize() + item.value.RawSize(); + } + return bufferSize - 1; +} +int64_t ITypesUtil::GetTotalSize(const std::vector &entries) +{ + int64_t bufferSize = 1; + for (const auto &item : entries) { + if (item.Size() > Constant::MAX_KEY_LENGTH) { + return -bufferSize; + } + bufferSize += item.RawSize(); + } + return bufferSize - 1; +} + +template +bool ITypesUtil::ReadVector(Parcel &parcel, std::vector &val, bool (Parcel::*read)(T &)) +{ + int32_t len = parcel.ReadInt32(); + if (len < 0) { + return false; + } + + size_t readAbleSize = parcel.GetReadableBytes(); + size_t size = static_cast(len); + if ((size > readAbleSize) || (size > val.max_size())) { + return false; + } + val.resize(size); + if (val.size() < size) { + return false; + } + + for (auto &v : val) { + if (!(parcel.*read)(v)) { + return false; + } + } + + return true; +} + +template +bool ITypesUtil::WriteVector(Parcel &parcel, const std::vector &val, bool (Parcel::*writer)(const T &)) +{ + if (val.size() > INT_MAX) { + return false; + } + + if (!parcel.WriteInt32(static_cast(val.size()))) { + return false; + } + + for (auto &v : val) { + if (!(parcel.*writer)(v)) { + return false; + } + } + return true; +} +} + diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h index fcf26133a..a58f7742a 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h @@ -27,6 +27,10 @@ #include "types.h" #include "idevice_status_change_listener.h" +namespace OHOS::DistributedRdb { +class IRdbService; +} + namespace OHOS::DistributedKv { /* * IPC-friendly Options struct without std::string schema field. @@ -46,7 +50,6 @@ struct OptionsIpc { bool dataOwnership; // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT }; -class IRdbService; class IKvStoreDataService : public IRemoteBroker { public: enum { @@ -98,7 +101,7 @@ public: virtual Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy) = 0; virtual Status StopWatchDeviceChange(sptr observer) = 0; - virtual sptr GetRdbService() = 0; + virtual sptr GetRdbService() = 0; }; class KvStoreDataServiceStub : public IRemoteStub { @@ -171,7 +174,7 @@ public: virtual Status GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy); virtual Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy); virtual Status StopWatchDeviceChange(sptr observer); - virtual sptr GetRdbService(); + virtual sptr GetRdbService(); private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp index be7827b39..56e0a2408 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp @@ -390,7 +390,7 @@ Status KvStoreDataServiceProxy::StopWatchDeviceChange(sptr(reply.ReadInt32()); } -sptr KvStoreDataServiceProxy::GetRdbService() +sptr KvStoreDataServiceProxy::GetRdbService() { MessageParcel data; if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) { @@ -405,7 +405,7 @@ sptr KvStoreDataServiceProxy::GetRdbService() return nullptr; } auto remoteObject = reply.ReadRemoteObject(); - return iface_cast(remoteObject); + return iface_cast(remoteObject); } int32_t KvStoreDataServiceStub::GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp deleted file mode 100644 index e4481c9cb..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 "RdbParcel" - -#include "rdb_parcel.h" -#include "log_print.h" - -namespace OHOS::DistributedKv { -RdbStoreParam::RdbStoreParam(const std::string& bundleName, const std::string& directory, - const std::string& storeName, int type, bool isAutoSync) - : bundleName_(bundleName), path_(directory), storeName_(storeName), type_(type), - isAutoSync_(isAutoSync) -{ -} - -bool RdbStoreParam::IsValid() const -{ - return !bundleName_.empty() && !path_.empty() && !storeName_.empty(); -} - -bool RdbStoreParam::Marshalling(MessageParcel& data) const -{ - if (!data.WriteString(bundleName_)) { - ZLOGE("RdbStoreParam write bundle name failed"); - return false; - } - if (!data.WriteString(path_)) { - ZLOGE("RdbStoreParam write directory failed"); - return false; - } - if (!data.WriteString(storeName_)) { - ZLOGE("RdbStoreParam write store name failed"); - return false; - } - if (!data.WriteInt32(type_)) { - ZLOGE("RdbStoreParam write type failed"); - return false; - } - if (!data.WriteBool(isAutoSync_)) { - ZLOGE("RdbStoreParam write auto sync failed"); - return false; - } - return true; -} - -bool RdbStoreParam::UnMarshalling(MessageParcel& data) -{ - if (!data.ReadString(bundleName_)) { - ZLOGE("RdbStoreParam read bundle name failed"); - return false; - } - if (!data.ReadString(path_)) { - ZLOGE("RdbStoreParam read directory failed"); - return false; - } - if (!data.ReadString(storeName_)) { - ZLOGE("RdbStoreParam read store name failed"); - return false; - } - if (!data.ReadInt32(type_)) { - ZLOGE("RdbStoreParam read type failed"); - return false; - } - if (!data.ReadBool(isAutoSync_)) { - ZLOGE("RdbStoreParam read auto sync failed"); - return false; - } - return true; -} -} - diff --git a/frameworks/innerkitsimpl/rdb/include/rdb_manager.h b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h new file mode 100644 index 000000000..ef7793f3b --- /dev/null +++ b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h @@ -0,0 +1,38 @@ +/* + * 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 DISTRIBUTED_RDB_MANAGER_H +#define DISTRIBUTED_RDB_MANAGER_H + +#include +#include +#include + + +namespace OHOS::DistributedRdb { +struct RdbSyncerParam; +class RdbSyncer; + +class RdbManager { +public: + static std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param); + + static int RegisterRdbServiceDeathObserver(const std::string &storeName, const std::function& observer); + + static int UnRegisterRdbServiceDeathObserver(const std::string &storeName); + +}; +} +#endif //DISTRIBUTED_RDB_MANAGER_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h b/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h similarity index 60% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h rename to frameworks/innerkitsimpl/rdb/include/rdb_syncer.h index 86e32f07e..1624aae70 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h +++ b/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h @@ -13,25 +13,16 @@ * limitations under the License. */ -#ifndef DISTRUBTEDDATAFWK_IRDB_STORE_H -#define DISTRUBTEDDATAFWK_IRDB_STORE_H +#ifndef DISTRIBUTED_RDB_SYNCER_H +#define DISTRIBUTED_RDB_SYNCER_H -#include -#include "rdb_parcel.h" +#include +#include -namespace OHOS::DistributedKv { -class IRdbStore : public IRemoteBroker { +namespace OHOS::DistributedRdb { +class RdbSyncer { public: - enum { - RDB_STORE_CMD_SET_DIST_TABLES, - RDB_STORE_CMD_SYNC, - RDB_STORE_CMD_SUBSCRIBE, - RDB_STORE_CMD_REMOVE_DEVICE_DATA, - RDB_STORE_CMD_MAX - }; - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbStore"); - virtual int SetDistributedTables(const std::vector& tables) = 0; }; } -#endif +#endif //DISTRIBUTED_DATA_RDB_SYNCER_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h b/frameworks/innerkitsimpl/rdb/include/rdb_types.h similarity index 58% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h rename to frameworks/innerkitsimpl/rdb/include/rdb_types.h index 2e2c25239..c6c5b5375 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h +++ b/frameworks/innerkitsimpl/rdb/include/rdb_types.h @@ -13,33 +13,51 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_RDB_PARCEL_H -#define DISTRIBUTEDDATAFWK_RDB_PARCEL_H +#ifndef DISTRIBUTEDDATAFWK_RDB_TYPES_H +#define DISTRIBUTEDDATAFWK_RDB_TYPES_H +#include +#include #include -#include -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { enum RdbDistributedType { RDB_DEVICE_COLLABORATION, RDB_DISTRIBUTED_TYPE_MAX }; -struct RdbStoreParam { -public: - RdbStoreParam() = default; - RdbStoreParam(const RdbStoreParam& param) = default; - RdbStoreParam(const std::string& bundleName, const std::string& directory, - const std::string& storeName, int type = RDB_DEVICE_COLLABORATION, bool isAutoSync = false); - bool IsValid() const; - bool Marshalling(MessageParcel& data) const; - bool UnMarshalling(MessageParcel& data); - +struct RdbSyncerParam { std::string bundleName_; std::string path_; std::string storeName_; - int type_ = 0; + int type_ = RDB_DEVICE_COLLABORATION; bool isAutoSync_ = false; }; + +enum SyncMode { + PUSH, + PULL, +}; + +struct SyncOption { + SyncMode mode; + bool isBlock; +}; + +using SyncResult = std::map; // networkId +using SyncCallback = std::function; + +enum SubscribeMode { + LOCAL, + REMOTE, + LOCAL_AND_REMOTE, +}; + +struct SubscribeOption { + SubscribeMode mode; +}; + +struct DropOption { +}; } #endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h b/frameworks/innerkitsimpl/rdb/src/irdb_service.h similarity index 68% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h rename to frameworks/innerkitsimpl/rdb/src/irdb_service.h index bca6359de..a7cbc707e 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h +++ b/frameworks/innerkitsimpl/rdb/src/irdb_service.h @@ -19,24 +19,21 @@ #include #include -#include "irdb_store.h" -#include "rdb_parcel.h" +#include "rdb_service.h" +#include "irdb_syncer.h" +#include "rdb_types.h" #include "rdb_client_death_recipient.h" -namespace OHOS::DistributedKv { -class IRdbService : public IRemoteBroker { +namespace OHOS::DistributedRdb { +class IRdbService : public RdbService, public IRemoteBroker { public: enum { - RDB_SERVICE_CMD_GET_STORE, + RDB_SERVICE_CMD_GET_SYNCER, RDB_SERVICE_CMD_REGISTER_CLIENT_DEATH, RDB_SERVICE_CMD_MAX }; - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbService"); - - virtual sptr GetRdbStore(const RdbStoreParam& param) = 0; - - virtual int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); }; } #endif diff --git a/frameworks/innerkitsimpl/rdb/src/irdb_syncer.h b/frameworks/innerkitsimpl/rdb/src/irdb_syncer.h new file mode 100644 index 000000000..68cf35947 --- /dev/null +++ b/frameworks/innerkitsimpl/rdb/src/irdb_syncer.h @@ -0,0 +1,36 @@ +/* + * 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 DISTRUBTED_IRDB_SYNCER_H +#define DISTRUBTED_IRDB_SYNCER_H + +#include +#include "rdb_types.h" +#include "rdb_syncer.h" + +namespace OHOS::DistributedRdb { +class IRdbSyncer : public RdbSyncer, public IRemoteBroker { +public: + enum { + RDB_SYNCER_CMD_SET_DIST_TABLES, + RDB_SYNCER_CMD_SYNC, + RDB_SYNCER_CMD_SUBSCRIBE, + RDB_SYNCER_CMD_REMOVE_DEVICE_DATA, + RDB_SYNCER_CMD_MAX + }; + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbSyncer"); +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.cpp similarity index 97% rename from frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp rename to frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.cpp index 0cca88839..c61fdc1a5 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.cpp @@ -18,7 +18,7 @@ #include "rdb_client_death_recipient.h" #include "log_print.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { RdbClientDeathRecipientProxy::RdbClientDeathRecipientProxy(const sptr &object) : IRemoteProxy(object) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h similarity index 92% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h rename to frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h index 7cb33d8ee..a5bbbc8cf 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h @@ -20,10 +20,10 @@ #include #include -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { class IRdbClientDeathRecipient : public IRemoteBroker { public: - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbClientDeathRecipient"); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbClientDeathRecipient"); }; class RdbClientDeathRecipientProxy : public IRemoteProxy { diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp new file mode 100644 index 000000000..671169f97 --- /dev/null +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "rdb_manager.h" +#include "rdb_manager_impl.h" + +namespace OHOS::DistributedRdb { +std::shared_ptr RdbManager::GetRdbSyncer(const RdbSyncerParam ¶m) +{ + return RdbManagerImpl::GetInstance().GetRdbSyncer(param); +} + +int RdbManager::RegisterRdbServiceDeathObserver(const std::string &storeName, const std::function& observer) +{ + return RdbManagerImpl::GetInstance().RegisterRdbServiceDeathObserver(storeName, observer); +} + +int RdbManager::UnRegisterRdbServiceDeathObserver(const std::string &storeName) +{ + return RdbManagerImpl::GetInstance().UnRegisterRdbServiceDeathObserver(storeName); +} +} \ No newline at end of file diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp new file mode 100644 index 000000000..67a70da83 --- /dev/null +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp @@ -0,0 +1,176 @@ +/* + * 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 "RdbManagerImpl" + +#include "rdb_manager_impl.h" + +#include "iservice_registry.h" +#include "ipc_skeleton.h" +#include "system_ability_definition.h" + +#include "log_print.h" +#include "ikvstore_data_service.h" +#include "irdb_service.h" + +using namespace OHOS::DistributedKv; +namespace OHOS::DistributedRdb { +class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { +public: + explicit ServiceDeathRecipient(RdbManagerImpl* owner) : owner_(owner) {} + void OnRemoteDied(const wptr &object) override + { + if (owner_ != nullptr) { + owner_->OnRemoteDied(); + } + } +private: + RdbManagerImpl* owner_; +}; + +static sptr GetDistributedDataManager() +{ + auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (manager == nullptr) { + ZLOGE("get system ability manager failed"); + return nullptr; + } + ZLOGI("get distributed data manager"); + auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); + return iface_cast(remoteObject); +} + +static void LinkToDeath(const sptr& remote) +{ + auto& manager = RdbManagerImpl::GetInstance(); + sptr deathRecipient = new(std::nothrow) ServiceDeathRecipient(&manager); + if (!remote->AddDeathRecipient(deathRecipient)) { + ZLOGE("add death recipient failed"); + } + ZLOGE("success"); +} + +RdbManagerImpl::RdbManagerImpl() +{ + ZLOGI("construct"); +} + +RdbManagerImpl::~RdbManagerImpl() +{ + ZLOGI("deconstruct"); +} + +RdbManagerImpl& RdbManagerImpl::GetInstance() +{ + static RdbManagerImpl manager; + return manager; +} + +std::shared_ptr RdbManagerImpl::GetRdbService() +{ + std::lock_guard lock(mutex_); + if (rdbService_ != nullptr) { + return rdbService_; + } + + if (distributedDataMgr_ == nullptr) { + distributedDataMgr_ = GetDistributedDataManager(); + } + if (distributedDataMgr_ == nullptr) { + ZLOGE("get distributed data manager failed"); + return nullptr; + } + + auto serviceObject = distributedDataMgr_->GetRdbService(); + if (serviceObject == nullptr) { + ZLOGE("get rdb service failed"); + return nullptr; + } + LinkToDeath(serviceObject->AsObject()); + rdbService_ = std::shared_ptr(serviceObject.GetRefPtr(), [holder = serviceObject] (const auto*) {}); + return rdbService_; +} + +std::shared_ptr RdbManagerImpl::GetRdbSyncer(const RdbSyncerParam ¶m) +{ + if (param.bundleName_.empty() || param.path_.empty() || param.storeName_.empty()) { + ZLOGE("param is invalid"); + return nullptr; + } + auto service = GetRdbService(); + if (service == nullptr) { + return nullptr; + } + RegisterClientDeathRecipient(param.bundleName_); + return service->GetRdbSyncer(param); +} + +int RdbManagerImpl::RegisterRdbServiceDeathObserver(const std::string& storeName, + const std::function& observer) +{ + serviceDeathObservers_.Insert(storeName, observer); + return 0; +} + +int RdbManagerImpl::UnRegisterRdbServiceDeathObserver(const std::string& storeName) +{ + std::lock_guard lock(mutex_); + serviceDeathObservers_.Erase(storeName); + return 0; +} + +void RdbManagerImpl::OnRemoteDied() +{ + ZLOGI("rdb service has dead!!"); + NotifyServiceDeath(); + ResetServiceHandle(); +} + +void RdbManagerImpl::ResetServiceHandle() +{ + ZLOGI("enter"); + std::lock_guard lock(mutex_); + distributedDataMgr_ = nullptr; + rdbService_ = nullptr; + clientDeathObject_ = nullptr; +} + +void RdbManagerImpl::NotifyServiceDeath() +{ + ZLOGI("enter"); + serviceDeathObservers_.ForEach([] (const auto& key, const auto& value) { + if (value != nullptr) { + value(); + } + return false; + }); +} + +void RdbManagerImpl::RegisterClientDeathRecipient(const std::string& bundleName) +{ + std::lock_guard lock(mutex_); + if (clientDeathObject_ != nullptr) { + return; + } + if (rdbService_ != nullptr) { + sptr object = new(std::nothrow) RdbClientDeathRecipientStub(); + if (rdbService_->RegisterClientDeathRecipient(bundleName, object) != 0) { + ZLOGE("register client death recipient failed"); + } else { + clientDeathObject_ = object; + } + } +} +} diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h new file mode 100644 index 000000000..e0097ee06 --- /dev/null +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h @@ -0,0 +1,68 @@ +/* + * 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 DISTRIBUTED_RDB_MANAGER_IMPL_H +#define DISTRIBUTED_RDB_MANAGER_IMPL_H + +#include +#include +#include + +#include "refbase.h" +#include "iremote_object.h" +#include "concurrent_map.h" +#include "rdb_types.h" +#include "rdb_syncer.h" + +namespace OHOS::DistributedKv { +class IKvStoreDataService; +} + +namespace OHOS::DistributedRdb { +class RdbService; +class RdbManagerImpl { +public: + static RdbManagerImpl &GetInstance(); + + std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param); + + int RegisterRdbServiceDeathObserver(const std::string &storeName, const std::function& callback); + + int UnRegisterRdbServiceDeathObserver(const std::string &storeName); + + void OnRemoteDied(); + +private: + RdbManagerImpl(); + + ~RdbManagerImpl(); + + std::shared_ptr GetRdbService(); + + void ResetServiceHandle(); + + void NotifyServiceDeath(); + + void RegisterClientDeathRecipient(const std::string &bundleName); + + std::mutex mutex_; + sptr distributedDataMgr_; + std::shared_ptr rdbService_; + sptr clientDeathObject_; + + ConcurrentMap> serviceDeathObservers_; +}; +} +#endif //DISTRIBUTED_RDB_MANAGER_IMPL_H diff --git a/services/distributeddataservice/app/src/rdb/rdb_store_factory.h b/frameworks/innerkitsimpl/rdb/src/rdb_service.h similarity index 59% rename from services/distributeddataservice/app/src/rdb/rdb_store_factory.h rename to frameworks/innerkitsimpl/rdb/src/rdb_service.h index 5e7afbf99..9c3274a68 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_store_factory.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service.h @@ -13,25 +13,23 @@ * limitations under the License. */ -#ifndef RDB_STORE_FACTORY_H -#define RDB_STORE_FACTORY_H +#ifndef DISTRIBUTED_RDB_SERVICE_H +#define DISTRIBUTED_RDB_SERVICE_H #include -#include "rdb_store.h" +#include -namespace OHOS::DistributedKv { -class RdbStoreFactory { +#include + +namespace OHOS::DistributedRdb { +struct RdbSyncerParam; +class RdbSyncer; + +class RdbService { public: - using Creator = std::function; - - static void Initialize(); - - static int RegisterCreator(int type, Creator& creator); - - static RdbStore* CreateStore(const RdbStoreParam& param); + virtual std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param) = 0; -private: - static Creator creators_[RDB_DISTRIBUTED_TYPE_MAX]; + virtual int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) = 0; }; } -#endif +#endif //DISTRIBUTED_RDB_SERVICE_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp similarity index 80% rename from frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp rename to frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp index 28f377b69..ce63bdee0 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp @@ -16,35 +16,37 @@ #define LOG_TAG "RdbServiceProxy" #include "rdb_service_proxy.h" -#include "irdb_store.h" +#include "rdb_syncer.h" +#include "itypes_util.h" #include "log_print.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { RdbServiceProxy::RdbServiceProxy(const sptr &object) : IRemoteProxy(object) { } -sptr RdbServiceProxy::GetRdbStore(const RdbStoreParam& param) +std::shared_ptr RdbServiceProxy::GetRdbSyncer(const RdbSyncerParam& param) { MessageParcel data; if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { ZLOGE("write descriptor failed"); return nullptr; } - if (!param.Marshalling(data)) { + if (!DistributedKv::ITypesUtil::Marshalling(param, data)) { return nullptr; } MessageParcel reply; MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_GET_STORE, data, reply, option) != 0) { + if (Remote()->SendRequest(RDB_SERVICE_CMD_GET_SYNCER, data, reply, option) != 0) { ZLOGE("send request failed"); return nullptr; } auto remoteObject = reply.ReadRemoteObject(); - return iface_cast(remoteObject); + auto syncer = iface_cast(remoteObject); + return std::shared_ptr(syncer.GetRefPtr(), [holder = syncer] (const auto*) {}); } int RdbServiceProxy::RegisterClientDeathRecipient(const std::string& bundleName, sptr object) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h similarity index 83% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h rename to frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h index 86cab5f82..5dab528a8 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h @@ -13,22 +13,23 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_IRDB_SERVICE_PROXY_H -#define DISTRIBUTEDDATAFWK_IRDB_SERVICE_PROXY_H +#ifndef DISTRIBUTED_RDB_SERVICE_PROXY_H +#define DISTRIBUTED_RDB_SERVICE_PROXY_H #include #include "irdb_service.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { class RdbServiceProxy : public IRemoteProxy { public: explicit RdbServiceProxy(const sptr& object); - sptr GetRdbStore(const RdbStoreParam& param) override; + std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param) override; int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) override; private: + static inline BrokerDelegator delegator_; }; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_service_stub.cpp similarity index 84% rename from frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp rename to frameworks/innerkitsimpl/rdb/src/rdb_service_stub.cpp index 777668c9e..17808369f 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_stub.cpp @@ -13,19 +13,20 @@ * limitations under the License. */ -#include "rdb_service_stub.h" - #define LOG_TAG "RdbServiceStub" + +#include "rdb_service_stub.h" #include "log_print.h" -#include "irdb_store.h" +#include "irdb_syncer.h" +#include "itypes_util.h" -namespace OHOS::DistributedKv { -int RdbServiceStub::OnRemoteGetRdbStore(MessageParcel& data, MessageParcel& reply) +namespace OHOS::DistributedRdb { +int RdbServiceStub::OnRemoteGetRdbSyncer(MessageParcel& data, MessageParcel& reply) { - RdbStoreParam param; - sptr store; - if (param.UnMarshalling(data)) { - store = GetRdbStore(param); + RdbSyncerParam param; + sptr store; + if (DistributedKv::ITypesUtil::UnMarshalling(data, param)) { + store = GetRdbSyncerInner(param); } reply.WriteRemoteObject(store->AsObject().GetRefPtr()); return 0; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h b/frameworks/innerkitsimpl/rdb/src/rdb_service_stub.h similarity index 73% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h rename to frameworks/innerkitsimpl/rdb/src/rdb_service_stub.h index ca0d1b149..3eeab5ce4 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_stub.h @@ -13,27 +13,30 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_IRDB_SERVICE_STUB_H -#define DISTRIBUTEDDATAFWK_IRDB_SERVICE_STUB_H +#ifndef DISTRIBUTED_RDB_SERVICE_STUB_H +#define DISTRIBUTED_RDB_SERVICE_STUB_H #include #include "irdb_service.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { class RdbServiceStub : public IRemoteStub { public: int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - + + std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param) override { return nullptr; }; + virtual sptr GetRdbSyncerInner(const RdbSyncerParam& param) = 0; + private: static bool CheckInterfaceToken(MessageParcel& data); - int OnRemoteGetRdbStore(MessageParcel& data, MessageParcel& reply); + int OnRemoteGetRdbSyncer(MessageParcel& data, MessageParcel& reply); int OnRemoteRegisterClientDeathRecipient(MessageParcel& data, MessageParcel& reply); using RequestHandle = int (RdbServiceStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandle HANDLES[RDB_SERVICE_CMD_MAX] = { - [RDB_SERVICE_CMD_GET_STORE] = &RdbServiceStub::OnRemoteGetRdbStore, + [RDB_SERVICE_CMD_GET_SYNCER] = &RdbServiceStub::OnRemoteGetRdbSyncer, [RDB_SERVICE_CMD_REGISTER_CLIENT_DEATH] = &RdbServiceStub::OnRemoteRegisterClientDeathRecipient, }; }; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.cpp similarity index 70% rename from frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp rename to frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.cpp index 749644505..ede9735a8 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.cpp @@ -13,21 +13,21 @@ * limitations under the License. */ -#include "rdb_store_proxy.h" +#include "rdb_syncer_proxy.h" -#define LOG_TAG "IRdbStoreProxy" +#define LOG_TAG "RdbSyncerProxy" #include "log_print.h" -namespace OHOS::DistributedKv { -RdbStoreProxy::RdbStoreProxy(const sptr &object) - : IRemoteProxy(object) +namespace OHOS::DistributedRdb { +RdbSyncerProxy::RdbSyncerProxy(const sptr &object) + : IRemoteProxy(object) { } -int RdbStoreProxy::SetDistributedTables(const std::vector &tables) +int RdbSyncerProxy::SetDistributedTables(const std::vector &tables) { MessageParcel data; - if (!data.WriteInterfaceToken(IRdbStore::GetDescriptor())) { + if (!data.WriteInterfaceToken(IRdbSyncer::GetDescriptor())) { ZLOGE("write descriptor failed"); return -1; } @@ -38,7 +38,7 @@ int RdbStoreProxy::SetDistributedTables(const std::vector &tables) MessageParcel reply; MessageOption option; - if (Remote()->SendRequest(RDB_STORE_CMD_SET_DIST_TABLES, data, reply, option) != 0) { + if (Remote()->SendRequest(RDB_SYNCER_CMD_SET_DIST_TABLES, data, reply, option) != 0) { ZLOGE("send request failed"); return -1; } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.h similarity index 69% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h rename to frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.h index c25443272..eff2a15ae 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.h @@ -13,21 +13,21 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_IRDB_STORE_PROXY_H -#define DISTRIBUTEDDATAFWK_IRDB_STORE_PROXY_H +#ifndef DISTRIBUTED_RDB_STORE_PROXY_H +#define DISTRIBUTED_RDB_STORE_PROXY_H #include -#include "irdb_store.h" +#include "irdb_syncer.h" -namespace OHOS::DistributedKv { -class RdbStoreProxy : public IRemoteProxy { +namespace OHOS::DistributedRdb { +class RdbSyncerProxy : public IRemoteProxy { public: - explicit RdbStoreProxy(const sptr& object); + explicit RdbSyncerProxy(const sptr& object); int SetDistributedTables(const std::vector& tables) override; private: - static inline BrokerDelegator delegator_; + static inline BrokerDelegator delegator_; }; } #endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.cpp similarity index 72% rename from frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp rename to frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.cpp index 60ebaecc6..001a4cc5f 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.cpp @@ -13,13 +13,13 @@ * limitations under the License. */ -#define LOG_TAG "IRdbStoreStub" +#define LOG_TAG "RdbSyncerStub" -#include "rdb_store_stub.h" +#include "rdb_syncer_stub.h" #include "log_print.h" -namespace OHOS::DistributedKv { -int RdbStoreStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply) +namespace OHOS::DistributedRdb { +int RdbSyncerStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply) { std::vector tables; data.ReadStringVector(&tables); @@ -27,9 +27,9 @@ int RdbStoreStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParce return 0; } -bool RdbStoreStub::CheckInterfaceToken(MessageParcel& data) +bool RdbSyncerStub::CheckInterfaceToken(MessageParcel& data) { - auto localDescriptor = IRdbStore::GetDescriptor(); + auto localDescriptor = IRdbSyncer::GetDescriptor(); auto remoteDescriptor = data.ReadInterfaceToken(); if (remoteDescriptor != localDescriptor) { ZLOGE("interface token is not equal"); @@ -38,13 +38,13 @@ bool RdbStoreStub::CheckInterfaceToken(MessageParcel& data) return true; } -int RdbStoreStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +int RdbSyncerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { ZLOGI("%{public}d", code); if (!CheckInterfaceToken(data)) { return -1; } - if (code >= 0 && code < RDB_STORE_CMD_MAX) { + if (code >= 0 && code < RDB_SYNCER_CMD_MAX) { return (this->*HANDLERS[code])(data, reply); } return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.h similarity index 68% rename from frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h rename to frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.h index 44af71260..8c72560fc 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.h @@ -13,14 +13,14 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_IRDB_STORE_STUB_H -#define DISTRIBUTEDDATAFWK_IRDB_STORE_STUB_H +#ifndef DISTRIBUTED_RDB_SYNCER_STUB_H +#define DISTRIBUTED_RDB_SYNCER_STUB_H #include -#include "irdb_store.h" +#include "irdb_syncer.h" -namespace OHOS::DistributedKv { -class RdbStoreStub : public IRemoteStub { +namespace OHOS::DistributedRdb { +class RdbSyncerStub : public IRemoteStub { public: int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; @@ -29,9 +29,9 @@ private: int OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); - using RequestHandle = int (RdbStoreStub::*)(MessageParcel &, MessageParcel &); - static constexpr RequestHandle HANDLERS[RDB_STORE_CMD_MAX] = { - [RDB_STORE_CMD_SET_DIST_TABLES] = &RdbStoreStub::OnRemoteSetDistributedTables, + using RequestHandle = int (RdbSyncerStub::*)(MessageParcel &, MessageParcel &); + static constexpr RequestHandle HANDLERS[RDB_SYNCER_CMD_MAX] = { + [RDB_SYNCER_CMD_SET_DIST_TABLES] = &RdbSyncerStub::OnRemoteSetDistributedTables, }; }; } diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 103a11c69..247833100 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -24,8 +24,10 @@ config("distributeddatafwk_config") { include_dirs = [ "include", "../../../frameworks/innerkitsimpl/distributeddatafwk/include", - "../../../frameworks/innerkitsimpl/distributeddatafwk/include/rdb", "../../../frameworks/innerkitsimpl/distributeddatafwk/src", + "../../../frameworks/common/include", + "../../../frameworks/innerkitsimpl/rdb/include", + "../../../frameworks/innerkitsimpl/rdb/src", "//utils/system/safwk/native/include", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", @@ -37,14 +39,17 @@ config("distributeddatafwk_public_config") { include_dirs = [ "include", - "../../../frameworks/innerkitsimpl/distributeddatafwk/include/rdb", + "../../../frameworks/innerkitsimpl/rdb/include", "//utils/native/base/include", ] } ohos_shared_library("distributeddata_inner") { part_name = "distributeddatamgr" - sources = [ + + common_sources = [ "../../../frameworks/common/src/itypes_util.cpp" ] + + kvdb_sources = [ "../../../frameworks/innerkitsimpl/distributeddatafwk/src/blob.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp", @@ -72,15 +77,16 @@ ohos_shared_library("distributeddata_inner") { ] rdb_sources = [ - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_service_stub.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_syncer_proxy.cpp", + "../../../frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.cpp", ] - sources += rdb_sources + sources = common_sources + kvdb_sources + rdb_sources configs = [ ":distributeddatafwk_config" ] diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index 5fdb1c580..51b479ae8 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -25,6 +25,7 @@ ohos_static_library("distributeddata_account_static") { "./src", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", ] cflags_cc = [ "-fvisibility=hidden" ] diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp index ae07da2bc..9c5d8e58d 100755 --- a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp @@ -148,8 +148,9 @@ std::string AccountDelegateImpl::GetDeviceAccountIdByUID(int32_t uid) const void AccountDelegateImpl::NotifyAccountChanged(const AccountEventInfo &accountEventInfo) { - observerMap_.ForEach([&](std::string key, std::shared_ptr val) { + observerMap_.ForEach([&accountEventInfo] (const auto& key, const auto& val) { val->OnAccountChanged(accountEventInfo); + return false; }); } @@ -159,11 +160,11 @@ Status AccountDelegateImpl::Subscribe(std::shared_ptr observer) if (observer == nullptr || observer->Name().empty()) { return Status::INVALID_ARGUMENT; } - if (observerMap_.ContainsKey(observer->Name())) { + if (observerMap_.Contains(observer->Name())) { return Status::INVALID_ARGUMENT; } - auto ret = observerMap_.Put(observer->Name(), observer); + auto ret = observerMap_.Insert(observer->Name(), observer); if (ret) { ZLOGD("end"); return Status::SUCCESS; @@ -178,11 +179,11 @@ Status AccountDelegateImpl::Unsubscribe(std::shared_ptr observer) if (observer == nullptr || observer->Name().empty()) { return Status::INVALID_ARGUMENT; } - if (!observerMap_.ContainsKey(observer->Name())) { + if (!observerMap_.Contains(observer->Name())) { return Status::INVALID_ARGUMENT; } - auto ret = observerMap_.Delete(observer->Name()); + auto ret = observerMap_.Erase(observer->Name()); if (ret) { ZLOGD("end"); return Status::SUCCESS; diff --git a/services/distributeddataservice/adapter/autils/test/unittest/concurrent_map_test.cpp b/services/distributeddataservice/adapter/autils/test/unittest/concurrent_map_test.cpp deleted file mode 100755 index ffb49ec0a..000000000 --- a/services/distributeddataservice/adapter/autils/test/unittest/concurrent_map_test.cpp +++ /dev/null @@ -1,72 +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. - */ - -#include -#include "concurrent_map.h" - -using namespace testing::ext; -using namespace OHOS::DistributedKv; - -class ConcurrentMapTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void ConcurrentMapTest::SetUpTestCase(void) -{} - -void ConcurrentMapTest::TearDownTestCase(void) -{} - -void ConcurrentMapTest::SetUp(void) -{} - -void ConcurrentMapTest::TearDown(void) -{} - -/** - * @tc.name: Test001 - * @tc.desc: test concurrent map CURD operation. - * @tc.type: FUNC - * @tc.require: AR000CQS31 - * @tc.author: hongbo - */ -HWTEST_F(ConcurrentMapTest, Test001, TestSize.Level0) -{ - ConcurrentMap cmap; - - int size = cmap.Size(); - ASSERT_TRUE(size == 0); - - std::string temp = "abc"; - cmap.Put(temp, temp); - - std::string value; - auto val = cmap.Get(temp, value); - ASSERT_TRUE(val); - ASSERT_STREQ(temp.c_str(), value.c_str()); - - auto isContained = cmap.ContainsKey(temp); - ASSERT_TRUE(isContained); - - ASSERT_TRUE(cmap.Delete(temp)); - - ASSERT_TRUE(cmap.Empty()); - - cmap.Clear(); -} \ No newline at end of file diff --git a/services/distributeddataservice/adapter/include/autils/concurrent_map.h b/services/distributeddataservice/adapter/include/autils/concurrent_map.h deleted file mode 100644 index 5bd1b8637..000000000 --- a/services/distributeddataservice/adapter/include/autils/concurrent_map.h +++ /dev/null @@ -1,101 +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 DISTRIBUTEDDATAMGR_CURRENT_MAP_H -#define DISTRIBUTEDDATAMGR_CURRENT_MAP_H - -#include -#include -#include -#include "visibility.h" - -namespace OHOS { -namespace DistributedKv { -template -class ConcurrentMap { -public: - KVSTORE_API explicit ConcurrentMap() {}; - - KVSTORE_API ~ConcurrentMap() {}; - - KVSTORE_API bool Put(const K &key, const V &value) - { - std::unique_lock lock(mapMutex_); - auto ret = map_.insert(std::pair(key, value)); - return ret.second; - } - - KVSTORE_API bool PutValue(const K key, const V value) - { - std::unique_lock lock(mapMutex_); - auto ret = map_.insert(std::pair(key, value)); - return ret.second; - } - - KVSTORE_API bool Empty() - { - std::lock_guard lock(mapMutex_); - return map_.empty(); - } - - KVSTORE_API int Size() - { - std::lock_guard lock(mapMutex_); - return map_.size(); - } - - KVSTORE_API bool Delete(const K &key) - { - std::unique_lock lock(mapMutex_); - return map_.erase(key); - } - - KVSTORE_API bool Get(const K &key, V &v) - { - std::lock_guard lock(mapMutex_); - auto ret = map_.find(key); - if (ret != map_.end()) { - v = ret->second; - return true; - } - return false; - } - - KVSTORE_API bool ContainsKey(const K &key) - { - std::lock_guard lock(mapMutex_); - return map_.count(key); - } - - KVSTORE_API void Clear() - { - std::unique_lock lock(mapMutex_); - map_.clear(); - } - - KVSTORE_API void ForEach(std::function fun) - { - std::lock_guard lock(mapMutex_); - for (auto const &kvPair : map_) { - fun(kvPair.first, kvPair.second); - } - } -private: - std::mutex mapMutex_; - std::map map_; -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // DISTRIBUTEDDATAMGR_CURRENT_MAP_H diff --git a/services/distributeddataservice/adapter/include/log/log_print.h b/services/distributeddataservice/adapter/include/log/log_print.h index ee71fd78a..0824f6387 100755 --- a/services/distributeddataservice/adapter/include/log/log_print.h +++ b/services/distributeddataservice/adapter/include/log/log_print.h @@ -35,6 +35,10 @@ namespace AppDistributedKv { KVSTORE_API static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0xD001620, "ZDDC" }; } // end namespace AppDistributesdKv + +namespace DistributedRdb { +KVSTORE_API static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0xD001630, "ZDDR" }; +} } // end namespace OHOS #define ZLOGD(fmt, ...) \ diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 5de1fc99c..92993e7fd 100755 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -37,7 +37,10 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include/rdb", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/rdb/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/rdb/src", + "//foundation/distributeddatamgr/distributeddatamgr/services/rdb", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", "//utils/system/safwk/native/include", "../adapter/include/account", @@ -52,7 +55,6 @@ config("module_private_config") { "//utils/native/base/include", "include", "src", - "src/rdb", "//third_party/json/single_include", "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk/main/cpp/include", "//base/security/huks/interfaces/innerkits/huks_standard/main/include", @@ -86,10 +88,10 @@ ohos_shared_library("distributeddataservice") { ] rdb_sources = [ - "src/rdb/rdb_device_store.cpp", - "src/rdb/rdb_service.cpp", - "src/rdb/rdb_store.cpp", - "src/rdb/rdb_store_factory.cpp", + "../../rdb/rdb_device_syncer.cpp", + "../../rdb/rdb_service_impl.cpp", + "../../rdb/rdb_syncer_factory.cpp", + "../../rdb/rdb_syncer_impl.cpp", ] sources += rdb_sources diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 47b1e7dc6..545c4d502 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -44,7 +44,7 @@ #include "permission_validator.h" #include "process_communicator_impl.h" #include "reporter.h" -#include "rdb_service.h" +#include "rdb_service_impl.h" #include "system_ability_definition.h" #include "uninstaller/uninstaller.h" @@ -1331,14 +1331,13 @@ Status KvStoreDataService::StopWatchDeviceChange(sptr KvStoreDataService::GetRdbService() +sptr KvStoreDataService::GetRdbService() { return rdbService_; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index fc06ae3a5..536fc2a6e 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -31,8 +31,12 @@ #include "backup_handler.h" #include "device_change_listener_impl.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { class IRdbService; +class RdbServiceImpl; +} + +namespace OHOS::DistributedKv { class KvStoreAccountObserver; class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { DECLARE_SYSTEM_ABILITY(KvStoreDataService); @@ -67,7 +71,7 @@ public: Status GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy) override; Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy) override; Status StopWatchDeviceChange(sptr observer) override; - sptr GetRdbService() override; + sptr GetRdbService() override; void OnDump() override; @@ -184,7 +188,7 @@ private: std::mutex deviceListenerMutex_; std::shared_ptr deviceListener_; - sptr rdbService_; + sptr rdbService_; }; class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 66b6b7bc3..24f43e0f7 100755 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -26,11 +26,12 @@ config("module_private_config") { "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/broadcaster", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/utils", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", "//utils/native/base/include", "//utils/system/safwk/native/include", "../include", "../src", - "../src/rdb", + "unittest", "../src/uninstaller", "../src/flowctrl_manager", "../../../../interfaces/innerkits/distributeddata", @@ -59,10 +60,6 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_device_store.cpp", - "../src/rdb/rdb_service.cpp", - "../src/rdb/rdb_store.cpp", - "../src/rdb/rdb_store_factory.cpp", "../src/single_kvstore_impl.cpp", "unittest/kvstore_impl_logical_isolation_test.cpp", ] @@ -116,10 +113,6 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_device_store.cpp", - "../src/rdb/rdb_service.cpp", - "../src/rdb/rdb_store.cpp", - "../src/rdb/rdb_store_factory.cpp", "../src/single_kvstore_impl.cpp", "unittest/kvstore_impl_physical_isolation_test.cpp", ] @@ -173,10 +166,6 @@ ohos_unittest("KvStoreDataServiceTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_device_store.cpp", - "../src/rdb/rdb_service.cpp", - "../src/rdb/rdb_store.cpp", - "../src/rdb/rdb_store_factory.cpp", "../src/single_kvstore_impl.cpp", "unittest/kvstore_data_service_test.cpp", ] @@ -228,7 +217,6 @@ ohos_unittest("KvStoreBackupTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_service.cpp", "../src/single_kvstore_impl.cpp", "unittest/kvstore_app_manager.cpp", "unittest/kvstore_backup_test.cpp", @@ -313,10 +301,6 @@ ohos_unittest("KvStoreSyncManagerTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_device_store.cpp", - "../src/rdb/rdb_service.cpp", - "../src/rdb/rdb_store.cpp", - "../src/rdb/rdb_store_factory.cpp", "../src/single_kvstore_impl.cpp", ] @@ -369,10 +353,6 @@ ohos_unittest("KvStoreUninstallerTest") { "../src/kvstore_sync_manager.cpp", "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", - "../src/rdb/rdb_device_store.cpp", - "../src/rdb/rdb_service.cpp", - "../src/rdb/rdb_store.cpp", - "../src/rdb/rdb_store_factory.cpp", "../src/single_kvstore_impl.cpp", "unittest/uninstaller_test.cpp", ] diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp index 1e1a02755..af8468479 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp @@ -41,7 +41,7 @@ #include "permission_validator.h" #include "process_communicator_impl.h" #include "reporter.h" -#include "rdb_service.h" +#include "rdb_service_impl.h" #include "system_ability_definition.h" #include "uninstaller/uninstaller.h" @@ -1305,7 +1305,7 @@ Status KvStoreDataService::StopWatchDeviceChange(sptr KvStoreDataService::GetRdbService() +sptr KvStoreDataService::GetRdbService() { return rdbService_; } diff --git a/services/distributeddataservice/app/test/unittest/rdb_service_impl.h b/services/distributeddataservice/app/test/unittest/rdb_service_impl.h new file mode 100644 index 000000000..cb606d012 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/rdb_service_impl.h @@ -0,0 +1,28 @@ +// +// Created by wuchunbo on 2022/1/21. +// + +#ifndef RDB_SERVICE_IMPL_MOCK_H +#define RDB_SERVICE_IMPL_MOCK_H + +#include "iremote_broker.h" +#include "iremote_stub.h" + +namespace OHOS::DistributedRdb { +class IRdbService : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); +}; + +class RdbServiceImpl : public IRemoteStub { +public: + RdbServiceImpl() = default; + ~RdbServiceImpl() override = default; + + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override + { + return 0; + } +}; +} +#endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_device_store.cpp b/services/rdb/rdb_device_syncer.cpp similarity index 73% rename from services/distributeddataservice/app/src/rdb/rdb_device_store.cpp rename to services/rdb/rdb_device_syncer.cpp index 7c86ff61f..a3fa2939e 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_device_store.cpp +++ b/services/rdb/rdb_device_syncer.cpp @@ -13,34 +13,22 @@ * limitations under the License. */ -#define LOG_TAG "RdbDeviceStore" +#define LOG_TAG "RdbDeviceSyncer" -#include "rdb_device_store.h" +#include "rdb_device_syncer.h" #include "log_print.h" -#include "rdb_store_factory.h" +#include "rdb_syncer_factory.h" #include "relational_store_manager.h" #include "relational_store_delegate.h" -namespace OHOS::DistributedKv { -void RdbDeviceStore::Initialize() -{ - RdbStoreFactory::Creator creator = RdbDeviceStore::CreateStore; - RdbStoreFactory::RegisterCreator(RDB_DEVICE_COLLABORATION, creator); -} - -RdbStore* RdbDeviceStore::CreateStore(const RdbStoreParam ¶m) -{ - ZLOGI("create device collaboration store"); - return new(std::nothrow) RdbDeviceStore(param); -} - -RdbDeviceStore::RdbDeviceStore(const RdbStoreParam ¶m) - : RdbStore(param), manager_(nullptr), delegate_(nullptr) +namespace OHOS::DistributedRdb { +RdbDeviceSyncer::RdbDeviceSyncer(const RdbSyncerParam ¶m) + : RdbSyncerImpl(param), isInit_(false), manager_(nullptr), delegate_(nullptr) { ZLOGI("construct %{public}s", param.storeName_.c_str()); } -RdbDeviceStore::~RdbDeviceStore() +RdbDeviceSyncer::~RdbDeviceSyncer() { ZLOGI("destroy"); if (manager_ != nullptr & delegate_ != nullptr) { @@ -49,9 +37,12 @@ RdbDeviceStore::~RdbDeviceStore() delete manager_; } -int RdbDeviceStore::Init() +int RdbDeviceSyncer::Init() { ZLOGI("enter"); + if (isInit_) { + return 0; + } manager_ = new(std::nothrow) DistributedDB::RelationalStoreManager(GetAppId(), GetUserId()); if (manager_ == nullptr) { ZLOGE("malloc manager failed"); @@ -61,17 +52,18 @@ int RdbDeviceStore::Init() ZLOGE("create meta data failed"); return -1; } + isInit_ = true; ZLOGI("success"); return 0; } -int RdbDeviceStore::CreateMetaData() +int RdbDeviceSyncer::CreateMetaData() { ZLOGI("enter"); return 0; } -int RdbDeviceStore::SetDistributedTables(const std::vector &tables) +int RdbDeviceSyncer::SetDistributedTables(const std::vector &tables) { auto delegate = GetDelegate(); if (delegate == nullptr) { @@ -89,7 +81,7 @@ int RdbDeviceStore::SetDistributedTables(const std::vector &tables) return 0; } -DistributedDB::RelationalStoreDelegate* RdbDeviceStore::GetDelegate() +DistributedDB::RelationalStoreDelegate* RdbDeviceSyncer::GetDelegate() { if (manager_ == nullptr) { ZLOGE("manager_ is nullptr"); diff --git a/services/distributeddataservice/app/src/rdb/rdb_device_store.h b/services/rdb/rdb_device_syncer.h similarity index 70% rename from services/distributeddataservice/app/src/rdb/rdb_device_store.h rename to services/rdb/rdb_device_syncer.h index c924d81d7..6ed93a513 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_device_store.h +++ b/services/rdb/rdb_device_syncer.h @@ -13,26 +13,25 @@ * limitations under the License. */ -#ifndef RDB_DEVICE_STORE_H -#define RDB_DEVICE_STORE_H +#ifndef DISTRIBUTED_RDB_DEVICE_SYNCER_H +#define DISTRIBUTED_RDB_DEVICE_SYNCER_H -#include "rdb_store.h" -#include "rdb_store_stub.h" +#include "rdb_syncer_impl.h" +#include "rdb_syncer_stub.h" +#include "rdb_types.h" +#include "rdb_syncer_factory.h" namespace DistributedDB { class RelationalStoreManager; class RelationalStoreDelegate; } -namespace OHOS::DistributedKv { -class RdbDeviceStore : public RdbStore { +namespace OHOS::DistributedRdb { +class RdbDeviceSyncer : public RdbSyncerImpl { public: - explicit RdbDeviceStore(const RdbStoreParam& param); + explicit RdbDeviceSyncer(const RdbSyncerParam& param); - ~RdbDeviceStore() override; - - static void Initialize(); - static RdbStore* CreateStore(const RdbStoreParam& param); + ~RdbDeviceSyncer() override; /* IPC interface */ int SetDistributedTables(const std::vector& tables) override; @@ -43,13 +42,14 @@ public: private: int CreateMetaData(); - DistributedDB::RelationalStoreManager* GetManager(); - DistributedDB::RelationalStoreDelegate* GetDelegate(); std::mutex mutex_; + bool isInit_; DistributedDB::RelationalStoreManager* manager_; DistributedDB::RelationalStoreDelegate* delegate_; + + static inline RdbSyncerRegistration registration_; }; } #endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_service.cpp b/services/rdb/rdb_service_impl.cpp similarity index 48% rename from services/distributeddataservice/app/src/rdb/rdb_service.cpp rename to services/rdb/rdb_service_impl.cpp index cc7b4686b..58fbb3cf4 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_service.cpp +++ b/services/rdb/rdb_service_impl.cpp @@ -13,38 +13,27 @@ * limitations under the License. */ -#define LOG_TAG "RdbService" +#define LOG_TAG "RdbServiceImpl" -#include "rdb_service.h" +#include "rdb_service_impl.h" #include "kvstore_utils.h" #include "log_print.h" -#include "rdb_store.h" -#include "rdb_store_factory.h" +#include "rdb_syncer_impl.h" +#include "rdb_syncer_factory.h" -namespace OHOS::DistributedKv { -static bool operator==(const sptr& store, const std::string& appId) -{ - auto* storePtr = (RdbStore*)store.GetRefPtr(); - return storePtr != nullptr && (storePtr->GetAppId() == appId); -} - -void RdbService::Initialzie() -{ - RdbStoreFactory::Initialize(); -} - -RdbService::ClientDeathRecipient::ClientDeathRecipient(DeathCallback& callback) +namespace OHOS::DistributedRdb { +RdbServiceImpl::ClientDeathRecipient::ClientDeathRecipient(DeathCallback& callback) : callback_(callback) { ZLOGI("construct"); } -RdbService::ClientDeathRecipient::~ClientDeathRecipient() +RdbServiceImpl::ClientDeathRecipient::~ClientDeathRecipient() { ZLOGI("deconstruct"); } -void RdbService::ClientDeathRecipient::OnRemoteDied(const wptr& object) +void RdbServiceImpl::ClientDeathRecipient::OnRemoteDied(const wptr& object) { auto objectSptr = object.promote(); if (objectSptr != nullptr && callback_ != nullptr) { @@ -52,66 +41,50 @@ void RdbService::ClientDeathRecipient::OnRemoteDied(const wptr& o } } -void RdbService::ClearClientRecipient(const std::string& bundleName, sptr& proxy) +void RdbServiceImpl::ClearClientRecipient(const std::string& bundleName, sptr& proxy) { std::lock_guard lock(recipientsLock_); ZLOGI("remove %{public}s", bundleName.c_str()); recipients_.erase(proxy); } -void RdbService::ClearClientStores(const std::string& bundleName) +void RdbServiceImpl::ClearClientSyncers(const std::string& bundleName) { ZLOGI("enter"); - auto appId = KvStoreUtils::GetAppIdByBundleName(bundleName); - std::lock_guard lock(storesLock_); - for (auto it = stores_.begin(); it != stores_.end();) { - if (it->second == appId) { - ZLOGI("remove %{public}s", it->first.c_str()); - stores_.erase(it++); - } else { - it++; - } - } + std::string appId = DistributedKv::KvStoreUtils::GetAppIdByBundleName(bundleName); + auto count = syncers_.EraseAllIf([&appId] (const std::string& key, const sptr& value) { + return value->GetAppId() == appId; + }); + ZLOGI("remove %{public}d", static_cast(count)); } -void RdbService::OnClientDied(const std::string &bundleName, sptr& proxy) +void RdbServiceImpl::OnClientDied(const std::string &bundleName, sptr& proxy) { ZLOGI("%{public}s died", bundleName.c_str()); ClearClientRecipient(bundleName, proxy); - ClearClientStores(bundleName); + ClearClientSyncers(bundleName); } -bool RdbService::CheckAccess(const RdbStoreParam& param) const +bool RdbServiceImpl::CheckAccess(const RdbSyncerParam& param) const { - return !KvStoreUtils::GetAppIdByBundleName(param.bundleName_).empty(); + return !DistributedKv::KvStoreUtils::GetAppIdByBundleName(param.bundleName_).empty(); } -sptr RdbService::CreateStore(const RdbStoreParam& param) +sptr RdbServiceImpl::CreateSyncer(const RdbSyncerParam& param) { - sptr store = RdbStoreFactory::CreateStore(param); - if (store == nullptr) { - ZLOGE("create temp store failed"); - return nullptr; - } - - std::lock_guard lock(storesLock_); - for (const auto& entry : stores_) { - if (*store == *entry.second) { - ZLOGI("find %{public}s", store->GetStoreId().c_str()); - return entry.second; - } - } - - ZLOGI("create new store %{public}s", param.storeName_.c_str()); - if (store->Init() != 0) { - ZLOGE("store init failed"); - return nullptr; + RdbSyncerImpl* syncerNew = RdbSyncerFactory::GetInstance().CreateSyncer(param); + auto syncer = syncers_.ComputeIfAbsent(syncerNew->GetIdentifier(), + [&syncerNew] (const std::string& key) { + ZLOGI("create new syncer %{public}s", key.c_str()); + return sptr(syncerNew); + }); + if (syncer != nullptr) { + syncer->Init(); } - stores_.insert({store->GetIdentifier(), store}); - return store; + return syncer; } -sptr RdbService::GetRdbStore(const RdbStoreParam& param) +sptr RdbServiceImpl::GetRdbSyncerInner(const RdbSyncerParam& param) { ZLOGI("%{public}s %{public}s %{public}s", param.bundleName_.c_str(), param.path_.c_str(), param.storeName_.c_str()); @@ -119,10 +92,10 @@ sptr RdbService::GetRdbStore(const RdbStoreParam& param) ZLOGI("check access failed"); return nullptr; } - return CreateStore(param); + return CreateSyncer(param); } -int RdbService::RegisterClientDeathRecipient(const std::string& bundleName, sptr proxy) +int RdbServiceImpl::RegisterClientDeathRecipient(const std::string& bundleName, sptr proxy) { if (proxy == nullptr) { ZLOGE("recipient is nullptr"); diff --git a/services/distributeddataservice/app/src/rdb/rdb_service.h b/services/rdb/rdb_service_impl.h similarity index 77% rename from services/distributeddataservice/app/src/rdb/rdb_service.h rename to services/rdb/rdb_service_impl.h index 40b132644..f22709880 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_service.h +++ b/services/rdb/rdb_service_impl.h @@ -21,27 +21,26 @@ #include #include #include -#include "rdb_store.h" +#include "rdb_syncer_impl.h" +#include "concurrent_map.h" -namespace OHOS::DistributedKv { -class RdbService : public RdbServiceStub { +namespace OHOS::DistributedRdb { +class RdbServiceImpl : public RdbServiceStub { public: - sptr GetRdbStore(const RdbStoreParam& param) override; + sptr GetRdbSyncerInner(const RdbSyncerParam& param) override; int RegisterClientDeathRecipient(const std::string& bundleName, sptr proxy) override; void OnClientDied(const std::string& bundleName, sptr& proxy); - static void Initialzie(); - private: - bool CheckAccess(const RdbStoreParam& param) const; + bool CheckAccess(const RdbSyncerParam& param) const; - sptr CreateStore(const RdbStoreParam& param); + sptr CreateSyncer(const RdbSyncerParam& param); void ClearClientRecipient(const std::string& bundleName, sptr& proxy); - void ClearClientStores(const std::string& bundleName); + void ClearClientSyncers(const std::string& bundleName); class ClientDeathRecipient : public DeathRecipient { public: @@ -53,8 +52,7 @@ private: DeathCallback callback_; }; - std::mutex storesLock_; - std::map> stores_; // identifier + ConcurrentMap> syncers_; //identifier std::mutex recipientsLock_; std::map, sptr> recipients_; }; diff --git a/services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp b/services/rdb/rdb_syncer_factory.cpp similarity index 50% rename from services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp rename to services/rdb/rdb_syncer_factory.cpp index 0fd19ea88..fb359a2c0 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp +++ b/services/rdb/rdb_syncer_factory.cpp @@ -13,44 +13,41 @@ * limitations under the License. */ -#define LOG_TAG "RdbStoreFactory" +#define LOG_TAG "RdbSyncerFactory" -#include "rdb_store_factory.h" +#include "rdb_syncer_factory.h" #include "log_print.h" -#include "rdb_device_store.h" +#include "rdb_types.h" -namespace OHOS::DistributedKv { -RdbStoreFactory::Creator RdbStoreFactory::creators_[RDB_DISTRIBUTED_TYPE_MAX]; - -void RdbStoreFactory::Initialize() +namespace OHOS::DistributedRdb { +RdbSyncerFactory& RdbSyncerFactory::GetInstance() { - RdbDeviceStore::Initialize(); + static RdbSyncerFactory factory; + return factory; } -int RdbStoreFactory::RegisterCreator(int type, Creator &creator) +void RdbSyncerFactory::Register(int type, const Creator &creator) { - if (type < 0 || type >= RDB_DISTRIBUTED_TYPE_MAX) { - ZLOGE("type=%{public}d is invalid", type); - return -1; - } if (creator == nullptr) { ZLOGE("creator is empty"); - return -1; + return; } ZLOGI("add creator for store type %{public}d", type); - creators_[type] = creator; - return 0; + creators_.insert({type, creator}); +} + +void RdbSyncerFactory::UnRegister(int type) +{ + creators_.erase(type); } -RdbStore* RdbStoreFactory::CreateStore(const RdbStoreParam& param) +RdbSyncerImpl* RdbSyncerFactory::CreateSyncer(const RdbSyncerParam& param) { - int type = param.type_; - if (type < 0 || type >= RDB_DISTRIBUTED_TYPE_MAX) { - ZLOGE("type=%{public}d is invalid", type); + auto it = creators_.find(param.type_); + if (it == creators_.end()) { return nullptr; } - - return (creators_[type])(param); + return it->second(param); } } diff --git a/services/rdb/rdb_syncer_factory.h b/services/rdb/rdb_syncer_factory.h new file mode 100644 index 000000000..908308beb --- /dev/null +++ b/services/rdb/rdb_syncer_factory.h @@ -0,0 +1,76 @@ +/* + * 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 DISTRIBUTED_RDB_STORE_FACTORY_H +#define DISTRIBUTED_RDB_STORE_FACTORY_H + +#include +#include +#include + +namespace OHOS::DistributedRdb { +class RdbSyncerImpl; +struct RdbSyncerParam; +class RdbSyncerFactory { +public: + using Creator = std::function; + + static RdbSyncerFactory& GetInstance(); + + void Register(int type, const Creator& creator); + + void UnRegister(int type); + + RdbSyncerImpl* CreateSyncer(const RdbSyncerParam& param); + +private: + std::map creators_; +}; + +template +class RdbSyncerCreator { +public: + RdbSyncerImpl* operator()(const RdbSyncerParam& param) + { + return static_cast(new(std::nothrow) T(param)); + } +}; + +template +class RdbSyncerRegistration { +public: + RdbSyncerRegistration(); + + ~RdbSyncerRegistration(); + + RdbSyncerRegistration(const RdbSyncerRegistration&) = delete; + RdbSyncerRegistration(RdbSyncerRegistration&&) = delete; + RdbSyncerRegistration& operator=(const RdbSyncerRegistration&) = delete; + RdbSyncerRegistration& operator=(RdbSyncerRegistration&&) = delete; +}; + +template +RdbSyncerRegistration::RdbSyncerRegistration() +{ + RdbSyncerFactory::GetInstance().Register(type, RdbSyncerCreator()); +} + +template +RdbSyncerRegistration::~RdbSyncerRegistration() +{ + RdbSyncerFactory::GetInstance().UnRegister(type); +} +} +#endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_store.cpp b/services/rdb/rdb_syncer_impl.cpp similarity index 69% rename from services/distributeddataservice/app/src/rdb/rdb_store.cpp rename to services/rdb/rdb_syncer_impl.cpp index a93631886..fbd0c58b3 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_store.cpp +++ b/services/rdb/rdb_syncer_impl.cpp @@ -13,65 +13,65 @@ * limitations under the License. */ -#define LOG_TAG "RdbStore" +#define LOG_TAG "RdbSyncerImpl" -#include "rdb_store.h" +#include "rdb_syncer_impl.h" #include "kvstore_utils.h" #include "log_print.h" -namespace OHOS::DistributedKv { +namespace OHOS::DistributedRdb { static std::string GetCurrentUserId() { return "0"; } -RdbStore::RdbStore(const RdbStoreParam ¶m) +RdbSyncerImpl::RdbSyncerImpl(const RdbSyncerParam ¶m) : type_(param.type_), bundleName_(param.bundleName_), path_(param.path_), storeId_(param.storeName_) { ZLOGI("construct %{public}s %{public}s %{public}s %{public}d", bundleName_.c_str(), userId_.c_str(), storeId_.c_str(), type_); - appId_ = KvStoreUtils::GetAppIdByBundleName(bundleName_); + appId_ = DistributedKv::KvStoreUtils::GetAppIdByBundleName(bundleName_); userId_ = GetCurrentUserId(); identifier_ = std::to_string(type_) + "-" + appId_ + "-" + userId_ + "-" + storeId_; } -RdbStore::~RdbStore() +RdbSyncerImpl::~RdbSyncerImpl() { ZLOGI("destroy %{public}s", storeId_.c_str()); } -bool RdbStore::operator==(const RdbStore& rhs) const +bool RdbSyncerImpl::operator==(const RdbSyncerImpl& rhs) const { return identifier_ == rhs.identifier_; } -std::string RdbStore::GetBundleName() const +std::string RdbSyncerImpl::GetBundleName() const { return bundleName_; } -std::string RdbStore::GetAppId() const +std::string RdbSyncerImpl::GetAppId() const { return appId_; } -std::string RdbStore::GetUserId() const +std::string RdbSyncerImpl::GetUserId() const { return userId_; } -std::string RdbStore::GetStoreId() const +std::string RdbSyncerImpl::GetStoreId() const { return storeId_; } -std::string RdbStore::GetIdentifier() const +std::string RdbSyncerImpl::GetIdentifier() const { return identifier_; } -std::string RdbStore::GetPath() const +std::string RdbSyncerImpl::GetPath() const { return path_; } diff --git a/services/distributeddataservice/app/src/rdb/rdb_store.h b/services/rdb/rdb_syncer_impl.h similarity index 69% rename from services/distributeddataservice/app/src/rdb/rdb_store.h rename to services/rdb/rdb_syncer_impl.h index 1fdad04ca..cda4288e9 100644 --- a/services/distributeddataservice/app/src/rdb/rdb_store.h +++ b/services/rdb/rdb_syncer_impl.h @@ -13,26 +13,26 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATASERVICE_RDB_STORE_H -#define DISTRIBUTEDDATASERVICE_RDB_STORE_H +#ifndef DISTRIBUTED_RDB_SYNCER_IMPL_H +#define DISTRIBUTED_RDB_SYNCER_IMPL_H -#include "rdb_store_stub.h" -#include "rdb_parcel.h" +#include "rdb_syncer_stub.h" +#include "rdb_types.h" -namespace OHOS::DistributedKv { -class RdbStore : public RdbStoreStub { +namespace OHOS::DistributedRdb { +class RdbSyncerImpl : public RdbSyncerStub { public: - explicit RdbStore(const RdbStoreParam& param); + explicit RdbSyncerImpl(const RdbSyncerParam& param); - RdbStore() = delete; - RdbStore(const RdbStore&) = delete; - RdbStore& operator=(const RdbStore&) = delete; + RdbSyncerImpl() = delete; + RdbSyncerImpl(const RdbSyncerImpl&) = delete; + RdbSyncerImpl& operator=(const RdbSyncerImpl&) = delete; - virtual ~RdbStore(); + virtual ~RdbSyncerImpl(); virtual int Init() = 0; - bool operator==(const RdbStore& rhs) const; + bool operator==(const RdbSyncerImpl& rhs) const; std::string GetBundleName() const; -- Gitee From 46bb2246460a49bed2c65b914bb98adc4eeca674 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Fri, 21 Jan 2022 20:10:11 +0800 Subject: [PATCH 2/7] fix codecheck Signed-off-by: wuchunbo --- frameworks/common/include/concurrent_map.h | 8 ++++---- frameworks/common/include/itypes_util.h | 4 +++- frameworks/common/src/itypes_util.cpp | 4 ++-- .../innerkitsimpl/rdb/include/rdb_manager.h | 3 +-- .../innerkitsimpl/rdb/include/rdb_syncer.h | 2 +- .../rdb/src/rdb_client_death_recipient.h | 2 +- .../innerkitsimpl/rdb/src/rdb_manager_impl.cpp | 5 ++--- .../innerkitsimpl/rdb/src/rdb_manager_impl.h | 2 +- frameworks/innerkitsimpl/rdb/src/rdb_service.h | 2 +- .../app/test/unittest/kvstore_data_service.cpp | 8 ++++---- .../app/test/unittest/rdb_service_impl.h | 17 ++++++++++++++--- services/rdb/rdb_service_impl.cpp | 7 ++----- services/rdb/rdb_service_impl.h | 5 ++--- services/rdb/rdb_syncer_factory.cpp | 2 +- 14 files changed, 39 insertions(+), 32 deletions(-) diff --git a/frameworks/common/include/concurrent_map.h b/frameworks/common/include/concurrent_map.h index 55967f1d6..bbdac5637 100644 --- a/frameworks/common/include/concurrent_map.h +++ b/frameworks/common/include/concurrent_map.h @@ -79,10 +79,10 @@ public: std::lock_guard lock(mutex_); auto it = entries_.find(key); if (it == entries_.end()) { - return std::pair{ false, mapped_type() }; + return std::pair { false, mapped_type() }; } - return std::pair{ true, it->second }; + return std::pair { true, it->second }; } bool Contains(const key_type& key) const noexcept @@ -94,7 +94,7 @@ public: bool Insert(const key_type &key, const mapped_type &value) noexcept { std::lock_guard lock(mutex_); - auto it = entries_.insert(value_type{key, value}); + auto it = entries_.insert(value_type{ key, value }); return it.second; } @@ -221,7 +221,7 @@ public: return it->second; } mapped_type value = action(key); - entries_.insert(value_type{key, value}); + entries_.insert(value_type{ key, value }); return value; } private: diff --git a/frameworks/common/include/itypes_util.h b/frameworks/common/include/itypes_util.h index ded6a2671..ee3e8f619 100644 --- a/frameworks/common/include/itypes_util.h +++ b/frameworks/common/include/itypes_util.h @@ -129,7 +129,9 @@ public: return Status::IPC_ERROR; } } - return Status::SUCCESS; } + return Status::SUCCESS; + } + private: template class VectorParcel : public MessageParcel { diff --git a/frameworks/common/src/itypes_util.cpp b/frameworks/common/src/itypes_util.cpp index fc5d9330c..ff654c7e2 100644 --- a/frameworks/common/src/itypes_util.cpp +++ b/frameworks/common/src/itypes_util.cpp @@ -16,9 +16,9 @@ #define LOG_TAG "ITypesUtil" #include "itypes_util.h" -#include -#include "autils/constant.h" #include +#include "autils/constant.h" +#include "log_print.h" namespace OHOS::DistributedKv { bool ITypesUtil::Marshalling(const Blob &blob, MessageParcel &data) diff --git a/frameworks/innerkitsimpl/rdb/include/rdb_manager.h b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h index ef7793f3b..8f3b6882b 100644 --- a/frameworks/innerkitsimpl/rdb/include/rdb_manager.h +++ b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h @@ -32,7 +32,6 @@ public: static int RegisterRdbServiceDeathObserver(const std::string &storeName, const std::function& observer); static int UnRegisterRdbServiceDeathObserver(const std::string &storeName); - }; } -#endif //DISTRIBUTED_RDB_MANAGER_H +#endif diff --git a/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h b/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h index 1624aae70..890be3105 100644 --- a/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h +++ b/frameworks/innerkitsimpl/rdb/include/rdb_syncer.h @@ -25,4 +25,4 @@ public: virtual int SetDistributedTables(const std::vector& tables) = 0; }; } -#endif //DISTRIBUTED_DATA_RDB_SYNCER_H +#endif diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h index a5bbbc8cf..b9e45fd7b 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_client_death_recipient.h @@ -28,7 +28,7 @@ public: class RdbClientDeathRecipientProxy : public IRemoteProxy { public: - RdbClientDeathRecipientProxy(const sptr& object); + explicit RdbClientDeathRecipientProxy(const sptr& object); virtual ~RdbClientDeathRecipientProxy(); private: diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp index 67a70da83..0cafed49c 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp @@ -25,7 +25,6 @@ #include "ikvstore_data_service.h" #include "irdb_service.h" -using namespace OHOS::DistributedKv; namespace OHOS::DistributedRdb { class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: @@ -40,7 +39,7 @@ private: RdbManagerImpl* owner_; }; -static sptr GetDistributedDataManager() +static sptr GetDistributedDataManager() { auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (manager == nullptr) { @@ -49,7 +48,7 @@ static sptr GetDistributedDataManager() } ZLOGI("get distributed data manager"); auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); - return iface_cast(remoteObject); + return iface_cast(remoteObject); } static void LinkToDeath(const sptr& remote) diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h index e0097ee06..0a4e9f723 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h @@ -65,4 +65,4 @@ private: ConcurrentMap> serviceDeathObservers_; }; } -#endif //DISTRIBUTED_RDB_MANAGER_IMPL_H +#endif diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_service.h b/frameworks/innerkitsimpl/rdb/src/rdb_service.h index 9c3274a68..7168c9b74 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_service.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service.h @@ -32,4 +32,4 @@ public: virtual int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) = 0; }; } -#endif //DISTRIBUTED_RDB_SERVICE_H +#endif diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp index af8468479..fa255e733 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp @@ -550,8 +550,8 @@ bool KvStoreDataService::CheckBackupFileExist(const std::string &deviceAccountId { auto pathType = KvStoreAppManager::ConvertPathType(bundleName, securityLevel); auto backupFileName = Constant::Concatenate({ Constant::DEFAULT_GROUP_ID, "_", bundleName, "_", storeId }); - std::initializer_list backFileList = {BackupHandler::GetBackupPath(deviceAccountId, pathType), - "/", BackupHandler::GetHashedBackupName(backupFileName)}; + std::initializer_list backFileList = { BackupHandler::GetBackupPath(deviceAccountId, pathType), + "/", BackupHandler::GetHashedBackupName(backupFileName) }; auto backFilePath = Constant::Concatenate(backFileList); if (!BackupHandler::FileExists(backFilePath)) { ZLOGE("BackupHandler file is not exist."); @@ -1265,7 +1265,7 @@ Status KvStoreDataService::GetDeviceList(std::vector &deviceInfoList { auto devices = KvStoreUtils::GetProviderInstance().GetRemoteNodesBasicInfo(); for (auto const &device : devices) { - deviceInfoList.push_back({device.deviceId, device.deviceName, device.deviceType}); + deviceInfoList.push_back({ device.deviceId, device.deviceName, device.deviceType }); } ZLOGD("strategy is %d.", strategy); return Status::SUCCESS; @@ -1284,7 +1284,7 @@ Status KvStoreDataService::StartWatchDeviceChange(sptrAsObject().GetRefPtr(); - deviceListeners_.insert({objectPtr, observer}); + deviceListeners_.insert({ objectPtr, observer }); ZLOGD("strategy is %d.", strategy); return Status::SUCCESS; } diff --git a/services/distributeddataservice/app/test/unittest/rdb_service_impl.h b/services/distributeddataservice/app/test/unittest/rdb_service_impl.h index cb606d012..4c35172fa 100644 --- a/services/distributeddataservice/app/test/unittest/rdb_service_impl.h +++ b/services/distributeddataservice/app/test/unittest/rdb_service_impl.h @@ -1,6 +1,17 @@ -// -// Created by wuchunbo on 2022/1/21. -// +/* + * 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 RDB_SERVICE_IMPL_MOCK_H #define RDB_SERVICE_IMPL_MOCK_H diff --git a/services/rdb/rdb_service_impl.cpp b/services/rdb/rdb_service_impl.cpp index 58fbb3cf4..ec4b236ec 100644 --- a/services/rdb/rdb_service_impl.cpp +++ b/services/rdb/rdb_service_impl.cpp @@ -43,9 +43,8 @@ void RdbServiceImpl::ClientDeathRecipient::OnRemoteDied(const wptr& proxy) { - std::lock_guard lock(recipientsLock_); ZLOGI("remove %{public}s", bundleName.c_str()); - recipients_.erase(proxy); + recipients_.Erase(proxy); } void RdbServiceImpl::ClearClientSyncers(const std::string& bundleName) @@ -102,7 +101,6 @@ int RdbServiceImpl::RegisterClientDeathRecipient(const std::string& bundleName, return -1; } - std::lock_guard lock(recipientsLock_); ClientDeathRecipient::DeathCallback callback = [bundleName, this] (sptr& object) { OnClientDied(bundleName, object); }; @@ -115,8 +113,7 @@ int RdbServiceImpl::RegisterClientDeathRecipient(const std::string& bundleName, ZLOGE("add death recipient failed"); return -1; } - auto it = recipients_.insert({proxy, recipient}); - if (!it.second) { + if (!recipients_.Insert(proxy, recipient)) { ZLOGE("insert failed"); return -1; } diff --git a/services/rdb/rdb_service_impl.h b/services/rdb/rdb_service_impl.h index f22709880..569def5a7 100644 --- a/services/rdb/rdb_service_impl.h +++ b/services/rdb/rdb_service_impl.h @@ -52,9 +52,8 @@ private: DeathCallback callback_; }; - ConcurrentMap> syncers_; //identifier - std::mutex recipientsLock_; - std::map, sptr> recipients_; + ConcurrentMap> syncers_; // identifier + ConcurrentMap, sptr> recipients_; }; } #endif diff --git a/services/rdb/rdb_syncer_factory.cpp b/services/rdb/rdb_syncer_factory.cpp index fb359a2c0..f073d69a5 100644 --- a/services/rdb/rdb_syncer_factory.cpp +++ b/services/rdb/rdb_syncer_factory.cpp @@ -33,7 +33,7 @@ void RdbSyncerFactory::Register(int type, const Creator &creator) return; } ZLOGI("add creator for store type %{public}d", type); - creators_.insert({type, creator}); + creators_.insert({ type, creator }); } void RdbSyncerFactory::UnRegister(int type) -- Gitee From 76198baf6a658c3a0ab310f83c3051670c1b204c Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Fri, 21 Jan 2022 21:36:10 +0800 Subject: [PATCH 3/7] ajust file directory Signed-off-by: wuchunbo --- frameworks/common/{include => }/concurrent_map.h | 0 .../distributeddatafwk}/include/itypes_util.h | 0 .../distributeddatafwk}/src/itypes_util.cpp | 0 interfaces/innerkits/distributeddata/BUILD.gn | 7 +++---- services/distributeddataservice/adapter/account/BUILD.gn | 2 +- services/distributeddataservice/app/BUILD.gn | 2 +- services/distributeddataservice/app/test/BUILD.gn | 2 +- 7 files changed, 6 insertions(+), 7 deletions(-) rename frameworks/common/{include => }/concurrent_map.h (100%) rename frameworks/{common => innerkitsimpl/distributeddatafwk}/include/itypes_util.h (100%) rename frameworks/{common => innerkitsimpl/distributeddatafwk}/src/itypes_util.cpp (100%) diff --git a/frameworks/common/include/concurrent_map.h b/frameworks/common/concurrent_map.h similarity index 100% rename from frameworks/common/include/concurrent_map.h rename to frameworks/common/concurrent_map.h diff --git a/frameworks/common/include/itypes_util.h b/frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h similarity index 100% rename from frameworks/common/include/itypes_util.h rename to frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h diff --git a/frameworks/common/src/itypes_util.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp similarity index 100% rename from frameworks/common/src/itypes_util.cpp rename to frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 247833100..0e879580d 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -25,7 +25,7 @@ config("distributeddatafwk_config") { "include", "../../../frameworks/innerkitsimpl/distributeddatafwk/include", "../../../frameworks/innerkitsimpl/distributeddatafwk/src", - "../../../frameworks/common/include", + "../../../frameworks/common", "../../../frameworks/innerkitsimpl/rdb/include", "../../../frameworks/innerkitsimpl/rdb/src", "//utils/system/safwk/native/include", @@ -47,8 +47,6 @@ config("distributeddatafwk_public_config") { ohos_shared_library("distributeddata_inner") { part_name = "distributeddatamgr" - common_sources = [ "../../../frameworks/common/src/itypes_util.cpp" ] - kvdb_sources = [ "../../../frameworks/innerkitsimpl/distributeddatafwk/src/blob.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/change_notification.cpp", @@ -73,6 +71,7 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp", "include/types.h", ] @@ -86,7 +85,7 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/rdb/src/rdb_syncer_stub.cpp", ] - sources = common_sources + kvdb_sources + rdb_sources + sources = kvdb_sources + rdb_sources configs = [ ":distributeddatafwk_config" ] diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index 51b479ae8..ee6412cdb 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -25,7 +25,7 @@ ohos_static_library("distributeddata_account_static") { "./src", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common", ] cflags_cc = [ "-fvisibility=hidden" ] diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 92993e7fd..805ea34d0 100755 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -37,7 +37,7 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/rdb/include", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/rdb/src", "//foundation/distributeddatamgr/distributeddatamgr/services/rdb", diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 24f43e0f7..1efe3cc35 100755 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -26,7 +26,7 @@ config("module_private_config") { "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/broadcaster", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/utils", - "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common", "//utils/native/base/include", "//utils/system/safwk/native/include", "../include", -- Gitee From eea1d4e39ebf1041c1d422a9b5d2fd1dbf5b4c04 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Fri, 21 Jan 2022 23:09:27 +0800 Subject: [PATCH 4/7] fix gn format error Signed-off-by: wuchunbo --- interfaces/innerkits/distributeddata/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 0e879580d..9e3943606 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -62,6 +62,7 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_single.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_snapshot.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_sync_callback.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client_death_observer.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_death_recipient_impl.cpp", @@ -71,7 +72,6 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp", "include/types.h", ] -- Gitee From ab820bc774d784dc9422ae19b4e45087b2ceb678 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Sat, 22 Jan 2022 11:50:07 +0800 Subject: [PATCH 5/7] move ServiceDeathRecipient into RdbManagerImpl inner Signed-off-by: wuchunbo --- .../innerkitsimpl/rdb/src/rdb_manager_impl.cpp | 16 ++-------------- .../innerkitsimpl/rdb/src/rdb_manager_impl.h | 13 +++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp index 0cafed49c..a12c6f4b7 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp @@ -26,19 +26,6 @@ #include "irdb_service.h" namespace OHOS::DistributedRdb { -class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { -public: - explicit ServiceDeathRecipient(RdbManagerImpl* owner) : owner_(owner) {} - void OnRemoteDied(const wptr &object) override - { - if (owner_ != nullptr) { - owner_->OnRemoteDied(); - } - } -private: - RdbManagerImpl* owner_; -}; - static sptr GetDistributedDataManager() { auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -54,7 +41,8 @@ static sptr GetDistributedDataManager() static void LinkToDeath(const sptr& remote) { auto& manager = RdbManagerImpl::GetInstance(); - sptr deathRecipient = new(std::nothrow) ServiceDeathRecipient(&manager); + sptr deathRecipient = + new(std::nothrow) RdbManagerImpl::ServiceDeathRecipient(&manager); if (!remote->AddDeathRecipient(deathRecipient)) { ZLOGE("add death recipient failed"); } diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h index 0a4e9f723..cf7298ed3 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h @@ -43,6 +43,19 @@ public: int UnRegisterRdbServiceDeathObserver(const std::string &storeName); void OnRemoteDied(); + + class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit ServiceDeathRecipient(RdbManagerImpl* owner) : owner_(owner) {} + void OnRemoteDied(const wptr &object) override + { + if (owner_ != nullptr) { + owner_->OnRemoteDied(); + } + } + private: + RdbManagerImpl* owner_; + }; private: RdbManagerImpl(); -- Gitee From e4ccee629b72ffd385b8594a71dae8a7d8a347f2 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Sat, 22 Jan 2022 16:36:45 +0800 Subject: [PATCH 6/7] remove const operator[] of ConcurrentMap Signed-off-by: wuchunbo --- frameworks/common/concurrent_map.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frameworks/common/concurrent_map.h b/frameworks/common/concurrent_map.h index bbdac5637..43db4d566 100644 --- a/frameworks/common/concurrent_map.h +++ b/frameworks/common/concurrent_map.h @@ -159,12 +159,6 @@ public: return entries_[key]; } - const mapped_type &operator[](const key_type &key) const noexcept - { - std::lock_guard lock(mutex_); - return entries_[key]; - } - void ForEach(const std::function &action) { if (action == nullptr) { -- Gitee From 0f61ab7f151d70ac202c63ad2cc5dd4281532555 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Sat, 22 Jan 2022 16:54:33 +0800 Subject: [PATCH 7/7] fix codecheck Signed-off-by: wuchunbo --- frameworks/common/concurrent_map.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/common/concurrent_map.h b/frameworks/common/concurrent_map.h index 43db4d566..015e1acab 100644 --- a/frameworks/common/concurrent_map.h +++ b/frameworks/common/concurrent_map.h @@ -94,7 +94,7 @@ public: bool Insert(const key_type &key, const mapped_type &value) noexcept { std::lock_guard lock(mutex_); - auto it = entries_.insert(value_type{ key, value }); + auto it = entries_.insert(value_type { key, value }); return it.second; } @@ -215,7 +215,7 @@ public: return it->second; } mapped_type value = action(key); - entries_.insert(value_type{ key, value }); + entries_.insert(value_type { key, value }); return value; } private: -- Gitee