From 16d0a7396a8d04fac156fc14c909d3c312a73410 Mon Sep 17 00:00:00 2001 From: ding_dong_dong Date: Sat, 25 Nov 2023 16:32:39 +0800 Subject: [PATCH] add code Signed-off-by: ding_dong_dong --- .../framework/include/store/general_store.h | 3 +- .../framework/include/store/general_value.h | 10 ++++++- .../service/rdb/rdb_general_store.cpp | 28 +++++++++++++++++-- .../service/rdb/rdb_general_store.h | 4 ++- .../service/rdb/rdb_service_impl.cpp | 9 ++++-- .../service/rdb/rdb_service_impl.h | 2 +- .../service/rdb/rdb_service_stub.cpp | 5 ++-- .../service/rdb/value_proxy.h | 3 +- 8 files changed, 52 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index ac2bb56f2..6265972f9 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -87,7 +87,8 @@ public: virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; - virtual int32_t SetDistributedTables(const std::vector &tables, int type) = 0; + virtual int32_t SetDistributedTables(const std::vector &tables, int type, + const std::vector &references) = 0; virtual int32_t SetTrackerTable(const std::string& tableName, const std::set& trackerColNames, const std::string& extendColName) = 0; diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h index c9c60cb8d..22ee01e9b 100644 --- a/services/distributeddataservice/framework/include/store/general_value.h +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -74,9 +74,17 @@ struct Asset { std::string hash; std::string path; }; + +struct Reference { + std::string sourceTable; + std::string targetTable; + std::map refFields; +}; + using Assets = std::vector; using Bytes = std::vector; -using Value = std::variant; +using Relations = std::map; +using Value = std::variant; using Values = std::vector; using VBucket = std::map; using VBuckets = std::vector; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 89b78ba81..bb8d09d1e 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -262,9 +262,19 @@ int32_t RdbGeneralStore::Clean(const std::vector &devices, int32_t switch (mode) { case CLOUD_INFO: status = delegate_->RemoveDeviceData("", static_cast(CLOUD_INFO)); + if (status == DistributedDB::OK) { + status = delegate_->RemoveDeviceData("", ClearMode::CLEAR_SHARED_TABLE); + break; + } + (void)delegate_->RemoveDeviceData("", ClearMode::CLEAR_SHARED_TABLE); break; case CLOUD_DATA: status = delegate_->RemoveDeviceData("", static_cast(CLOUD_DATA)); + if (status == DistributedDB::OK) { + status = delegate_->RemoveDeviceData("", ClearMode::CLEAR_SHARED_TABLE); + break; + } + (void)delegate_->RemoveDeviceData("", ClearMode::CLEAR_SHARED_TABLE); break; case NEARBY_DATA: if (devices.empty()) { @@ -349,7 +359,7 @@ RdbGeneralStore::DBProcessCB RdbGeneralStore::GetDBProcessCB(DetailAsync async, if (async) { async(details); } - + if (highMode == AUTO_SYNC_MODE && autoAsync) { autoAsync(details); } @@ -382,7 +392,8 @@ int32_t RdbGeneralStore::AddRef() return ++ref_; } -int32_t RdbGeneralStore::SetDistributedTables(const std::vector &tables, int32_t type) +int32_t RdbGeneralStore::SetDistributedTables(const std::vector &tables, int32_t type, + const std::vector &references) { std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { @@ -398,6 +409,17 @@ int32_t RdbGeneralStore::SetDistributedTables(const std::vector &ta return GeneralError::E_ERROR; } } + std::vector properties; + for (const auto &reference : references) { + DistributedDB::TableReferenceProperty referenceProperty = + { reference.sourceTable, reference.targetTable, reference.refFields }; + properties.emplace_back(referenceProperty); + } + auto status = delegate_->SetReference(properties); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("distributed table set reference failed, err:%{public}d", status); + return GeneralError::E_ERROR; + } return GeneralError::E_OK; } @@ -517,4 +539,4 @@ void RdbGeneralStore::ObserverProxy::OnChange(DBOrigin origin, const std::string } watcher_->OnChange(genOrigin, fields, std::move(changeInfo)); } -} // namespace OHOS::DistributedRdb +} // namespace OHOS::DistributedRdb \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index da5d9e619..ae642c08c 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -40,6 +40,7 @@ public: using Database = DistributedData::Database; using GenErr = DistributedData::GeneralError; using RdbStore = OHOS::NativeRdb::RdbStore; + using Reference = DistributedData::Reference; explicit RdbGeneralStore(const StoreMetaData &meta); ~RdbGeneralStore(); @@ -47,7 +48,8 @@ public: bool IsBound() override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; - int32_t SetDistributedTables(const std::vector &tables, int32_t type) override; + int32_t SetDistributedTables(const std::vector &tables, int32_t type, + const std::vector &references) override; int32_t SetTrackerTable(const std::string& tableName, const std::set& trackerColNames, const std::string& extendColName) override; int32_t BatchInsert(const std::string &table, VBuckets &&values) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 7efe75d3c..8fb210c1e 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -240,7 +240,7 @@ std::shared_ptr RdbServiceImpl::GetStore(const Rd } int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, - int32_t type) + const std::vector &references, int32_t type) { if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), @@ -253,7 +253,12 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const Anonymous::Change(param.storeName_).c_str()); return RDB_ERROR; } - return store->SetDistributedTables(tables, type); + std::vector relationships; + for (const auto &reference : references) { + DistributedData::Reference relationship = { reference.sourceTable, reference.targetTable, reference.refFields }; + relationships.emplace_back(relationship); + } + return store->SetDistributedTables(tables, type, relationships); } void RdbServiceImpl::OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details &&result) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index ac03b903d..00c6eab3d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -49,7 +49,7 @@ public: int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, - int32_t type = DISTRIBUTED_DEVICE) override; + const std::vector &references, int32_t type = DISTRIBUTED_DEVICE) override; int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 9769d4e6a..d9c273d75 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -92,14 +92,15 @@ int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, Messag { RdbSyncerParam param; std::vector tables; + std::vector references; int32_t type; - if (!ITypesUtil::Unmarshal(data, param, tables, type)) { + if (!ITypesUtil::Unmarshal(data, param, tables, references, type)) { ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu type:%{public}d", param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), tables.size(), type); return IPC_STUB_INVALID_DATA_ERR; } - auto status = SetDistributedTables(param, tables, type); + auto status = SetDistributedTables(param, tables, references, type); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; diff --git a/services/distributeddataservice/service/rdb/value_proxy.h b/services/distributeddataservice/service/rdb/value_proxy.h index 829540eec..ca0df65ec 100644 --- a/services/distributeddataservice/service/rdb/value_proxy.h +++ b/services/distributeddataservice/service/rdb/value_proxy.h @@ -91,7 +91,8 @@ public: private: std::vector assets_; }; - using Proxy = std::variant; + using Relations = std::map; + using Proxy = std::variant; class Value { public: -- Gitee