leveldb/database/
options.rs

1use cruzbit_leveldb_sys::*;
2
3use libc::size_t;
4
5use super::cache::Cache;
6
7/// Options to consider when opening a new or pre-existing database.
8///
9/// Note that in contrast to the leveldb C API, the Comparator is not
10/// passed using this structure.
11///
12/// For more detailed explanations, consider the
13/// [leveldb documentation](https://github.com/google/leveldb/tree/master/doc)
14pub struct Options {
15    /// create the database if missing
16    ///
17    /// default: false
18    pub create_if_missing: bool,
19    /// report an error if the DB already exists instead of opening.
20    ///
21    /// default: false
22    pub error_if_exists: bool,
23    /// paranoid checks make the database report an error as soon as
24    /// corruption is detected.
25    ///
26    /// default: false
27    pub paranoid_checks: bool,
28    /// Override the size of the write buffer to use.
29    ///
30    /// default: None
31    pub write_buffer_size: Option<size_t>,
32    /// Override the max number of open files.
33    ///
34    /// default: None
35    pub max_open_files: Option<i32>,
36    /// Override the size of the blocks leveldb uses for writing and caching.
37    ///
38    /// default: None
39    pub block_size: Option<size_t>,
40    /// Override the interval between restart points.
41    ///
42    /// default: None
43    pub block_restart_interval: Option<i32>,
44    /// Define whether leveldb should write compressed or not.
45    ///
46    /// default: Compression::No
47    pub compression: Compression,
48    /// A cache to use during read operations.
49    ///
50    /// default: None
51    pub cache: Option<Cache>,
52}
53
54impl std::fmt::Debug for Options {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        f.debug_tuple("")
57            .field(&self.create_if_missing)
58            .field(&self.error_if_exists)
59            .field(&self.paranoid_checks)
60            .field(&self.write_buffer_size)
61            .field(&self.max_open_files)
62            .field(&self.block_size)
63            .field(&self.block_restart_interval)
64            .finish()
65    }
66}
67
68impl Default for Options {
69    fn default() -> Self {
70        Self::new()
71    }
72}
73
74impl Options {
75    /// Create a new `Options` struct with default settings.
76    pub fn new() -> Options {
77        Options {
78            create_if_missing: false,
79            error_if_exists: false,
80            paranoid_checks: false,
81            write_buffer_size: None,
82            max_open_files: None,
83            block_size: None,
84            block_restart_interval: None,
85            compression: Compression::No,
86            cache: None,
87        }
88    }
89}
90
91/// The write options to use for a write operation.
92#[derive(Copy, Clone, Debug)]
93pub struct WriteOptions {
94    /// `fsync` before acknowledging a write operation.
95    ///
96    /// default: false
97    pub sync: bool,
98}
99
100impl Default for WriteOptions {
101    fn default() -> Self {
102        Self::new()
103    }
104}
105
106impl WriteOptions {
107    /// Return a new `WriteOptions` struct with default settings.
108    pub fn new() -> WriteOptions {
109        WriteOptions { sync: false }
110    }
111}
112
113/// The read options to use for any read operation.
114#[derive(Copy, Clone, Debug)]
115pub struct ReadOptions {
116    /// Whether to verify the saved checksums on read.
117    ///
118    /// default: false
119    pub verify_checksums: bool,
120    /// Whether to fill the internal cache with the
121    /// results of the read.
122    ///
123    /// default: true
124    pub fill_cache: bool,
125}
126
127impl Default for ReadOptions {
128    fn default() -> Self {
129        Self::new()
130    }
131}
132
133impl ReadOptions {
134    /// Return a `ReadOptions` struct with the default values.
135    pub fn new() -> ReadOptions {
136        ReadOptions {
137            verify_checksums: false,
138            fill_cache: true,
139        }
140    }
141}
142
143#[allow(missing_docs)]
144/// # Safety
145pub unsafe fn c_options(
146    options: &Options,
147    comparator: Option<*mut leveldb_comparator_t>,
148) -> *mut leveldb_options_t {
149    let c_options = leveldb_options_create();
150    leveldb_options_set_create_if_missing(c_options, options.create_if_missing as u8);
151    leveldb_options_set_error_if_exists(c_options, options.error_if_exists as u8);
152    leveldb_options_set_paranoid_checks(c_options, options.paranoid_checks as u8);
153    if let Some(wbs) = options.write_buffer_size {
154        leveldb_options_set_write_buffer_size(c_options, wbs);
155    }
156    if let Some(mf) = options.max_open_files {
157        leveldb_options_set_max_open_files(c_options, mf);
158    }
159    if let Some(bs) = options.block_size {
160        leveldb_options_set_block_size(c_options, bs);
161    }
162    if let Some(bi) = options.block_restart_interval {
163        leveldb_options_set_block_restart_interval(c_options, bi);
164    }
165    leveldb_options_set_compression(c_options, options.compression);
166    if let Some(c) = comparator {
167        leveldb_options_set_comparator(c_options, c);
168    }
169    if let Some(ref cache) = options.cache {
170        leveldb_options_set_cache(c_options, cache.raw_ptr());
171    }
172    c_options
173}
174
175#[allow(missing_docs)]
176/// # Safety
177pub unsafe fn c_writeoptions(options: &WriteOptions) -> *mut leveldb_writeoptions_t {
178    let c_writeoptions = leveldb_writeoptions_create();
179    leveldb_writeoptions_set_sync(c_writeoptions, options.sync as u8);
180    c_writeoptions
181}
182
183#[allow(missing_docs)]
184/// # Safety
185pub unsafe fn c_readoptions(options: &ReadOptions) -> *mut leveldb_readoptions_t {
186    let c_readoptions = leveldb_readoptions_create();
187    leveldb_readoptions_set_verify_checksums(c_readoptions, options.verify_checksums as u8);
188    leveldb_readoptions_set_fill_cache(c_readoptions, options.fill_cache as u8);
189
190    c_readoptions
191}