Skip to content

Commit cf71471

Browse files
authored
store: add Mode::ReadWriteExisting (#7460)
Introduce Mode::ReadWriteExisting for opening a database. It’s like Mode::ReadWrite except it requires that the database exists and will not create it if it doesn’t. This is currently unused, but it’s going to be used in future commits.
1 parent a1337e9 commit cf71471

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

core/store/src/config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ pub struct StoreConfig {
5050
}
5151

5252
/// Mode in which to open the storage.
53-
#[derive(Clone, Copy)]
53+
#[derive(Clone, Copy, Eq, PartialEq)]
5454
pub enum Mode {
55+
/// Open an existing database in read-only mode. Fail if it doesn’t exist.
5556
ReadOnly,
57+
/// Open an existing database in read-write mode. Fail if it doesn’t exist.
58+
ReadWriteExisting,
59+
/// Open a database in read-write mode. create if it doesn’t exist.
5660
ReadWrite,
5761
}
5862

core/store/src/db/rocksdb.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ impl RocksDB {
8383
Mode::ReadOnly => {
8484
DB::open_cf_descriptors_read_only(&options, path, cf_descriptors, false)
8585
}
86-
Mode::ReadWrite => DB::open_cf_descriptors(&options, path, cf_descriptors),
86+
Mode::ReadWriteExisting | Mode::ReadWrite => {
87+
// Difference between the two read-write modes is captured in
88+
// options. See rocksdb_options.
89+
DB::open_cf_descriptors(&options, path, cf_descriptors)
90+
}
8791
}
8892
.map_err(into_other)?;
8993
if cfg!(feature = "single_thread_rocksdb") {
@@ -294,12 +298,11 @@ fn next_prefix(prefix: &[u8]) -> Option<Vec<u8>> {
294298

295299
/// DB level options
296300
fn rocksdb_options(store_config: &StoreConfig, mode: Mode) -> Options {
297-
let read_write = matches!(mode, Mode::ReadWrite);
298301
let mut opts = Options::default();
299302

300303
set_compression_options(&mut opts);
301-
opts.create_missing_column_families(true);
302-
opts.create_if_missing(read_write);
304+
opts.create_missing_column_families(mode != Mode::ReadOnly);
305+
opts.create_if_missing(mode == Mode::ReadWrite);
303306
opts.set_use_fsync(false);
304307
opts.set_max_open_files(store_config.max_open_files.try_into().unwrap_or(i32::MAX));
305308
opts.set_keep_log_file_num(1);
@@ -320,7 +323,8 @@ fn rocksdb_options(store_config: &StoreConfig, mode: Mode) -> Options {
320323
opts.set_max_total_wal_size(bytesize::GIB);
321324
}
322325

323-
if read_write && store_config.enable_statistics {
326+
// TODO(mina86): Perhaps enable statistics even in read-only mode?
327+
if mode != Mode::ReadOnly && store_config.enable_statistics {
324328
// Rust API doesn't permit choosing stats level. The default stats level
325329
// is `kExceptDetailedTimers`, which is described as: "Collects all
326330
// stats except time inside mutex lock AND time spent on compression."

0 commit comments

Comments
 (0)