BaseEventStore
Event store interface for event-sourced persistence. All event store adapters (Memory, MessageDB, etc.) implement this contract.
See Event Store Adapters for concrete adapter configuration.
This class outlines the base event store capabilities to be implemented in all supported event store adapters.
It is also a marker interface for registering event store classes with the domain.
Source code in src/protean/port/event_store.py
123 124 125 126 | |
close
close() -> None
Close the event store and release all connections.
Subclasses that hold external resources (connection pools, sockets, etc.) should override this to perform cleanup. The default implementation is a no-op so that adapters without external resources (e.g. the in-memory store) work without changes.
Source code in src/protean/port/event_store.py
128 129 130 131 132 133 134 135 | |
read_all
read_all(
stream: str = "$all", *, page_size: int = 1000
) -> Iterator[Message]
Yield every message in stream, paging through the store in bounded batches.
A cold-load read that must be complete (a full projection rebuild, a
backup, an integrity check) cannot rely on a single large read with a
sentinel no_of_messages: past the cap it silently truncates. This
iterator pages the store in page_size batches and advances a cursor
until a short page signals the end, so it reads the whole stream at a
bounded memory cost regardless of size.
Paging is done on the raw store rows, not on deserialized messages, so
that interleaved snapshot rows (type == "SNAPSHOT", no metadata) do
not distort it. Snapshots are skipped from the output — the iterator
yields events and commands only — but they still count toward the raw
page, so a page carrying a snapshot neither ends the read early nor
desyncs the cursor. The cursor advances from the last raw row of each
page, which may itself be a snapshot; its top-level position is read
directly, never through Message.deserialize.
The cursor field follows the stream shape (ADR-0024): $all and a bare
category page by global_position; a specific stream (category-id)
pages by its own per-stream position. Reads are inclusive
(>= position), so each next page resumes one past the last row seen,
which avoids re-emitting the boundary row.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream to read.
TYPE:
|
page_size
|
Number of messages to read per underlying
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Message
|
Every |
Message
|
no duplicates across page boundaries. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If |
Source code in src/protean/port/event_store.py
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 | |
load_aggregate
load_aggregate(
part_of: type[BaseAggregate],
identifier: str,
*,
at_version: int | None = None,
as_of: datetime | None = None,
) -> BaseAggregate | None
Load an aggregate from underlying events.
By default, reconstitutes the aggregate to its current (latest) state.
When at_version or as_of is provided, reconstitutes a historical
snapshot of the aggregate: a temporal query.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate's class.
TYPE:
|
identifier
|
Unique aggregate identifier.
TYPE:
|
at_version
|
Reconstitute to this exact version (0-indexed). Version 0 is the state after the first event.
TYPE:
|
as_of
|
Reconstitute the aggregate as of this timestamp.
Only events written on or before
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseAggregate | None
|
The fully-formed aggregate, or |
BaseAggregate | None
|
(and no temporal param was given that would raise instead). |
Source code in src/protean/port/event_store.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
create_snapshot
create_snapshot(
part_of: type[BaseAggregate], identifier: str
) -> bool
Create a snapshot for a specific event-sourced aggregate instance.
Reads the full event stream for the aggregate, reconstructs it via
from_events(), and writes a snapshot to the snapshot stream.
This bypasses the snapshot threshold -- manual triggers always create
a snapshot regardless of event count.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate class
TYPE:
|
identifier
|
Unique aggregate identifier
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if a snapshot was created. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If the aggregate is not event-sourced. |
ObjectNotFoundError
|
If no events exist for the given identifier. |
Source code in src/protean/port/event_store.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 | |
create_snapshots
create_snapshots(part_of: type[BaseAggregate]) -> int
Create snapshots for all instances of an event-sourced aggregate.
Discovers all unique aggregate identifiers in the stream category, then creates a snapshot for each.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of snapshots created. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If the aggregate is not event-sourced. |
Source code in src/protean/port/event_store.py
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | |
trace_causation
trace_causation(message_id: str | Message) -> list[Message]
Walk UP the causation chain from a message to the root.
Returns an ordered list of Messages from the root command (first) to the given message (last). The given message itself is included.
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
List of |
list[Message]
|
target last). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the message cannot be found in the event store. |
Source code in src/protean/port/event_store.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 | |
trace_effects
trace_effects(
message_id: str | Message, *, recursive: bool = True
) -> list[Message]
Walk DOWN the causation chain to find all effects of a message.
Returns messages that were caused by the given message, ordered by
global_position (chronological order).
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
recursive
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
List of |
list[Message]
|
in chronological order. The given message itself is NOT included. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the message cannot be found in the event store. |
Source code in src/protean/port/event_store.py
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
build_causation_tree
build_causation_tree(
correlation_id: str,
) -> CausationNode | None
Build a full causation tree for a correlation ID.
Returns the root node of the tree with children recursively populated.
| PARAMETER | DESCRIPTION |
|---|---|
correlation_id
|
The correlation ID to trace.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CausationNode | None
|
Root |
CausationNode | None
|
messages found. |
Source code in src/protean/port/event_store.py
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 | |
stream_head_position
stream_head_position(stream_category: str) -> int
Return the global_position of the newest message in a category stream.
Public wrapper around _stream_head_position.
| PARAMETER | DESCRIPTION |
|---|---|
stream_category
|
The stream category to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The |
int
|
stream has no messages. |
Source code in src/protean/port/event_store.py
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 | |
verify
verify() -> IntegrityReport
Check the store's internal invariants without mutating anything.
Reads the whole store once through $all and reports every violation
of these invariants:
- every row carries its required fields (
id,stream_name,position), - per-stream
positionis gapless from the stream base (0), global_positionis strictly increasing store-wide,- message ids are unique,
- each
:snapshot-stream carries a well-formed snapshot whose_versiondoes not exceed its aggregate stream head.
A corrupt row is reported, never silently skipped: that is the whole
point of the check, so a row missing a required field or a snapshot with
a non-integer _version becomes a violation rather than a pass, and
the missing-field guard runs first so those checks never touch an absent
value. This handles the corruption a store can actually hold: every
adapter types its columns (MessageDB by SQL column type, the memory
adapter by its pydantic model), so a present-but-wrongly-typed field
(a list id, a string position) does not arise from a read.
This asserts the store's internal consistency, not a schema version
(none is stored today). It is read-only: a clean store yields a report
with no violations (ok is True).
Source code in src/protean/port/event_store.py
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 | |
CausationNode
Tree node used by build_causation_tree() to represent the causation
hierarchy of messages sharing a correlation_id.