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}
19
20impl ProviderSequenceValues {
21    pub fn new(provider: Arc<dyn PersistentStorageProvider>) -> Self {
22        Self { provider }
23    }
24}
25
26impl LatestSequenceValues for ProviderSequenceValues {
27    fn latest_sequence_values(
28        &self,
29    ) -> StorageBackendResult<HashMap<SequencePositionKey, SequenceValuePosition>> {
30        let session = self.provider.open_session()?;
31        let rows = with_read_transaction(&session, |catalog| catalog.load_sequence_rows())?;
32        Ok(rows
33            .into_iter()
34            .map(|row| {
35                (
36                    SequencePositionKey {
37                        object: row.object_id,
38                        definition: row.definition_generation,
39                    },
40                    SequenceValuePosition {
41                        current: row.current,
42                        called: row.called,
43                        log_count: row.log_count,
44                    },
45                )
46            })
47            .collect())
48    }
49}