/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "UnifiedData" #include "unified_data.h" #include "logger.h" namespace OHOS { namespace UDMF { UnifiedData::UnifiedData() { properties_ = std::make_shared(); auto duration = std::chrono::system_clock::now().time_since_epoch(); properties_->timestamp = std::chrono::duration_cast(duration).count(); } UnifiedData::UnifiedData(std::shared_ptr properties) { properties_ = properties; auto duration = std::chrono::system_clock::now().time_since_epoch(); properties_->timestamp = std::chrono::duration_cast(duration).count(); } int64_t UnifiedData::GetSize() { int64_t totalSize = 0; for (const auto &record : this->records_) { totalSize += record->GetSize(); } return totalSize; } std::string UnifiedData::GetGroupId() const { return this->runtime_->key.groupId; } std::shared_ptr UnifiedData::GetRuntime() const { return this->runtime_; } void UnifiedData::SetRuntime(Runtime &runtime) { this->runtime_ = std::make_shared(runtime); } void UnifiedData::AddRecord(const std::shared_ptr &record) { if (record == nullptr) { return; } record->SetRecordId(++recordId_); this->records_.push_back(record); } void UnifiedData::AddRecords(const std::vector> &records) { for (auto &record :records) { if (record == nullptr) { return; } record->SetRecordId(++recordId_); this->records_.push_back(record); } } std::shared_ptr UnifiedData::GetRecordAt(std::size_t index) { if (records_.size() > index) { return records_[index]; } return nullptr; } void UnifiedData::SetRecords(std::vector> records) { this->records_ = std::move(records); } std::vector> UnifiedData::GetRecords() const { return this->records_; } std::string UnifiedData::GetTypes() { std::string types; for (const auto &record : records_) { for (const auto &type : record->GetUtdIds()) { types.append("-").append(type); } } return types; } std::vector UnifiedData::GetTypesLabels() const { std::vector types; for (const std::shared_ptr &record : records_) { types.push_back(UtdUtils::GetUtdIdFromUtdEnum(record->GetType())); } return types; } bool UnifiedData::HasType(const std::string &type) const { for (const std::shared_ptr &record : records_) { if (UtdUtils::GetUtdIdFromUtdEnum(record->GetType()) == type) { return true; } } return false; } std::vector UnifiedData::GetEntriesTypes() const { std::set labels; for (const auto &record : records_) { auto types = record->GetUtdIds(); labels.insert(types.begin(), types.end()); } return std::vector(labels.begin(), labels.end()); } bool UnifiedData::HasTypeInEntries(const std::string &type) const { for (const auto &record : records_) { auto types = record->GetUtdIds(); if (types.find(type) != types.end()) { return true; } } return false; } bool UnifiedData::IsEmpty() const { return records_.empty(); } bool UnifiedData::IsValid() { if (this->IsEmpty()) { LOG_ERROR(UDMF_FRAMEWORK, "Empty data without any record!"); return false; } if (this->GetSize() > MAX_DATA_SIZE) { LOG_ERROR(UDMF_FRAMEWORK, "Exceeded data limit, UnifiedData size: %{public}" PRId64 " !", this->GetSize()); return false; } return true; } bool UnifiedData::IsComplete() { std::shared_ptr runtime = this->GetRuntime(); if (runtime == nullptr) { return false; } if (static_cast(this->GetRecords().size()) != runtime->recordTotalNum) { LOG_ERROR(UDMF_FRAMEWORK, "The records of unifiedData is incomplete, expected recordsNum is %{public}u, not %{public}zu.", runtime->recordTotalNum, this->GetRecords().size()); return false; } return true; } void UnifiedData::SetProperties(std::shared_ptr properties) { if (!properties) { LOG_ERROR(UDMF_FRAMEWORK, "properties is null!"); return; } properties->timestamp = properties_->timestamp; properties_ = properties; } std::shared_ptr UnifiedData::GetProperties() const { return properties_; } void UnifiedData::SetDataId(uint32_t dataId) { dataId_ = dataId; } uint32_t UnifiedData::GetDataId() const { return dataId_; } void UnifiedData::SetChannelName(const std::string &name) { channelName_ = std::move(name); } } // namespace UDMF } // namespace OHOS