rust_rocksdb/
sst_file_manager.rs

1use std::ptr::NonNull;
2use std::sync::Arc;
3
4use crate::env::Env;
5use crate::ffi;
6
7pub(crate) struct SstFileManagerWrapper {
8    pub(crate) inner: NonNull<ffi::rocksdb_sst_file_manager_t>,
9    pub(crate) _env: Env,
10}
11
12unsafe impl Send for SstFileManagerWrapper {}
13unsafe impl Sync for SstFileManagerWrapper {}
14
15impl Drop for SstFileManagerWrapper {
16    fn drop(&mut self) {
17        unsafe {
18            ffi::rocksdb_sst_file_manager_destroy(self.inner.as_ptr());
19        }
20    }
21}
22
23#[derive(Clone)]
24pub struct SstFileManager(pub(crate) Arc<SstFileManagerWrapper>);
25
26impl SstFileManager {
27    /// Creates a new `SstFileManager` using the default `Env`.
28    ///
29    /// SstFileManager tracks and controls total SST file space usage, enabling
30    /// applications to cap disk utilization and throttle deletions.
31    /// see [docs](https://github.com/facebook/rocksdb/wiki/SST-File-Manager) for more details.
32    pub fn new() -> Self {
33        let env = Env::new().expect("Could not create default Env");
34        let inner = NonNull::new(unsafe { ffi::rocksdb_sst_file_manager_create(env.0.inner) })
35            .expect("Could not create RocksDB sst file manager");
36        SstFileManager(Arc::new(SstFileManagerWrapper { inner, _env: env }))
37    }
38
39    /// Sets the maximum allowed total SST file size in bytes.
40    pub fn set_max_allowed_space_usage(&self, bytes: u64) {
41        unsafe {
42            ffi::rocksdb_sst_file_manager_set_max_allowed_space_usage(self.0.inner.as_ptr(), bytes);
43        }
44    }
45
46    /// Sets the compaction buffer size in bytes used by the manager for space accounting.
47    pub fn set_compaction_buffer_size(&self, bytes: u64) {
48        unsafe {
49            ffi::rocksdb_sst_file_manager_set_compaction_buffer_size(self.0.inner.as_ptr(), bytes);
50        }
51    }
52
53    /// Returns true if the total SST file size has reached or exceeded the configured limit.
54    pub fn is_max_allowed_space_reached(&self) -> bool {
55        unsafe { ffi::rocksdb_sst_file_manager_is_max_allowed_space_reached(self.0.inner.as_ptr()) }
56    }
57
58    /// Returns true if the space limit is reached, including compaction output under accounting.
59    pub fn is_max_allowed_space_reached_including_compactions(&self) -> bool {
60        unsafe {
61            ffi::rocksdb_sst_file_manager_is_max_allowed_space_reached_including_compactions(
62                self.0.inner.as_ptr(),
63            )
64        }
65    }
66
67    /// Returns the total size of SST files tracked by this manager in bytes.
68    pub fn get_total_size(&self) -> u64 {
69        unsafe { ffi::rocksdb_sst_file_manager_get_total_size(self.0.inner.as_ptr()) }
70    }
71
72    /// Returns the configured file deletion rate in bytes per second. Negative means unlimited.
73    pub fn get_delete_rate_bytes_per_second(&self) -> i64 {
74        unsafe {
75            ffi::rocksdb_sst_file_manager_get_delete_rate_bytes_per_second(self.0.inner.as_ptr())
76        }
77    }
78
79    /// Sets the file deletion rate in bytes per second. Use a negative value to disable limiting.
80    pub fn set_delete_rate_bytes_per_second(&self, rate: i64) {
81        unsafe {
82            ffi::rocksdb_sst_file_manager_set_delete_rate_bytes_per_second(
83                self.0.inner.as_ptr(),
84                rate,
85            );
86        }
87    }
88
89    /// Returns the maximum trash-to-DB size ratio used for trash space control.
90    pub fn get_max_trash_db_ratio(&self) -> f64 {
91        unsafe { ffi::rocksdb_sst_file_manager_get_max_trash_db_ratio(self.0.inner.as_ptr()) }
92    }
93
94    /// Sets the maximum trash-to-DB size ratio used for trash space control.
95    pub fn set_max_trash_db_ratio(&self, ratio: f64) {
96        unsafe {
97            ffi::rocksdb_sst_file_manager_set_max_trash_db_ratio(self.0.inner.as_ptr(), ratio);
98        }
99    }
100
101    /// Returns the total trash size tracked by this manager in bytes.
102    pub fn get_total_trash_size(&self) -> u64 {
103        unsafe { ffi::rocksdb_sst_file_manager_get_total_trash_size(self.0.inner.as_ptr()) }
104    }
105}
106
107impl Default for SstFileManager {
108    fn default() -> Self {
109        Self::new()
110    }
111}