Skip to main content

tuwunel_database/map/
insert.rs

1//! Insert a Key+Value into the database.
2//!
3//! Overloads are provided for the user to choose the most efficient
4//! serialization or bypass for pre=serialized (raw) inputs.
5
6use tuwunel_core::implement;
7
8use crate::util::or_else;
9
10/// Stores a raw key and raw value in this map.
11///
12/// The write uses the map's configured options, flushes immediately when the
13/// engine is uncorked, and then notifies matching watchers. When the engine is
14/// corked, the surrounding cork controls flushing.
15///
16/// # Panics
17///
18/// Panics if RocksDB rejects the write or an uncorked flush fails.
19#[implement(super::Map)]
20#[tracing::instrument(skip_all, fields(%self), level = "trace")]
21pub fn insert<K, V>(&self, key: &K, val: V)
22where
23	K: AsRef<[u8]> + ?Sized,
24	V: AsRef<[u8]>,
25{
26	let write_options = &self.write_options;
27	self.engine
28		.db
29		.put_cf_opt(&self.cf(), key, val, write_options)
30		.or_else(or_else)
31		.expect("database insert error");
32
33	if !self.engine.corked() {
34		self.engine.flush().expect("database flush error");
35	}
36
37	self.notify(key.as_ref());
38}