1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "unified_data.h"
17 #include <cinttypes>
18 #include "logger.h"
19
20 namespace OHOS {
21 namespace UDMF {
GetSize()22 int64_t UnifiedData::GetSize()
23 {
24 int64_t totalSize = 0;
25 for (const auto &record : this->records_) {
26 totalSize += record->GetSize();
27 }
28 return totalSize;
29 }
30
GetGroupId() const31 std::string UnifiedData::GetGroupId() const
32 {
33 return this->runtime_->key.groupId;
34 }
35
GetRuntime() const36 std::shared_ptr<Runtime> UnifiedData::GetRuntime() const
37 {
38 return this->runtime_;
39 }
40
SetRuntime(Runtime & runtime)41 void UnifiedData::SetRuntime(Runtime &runtime)
42 {
43 this->runtime_ = std::make_shared<Runtime>(runtime);
44 }
45
AddRecord(const std::shared_ptr<UnifiedRecord> & record)46 void UnifiedData::AddRecord(const std::shared_ptr<UnifiedRecord> &record)
47 {
48 if (record == nullptr) {
49 return;
50 }
51 this->records_.push_back(record);
52 }
53
GetRecordAt(std::size_t index)54 std::shared_ptr<UnifiedRecord> UnifiedData::GetRecordAt(std::size_t index)
55 {
56 if (records_.size() > index) {
57 return records_[index];
58 }
59 return nullptr;
60 }
61
SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)62 void UnifiedData::SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)
63 {
64 this->records_ = std::move(records);
65 }
66
GetRecords() const67 std::vector<std::shared_ptr<UnifiedRecord>> UnifiedData::GetRecords() const
68 {
69 return this->records_;
70 }
71
GetUDTypes()72 std::vector<UDType> UnifiedData::GetUDTypes()
73 {
74 std::vector<UDType> typeSet;
75 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
76 typeSet.push_back(record->GetType());
77 }
78 return typeSet;
79 }
80
GetTypes()81 std::string UnifiedData::GetTypes()
82 {
83 std::string types;
84 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
85 types.append("-").append(UD_TYPE_MAP.at(record->GetType()));
86 }
87 return types;
88 }
89
IsEmpty() const90 bool UnifiedData::IsEmpty() const
91 {
92 return records_.empty();
93 }
94
IsValid()95 bool UnifiedData::IsValid()
96 {
97 if (this->IsEmpty()) {
98 LOG_ERROR(UDMF_FRAMEWORK, "Empty data without any record!");
99 return false;
100 }
101 if (this->GetSize() > MAX_DATA_SIZE) {
102 LOG_ERROR(UDMF_FRAMEWORK, "Exceeded data limit, UnifiedData size: %{public}" PRId64 " !", this->GetSize());
103 return false;
104 }
105 return true;
106 }
107
IsComplete()108 bool UnifiedData::IsComplete()
109 {
110 std::shared_ptr<Runtime> runtime = this->GetRuntime();
111 if (runtime == nullptr) {
112 return false;
113 }
114 if (static_cast<uint32_t>(this->GetRecords().size()) != runtime->recordTotalNum) {
115 LOG_ERROR(UDMF_FRAMEWORK,
116 "The records of unifiedData is incomplete, expected recordsNum is %{public}u, not %{public}zu.",
117 runtime->recordTotalNum, this->GetRecords().size());
118 return false;
119 }
120 return true;
121 }
122 } // namespace UDMF
123 } // namespace OHOS