helpers.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include <yt/cpp/mapreduce/interface/io.h>
  3. #include <yt/cpp/mapreduce/interface/config.h>
  4. #include <yt/cpp/mapreduce/common/helpers.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. template <class TOptions>
  8. struct TIOOptionsTraits;
  9. template <>
  10. struct TIOOptionsTraits<TFileReaderOptions>
  11. {
  12. static constexpr const char* const ConfigName = "file_reader";
  13. };
  14. template <>
  15. struct TIOOptionsTraits<TFileWriterOptions>
  16. {
  17. static constexpr const char* const ConfigName = "file_writer";
  18. };
  19. template <>
  20. struct TIOOptionsTraits<TTableReaderOptions>
  21. {
  22. static constexpr const char* const ConfigName = "table_reader";
  23. };
  24. template <>
  25. struct TIOOptionsTraits<TTableWriterOptions>
  26. {
  27. static constexpr const char* const ConfigName = "table_writer";
  28. };
  29. template <class TOptions>
  30. TNode FormIORequestParameters(const TOptions& options)
  31. {
  32. TNode params = TNode::CreateMap();
  33. if (options.Config_) {
  34. params[TIOOptionsTraits<TOptions>::ConfigName] = *options.Config_;
  35. }
  36. return params;
  37. }
  38. template <class TOptions>
  39. TNode FormIORequestParameters(
  40. const TRichYPath& path,
  41. const TOptions& options)
  42. {
  43. auto params = PathToParamNode(path);
  44. if (options.Config_) {
  45. params[TIOOptionsTraits<TOptions>::ConfigName] = *options.Config_;
  46. }
  47. return params;
  48. }
  49. template <>
  50. inline TNode FormIORequestParameters(
  51. const TRichYPath& path,
  52. const TFileReaderOptions& options)
  53. {
  54. auto params = PathToParamNode(path);
  55. if (options.Config_) {
  56. params[TIOOptionsTraits<TTableReaderOptions>::ConfigName] = *options.Config_;
  57. }
  58. if (options.Offset_) {
  59. params["offset"] = *options.Offset_;
  60. }
  61. if (options.Length_) {
  62. params["length"] = *options.Length_;
  63. }
  64. return params;
  65. }
  66. static void AddWriterOptionsToNode(const TWriterOptions& options, TNode* node)
  67. {
  68. if (options.EnableEarlyFinish_) {
  69. (*node)["enable_early_finish"] = *options.EnableEarlyFinish_;
  70. }
  71. if (options.UploadReplicationFactor_) {
  72. (*node)["upload_replication_factor"] = *options.UploadReplicationFactor_;
  73. }
  74. if (options.MinUploadReplicationFactor_) {
  75. (*node)["min_upload_replication_factor"] = *options.MinUploadReplicationFactor_;
  76. }
  77. if (options.DesiredChunkSize_) {
  78. (*node)["desired_chunk_size"] = *options.DesiredChunkSize_;
  79. }
  80. }
  81. template <>
  82. inline TNode FormIORequestParameters(
  83. const TRichYPath& path,
  84. const TFileWriterOptions& options)
  85. {
  86. auto params = PathToParamNode(path);
  87. TNode fileWriter = TNode::CreateMap();
  88. if (options.Config_) {
  89. fileWriter = *options.Config_;
  90. }
  91. if (options.WriterOptions_) {
  92. AddWriterOptionsToNode(*options.WriterOptions_, &fileWriter);
  93. }
  94. if (fileWriter.Empty()) {
  95. AddWriterOptionsToNode(
  96. TWriterOptions()
  97. .EnableEarlyFinish(true)
  98. .UploadReplicationFactor(3)
  99. .MinUploadReplicationFactor(2),
  100. &fileWriter);
  101. }
  102. params[TIOOptionsTraits<TFileWriterOptions>::ConfigName] = fileWriter;
  103. if (options.ComputeMD5_) {
  104. params["compute_md5"] = *options.ComputeMD5_;
  105. }
  106. return params;
  107. }
  108. template <>
  109. inline TNode FormIORequestParameters(
  110. const TRichYPath& path,
  111. const TTableWriterOptions& options)
  112. {
  113. auto params = PathToParamNode(path);
  114. auto tableWriter = TConfig::Get()->TableWriter;
  115. if (options.Config_) {
  116. MergeNodes(tableWriter, *options.Config_);
  117. }
  118. if (options.WriterOptions_) {
  119. AddWriterOptionsToNode(*options.WriterOptions_, &tableWriter);
  120. }
  121. if (!tableWriter.Empty()) {
  122. params[TIOOptionsTraits<TTableWriterOptions>::ConfigName] = std::move(tableWriter);
  123. }
  124. return params;
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////
  127. }