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::{c_char, c_int, c_uchar, c_void};
7use libc::size_t;
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(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char) -> *mut leveldb_t;
55    /// Close the database represented by a `leveldb_t` handle
56    ///
57    /// Note that this operation cannot fail.
58    pub fn leveldb_close(db: *mut leveldb_t);
59    pub fn leveldb_put(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, key: *const c_char, keylen: size_t, val: *const c_char, vallen: size_t, errptr: *mut *mut c_char);
60    pub fn leveldb_delete(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, key: *const c_char, keylen: size_t, errptr: *mut *mut c_char);
61    pub fn leveldb_write(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, batch: *mut leveldb_writebatch_t, errptr: *mut *mut c_char);
62    pub fn leveldb_get(db: *mut leveldb_t, options: *const leveldb_readoptions_t, key: *const c_char, keylen: size_t, vallen: *mut size_t, errptr: *mut *mut c_char) -> *mut c_char;
63    pub fn leveldb_create_iterator(db: *mut leveldb_t, options: *const leveldb_readoptions_t) -> *mut leveldb_iterator_t;
64    pub fn leveldb_create_snapshot(db: *mut leveldb_t) -> *mut leveldb_snapshot_t;
65    pub fn leveldb_release_snapshot(db: *mut leveldb_t, snapshot: *const leveldb_snapshot_t);
66    pub fn leveldb_property_value(db: *mut leveldb_t, propname: *const c_char) -> *mut c_char;
67
68    // TODO: const'ness of pointers here is in question
69    pub fn leveldb_approximate_sizes(db: *mut leveldb_t, num_ranges: c_int, range_start_key: *const *const c_char, range_start_key_len: *const size_t, range_limit_key: *const *const c_char, range_limit_key_len: *const size_t, sizes: *mut u64);
70    pub fn leveldb_compact_range(db: *mut leveldb_t, start_key: *const c_char, start_key_len: size_t, limit_key: *const c_char, limit_key_len: size_t);
71
72    // Management operations
73    pub fn leveldb_destroy_db(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char);
74    pub fn leveldb_repair_db(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char);
75
76    // Iterator
77    pub fn leveldb_iter_destroy(it: *mut leveldb_iterator_t);
78    pub fn leveldb_iter_valid(it: *const leveldb_iterator_t) -> c_uchar;
79    pub fn leveldb_iter_seek_to_first(it: *mut leveldb_iterator_t);
80    pub fn leveldb_iter_seek_to_last(it: *mut leveldb_iterator_t);
81    pub fn leveldb_iter_seek(it: *mut leveldb_iterator_t, k: *const c_char, klen: size_t);
82    pub fn leveldb_iter_next(it: *mut leveldb_iterator_t);
83    pub fn leveldb_iter_prev(it: *mut leveldb_iterator_t);
84    pub fn leveldb_iter_key(it: *const leveldb_iterator_t, klen: *const size_t) -> *const c_char;
85    pub fn leveldb_iter_value(it: *const leveldb_iterator_t, vlen: *const size_t) -> *const c_char;
86    pub fn leveldb_iter_get_error(it: *const leveldb_iterator_t, errptr: *const *const c_char);
87
88    // Write batch
89    pub fn leveldb_writebatch_create() -> *mut leveldb_writebatch_t;
90    pub fn leveldb_writebatch_destroy(b: *mut leveldb_writebatch_t);
91    pub fn leveldb_writebatch_clear(b: *mut leveldb_writebatch_t);
92    pub fn leveldb_writebatch_put(b: *mut leveldb_writebatch_t, key: *const c_char, keylen: size_t, val: *const c_char, vallen: size_t);
93    pub fn leveldb_writebatch_delete(b: *mut leveldb_writebatch_t, key: *const c_char, keylen: size_t);
94    pub fn leveldb_writebatch_iterate(
95        b: *mut leveldb_writebatch_t,
96        state: *mut c_void,
97        put: extern fn(*mut c_void, *const c_char, size_t, *const c_char, size_t),
98        deleted: extern fn(*mut c_void, *const c_char, size_t)
99    );
100
101    // Options
102    /// Create a new `leveldb_options_t` (not the database, but the database *configuration*!)
103    pub fn leveldb_options_create() -> *mut leveldb_options_t;
104    /// Deallocate a `leveldb_options_t` handle (not the database!)
105    pub fn leveldb_options_destroy(o: *mut leveldb_options_t);
106    pub fn leveldb_options_set_comparator(o: *mut leveldb_options_t, c: *mut leveldb_comparator_t);
107    pub fn leveldb_options_set_filter_policy(o: *mut leveldb_options_t, c: *mut leveldb_filterpolicy_t);
108    /// Modify `o` to specify whether a new database should be created if none exists yet
109    ///
110    /// - If `val` is != 0, new database creation is enabled
111    /// - If `val` is 0,    no new database will be created if none exists yet (default)
112    pub fn leveldb_options_set_create_if_missing(o: *mut leveldb_options_t, val: c_uchar);
113    pub fn leveldb_options_set_error_if_exists(o: *mut leveldb_options_t, val: c_uchar);
114    pub fn leveldb_options_set_paranoid_checks(o: *mut leveldb_options_t, val: c_uchar);
115    pub fn leveldb_options_set_env(o: *mut leveldb_options_t, env: *mut leveldb_env_t);
116    pub fn leveldb_options_set_info_log(o: *mut leveldb_options_t, logger: *mut leveldb_logger_t);
117    pub fn leveldb_options_set_write_buffer_size(o: *mut leveldb_options_t, size: size_t);
118    pub fn leveldb_options_set_max_open_files(o: *mut leveldb_options_t, num: c_int);
119    pub fn leveldb_options_set_cache(o: *mut leveldb_options_t, cache: *mut leveldb_cache_t);
120    pub fn leveldb_options_set_block_size(o: *mut leveldb_options_t, size: size_t);
121    pub fn leveldb_options_set_block_restart_interval(o: *mut leveldb_options_t, interval: c_int);
122    pub fn leveldb_options_set_compression(o: *mut leveldb_options_t, val: Compression);
123
124    // Comparator
125    pub fn leveldb_comparator_create(
126        state: *mut c_void,
127        destructor: extern fn(*mut c_void),
128        compare: extern fn(*mut c_void, *const c_char, size_t, *const c_char, size_t) -> c_int,
129        name: extern fn(*mut c_void) -> *const c_char
130    ) -> *mut leveldb_comparator_t;
131    pub fn leveldb_comparator_destroy(c: *mut leveldb_comparator_t);
132
133    // Filter policy
134    //pub leveldb_filterpolicy_create(state: *mut c_void, /* TODO */) -> *mut leveldb_filterpolicy_t;
135    /*
136        extern leveldb_filterpolicy_t* leveldb_filterpolicy_create(
137            void* state,
138            void (*destructor)(void*),
139            char* (*create_filter)(
140                void*,
141                const char* const* key_array, const size_t* key_length_array,
142                int num_keys,
143                size_t* filter_length),
144            unsigned char (*key_may_match)(
145                void*,
146                const char* key, size_t length,
147                const char* filter, size_t filter_length),
148            const char* (*name)(void*));
149    */
150    pub fn leveldb_filterpolicy_destroy(p: *mut leveldb_filterpolicy_t);
151    pub fn leveldb_filterpolicy_create_bloom(bits_per_key: c_int) -> *mut leveldb_filterpolicy_t;
152
153    // Read options
154    pub fn leveldb_readoptions_create() -> *mut leveldb_readoptions_t;
155    pub fn leveldb_readoptions_destroy(o: *mut leveldb_readoptions_t);
156    pub fn leveldb_readoptions_set_verify_checksums(o: *mut leveldb_readoptions_t, val: c_uchar);
157    pub fn leveldb_readoptions_set_fill_cache(o: *mut leveldb_readoptions_t, val: c_uchar);
158    pub fn leveldb_readoptions_set_snapshot(o: *mut leveldb_readoptions_t, snapshot: *const leveldb_snapshot_t);
159
160    // Write options
161    pub fn leveldb_writeoptions_create() -> *mut leveldb_writeoptions_t;
162    pub fn leveldb_writeoptions_destroy(o: *mut leveldb_writeoptions_t);
163    pub fn leveldb_writeoptions_set_sync(o: *mut leveldb_writeoptions_t, val: c_uchar);
164
165    // Cache
166    pub fn leveldb_cache_create_lru(capacity: size_t) -> *mut leveldb_cache_t;
167    pub fn leveldb_cache_destroy(c: *mut leveldb_cache_t);
168
169    // Env
170    pub fn leveldb_create_default_env() -> *mut leveldb_env_t;
171    pub fn leveldb_env_destroy(e: *mut leveldb_env_t);
172
173    // Utility
174    pub fn leveldb_free(ptr: *mut c_void);
175
176    // Versioning
177    pub fn leveldb_major_version() -> c_int;
178    pub fn leveldb_minor_version() -> c_int;
179}