popoto.stores.filesystem¶
popoto.stores.filesystem
¶
Filesystem-based content store using content-addressable storage.
Files are organized under a configurable base path with two-level directory sharding based on the SHA-256 hash of the content. Writes are atomic (temp file + os.rename) to prevent partial-write corruption.
Directory structure
{base_path}/{ModelClassName}/{key_value}.{ext} <- live file {base_path}/.versions/{hash_prefix}/{hash}.{ext} <- archived versions
FilesystemStore
¶
Bases: AbstractContentStore
Content-addressable filesystem storage backend.
Stores content files organized by model class name and key value. Uses SHA-256 hashing for content addressing and versioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
str
|
Root directory for content storage. Defaults to ~/.popoto/content/ or POPOTO_CONTENT_PATH env var. |
None
|
extension
|
str
|
Default file extension for stored content. Default ".txt". |
'.txt'
|
Example
store = FilesystemStore(base_path="/data/content") ref = store.save(b"hello world", key="greeting", model_class_name="Memory") content = store.load(ref) assert content == b"hello world"
Source code in src/popoto/stores/filesystem.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 | |
save(content, key, model_class_name)
¶
Persist content to filesystem and return a reference string.
If a live file already exists at the target path, its current content is archived to .versions/ before the new content is written.
Reference format: $CF:{sha256_hash}:{model_class_name}/{sanitized_key}{ext}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes
|
Raw content bytes to store. |
required |
key
|
str
|
The model instance's key value (for human-readable naming). |
required |
model_class_name
|
str
|
The model class name (for directory organization). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Reference string in the format $CF:{hash}:{relative_path} |
Raises:
| Type | Description |
|---|---|
IOError
|
If the content cannot be written to the filesystem. |
Source code in src/popoto/stores/filesystem.py
load(reference)
¶
Load content from filesystem by reference string.
Attempts to read from the live path first. If the live file's hash doesn't match the reference, falls back to the versioned archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
str
|
Reference string in $CF:{hash}:{relative_path} format. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The raw content bytes. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the referenced content cannot be found. |
Source code in src/popoto/stores/filesystem.py
delete(reference)
¶
Remove a content file by reference.
Removes the live file only. Archived versions are left intact (append-only). Use garbage_collect() to clean orphaned versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
str
|
Reference string in $CF:{hash}:{relative_path} format. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the live file does not exist. |
Source code in src/popoto/stores/filesystem.py
exists(reference)
¶
Check if content exists for the given reference.
Mirrors load() logic: checks the live path hash first, then falls back to the version archive. This ensures exists() returning True guarantees that load() will succeed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
str
|
Reference string in $CF:{hash}:{relative_path} format. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the content can be loaded, False otherwise. |