@@ -67,10 +67,23 @@ def to_public(self) -> List[ydb_topic_public_types.PublicCodec]:
6767 return list (map (Codec .to_public , self .codecs ))
6868
6969
70- @dataclass
71- class OffsetsRange (IFromProto ):
72- start : int
73- end : int
70+ @dataclass (order = True )
71+ class OffsetsRange (IFromProto , IToProto ):
72+ """
73+ half-opened interval, include [start, end) offsets
74+ """
75+
76+ __slots__ = ("start" , "end" )
77+
78+ start : int # first offset
79+ end : int # offset after last, included to range
80+
81+ def __post_init__ (self ):
82+ if self .end < self .start :
83+ raise ValueError (
84+ "offset end must be not less then start. Got start=%s end=%s"
85+ % (self .start , self .end )
86+ )
7487
7588 @staticmethod
7689 def from_proto (msg : ydb_topic_pb2 .OffsetsRange ) -> "OffsetsRange" :
@@ -79,6 +92,20 @@ def from_proto(msg: ydb_topic_pb2.OffsetsRange) -> "OffsetsRange":
7992 end = msg .end ,
8093 )
8194
95+ def to_proto (self ) -> ydb_topic_pb2 .OffsetsRange :
96+ return ydb_topic_pb2 .OffsetsRange (
97+ start = self .start ,
98+ end = self .end ,
99+ )
100+
101+ def is_intersected_with (self , other : "OffsetsRange" ) -> bool :
102+ return (
103+ self .start <= other .start < self .end
104+ or self .start < other .end <= self .end
105+ or other .start <= self .start < other .end
106+ or other .start < self .end <= other .end
107+ )
108+
82109
83110@dataclass
84111class UpdateTokenRequest (IToProto ):
@@ -527,23 +554,67 @@ def from_proto(
527554 )
528555
529556 @dataclass
530- class CommitOffsetRequest :
557+ class CommitOffsetRequest ( IToProto ) :
531558 commit_offsets : List ["PartitionCommitOffset" ]
532559
560+ def to_proto (self ) -> ydb_topic_pb2 .StreamReadMessage .CommitOffsetRequest :
561+ res = ydb_topic_pb2 .StreamReadMessage .CommitOffsetRequest (
562+ commit_offsets = list (
563+ map (
564+ StreamReadMessage .CommitOffsetRequest .PartitionCommitOffset .to_proto ,
565+ self .commit_offsets ,
566+ )
567+ ),
568+ )
569+ return res
570+
533571 @dataclass
534- class PartitionCommitOffset :
572+ class PartitionCommitOffset ( IToProto ) :
535573 partition_session_id : int
536574 offsets : List ["OffsetsRange" ]
537575
576+ def to_proto (
577+ self ,
578+ ) -> ydb_topic_pb2 .StreamReadMessage .CommitOffsetRequest .PartitionCommitOffset :
579+ res = ydb_topic_pb2 .StreamReadMessage .CommitOffsetRequest .PartitionCommitOffset (
580+ partition_session_id = self .partition_session_id ,
581+ offsets = list (map (OffsetsRange .to_proto , self .offsets )),
582+ )
583+ return res
584+
538585 @dataclass
539- class CommitOffsetResponse :
540- partitions_committed_offsets : List ["PartitionCommittedOffset" ]
586+ class CommitOffsetResponse (IFromProto ):
587+ partitions_committed_offsets : List [
588+ "StreamReadMessage.CommitOffsetResponse.PartitionCommittedOffset"
589+ ]
590+
591+ @staticmethod
592+ def from_proto (
593+ msg : ydb_topic_pb2 .StreamReadMessage .CommitOffsetResponse ,
594+ ) -> "StreamReadMessage.CommitOffsetResponse" :
595+ return StreamReadMessage .CommitOffsetResponse (
596+ partitions_committed_offsets = list (
597+ map (
598+ StreamReadMessage .CommitOffsetResponse .PartitionCommittedOffset .from_proto ,
599+ msg .partitions_committed_offsets ,
600+ )
601+ )
602+ )
541603
542604 @dataclass
543- class PartitionCommittedOffset :
605+ class PartitionCommittedOffset ( IFromProto ) :
544606 partition_session_id : int
545607 committed_offset : int
546608
609+ @staticmethod
610+ def from_proto (
611+ msg : ydb_topic_pb2 .StreamReadMessage .CommitOffsetResponse .PartitionCommittedOffset ,
612+ ) -> "StreamReadMessage.CommitOffsetResponse.PartitionCommittedOffset" :
613+ return StreamReadMessage .CommitOffsetResponse .PartitionCommittedOffset (
614+ partition_session_id = msg .partition_session_id ,
615+ committed_offset = msg .committed_offset ,
616+ )
617+
547618 @dataclass
548619 class PartitionSessionStatusRequest :
549620 partition_session_id : int
@@ -576,16 +647,18 @@ def from_proto(
576647 @dataclass
577648 class StartPartitionSessionResponse (IToProto ):
578649 partition_session_id : int
579- read_offset : int
580- commit_offset : int
650+ read_offset : Optional [ int ]
651+ commit_offset : Optional [ int ]
581652
582653 def to_proto (
583654 self ,
584655 ) -> ydb_topic_pb2 .StreamReadMessage .StartPartitionSessionResponse :
585656 res = ydb_topic_pb2 .StreamReadMessage .StartPartitionSessionResponse ()
586657 res .partition_session_id = self .partition_session_id
587- res .read_offset = self .read_offset
588- res .commit_offset = self .commit_offset
658+ if self .read_offset is not None :
659+ res .read_offset = self .read_offset
660+ if self .commit_offset is not None :
661+ res .commit_offset = self .commit_offset
589662 return res
590663
591664 @dataclass
@@ -609,6 +682,8 @@ def to_proto(self) -> ydb_topic_pb2.StreamReadMessage.FromClient:
609682 res = ydb_topic_pb2 .StreamReadMessage .FromClient ()
610683 if isinstance (self .client_message , StreamReadMessage .ReadRequest ):
611684 res .read_request .CopyFrom (self .client_message .to_proto ())
685+ elif isinstance (self .client_message , StreamReadMessage .CommitOffsetRequest ):
686+ res .commit_offset_request .CopyFrom (self .client_message .to_proto ())
612687 elif isinstance (self .client_message , StreamReadMessage .InitRequest ):
613688 res .init_request .CopyFrom (self .client_message .to_proto ())
614689 elif isinstance (
@@ -618,7 +693,9 @@ def to_proto(self) -> ydb_topic_pb2.StreamReadMessage.FromClient:
618693 self .client_message .to_proto ()
619694 )
620695 else :
621- raise NotImplementedError ()
696+ raise NotImplementedError (
697+ "Unknown message type: %s" % type (self .client_message )
698+ )
622699 return res
623700
624701 @dataclass
@@ -639,6 +716,13 @@ def from_proto(
639716 msg .read_response
640717 ),
641718 )
719+ elif mess_type == "commit_offset_response" :
720+ return StreamReadMessage .FromServer (
721+ server_status = server_status ,
722+ server_message = StreamReadMessage .CommitOffsetResponse .from_proto (
723+ msg .commit_offset_response
724+ ),
725+ )
642726 elif mess_type == "init_response" :
643727 return StreamReadMessage .FromServer (
644728 server_status = server_status ,
0 commit comments