popoto.fields.event_stream¶
popoto.fields.event_stream
¶
EventStreamMixin — append-only mutation log via Redis Streams.
This module provides a mixin class that automatically appends to a Redis Stream on every save() or delete() call. It captures model class, primary key, operation type, timestamp, and configurable metadata fields.
Design
- _xadd_mutation() is called by base.py after successful save/delete
- _xadd_event() is a public method for non-save operations (e.g., ConfidenceField.update_confidence, CoOccurrenceField.strengthen)
- Stream entries use approximate MAXLEN trimming to bound memory
- Streams are partitionable by a configurable key field
Redis Key Patterns
- stream:{stream_name} — default stream key
- stream:{stream_name}:{partition_value} — partitioned stream key
Stream Entry Fields (all strings per Redis Streams spec): - model: Model class name - pk: Redis key of the instance - op: One of "create", "update", "delete" (or custom for _xadd_event) - ts: Unix timestamp string - changed_fields: Comma-separated list of updated fields (empty string for full saves) - Plus any fields named in _stream_metadata_fields
Example
class Memory(EventStreamMixin, Model): key = UniqueKeyField() content = StringField() source = StringField()
_stream_name = "memory_mutations"
_stream_metadata_fields = ("source",)
memory = Memory(key="fact1", content="hello", source="user") memory.save() # XADD to stream:memory_mutations
Partitioned streams:¶
class PartitionedMemory(EventStreamMixin, Model): key = UniqueKeyField() tenant = StringField()
_stream_name = "mutations"
_stream_partition_field = "tenant"
m = PartitionedMemory(key="x", tenant="acme") m.save() # XADD to stream:mutations:acme
EventStreamMixin
¶
Mixin that appends mutation entries to a Redis Stream on save/delete.
Add as a base class alongside Model
class MyModel(EventStreamMixin, Model): _stream_name = "my_mutations"
Class Attributes
_stream_name: Name for the Redis Stream. Default "mutations". _stream_partition_field: Optional field name to partition streams by. When set, the stream key includes the field's value. _stream_max_length: Approximate max entries in the stream. Default 10000. _stream_metadata_fields: Tuple of field names whose values are included in stream entries as additional key-value pairs.
Note: Attributes prefixed with underscore to avoid conflict with Popoto's ModelBase metaclass, which requires public attributes to be Fields.
Source code in src/popoto/fields/event_stream.py
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 | |