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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use crocksdb_ffi::{self, DBValueType, DBWriteBatch, DBWriteBatchIterator};
use libc::{c_void, size_t};
use std::marker::PhantomData;
use std::slice;

pub struct WriteBatch {
    pub(crate) inner: *mut DBWriteBatch,
}

unsafe impl Send for WriteBatch {}
impl Default for WriteBatch {
    fn default() -> WriteBatch {
        WriteBatch {
            inner: unsafe { crocksdb_ffi::crocksdb_writebatch_create() },
        }
    }
}

pub struct WriteBatchCallback<'a> {
    cb_ptr: *mut c_void,
    cfs: &'a [&'a str],
}

impl<'a> WriteBatchCallback<'a> {
    fn invoke(&mut self, cf_id: u32, value_type: DBValueType, key: &[u8], value: Option<&[u8]>) {
        let cf = self.cfs[cf_id as usize];
        unsafe {
            let cb: &mut &mut dyn FnMut(&str, DBValueType, &[u8], Option<&[u8]>) =
                &mut *(self.cb_ptr as *mut _);
            cb(cf, value_type, key, value);
        }
    }
}

impl WriteBatch {
    pub fn new() -> WriteBatch {
        WriteBatch::default()
    }

    pub fn with_capacity(cap: usize) -> WriteBatch {
        WriteBatch {
            inner: unsafe { crocksdb_ffi::crocksdb_writebatch_create_with_capacity(cap) },
        }
    }

    pub fn count(&self) -> usize {
        unsafe { crocksdb_ffi::crocksdb_writebatch_count(self.inner) as usize }
    }

    pub fn is_empty(&self) -> bool {
        self.count() == 0
    }

    pub fn data_size(&self) -> usize {
        unsafe {
            let mut data_size: usize = 0;
            let _ = crocksdb_ffi::crocksdb_writebatch_data(self.inner, &mut data_size);
            return data_size;
        }
    }

    pub fn clear(&self) {
        unsafe {
            crocksdb_ffi::crocksdb_writebatch_clear(self.inner);
        }
    }

    pub fn set_save_point(&mut self) {
        unsafe {
            crocksdb_ffi::crocksdb_writebatch_set_save_point(self.inner);
        }
    }

    pub fn rollback_to_save_point(&mut self) -> Result<(), String> {
        unsafe {
            ffi_try!(crocksdb_writebatch_rollback_to_save_point(self.inner));
        }
        Ok(())
    }

    pub fn pop_save_point(&mut self) -> Result<(), String> {
        unsafe {
            ffi_try!(crocksdb_writebatch_pop_save_point(self.inner));
        }
        Ok(())
    }

    pub fn data(&self) -> &[u8] {
        let mut val_len: size_t = 0;
        let val_len_ptr: *mut size_t = &mut val_len;
        unsafe {
            let val_ptr = crocksdb_ffi::crocksdb_writebatch_data(self.inner, val_len_ptr);
            slice::from_raw_parts(val_ptr, val_len as usize)
        }
    }

    pub fn append(&mut self, src: &[u8]) {
        unsafe {
            crocksdb_ffi::crocksdb_writebatch_append_content(
                self.inner,
                src.as_ptr(),
                src.len() as size_t,
            );
        }
    }

    pub fn iterate<F>(&self, cfs: &[&str], mut iterator_fn: F)
    where
        F: FnMut(&str, DBValueType, &[u8], Option<&[u8]>),
    {
        unsafe {
            let mut cb: &mut dyn FnMut(&str, DBValueType, &[u8], Option<&[u8]>) = &mut iterator_fn;
            let cb_ptr = &mut cb;
            let cb_proxy = Box::new(WriteBatchCallback {
                cfs,
                cb_ptr: cb_ptr as *mut _ as *mut c_void,
            });
            let state = Box::into_raw(cb_proxy) as *mut c_void;
            crocksdb_ffi::crocksdb_writebatch_iterate_cf(
                self.inner,
                state,
                put_fn,
                put_cf_fn,
                delete_fn,
                delete_cf_fn,
            );
        }
    }

    pub fn iter(&self) -> WriteBatchIter {
        WriteBatchIter::new(self)
    }
}

pub struct WriteBatchIter<'a> {
    props: PhantomData<&'a DBWriteBatchIterator>,
    inner: *mut DBWriteBatchIterator,
}

impl<'a> Drop for WriteBatchIter<'a> {
    fn drop(&mut self) {
        unsafe {
            crocksdb_ffi::crocksdb_writebatch_iterator_destroy(self.inner);
        }
    }
}

impl<'a> WriteBatchIter<'a> {
    fn new(wb: &'a WriteBatch) -> WriteBatchIter<'a> {
        unsafe {
            WriteBatchIter {
                props: PhantomData,
                inner: crocksdb_ffi::crocksdb_writebatch_iterator_create(wb.inner),
            }
        }
    }

    fn from_bytes(data: &'a [u8]) -> WriteBatchIter<'a> {
        unsafe {
            WriteBatchIter {
                props: PhantomData,
                inner: crocksdb_ffi::crocksdb_writebatch_ref_iterator_create(
                    data.as_ptr(),
                    data.len() as size_t,
                ),
            }
        }
    }
}

impl<'a> Iterator for WriteBatchIter<'a> {
    type Item = (DBValueType, u32, &'a [u8], &'a [u8]);

    fn next(&mut self) -> Option<(DBValueType, u32, &'a [u8], &'a [u8])> {
        unsafe {
            if !crocksdb_ffi::crocksdb_writebatch_iterator_valid(self.inner) {
                return None;
            }
            let mut klen: size_t = 0;
            let k = crocksdb_ffi::crocksdb_writebatch_iterator_key(self.inner, &mut klen);
            let key = slice::from_raw_parts(k, klen);

            let mut vlen: size_t = 0;
            let v = crocksdb_ffi::crocksdb_writebatch_iterator_value(self.inner, &mut vlen);
            let val = slice::from_raw_parts(v, vlen);

            let value_type = match crocksdb_ffi::crocksdb_writebatch_iterator_value_type(self.inner)
            {
                DBValueType::TypeColumnFamilyDeletion => DBValueType::TypeDeletion,
                DBValueType::TypeColumnFamilyValue => DBValueType::TypeValue,
                DBValueType::TypeColumnFamilyMerge => DBValueType::TypeMerge,
                DBValueType::TypeColumnFamilyRangeDeletion => DBValueType::TypeRangeDeletion,
                other => other,
            };
            let column_family =
                crocksdb_ffi::crocksdb_writebatch_iterator_column_family_id(self.inner);

            crocksdb_ffi::crocksdb_writebatch_iterator_next(self.inner);

            Some((value_type, column_family, key, val))
        }
    }
}

pub struct WriteBatchRef<'a> {
    data: &'a [u8],
}

impl<'a> WriteBatchRef<'a> {
    pub fn new(data: &'a [u8]) -> WriteBatchRef<'a> {
        WriteBatchRef { data }
    }

    pub fn count(&self) -> usize {
        unsafe {
            crocksdb_ffi::crocksdb_writebatch_ref_count(
                self.data.as_ptr(),
                self.data.len() as size_t,
            ) as usize
        }
    }

    pub fn iter(&self) -> WriteBatchIter<'a> {
        WriteBatchIter::from_bytes(self.data)
    }
}

pub unsafe extern "C" fn put_fn(
    state: *mut c_void,
    k: *const u8,
    klen: size_t,
    v: *const u8,
    vlen: size_t,
) {
    let proxy: &mut WriteBatchCallback = &mut *(state as *mut WriteBatchCallback);
    let a: &[u8] = slice::from_raw_parts(k as *const u8, klen as usize);
    let b: &[u8] = slice::from_raw_parts(v as *const u8, vlen as usize);
    proxy.invoke(0, DBValueType::TypeValue, a, Some(b));
}

pub unsafe extern "C" fn put_cf_fn(
    state: *mut c_void,
    cf_id: u32,
    k: *const u8,
    klen: size_t,
    v: *const u8,
    vlen: size_t,
) {
    let proxy: &mut WriteBatchCallback = &mut *(state as *mut WriteBatchCallback);
    let a: &[u8] = slice::from_raw_parts(k as *const u8, klen as usize);
    let b: &[u8] = slice::from_raw_parts(v as *const u8, vlen as usize);
    proxy.invoke(cf_id, DBValueType::TypeValue, a, Some(b));
}

pub unsafe extern "C" fn delete_fn(state: *mut c_void, k: *const u8, klen: size_t) {
    let proxy: &mut WriteBatchCallback = &mut *(state as *mut WriteBatchCallback);
    let k: &[u8] = slice::from_raw_parts(k as *const u8, klen as usize);
    proxy.invoke(0, DBValueType::TypeDeletion, k, None);
}

pub unsafe extern "C" fn delete_cf_fn(state: *mut c_void, cf_id: u32, k: *const u8, klen: size_t) {
    let proxy: &mut WriteBatchCallback = &mut *(state as *mut WriteBatchCallback);
    let k: &[u8] = slice::from_raw_parts(k as *const u8, klen as usize);
    proxy.invoke(cf_id, DBValueType::TypeDeletion, k, None);
}

impl Drop for WriteBatch {
    fn drop(&mut self) {
        unsafe { crocksdb_ffi::crocksdb_writebatch_destroy(self.inner) }
    }
}