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
use crate::ffi;

use crate::{handle::Handle, Error, WriteBatch, WriteOptions};

pub trait WriteOps {
    fn write_full(&self, batch: &WriteBatch, writeopts: Option<&WriteOptions>)
        -> Result<(), Error>;

    fn write(&self, batch: &WriteBatch) -> Result<(), Error> {
        self.write_full(batch, None)
    }

    fn write_opt(&self, batch: &WriteBatch, writeopts: &WriteOptions) -> Result<(), Error> {
        self.write_full(batch, Some(writeopts))
    }

    fn write_without_wal(&self, batch: &WriteBatch) -> Result<(), Error> {
        let mut wo = WriteOptions::new();
        wo.disable_wal(true);
        self.write_opt(batch, &wo)
    }
}

impl<T> WriteOps for T
where
    T: Handle<ffi::rocksdb_t> + super::Write,
{
    fn write_full(
        &self,
        batch: &WriteBatch,
        writeopts: Option<&WriteOptions>,
    ) -> Result<(), Error> {
        let mut default_writeopts = None;

        let wo_handle = WriteOptions::input_or_default(writeopts, &mut default_writeopts)?;

        unsafe {
            ffi_try!(ffi::rocksdb_write(self.handle(), wo_handle, batch.handle(),));
            Ok(())
        }
    }
}