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 {
21 static std::set<std::string> FILE_TYPES = {
22 "general.file", "general.image", "general.video", "general.audio", "general.folder", "general.file-uri" };
UnifiedData()23 UnifiedData::UnifiedData()
24 {
25 properties_ = std::make_shared<UnifiedDataProperties>();
26 auto duration = std::chrono::system_clock::now().time_since_epoch();
27 properties_->timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
28 }
29
UnifiedData(std::shared_ptr<UnifiedDataProperties> properties)30 UnifiedData::UnifiedData(std::shared_ptr<UnifiedDataProperties> properties)
31 {
32 properties_ = properties;
33 auto duration = std::chrono::system_clock::now().time_since_epoch();
34 properties_->timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
35 }
36
GetSize()37 int64_t UnifiedData::GetSize()
38 {
39 int64_t totalSize = 0;
40 for (const auto &record : this->records_) {
41 totalSize += record->GetSize();
42 }
43 return totalSize;
44 }
45
GetGroupId() const46 std::string UnifiedData::GetGroupId() const
47 {
48 return this->runtime_->key.groupId;
49 }
50
GetRuntime() const51 std::shared_ptr<Runtime> UnifiedData::GetRuntime() const
52 {
53 return this->runtime_;
54 }
55
SetRuntime(Runtime & runtime)56 void UnifiedData::SetRuntime(Runtime &runtime)
57 {
58 this->runtime_ = std::make_shared<Runtime>(runtime);
59 }
60
AddRecord(const std::shared_ptr<UnifiedRecord> & record)61 void UnifiedData::AddRecord(const std::shared_ptr<UnifiedRecord> &record)
62 {
63 if (record == nullptr) {
64 return;
65 }
66 record->SetRecordId(++recordId_);
67 this->records_.push_back(record);
68 }
69
AddRecords(const std::vector<std::shared_ptr<UnifiedRecord>> & records)70 void UnifiedData::AddRecords(const std::vector<std::shared_ptr<UnifiedRecord>> &records)
71 {
72 for (auto &record :records) {
73 if (record == nullptr) {
74 return;
75 }
76 record->SetRecordId(++recordId_);
77 this->records_.push_back(record);
78 }
79 }
80
GetRecordAt(std::size_t index)81 std::shared_ptr<UnifiedRecord> UnifiedData::GetRecordAt(std::size_t index)
82 {
83 if (records_.size() > index) {
84 return records_[index];
85 }
86 return nullptr;
87 }
88
SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)89 void UnifiedData::SetRecords(std::vector<std::shared_ptr<UnifiedRecord>> records)
90 {
91 this->records_ = std::move(records);
92 }
93
GetRecords() const94 std::vector<std::shared_ptr<UnifiedRecord>> UnifiedData::GetRecords() const
95 {
96 return this->records_;
97 }
98
GetTypesLabels() const99 std::vector<std::string> UnifiedData::GetTypesLabels() const
100 {
101 std::vector<std::string> types;
102 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
103 std::vector<std::string> recordTypes = record->GetTypes();
104 types.insert(types.end(),
105 std::make_move_iterator(recordTypes.begin()), std::make_move_iterator(recordTypes.end()));
106 }
107 return types;
108 }
109
HasType(const std::string & type) const110 bool UnifiedData::HasType(const std::string &type) const
111 {
112 for (const std::shared_ptr<UnifiedRecord> &record : records_) {
113 std::vector<std::string> recordTypes = record->GetTypes();
114 if (std::find(recordTypes.begin(), recordTypes.end(), type) != recordTypes.end()) {
115 return true;
116 }
117 }
118 return false;
119 }
120
GetEntriesTypes() const121 std::vector<std::string> UnifiedData::GetEntriesTypes() const
122 {
123 std::set<std::string> labels;
124 for (const auto &record : records_) {
125 auto types = record->GetUtdIds();
126 labels.insert(types.begin(), types.end());
127 }
128 return std::vector<std::string>(labels.begin(), labels.end());
129 }
130
HasTypeInEntries(const std::string & type) const131 bool UnifiedData::HasTypeInEntries(const std::string &type) const
132 {
133 for (const auto &record : records_) {
134 auto types = record->GetUtdIds();
135 if (types.find(type) != types.end()) {
136 return true;
137 }
138 }
139 return false;
140 }
141
IsEmpty() const142 bool UnifiedData::IsEmpty() const
143 {
144 return records_.empty();
145 }
146
IsValid()147 bool UnifiedData::IsValid()
148 {
149 if (this->IsEmpty()) {
150 LOG_ERROR(UDMF_FRAMEWORK, "Empty data without any record!");
151 return false;
152 }
153 if (this->GetSize() > MAX_DATA_SIZE) {
154 LOG_ERROR(UDMF_FRAMEWORK, "Exceeded data limit, UnifiedData size: %{public}" PRId64 " !", this->GetSize());
155 return false;
156 }
157 return true;
158 }
159
IsComplete()160 bool UnifiedData::IsComplete()
161 {
162 std::shared_ptr<Runtime> runtime = this->GetRuntime();
163 if (runtime == nullptr) {
164 return false;
165 }
166 if (static_cast<uint32_t>(this->GetRecords().size()) != runtime->recordTotalNum) {
167 LOG_ERROR(UDMF_FRAMEWORK,
168 "The records of unifiedData is incomplete, expected recordsNum is %{public}u, not %{public}zu.",
169 runtime->recordTotalNum, this->GetRecords().size());
170 return false;
171 }
172 return true;
173 }
174
HasFileType() const175 bool UnifiedData::HasFileType() const
176 {
177 auto types = GetTypIds();
178 std::set<std::string> intersection;
179 std::set_intersection(FILE_TYPES.begin(), FILE_TYPES.end(), types.begin(), types.end(),
180 std::inserter(intersection, intersection.begin()));
181 return !intersection.empty();
182 }
183
SetProperties(std::shared_ptr<UnifiedDataProperties> properties)184 void UnifiedData::SetProperties(std::shared_ptr<UnifiedDataProperties> properties)
185 {
186 if (!properties) {
187 LOG_ERROR(UDMF_FRAMEWORK, "properties is null!");
188 return;
189 }
190 properties->timestamp = properties_->timestamp;
191 properties_ = properties;
192 }
193
GetProperties() const194 std::shared_ptr<UnifiedDataProperties> UnifiedData::GetProperties() const
195 {
196 return properties_;
197 }
198
SetDataId(uint32_t dataId)199 void UnifiedData::SetDataId(uint32_t dataId)
200 {
201 dataId_ = dataId;
202 }
203
GetDataId() const204 uint32_t UnifiedData::GetDataId() const
205 {
206 return dataId_;
207 }
208
SetChannelName(const std::string & name)209 void UnifiedData::SetChannelName(const std::string &name)
210 {
211 channelName_ = std::move(name);
212 }
213
GetTypIds() const214 std::set<std::string> UnifiedData::GetTypIds() const
215 {
216 std::set<std::string> types;
217 for (const auto &record : records_) {
218 std::set<std::string> recordTypes = record->GetUtdIds();
219 types.insert(recordTypes.begin(), recordTypes.end());
220 }
221 return types;
222 }
223
GetFileUris() const224 std::vector<std::string> UnifiedData::GetFileUris() const
225 {
226 std::vector<std::string> uris;
227 for (auto record : records_) {
228 std::string oriUri;
229 if (record == nullptr || !record->HasFileType(oriUri)) {
230 continue;
231 }
232 uris.push_back(oriUri);
233 }
234 return uris;
235 }
236
237 } // namespace UDMF
238 } // namespace OHOS