/src/storage/BucketDeserializer.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 82 lines · 47 code · 13 blank · 22 comment · 6 complexity · 1b852cd05f30687225fec92007d9a71f 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/BucketDeserializer.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Methods implementation of Cynara::BucketDeserializer
  21. */
  22. #include <array>
  23. #include <memory>
  24. #include <string>
  25. #include <vector>
  26. #include <config/PathConfig.h>
  27. #include <exceptions/BucketRecordCorruptedException.h>
  28. #include <types/PolicyCollection.h>
  29. #include <types/PolicyResult.h>
  30. #include <types/PolicyType.h>
  31. #include <storage/StorageDeserializer.h>
  32. #include "BucketDeserializer.h"
  33. namespace Cynara {
  34. PolicyCollection BucketDeserializer::loadPolicies(void) {
  35. PolicyCollection policies;
  36. // TODO: Get someone smart to do error checking on stream
  37. std::string line;
  38. std::size_t lineNum = 1;
  39. while(std::getline(*m_inStream, line, PathConfig::StoragePath::recordSeparator)) {
  40. if (line.empty())
  41. break;
  42. try {
  43. std::size_t beginToken = 0;
  44. auto policyKey = parseKey(line, beginToken);
  45. auto policyType = StorageDeserializer::parsePolicyType(line, beginToken);
  46. auto metadata = StorageDeserializer::parseMetadata(line, beginToken);
  47. PolicyResult policyResult(policyType, metadata);
  48. policies.push_back(std::make_shared<Policy>(policyKey, policyResult));
  49. } catch (const BucketRecordCorruptedException &ex) {
  50. throw ex.withLineNumber(lineNum);
  51. }
  52. ++lineNum;
  53. }
  54. return policies;
  55. }
  56. PolicyKey BucketDeserializer::parseKey(const std::string &line, std::size_t &beginToken) {
  57. std::array<std::string, 3> keyFeatures;
  58. for (std::size_t tokenNum = 0; tokenNum < keyFeatures.size(); ++tokenNum) {
  59. auto endToken = line.find(PathConfig::StoragePath::fieldSeparator, beginToken);
  60. if (endToken != std::string::npos) {
  61. keyFeatures[tokenNum] = line.substr(beginToken, endToken - beginToken);
  62. beginToken = endToken + 1;
  63. } else {
  64. throw BucketRecordCorruptedException(line);
  65. }
  66. }
  67. return PolicyKey(keyFeatures[0], keyFeatures[1], keyFeatures[2]);
  68. }
  69. } /* namespace Cynara */