-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathinsert.rs
More file actions
91 lines (79 loc) · 2.25 KB
/
Copy pathinsert.rs
File metadata and controls
91 lines (79 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Insert a Key+Value into the database.
//!
//! Overloads are provided for the user to choose the most efficient
//! serialization or bypass for pre=serialized (raw) inputs.
use std::{convert::AsRef, fmt::Debug};
use rocksdb::WriteBatchWithTransaction;
use tuwunel_core::implement;
use crate::util::or_else;
/// Insert Key/Value
///
/// - Key is raw
/// - Val is raw
#[implement(super::Map)]
#[tracing::instrument(skip_all, fields(%self), level = "trace")]
pub fn insert<K, V>(&self, key: &K, val: V)
where
K: AsRef<[u8]> + ?Sized,
V: AsRef<[u8]>,
{
let write_options = &self.write_options;
self.engine
.db
.put_cf_opt(&self.cf(), key, val, write_options)
.or_else(or_else)
.expect("database insert error");
if !self.engine.corked() {
self.engine.flush().expect("database flush error");
}
self.notify(key.as_ref());
}
#[implement(super::Map)]
#[tracing::instrument(skip(self, iter), fields(%self), level = "trace")]
pub fn insert_batch<'a, I, K, V>(&'a self, iter: I)
where
I: Iterator<Item = (K, V)> + Send + Debug,
K: AsRef<[u8]> + Sized + Debug + 'a,
V: AsRef<[u8]> + Sized + 'a,
{
let mut batch = WriteBatchWithTransaction::<false>::default();
for (key, val) in iter {
batch.put_cf(&self.cf(), key.as_ref(), val.as_ref());
}
let write_options = &self.write_options;
self.engine
.db
.write_opt(batch, write_options)
.or_else(or_else)
.expect("database insert batch error");
if !self.engine.corked() {
self.engine.flush().expect("database flush error");
}
}
/// Atomically write a batch of raw put and delete operations.
#[implement(super::Map)]
#[tracing::instrument(skip(self, puts, dels), fields(%self), level = "trace")]
pub fn write_batch_raw<Ip, Ik>(&self, puts: Ip, dels: Ik)
where
Ip: IntoIterator<Item = (Vec<u8>, Vec<u8>)>,
Ik: IntoIterator<Item = Vec<u8>>,
{
let mut batch = WriteBatchWithTransaction::<false>::default();
let cf = self.cf();
for (k, v) in puts {
batch.put_cf(&cf, &k, &v);
}
for k in dels {
batch.delete_cf(&cf, &k);
}
let write_options = &self.write_options;
use crate::util::or_else as db_or_else;
self.engine
.db
.write_opt(batch, write_options)
.or_else(db_or_else)
.expect("database write batch error");
if !self.engine.corked() {
self.engine.flush().expect("database flush error");
}
}