From ac0e1e341f9dcd6474c0bdc527b3bbd57e5e8990 Mon Sep 17 00:00:00 2001 From: lizhuojun Date: Thu, 14 Nov 2024 16:36:35 +0800 Subject: [PATCH] udmf support mult-entries Signed-off-by: lizhuojun --- framework/common/tlv_tag.h | 4 +- framework/common/tlv_util.cpp | 85 ++++--- framework/common/udmf_types_util.cpp | 2 - .../innerkitsimpl/client/udmf_client.cpp | 2 + .../innerkitsimpl/common/unified_meta.cpp | 71 ++++++ .../innerkitsimpl/convert/udmf_conversion.cpp | 4 + .../data/application_defined_record.cpp | 2 +- framework/innerkitsimpl/data/audio.cpp | 5 +- framework/innerkitsimpl/data/file.cpp | 3 +- framework/innerkitsimpl/data/folder.cpp | 5 +- framework/innerkitsimpl/data/html.cpp | 3 +- framework/innerkitsimpl/data/image.cpp | 5 +- framework/innerkitsimpl/data/link.cpp | 3 +- framework/innerkitsimpl/data/plain_text.cpp | 4 +- .../data/system_defined_appitem.cpp | 5 +- .../data/system_defined_form.cpp | 6 +- .../data/system_defined_pixelmap.cpp | 2 +- .../data/system_defined_record.cpp | 2 +- framework/innerkitsimpl/data/text.cpp | 2 +- .../data/unified_data_helper.cpp | 20 +- .../innerkitsimpl/data/unified_record.cpp | 34 ++- framework/innerkitsimpl/data/video.cpp | 5 +- .../service/udmf_service_proxy.cpp | 3 + .../test/unittest/udmf_client_test.cpp | 225 +++++++++++++++++- interfaces/innerkits/common/unified_meta.h | 8 +- interfaces/innerkits/data/unified_record.h | 6 +- 26 files changed, 444 insertions(+), 72 deletions(-) mode change 100755 => 100644 framework/innerkitsimpl/data/html.cpp mode change 100755 => 100644 framework/innerkitsimpl/data/link.cpp mode change 100755 => 100644 framework/innerkitsimpl/data/plain_text.cpp mode change 100755 => 100644 framework/innerkitsimpl/data/system_defined_appitem.cpp mode change 100755 => 100644 framework/innerkitsimpl/data/system_defined_pixelmap.cpp diff --git a/framework/common/tlv_tag.h b/framework/common/tlv_tag.h index 8c0d15a..a9361be 100644 --- a/framework/common/tlv_tag.h +++ b/framework/common/tlv_tag.h @@ -72,7 +72,9 @@ enum class TAG : uint16_t { TAG_WANT, TAG_PIXELMAP, TAG_OBJECT_VALUE, - TAG_RUNTIME + TAG_RUNTIME, + TAG_RECORD_UTD_ID, + TAG_RECORD_ENTRIES }; } #endif //UDMF_TLV_TAG_H diff --git a/framework/common/tlv_util.cpp b/framework/common/tlv_util.cpp index 6d13775..24c2fb7 100644 --- a/framework/common/tlv_util.cpp +++ b/framework/common/tlv_util.cpp @@ -279,7 +279,8 @@ template <> size_t CountBufferSize(const UnifiedRecord &input, TLVObject &data) { std::string version = UTILS::GetCurrentSdkVersion(); return data.CountHead() + data.Count(version) + data.CountBasic(static_cast(input.GetType())) + - data.Count(input.GetUid()) + CountBufferSize(input.GetOriginValue(), data); + data.Count(input.GetUid()) + CountBufferSize(input.GetOriginValue(), data) + data.Count(input.GetUtdId()) + + CountBufferSize(input.GetInnerEntries(), data); } template <> bool Writing(const UnifiedRecord &input, TLVObject &data, TAG tag) @@ -300,44 +301,72 @@ template <> bool Writing(const UnifiedRecord &input, TLVObject &data, TAG tag) if (!TLVUtil::Writing(input.GetOriginValue(), data, TAG::TAG_RECORD_VALUE)) { return false; } + if (!data.Write(TAG::TAG_RECORD_UTD_ID, input.GetUtdId())) { + return false; + } + if (!TLVUtil::Writing(input.GetInnerEntries(), data, TAG::TAG_RECORD_ENTRIES)) { + return false; + } return data.WriteBackHead(static_cast(tag), tagCursor, data.GetCursor() - tagCursor - sizeof(TLVHead)); } -template <> bool Reading(UnifiedRecord &output, TLVObject &data, const TLVHead &head) +static bool ProcessReadRecord(TLVObject &data, TLVHead &headItem, UnifiedRecord &output) { - auto endCursor = data.GetCursor() + head.len; - UDType dataType; std::string uid; ValueType value; + std::string utdId; + std::shared_ptr> entries; + switch (headItem.tag) { + case static_cast(TAG::TAG_VERSION): + data.Skip(headItem); + break; + case static_cast(TAG::TAG_UD_TYPE): + UDType dataType; + if (!TLVUtil::Reading(dataType, data, headItem)) { + return false; + } + output.SetType(dataType); + break; + case static_cast(TAG::TAG_UID): + if (!data.Read(uid, headItem)) { + return false; + } + output.SetUid(uid); + break; + case static_cast(TAG::TAG_RECORD_VALUE): + if (!TLVUtil::Reading(value, data, headItem)) { + return false; + } + output.SetValue(value); + break; + case static_cast(TAG::TAG_RECORD_UTD_ID): + if (!data.Read(utdId, headItem)) { + return false; + } + output.SetUtdId(utdId); + break; + case static_cast(TAG::TAG_RECORD_ENTRIES): + if (!TLVUtil::Reading(entries, data, headItem)) { + return false; + } + output.SetInnerEntries(entries); + break; + default: + data.Skip(headItem); + } + return true; +} + +template <> bool Reading(UnifiedRecord &output, TLVObject &data, const TLVHead &head) +{ + auto endCursor = data.GetCursor() + head.len; while (data.GetCursor() < endCursor) { TLVHead headItem{}; if (!data.ReadHead(headItem)) { return false; } - switch (headItem.tag) { - case static_cast(TAG::TAG_VERSION): - data.Skip(headItem); - break; - case static_cast(TAG::TAG_UD_TYPE): - if (!TLVUtil::Reading(dataType, data, headItem)) { - return false; - } - output.SetType(dataType); - break; - case static_cast(TAG::TAG_UID): - if (!data.Read(uid, headItem)) { - return false; - } - output.SetUid(uid); - break; - case static_cast(TAG::TAG_RECORD_VALUE): - if (!TLVUtil::Reading(value, data, headItem)) { - return false; - } - output.SetValue(value); - break; - default: - data.Skip(headItem); + if (!ProcessReadRecord(data, headItem, output)) { + return false; } } return true; diff --git a/framework/common/udmf_types_util.cpp b/framework/common/udmf_types_util.cpp index ad2d572..19e7e05 100644 --- a/framework/common/udmf_types_util.cpp +++ b/framework/common/udmf_types_util.cpp @@ -59,7 +59,6 @@ bool Unmarshalling(UnifiedData &output, MessageParcel &parcel) LOG_ERROR(UDMF_SERVICE, "Unmarshall unified data failed!"); return false; } - UdmfConversion::ConvertRecordToSubclass(output); return true; } @@ -99,7 +98,6 @@ bool Unmarshalling(std::vector &output, MessageParcel &parcel) LOG_ERROR(UDMF_SERVICE, "Unmarshall unified data set failed!"); return false; } - UdmfConversion::ConvertRecordToSubclass(output); return true; } diff --git a/framework/innerkitsimpl/client/udmf_client.cpp b/framework/innerkitsimpl/client/udmf_client.cpp index 3b49422..6869217 100644 --- a/framework/innerkitsimpl/client/udmf_client.cpp +++ b/framework/innerkitsimpl/client/udmf_client.cpp @@ -16,6 +16,7 @@ #include "udmf_client.h" #include "dds_trace.h" +#include "udmf_conversion.h" #include "udmf_radar_reporter.h" #include "logger.h" @@ -173,6 +174,7 @@ Status UdmfClient::GetSummary(const QueryOption &query, Summary &summary) } auto it = dataCache_.Find(query.key); if (it.first) { + UdmfConversion::InitValueObject(it.second); UnifiedDataHelper::GetSummary(it.second, summary); LOG_INFO(UDMF_CLIENT, "GetSummary in cache! key = %{public}s", query.key.c_str()); return E_OK; diff --git a/framework/innerkitsimpl/common/unified_meta.cpp b/framework/innerkitsimpl/common/unified_meta.cpp index 8aa8769..04f790f 100644 --- a/framework/innerkitsimpl/common/unified_meta.cpp +++ b/framework/innerkitsimpl/common/unified_meta.cpp @@ -681,5 +681,76 @@ UDDetails ObjectUtils::ConvertToUDDetails(std::shared_ptr object) } return details; } + +int64_t ObjectUtils::GetValueSize(ValueType value) +{ + if (value.index() ==0) { + return 0; + } + if (std::holds_alternative(value)) { + return std::get(value).size(); + } + if (std::holds_alternative>(value)) { + return GetObjectValueSize(std::get>(value)); + } + if (std::holds_alternative>(value)) { + return std::get>(value).size(); + } + if (std::holds_alternative>(value)) { + auto pixelMap = std::get>(value); + return pixelMap->GetByteCount(); + } + if (std::holds_alternative>(value)) { + auto want = std::get>(value); + Parcel parcel; + if (!want->Marshalling(parcel)) { + LOG_ERROR(UDMF_FRAMEWORK, "Marshalling want error when GetValueSize!"); + return 0; + } + return parcel.GetDataSize(); + } + size_t size = std::visit([] (const auto &val) { + return sizeof(val); + }, value); + return size; +} + +int64_t ObjectUtils::GetObjectValueSize(std::shared_ptr object) +{ + if (object == nullptr) { + return 0; + } + int64_t size = 0; + std::set isNotNeedToCount = + {VALUE_TYPE, UNIFORM_DATA_TYPE, ARRAY_BUFFER_LENGTH, THUMB_DATA_LENGTH, APP_ICON_LENGTH}; + for (auto [key, value] : object->value_) { + if (isNotNeedToCount.find(key) != isNotNeedToCount.end()) { + continue; + } + if (key == DETAILS) { + if (!std::holds_alternative>(value)) { + LOG_ERROR(UDMF_FRAMEWORK, "Details is not correct!"); + continue; + } + size += GetAllObjectSize(std::get>(value)); + continue; + } + size += GetValueSize(value); + } + return size; +} + +int64_t ObjectUtils::GetAllObjectSize(std::shared_ptr object) +{ + if (object == nullptr) { + return 0; + } + int64_t size = 0; + for (auto [key, value] : object->value_) { + size += key.size() + GetValueSize(value); + } + return size; +} + } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/framework/innerkitsimpl/convert/udmf_conversion.cpp b/framework/innerkitsimpl/convert/udmf_conversion.cpp index 205c7bf..7e551fe 100644 --- a/framework/innerkitsimpl/convert/udmf_conversion.cpp +++ b/framework/innerkitsimpl/convert/udmf_conversion.cpp @@ -56,6 +56,8 @@ void UdmfConversion::ConvertRecordToSubclass(std::shared_ptr &rec auto type = record->GetType(); auto value = record->GetOriginValue(); auto uid = record->GetUid(); + auto entries = record->GetInnerEntries(); + auto utdId = record->GetUtdId(); switch (type) { case UDType::TEXT: { record = std::make_shared(type, value); @@ -118,6 +120,8 @@ void UdmfConversion::ConvertRecordToSubclass(std::shared_ptr &rec } } record->SetUid(uid); + record->SetUtdId(utdId); + record->SetInnerEntries(entries); SetValueWhenNotUds(record); } diff --git a/framework/innerkitsimpl/data/application_defined_record.cpp b/framework/innerkitsimpl/data/application_defined_record.cpp index 350a344..40e979d 100644 --- a/framework/innerkitsimpl/data/application_defined_record.cpp +++ b/framework/innerkitsimpl/data/application_defined_record.cpp @@ -55,7 +55,7 @@ ApplicationDefinedRecord::ApplicationDefinedRecord(UDType type, ValueType value) int64_t ApplicationDefinedRecord::GetSize() { - return rawData_.size() + applicationDefinedType.size(); + return rawData_.size() + GetEntriesSize(); } std::string ApplicationDefinedRecord::GetApplicationDefinedType() const diff --git a/framework/innerkitsimpl/data/audio.cpp b/framework/innerkitsimpl/data/audio.cpp index 15682d9..a30e5e2 100644 --- a/framework/innerkitsimpl/data/audio.cpp +++ b/framework/innerkitsimpl/data/audio.cpp @@ -19,16 +19,17 @@ namespace OHOS { namespace UDMF { Audio::Audio() : Audio("") { + SetType(AUDIO); } Audio::Audio(const std::string &uri) : File(uri) { - this->dataType_ = AUDIO; + SetType(AUDIO); } Audio::Audio(UDType type, ValueType value) : File(type, value) { - this->dataType_ = AUDIO; + SetType(AUDIO); } } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/framework/innerkitsimpl/data/file.cpp b/framework/innerkitsimpl/data/file.cpp index 43beaed..a93f5b8 100644 --- a/framework/innerkitsimpl/data/file.cpp +++ b/framework/innerkitsimpl/data/file.cpp @@ -45,7 +45,8 @@ File::File(UDType type, ValueType value) : UnifiedRecord(type, value) int64_t File::GetSize() { - return this->oriUri_.size() + this->remoteUri_.size(); + return this->oriUri_.size() + this->remoteUri_.size() + UnifiedDataUtils::GetDetailsSize(this->details_) + + GetEntriesSize(); } std::string File::GetUri() const diff --git a/framework/innerkitsimpl/data/folder.cpp b/framework/innerkitsimpl/data/folder.cpp index 3bb45e9..e6b973f 100644 --- a/framework/innerkitsimpl/data/folder.cpp +++ b/framework/innerkitsimpl/data/folder.cpp @@ -19,16 +19,17 @@ namespace OHOS { namespace UDMF { Folder::Folder() : Folder("") { + SetType(FOLDER); } Folder::Folder(const std::string &uri) : File(uri) { - this->dataType_ = FOLDER; + SetType(FOLDER); } Folder::Folder(UDType type, ValueType value) : File(type, value) { - this->dataType_ = FOLDER; + SetType(FOLDER); } } // namespace UDMF } // namespace OHOS diff --git a/framework/innerkitsimpl/data/html.cpp b/framework/innerkitsimpl/data/html.cpp old mode 100755 new mode 100644 index 3aebd8d..8e3b5c9 --- a/framework/innerkitsimpl/data/html.cpp +++ b/framework/innerkitsimpl/data/html.cpp @@ -51,7 +51,8 @@ Html::Html(UDType type, ValueType value) : Text(type, value) int64_t Html::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_) + this->htmlContent_.size() + this->plainContent_.size(); + return UnifiedDataUtils::GetDetailsSize(this->details_) + this->htmlContent_.size() + this->plainContent_.size() + + GetEntriesSize(); } std::string Html::GetHtmlContent() const diff --git a/framework/innerkitsimpl/data/image.cpp b/framework/innerkitsimpl/data/image.cpp index a2a891d..90846a6 100644 --- a/framework/innerkitsimpl/data/image.cpp +++ b/framework/innerkitsimpl/data/image.cpp @@ -19,16 +19,17 @@ namespace OHOS { namespace UDMF { Image::Image() : Image("") { + SetType(IMAGE); } Image::Image(const std::string &uri) : File(uri) { - this->dataType_ = IMAGE; + SetType(IMAGE); } Image::Image(UDType type, ValueType value) : File(type, value) { - this->dataType_ = IMAGE; + SetType(IMAGE); } } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/framework/innerkitsimpl/data/link.cpp b/framework/innerkitsimpl/data/link.cpp old mode 100755 new mode 100644 index ebfe38e..53d29e2 --- a/framework/innerkitsimpl/data/link.cpp +++ b/framework/innerkitsimpl/data/link.cpp @@ -54,7 +54,8 @@ Link::Link(const std::string &url, const std::string &description) int64_t Link::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_) + this->url_.size() + this->description_.size(); + return UnifiedDataUtils::GetDetailsSize(this->details_) + this->url_.size() + this->description_.size() + + GetEntriesSize(); } std::string Link::GetUrl() const diff --git a/framework/innerkitsimpl/data/plain_text.cpp b/framework/innerkitsimpl/data/plain_text.cpp old mode 100755 new mode 100644 index da7d2a8..508fb0b --- a/framework/innerkitsimpl/data/plain_text.cpp +++ b/framework/innerkitsimpl/data/plain_text.cpp @@ -19,6 +19,7 @@ namespace OHOS { namespace UDMF { PlainText::PlainText() : PlainText("", "") { + SetType(PLAIN_TEXT); } PlainText::PlainText(const std::string &content, const std::string &abstract) @@ -50,7 +51,8 @@ PlainText::PlainText(UDType type, ValueType value) : Text(type, value) int64_t PlainText::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_) + this->content_.size() + this->abstract_.size(); + return UnifiedDataUtils::GetDetailsSize(this->details_) + this->content_.size() + this->abstract_.size() + + GetEntriesSize(); } std::string PlainText::GetContent() const diff --git a/framework/innerkitsimpl/data/system_defined_appitem.cpp b/framework/innerkitsimpl/data/system_defined_appitem.cpp old mode 100755 new mode 100644 index 9cfc622..de5fceb --- a/framework/innerkitsimpl/data/system_defined_appitem.cpp +++ b/framework/innerkitsimpl/data/system_defined_appitem.cpp @@ -43,8 +43,9 @@ SystemDefinedAppItem::SystemDefinedAppItem(UDType type, ValueType value) : Syste int64_t SystemDefinedAppItem::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_) + this->appId_.size() + this->appName_.size() - + this->appIconId_.size() + this->appLabelId_.size() + this->bundleName_.size() + this->abilityName_.size(); + return UnifiedDataUtils::GetDetailsSize(this->details_) + this->appId_.size() + this->appName_.size() + + this->appIconId_.size() + this->appLabelId_.size() + this->bundleName_.size() + this->abilityName_.size() + + GetEntriesSize(); } std::string SystemDefinedAppItem::GetAppId() const diff --git a/framework/innerkitsimpl/data/system_defined_form.cpp b/framework/innerkitsimpl/data/system_defined_form.cpp index 1bfcbc3..10a9ba2 100644 --- a/framework/innerkitsimpl/data/system_defined_form.cpp +++ b/framework/innerkitsimpl/data/system_defined_form.cpp @@ -19,12 +19,12 @@ namespace OHOS { namespace UDMF { SystemDefinedForm::SystemDefinedForm() { - this->dataType_ = SYSTEM_DEFINED_FORM; + SetType(SYSTEM_DEFINED_FORM); } SystemDefinedForm::SystemDefinedForm(UDType type, ValueType value) : SystemDefinedRecord(type, value) { - this->dataType_ = SYSTEM_DEFINED_FORM; + SetType(SYSTEM_DEFINED_FORM); if (std::holds_alternative>(value)) { auto object = std::get>(value); object->GetValue(FORMID, formId_); @@ -43,7 +43,7 @@ SystemDefinedForm::SystemDefinedForm(UDType type, ValueType value) : SystemDefin int64_t SystemDefinedForm::GetSize() { return UnifiedDataUtils::GetDetailsSize(this->details_) + sizeof(formId_) + this->formName_.size() - + this->bundleName_.size() + this->abilityName_.size() + this->module_.size(); + + this->bundleName_.size() + this->abilityName_.size() + this->module_.size() + GetEntriesSize(); } int32_t SystemDefinedForm::GetFormId() const diff --git a/framework/innerkitsimpl/data/system_defined_pixelmap.cpp b/framework/innerkitsimpl/data/system_defined_pixelmap.cpp old mode 100755 new mode 100644 index 94d6b39..355cd2c --- a/framework/innerkitsimpl/data/system_defined_pixelmap.cpp +++ b/framework/innerkitsimpl/data/system_defined_pixelmap.cpp @@ -61,7 +61,7 @@ SystemDefinedPixelMap::SystemDefinedPixelMap(UDType type, ValueType value) : Sys int64_t SystemDefinedPixelMap::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size(); + return UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size() + GetEntriesSize(); } std::vector SystemDefinedPixelMap::GetRawData() const diff --git a/framework/innerkitsimpl/data/system_defined_record.cpp b/framework/innerkitsimpl/data/system_defined_record.cpp index e4dc3e2..e382c24 100644 --- a/framework/innerkitsimpl/data/system_defined_record.cpp +++ b/framework/innerkitsimpl/data/system_defined_record.cpp @@ -23,7 +23,7 @@ SystemDefinedRecord::SystemDefinedRecord() : UnifiedRecord(SYSTEM_DEFINED_RECORD int64_t SystemDefinedRecord::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_); + return UnifiedDataUtils::GetDetailsSize(this->details_) + GetEntriesSize(); } SystemDefinedRecord::SystemDefinedRecord(UDType type, ValueType value) : UnifiedRecord(type, value) diff --git a/framework/innerkitsimpl/data/text.cpp b/framework/innerkitsimpl/data/text.cpp index 9d473c8..7b1c476 100644 --- a/framework/innerkitsimpl/data/text.cpp +++ b/framework/innerkitsimpl/data/text.cpp @@ -41,7 +41,7 @@ Text::Text(UDType type, ValueType value) : UnifiedRecord(type, value) int64_t Text::GetSize() { - return UnifiedDataUtils::GetDetailsSize(this->details_); + return UnifiedDataUtils::GetDetailsSize(this->details_) + GetEntriesSize(); } void Text::SetDetails(UDDetails &variantMap) diff --git a/framework/innerkitsimpl/data/unified_data_helper.cpp b/framework/innerkitsimpl/data/unified_data_helper.cpp index 07109a0..5bb3ba0 100644 --- a/framework/innerkitsimpl/data/unified_data_helper.cpp +++ b/framework/innerkitsimpl/data/unified_data_helper.cpp @@ -100,20 +100,24 @@ void UnifiedDataHelper::CreateDirIfNotExist(const std::string& dirPath, const mo void UnifiedDataHelper::GetSummary(const UnifiedData &data, Summary &summary) { for (const auto &record : data.GetRecords()) { - int64_t recordSize = record->GetSize(); - auto udType = UtdUtils::GetUtdIdFromUtdEnum(record->GetType()); - auto it = summary.summary.find(udType); - if (it == summary.summary.end()) { - summary.summary[udType] = recordSize; - } else { - summary.summary[udType] += recordSize; + auto entry = *record->GetEntries(); + for (const auto &[utdId, value] : entry) { + auto valueSize = ObjectUtils::GetValueSize(value); + auto it = summary.summary.find(utdId); + if (it == summary.summary.end()) { + summary.summary[utdId] = valueSize; + } else { + summary.summary[utdId] += valueSize; + } + summary.totalSize += valueSize; } - summary.totalSize += recordSize; } } bool UnifiedDataHelper::Pack(UnifiedData &data) { + UdmfConversion::InitValueObject(data); + Summary summary; GetSummary(data, summary); diff --git a/framework/innerkitsimpl/data/unified_record.cpp b/framework/innerkitsimpl/data/unified_record.cpp index 0afab7f..e8a55c6 100644 --- a/framework/innerkitsimpl/data/unified_record.cpp +++ b/framework/innerkitsimpl/data/unified_record.cpp @@ -54,7 +54,7 @@ void UnifiedRecord::SetType(const UDType &type) int64_t UnifiedRecord::GetSize() { - return 0; + return GetValueSize() + GetEntriesSize(); } std::string UnifiedRecord::GetUid() const @@ -82,6 +82,11 @@ ValueType UnifiedRecord::GetOriginValue() const return value_; } +int64_t UnifiedRecord::GetValueSize() +{ + return ObjectUtils::GetValueSize(value_); +} + bool UnifiedRecord::HasType(const std::string &utdId) const { if (entries_->find(utdId) != entries_->end()) { @@ -124,15 +129,40 @@ ValueType UnifiedRecord::GetEntry(const std::string &utdId) return std::monostate(); } -std::shared_ptr> UnifiedRecord::GetEntries() const +std::shared_ptr> UnifiedRecord::GetEntries() { auto res = std::make_shared>(*entries_); if (!utdId_.empty()) { + if (!std::holds_alternative>(value_)) { + InitObject(); + } res->insert_or_assign(utdId_, value_); } return res; } +std::shared_ptr> UnifiedRecord::GetInnerEntries() const +{ + return entries_; +} + +void UnifiedRecord::SetInnerEntries(std::shared_ptr> entries) +{ + entries_ = entries; +} + +int64_t UnifiedRecord::GetEntriesSize() +{ + if (entries_ == nullptr) { + return 0; + } + int64_t size = 0; + for (auto &entry : *entries_) { + size += ObjectUtils::GetValueSize(entry.second); + } + return size; +} + std::set UnifiedRecord::GetUtdIds() const { std::set utdIds; diff --git a/framework/innerkitsimpl/data/video.cpp b/framework/innerkitsimpl/data/video.cpp index eb186c5..afb9923 100644 --- a/framework/innerkitsimpl/data/video.cpp +++ b/framework/innerkitsimpl/data/video.cpp @@ -19,16 +19,17 @@ namespace OHOS { namespace UDMF { Video::Video() : Video("") { + SetType(VIDEO); } Video::Video(const std::string &uri) : File(uri) { - this->dataType_ = VIDEO; + SetType(VIDEO); } Video::Video(UDType type, ValueType value) : File(type, value) { - this->dataType_ = VIDEO; + SetType(VIDEO); } } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/framework/innerkitsimpl/service/udmf_service_proxy.cpp b/framework/innerkitsimpl/service/udmf_service_proxy.cpp index f8b6c50..cca560f 100644 --- a/framework/innerkitsimpl/service/udmf_service_proxy.cpp +++ b/framework/innerkitsimpl/service/udmf_service_proxy.cpp @@ -79,6 +79,7 @@ int32_t UdmfServiceProxy::GetData(const QueryOption &query, UnifiedData &unified LOG_ERROR(UDMF_SERVICE, "Unmarshal UnifiedData failed!"); return E_READ_PARCEL_ERROR; } + UdmfConversion::ConvertRecordToSubclass(unifiedData); return status; } @@ -95,6 +96,7 @@ int32_t UdmfServiceProxy::GetBatchData(const QueryOption &query, std::vectorGetSize(); ASSERT_EQ(status, E_OK); + EXPECT_EQ(summary.summary["general.text"], record1->GetSize()); + EXPECT_EQ(summary.summary["general.plain-text"], record2->GetSize()); + EXPECT_EQ(summary.summary["general.file"], record3->GetSize()); + EXPECT_EQ(summary.summary["general.image"], record4->GetSize()); + EXPECT_EQ(summary.summary["SystemDefinedType"], record5->GetSize()); + EXPECT_EQ(summary.summary["openharmony.form"], record6->GetSize()); + EXPECT_EQ(summary.summary["applicationDefinedType"], record7->GetSize()); ASSERT_EQ(summary.totalSize, size); - ASSERT_EQ(summary.summary["general.text"], record1->GetSize()); - ASSERT_EQ(summary.summary["general.plain-text"], record2->GetSize()); - ASSERT_EQ(summary.summary["general.file"], record3->GetSize()); - ASSERT_EQ(summary.summary["general.image"], record4->GetSize()); - ASSERT_EQ(summary.summary["SystemDefinedType"], record5->GetSize()); - ASSERT_EQ(summary.summary["openharmony.form"], record6->GetSize()); - ASSERT_EQ(summary.summary["ApplicationDefinedType"], record7->GetSize()); LOG_INFO(UDMF_TEST, "GetSummary001 end."); } @@ -2678,4 +2678,215 @@ HWTEST_F(UdmfClientTest, GetSelfBundleName001, TestSize.Level1) EXPECT_NE(ret, ""); LOG_INFO(UDMF_TEST, "GetSelfBundleName001 end."); } + +/** +* @tc.name: GetSummary004 +* @tc.desc: Get summary data for entries +* @tc.type: FUNC +*/ +HWTEST_F(UdmfClientTest, GetSummary004, TestSize.Level1) +{ + LOG_INFO(UDMF_TEST, "GetSummary004 begin."); + + CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG }; + UnifiedData data; + std::string key; + + UDDetails details; + details.insert({ "udmf_key", "udmf_value" }); + + std::shared_ptr obj = std::make_shared(); + obj->value_[UNIFORM_DATA_TYPE] = "general.file-uri"; + obj->value_[FILE_URI_PARAM] = "http://demo.com"; + obj->value_[FILE_TYPE] = "abcdefg"; + auto record = std::make_shared(FILE_URI, obj); + auto size0 = record->GetSize(); + + auto plainText = PlainText("content", "abstract"); + auto size1 = plainText.GetSize(); + plainText.InitObject(); + record->AddEntry(plainText.GetUtdId(), plainText.GetOriginValue()); + + auto image = Image("uri"); + image.SetDetails(details); + auto size2 = image.GetSize(); + image.InitObject(); + record->AddEntry(image.GetUtdId(), image.GetOriginValue()); + + std::vector raw = {1, 2, 3, 4, 5}; + SystemDefinedPixelMap pixelMap = SystemDefinedPixelMap(raw); + pixelMap.SetDetails(details); + auto size3 = pixelMap.GetSize(); + pixelMap.InitObject(); + record->AddEntry(pixelMap.GetUtdId(), pixelMap.GetOriginValue()); + + raw = {1, 2, 3, 4, 5}; + auto applicationDefinedRecord = ApplicationDefinedRecord("my.type", raw); + auto size4 = applicationDefinedRecord.GetSize(); + applicationDefinedRecord.InitObject(); + record->AddEntry(applicationDefinedRecord.GetUtdId(), applicationDefinedRecord.GetOriginValue()); + + data.AddRecord(record); + + auto status = UdmfClient::GetInstance().SetData(option1, data, key); + ASSERT_EQ(status, E_OK); + + QueryOption option2 = { .key = key }; + Summary summary; + status = UdmfClient::GetInstance().GetSummary(option2, summary); + + ASSERT_EQ(status, E_OK); + EXPECT_EQ(summary.summary["general.file-uri"], size0); + EXPECT_EQ(summary.summary["general.plain-text"], size1); + EXPECT_EQ(summary.summary["general.image"], size2); + EXPECT_EQ(summary.summary["openharmony.pixel-map"], size3); + EXPECT_EQ(summary.summary["my.type"], size4); + + EXPECT_EQ(summary.totalSize, record->GetSize()); + + UnifiedData readData; + status = UdmfClient::GetInstance().GetData(option2, readData); + ASSERT_EQ(E_OK, status); + ASSERT_EQ(1, readData.GetRecords().size()); + auto readRecord = readData.GetRecordAt(0); + auto entries = readRecord->GetEntries(); + ASSERT_EQ(5, entries->size()); + auto readFileUri = std::get>(entries->at("general.file-uri")); + EXPECT_EQ("abcdefg", std::get(readFileUri->value_[FILE_TYPE])); + auto readPlainText = std::get>(entries->at("general.plain-text")); + EXPECT_EQ("abstract", std::get(readPlainText->value_[ABSTRACT])); + auto readImage = std::get>(entries->at("general.image")); + EXPECT_EQ("uri", std::get(readImage->value_[ORI_URI])); + auto readPixelMap = std::get>(entries->at("openharmony.pixel-map")); + EXPECT_EQ(5, std::get>(readPixelMap->value_[PIXEL_MAP]).size()); + auto readDefinedRecord = std::get>(entries->at("my.type")); + EXPECT_EQ(5, std::get>(readDefinedRecord->value_[ARRAY_BUFFER]).size()); + + auto value = std::get>(readRecord->GetValue()); + EXPECT_EQ("abcdefg", std::get(value->value_[FILE_TYPE])); + + LOG_INFO(UDMF_TEST, "GetSummary004 end."); +} + +/** +* @tc.name: GetSummary005 +* @tc.desc: Get summary data for entries +* @tc.type: FUNC +*/ +HWTEST_F(UdmfClientTest, GetSummary005, TestSize.Level1) +{ + LOG_INFO(UDMF_TEST, "GetSummary005 begin."); + + CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG }; + UnifiedData data; + std::string key; + + UDDetails details; + details.insert({ "udmf_key", "udmf_value" }); + + auto record = std::make_shared("content1", "content2"); + auto size0 = record->GetSize(); + + auto link = Link("url", "descritpion"); + auto size1 = link.GetSize(); + link.InitObject(); + record->AddEntry(link.GetUtdId(), link.GetOriginValue()); + + auto folder = Folder("uri"); + folder.SetDetails(details); + auto size2 = folder.GetSize(); + folder.InitObject(); + record->AddEntry(folder.GetUtdId(), folder.GetOriginValue()); + + std::vector raw = {1, 2, 3, 4, 5}; + auto applicationDefinedRecord1 = ApplicationDefinedRecord("your.type", raw); + auto size3 = applicationDefinedRecord1.GetSize(); + applicationDefinedRecord1.InitObject(); + record->AddEntry(applicationDefinedRecord1.GetUtdId(), applicationDefinedRecord1.GetOriginValue()); + + raw = {1, 2, 3, 4, 5}; + auto applicationDefinedRecord2 = ApplicationDefinedRecord("my.type", raw); + auto size4 = applicationDefinedRecord2.GetSize(); + applicationDefinedRecord2.InitObject(); + record->AddEntry(applicationDefinedRecord2.GetUtdId(), applicationDefinedRecord2.GetOriginValue()); + + auto form = SystemDefinedForm(); + form.SetDetails(details); + form.SetDetails(details); + form.SetFormId(123); + form.SetFormName("formName"); + form.SetModule("module"); + form.SetAbilityName("abilityName"); + form.SetBundleName("bundleName"); + auto size5 = form.GetSize(); + form.InitObject(); + record->AddEntry(form.GetUtdId(), form.GetOriginValue()); + + raw = {1, 2, 3, 4, 5}; + std::shared_ptr obj = std::make_shared(); + obj->value_[UNIFORM_DATA_TYPE] = "general.content-form"; + obj->value_[THUMB_DATA] = raw; + obj->value_[THUMB_DATA_LENGTH] = 5; + obj->value_[DESCRIPTION] = "descritpion"; + obj->value_[TITLE] = "title"; + obj->value_[APP_ICON] = raw; + obj->value_[APP_ICON_LENGTH] = 5; + obj->value_[APP_NAME] = "appName"; + obj->value_[LINK_URL] = "linkUri"; + auto contentForm = UnifiedRecord(CONTENT_FORM, obj); + auto size6 = contentForm.GetSize(); + record->AddEntry(contentForm.GetUtdId(), contentForm.GetOriginValue()); + + data.AddRecord(record); + + auto status = UdmfClient::GetInstance().SetData(option1, data, key); + ASSERT_EQ(status, E_OK); + + QueryOption option2 = { .key = key }; + Summary summary; + status = UdmfClient::GetInstance().GetSummary(option2, summary); + + LOG_INFO(UDMF_TEST, "GetSummary005 GetSummary."); + + ASSERT_EQ(status, E_OK); + EXPECT_EQ(summary.summary["general.html"], size0); + EXPECT_EQ(summary.summary["general.hyperlink"], size1); + EXPECT_EQ(summary.summary["general.folder"], size2); + EXPECT_EQ(summary.summary["your.type"], size3); + EXPECT_EQ(summary.summary["my.type"], size4); + EXPECT_EQ(summary.summary["openharmony.form"], size5); + EXPECT_EQ(summary.summary["general.content-form"], size6); + + EXPECT_EQ(summary.totalSize, record->GetSize()); + + UnifiedData readData; + status = UdmfClient::GetInstance().GetData(option2, readData); + + ASSERT_EQ(E_OK, status); + ASSERT_EQ(1, readData.GetRecords().size()); + auto readRecord = readData.GetRecordAt(0); + auto entries = readRecord->GetEntries(); + ASSERT_EQ(7, entries->size()); + + auto readHtml = std::get>(entries->at("general.html")); + EXPECT_EQ("content1", std::get(readHtml->value_[HTML_CONTENT])); + auto readHyperlink = std::get>(entries->at("general.hyperlink")); + EXPECT_EQ("descritpion", std::get(readHyperlink->value_[DESCRIPTION])); + auto readFolder = std::get>(entries->at("general.folder")); + EXPECT_EQ("uri", std::get(readFolder->value_[ORI_URI])); + auto readDefinedRecord = std::get>(entries->at("your.type")); + EXPECT_EQ(5, std::get>(readDefinedRecord->value_[ARRAY_BUFFER]).size()); + auto readDefinedRecord2 = std::get>(entries->at("my.type")); + EXPECT_EQ(5, std::get>(readDefinedRecord2->value_[ARRAY_BUFFER]).size()); + auto readForm = std::get>(entries->at("openharmony.form")); + EXPECT_EQ("module", std::get(readForm->value_[MODULE])); + auto readCotentForm = std::get>(entries->at("general.content-form")); + EXPECT_EQ("title", std::get(readCotentForm->value_[TITLE])); + + auto value = std::get>(readRecord->GetValue()); + EXPECT_EQ("content2", std::get(value->value_[PLAIN_CONTENT])); + + LOG_INFO(UDMF_TEST, "GetSummary005 end."); +} + } // OHOS::Test \ No newline at end of file diff --git a/interfaces/innerkits/common/unified_meta.h b/interfaces/innerkits/common/unified_meta.h index 2926b49..1541149 100644 --- a/interfaces/innerkits/common/unified_meta.h +++ b/interfaces/innerkits/common/unified_meta.h @@ -624,8 +624,12 @@ struct API_EXPORT Object { }; namespace ObjectUtils { - std::shared_ptr ConvertToObject(UDDetails &details); - UDDetails ConvertToUDDetails(std::shared_ptr object); + API_EXPORT std::shared_ptr ConvertToObject(UDDetails &details); + API_EXPORT UDDetails ConvertToUDDetails(std::shared_ptr object); + + int64_t GetValueSize(ValueType value); + int64_t GetObjectValueSize(std::shared_ptr object); + int64_t GetAllObjectSize(std::shared_ptr object); template bool ConvertVariant(T &&input, std::variant &output) diff --git a/interfaces/innerkits/data/unified_record.h b/interfaces/innerkits/data/unified_record.h index 9806374..c4d35a2 100644 --- a/interfaces/innerkits/data/unified_record.h +++ b/interfaces/innerkits/data/unified_record.h @@ -41,6 +41,7 @@ public: ValueType GetValue(); void SetValue(const ValueType &value); ValueType GetOriginValue() const; + int64_t GetValueSize(); void SetUtdId(const std::string &utdId); std::set GetUtdIds() const; @@ -49,7 +50,10 @@ public: bool HasType(const std::string &utdId) const; void AddEntry(const std::string &utdId, ValueType &&value); ValueType GetEntry(const std::string &utdId); - std::shared_ptr> GetEntries() const; + std::shared_ptr> GetEntries(); + std::shared_ptr> GetInnerEntries() const; + void SetInnerEntries(std::shared_ptr> entries); + int64_t GetEntriesSize(); void SetEntryGetter(const std::vector &utdIds, const std::shared_ptr &entryGetter); std::shared_ptr GetEntryGetter(); -- Gitee