cruzbit_leveldb_sys/
lib.rs

1#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
2
3extern crate libc;
4#[macro_use]
5extern crate ffi_opaque;
6use libc::size_t;
7use libc::{c_char, c_int, c_uchar, c_void};
8
9// These are opaque types that LevelDB uses.
10opaque! {
11    /// Opaque handle representing an opened database. The handle is thread-safe.
12    pub struct leveldb_t;
13    pub struct leveldb_cache_t;
14    pub struct leveldb_comparator_t;
15    pub struct leveldb_env_t;
16    pub struct leveldb_filelock_t;
17    pub struct leveldb_filterpolicy_t;
18    /// Opaque handle representing an ongoing iteration process through the database.
19    /// This handle is not thread safe.
20    pub struct leveldb_iterator_t;
21    pub struct leveldb_logger_t;
22    /// Opaque handle representing options used when opening a database. May be discarded after use,
23    /// using `leveldb_free`.
24    pub struct leveldb_options_t;
25    pub struct leveldb_randomfile_t;
26    /// Opaque handle representing options used during a read operation. May be discarded after use,
27    /// using `leveldb_free`.
28    pub struct leveldb_readoptions_t;
29    pub struct leveldb_seqfile_t;
30    pub struct leveldb_snapshot_t;
31    pub struct leveldb_writablefile_t;
32    pub struct leveldb_writebatch_t;
33    /// Opaque handle representing options used during a read operation. May be discarded after use,
34    /// using `leveldb_free`.
35    pub struct leveldb_writeoptions_t;
36}
37
38#[repr(C)]
39#[derive(Copy, Clone)]
40pub enum Compression {
41    No = 0,
42    Snappy = 1,
43}
44
45extern "C" {
46    // DB operations
47
48    /// Open the database at path `name` with the configurations set in `options`.
49    /// In case of success, the return value represents an open database.
50    ///
51    /// If this operation fails,
52    /// - `leveldb_t` is a nullpointer
53    /// - `errptr` contains more information about the error reason
54    pub fn leveldb_open(
55        options: *const leveldb_options_t,
56        name: *const c_char,
57        errptr: *mut *mut c_char,
58    ) -> *mut leveldb_t;
59    /// Close the database represented by a `leveldb_t` handle
60    ///
61    /// Note that this operation cannot fail.
62    pub fn leveldb_close(db: *mut leveldb_t);
63    pub fn leveldb_put(
64        db: *mut leveldb_t,
65        options: *const leveldb_writeoptions_t,
66        key: *const c_char,
67        keylen: size_t,
68        val: *const c_char,
69        vallen: size_t,
70        errptr: *mut *mut c_char,
71    );
72    pub fn leveldb_delete(
73        db: *mut leveldb_t,
74        options: *const leveldb_writeoptions_t,
75        key: *const c_char,
76        keylen: size_t,
77        errptr: *mut *mut c_char,
78    );
79    pub fn leveldb_write(
80        db: *mut leveldb_t,
81        options: *const leveldb_writeoptions_t,
82        batch: *mut leveldb_writebatch_t,
83        errptr: *mut *mut c_char,
84    );
85    pub fn leveldb_get(
86        db: *mut leveldb_t,
87        options: *const leveldb_readoptions_t,
88        key: *const c_char,
89        keylen: size_t,
90        vallen: *mut size_t,
91        errptr: *mut *mut c_char,
92    ) -> *mut c_char;
93    pub fn leveldb_create_iterator(
94        db: *mut leveldb_t,
95        options: *const leveldb_readoptions_t,
96    ) -> *mut leveldb_iterator_t;
97    pub fn leveldb_create_snapshot(db: *mut leveldb_t) -> *mut leveldb_snapshot_t;
98    pub fn leveldb_release_snapshot(db: *mut leveldb_t, snapshot: *const leveldb_snapshot_t);
99    pub fn leveldb_property_value(db: *mut leveldb_t, propname: *const c_char) -> *mut c_char;
100
101    // TODO: const'ness of pointers here is in question
102    pub fn leveldb_approximate_sizes(
103        db: *mut leveldb_t,
104        num_ranges: c_int,
105        range_start_key: *const *const c_char,
106        range_start_key_len: *const size_t,
107        range_limit_key: *const *const c_char,
108        range_limit_key_len: *const size_t,
109        sizes: *mut u64,
110    );
111    pub fn leveldb_compact_range(
112        db: *mut leveldb_t,
113        start_key: *const c_char,
114        start_key_len: size_t,
115        limit_key: *const c_char,
116        limit_key_len: size_t,
117    );
118
119    // Management operations
120    pub fn leveldb_destroy_db(
121        options: *const leveldb_options_t,
122        name: *const c_char,
123        errptr: *mut *mut c_char,
124    );
125    pub fn leveldb_repair_db(
126        options: *const leveldb_options_t,
127        name: *const c_char,
128        errptr: *mut *mut c_char,
129    );
130
131    // Iterator
132    pub fn leveldb_iter_destroy(it: *mut leveldb_iterator_t);
133    pub fn leveldb_iter_valid(it: *const leveldb_iterator_t) -> c_uchar;
134    pub fn leveldb_iter_seek_to_first(it: *mut leveldb_iterator_t);
135    pub fn leveldb_iter_seek_to_last(it: *mut leveldb_iterator_t);
136    pub fn leveldb_iter_seek(it: *mut leveldb_iterator_t, k: *const c_char, klen: size_t);
137    pub fn leveldb_iter_next(it: *mut leveldb_iterator_t);
138    pub fn leveldb_iter_prev(it: *mut leveldb_iterator_t);
139    pub fn leveldb_iter_key(it: *const leveldb_iterator_t, klen: *const size_t) -> *const c_char;
140    pub fn leveldb_iter_value(it: *const leveldb_iterator_t, vlen: *const size_t) -> *const c_char;
141    pub fn leveldb_iter_get_error(it: *const leveldb_iterator_t, errptr: *const *const c_char);
142
143    // Write batch
144    pub fn leveldb_writebatch_create() -> *mut leveldb_writebatch_t;
145    pub fn leveldb_writebatch_destroy(b: *mut leveldb_writebatch_t);
146    pub fn leveldb_writebatch_clear(b: *mut leveldb_writebatch_t);
147    pub fn leveldb_writebatch_put(
148        b: *mut leveldb_writebatch_t,
149        key: *const c_char,
150        keylen: size_t,
151        val: *const c_char,
152        vallen: size_t,
153    );
154    pub fn leveldb_writebatch_delete(
155        b: *mut leveldb_writebatch_t,
156        key: *const c_char,
157        keylen: size_t,
158    );
159    pub fn leveldb_writebatch_iterate(
160        b: *mut leveldb_writebatch_t,
161        state: *mut c_void,
162        put: extern "C" fn(*mut c_void, *const c_char, size_t, *const c_char, size_t),
163        deleted: extern "C" fn(*mut c_void, *const c_char, size_t),
164    );
165
166    // Options
167    /// Create a new `leveldb_options_t` (not the database, but the database *configuration*!)
168    pub fn leveldb_options_create() -> *mut leveldb_options_t;
169    /// Deallocate a `leveldb_options_t` handle (not the database!)
170    pub fn leveldb_options_destroy(o: *mut leveldb_options_t);
171    pub fn leveldb_options_set_comparator(o: *mut leveldb_options_t, c: *mut leveldb_comparator_t);
172    pub fn leveldb_options_set_filter_policy(
173        o: *mut leveldb_options_t,
174        c: *mut leveldb_filterpolicy_t,
175    );
176    /// Modify `o` to specify whether a new database should be created if none exists yet
177    ///
178    /// - If `val` is != 0, new database creation is enabled
179    /// - If `val` is 0,    no new database will be created if none exists yet (default)
180    pub fn leveldb_options_set_create_if_missing(o: *mut leveldb_options_t, val: c_uchar);
181    pub fn leveldb_options_set_error_if_exists(o: *mut leveldb_options_t, val: c_uchar);
182    pub fn leveldb_options_set_paranoid_checks(o: *mut leveldb_options_t, val: c_uchar);
183    pub fn leveldb_options_set_env(o: *mut leveldb_options_t, env: *mut leveldb_env_t);
184    pub fn leveldb_options_set_info_log(o: *mut leveldb_options_t, logger: *mut leveldb_logger_t);
185    pub fn leveldb_options_set_write_buffer_size(o: *mut leveldb_options_t, size: size_t);
186    pub fn leveldb_options_set_max_open_files(o: *mut leveldb_options_t, num: c_int);
187    pub fn leveldb_options_set_cache(o: *mut leveldb_options_t, cache: *mut leveldb_cache_t);
188    pub fn leveldb_options_set_block_size(o: *mut leveldb_options_t, size: size_t);
189    pub fn leveldb_options_set_block_restart_interval(o: *mut leveldb_options_t, interval: c_int);
190    pub fn leveldb_options_set_compression(o: *mut leveldb_options_t, val: Compression);
191
192    // Comparator
193    pub fn leveldb_comparator_create(
194        state: *mut c_void,
195        destructor: extern "C" fn(*mut c_void),
196        compare: extern "C" fn(*mut c_void, *const c_char, size_t, *const c_char, size_t) -> c_int,
197        name: extern "C" fn(*mut c_void) -> *const c_char,
198    ) -> *mut leveldb_comparator_t;
199    pub fn leveldb_comparator_destroy(c: *mut leveldb_comparator_t);
200
201    // Filter policy
202    //pub leveldb_filterpolicy_create(state: *mut c_void, /* TODO */) -> *mut leveldb_filterpolicy_t;
203    /*
204        extern leveldb_filterpolicy_t* leveldb_filterpolicy_create(
205            void* state,
206            void (*destructor)(void*),
207            char* (*create_filter)(
208                void*,
209                const char* const* key_array, const size_t* key_length_array,
210                int num_keys,
211                size_t* filter_length),
212            unsigned char (*key_may_match)(
213                void*,
214                const char* key, size_t length,
215                const char* filter, size_t filter_length),
216            const char* (*name)(void*));
217    */
218    pub fn leveldb_filterpolicy_destroy(p: *mut leveldb_filterpolicy_t);
219    pub fn leveldb_filterpolicy_create_bloom(bits_per_key: c_int) -> *mut leveldb_filterpolicy_t;
220
221    // Read options
222    pub fn leveldb_readoptions_create() -> *mut leveldb_readoptions_t;
223    pub fn leveldb_readoptions_destroy(o: *mut leveldb_readoptions_t);
224    pub fn leveldb_readoptions_set_verify_checksums(o: *mut leveldb_readoptions_t, val: c_uchar);
225    pub fn leveldb_readoptions_set_fill_cache(o: *mut leveldb_readoptions_t, val: c_uchar);
226    pub fn leveldb_readoptions_set_snapshot(
227        o: *mut leveldb_readoptions_t,
228        snapshot: *const leveldb_snapshot_t,
229    );
230
231    // Write options
232    pub fn leveldb_writeoptions_create() -> *mut leveldb_writeoptions_t;
233    pub fn leveldb_writeoptions_destroy(o: *mut leveldb_writeoptions_t);
234    pub fn leveldb_writeoptions_set_sync(o: *mut leveldb_writeoptions_t, val: c_uchar);
235
236    // Cache
237    pub fn leveldb_cache_create_lru(capacity: size_t) -> *mut leveldb_cache_t;
238    pub fn leveldb_cache_destroy(c: *mut leveldb_cache_t);
239
240    // Env
241    pub fn leveldb_create_default_env() -> *mut leveldb_env_t;
242    pub fn leveldb_env_destroy(e: *mut leveldb_env_t);
243
244    // Utility
245    pub fn leveldb_free(ptr: *mut c_void);
246
247    // Versioning
248    pub fn leveldb_major_version() -> c_int;
249    pub fn leveldb_minor_version() -> c_int;
250}