From e20ff275b73f2496e86962eaf33bf5ac2ba9ade9 Mon Sep 17 00:00:00 2001 From: lizhuojun Date: Mon, 16 Dec 2024 10:16:14 +0800 Subject: [PATCH] store by slice Signed-off-by: lizhuojun --- framework/common/udmf_types_util.cpp | 2 - framework/common/unittest/BUILD.gn | 5 +- .../innerkitsimpl/common/unified_meta.cpp | 74 +++++++++++++++++++ .../data/application_defined_record.cpp | 2 +- framework/innerkitsimpl/data/audio.cpp | 5 +- framework/innerkitsimpl/data/file.cpp | 2 +- framework/innerkitsimpl/data/folder.cpp | 5 +- framework/innerkitsimpl/data/image.cpp | 5 +- framework/innerkitsimpl/data/plain_text.cpp | 1 + .../data/system_defined_form.cpp | 4 +- .../data/unified_data_helper.cpp | 10 +-- .../innerkitsimpl/data/unified_record.cpp | 2 +- framework/innerkitsimpl/data/video.cpp | 5 +- .../service/udmf_service_proxy.cpp | 3 + .../unittest/udmf_client_sa_invoke_test.cpp | 49 +++++++++++- .../test/unittest/udmf_client_test.cpp | 18 ++--- interfaces/innerkits/common/unified_meta.h | 8 +- .../innerkits/convert/udmf_conversion.h | 11 ++- 18 files changed, 166 insertions(+), 45 deletions(-) mode change 100755 => 100644 framework/innerkitsimpl/data/plain_text.cpp diff --git a/framework/common/udmf_types_util.cpp b/framework/common/udmf_types_util.cpp index 47b9aa8..b4dda78 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/common/unittest/BUILD.gn b/framework/common/unittest/BUILD.gn index 4afce7f..42cbb07 100644 --- a/framework/common/unittest/BUILD.gn +++ b/framework/common/unittest/BUILD.gn @@ -129,7 +129,10 @@ ohos_unittest("UdmfTypesUtilAbnormalTest") { ohos_unittest("TlvUtilTest") { module_out_path = module_output_path - sources = [ "tlv_util_test.cpp" ] + sources = [ + "${udmf_framework_path}/innerkitsimpl/convert/udmf_conversion.cpp", + "tlv_util_test.cpp" + ] configs = [ ":module_private_config" ] diff --git a/framework/innerkitsimpl/common/unified_meta.cpp b/framework/innerkitsimpl/common/unified_meta.cpp index 8aa8769..1783c36 100644 --- a/framework/innerkitsimpl/common/unified_meta.cpp +++ b/framework/innerkitsimpl/common/unified_meta.cpp @@ -674,6 +674,9 @@ std::shared_ptr ObjectUtils::ConvertToObject(UDDetails &details) UDDetails ObjectUtils::ConvertToUDDetails(std::shared_ptr object) { UDDetails details; + if (object == nullptr) { + return details; + } for (auto [key, value] : object->value_) { if (!ConvertVariant(std::move(value), details[key])) { LOG_ERROR(UnifiedRecord, "object convert to UDDetails failed, object key is %{public}s", key.c_str()); @@ -681,5 +684,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/data/application_defined_record.cpp b/framework/innerkitsimpl/data/application_defined_record.cpp index 350a344..81e6d8d 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(); } 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..80939e2 100644 --- a/framework/innerkitsimpl/data/file.cpp +++ b/framework/innerkitsimpl/data/file.cpp @@ -45,7 +45,7 @@ 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_); } 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/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/plain_text.cpp b/framework/innerkitsimpl/data/plain_text.cpp old mode 100755 new mode 100644 index 9a28b4f..0b505ef --- a/framework/innerkitsimpl/data/plain_text.cpp +++ b/framework/innerkitsimpl/data/plain_text.cpp @@ -21,6 +21,7 @@ namespace OHOS { namespace UDMF { PlainText::PlainText() : PlainText("", "") { + SetType(PLAIN_TEXT); } PlainText::PlainText(const std::string &content, const std::string &abstract) diff --git a/framework/innerkitsimpl/data/system_defined_form.cpp b/framework/innerkitsimpl/data/system_defined_form.cpp index 1bfcbc3..6ffd9b5 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_); diff --git a/framework/innerkitsimpl/data/unified_data_helper.cpp b/framework/innerkitsimpl/data/unified_data_helper.cpp index a7a1303..1e9bb89 100644 --- a/framework/innerkitsimpl/data/unified_data_helper.cpp +++ b/framework/innerkitsimpl/data/unified_data_helper.cpp @@ -32,7 +32,6 @@ constexpr mode_t MODE = 0700; static constexpr int64_t MAX_KV_RECORD_SIZE = 2 * 1024 * 1024; static constexpr int64_t MAX_KV_DATA_SIZE = 4 * 1024 * 1024; static constexpr int64_t MAX_IPC_RAW_DATA_SIZE = 128 * 1024 * 1024; -static constexpr int64_t MAX_SA_DRAG_RECORD_SIZE = 3.5 * 1024 * 1024; constexpr const char *TEMP_UNIFIED_DATA_ROOT_PATH = "data/storage/el2/base/temp/udata"; constexpr const char *TEMP_UNIFIED_DATA_SUFFIX = ".ud"; @@ -118,6 +117,8 @@ void UnifiedDataHelper::GetSummary(const UnifiedData &data, Summary &summary) bool UnifiedDataHelper::Pack(UnifiedData &data) { + UdmfConversion::InitValueObject(data); + Summary summary; GetSummary(data, summary); @@ -233,13 +234,6 @@ int32_t UnifiedDataHelper::ProcessBigData(UnifiedData &data, Intention intention LOG_ERROR(UDMF_SERVICE, "Exceeded ipc-send data limit, totalSize:%{public}" PRId64 " !", size); return E_INVALID_PARAMETERS; } - for (const auto &record : data.GetRecords()) { - auto recordSize = record->GetSize(); - if (recordSize > MAX_SA_DRAG_RECORD_SIZE) { - LOG_ERROR(UDMF_SERVICE, "Exceeded drag single record limit, totalSize:%{public}" PRId64 " !", recordSize); - return E_INVALID_PARAMETERS; - } - } LOG_DEBUG(UDMF_SERVICE, "Processing udmf data in memory"); return E_OK; } diff --git a/framework/innerkitsimpl/data/unified_record.cpp b/framework/innerkitsimpl/data/unified_record.cpp index 7c3d4eb..f7ed309 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 ObjectUtils::GetValueSize(value_); } std::string UnifiedRecord::GetUid() const 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::vector(readData.GetRecordAt(0)); + EXPECT_EQ(4 * 1024 * 1024, readPixelMap1->GetRawData().size()); LOG_INFO(UDMF_TEST, "SetData004 end."); } @@ -232,12 +240,20 @@ HWTEST_F(UdmfClientSaInvokeTest, SetData006, TestSize.Level1) CustomOption option = { .intention = Intention::UD_INTENTION_DRAG }; std::vector rawData; - for (int i = 0; i < 3.5 * 1024 * 1024; i++) { + for (int i = 0; i < 5 * 1024 * 1024; i++) { rawData.emplace_back(1); } auto pixelMap = std::make_shared(rawData); UnifiedData data; data.AddRecord(pixelMap); + + std::vector rawData2; + for (int i = 0; i < 10 * 1024 * 1024; i++) { + rawData2.emplace_back(1); + } + auto pixelMap2 = std::make_shared(rawData2); + data.AddRecord(pixelMap2); + std::string key; auto status = UdmfClient::GetInstance().SetData(option, data, key); ASSERT_EQ(E_OK, status); @@ -246,10 +262,35 @@ HWTEST_F(UdmfClientSaInvokeTest, SetData006, TestSize.Level1) QueryOption queryOption = {.key = key}; status = UdmfClient::GetInstance().GetData(queryOption, readData); ASSERT_EQ(E_OK, status); - ASSERT_EQ(1, readData.GetRecords().size()); + ASSERT_EQ(2, readData.GetRecords().size()); auto readPixelMap = std::static_pointer_cast(readData.GetRecordAt(0)); - EXPECT_EQ(3.5 * 1024 * 1024, readPixelMap->GetRawData().size()); + EXPECT_EQ(5 * 1024 * 1024, readPixelMap->GetRawData().size()); + + auto readPixelMap2 = std::static_pointer_cast(readData.GetRecordAt(1)); + EXPECT_EQ(10 * 1024 * 1024, readPixelMap2->GetRawData().size()); LOG_INFO(UDMF_TEST, "SetData006 end."); } +/** +* @tc.name: SetData007 +* @tc.desc: Test set data error. Record size equals 4M. +* @tc.type: FUNC +*/ +HWTEST_F(UdmfClientSaInvokeTest, SetData007, TestSize.Level1) +{ + LOG_INFO(UDMF_TEST, "SetData007 begin."); + + CustomOption option = { .intention = Intention::UD_INTENTION_DRAG }; + std::vector rawData; + for (int i = 0; i < 128 * 1024 * 1024 + 1; i++) { + rawData.emplace_back(1); + } + auto pixelMap = std::make_shared(rawData); + UnifiedData data; + data.AddRecord(pixelMap); + std::string key; + auto status = UdmfClient::GetInstance().SetData(option, data, key); + ASSERT_EQ(E_INVALID_PARAMETERS, status); +} + } // OHOS::Test \ No newline at end of file diff --git a/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp b/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp index bd3a3df..f9176c4 100644 --- a/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp +++ b/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp @@ -1248,15 +1248,15 @@ HWTEST_F(UdmfClientTest, GetSummary001, TestSize.Level1) size += record6->GetSize(); size += record7->GetSize(); - ASSERT_EQ(status, E_OK); - 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()); + EXPECT_EQ(status, E_OK); + EXPECT_EQ(summary.totalSize, size); + 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()); LOG_INFO(UDMF_TEST, "GetSummary001 end."); } diff --git a/interfaces/innerkits/common/unified_meta.h b/interfaces/innerkits/common/unified_meta.h index 2926b49..a9f7670 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); + std::shared_ptr API_EXPORT ConvertToObject(UDDetails &details); + UDDetails API_EXPORT 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/convert/udmf_conversion.h b/interfaces/innerkits/convert/udmf_conversion.h index 80e35de..ebef4e2 100644 --- a/interfaces/innerkits/convert/udmf_conversion.h +++ b/interfaces/innerkits/convert/udmf_conversion.h @@ -17,17 +17,16 @@ #define UDMF_UDMF_CONVERSION_H #include "unified_data.h" -#include "visibility.h" namespace OHOS::UDMF { class UdmfConversion { public: - static void API_EXPORT ConvertRecordToSubclass(std::shared_ptr &record); - static void API_EXPORT ConvertRecordToSubclass(UnifiedData &data); - static void API_EXPORT ConvertRecordToSubclass(std::vector &datas); + static void ConvertRecordToSubclass(std::shared_ptr &record); + static void ConvertRecordToSubclass(UnifiedData &data); + static void ConvertRecordToSubclass(std::vector &datas); - static void API_EXPORT InitValueObject(UnifiedData &data); - static void API_EXPORT InitValueObject(std::vector &datas); + static void InitValueObject(UnifiedData &data); + static void InitValueObject(std::vector &datas); private: static void SetValueWhenNotUds(std::shared_ptr record); -- Gitee