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 #define LOG_TAG "UnifiedData"
16 #include "unified_data.h"
17 #include "logger.h"
18
19 namespace OHOS {
20 namespace UDMF {
UnifiedData()21 UnifiedData::UnifiedData()
22 {
23 properties_ = std::make_shared<UnifiedDataProperties>();
24 auto duration = std::chrono::system_clock::now().time_since_epoch();
25 properties_->timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
26 }
27
UnifiedData(std::shared_ptr<UnifiedDataProperties> properties)28 UnifiedData::UnifiedData(std::shared_ptr<UnifiedDataProperties> properties)
29 {
30 properties_ = properties;
31 auto duration = std::chrono::system_clock::now().time_since_epoch();
32 properties_->timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
33 }
34
GetSize()35 int64_t UnifiedData::GetSize()
36 {
37 int64_t totalSize = 0;
38 for (const auto &record : this->records_) {
39 totalSize += record->GetSize();
40 }
41 return totalSize;
42 }
43
GetGroupId() const44 std::string UnifiedData::GetGroupId() const
45 {
46 return this->runtime_->key.groupId;
47 }
48
GetRuntime() const49 std::shared_ptr<Runtime> UnifiedData::GetRuntime() const
50 {
51 return this->runtime_;
52 }
53
SetRuntime(Runtime & runtime)54 void UnifiedData::SetRuntime(Runtime &runtime)
55 {
56 this->runtime_ = std::make_shared<Runtime>(runtime);
57 }
58
AddRecord(const std::shared_ptr<UnifiedRecord> & record)59 void UnifiedData::AddRecord(const std::shared_ptr<UnifiedRecord> &record)
60 {
61 if (record == nullptr) {
62 return;
63 }
64 record->SetRecordId(++recordId_);
65 this->records_.push_back(record);
66 }
67
AddRecords(const std::vector<std::shared_ptr<UnifiedRecord>> & records)68 void UnifiedData::AddRecords(const std::vector<std::shared_ptr<UnifiedRecord>> &records)
69 {
70 for (auto &record :records) {
71 if (record == nullptr) {
72 return;
73 }
74 record->SetRecordId(++recordId_);
75 this->records_.push_back(record);
76 }
77 }
78
GetRecordAt(std::size_t index)79 std::shared_ptr<UnifiedRecord> UnifiedData::GetRecordAt(std::size_t index)
80 {
81 if (records_.size() > index) {
82 return records_[index];
83 }
84 return nullptr;
85 }
86
SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)87 void UnifiedData::SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)
88 {
89 this->records_ = std::move(records);
90 }
91
GetRecords() const92 std::vector<std::shared_ptr<UnifiedRecord>> UnifiedData::GetRecords() const
93 {
94 return this->records_;
95 }
96
GetTypes()97 std::string UnifiedData::GetTypes()
98 {
99 std::string types;
100 for (const auto &record : records_) {
101 for (const auto &type : record->GetUtdIds()) {
102 types.append("-").append(type);
103 }
104 }
105 return types;
106 }
107
GetTypesLabels() const108 std::vector<std::string> UnifiedData::GetTypesLabels() const
109 {
110 std::vector<std::string> types;
111 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
112 types.push_back(UtdUtils::GetUtdIdFromUtdEnum(record->GetType()));
113 }
114 return types;
115 }
116
HasType(const std::string & type) const117 bool UnifiedData::HasType(const std::string &type) const
118 {
119 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
120 if (UtdUtils::GetUtdIdFromUtdEnum(record->GetType()) == type) {
121 return true;
122 }
123 }
124 return false;
125 }
126
GetEntriesTypes() const127 std::vector<std::string> UnifiedData::GetEntriesTypes() const
128 {
129 std::set<std::string> labels;
130 for (const auto &record : records_) {
131 auto types = record->GetUtdIds();
132 labels.insert(types.begin(), types.end());
133 }
134 return std::vector<std::string>(labels.begin(), labels.end());
135 }
136
HasTypeInEntries(const std::string & type) const137 bool UnifiedData::HasTypeInEntries(const std::string &type) const
138 {
139 for (const auto &record : records_) {
140 auto types = record->GetUtdIds();
141 if (types.find(type) != types.end()) {
142 return true;
143 }
144 }
145 return false;
146 }
147
IsEmpty() const148 bool UnifiedData::IsEmpty() const
149 {
150 return records_.empty();
151 }
152
IsValid()153 bool UnifiedData::IsValid()
154 {
155 if (this->IsEmpty()) {
156 LOG_ERROR(UDMF_FRAMEWORK, "Empty data without any record!");
157 return false;
158 }
159 if (this->GetSize() > MAX_DATA_SIZE) {
160 LOG_ERROR(UDMF_FRAMEWORK, "Exceeded data limit, UnifiedData size: %{public}" PRId64 " !", this->GetSize());
161 return false;
162 }
163 return true;
164 }
165
IsComplete()166 bool UnifiedData::IsComplete()
167 {
168 std::shared_ptr<Runtime> runtime = this->GetRuntime();
169 if (runtime == nullptr) {
170 return false;
171 }
172 if (static_cast<uint32_t>(this->GetRecords().size()) != runtime->recordTotalNum) {
173 LOG_ERROR(UDMF_FRAMEWORK,
174 "The records of unifiedData is incomplete, expected recordsNum is %{public}u, not %{public}zu.",
175 runtime->recordTotalNum, this->GetRecords().size());
176 return false;
177 }
178 return true;
179 }
180
SetProperties(std::shared_ptr<UnifiedDataProperties> properties)181 void UnifiedData::SetProperties(std::shared_ptr<UnifiedDataProperties> properties)
182 {
183 if (!properties) {
184 LOG_ERROR(UDMF_FRAMEWORK, "properties is null!");
185 return;
186 }
187 properties->timestamp = properties_->timestamp;
188 properties_ = properties;
189 }
190
GetProperties() const191 std::shared_ptr<UnifiedDataProperties> UnifiedData::GetProperties() const
192 {
193 return properties_;
194 }
195
SetDataId(uint32_t dataId)196 void UnifiedData::SetDataId(uint32_t dataId)
197 {
198 dataId_ = dataId;
199 }
200
GetDataId() const201 uint32_t UnifiedData::GetDataId() const
202 {
203 return dataId_;
204 }
205
SetChannelName(const std::string & name)206 void UnifiedData::SetChannelName(const std::string &name)
207 {
208 channelName_ = std::move(name);
209 }
210 } // namespace UDMF
211 } // namespace OHOS