popoto.fields.indexed_field_mixin¶
popoto.fields.indexed_field_mixin
¶
Indexed Field Mixin - Secondary Indexing for Non-Key Fields¶
This module provides the IndexedFieldMixin class, which enables exact-match secondary indexing on fields that are NOT part of the model's Redis key.
Design Philosophy¶
KeyField conflates two concerns: identity (forming the Redis storage key) and indexing (enabling queries). IndexedFieldMixin decouples these by providing Set-based indexing without making the field part of the Redis key.
This allows developers to query on fields like email, status, or
category without making them part of the model's identity.
The implementation follows the exact same pattern as KeyFieldMixin:
- on_save(): Maintains a Redis Set at $IdxF:Model:field_name:value
- on_delete(): Removes from the Set
- filter_query(): Uses SMEMBERS/SUNION for lookups
- get_filter_query_params(): Declares supported query lookups
Index Key Pattern¶
$IdxF:ModelName:field_name:value -> Set of redis_keys
This mirrors the $KeyF pattern used by KeyFieldMixin but uses the
$IdxF prefix (auto-generated by FieldBase metaclass via field_class_key).
Usage¶
from popoto import Model, Field
from popoto.fields.shortcuts import IndexedField, UniqueField
class User(Model):
user_id = AutoKeyField()
email = UniqueField(type=str) # indexed + unique
status = IndexedField(type=str) # indexed, not unique
# Efficient exact-match queries without making email a KeyField
User.query.filter(email="alice@example.com")
User.query.filter(status="active")
User.query.filter(status__in=["active", "pending"])
IndexedFieldMixin
¶
Mixin that provides Set-based secondary indexing for non-key fields.
When mixed with Field, this mixin maintains Redis Sets that track which model instances have a given value for the indexed field. This enables efficient exact-match queries without making the field part of the model's Redis key (identity).
Supports the same query lookups as KeyFieldMixin:
- Exact match: filter(field=value)
- IN queries: filter(field__in=[v1, v2])
- Null checks: filter(field__isnull=True/False)
- Pattern matching: filter(field__startswith="prefix")
- Pattern matching: filter(field__endswith="suffix")
Uniqueness enforcement is available via unique=True, which checks
the index Set before adding a new entry. This is best-effort under
concurrent writes (same trade-off as UniqueKeyField).
Attributes:
| Name | Type | Description |
|---|---|---|
indexed |
bool
|
Always True for indexed fields. |
Source code in src/popoto/fields/indexed_field_mixin.py
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 | |
on_save(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Maintain the secondary index Set when a model instance is saved.
Adds the model instance's Redis key to the Set at
$IdxF:Model:field_name:value. If the value has changed since
last save, removes the old index entry first.
When unique=True, checks the target Set before adding. Raises
ModelException if another instance already occupies that value.
Note: this uniqueness check has a race condition under concurrent
writes -- same trade-off as UniqueKeyField.
All operations use the provided pipeline for zero extra round-trips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
Model
|
The Model instance being saved. |
required |
field_name
|
str
|
The name of this field on the model. |
required |
field_value
|
The value being saved for this field. |
required | |
pipeline
|
Pipeline
|
Optional Redis pipeline for batched operations. |
None
|
Returns:
| Type | Description |
|---|---|
|
The pipeline (if provided) or the SADD result. |
Raises:
| Type | Description |
|---|---|
ModelException
|
If unique=True and the value is already taken by a different instance. |
Source code in src/popoto/fields/indexed_field_mixin.py
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 | |
on_delete(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Remove the model instance from the index Set on delete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
Model
|
The Model instance being deleted. |
required |
field_name
|
str
|
The name of this field on the model. |
required |
field_value
|
The value stored for this field. |
required | |
pipeline
|
Pipeline
|
Optional Redis pipeline for batched operations. |
None
|
Returns:
| Type | Description |
|---|---|
|
The pipeline (if provided) or the SREM result. |
Source code in src/popoto/fields/indexed_field_mixin.py
get_filter_query_params(field_name)
¶
Return the set of valid query parameter names for filtering on this field.
Supports the same lookups as KeyFieldMixin: - exact match - __in - __isnull - __startswith - __endswith
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
The name of this field on the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
set |
set
|
Valid query parameter strings. |
Source code in src/popoto/fields/indexed_field_mixin.py
filter_query(model, field_name, **query_params)
classmethod
¶
Execute a filter query on this indexed field and return matching Redis keys.
Uses Set-based lookups for exact match and __in queries. Falls back to SCAN for pattern queries (__startswith, __endswith).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The Model class being queried. |
required |
field_name
|
str
|
The name of this field on the model. |
required |
**query_params
|
Dict mapping query parameter names to values. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
set |
set
|
Redis keys (as bytes) of matching model instances. |
Raises:
| Type | Description |
|---|---|
QueryException
|
If __isnull receives a non-boolean value. |
Source code in src/popoto/fields/indexed_field_mixin.py
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 | |