Skip to main content

tuwunel_database/
map.rs

1mod clear;
2pub mod compact;
3mod contains;
4mod count;
5mod del;
6mod get;
7mod get_batch;
8mod insert;
9mod keys;
10mod keys_from;
11mod keys_prefix;
12mod open;
13mod options;
14mod put;
15mod qry;
16mod qry_batch;
17mod remove;
18mod rev_keys;
19mod rev_keys_from;
20mod rev_keys_prefix;
21mod rev_stream;
22mod rev_stream_from;
23mod rev_stream_prefix;
24mod stream;
25mod stream_from;
26mod stream_prefix;
27mod watch;
28
29use std::{
30	ffi::CStr,
31	fmt,
32	fmt::{Debug, Display},
33	sync::Arc,
34};
35
36use rocksdb::{AsColumnFamilyRef, ColumnFamily, ReadOptions, WriteOptions};
37use tuwunel_core::Result;
38
39pub(crate) use self::options::{
40	cache_iter_options_default, cache_read_options_default, iter_options_default,
41	read_options_default, write_options_default,
42};
43use self::watch::Watch;
44pub use self::{get_batch::Get, qry_batch::Qry};
45use crate::Engine;
46
47pub struct Map {
48	name: &'static str,
49	watch: Watch,
50	cf: Arc<ColumnFamily>,
51	engine: Arc<Engine>,
52	read_options: ReadOptions,
53	cache_read_options: ReadOptions,
54	write_options: WriteOptions,
55}
56
57impl Map {
58	pub(crate) fn open(engine: &Arc<Engine>, name: &'static str) -> Result<Arc<Self>> {
59		Ok(Arc::new(Self {
60			name,
61			watch: Watch::default(),
62			cf: open::open(engine, name),
63			engine: engine.clone(),
64			read_options: read_options_default(engine),
65			cache_read_options: cache_read_options_default(engine),
66			write_options: write_options_default(engine),
67		}))
68	}
69
70	#[inline]
71	pub fn property_integer(&self, name: &CStr) -> Result<u64> {
72		self.engine.property_integer(&self.cf(), name)
73	}
74
75	#[inline]
76	pub fn property(&self, name: &str) -> Result<String> {
77		self.engine.property(&self.cf(), name)
78	}
79
80	#[inline]
81	pub fn name(&self) -> &str { self.name }
82
83	#[inline]
84	pub(crate) fn engine(&self) -> &Arc<Engine> { &self.engine }
85
86	#[inline]
87	pub(crate) fn cf(&self) -> impl AsColumnFamilyRef + '_ { &*self.cf }
88}
89
90impl Debug for Map {
91	fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
92		write!(out, "Map {{name: {0}}}", self.name)
93	}
94}
95
96impl Display for Map {
97	fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result { write!(out, "{0}", self.name) }
98}