C++ YT Wrapper
C++ library for working with YT clusters
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1#pragma once
2
7
8
9#include "fwd.h"
10
11#include "abortable_stream.h"
13#include "common.h"
14#include "distributed_session.h"
15#include "format.h"
16#include "node.h"
17#include "mpl.h"
18#include "skiff_row.h"
19
20#include <google/protobuf/message.h>
21
22#include <util/stream/input.h>
23#include <util/stream/output.h>
24#include <util/generic/yexception.h>
25#include <util/generic/maybe.h>
26
27namespace NYT {
28
30
35template<class... TProtoRowTypes>
36class TProtoOneOf
37{
38public:
39 static_assert(
40 (TIsBaseOf<::google::protobuf::Message, TProtoRowTypes>::Value && ...),
41 "Template parameters can only be protobuf types");
42
43 TProtoOneOf() = delete;
44};
45
50template<class... TSkiffRowTypes>
51class TSkiffRowOneOf
52{
53public:
54 static_assert(
56 "Template parameters can only be SkiffRow types");
57
58 TSkiffRowOneOf() = delete;
59};
60
62
64namespace NDetail {
65
67
68template <class TTuple>
69struct TProtoOneOfFromTuple;
70
71template <class... Ts>
72struct TProtoOneOfFromTuple<std::tuple<Ts...>>
73{
74 using TType = TProtoOneOf<Ts...>;
75};
76
77template <class... Ts>
78struct TProtoOneOfUnique
79{
80 using TTuple = typename TUniqueTypes<std::tuple<>, std::tuple<Ts...>>::TType;
81 using TType = typename TProtoOneOfFromTuple<TTuple>::TType;
82};
83
85
86} // namespace NDetail
88
90
91struct INodeReaderImpl;
92struct IYaMRReaderImpl;
93struct IProtoReaderImpl;
94struct ISkiffRowReaderImpl;
95struct INodeWriterImpl;
96struct IYaMRWriterImpl;
97struct IProtoWriterImpl;
98
100
103 : public yexception
104{ };
105
107
110 : public TThrRefBase
111 , public IAbortableInputStream
112{ };
113
116 : public TThrRefBase
117 , public IOutputStream
118{
119public:
120 virtual size_t GetBufferMemoryUsage() const
121 {
122 return 0;
123 }
124};
125
127 : public TThrRefBase
128 , public IOutputStream
129{
130public:
131 virtual TWriteFileFragmentResult GetWriteFragmentResult() const = 0;
132};
133
135
138 : public TThrRefBase
139 , public IAbortableInputStream
140{
141public:
153 virtual bool Retry(
154 const TMaybe<ui32>& rangeIndex,
155 const TMaybe<ui64>& rowIndex,
156 const std::exception_ptr& error) = 0;
157
159 virtual void ResetRetries() = 0;
160
165 virtual bool HasRangeIndices() const = 0;
166};
167
172 : public TThrRefBase
173 , public IOutputStream
174{
175public:
181 virtual void NotifyRowEnd() = 0;
182
186 virtual void Abort()
187 { }
188
189 virtual size_t GetBufferMemoryUsage() const
190 {
191 return 0;
192 }
193};
194
197{
198public:
199 virtual ~IProxyOutput()
200 { }
201
203 virtual size_t GetStreamCount() const = 0;
204
206 virtual IOutputStream* GetStream(size_t tableIndex) const = 0;
207
209 virtual void OnRowFinished(size_t tableIndex) = 0;
210
214 virtual void Abort()
215 { }
216
217 virtual size_t GetBufferMemoryUsage() const
218 {
219 return 0;
220 }
221};
222
224
246template <class T, class>
248 : public TThrRefBase
249{
250public:
252 const T& GetRow() const;
253
256
258 void MoveRow(T* result);
259
261 bool IsValid() const;
262
264 void Next();
265
267 ui32 GetTableIndex() const;
268
270 ui32 GetRangeIndex() const;
271
273 ui64 GetRowIndex() const;
274
276 i64 GetTabletIndex() const;
277
279 bool IsEndOfStream() const;
280
283
285 void Abort();
286
288 bool IsAborted() const;
289};
290
300template <class T>
302{
303public:
306 {
307 if (reader && reader->IsValid()) {
308 Reader_ = reader;
309 } else {
310 Reader_ = nullptr;
311 }
312 }
313
315 bool operator==(const TTableReaderIterator& it) const
316 {
317 return Reader_ == it.Reader_;
318 }
319
322 {
323 return *Reader_;
324 }
325
328 {
329 return *Reader_;
330 }
331
334 {
335 Reader_->Next();
336 if (!Reader_->IsValid()) {
337 Reader_ = nullptr;
338 }
339 return *this;
340 }
341
342private:
343 TTableReader<T>* Reader_;
344};
345
349template <class T>
351{
352 return TTableReaderIterator<T>(&reader);
353}
354
358template <class T>
360{
361 return TTableReaderIterator<T>(nullptr);
362}
363
365
378template <class T, class>
380 : public TThrRefBase
381{
382public:
385
387 bool IsValid() const;
388
390 void Next();
391};
392
394
396template <class T, class>
398 : public TThrRefBase
399{
400public:
404 void AddRow(const T& row);
405
408 void Finish();
409
410 size_t GetBufferMemoryUsage() const;
411};
412
414
420template <class T>
422 : public TThrRefBase
423{
424public:
428 virtual TWriteTableFragmentResult GetWriteFragmentResult() const = 0;
429
431 virtual void AddRow(const T& row) = 0;
432
436 virtual void Finish() = 0;
437};
438
440
445{
447 TStringBuf Key;
448
450 TStringBuf SubKey;
451
453 TStringBuf Value;
454};
455
457
460{
461public:
462 virtual ~IIOClient() = default;
463
465 virtual IFileReaderPtr CreateFileReader(
466 const TRichYPath& path,
467 const TFileReaderOptions& options = TFileReaderOptions()) = 0;
468
474 virtual IFileReaderPtr CreateFilePartitionReader(
475 const TString& cookie,
476 const TFilePartitionReaderOptions& options = {}) = 0;
477
479 virtual IFileWriterPtr CreateFileWriter(
480 const TRichYPath& path,
481 const TFileWriterOptions& options = TFileWriterOptions()) = 0;
482
486 virtual IFileFragmentWriterPtr CreateFileFragmentWriter(
487 const TDistributedWriteFileCookie& cookie,
489
491 template <class T>
492 TTableReaderPtr<T> CreateTableReader(
493 const TRichYPath& path,
494 const TTableReaderOptions& options = TTableReaderOptions());
495
497 template <class T>
498 TTableWriterPtr<T> CreateTableWriter(
499 const TRichYPath& path,
500 const TTableWriterOptions& options = TTableWriterOptions());
501
503 virtual TTableWriterPtr<::google::protobuf::Message> CreateTableWriter(
504 const TRichYPath& path,
505 const ::google::protobuf::Descriptor& descriptor,
506 const TTableWriterOptions& options = TTableWriterOptions()) = 0;
507
511 template <class T>
512 ITableFragmentWriterPtr<T> CreateTableFragmentWriter(
513 const TDistributedWriteTableCookie& cookie,
514 const TTableFragmentWriterOptions& options = {});
515
517 virtual TRawTableReaderPtr CreateRawReader(
518 const TRichYPath& path,
519 const TFormat& format,
520 const TTableReaderOptions& options = TTableReaderOptions()) = 0;
521
523 virtual TRawTableWriterPtr CreateRawWriter(
524 const TRichYPath& path,
525 const TFormat& format,
526 const TTableWriterOptions& options = TTableWriterOptions()) = 0;
527
536 virtual TRawTableReaderPtr CreateRawTablePartitionReader(
537 const TString& cookie,
538 const TFormat& format,
539 const TTablePartitionReaderOptions& options = {}) = 0;
540
547 template <class T>
548 TTableReaderPtr<T> CreateTablePartitionReader(
549 const TString& cookie,
550 const TTablePartitionReaderOptions& options = {});
551
567 virtual IFileReaderPtr CreateBlobTableReader(
568 const TYPath& path,
569 const TKey& blobId,
570 const TBlobTableReaderOptions& options = TBlobTableReaderOptions()) = 0;
571
572private:
573 virtual ::TIntrusivePtr<INodeReaderImpl> CreateNodeReader(
574 const TRichYPath& path, const TTableReaderOptions& options) = 0;
575
576 virtual ::TIntrusivePtr<IYaMRReaderImpl> CreateYaMRReader(
577 const TRichYPath& path, const TTableReaderOptions& options) = 0;
578
579 virtual ::TIntrusivePtr<IProtoReaderImpl> CreateProtoReader(
580 const TRichYPath& path,
581 const TTableReaderOptions& options,
582 const ::google::protobuf::Message* prototype) = 0;
583
584 virtual ::TIntrusivePtr<ISkiffRowReaderImpl> CreateSkiffRowReader(
585 const TRichYPath& path,
586 const TTableReaderOptions& options,
587 const ISkiffRowSkipperPtr& skipper,
588 const NSkiff::TSkiffSchemaPtr& requestedSchema,
589 const NSkiff::TSkiffSchemaPtr& parserSchema) = 0;
590
591 virtual ::TIntrusivePtr<INodeReaderImpl> CreateNodeTablePartitionReader(
592 const TString& cookie, const TTablePartitionReaderOptions& options) = 0;
593
594 virtual ::TIntrusivePtr<IProtoReaderImpl> CreateProtoTablePartitionReader(
595 const TString& cookie,
596 const TTablePartitionReaderOptions& options,
597 const ::google::protobuf::Message* prototype) = 0;
598
599 virtual ::TIntrusivePtr<ISkiffRowReaderImpl> CreateSkiffRowTablePartitionReader(
600 const TString& cookie,
601 const TTablePartitionReaderOptions& options,
602 const ISkiffRowSkipperPtr& skipper,
603 const NSkiff::TSkiffSchemaPtr& requestedSchema,
604 const NSkiff::TSkiffSchemaPtr& parserSchema) = 0;
605
606 virtual ::TIntrusivePtr<INodeWriterImpl> CreateNodeWriter(
607 const TRichYPath& path, const TTableWriterOptions& options) = 0;
608
609 virtual ::TIntrusivePtr<IYaMRWriterImpl> CreateYaMRWriter(
610 const TRichYPath& path, const TTableWriterOptions& options) = 0;
611
612 virtual ::TIntrusivePtr<IProtoWriterImpl> CreateProtoWriter(
613 const TRichYPath& path,
614 const TTableWriterOptions& options,
615 const ::google::protobuf::Message* prototype) = 0;
616
617 virtual ::TIntrusivePtr<ITableFragmentWriter<TNode>> CreateNodeFragmentWriter(
618 const TDistributedWriteTableCookie& cookie,
619 const TTableFragmentWriterOptions& options) = 0;
620
621 virtual ::TIntrusivePtr<ITableFragmentWriter<TYaMRRow>> CreateYaMRFragmentWriter(
622 const TDistributedWriteTableCookie& cookie,
623 const TTableFragmentWriterOptions& options) = 0;
624
625 virtual ::TIntrusivePtr<ITableFragmentWriter<Message>> CreateProtoFragmentWriter(
626 const TDistributedWriteTableCookie& cookie,
627 const TTableFragmentWriterOptions& options,
628 const ::google::protobuf::Message* prototype) = 0;
629};
630
632
639template <typename T>
640TTableReaderPtr<T> CreateTableReader(
641 IInputStream* stream,
642 const TTableReaderOptions& options = {});
643
650template <class... Ts>
651TTableReaderPtr<typename NDetail::TProtoOneOfUnique<Ts...>::TType> CreateProtoMultiTableReader(
652 IInputStream* stream,
653 const TTableReaderOptions& options = {});
654
662template <class T>
664 IInputStream* stream,
665 int tableCount,
666 const TTableReaderOptions& options = {});
667
669template <>
670TTableReaderPtr<TNode> CreateTableReader<TNode>(
671 IInputStream* stream, const TTableReaderOptions& options);
672
674template <>
675TTableReaderPtr<TYaMRRow> CreateTableReader<TYaMRRow>(
676 IInputStream* stream, const TTableReaderOptions& options);
677
678namespace NDetail {
679
681::TIntrusivePtr<IProtoReaderImpl> CreateProtoReader(
682 IInputStream* stream,
683 const TTableReaderOptions& options,
684 const ::google::protobuf::Descriptor* descriptor);
685
686
688::TIntrusivePtr<IProtoReaderImpl> CreateProtoReader(
689 IInputStream* stream,
690 const TTableReaderOptions& options,
691 TVector<const ::google::protobuf::Descriptor*> descriptors);
692
693} // namespace NDetail
694
696
698template <typename T>
700
702template <typename T>
703TTableReaderPtr<T> CreateConcreteProtobufReader(const TTableReaderPtr<Message>& reader);
704
706template <typename T>
707TTableReaderPtr<Message> CreateGenericProtobufReader(TTableReader<T>* reader);
708
710template <typename T>
711TTableReaderPtr<Message> CreateGenericProtobufReader(const TTableReaderPtr<T>& reader);
712
714
715} // namespace NYT
716
717#define IO_INL_H_
718#include "io-inl.h"
719#undef IO_INL_H_
Input stream that supports aborting the read operation.
Definition abortable_stream.h:16
Definition io.h:129
Interface representing YT file reader.
Definition io.h:112
Interface representing YT file writer.
Definition io.h:118
Interface for creating table and file readers and writer.
Definition io.h:460
virtual IFileReaderPtr CreateFileReader(const TRichYPath &path, const TFileReaderOptions &options=TFileReaderOptions())=0
Create a reader for file at path.
virtual TTableWriterPtr<::google::protobuf::Message > CreateTableWriter(const TRichYPath &path, const ::google::protobuf::Descriptor &descriptor, const TTableWriterOptions &options=TTableWriterOptions())=0
Create a writer to write protobuf messages with specified descriptor.
virtual IFileReaderPtr CreateBlobTableReader(const TYPath &path, const TKey &blobId, const TBlobTableReaderOptions &options=TBlobTableReaderOptions())=0
Create a reader for blob table at path.
virtual TRawTableWriterPtr CreateRawWriter(const TRichYPath &path, const TFormat &format, const TTableWriterOptions &options=TTableWriterOptions())=0
Create a reader to write a table using specified format.
TTableReaderPtr< T > CreateTableReader(const TRichYPath &path, const TTableReaderOptions &options=TTableReaderOptions())
Create a typed reader for table at path.
virtual TRawTableReaderPtr CreateRawReader(const TRichYPath &path, const TFormat &format, const TTableReaderOptions &options=TTableReaderOptions())=0
Create a reader to read a table using specified format.
virtual TRawTableReaderPtr CreateRawTablePartitionReader(const TString &cookie, const TFormat &format, const TTablePartitionReaderOptions &options={})=0
Create raw reader of table partition.
TTableWriterPtr< T > CreateTableWriter(const TRichYPath &path, const TTableWriterOptions &options=TTableWriterOptions())
Create a typed writer for table at path.
virtual IFileFragmentWriterPtr CreateFileFragmentWriter(const TDistributedWriteFileCookie &cookie, const TFileFragmentWriterOptions &options=TFileFragmentWriterOptions())=0
Create a fragment writer for given distributed file session cookie.
TTableReaderPtr< T > CreateTablePartitionReader(const TString &cookie, const TTablePartitionReaderOptions &options={})
Create reader of table partition.
ITableFragmentWriterPtr< T > CreateTableFragmentWriter(const TDistributedWriteTableCookie &cookie, const TTableFragmentWriterOptions &options={})
Create a fragment table writer for given distributed table session cookie.
virtual IFileWriterPtr CreateFileWriter(const TRichYPath &path, const TFileWriterOptions &options=TFileWriterOptions())=0
Create a writer for file at path.
virtual IFileReaderPtr CreateFilePartitionReader(const TString &cookie, const TFilePartitionReaderOptions &options={})=0
Create a reader of file partition.
Interface to deal with multiple raw output streams.
Definition io.h:197
virtual size_t GetStreamCount() const =0
Get amount of managed streams.
virtual void OnRowFinished(size_t tableIndex)=0
This handler must be called right after the next row has been written.
virtual void Abort()
Try to abort writing process as soon as possible (makes sense for multi-threaded writers).
Definition io.h:214
virtual IOutputStream * GetStream(size_t tableIndex) const =0
Get stream corresponding to the specified table index.
Class template for distributed table API fragment writer.
Definition io.h:423
virtual TWriteTableFragmentResult GetWriteFragmentResult() const =0
Get object with session-specific table fragment write information.
virtual void AddRow(const T &row)=0
Submit a row for writing.
virtual void Finish()=0
Completes writing and makes write fragment result available.
Class of exceptions connected to reading or writing tables or files.
Definition io.h:104
"Marker" type to use for several protobuf types in NYT::TTableReader.
Definition io.h:37
Low-level interface to read YT table with retries.
Definition io.h:140
virtual bool HasRangeIndices() const =0
May the input stream contain table ranges?
virtual void ResetRetries()=0
Resets retry attempt count to the initial value (then Retry() can be called again).
virtual bool Retry(const TMaybe< ui32 > &rangeIndex, const TMaybe< ui64 > &rowIndex, const std::exception_ptr &error)=0
Retry table read starting from the specified rangeIndex and rowIndex.
Low-level interface to write YT table.
Definition io.h:174
virtual void NotifyRowEnd()=0
Call this method after complete row representation is written to the stream.
virtual void Abort()
Try to abort writing process as soon as possible (makes sense for multi-threaded writers).
Definition io.h:186
Class to facilitate reading table rows sorted by key.
Definition io.h:381
void Next()
Move cursor to the next range.
TTableReader< T > & GetRange()
Get reader for rows with the same key.
bool IsValid() const
Check whether all rows are read.
Iterator for use in range-based-for.
Definition io.h:302
TTableReaderIterator(TTableReader< T > *reader)
Construct iterator from table reader (can be nullptr).
Definition io.h:305
bool operator==(const TTableReaderIterator &it) const
Equality operator.
Definition io.h:315
TTableReader< T > & operator*()
Dereference operator.
Definition io.h:321
TTableReaderIterator & operator++()
Preincrement operator.
Definition io.h:333
const TTableReader< T > & operator*() const
Const dereference operator.
Definition io.h:327
Class template to read typed rows from YT tables.
Definition io.h:249
bool IsRawReaderExhausted() const
Returns true if job raw input stream was closed and false otherwise.
const T & GetRow() const
Get current row.
void Abort()
Abort the reader, interrupting any in-progress or future reads.
ui32 GetTableIndex() const
Get table index of the current row.
ui32 GetRangeIndex() const
Get range index of the current row (zero if it is unknown or read request contains no ranges).
bool IsEndOfStream() const
Returns true if job consumed all the input and false otherwise.
void MoveRow(T *result)
Extract current row to result; further calls to GetRow and MoveRow will fail.
void Next()
Move the cursor to the next row.
bool IsValid() const
Check whether all the rows were read.
i64 GetTabletIndex() const
Get current tablet index (for ordered dynamic tables).
ui64 GetRowIndex() const
Get current row index (zero if it unknown).
bool IsAborted() const
Check whether the reader is aborted.
T MoveRow()
Extract current row; further calls to GetRow and MoveRow will fail.
Class template to write typed rows to YT tables.
Definition io.h:399
void AddRow(const T &row)
Submit a row for writing.
::TIntrusivePtr< IProtoReaderImpl > CreateProtoReader(IInputStream *stream, const TTableReaderOptions &options, const ::google::protobuf::Descriptor *descriptor)
Create a protobuf table reader from a stream.
Main namespace of YT client.
Definition abortable_stream.h:7
TTableReaderPtr< TNode > CreateTableReader< TNode >(IInputStream *stream, const TTableReaderOptions &options)
Create a NYT::TNode table reader from a stream.
TTableReaderPtr< typename NDetail::TProtoOneOfUnique< Ts... >::TType > CreateProtoMultiTableReader(IInputStream *stream, const TTableReaderOptions &options={})
Create a protobuf multi table reader from a stream.
TTableReaderIterator< T > end(TTableReader< T > &)
Function to facilitate range-based-for for NYT::TTableReader.
Definition io.h:359
TTableReaderPtr< TYaMRRow > CreateTableReader< TYaMRRow >(IInputStream *stream, const TTableReaderOptions &options)
Create a NYT::TYaMRRow table reader from a stream.
TTableReaderPtr< T > CreateConcreteProtobufReader(TTableReader< Message > *reader)
Convert generic protobuf table reader to a concrete one (for certain type T).
TTableReaderPtr< T > CreateTableReader(IInputStream *stream, const TTableReaderOptions &options={})
Create a protobuf table reader from a stream.
TTableReaderIterator< T > begin(TTableReader< T > &reader)
Function to facilitate range-based-for for NYT::TTableReader.
Definition io.h:350
TTableReaderPtr< Message > CreateGenericProtobufReader(TTableReader< T > *reader)
Convert a concrete (for certain type T) protobuf table reader to a generic one.
Options for NYT::IIOClient::CreateBlobTableReader.
Definition client_method_options.h:368
Options for writing file fragment in distributed session.
Definition client_method_options.h:677
Options for NYT::IClient::CreateFilePartitionReader.
Definition client_method_options.h:585
Options for reading file from YT.
Definition client_method_options.h:569
Options for writing file.
Definition client_method_options.h:647
Class representing YT data format.
Definition format.h:38
Definition skiff_row.h:38
Path with additional attributes.
Definition common.h:1040
Options for writing table fragment in distributed session.
Definition client_method_options.h:878
Options for NYT::IClient::CreatePartitionTableReader.
Definition client_method_options.h:805
Options for NYT::IClient::CreateTableReader.
Definition client_method_options.h:781
Options for NYT::IClient::CreateTableWriter.
Definition client_method_options.h:825
Type representing YaMR table row.
Definition io.h:445
TStringBuf Value
Value column.
Definition io.h:453
TStringBuf Key
Key column.
Definition io.h:447
TStringBuf SubKey
Subkey column.
Definition io.h:450