ckb_rocksdb/ops/
put.rs

1// Copyright 2019 Tyler Neely
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use crate::ffi;
17use libc::{c_char, size_t};
18
19use crate::{handle::Handle, ColumnFamily, Error, WriteOptions};
20
21pub trait Put<W> {
22    fn put_full<K, V>(&self, key: K, value: V, writeopts: Option<&W>) -> Result<(), Error>
23    where
24        K: AsRef<[u8]>,
25        V: AsRef<[u8]>;
26
27    /// Insert a value into the database under the given key.
28    fn put<K, V>(&self, key: K, value: V) -> Result<(), Error>
29    where
30        K: AsRef<[u8]>,
31        V: AsRef<[u8]>,
32    {
33        self.put_full(key, value, None)
34    }
35
36    fn put_opt<K, V>(&self, key: K, value: V, writeopts: &W) -> Result<(), Error>
37    where
38        K: AsRef<[u8]>,
39        V: AsRef<[u8]>,
40    {
41        self.put_full(key, value, Some(writeopts))
42    }
43}
44
45pub trait PutCF<W> {
46    fn put_cf_full<K, V>(
47        &self,
48        cf: Option<&ColumnFamily>,
49        key: K,
50        value: V,
51        writeopts: Option<&W>,
52    ) -> Result<(), Error>
53    where
54        K: AsRef<[u8]>,
55        V: AsRef<[u8]>;
56
57    fn put_cf<K, V>(&self, cf: &ColumnFamily, key: K, value: V) -> Result<(), Error>
58    where
59        K: AsRef<[u8]>,
60        V: AsRef<[u8]>,
61    {
62        self.put_cf_full(Some(cf), key, value, None)
63    }
64
65    fn put_cf_opt<K, V>(
66        &self,
67        cf: &ColumnFamily,
68        key: K,
69        value: V,
70        writeopts: &W,
71    ) -> Result<(), Error>
72    where
73        K: AsRef<[u8]>,
74        V: AsRef<[u8]>,
75    {
76        self.put_cf_full(Some(cf), key, value, Some(writeopts))
77    }
78}
79
80impl<T, W> Put<W> for T
81where
82    T: PutCF<W>,
83{
84    fn put_full<K: AsRef<[u8]>, V: AsRef<[u8]>>(
85        &self,
86        key: K,
87        value: V,
88        writeopts: Option<&W>,
89    ) -> Result<(), Error> {
90        self.put_cf_full(None, key, value, writeopts)
91    }
92}
93
94impl<T> PutCF<WriteOptions> for T
95where
96    T: Handle<ffi::rocksdb_t> + super::Write,
97{
98    fn put_cf_full<K, V>(
99        &self,
100        cf: Option<&ColumnFamily>,
101        key: K,
102        value: V,
103        writeopts: Option<&WriteOptions>,
104    ) -> Result<(), Error>
105    where
106        K: AsRef<[u8]>,
107        V: AsRef<[u8]>,
108    {
109        let mut default_writeopts = None;
110
111        let wo_handle = WriteOptions::input_or_default(writeopts, &mut default_writeopts)?;
112
113        let key = key.as_ref();
114        let value = value.as_ref();
115        let key_ptr = key.as_ptr() as *const c_char;
116        let key_len = key.len() as size_t;
117        let val_ptr = value.as_ptr() as *const c_char;
118        let val_len = value.len() as size_t;
119
120        unsafe {
121            match cf {
122                Some(cf) => ffi_try!(ffi::rocksdb_put_cf(
123                    self.handle(),
124                    wo_handle,
125                    cf.handle(),
126                    key_ptr,
127                    key_len,
128                    val_ptr,
129                    val_len,
130                )),
131                None => ffi_try!(ffi::rocksdb_put(
132                    self.handle(),
133                    wo_handle,
134                    key_ptr,
135                    key_len,
136                    val_ptr,
137                    val_len,
138                )),
139            }
140
141            Ok(())
142        }
143    }
144}