-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_mappers.py
More file actions
348 lines (306 loc) · 11.1 KB
/
_mappers.py
File metadata and controls
348 lines (306 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from datetime import datetime
from typing import cast
from google.protobuf.internal.containers import RepeatedCompositeFieldContainer
import streamstore._lib.s2.v1alpha.s2_pb2 as msgs
from streamstore.schemas import (
AccessTokenInfo,
AccessTokenScope,
AppendInput,
AppendOutput,
BasinConfig,
BasinInfo,
BasinScope,
BasinState,
Operation,
OperationGroupPermissions,
Permission,
ReadLimit,
Record,
ResourceMatchOp,
ResourceMatchRule,
SeqNum,
SequencedRecord,
StorageClass,
StreamConfig,
StreamInfo,
TailOffset,
Timestamp,
Timestamping,
TimestampingMode,
)
_ReadStart = SeqNum | Timestamp | TailOffset
def append_record_message(record: Record) -> msgs.AppendRecord:
headers = [msgs.Header(name=name, value=value) for (name, value) in record.headers]
return msgs.AppendRecord(
timestamp=record.timestamp, headers=headers, body=record.body
)
def append_input_message(stream: str, input: AppendInput) -> msgs.AppendInput:
records = [append_record_message(r) for r in input.records]
return msgs.AppendInput(
stream=stream,
records=records,
match_seq_num=input.match_seq_num,
fencing_token=input.fencing_token,
)
def read_request_message(
stream: str,
start: _ReadStart,
limit: ReadLimit | None,
until: int | None,
) -> msgs.ReadRequest:
seq_num, timestamp, tail_offset = _read_start_pos(start)
return msgs.ReadRequest(
stream=stream,
seq_num=seq_num,
timestamp=timestamp,
tail_offset=tail_offset,
limit=_read_limit_message(limit),
until=until,
)
def read_session_request_message(
stream: str,
start: _ReadStart,
limit: ReadLimit | None,
until: int | None,
clamp: bool = False,
) -> msgs.ReadSessionRequest:
seq_num, timestamp, tail_offset = _read_start_pos(start)
return msgs.ReadSessionRequest(
stream=stream,
seq_num=seq_num,
timestamp=timestamp,
tail_offset=tail_offset,
limit=_read_limit_message(limit),
until=until,
clamp=clamp,
)
def _read_start_pos(start: _ReadStart) -> tuple[int | None, int | None, int | None]:
seq_num = None
timestamp = None
tail_offset = None
if isinstance(start, SeqNum):
seq_num = start.value
elif isinstance(start, Timestamp):
timestamp = start.value
elif isinstance(start, TailOffset):
tail_offset = start.value
else:
raise ValueError("start doesn't match any of the expected types")
return (
seq_num,
timestamp,
tail_offset,
)
def basin_info_schema(info: msgs.BasinInfo) -> BasinInfo:
return BasinInfo(info.name, BasinScope(info.scope), BasinState(info.state))
def stream_info_schema(info: msgs.StreamInfo) -> StreamInfo:
return StreamInfo(
info.name,
datetime.fromtimestamp(info.created_at),
datetime.fromtimestamp(info.deleted_at) if info.deleted_at != 0 else None,
)
def stream_config_message(
config: StreamConfig | None = None,
return_mask_paths: bool = False,
mask_path_prefix: str = "",
) -> msgs.StreamConfig | tuple[msgs.StreamConfig, list[str]]:
paths = []
stream_config = msgs.StreamConfig()
if config:
storage_class = config.storage_class
retention_age = config.retention_age
timestamping = config.timestamping
delete_on_empty_min_age = config.delete_on_empty_min_age
if storage_class is not None:
paths.append(f"{mask_path_prefix}storage_class")
stream_config.storage_class = storage_class.value
if retention_age is not None:
paths.append(f"{mask_path_prefix}retention_policy")
stream_config.age = retention_age
if timestamping is not None:
paths.append(f"{mask_path_prefix}timestamping")
if timestamping.mode is not None:
paths.append(f"{mask_path_prefix}timestamping.mode")
stream_config.timestamping.mode = timestamping.mode.value
if timestamping.uncapped is not None:
paths.append(f"{mask_path_prefix}timestamping.uncapped")
stream_config.timestamping.uncapped = timestamping.uncapped
if delete_on_empty_min_age is not None:
paths.append(f"{mask_path_prefix}delete_on_empty.min_age_secs")
stream_config.delete_on_empty.min_age_secs = delete_on_empty_min_age
if return_mask_paths:
return (stream_config, paths)
return stream_config
def basin_config_message(
config: BasinConfig | None = None,
return_mask_paths: bool = False,
) -> msgs.BasinConfig | tuple[msgs.BasinConfig, list[str]]:
paths = []
basin_config = msgs.BasinConfig()
if config:
if return_mask_paths:
default_stream_config, deep_paths = cast(
tuple[msgs.StreamConfig, list[str]],
stream_config_message(
config.default_stream_config,
return_mask_paths,
mask_path_prefix="default_stream_config.",
),
)
paths.extend(deep_paths)
else:
default_stream_config = cast(
msgs.StreamConfig, stream_config_message(config.default_stream_config)
)
basin_config.default_stream_config.CopyFrom(default_stream_config)
if config.create_stream_on_append is not None:
basin_config.create_stream_on_append = config.create_stream_on_append
paths.append("create_stream_on_append")
if return_mask_paths:
return (basin_config, paths)
return basin_config
def stream_config_schema(config: msgs.StreamConfig) -> StreamConfig:
return StreamConfig(
StorageClass(config.storage_class),
config.age,
Timestamping(
mode=TimestampingMode(config.timestamping.mode),
uncapped=config.timestamping.uncapped,
),
config.delete_on_empty.min_age_secs,
)
def basin_config_schema(config: msgs.BasinConfig) -> BasinConfig:
return BasinConfig(
stream_config_schema(config.default_stream_config),
config.create_stream_on_append,
)
def append_output_schema(output: msgs.AppendOutput) -> AppendOutput:
return AppendOutput(
output.start_seq_num,
output.start_timestamp,
output.end_seq_num,
output.end_timestamp,
output.next_seq_num,
output.last_timestamp,
)
def sequenced_records_schema(
batch: msgs.SequencedRecordBatch, ignore_command_records: bool = False
) -> list[SequencedRecord]:
if ignore_command_records:
return [
SequencedRecord(
sr.seq_num,
sr.body,
[(h.name, h.value) for h in sr.headers],
sr.timestamp,
)
for sr in batch.records
if _not_a_command_record(sr.headers)
]
return [
SequencedRecord(
sr.seq_num, sr.body, [(h.name, h.value) for h in sr.headers], sr.timestamp
)
for sr in batch.records
]
def access_token_info_message(
id: str, scope: AccessTokenScope, auto_prefix_streams: bool, expires_at: int | None
) -> msgs.AccessTokenInfo:
def resource_set(rule: ResourceMatchRule | None) -> msgs.ResourceSet | None:
if rule is None:
return None
match rule.match_op:
case ResourceMatchOp.EXACT:
return msgs.ResourceSet(exact=rule.value)
case ResourceMatchOp.PREFIX:
return msgs.ResourceSet(prefix=rule.value)
case _:
raise ValueError(
"ResourceMatchOp doesn't match any of the expected values"
)
def permissions(perm: Permission) -> msgs.ReadWritePermissions:
read = False
write = False
match perm:
case Permission.UNSPECIFIED:
pass
case Permission.READ:
read = True
case Permission.WRITE:
write = True
case Permission.READ_WRITE:
read = True
write = True
return msgs.ReadWritePermissions(read=read, write=write)
def permitted_op_groups(
op_group_perms: OperationGroupPermissions | None,
) -> msgs.PermittedOperationGroups | None:
if op_group_perms is None:
return None
return msgs.PermittedOperationGroups(
account=permissions(op_group_perms.account),
basin=permissions(op_group_perms.basin),
stream=permissions(op_group_perms.stream),
)
return msgs.AccessTokenInfo(
id=id,
expires_at=expires_at,
auto_prefix_streams=auto_prefix_streams,
scope=msgs.AccessTokenScope(
basins=resource_set(scope.basins),
streams=resource_set(scope.streams),
access_tokens=resource_set(scope.access_tokens),
op_groups=permitted_op_groups(scope.op_group_perms),
ops=(op.value for op in scope.ops),
),
)
def access_token_info_schema(info: msgs.AccessTokenInfo) -> AccessTokenInfo:
def resource_match_rule(resource_set: msgs.ResourceSet) -> ResourceMatchRule | None:
if not resource_set.HasField("matching"):
return None
match resource_set.WhichOneof("matching"):
case "exact":
return ResourceMatchRule(ResourceMatchOp.EXACT, resource_set.exact)
case "prefix":
return ResourceMatchRule(ResourceMatchOp.PREFIX, resource_set.prefix)
case _:
raise RuntimeError(
"ResourceSet matching doesn't match any of the expected values"
)
def permission(perms: msgs.ReadWritePermissions) -> Permission:
if perms.read and perms.write:
return Permission.READ_WRITE
elif perms.read:
return Permission.READ
elif perms.write:
return Permission.WRITE
else:
return Permission.UNSPECIFIED
return AccessTokenInfo(
id=info.id,
scope=AccessTokenScope(
basins=resource_match_rule(info.scope.basins),
streams=resource_match_rule(info.scope.streams),
access_tokens=resource_match_rule(info.scope.access_tokens),
op_group_perms=OperationGroupPermissions(
account=permission(info.scope.op_groups.account),
basin=permission(info.scope.op_groups.basin),
stream=permission(info.scope.op_groups.stream),
),
ops=[Operation(op) for op in info.scope.ops],
),
auto_prefix_streams=info.auto_prefix_streams,
expires_at=info.expires_at if info.HasField("expires_at") else None,
)
def _read_limit_message(limit: ReadLimit | None) -> msgs.ReadLimit:
return (
msgs.ReadLimit(count=limit.count, bytes=limit.bytes)
if limit
else msgs.ReadLimit()
)
def _not_a_command_record(
headers: RepeatedCompositeFieldContainer[msgs.Header],
) -> bool:
if len(headers) == 1 and headers[0].name == b"":
return False
return True