mirror of
https://github.com/girlbossceo/conduwuit.git
synced 2024-11-25 22:48:34 +00:00
add serialized insert interface
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
2f24d7117a
commit
d3d11356ee
|
@ -1,21 +1,194 @@
|
|||
use std::{convert::AsRef, fmt::Debug};
|
||||
//! 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, io::Write};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use conduit::implement;
|
||||
use rocksdb::WriteBatchWithTransaction;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::util::or_else;
|
||||
use crate::{ser, util::or_else};
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
#[tracing::instrument(skip(self, value), fields(%self), level = "trace")]
|
||||
pub fn insert<K, V>(&self, key: &K, value: &V)
|
||||
pub fn put<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<[u8]> + ?Sized + Debug,
|
||||
V: AsRef<[u8]> + ?Sized,
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
let mut val_buf = Vec::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized
|
||||
/// - Val is raw
|
||||
#[implement(super::Map)]
|
||||
pub fn put_raw<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: AsRef<[u8]>,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
self.bput_raw(key, val, &mut key_buf);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is raw
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
pub fn raw_put<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut val_buf = Vec::new();
|
||||
self.raw_bput(key, val, &mut val_buf);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
pub fn put_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
let mut val_buf = ArrayVec::<u8, VMAX>::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
pub fn aput_put<const KMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = ArrayVec::<u8, KMAX>::new();
|
||||
let mut val_buf = Vec::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
pub fn aput<const KMAX: usize, const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = ArrayVec::<u8, KMAX>::new();
|
||||
let mut val_buf = ArrayVec::<u8, VMAX>::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is raw
|
||||
#[implement(super::Map)]
|
||||
pub fn aput_raw<const KMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: AsRef<[u8]>,
|
||||
{
|
||||
let mut key_buf = ArrayVec::<u8, KMAX>::new();
|
||||
self.bput_raw(key, val, &mut key_buf);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is raw
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
pub fn raw_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut val_buf = ArrayVec::<u8, VMAX>::new();
|
||||
self.raw_bput(key, val, &mut val_buf);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized to supplied buffer
|
||||
/// - Val is serialized to supplied buffer
|
||||
#[implement(super::Map)]
|
||||
pub fn bput<K, V, Bk, Bv>(&self, key: K, val: V, mut buf: (Bk, Bv))
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
Bk: Write + AsRef<[u8]>,
|
||||
Bv: Write + AsRef<[u8]>,
|
||||
{
|
||||
let val = ser::serialize(&mut buf.1, val).expect("failed to serialize insertion val");
|
||||
self.bput_raw(key, val, &mut buf.0);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized to supplied buffer
|
||||
/// - Val is raw
|
||||
#[implement(super::Map)]
|
||||
pub fn bput_raw<K, V, Bk>(&self, key: K, val: V, mut buf: Bk)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: AsRef<[u8]>,
|
||||
Bk: Write + AsRef<[u8]>,
|
||||
{
|
||||
let key = ser::serialize(&mut buf, key).expect("failed to serialize insertion key");
|
||||
self.insert(&key, val);
|
||||
}
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is raw
|
||||
/// - Val is serialized to supplied buffer
|
||||
#[implement(super::Map)]
|
||||
pub fn raw_bput<K, V, Bv>(&self, key: K, val: V, mut buf: Bv)
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
V: Serialize,
|
||||
Bv: Write + AsRef<[u8]>,
|
||||
{
|
||||
let val = ser::serialize(&mut buf, val).expect("failed to serialize insertion val");
|
||||
self.insert(&key, val);
|
||||
}
|
||||
|
||||
/// 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.db
|
||||
.db
|
||||
.put_cf_opt(&self.cf(), key, value, write_options)
|
||||
.put_cf_opt(&self.cf(), key, val, write_options)
|
||||
.or_else(or_else)
|
||||
.expect("database insert error");
|
||||
|
||||
|
|
|
@ -7,18 +7,18 @@ use serde::Serialize;
|
|||
use crate::{ser, util::or_else};
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn del<K>(&self, key: &K)
|
||||
pub fn del<K>(&self, key: K)
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
K: Serialize + Debug,
|
||||
{
|
||||
let mut buf = Vec::<u8>::with_capacity(64);
|
||||
self.bdel(key, &mut buf);
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn adel<const MAX: usize, K>(&self, key: &K)
|
||||
pub fn adel<const MAX: usize, K>(&self, key: K)
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
K: Serialize + Debug,
|
||||
{
|
||||
let mut buf = ArrayVec::<u8, MAX>::new();
|
||||
self.bdel(key, &mut buf);
|
||||
|
@ -26,9 +26,9 @@ where
|
|||
|
||||
#[implement(super::Map)]
|
||||
#[tracing::instrument(skip(self, buf), fields(%self), level = "trace")]
|
||||
pub fn bdel<K, B>(&self, key: &K, buf: &mut B)
|
||||
pub fn bdel<K, B>(&self, key: K, buf: &mut B)
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
K: Serialize + Debug,
|
||||
B: Write + AsRef<[u8]>,
|
||||
{
|
||||
let key = ser::serialize(buf, key).expect("failed to serialize deletion key");
|
||||
|
|
Loading…
Reference in a new issue