helpers.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(
  31. const TRichYPath& path,
  32. const TOptions& options)
  33. {
  34. auto params = PathToParamNode(path);
  35. if (options.Config_) {
  36. params[TIOOptionsTraits<TOptions>::ConfigName] = *options.Config_;
  37. }
  38. return params;
  39. }
  40. template <>
  41. inline TNode FormIORequestParameters(
  42. const TRichYPath& path,
  43. const TFileReaderOptions& options)
  44. {
  45. auto params = PathToParamNode(path);
  46. if (options.Config_) {
  47. params[TIOOptionsTraits<TTableReaderOptions>::ConfigName] = *options.Config_;
  48. }
  49. if (options.Offset_) {
  50. params["offset"] = *options.Offset_;
  51. }
  52. if (options.Length_) {
  53. params["length"] = *options.Length_;
  54. }
  55. return params;
  56. }
  57. static void AddWriterOptionsToNode(const TWriterOptions& options, TNode* node)
  58. {
  59. if (options.EnableEarlyFinish_) {
  60. (*node)["enable_early_finish"] = *options.EnableEarlyFinish_;
  61. }
  62. if (options.UploadReplicationFactor_) {
  63. (*node)["upload_replication_factor"] = *options.UploadReplicationFactor_;
  64. }
  65. if (options.MinUploadReplicationFactor_) {
  66. (*node)["min_upload_replication_factor"] = *options.MinUploadReplicationFactor_;
  67. }
  68. if (options.DesiredChunkSize_) {
  69. (*node)["desired_chunk_size"] = *options.DesiredChunkSize_;
  70. }
  71. }
  72. template <>
  73. inline TNode FormIORequestParameters(
  74. const TRichYPath& path,
  75. const TFileWriterOptions& options)
  76. {
  77. auto params = PathToParamNode(path);
  78. TNode fileWriter = TNode::CreateMap();
  79. if (options.Config_) {
  80. fileWriter = *options.Config_;
  81. }
  82. if (options.WriterOptions_) {
  83. AddWriterOptionsToNode(*options.WriterOptions_, &fileWriter);
  84. }
  85. if (fileWriter.Empty()) {
  86. AddWriterOptionsToNode(
  87. TWriterOptions()
  88. .EnableEarlyFinish(true)
  89. .UploadReplicationFactor(3)
  90. .MinUploadReplicationFactor(2),
  91. &fileWriter);
  92. }
  93. params[TIOOptionsTraits<TFileWriterOptions>::ConfigName] = fileWriter;
  94. if (options.ComputeMD5_) {
  95. params["compute_md5"] = *options.ComputeMD5_;
  96. }
  97. return params;
  98. }
  99. template <>
  100. inline TNode FormIORequestParameters(
  101. const TRichYPath& path,
  102. const TTableWriterOptions& options)
  103. {
  104. auto params = PathToParamNode(path);
  105. auto tableWriter = TConfig::Get()->TableWriter;
  106. if (options.Config_) {
  107. MergeNodes(tableWriter, *options.Config_);
  108. }
  109. if (options.WriterOptions_) {
  110. AddWriterOptionsToNode(*options.WriterOptions_, &tableWriter);
  111. }
  112. if (!tableWriter.Empty()) {
  113. params[TIOOptionsTraits<TTableWriterOptions>::ConfigName] = std::move(tableWriter);
  114. }
  115. return params;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. }