tinycdb_sys/
lib.rs

1#![allow(non_camel_case_types)]
2#![allow(dead_code)]
3
4extern crate libc;
5
6use libc::{c_int, c_uchar, c_uint, c_void};
7
8#[repr(C)]
9pub struct cdb {
10    // File descriptor
11    pub cdb_fd: c_int,
12
13    // Datafile size
14    cdb_fsize: c_uint,
15
16    // End of data ptr
17    cdb_dend: c_uint,
18
19    // mmap'ed file memory
20    cdb_mem: *const c_uchar,
21
22    // Found data
23    cdb_vpos: c_uint,
24    cdb_vlen: c_uint,
25
26    // Found key
27    cdb_kpos: c_uint,
28    cdb_klen: c_uint,
29}
30
31// Macros defined in C
32impl cdb {
33    #[inline]
34    pub fn cdb_datapos(&self) -> c_uint {
35        self.cdb_vpos
36    }
37
38    #[inline]
39    pub fn cdb_datalen(&self) -> c_uint {
40        self.cdb_vlen
41    }
42
43    #[inline]
44    pub fn cdb_keypos(&self) -> c_uint {
45        self.cdb_kpos
46    }
47
48    #[inline]
49    pub fn cdb_keylen(&self) -> c_uint {
50        self.cdb_klen
51    }
52}
53
54#[repr(C)]
55pub struct cdb_find {
56    cdb_cdbp: *mut cdb,
57    cdb_hval: c_uint,
58    cdb_htp: *const c_uchar,
59    cdb_htab: *const c_uchar,
60    cdb_htend: *const c_uchar,
61    cdb_httodo: c_uint,
62    cdb_key: *const c_void,
63    cdb_klen: c_uint,
64}
65
66#[repr(C)]
67pub struct cdb_make {
68    // File descriptor
69    pub cdb_fd: c_int,
70
71    // Data position so far
72    cdb_dpos: c_uint,
73
74    // Record count so far
75    cdb_rcnt: c_uint,
76
77    // Write buffer
78    cdb_buf: [c_uchar; 4096],
79
80    // Current buf position
81    cdb_bpos: *mut c_uchar,
82
83    // List of arrays of record infos
84    // OLD: cdb_rl*
85    cdb_rec: [*mut c_void; 256],
86}
87
88/**
89 * `CdbPutMode` represents the different behaviours that will be used when
90 * inserting a key into a database where the key already exists.
91 */
92#[repr(C)]
93#[derive(Clone, Copy, Debug, PartialEq, Eq)]
94pub enum CdbPutMode {
95    /**
96     * No duplicate checking will be performed.  This is the same as just
97     * calling `CdbCreator.add()`.
98     */
99    Add      = 0,
100
101    /**
102     * If the key already exists in the database, it will be removed prior
103     * to adding the new value.  This can be quite slow if the file is
104     * large, due to having to copy data around.
105     */
106    Replace  = 1,
107
108    /**
109     * Insert the key into the database only if the key does not already
110     * exist.  Note that since a simple query of the database only returns
111     * the first key, this is really only useful to save space in the
112     * database.
113     */
114    Insert   = 2,
115
116    /**
117     * Add the key to the database unconditionally, but also check if it
118     * already existed.
119     *
120     * TODO: what return value does put give?
121     */
122    Warn     = 3,
123
124    /**
125     * If the key already exists in the database, zero it out before adding
126     * this key/value pair.  See the comments on `CdbCreator.remove()` for some
127     * caveats regarding zeroing out keys in the database.
128     */
129    Replace0 = 4,
130}
131
132#[repr(C)]
133pub enum CdbFindMode {
134    Find   = 0,     // == CDB_PUT_ADD
135    Remove = 1,     // == CDB_PUT_REPLACE
136    Fill0  = 4,     // == CDB_PUT_REPLACE0
137}
138
139extern "C" {
140    pub fn cdb_init(cdbp: *mut cdb, fd: c_int) -> c_int;
141    pub fn cdb_free(cdbp: *mut cdb);
142    pub fn cdb_read(cdbp: *const cdb, buf: *mut c_void, len: c_uint, pos: c_uint) -> c_int;
143    pub fn cdb_get(cdbp: *const cdb, len: c_uint, pos: c_uint) -> *const c_void;
144
145    pub fn cdb_find(cdbp: *mut cdb, key: *const c_void, klen: c_uint) -> c_int;
146    pub fn cdb_findinit(cdbfp: *mut cdb_find, cdb: *mut cdb, key: *const c_void, klen: c_uint) -> c_int;
147    pub fn cdb_findnext(cdbfp: *mut cdb_find) -> c_int;
148
149    pub fn cdb_make_start(cdbmp: *mut cdb_make, fd: c_int) -> c_int;
150    pub fn cdb_make_add(cdbmp: *mut cdb_make, key: *const c_void, klen: c_uint, val: *const c_void, vlen: c_uint) -> c_int;
151    pub fn cdb_make_exists(cdbmp: *mut cdb_make, key: *const c_void, klen: c_uint) -> c_int;
152    pub fn cdb_make_find(cdbmp: *mut cdb_make, key: *const c_void, klen: c_uint, mode: CdbFindMode) -> c_int;
153    pub fn cdb_make_put(cdbmp: *mut cdb_make, key: *const c_void, klen: c_uint, val: *const c_void, vlen: c_uint, mode: CdbPutMode) -> c_int;
154    pub fn cdb_make_finish(cdbmp: *mut cdb_make) -> c_int;
155
156    pub fn cdb_seqnext(cptr: *mut c_uint, cdbp: *mut cdb) -> c_int;
157}
158
159// A C macro
160pub unsafe fn cdb_seqinit(cptr: *mut c_uint, _cdbp: *mut cdb) {
161    *cptr = 2048;
162}