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
118 119 120 121 | |
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
123 124 125 126 127 128 129 130 | |
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
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 | |
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
386 387 388 389 390 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 | |
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
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
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
703 704 705 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 | |
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
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | |
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
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | |
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
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 974 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 | |
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
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 | |
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
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 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 | |
CausationNode
Tree node used by build_causation_tree() to represent the causation
hierarchy of messages sharing a correlation_id.