Skip to main content

uqa_execution/catalog/sequence/latest_values/
provider.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Read committed sequence value records through an independent session of the storage provider.
8
9use std::{collections::HashMap, sync::Arc};
10
11use super::LatestSequenceValues;
12use crate::{catalog::snapshot_read::with_read_transaction, row_locks::SequencePositionKey};
13use uqa_storage::{PersistentStorageProvider, SequenceValuePosition, StorageBackendResult};
14
15/// Reads the value records of the latest commit in a read transaction of its own, which leaves the caller's transaction and snapshot alone.
16pub struct ProviderSequenceValues {
17    provider: Arc<dyn PersistentStorageProvider>,
18    _memory: Option<uqa_core::memory::MemoryReservation>,
19}
20
21impl ProviderSequenceValues {
22    pub fn new(provider: Arc<dyn PersistentStorageProvider>) -> Self {
23        Self {
24            provider,
25            _memory: None,
26        }
27    }
28
29    pub fn shared_with_control(
30        provider: Arc<dyn PersistentStorageProvider>,
31        control: &uqa_core::memory::ProductionControl<'_>,
32    ) -> Result<Arc<Self>, uqa_sql::SQLError> {
33        let memory = control.reserve(size_of::<Self>())?;
34        Ok(Arc::new(Self {
35            provider,
36            _memory: memory,
37        }))
38    }
39}
40
41impl LatestSequenceValues for ProviderSequenceValues {
42    fn latest_sequence_values(
43        &self,
44    ) -> StorageBackendResult<HashMap<SequencePositionKey, SequenceValuePosition>> {
45        let session = self.provider.open_session()?;
46        let rows = with_read_transaction(&session, |catalog| catalog.load_sequence_rows())?;
47        Ok(rows
48            .into_iter()
49            .map(|row| {
50                (
51                    SequencePositionKey {
52                        object: row.object_id,
53                        definition: row.definition_generation,
54                    },
55                    SequenceValuePosition {
56                        current: row.current,
57                        called: row.called,
58                        log_count: row.log_count,
59                    },
60                )
61            })
62            .collect())
63    }
64}