skiff_row.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/skiff_row.h
  4. /// Header containing interfaces that you need to define for using TSkiffRowTableReader
  5. /// What you need to do for your struct type TMyType:
  6. /// 1. Write `true` specialization TIsSkiffRow<TMyType>;
  7. /// 2. Write specialization GetSkiffSchema<TMyType>();
  8. /// 3. Write your own parser derived from ISkiffRowParser and write specialization GetSkiffParser<TMyType>() which returns this parser.
  9. #include "fwd.h"
  10. #include <yt/cpp/mapreduce/skiff/skiff_schema.h>
  11. #include <yt/cpp/mapreduce/interface/format.h>
  12. #include <library/cpp/skiff/skiff.h>
  13. #include <util/generic/maybe.h>
  14. namespace NYT {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. //! Need to write `true_type` specialization for your row type `T`.
  17. /// And implement two functions: `GetSkiffSchema` and `CreateSkiffParser`.
  18. ///
  19. /// Example:
  20. ///
  21. /// template <>
  22. /// struct TIsSkiffRow<T>
  23. /// : std::true_type
  24. /// { };
  25. ///
  26. template<class T>
  27. struct TIsSkiffRow
  28. : std::false_type
  29. { };
  30. ////////////////////////////////////////////////////////////////////////////////
  31. //! Return skiff schema for row type `T`.
  32. /// Need to write its specialization.
  33. template <typename T>
  34. NSkiff::TSkiffSchemaPtr GetSkiffSchema(const TMaybe<TSkiffRowHints>& /*hints*/)
  35. {
  36. static_assert(TDependentFalse<T>, "Unimplemented `GetSkiffSchema` method");
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////
  39. //! Allow to parse rows as user's structs from stream (TCheckedInDebugSkiffParser).
  40. /// Need to write derived class for your own row type.
  41. ///
  42. /// Example:
  43. ///
  44. /// class TMySkiffRowParser : public ISkiffRowParser
  45. /// {
  46. /// public:
  47. /// TMySkiffRowParser(TMySkiffRow* row)
  48. /// : Row_(row)
  49. /// {}
  50. ///
  51. /// void Parse(NSkiff::TCheckedInDebugSkiffParser* parser)
  52. /// . {
  53. /// Row_->SomeInt64Field = parser->ParseInt64();
  54. /// }
  55. ///
  56. /// private:
  57. /// TMySkiffRow* Row_;
  58. /// }
  59. ///
  60. class ISkiffRowParser
  61. : public TThrRefBase
  62. {
  63. public:
  64. //! Read one row from parser
  65. virtual void Parse(NSkiff::TCheckedInDebugSkiffParser* /*parser*/) = 0;
  66. };
  67. //! Creates a parser for row type `T`.
  68. template <typename T>
  69. ISkiffRowParserPtr CreateSkiffParser(T* /*row*/, const TMaybe<TSkiffRowHints>& /*hints*/)
  70. {
  71. static_assert(TDependentFalse<T>, "Unimplemented `CreateSkiffParser` function");
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. //! Allow to skip row content without getting row.
  75. /// By default row will be parsed using your parser derived from ISkiffRowParser.
  76. /// If you want, you can write more optimal skipper, but it isn't required.
  77. class ISkiffRowSkipper
  78. : public TThrRefBase
  79. {
  80. public:
  81. virtual void SkipRow(NSkiff::TCheckedInDebugSkiffParser* /*parser*/) = 0;
  82. };
  83. //! Default ISkiffRowSkipper implementation.
  84. template <typename T>
  85. class TSkiffRowSkipper : public ISkiffRowSkipper {
  86. public:
  87. explicit TSkiffRowSkipper(const TMaybe<TSkiffRowHints>& hints)
  88. : Parser_(CreateSkiffParser<T>(&Row_, hints))
  89. { }
  90. void SkipRow(NSkiff::TCheckedInDebugSkiffParser* parser) {
  91. Parser_->Parse(parser);
  92. }
  93. private:
  94. T Row_;
  95. ISkiffRowParserPtr Parser_;
  96. };
  97. //! Creates a skipper for row type 'T'.
  98. /// You don't need to write its specialization.
  99. template <typename T>
  100. ISkiffRowSkipperPtr CreateSkiffSkipper(const TMaybe<TSkiffRowHints>& hints)
  101. {
  102. return ::MakeIntrusive<TSkiffRowSkipper<T>>(hints);
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. } // namespace NYT