Skip to main content

tuwunel_database/map/
put.rs

1//! Serialize and Insert a Key+Value into the database.
2//!
3//! Overloads are provided for the user to choose the most efficient
4//! serialization. When no serialization is required for both key and
5//! value simply use insert() (see insert.rs).
6
7use std::{fmt::Debug, io::Write};
8
9use serde::Serialize;
10use tuwunel_core::{arrayvec::ArrayVec, implement};
11
12use crate::{
13	keyval::{KeyBuf, ValBuf},
14	ser,
15};
16
17/// Stores a serialized key and serialized value using owned buffers.
18///
19/// Both values pass through the database serializer before raw insertion.
20/// Matching watchers are notified after RocksDB accepts the write.
21///
22/// # Panics
23///
24/// Panics if either value cannot be serialized, RocksDB rejects the write, or
25/// an uncorked flush fails.
26#[implement(super::Map)]
27#[inline]
28pub fn put<K, V>(&self, key: K, val: V)
29where
30	K: Serialize + Debug,
31	V: Serialize,
32{
33	let mut key_buf = KeyBuf::new();
34	let mut val_buf = ValBuf::new();
35	self.bput(key, val, (&mut key_buf, &mut val_buf));
36}
37
38/// Stores a serialized key and raw value using an owned key buffer.
39///
40/// The key passes through the database serializer before raw insertion.
41/// Matching watchers are notified after RocksDB accepts the write.
42///
43/// # Panics
44///
45/// Panics if the key cannot be serialized, RocksDB rejects the write, or an
46/// uncorked flush fails.
47#[implement(super::Map)]
48#[inline]
49pub fn put_raw<K, V>(&self, key: K, val: V)
50where
51	K: Serialize + Debug,
52	V: AsRef<[u8]>,
53{
54	let mut key_buf = KeyBuf::new();
55	self.bput_raw(key, val, &mut key_buf);
56}
57
58/// Stores a raw key and serialized value using an owned value buffer.
59///
60/// The value passes through the database serializer before raw insertion.
61/// Matching watchers are notified after RocksDB accepts the write.
62///
63/// # Panics
64///
65/// Panics if the value cannot be serialized, RocksDB rejects the write, or an
66/// uncorked flush fails.
67#[implement(super::Map)]
68#[inline]
69pub fn raw_put<K, V>(&self, key: K, val: V)
70where
71	K: AsRef<[u8]>,
72	V: Serialize,
73{
74	let mut val_buf = ValBuf::new();
75	self.raw_bput(key, val, &mut val_buf);
76}
77
78/// Stores a serialized key and value with fixed-capacity value storage.
79///
80/// The key uses an owned buffer, while `VMAX` bounds the complete encoded value
81/// without a heap fallback. Matching watchers are notified after RocksDB
82/// accepts the write.
83///
84/// # Panics
85///
86/// Panics if serialization fails, the encoded value exceeds `VMAX`, RocksDB
87/// rejects the write, or an uncorked flush fails.
88#[implement(super::Map)]
89#[inline]
90pub fn put_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
91where
92	K: Serialize + Debug,
93	V: Serialize,
94{
95	let mut key_buf = KeyBuf::new();
96	let mut val_buf = ArrayVec::<u8, VMAX>::new();
97	self.bput(key, val, (&mut key_buf, &mut val_buf));
98}
99
100/// Stores a serialized key and value with fixed-capacity key storage.
101///
102/// `KMAX` bounds the complete encoded key without a heap fallback, while the
103/// value uses an owned buffer. Matching watchers are notified after RocksDB
104/// accepts the write.
105///
106/// # Panics
107///
108/// Panics if serialization fails, the encoded key exceeds `KMAX`, RocksDB
109/// rejects the write, or an uncorked flush fails.
110#[implement(super::Map)]
111#[inline]
112pub fn aput_put<const KMAX: usize, K, V>(&self, key: K, val: V)
113where
114	K: Serialize + Debug,
115	V: Serialize,
116{
117	let mut key_buf = ArrayVec::<u8, KMAX>::new();
118	let mut val_buf = ValBuf::new();
119	self.bput(key, val, (&mut key_buf, &mut val_buf));
120}
121
122/// Stores a serialized key and value using fixed-capacity buffers.
123///
124/// `KMAX` and `VMAX` bound the complete encoded key and value without heap
125/// fallbacks. Matching watchers are notified after RocksDB accepts the write.
126///
127/// # Panics
128///
129/// Panics if serialization fails, either encoded value exceeds its capacity,
130/// RocksDB rejects the write, or an uncorked flush fails.
131#[implement(super::Map)]
132#[inline]
133pub fn aput<const KMAX: usize, const VMAX: usize, K, V>(&self, key: K, val: V)
134where
135	K: Serialize + Debug,
136	V: Serialize,
137{
138	let mut key_buf = ArrayVec::<u8, KMAX>::new();
139	let mut val_buf = ArrayVec::<u8, VMAX>::new();
140	self.bput(key, val, (&mut key_buf, &mut val_buf));
141}
142
143/// Stores a serialized key and raw value with fixed-capacity key storage.
144///
145/// `KMAX` bounds the complete encoded key without a heap fallback. Matching
146/// watchers are notified after RocksDB accepts the write.
147///
148/// # Panics
149///
150/// Panics if serialization fails, the encoded key exceeds `KMAX`, RocksDB
151/// rejects the write, or an uncorked flush fails.
152#[implement(super::Map)]
153#[inline]
154pub fn aput_raw<const KMAX: usize, K, V>(&self, key: K, val: V)
155where
156	K: Serialize + Debug,
157	V: AsRef<[u8]>,
158{
159	let mut key_buf = ArrayVec::<u8, KMAX>::new();
160	self.bput_raw(key, val, &mut key_buf);
161}
162
163/// Stores a raw key and serialized value with fixed-capacity value storage.
164///
165/// `VMAX` bounds the complete encoded value without a heap fallback. Matching
166/// watchers are notified after RocksDB accepts the write.
167///
168/// # Panics
169///
170/// Panics if serialization fails, the encoded value exceeds `VMAX`, RocksDB
171/// rejects the write, or an uncorked flush fails.
172#[implement(super::Map)]
173#[inline]
174pub fn raw_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
175where
176	K: AsRef<[u8]>,
177	V: Serialize,
178{
179	let mut val_buf = ArrayVec::<u8, VMAX>::new();
180	self.raw_bput(key, val, &mut val_buf);
181}
182
183/// Stores a serialized key and value using caller-supplied buffers.
184///
185/// Serialization appends the encoded key and value to the tuple's first and
186/// second buffers, respectively. The write uses each buffer's full resulting
187/// contents and then notifies matching watchers after RocksDB accepts it.
188///
189/// # Panics
190///
191/// Panics if either value cannot be serialized, RocksDB rejects the write, or
192/// an uncorked flush fails.
193#[implement(super::Map)]
194pub fn bput<K, V, Bk, Bv>(&self, key: K, val: V, mut buf: (Bk, Bv))
195where
196	K: Serialize + Debug,
197	V: Serialize,
198	Bk: Write + AsRef<[u8]>,
199	Bv: Write + AsRef<[u8]>,
200{
201	let val = ser::serialize(&mut buf.1, val).expect("failed to serialize insertion val");
202	self.bput_raw(key, val, &mut buf.0);
203}
204
205/// Stores a serialized key and raw value using a caller-supplied key buffer.
206///
207/// Serialization appends the encoded key to the supplied buffer, and the write
208/// uses its full resulting contents. Matching watchers are notified after
209/// RocksDB accepts the write.
210///
211/// # Panics
212///
213/// Panics if the key cannot be serialized, RocksDB rejects the write, or an
214/// uncorked flush fails.
215#[implement(super::Map)]
216#[tracing::instrument(skip(self, val, buf), level = "trace")]
217pub fn bput_raw<K, V, Bk>(&self, key: K, val: V, mut buf: Bk)
218where
219	K: Serialize + Debug,
220	V: AsRef<[u8]>,
221	Bk: Write + AsRef<[u8]>,
222{
223	let key = ser::serialize(&mut buf, key).expect("failed to serialize insertion key");
224	self.insert(&key, val);
225}
226
227/// Stores a raw key and serialized value using a caller-supplied value buffer.
228///
229/// Serialization appends the encoded value to the supplied buffer, and the
230/// write uses its full resulting contents. Matching watchers are notified after
231/// RocksDB accepts the write.
232///
233/// # Panics
234///
235/// Panics if the value cannot be serialized, RocksDB rejects the write, or an
236/// uncorked flush fails.
237#[implement(super::Map)]
238pub fn raw_bput<K, V, Bv>(&self, key: K, val: V, mut buf: Bv)
239where
240	K: AsRef<[u8]>,
241	V: Serialize,
242	Bv: Write + AsRef<[u8]>,
243{
244	let val = ser::serialize(&mut buf, val).expect("failed to serialize insertion val");
245	self.insert(&key, val);
246}