assembly_data/fdb/ro/
mod.rs

1//! Read-Only low level access to a database file
2
3use std::{ops::Deref, sync::Arc};
4
5use assembly_core::buffer::{CastError, MinimallyAligned, Repr};
6
7use self::buffer::Buffer;
8
9use super::file::ArrayHeader;
10
11pub mod buffer;
12pub mod handle;
13pub mod slice;
14
15/// An owned, atomically-reference counted handle to a database
16pub type ArcHandle<B, T> = BaseHandle<Arc<B>, T>;
17
18impl<B: AsRef<[u8]>> ArcHandle<B, ()> {
19    /// Create a new atomically-reference counted handle
20    pub fn new_arc(inner: B) -> Self {
21        Self::new(Arc::new(inner))
22    }
23}
24
25impl<B: AsRef<[u8]>, T: Copy> ArcHandle<B, T> {
26    /// Borrow the atomically-reference counted handle as a byte handle
27    ///
28    /// You can use this function to make cloning cheaper
29    pub fn as_bytes_handle(&self) -> Handle<T> {
30        BaseHandle {
31            mem: Buffer::new(self.mem.as_ref().as_ref()),
32            raw: self.raw,
33        }
34    }
35}
36
37/// Base type for a handle to an in-memory data structure
38///
39/// This is basic layout of a handle to an in-memory FDB database.
40/// Internally, there is a pointer (`&[u8]`/`Box<[u8]>`/`Rc<[u8]>`/`Arc<Mmap>`/…)
41/// the memory slice as well as a value that represents the
42/// current target.
43#[derive(Clone, Debug)]
44pub struct BaseHandle<P: Deref, T>
45where
46    <P as Deref>::Target: AsRef<[u8]>,
47{
48    /// The memory pointer
49    pub(super) mem: P,
50    /// The raw value
51    pub(super) raw: T,
52}
53
54impl<P, T> Copy for BaseHandle<P, T>
55where
56    P: Deref + Copy,
57    T: Copy,
58    <P as Deref>::Target: AsRef<[u8]>,
59{
60}
61
62impl<P: Deref> BaseHandle<P, ()>
63where
64    <P as Deref>::Target: AsRef<[u8]>,
65{
66    /// Creates a new handle
67    pub fn new(mem: P) -> Self {
68        Self { mem, raw: () }
69    }
70}
71
72impl<T, P: Deref> BaseHandle<P, Option<T>>
73where
74    <P as Deref>::Target: AsRef<[u8]>,
75{
76    /// Turns a handle of an option into an option of a handle
77    pub fn transpose(self) -> Option<BaseHandle<P, T>> {
78        if let Some(raw) = self.raw {
79            Some(BaseHandle { mem: self.mem, raw })
80        } else {
81            None
82        }
83    }
84}
85
86impl<P: Deref, T> BaseHandle<P, T>
87where
88    <P as Deref>::Target: AsRef<[u8]>,
89{
90    /// Get a reference to the raw value inside
91    pub fn raw(&self) -> &T {
92        &self.raw
93    }
94
95    /// Get a reference to the raw value inside
96    pub fn raw_mut(&mut self) -> &mut T {
97        &mut self.raw
98    }
99
100    /// Get the byte slice for the whole database
101    pub fn as_bytes(&self) -> &[u8] {
102        self.mem.deref().as_ref()
103    }
104
105    /// Replace the value that is stored with the memory pointer
106    pub fn replace<O>(self, raw: O) -> BaseHandle<P, O> {
107        BaseHandle { mem: self.mem, raw }
108    }
109}
110
111/// The basic handle into a byte buffer
112pub type Handle<'a, T> = BaseHandle<Buffer<'a>, T>;
113
114impl<'a, T> Handle<'a, T> {
115    /// Returns a copy of the contained buffer
116    pub fn buf(self) -> Buffer<'a> {
117        self.mem
118    }
119
120    /// Get the raw value out of the handle
121    pub fn into_raw(self) -> T {
122        self.raw
123    }
124
125    /// Wrap a value as a handle
126    pub(crate) fn wrap<R>(&self, raw: R) -> Handle<'a, R> {
127        Handle { mem: self.mem, raw }
128    }
129
130    /// Map a cast reference
131    pub(crate) fn try_map_cast<R: MinimallyAligned>(
132        &self,
133        offset: u32,
134    ) -> Result<RefHandle<'a, R>, CastError> {
135        let raw: &'a R = self.mem.try_cast(offset)?;
136        Ok(self.wrap(raw))
137    }
138
139    /// Map a casted slice
140    pub(crate) fn try_map_cast_slice<R: MinimallyAligned>(
141        &self,
142        offset: u32,
143        count: u32,
144    ) -> Result<RefHandle<'a, [R]>, CastError> {
145        let raw: &'a [R] = self.mem.try_cast_slice(offset, count)?;
146        Ok(self.wrap(raw))
147    }
148
149    /// Map a casted array
150    pub(crate) fn try_map_cast_array<R: MinimallyAligned>(
151        &self,
152        array: ArrayHeader,
153    ) -> Result<RefHandle<'a, [R]>, CastError> {
154        let raw: &'a [R] = self.mem.try_cast_slice(array.base_offset, array.count)?;
155        Ok(self.wrap(raw))
156    }
157
158    /// Map something with a closure
159    pub fn map<X>(self, mapper: impl Fn(Buffer<'a>, T) -> X) -> Handle<'a, X> {
160        let raw = mapper(self.mem, self.raw);
161        Handle { mem: self.mem, raw }
162    }
163
164    /// Map the value with a closure
165    pub fn map_val<X>(self, mapper: impl Fn(T) -> X) -> Handle<'a, X> {
166        let raw = mapper(self.raw);
167        Handle { mem: self.mem, raw }
168    }
169
170    /// Map something with a closure
171    pub fn try_map<X, E>(
172        self,
173        mapper: impl Fn(Buffer<'a>, T) -> Result<X, E>,
174    ) -> Result<Handle<'a, X>, E> {
175        let raw = mapper(self.mem, self.raw)?;
176        Ok(Handle { mem: self.mem, raw })
177    }
178}
179
180impl<'a, T> RefHandle<'a, [T]> {
181    /// Get the reference at `index`
182    pub fn get(self, index: usize) -> Option<RefHandle<'a, T>> {
183        self.raw.get(index).map(|raw| self.wrap(raw))
184    }
185}
186
187/// A handle that contains a reference
188pub type RefHandle<'a, T> = Handle<'a, &'a T>;
189
190impl<'a, T: Repr> RefHandle<'a, T> {
191    /// Extract a value from a reference
192    pub fn map_extract(self) -> Handle<'a, T::Value> {
193        self.wrap(self.raw.extract())
194    }
195}