Skip to main content

uqa_sql/plan/
analyzed.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Immutable source topology retains the reservations of its independently shared analysis generation.
8
9use super::AnalyzedSource;
10use crate::SQLError;
11use std::{ops::Deref, sync::Arc};
12use uqa_core::memory::{Produced, ProductionControl};
13
14#[derive(Clone, Debug)]
15pub struct SharedAnalyzedSource(Storage);
16
17#[derive(Clone, Debug)]
18enum Storage {
19    Ordinary(Arc<AnalyzedSource>),
20    Admitted(Arc<Produced<AnalyzedSource>>),
21}
22
23impl SharedAnalyzedSource {
24    pub fn shares_storage(&self, other: &Self) -> bool {
25        std::ptr::eq::<AnalyzedSource>(&raw const **self, &raw const **other)
26    }
27    pub fn from_produced(
28        value: Produced<AnalyzedSource>,
29        control: &ProductionControl<'_>,
30    ) -> Result<Self, SQLError> {
31        Ok(Self(Storage::Admitted(value.into_shared(control)?)))
32    }
33
34    pub fn reserved_bytes(&self) -> usize {
35        match &self.0 {
36            Storage::Ordinary(_) => 0,
37            Storage::Admitted(value) => value.reserved_bytes(),
38        }
39    }
40}
41
42impl Deref for SharedAnalyzedSource {
43    type Target = AnalyzedSource;
44    fn deref(&self) -> &Self::Target {
45        match &self.0 {
46            Storage::Ordinary(value) => value,
47            Storage::Admitted(value) => value,
48        }
49    }
50}
51
52impl From<Arc<AnalyzedSource>> for SharedAnalyzedSource {
53    fn from(value: Arc<AnalyzedSource>) -> Self {
54        Self(Storage::Ordinary(value))
55    }
56}