Storage¶
Design rationale: Session storage and ADR 0002.
Storage lifecycle¶
Bases: SinkFactory
Manages the lifecycle of a storage backend for dual-context producers.
Async device logics use await sink(key).put(frame); sync document
callbacks use sink(key).put_nowait(frame). One drain task per key
(spawned by sink) writes frames to the backend; the last drain out
closes the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
io
|
StorageIO
|
Backend mechanics (open / uri / resource_info). |
required |
path_provider
|
PathProvider
|
Resolves where each burst lands. Consulted at first |
required |
maxsize
|
int
|
Per-key frame queue bound. Defaults to 100. |
100
|
Source code in src/redsun/storage/_base.py
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 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 | |
path_provider
property
¶
The provider consulted at first register for burst paths.
register
¶
Register a new stream with the backend.
Raises:
| Type | Description |
|---|---|
StoreStateError
|
If the store is open, opening, or closing. |
KeyError
|
If |
Source code in src/redsun/storage/_base.py
sink
¶
Create the producer handle for data_key and spawn its drain.
Must be called on the event-loop thread (device prepare or a document callback), which is where all producers live.
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
StoreStateError
|
If a live sink already exists for |
Source code in src/redsun/storage/_base.py
open
async
¶
Idempotently open the backend store.
The first caller opens; concurrent callers await the same open on the lock. Called eagerly from device data logics at prepare time, and lazily by every drain before its first write.
Raises:
| Type | Description |
|---|---|
StoreStateError
|
If no stream is registered. |
Source code in src/redsun/storage/_base.py
close
async
¶
Tear down the current burst.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flush
|
bool
|
If True (default) queued frames are written before closing; if False they are dropped (abort semantics). |
True
|
Notes
Drain write failures don't raise at the producer: gather is
called with return_exceptions=True, so a drain that dies mid-burst
surfaces nowhere until its exception is collected here - close()
is the error observation point for the whole burst.
Source code in src/redsun/storage/_base.py
Producer-facing handle for one stream's frame queue.
Exposes only the producer face of the underlying queue: the consumer
face (async_get, immediate shutdown, clear) stays private to
the owning storage's drain task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queue
|
Queue
|
The bounded frame queue owned by the storage layer. |
required |
Source code in src/redsun/storage/_sink.py
put
async
¶
Enqueue a frame from an async producer, parking when the queue is full.
Raises:
| Type | Description |
|---|---|
QueueShutDown
|
If the stream has reached capacity or was closed. |
Source code in src/redsun/storage/_sink.py
put_nowait
¶
Enqueue a frame from a sync producer without ever blocking.
Safe inside emit_sync document callbacks on the loop thread.
Raises:
| Type | Description |
|---|---|
QueueFull
|
If the queue is full - a callback bug or a stalled backend. |
QueueShutDown
|
If the stream has reached capacity or was closed. |
Source code in src/redsun/storage/_sink.py
close
¶
Shut the stream down cleanly. Idempotent.
Queued frames are still written (flush semantics); further puts
raise culsans.QueueShutDown.
Specification of a frame stream for a data sink channel.
Attributes:
| Name | Type | Description |
|---|---|---|
data_key |
str
|
Channel identity, e.g. the detector's datakey name. |
shape |
tuple[int, int]
|
Shape of a single frame, e.g. |
dtype |
str
|
NumPy dtype of the frames (e.g. |
capacity |
int | None
|
Maximum number of frames. |
Source code in src/redsun/storage/_base.py
Bases: RuntimeError
Raised when a storage operation is attempted in an invalid state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verb
|
str
|
The operation attempted. |
required |
why
|
str
|
Why the operation is invalid right now. |
required |
Source code in src/redsun/storage/_base.py
Backend protocols¶
Bases: Protocol
Consumer API for ophyd-async data logics and document callbacks.
Source code in src/redsun/storage/_base.py
Bases: Protocol
Unopened store factory. Implements the API for creating a new storage backend instance.
Source code in src/redsun/storage/_base.py
Bases: Protocol
Hot write path of a multi-stream storage backend.
Source code in src/redsun/storage/_base.py
Path providing¶
Bases: PathProvider
Session-scoped path provider.
Computes paths in the layout:
The date is evaluated per request, not at construction. The counter is
per (session, plan) and only ever increases: the date directory groups
files, it does not scope the counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
Path | None
|
Base directory for storage; |
None
|
session
|
str
|
Session name, fixed for the provider's lifetime. Defaults to
|
'unknown-session'
|
max_digits
|
int
|
Zero-padding width for the counter. Defaults to 5 (e.g. |
5
|
now
|
Callable[[], datetime] | None
|
Clock used to compute the date directory, for testing. Defaults to
|
None
|
Source code in src/redsun/storage/_path_provider.py
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 | |
set_base_dir
¶
Change the base directory, resetting and rescanning all counters.
Source code in src/redsun/storage/_path_provider.py
Registry¶
register_storage
¶
Register a storage instance under (group, storage.mimetype).
Raises:
| Type | Description |
|---|---|
KeyError
|
If a storage is already registered under that key. |
Source code in src/redsun/storage/_registry.py
get_storage
¶
Resolve the storage instance for (group, mimetype).
Raises:
| Type | Description |
|---|---|
KeyError
|
If no storage is registered under that key. |
Source code in src/redsun/storage/_registry.py
reset_group
async
¶
Abort every storage in group: close with drop semantics.
Instances stay registered - a session-scoped storage survives the teardown of one burst.
Source code in src/redsun/storage/_registry.py
clear_registry
¶
Remove every registered storage. For session teardown and tests.
This does not close the dropped storages - any that are still open
leak their open store. Callers must reset_group (or close each
storage directly) first if a storage might still be open.