leveldb/database/
options.rs

1use 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 Options {
69    /// Create a new `Options` struct with default settings.
70    pub fn new() -> Options {
71        Options {
72            create_if_missing: false,
73            error_if_exists: false,
74            paranoid_checks: false,
75            write_buffer_size: None,
76            max_open_files: None,
77            block_size: None,
78            block_restart_interval: None,
79            compression: Compression::No,
80            cache: None,
81        }
82    }
83}
84
85/// The write options to use for a write operation.
86#[derive(Copy, Clone, Debug)]
87pub struct WriteOptions {
88    /// `fsync` before acknowledging a write operation.
89    ///
90    /// default: false
91    pub sync: bool,
92}
93
94impl WriteOptions {
95    /// Return a new `WriteOptions` struct with default settings.
96    pub fn new() -> WriteOptions {
97        WriteOptions { sync: false }
98    }
99}
100
101/// The read options to use for any read operation.
102#[derive(Copy, Clone, Debug)]
103pub struct ReadOptions  {
104    /// Whether to verify the saved checksums on read.
105    ///
106    /// default: false
107    pub verify_checksums: bool,
108    /// Whether to fill the internal cache with the
109    /// results of the read.
110    ///
111    /// default: true
112    pub fill_cache: bool,
113}
114
115impl ReadOptions {
116    /// Return a `ReadOptions` struct with the default values.
117    pub fn new() -> ReadOptions {
118        ReadOptions {
119            verify_checksums: false,
120            fill_cache: true,
121        }
122    }
123}
124
125
126#[allow(missing_docs)]
127pub unsafe fn c_options(options: &Options,
128                        comparator: Option<*mut leveldb_comparator_t>)
129                        -> *mut leveldb_options_t {
130    let c_options = leveldb_options_create();
131    leveldb_options_set_create_if_missing(c_options, options.create_if_missing as u8);
132    leveldb_options_set_error_if_exists(c_options, options.error_if_exists as u8);
133    leveldb_options_set_paranoid_checks(c_options, options.paranoid_checks as u8);
134    if let Some(wbs) = options.write_buffer_size {
135        leveldb_options_set_write_buffer_size(c_options, wbs);
136    }
137    if let Some(mf) = options.max_open_files {
138        leveldb_options_set_max_open_files(c_options, mf);
139    }
140    if let Some(bs) = options.block_size {
141        leveldb_options_set_block_size(c_options, bs);
142    }
143    if let Some(bi) = options.block_restart_interval {
144        leveldb_options_set_block_restart_interval(c_options, bi);
145    }
146    leveldb_options_set_compression(c_options, options.compression);
147    if let Some(c) = comparator {
148        leveldb_options_set_comparator(c_options, c);
149    }
150    if let Some(ref cache) = options.cache {
151        leveldb_options_set_cache(c_options, cache.raw_ptr());
152    }
153    c_options
154}
155
156#[allow(missing_docs)]
157pub unsafe fn c_writeoptions(options: &WriteOptions) -> *mut leveldb_writeoptions_t {
158    let c_writeoptions = leveldb_writeoptions_create();
159    leveldb_writeoptions_set_sync(c_writeoptions, options.sync as u8);
160    c_writeoptions
161}
162
163#[allow(missing_docs)]
164pub unsafe fn c_readoptions(options: &ReadOptions) -> *mut leveldb_readoptions_t {
165    let c_readoptions = leveldb_readoptions_create();
166    leveldb_readoptions_set_verify_checksums(c_readoptions, options.verify_checksums as u8);
167    leveldb_readoptions_set_fill_cache(c_readoptions, options.fill_cache as u8);
168
169    c_readoptions
170}