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 buckets[bucketId] = PolicyBucket(bucketId, PolicyResult(policyType, metadata));
63 void StorageDeserializer::loadBuckets(Buckets &buckets) {
64 for (auto &bucketIter : buckets) {
65 const auto &bucketId = bucketIter.first;
66 auto &bucket = bucketIter.second;
68 auto bucketDeserializer = m_bucketStreamOpener(bucketId);
69 if (bucketDeserializer != nullptr) {
70 const auto policies = bucketDeserializer->loadPolicies();
71 for (const auto policy : policies) {
72 bucket.insertPolicy(policy);
75 throw BucketDeserializationException(bucketId);
80 PolicyBucketId StorageDeserializer::parseBucketId(const std::string &line,
81 std::size_t &beginToken) {
82 auto bucketNameEndToken = line.find(StorageSerializer::fieldSeparator(), beginToken);
83 if (bucketNameEndToken != std::string::npos) {
84 auto bucketName = line.substr(beginToken, bucketNameEndToken - beginToken);
85 beginToken = bucketNameEndToken + 1;
89 // TODO: Should throw other exception
90 throw BucketRecordCorruptedException(line);
93 PolicyType StorageDeserializer::parsePolicyType(const std::string &line, std::size_t &beginToken) {
94 PolicyType policyType;
96 std::size_t newBegin = 0;
97 policyType = std::stoi(line.substr(beginToken), &newBegin, 16);
98 beginToken += newBegin;
100 throw BucketRecordCorruptedException(line);
106 PolicyResult::PolicyMetadata StorageDeserializer::parseMetadata(const std::string &line,
107 std::size_t &beginToken) {
108 if (beginToken < line.size()) {
109 auto ret = line.substr(beginToken + 1);
110 beginToken = line.size();
114 return std::string();
117 } /* namespace Cynara */