Skip to main content

uqa_storage/
read_control.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Query-owned provider reads share allocation limits and cancellation with their consumers.
8
9use crate::StorageBackendResult;
10use uqa_core::{memory::MemoryBudget, CancellationToken};
11
12pub type ValueReadVisitor<'a> = dyn FnMut(Option<&[u8]>) -> StorageBackendResult<()> + 'a;
13pub type KeyValueReadVisitor<'a> = dyn FnMut(&[u8], &[u8]) -> StorageBackendResult<()> + 'a;
14
15#[derive(Clone, Debug)]
16pub struct StorageReadControl {
17    memory: MemoryBudget,
18    cancellation: CancellationToken,
19}
20impl StorageReadControl {
21    pub fn with_limit(limit: usize) -> Self {
22        Self {
23            memory: MemoryBudget::new(limit),
24            cancellation: CancellationToken::new(),
25        }
26    }
27    pub fn new(memory: &MemoryBudget, cancellation: &CancellationToken) -> Self {
28        Self {
29            memory: memory.clone(),
30            cancellation: cancellation.clone(),
31        }
32    }
33    pub fn memory(&self) -> &MemoryBudget {
34        &self.memory
35    }
36    pub fn cancellation(&self) -> &CancellationToken {
37        &self.cancellation
38    }
39    pub fn check(&self) -> StorageBackendResult<()> {
40        self.cancellation.check()?;
41        Ok(())
42    }
43}