popoto.fields.content_field¶
popoto.fields.content_field
¶
ContentField: Large content storage with filesystem offloading.
Routes large content values (documents, text, binary data) to filesystem storage via a pluggable ContentStore backend. Redis stores only a compact reference string ($CF:{hash}:{path}), keeping memory usage minimal.
Content is lazy-loaded on attribute access -- queried model instances start with the reference string and only read from the filesystem when the content attribute is actually accessed.
Design
- on_save() writes content to filesystem BEFORE pipeline.execute(), then adds an HSET command to store the reference in Redis.
- The descriptor (get) detects $CF: references and transparently loads from the filesystem, caching on the instance dict.
- on_delete() is a no-op (append-only storage). Use garbage_collect() to clean orphaned files.
Example
class Document(popoto.Model): name = popoto.KeyField() body = ContentField()
doc = Document(name="readme", body="# Hello World") doc.save() # body written to filesystem, $CF reference in Redis
ContentField
¶
Bases: Field
Field type for storing large content on the filesystem.
Values assigned to a ContentField are written to a configurable content store (default: FilesystemStore) on save. Redis stores only a compact reference string. On attribute access, the content is lazy-loaded from the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
A ContentStore instance or "filesystem" (default). |
'filesystem'
|
|
**kwargs
|
Standard Field keyword arguments. |
{}
|
Example
class Memory(popoto.Model): topic = popoto.KeyField() content = ContentField(store="filesystem")
m = Memory(topic="revenue", content="# Revenue Analysis\n...") m.save()
Source code in src/popoto/fields/content_field.py
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 | |
store
property
¶
Get the content store instance.
format_value_pre_save(field_value, **kwargs)
¶
Pass through the value. Filesystem write happens in on_save().
We cannot write to filesystem here because we lack access to the model instance (needed for key values and class name). The value passes through unchanged; on_save() handles the filesystem write and Redis reference update.
Source code in src/popoto/fields/content_field.py
on_save(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Write content to filesystem and update Redis with reference.
This hook runs after HSET is queued on the pipeline but before pipeline.execute(). It writes content to filesystem first, then adds an HSET command to overwrite the field value with the $CF reference string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
The Model instance being saved. |
required | |
field_name
|
str
|
Name of this field on the model. |
required |
field_value
|
Current value (raw content or existing $CF reference). |
required | |
pipeline
|
Redis pipeline for batched operations. |
None
|
|
**kwargs
|
Additional context. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The pipeline with reference update command added. |
Source code in src/popoto/fields/content_field.py
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 | |
on_delete(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
No-op on delete. Content files are append-only.
Use garbage_collect() to remove orphaned content files.
Source code in src/popoto/fields/content_field.py
garbage_collect(model_class)
classmethod
¶
Remove orphaned content files not referenced by any live model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
The Model class to garbage collect content for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Number of orphaned files removed. |
Source code in src/popoto/fields/content_field.py
get_default_store()
¶
Get or create the default FilesystemStore.