skiff_schema.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "skiff_schema.h"
  2. #include "skiff.h"
  3. #include <util/generic/hash.h>
  4. namespace NSkiff {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. bool operator==(const TSkiffSchema& lhs, const TSkiffSchema& rhs)
  7. {
  8. if (lhs.GetWireType() != rhs.GetWireType() || lhs.GetName() != rhs.GetName()) {
  9. return false;
  10. }
  11. const auto& lhsChildren = lhs.GetChildren();
  12. const auto& rhsChildren = rhs.GetChildren();
  13. return std::equal(
  14. std::begin(lhsChildren),
  15. std::end(lhsChildren),
  16. std::begin(rhsChildren),
  17. std::end(rhsChildren),
  18. TSkiffSchemaPtrEqual());
  19. }
  20. bool operator!=(const TSkiffSchema& lhs, const TSkiffSchema& rhs)
  21. {
  22. return !(lhs == rhs);
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////
  25. void PrintShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema, IOutputStream* out)
  26. {
  27. (*out) << ToString(schema->GetWireType());
  28. if (!IsSimpleType(schema->GetWireType())) {
  29. auto children = schema->GetChildren();
  30. if (!children.empty()) {
  31. (*out) << '<';
  32. for (const auto& child : children) {
  33. PrintShortDebugString(child, out);
  34. (*out) << ';';
  35. }
  36. (*out) << '>';
  37. }
  38. }
  39. }
  40. TString GetShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema)
  41. {
  42. TStringStream out;
  43. PrintShortDebugString(schema, &out);
  44. return out.Str();
  45. }
  46. std::shared_ptr<TSimpleTypeSchema> CreateSimpleTypeSchema(EWireType type)
  47. {
  48. return std::make_shared<TSimpleTypeSchema>(type);
  49. }
  50. static void VerifyNonemptyChildren(const TSkiffSchemaList& children, EWireType wireType)
  51. {
  52. if (children.empty()) {
  53. ythrow TSkiffException() << "\"" << ToString(wireType) << "\" must have at least one child";
  54. }
  55. }
  56. std::shared_ptr<TTupleSchema> CreateTupleSchema(TSkiffSchemaList children)
  57. {
  58. return std::make_shared<TTupleSchema>(std::move(children));
  59. }
  60. std::shared_ptr<TVariant8Schema> CreateVariant8Schema(TSkiffSchemaList children)
  61. {
  62. VerifyNonemptyChildren(children, EWireType::Variant8);
  63. return std::make_shared<TVariant8Schema>(std::move(children));
  64. }
  65. std::shared_ptr<TVariant16Schema> CreateVariant16Schema(TSkiffSchemaList children)
  66. {
  67. VerifyNonemptyChildren(children, EWireType::Variant16);
  68. return std::make_shared<TVariant16Schema>(std::move(children));
  69. }
  70. std::shared_ptr<TRepeatedVariant8Schema> CreateRepeatedVariant8Schema(TSkiffSchemaList children)
  71. {
  72. VerifyNonemptyChildren(children, EWireType::RepeatedVariant8);
  73. return std::make_shared<TRepeatedVariant8Schema>(std::move(children));
  74. }
  75. std::shared_ptr<TRepeatedVariant16Schema> CreateRepeatedVariant16Schema(TSkiffSchemaList children)
  76. {
  77. VerifyNonemptyChildren(children, EWireType::RepeatedVariant16);
  78. return std::make_shared<TRepeatedVariant16Schema>(std::move(children));
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////
  81. TSkiffSchema::TSkiffSchema(EWireType type)
  82. : Type_(type)
  83. { }
  84. EWireType TSkiffSchema::GetWireType() const
  85. {
  86. return Type_;
  87. }
  88. std::shared_ptr<TSkiffSchema> TSkiffSchema::SetName(TString name)
  89. {
  90. Name_ = std::move(name);
  91. return shared_from_this();
  92. }
  93. const TString& TSkiffSchema::GetName() const
  94. {
  95. return Name_;
  96. }
  97. const TSkiffSchemaList& TSkiffSchema::GetChildren() const
  98. {
  99. static const TSkiffSchemaList children;
  100. return children;
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. TSimpleTypeSchema::TSimpleTypeSchema(EWireType type)
  104. : TSkiffSchema(type)
  105. {
  106. Y_ABORT_UNLESS(IsSimpleType(type));
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////
  109. size_t TSkiffSchemaPtrHasher::operator()(const std::shared_ptr<TSkiffSchema>& schema) const
  110. {
  111. return THash<NSkiff::TSkiffSchema>()(*schema);
  112. }
  113. size_t TSkiffSchemaPtrEqual::operator()(
  114. const std::shared_ptr<TSkiffSchema>& lhs,
  115. const std::shared_ptr<TSkiffSchema>& rhs) const
  116. {
  117. return *lhs == *rhs;
  118. }
  119. ////////////////////////////////////////////////////////////////////////////////
  120. } // namespace NSkiff
  121. ////////////////////////////////////////////////////////////////////////////////
  122. size_t THash<NSkiff::TSkiffSchema>::operator()(const NSkiff::TSkiffSchema &schema) const
  123. {
  124. auto hash = CombineHashes(
  125. THash<TString>()(schema.GetName()),
  126. static_cast<size_t>(schema.GetWireType()));
  127. for (const auto& child : schema.GetChildren()) {
  128. hash = CombineHashes(hash, (*this)(*child));
  129. }
  130. return hash;
  131. }
  132. ////////////////////////////////////////////////////////////////////////////////