leveldb/database/
options.rs1use cruzbit_leveldb_sys::*;
2
3use libc::size_t;
4
5use super::cache::Cache;
6
7pub struct Options {
15 pub create_if_missing: bool,
19 pub error_if_exists: bool,
23 pub paranoid_checks: bool,
28 pub write_buffer_size: Option<size_t>,
32 pub max_open_files: Option<i32>,
36 pub block_size: Option<size_t>,
40 pub block_restart_interval: Option<i32>,
44 pub compression: Compression,
48 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 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#[derive(Copy, Clone, Debug)]
93pub struct WriteOptions {
94 pub sync: bool,
98}
99
100impl Default for WriteOptions {
101 fn default() -> Self {
102 Self::new()
103 }
104}
105
106impl WriteOptions {
107 pub fn new() -> WriteOptions {
109 WriteOptions { sync: false }
110 }
111}
112
113#[derive(Copy, Clone, Debug)]
115pub struct ReadOptions {
116 pub verify_checksums: bool,
120 pub fill_cache: bool,
125}
126
127impl Default for ReadOptions {
128 fn default() -> Self {
129 Self::new()
130 }
131}
132
133impl ReadOptions {
134 pub fn new() -> ReadOptions {
136 ReadOptions {
137 verify_checksums: false,
138 fill_cache: true,
139 }
140 }
141}
142
143#[allow(missing_docs)]
144pub 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)]
176pub 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)]
184pub 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}