Skip to main content

uni_store/runtime/
context.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4use crate::runtime::l0::L0Buffer;
5use parking_lot::RwLock;
6use std::sync::Arc;
7use std::time::Instant;
8
9#[derive(Clone)]
10pub struct QueryContext {
11    pub l0: Arc<RwLock<L0Buffer>>,
12    pub transaction_l0: Option<Arc<RwLock<L0Buffer>>>,
13    /// L0 buffers currently being flushed to L1.
14    /// These remain visible to reads until flush completes successfully.
15    pub pending_flush_l0s: Vec<Arc<RwLock<L0Buffer>>>,
16    pub deadline: Option<Instant>,
17}
18
19impl QueryContext {
20    pub fn new(l0: Arc<RwLock<L0Buffer>>) -> Self {
21        Self {
22            l0,
23            transaction_l0: None,
24            pending_flush_l0s: Vec::new(),
25            deadline: None,
26        }
27    }
28
29    pub fn new_with_tx(
30        l0: Arc<RwLock<L0Buffer>>,
31        transaction_l0: Option<Arc<RwLock<L0Buffer>>>,
32    ) -> Self {
33        Self {
34            l0,
35            transaction_l0,
36            pending_flush_l0s: Vec::new(),
37            deadline: None,
38        }
39    }
40
41    pub fn new_with_pending(
42        l0: Arc<RwLock<L0Buffer>>,
43        transaction_l0: Option<Arc<RwLock<L0Buffer>>>,
44        pending_flush_l0s: Vec<Arc<RwLock<L0Buffer>>>,
45    ) -> Self {
46        Self {
47            l0,
48            transaction_l0,
49            pending_flush_l0s,
50            deadline: None,
51        }
52    }
53
54    pub fn set_deadline(&mut self, deadline: Instant) {
55        self.deadline = Some(deadline);
56    }
57
58    pub fn check_timeout(&self) -> anyhow::Result<()> {
59        if let Some(deadline) = self.deadline
60            && Instant::now() > deadline
61        {
62            return Err(anyhow::anyhow!("Query timed out"));
63        }
64        Ok(())
65    }
66}