2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file src/storage/StorageDeserializer.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Implementation for Cynara::StorageDeserializer
27 #include <exceptions/BucketDeserializationException.h>
28 #include <exceptions/BucketRecordCorruptedException.h>
29 #include <types/PolicyType.h>
31 #include <storage/BucketDeserializer.h>
32 #include <storage/Buckets.h>
33 #include <storage/StorageSerializer.h>
35 #include "StorageDeserializer.h"
39 StorageDeserializer::StorageDeserializer(std::shared_ptr<std::istream> inStream,
40 BucketStreamOpener bucketStreamOpener)
41 : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) {
44 void StorageDeserializer::initBuckets(Buckets &buckets) {
47 for (std::size_t lineNum = 1; !m_inStream->eof(); ++lineNum) {
49 std::getline(*m_inStream, line, StorageSerializer::recordSeparator());
54 std::size_t beginToken = 0;
55 auto bucketId = parseBucketId(line, beginToken);
56 auto policyType = parsePolicyType(line, beginToken);
57 auto metadata = parseMetadata(line, beginToken);
59 //it's safe to simply insert; buckets were cleared earlier, all ids should be unique
60 buckets.insert({ bucketId, PolicyBucket(bucketId, PolicyResult(policyType, metadata)) });
64 void StorageDeserializer::loadBuckets(Buckets &buckets) {
65 for (auto &bucketIter : buckets) {
66 const auto &bucketId = bucketIter.first;
67 auto &bucket = bucketIter.second;
69 auto bucketDeserializer = m_bucketStreamOpener(bucketId);
70 if (bucketDeserializer != nullptr) {
71 const auto policies = bucketDeserializer->loadPolicies();
72 for (const auto policy : policies) {
73 bucket.insertPolicy(policy);
76 throw BucketDeserializationException(bucketId);
81 PolicyBucketId StorageDeserializer::parseBucketId(const std::string &line,
82 std::size_t &beginToken) {
83 auto bucketNameEndToken = line.find(StorageSerializer::fieldSeparator(), beginToken);
84 if (bucketNameEndToken != std::string::npos) {
85 auto bucketName = line.substr(beginToken, bucketNameEndToken - beginToken);
86 beginToken = bucketNameEndToken + 1;
90 // TODO: Should throw other exception
91 throw BucketRecordCorruptedException(line);
94 PolicyType StorageDeserializer::parsePolicyType(const std::string &line, std::size_t &beginToken) {
95 PolicyType policyType;
97 std::size_t newBegin = 0;
98 policyType = std::stoi(line.substr(beginToken), &newBegin, 16);
99 beginToken += newBegin;
101 throw BucketRecordCorruptedException(line);
107 PolicyResult::PolicyMetadata StorageDeserializer::parseMetadata(const std::string &line,
108 std::size_t &beginToken) {
109 if (beginToken < line.size()) {
110 auto ret = line.substr(beginToken + 1);
111 beginToken = line.size();
115 return std::string();
118 } /* namespace Cynara */