|
1 | 1 | import datetime |
| 2 | +import typing |
2 | 3 | from dataclasses import dataclass, field |
3 | 4 | from typing import List, Union, Dict |
4 | 5 |
|
| 6 | +from google.protobuf.message import Message |
| 7 | + |
5 | 8 | from ydb._topic_wrapper.common import OffsetsRange, IToProto, UpdateTokenRequest, UpdateTokenResponse, IFromProto |
6 | 9 | from google.protobuf.duration_pb2 import Duration as ProtoDuration |
7 | 10 |
|
|
14 | 17 |
|
15 | 18 | class StreamReadMessage: |
16 | 19 | @dataclass |
17 | | - class PartitionSession: |
| 20 | + class PartitionSession(IFromProto): |
18 | 21 | partition_session_id: int |
19 | 22 | path: str |
20 | 23 | partition_id: int |
21 | 24 |
|
| 25 | + @staticmethod |
| 26 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.PartitionSession) -> "StreamReadMessage.PartitionSession": |
| 27 | + return StreamReadMessage.PartitionSession( |
| 28 | + partition_session_id=msg.partition_session_id, |
| 29 | + path=msg.path, |
| 30 | + partition_id=msg.partition_id, |
| 31 | + ) |
| 32 | + |
22 | 33 | @dataclass |
23 | 34 | class InitRequest(IToProto): |
24 | 35 | topics_read_settings: List["StreamReadMessage.InitRequest.TopicReadSettings"] |
@@ -56,36 +67,90 @@ def from_proto(msg: ydb_topic_pb2.StreamReadMessage.InitResponse) -> "StreamRead |
56 | 67 | return StreamReadMessage.InitResponse(session_id=msg.session_id) |
57 | 68 |
|
58 | 69 | @dataclass |
59 | | - class ReadRequest: |
| 70 | + class ReadRequest(IToProto): |
60 | 71 | bytes_size: int |
61 | 72 |
|
| 73 | + def to_proto(self) -> ydb_topic_pb2.StreamReadMessage.ReadRequest: |
| 74 | + res = ydb_topic_pb2.StreamReadMessage.ReadRequest() |
| 75 | + res.bytes_size = self.bytes_size |
| 76 | + return res |
| 77 | + |
62 | 78 | @dataclass |
63 | | - class ReadResponse: |
| 79 | + class ReadResponse(IFromProto): |
64 | 80 | partition_data: List["StreamReadMessage.ReadResponse.PartitionData"] |
65 | 81 | bytes_size: int |
66 | 82 |
|
| 83 | + @staticmethod |
| 84 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.ReadResponse) -> "StreamReadMessage.ReadResponse": |
| 85 | + partition_data = [] |
| 86 | + for proto_partition_data in msg.partition_data: |
| 87 | + partition_data.append(StreamReadMessage.ReadResponse.PartitionData.from_proto(proto_partition_data)) |
| 88 | + return StreamReadMessage.ReadResponse( |
| 89 | + partition_data=partition_data, |
| 90 | + bytes_size=msg.bytes_size, |
| 91 | + ) |
| 92 | + |
67 | 93 | @dataclass |
68 | | - class MessageData: |
| 94 | + class MessageData(IFromProto): |
69 | 95 | offset: int |
70 | 96 | seq_no: int |
71 | 97 | created_at: datetime.datetime |
72 | 98 | data: bytes |
73 | 99 | uncompresed_size: int |
74 | 100 | message_group_id: str |
75 | 101 |
|
| 102 | + @staticmethod |
| 103 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.ReadResponse.MessageData) ->\ |
| 104 | + "StreamReadMessage.ReadResponse.MessageData": |
| 105 | + return StreamReadMessage.ReadResponse.MessageData( |
| 106 | + offset=msg.offset, |
| 107 | + seq_no=msg.seq_no, |
| 108 | + created_at=msg.created_at.ToDatetime(), |
| 109 | + data=msg.data, |
| 110 | + uncompresed_size=msg.uncompressed_size, |
| 111 | + message_group_id=msg.message_group_id |
| 112 | + ) |
| 113 | + |
76 | 114 | @dataclass |
77 | | - class Batch: |
| 115 | + class Batch(IFromProto): |
78 | 116 | message_data: List["StreamReadMessage.ReadResponse.MessageData"] |
79 | 117 | producer_id: str |
80 | 118 | write_session_meta: Dict[str, str] |
81 | 119 | codec: int |
82 | 120 | written_at: datetime.datetime |
83 | 121 |
|
| 122 | + @staticmethod |
| 123 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.ReadResponse.Batch) -> \ |
| 124 | + "StreamReadMessage.ReadResponse.Batch": |
| 125 | + message_data = [] |
| 126 | + for message in msg.message_data: |
| 127 | + message_data.append(StreamReadMessage.ReadResponse.MessageData.from_proto(message)) |
| 128 | + return StreamReadMessage.ReadResponse.Batch( |
| 129 | + message_data=message_data, |
| 130 | + producer_id=msg.producer_id, |
| 131 | + write_session_meta=dict(msg.write_session_meta), |
| 132 | + codec=msg.codec, |
| 133 | + written_at=msg.written_at.ToDatetime(), |
| 134 | + ) |
| 135 | + |
| 136 | + |
84 | 137 | @dataclass |
85 | | - class PartitionData: |
| 138 | + class PartitionData(IFromProto): |
86 | 139 | partition_session_id: int |
87 | 140 | batches: List["StreamReadMessage.ReadResponse.Batch"] |
88 | 141 |
|
| 142 | + @staticmethod |
| 143 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.ReadResponse.PartitionData) ->\ |
| 144 | + "StreamReadMessage.ReadResponse.PartitionData": |
| 145 | + batches = [] |
| 146 | + for proto_batch in msg.batches: |
| 147 | + batches.append(StreamReadMessage.ReadResponse.Batch.from_proto(proto_batch)) |
| 148 | + return StreamReadMessage.ReadResponse.PartitionData( |
| 149 | + partition_session_id=msg.partition_session_id, |
| 150 | + batches=batches, |
| 151 | + ) |
| 152 | + |
| 153 | + |
89 | 154 | @dataclass |
90 | 155 | class CommitOffsetRequest: |
91 | 156 | commit_offsets: List["PartitionCommitOffset"] |
@@ -116,17 +181,33 @@ class PartitionSessionStatusResponse: |
116 | 181 | write_time_high_watermark: float |
117 | 182 |
|
118 | 183 | @dataclass |
119 | | - class StartPartitionSessionRequest: |
| 184 | + class StartPartitionSessionRequest(IFromProto): |
120 | 185 | partition_session: "StreamReadMessage.PartitionSession" |
121 | 186 | committed_offset: int |
122 | 187 | partition_offsets: OffsetsRange |
123 | 188 |
|
| 189 | + @staticmethod |
| 190 | + def from_proto(msg: ydb_topic_pb2.StreamReadMessage.StartPartitionSessionRequest) -> \ |
| 191 | + "StreamReadMessage.StartPartitionSessionRequest": |
| 192 | + return StreamReadMessage.StartPartitionSessionRequest( |
| 193 | + partition_session=StreamReadMessage.PartitionSession.from_proto(msg.partition_session), |
| 194 | + committed_offset=msg.committed_offset, |
| 195 | + partition_offsets=OffsetsRange.from_proto(msg.partition_offsets) |
| 196 | + ) |
| 197 | + |
124 | 198 | @dataclass |
125 | | - class StartPartitionSessionResponse: |
| 199 | + class StartPartitionSessionResponse(IToProto): |
126 | 200 | partition_session_id: int |
127 | 201 | read_offset: int |
128 | 202 | commit_offset: int |
129 | 203 |
|
| 204 | + def to_proto(self) -> ydb_topic_pb2.StreamReadMessage.StartPartitionSessionResponse: |
| 205 | + res = ydb_topic_pb2.StreamReadMessage.StartPartitionSessionResponse() |
| 206 | + res.partition_session_id = self.partition_session_id |
| 207 | + res.read_offset = self.read_offset |
| 208 | + res.commit_offset = self.commit_offset |
| 209 | + return res |
| 210 | + |
130 | 211 | @dataclass |
131 | 212 | class StopPartitionSessionRequest: |
132 | 213 | partition_session_id: int |
@@ -159,7 +240,11 @@ class FromServer(IFromProto): |
159 | 240 | @staticmethod |
160 | 241 | def from_proto(msg: ydb_topic_pb2.StreamReadMessage.FromServer) -> "StreamReadMessage.FromServer": |
161 | 242 | mess_type = msg.WhichOneof("server_message") |
162 | | - if mess_type == "init_response": |
| 243 | + if mess_type == "read_response": |
| 244 | + return StreamReadMessage.FromServer( |
| 245 | + server_message=StreamReadMessage.ReadResponse.from_proto(msg.init_response) |
| 246 | + ) |
| 247 | + elif mess_type == "init_response": |
163 | 248 | return StreamReadMessage.FromServer( |
164 | 249 | server_message=StreamReadMessage.InitResponse.from_proto(msg.init_response), |
165 | 250 | ) |
|
0 commit comments