PageRenderTime 43ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/storage/StorageDeserializer.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 118 lines | 76 code | 19 blank | 23 comment | 10 complexity | 21e1ab6f68628debff3f0525e0feac6a MD5 | raw file
  1. /*
  2. * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. /**
  17. * @file src/storage/StorageDeserializer.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Implementation for Cynara::StorageDeserializer
  21. */
  22. #include <istream>
  23. #include <memory>
  24. #include <string>
  25. #include <config/PathConfig.h>
  26. #include <exceptions/BucketDeserializationException.h>
  27. #include <exceptions/BucketRecordCorruptedException.h>
  28. #include <types/PolicyType.h>
  29. #include <storage/BucketDeserializer.h>
  30. #include <storage/Buckets.h>
  31. #include "StorageDeserializer.h"
  32. namespace Cynara {
  33. StorageDeserializer::StorageDeserializer(std::shared_ptr<std::istream> inStream,
  34. BucketStreamOpener bucketStreamOpener)
  35. : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) {
  36. }
  37. void StorageDeserializer::initBuckets(Buckets &buckets) {
  38. buckets.clear();
  39. std::size_t lineNum = 1;
  40. std::string line;
  41. while (std::getline(*m_inStream, line, PathConfig::StoragePath::recordSeparator)) {
  42. if (line.empty())
  43. break;
  44. std::size_t beginToken = 0;
  45. auto bucketId = parseBucketId(line, beginToken);
  46. auto policyType = parsePolicyType(line, beginToken);
  47. auto metadata = parseMetadata(line, beginToken);
  48. //it's safe to simply insert; buckets were cleared earlier, all ids should be unique
  49. buckets.insert({ bucketId, PolicyBucket(bucketId, PolicyResult(policyType, metadata)) });
  50. ++lineNum;
  51. }
  52. }
  53. void StorageDeserializer::loadBuckets(Buckets &buckets) {
  54. for (auto &bucketIter : buckets) {
  55. const auto &bucketId = bucketIter.first;
  56. auto &bucket = bucketIter.second;
  57. auto bucketDeserializer = m_bucketStreamOpener(bucketId);
  58. if (bucketDeserializer != nullptr) {
  59. const auto policies = bucketDeserializer->loadPolicies();
  60. for (const auto policy : policies) {
  61. bucket.insertPolicy(policy);
  62. }
  63. } else {
  64. throw BucketDeserializationException(bucketId);
  65. }
  66. }
  67. }
  68. PolicyBucketId StorageDeserializer::parseBucketId(const std::string &line,
  69. std::size_t &beginToken) {
  70. auto bucketNameEndToken = line.find(PathConfig::StoragePath::fieldSeparator, beginToken);
  71. if (bucketNameEndToken != std::string::npos) {
  72. auto bucketName = line.substr(beginToken, bucketNameEndToken - beginToken);
  73. beginToken = bucketNameEndToken + 1;
  74. return bucketName;
  75. }
  76. // TODO: Should throw other exception
  77. throw BucketRecordCorruptedException(line);
  78. }
  79. PolicyType StorageDeserializer::parsePolicyType(const std::string &line, std::size_t &beginToken) {
  80. PolicyType policyType;
  81. try {
  82. std::size_t newBegin = 0;
  83. policyType = std::stoi(line.substr(beginToken), &newBegin, 16);
  84. beginToken += newBegin;
  85. } catch (...) {
  86. throw BucketRecordCorruptedException(line);
  87. }
  88. return policyType;
  89. }
  90. PolicyResult::PolicyMetadata StorageDeserializer::parseMetadata(const std::string &line,
  91. std::size_t &beginToken) {
  92. if (beginToken < line.size()) {
  93. auto ret = line.substr(beginToken + 1);
  94. beginToken = line.size();
  95. return ret;
  96. }
  97. return std::string();
  98. }
  99. } /* namespace Cynara */