helpers.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "helpers.h"
  2. #include <yt/cpp/mapreduce/interface/config.h>
  3. #include <yt/cpp/mapreduce/interface/serialize.h>
  4. #include <yt/cpp/mapreduce/interface/fluent.h>
  5. #include <library/cpp/yson/node/node_builder.h>
  6. #include <library/cpp/yson/node/node_visitor.h>
  7. #include <library/cpp/yson/parser.h>
  8. #include <library/cpp/yson/writer.h>
  9. #include <library/cpp/json/json_reader.h>
  10. #include <library/cpp/json/json_value.h>
  11. #include <util/stream/input.h>
  12. #include <util/stream/output.h>
  13. #include <util/stream/str.h>
  14. namespace NYT {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. TString NodeListToYsonString(const TNode::TListType& nodes)
  17. {
  18. TStringStream stream;
  19. ::NYson::TYsonWriter writer(&stream, NYson::EYsonFormat::Binary, ::NYson::EYsonType::ListFragment);
  20. auto list = BuildYsonListFluently(&writer);
  21. for (const auto& node : nodes) {
  22. list.Item().Value(node);
  23. }
  24. return stream.Str();
  25. }
  26. TNode PathToNode(const TRichYPath& path)
  27. {
  28. TNode result;
  29. TNodeBuilder builder(&result);
  30. Serialize(path, &builder);
  31. return result;
  32. }
  33. TNode PathToParamNode(const TRichYPath& path)
  34. {
  35. return TNode()("path", PathToNode(path));
  36. }
  37. TString AttributesToYsonString(const TNode& node)
  38. {
  39. return BuildYsonStringFluently().BeginMap()
  40. .Item("attributes").Value(node)
  41. .EndMap();
  42. }
  43. TString AttributeFilterToYsonString(const TAttributeFilter& filter)
  44. {
  45. return BuildYsonStringFluently().BeginMap()
  46. .Item("attributes").Value(filter)
  47. .EndMap();
  48. }
  49. TNode NodeFromTableSchema(const TTableSchema& schema)
  50. {
  51. TNode result;
  52. TNodeBuilder builder(&result);
  53. Serialize(schema, &builder);
  54. return result;
  55. }
  56. void MergeNodes(TNode& dst, const TNode& src)
  57. {
  58. if (dst.IsMap() && src.IsMap()) {
  59. auto& dstMap = dst.AsMap();
  60. const auto& srcMap = src.AsMap();
  61. for (const auto& srcItem : srcMap) {
  62. const auto& key = srcItem.first;
  63. auto dstItem = dstMap.find(key);
  64. if (dstItem != dstMap.end()) {
  65. MergeNodes(dstItem->second, srcItem.second);
  66. } else {
  67. dstMap[key] = srcItem.second;
  68. }
  69. }
  70. } else {
  71. if (dst.GetType() == src.GetType() && src.HasAttributes()) {
  72. auto attributes = dst.GetAttributes();
  73. MergeNodes(attributes, src.GetAttributes());
  74. dst = src;
  75. dst.Attributes() = attributes;
  76. } else {
  77. dst = src;
  78. }
  79. }
  80. }
  81. TYPath AddPathPrefix(const TYPath& path, const TString& pathPrefix)
  82. {
  83. if (path.StartsWith("//") || path.StartsWith("#")) {
  84. return path;
  85. }
  86. return pathPrefix + path;
  87. }
  88. TString GetWriteTableCommand(const TString& apiVersion)
  89. {
  90. return apiVersion == "v2" ? "write" : "write_table";
  91. }
  92. TString GetReadTableCommand(const TString& apiVersion)
  93. {
  94. return apiVersion == "v2" ? "read" : "read_table";
  95. }
  96. TString GetWriteFileCommand(const TString& apiVersion)
  97. {
  98. return apiVersion == "v2" ? "upload" : "write_file";
  99. }
  100. TString GetReadFileCommand(const TString& apiVersion)
  101. {
  102. return apiVersion == "v2" ? "download" : "read_file";
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. } // namespace NYT