rocksdb/
env.rs

1use std::sync::Arc;
2
3use libc::{self, c_int};
4
5use crate::{ffi, Error};
6
7/// An Env is an interface used by the rocksdb implementation to access
8/// operating system functionality like the filesystem etc. Callers
9/// may wish to provide a custom Env object when opening a database to
10/// get fine gain control; e.g., to rate limit file system operations.
11///
12/// All Env implementations are safe for concurrent access from
13/// multiple threads without any external synchronization.
14///
15/// Note: currently, C API behinds C++ API for various settings.
16/// See also: `rocksdb/include/env.h`
17#[derive(Clone)]
18pub struct Env(pub(crate) Arc<EnvWrapper>);
19
20pub(crate) struct EnvWrapper {
21    pub(crate) inner: *mut ffi::rocksdb_env_t,
22}
23
24impl Drop for EnvWrapper {
25    fn drop(&mut self) {
26        unsafe {
27            ffi::rocksdb_env_destroy(self.inner);
28        }
29    }
30}
31
32impl Env {
33    /// Returns default env
34    pub fn new() -> Result<Self, Error> {
35        let env = unsafe { ffi::rocksdb_create_default_env() };
36        if env.is_null() {
37            Err(Error::new("Could not create mem env".to_owned()))
38        } else {
39            Ok(Self(Arc::new(EnvWrapper { inner: env })))
40        }
41    }
42
43    /// Returns a new environment that stores its data in memory and delegates
44    /// all non-file-storage tasks to base_env.
45    pub fn mem_env() -> Result<Self, Error> {
46        let env = unsafe { ffi::rocksdb_create_mem_env() };
47        if env.is_null() {
48            Err(Error::new("Could not create mem env".to_owned()))
49        } else {
50            Ok(Self(Arc::new(EnvWrapper { inner: env })))
51        }
52    }
53
54    /// Sets the number of background worker threads of a specific thread pool for this environment.
55    /// `LOW` is the default pool.
56    ///
57    /// Default: 1
58    pub fn set_background_threads(&mut self, num_threads: c_int) {
59        unsafe {
60            ffi::rocksdb_env_set_background_threads(self.0.inner, num_threads);
61        }
62    }
63
64    /// Sets the size of the high priority thread pool that can be used to
65    /// prevent compactions from stalling memtable flushes.
66    pub fn set_high_priority_background_threads(&mut self, n: c_int) {
67        unsafe {
68            ffi::rocksdb_env_set_high_priority_background_threads(self.0.inner, n);
69        }
70    }
71
72    /// Sets the size of the low priority thread pool that can be used to
73    /// prevent compactions from stalling memtable flushes.
74    pub fn set_low_priority_background_threads(&mut self, n: c_int) {
75        unsafe {
76            ffi::rocksdb_env_set_low_priority_background_threads(self.0.inner, n);
77        }
78    }
79
80    /// Sets the size of the bottom priority thread pool that can be used to
81    /// prevent compactions from stalling memtable flushes.
82    pub fn set_bottom_priority_background_threads(&mut self, n: c_int) {
83        unsafe {
84            ffi::rocksdb_env_set_bottom_priority_background_threads(self.0.inner, n);
85        }
86    }
87
88    /// Wait for all threads started by StartThread to terminate.
89    pub fn join_all_threads(&mut self) {
90        unsafe {
91            ffi::rocksdb_env_join_all_threads(self.0.inner);
92        }
93    }
94
95    /// Lowering IO priority for threads from the specified pool.
96    pub fn lower_thread_pool_io_priority(&mut self) {
97        unsafe {
98            ffi::rocksdb_env_lower_thread_pool_io_priority(self.0.inner);
99        }
100    }
101
102    /// Lowering IO priority for high priority thread pool.
103    pub fn lower_high_priority_thread_pool_io_priority(&mut self) {
104        unsafe {
105            ffi::rocksdb_env_lower_high_priority_thread_pool_io_priority(self.0.inner);
106        }
107    }
108
109    /// Lowering CPU priority for threads from the specified pool.
110    pub fn lower_thread_pool_cpu_priority(&mut self) {
111        unsafe {
112            ffi::rocksdb_env_lower_thread_pool_cpu_priority(self.0.inner);
113        }
114    }
115
116    /// Lowering CPU priority for high priority thread pool.
117    pub fn lower_high_priority_thread_pool_cpu_priority(&mut self) {
118        unsafe {
119            ffi::rocksdb_env_lower_high_priority_thread_pool_cpu_priority(self.0.inner);
120        }
121    }
122}
123
124unsafe impl Send for EnvWrapper {}
125unsafe impl Sync for EnvWrapper {}