1mod clear;
2pub mod compact;
3mod contains;
4mod count;
5mod del;
6mod del_prefix;
7mod get;
8mod get_batch;
9mod insert;
10mod keys;
11mod keys_from;
12mod keys_prefix;
13mod open;
14mod options;
15mod put;
16mod qry;
17mod qry_batch;
18mod remove;
19mod rev_keys;
20mod rev_keys_from;
21mod rev_keys_prefix;
22mod rev_stream;
23mod rev_stream_from;
24mod rev_stream_prefix;
25mod seek;
26mod stream;
27mod stream_from;
28mod stream_prefix;
29mod watch;
30
31use std::{
32 ffi::CStr,
33 fmt,
34 fmt::{Debug, Display},
35 sync::Arc,
36};
37
38use rocksdb::{AsColumnFamilyRef, ColumnFamily, DBCommon, ReadOptions, WriteOptions};
39use tuwunel_core::Result;
40
41pub(crate) use self::options::{
42 cache_iter_options_default, cache_read_options_default, iter_options_default,
43 read_options_default, write_options_default,
44};
45use self::watch::Watch;
46pub use self::{get_batch::Get, qry_batch::Qry};
51use crate::{Engine, util::map_err};
52
53pub struct Map {
58 name: &'static str,
59 watch: Watch,
60 cf: Arc<ColumnFamily>,
61 engine: Arc<Engine>,
62 read_options: ReadOptions,
63 cache_read_options: ReadOptions,
64 write_options: WriteOptions,
65}
66
67impl Map {
68 pub(crate) fn open(engine: &Arc<Engine>, name: &'static str) -> Result<Arc<Self>> {
74 Ok(Arc::new(Self {
75 name,
76 watch: Watch::default(),
77 cf: open::open(engine, name),
78 engine: engine.clone(),
79 read_options: read_options_default(engine),
80 cache_read_options: cache_read_options_default(engine),
81 write_options: write_options_default(engine),
82 }))
83 }
84
85 #[tracing::instrument(
92 level = "info",
93 skip_all,
94 fields(
95 map = self.name(),
96 sequence = ?self.engine.current_sequence(),
97 ),
98 )]
99 pub fn sort(&self) -> Result {
100 let cf = self.cf();
101 let flushoptions = rocksdb::FlushOptions::default();
102 DBCommon::flush_cf_opt(&self.engine.db, &cf, &flushoptions).map_err(map_err)
103 }
104
105 #[inline]
110 pub fn property_integer(&self, name: &CStr) -> Result<u64> {
111 self.engine.property_integer(&self.cf(), name)
112 }
113
114 #[inline]
119 pub fn property(&self, name: &str) -> Result<String> {
120 self.engine.property(&self.cf(), name)
121 }
122
123 #[inline]
128 pub fn name(&self) -> &str { self.name }
129
130 #[inline]
135 pub(crate) fn engine(&self) -> &Arc<Engine> { &self.engine }
136
137 #[inline]
141 pub(crate) fn cf(&self) -> impl AsColumnFamilyRef + '_ { &*self.cf }
142
143 #[inline]
148 pub(crate) fn cf_id(&self) -> u32 { self.cf().id() }
149}
150
151impl Debug for Map {
152 fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
153 write!(out, "Map {{name: {0}}}", self.name)
154 }
155}
156
157impl Display for Map {
158 fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result { write!(out, "{0}", self.name) }
159}