cypress.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/cypress.h
  4. ///
  5. /// Header containing interface to execute [Cypress](https://ytsaurus.tech/docs/en/user-guide/storage/cypress.html)-related commands.
  6. #include "fwd.h"
  7. #include "client_method_options.h"
  8. #include "common.h"
  9. #include "node.h"
  10. #include <util/generic/maybe.h>
  11. namespace NYT {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. /// Client interface to execute [Cypress](https://ytsaurus.tech/docs/en/user-guide/storage/cypress.html)-related commands.
  14. class ICypressClient
  15. {
  16. public:
  17. virtual ~ICypressClient() = default;
  18. ///
  19. /// @brief Create Cypress node of given type.
  20. ///
  21. /// @param path Path in Cypress to the new object.
  22. /// @param type New node type.
  23. /// @param options Optional parameters.
  24. ///
  25. /// @return Id of the created node.
  26. ///
  27. /// @note All but the last components must exist unless @ref NYT::TCreateOptions::Recursive is `true`.
  28. ///
  29. /// @note The node itself must not exist unless @ref NYT::TCreateOptions::IgnoreExisting or @ref NYT::TCreateOptions::Force are `true`.
  30. ///
  31. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#create)
  32. virtual TNodeId Create(
  33. const TYPath& path,
  34. ENodeType type,
  35. const TCreateOptions& options = TCreateOptions()) = 0;
  36. ///
  37. /// @brief Create table with schema inferred from the template argument.
  38. ///
  39. /// @tparam TRowType type of C++ representation of the row to be stored in the table.
  40. /// @param path Path in Cypress to the new table.
  41. /// @param sortColumns List of columns to mark as sorted in schema.
  42. /// @param options Optional parameters.
  43. ///
  44. /// @return Id of the created node.
  45. ///
  46. /// @note If "schema" is passed in `options.Attributes` it has priority over the deduced schema (the latter is ignored).
  47. template <typename TRowType>
  48. TNodeId CreateTable(
  49. const TYPath& path,
  50. const TSortColumns& sortColumns = TSortColumns(),
  51. const TCreateOptions& options = TCreateOptions());
  52. ///
  53. /// @brief Remove Cypress node.
  54. ///
  55. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#remove)
  56. virtual void Remove(
  57. const TYPath& path,
  58. const TRemoveOptions& options = TRemoveOptions()) = 0;
  59. ///
  60. /// @brief Check if Cypress node exists.
  61. ///
  62. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#exists)
  63. virtual bool Exists(
  64. const TYPath& path,
  65. const TExistsOptions& options = TExistsOptions()) = 0;
  66. ///
  67. /// @brief Get Cypress node contents.
  68. ///
  69. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#get)
  70. virtual TNode Get(
  71. const TYPath& path,
  72. const TGetOptions& options = TGetOptions()) = 0;
  73. ///
  74. /// @brief Set Cypress node contents.
  75. ///
  76. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#set)
  77. virtual void Set(
  78. const TYPath& path,
  79. const TNode& value,
  80. const TSetOptions& options = TSetOptions()) = 0;
  81. ///
  82. /// @brief Set multiple attributes for cypress path.
  83. ///
  84. /// @param path Path to root of the attributes to be set e.g. "//path/to/table/@";
  85. /// it is important to make sure that path ends with "/@".
  86. /// @param attributes Map with attributes
  87. /// @param options Optional parameters.
  88. ///
  89. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#multiset_attributes)
  90. virtual void MultisetAttributes(
  91. const TYPath& path,
  92. const TNode::TMapType& attributes,
  93. const TMultisetAttributesOptions& options = TMultisetAttributesOptions()) = 0;
  94. ///
  95. /// @brief List Cypress map or attribute node keys.
  96. ///
  97. /// @param path Path in the tree to the node in question.
  98. /// @param options Optional parameters.
  99. ///
  100. /// @return List of keys with attributes (if they were required in @ref NYT::TListOptions::AttributeFilter).
  101. ///
  102. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#list)
  103. virtual TNode::TListType List(
  104. const TYPath& path,
  105. const TListOptions& options = TListOptions()) = 0;
  106. ///
  107. /// @brief Copy Cypress node.
  108. ///
  109. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#copy)
  110. virtual TNodeId Copy(
  111. const TYPath& sourcePath,
  112. const TYPath& destinationPath,
  113. const TCopyOptions& options = TCopyOptions()) = 0;
  114. ///
  115. /// @brief Move Cypress node (equivalent to copy-then-remove).
  116. ///
  117. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#move)
  118. virtual TNodeId Move(
  119. const TYPath& sourcePath,
  120. const TYPath& destinationPath,
  121. const TMoveOptions& options = TMoveOptions()) = 0;
  122. ///
  123. /// @brief Create link to Cypress node.
  124. ///
  125. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#link)
  126. virtual TNodeId Link(
  127. const TYPath& targetPath,
  128. const TYPath& linkPath,
  129. const TLinkOptions& options = TLinkOptions()) = 0;
  130. ///
  131. /// @brief Concatenate several tables into one.
  132. ///
  133. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#concatenate)
  134. virtual void Concatenate(
  135. const TVector<TRichYPath>& sourcePaths,
  136. const TRichYPath& destinationPath,
  137. const TConcatenateOptions& options = TConcatenateOptions()) = 0;
  138. ///
  139. /// @brief Concatenate several tables into one.
  140. ///
  141. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#concatenate)
  142. virtual void Concatenate(
  143. const TVector<TYPath>& sourcePaths,
  144. const TYPath& destinationPath,
  145. const TConcatenateOptions& options = TConcatenateOptions());
  146. ///
  147. /// @brief Canonize YPath, moving all the complex YPath features to attributes.
  148. ///
  149. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#parse_ypath)
  150. virtual TRichYPath CanonizeYPath(const TRichYPath& path) = 0;
  151. ///
  152. /// @brief Get statistics for given sets of columns in given table ranges.
  153. ///
  154. /// @note Paths must contain column selectors.
  155. ///
  156. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#get_table_columnar_statistics)
  157. virtual TVector<TTableColumnarStatistics> GetTableColumnarStatistics(
  158. const TVector<TRichYPath>& paths,
  159. const TGetTableColumnarStatisticsOptions& options = {}) = 0;
  160. ///
  161. /// @brief Divide input tables into disjoint partitions.
  162. ///
  163. /// Resulted partitions are vectors of rich YPaths.
  164. /// Each partition can be given to a separate worker for further independent processing.
  165. ///
  166. virtual TMultiTablePartitions GetTablePartitions(
  167. const TVector<TRichYPath>& paths,
  168. const TGetTablePartitionsOptions& options) = 0;
  169. ///
  170. /// @brief Get file from file cache.
  171. ///
  172. /// @param md5Signature MD5 digest of the file.
  173. /// @param cachePath Path to the file cache.
  174. /// @param options Optional parameters.
  175. ///
  176. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#get_file_from_cache)
  177. virtual TMaybe<TYPath> GetFileFromCache(
  178. const TString& md5Signature,
  179. const TYPath& cachePath,
  180. const TGetFileFromCacheOptions& options = TGetFileFromCacheOptions()) = 0;
  181. ///
  182. /// @brief Put file to file cache.
  183. ///
  184. /// @param filePath Path in Cypress to the file to cache.
  185. /// @param md5Signature Expected MD5 digest of the file.
  186. /// @param cachePath Path to the file cache.
  187. /// @param options Optional parameters.
  188. ///
  189. /// @note The file in `filePath` must have been written with @ref NYT::TFileWriterOptions::ComputeMD5 set to `true`.
  190. ///
  191. /// @see [YT doc](https://ytsaurus.tech/docs/en/api/commands.html#put_file_to_cache)
  192. virtual TYPath PutFileToCache(
  193. const TYPath& filePath,
  194. const TString& md5Signature,
  195. const TYPath& cachePath,
  196. const TPutFileToCacheOptions& options = TPutFileToCacheOptions()) = 0;
  197. };
  198. ////////////////////////////////////////////////////////////////////////////////
  199. template <typename TRowType>
  200. TNodeId ICypressClient::CreateTable(
  201. const TYPath& path,
  202. const TSortColumns& sortColumns,
  203. const TCreateOptions& options)
  204. {
  205. static_assert(
  206. std::is_base_of_v<::google::protobuf::Message, TRowType>,
  207. "TRowType must be inherited from google::protobuf::Message");
  208. TCreateOptions actualOptions = options;
  209. if (!actualOptions.Attributes_) {
  210. actualOptions.Attributes_ = TNode::CreateMap();
  211. }
  212. if (!actualOptions.Attributes_->HasKey("schema")) {
  213. actualOptions.Attributes_->AsMap().emplace(
  214. "schema",
  215. CreateTableSchema<TRowType>(sortColumns).ToNode());
  216. }
  217. return Create(path, ENodeType::NT_TABLE, actualOptions);
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. } // namespace NYT