rocksdb/
sst_file_manager.rs

1use std::sync::Arc;
2
3use crate::{ffi, Env, Error};
4
5/// SstFileManager is used to track SST files in the database and control their
6/// deletion rate.
7///
8/// All SstFileManager public functions are thread-safe.
9///
10/// SstFileManager can be used to:
11/// * Set a limit on the total size of SST files. Once the limit is exceeded,
12///   writes will fail with a specific status.
13/// * Control the deletion rate of obsolete files to avoid saturating the
14///   disk bandwidth with deletion operations.
15/// * Track the total size of SST files and the total size of trash files.
16///
17/// See also: `rocksdb/include/rocksdb/sst_file_manager.h`
18#[derive(Clone)]
19pub struct SstFileManager(pub(crate) Arc<SstFileManagerWrapper>);
20
21pub(crate) struct SstFileManagerWrapper {
22    pub(crate) inner: *mut ffi::rocksdb_sst_file_manager_t,
23}
24
25impl Drop for SstFileManagerWrapper {
26    fn drop(&mut self) {
27        unsafe {
28            ffi::rocksdb_sst_file_manager_destroy(self.inner);
29        }
30    }
31}
32
33impl SstFileManager {
34    /// Creates a new SstFileManager with the given environment.
35    ///
36    /// # Arguments
37    ///
38    /// * `env` - The environment to use for file operations
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the SstFileManager cannot be created.
43    pub fn new(env: &Env) -> Result<Self, Error> {
44        let manager = unsafe { ffi::rocksdb_sst_file_manager_create(env.0.inner) };
45        if manager.is_null() {
46            Err(Error::new("Could not create SstFileManager".to_owned()))
47        } else {
48            Ok(Self(Arc::new(SstFileManagerWrapper { inner: manager })))
49        }
50    }
51
52    /// Sets the maximum allowed space usage for SST files.
53    ///
54    /// If the total size of SST files exceeds this limit, writes will fail.
55    /// Setting this to 0 disables the limit.
56    ///
57    /// # Arguments
58    ///
59    /// * `max_allowed_space` - Maximum allowed space in bytes (0 = unlimited)
60    pub fn set_max_allowed_space_usage(&self, max_allowed_space: u64) {
61        unsafe {
62            ffi::rocksdb_sst_file_manager_set_max_allowed_space_usage(
63                self.0.inner,
64                max_allowed_space,
65            );
66        }
67    }
68
69    /// Sets the size of the compaction buffer.
70    ///
71    /// The compaction buffer is used to reserve space for compaction output.
72    /// If set, the SstFileManager will consider this space as used when
73    /// checking against the maximum allowed space.
74    ///
75    /// # Arguments
76    ///
77    /// * `compaction_buffer_size` - Size of the compaction buffer in bytes
78    pub fn set_compaction_buffer_size(&self, compaction_buffer_size: u64) {
79        unsafe {
80            ffi::rocksdb_sst_file_manager_set_compaction_buffer_size(
81                self.0.inner,
82                compaction_buffer_size,
83            );
84        }
85    }
86
87    /// Returns true if the total size of SST files exceeded the maximum allowed space.
88    pub fn is_max_allowed_space_reached(&self) -> bool {
89        unsafe { ffi::rocksdb_sst_file_manager_is_max_allowed_space_reached(self.0.inner) }
90    }
91
92    /// Returns true if the total size of SST files plus compaction buffer size
93    /// exceeded the maximum allowed space.
94    pub fn is_max_allowed_space_reached_including_compactions(&self) -> bool {
95        unsafe {
96            ffi::rocksdb_sst_file_manager_is_max_allowed_space_reached_including_compactions(
97                self.0.inner,
98            )
99        }
100    }
101
102    /// Returns the total size of all tracked SST files in bytes.
103    pub fn get_total_size(&self) -> u64 {
104        unsafe { ffi::rocksdb_sst_file_manager_get_total_size(self.0.inner) }
105    }
106
107    /// Returns the current delete rate in bytes per second.
108    ///
109    /// Returns 0 if there is no rate limiting.
110    pub fn get_delete_rate_bytes_per_second(&self) -> i64 {
111        unsafe { ffi::rocksdb_sst_file_manager_get_delete_rate_bytes_per_second(self.0.inner) }
112    }
113
114    /// Sets the delete rate limit in bytes per second.
115    ///
116    /// This controls how fast obsolete files are deleted. Setting this to 0
117    /// disables rate limiting and files will be deleted immediately.
118    ///
119    /// # Arguments
120    ///
121    /// * `delete_rate` - Delete rate in bytes per second (0 = unlimited)
122    pub fn set_delete_rate_bytes_per_second(&self, delete_rate: i64) {
123        unsafe {
124            ffi::rocksdb_sst_file_manager_set_delete_rate_bytes_per_second(
125                self.0.inner,
126                delete_rate,
127            );
128        }
129    }
130
131    /// Returns the maximum trash to DB size ratio.
132    ///
133    /// If the trash size exceeds this ratio of the total DB size, files will be
134    /// deleted immediately regardless of the delete rate limit.
135    pub fn get_max_trash_db_ratio(&self) -> f64 {
136        unsafe { ffi::rocksdb_sst_file_manager_get_max_trash_db_ratio(self.0.inner) }
137    }
138
139    /// Sets the maximum trash to DB size ratio.
140    ///
141    /// When the size of pending-deletion files (trash) exceeds this ratio
142    /// relative to the database size, files will be deleted immediately
143    /// regardless of the delete rate limit.
144    ///
145    /// # Arguments
146    ///
147    /// * `ratio` - Maximum trash to DB size ratio (e.g., 0.25 = 25%)
148    pub fn set_max_trash_db_ratio(&self, ratio: f64) {
149        unsafe {
150            ffi::rocksdb_sst_file_manager_set_max_trash_db_ratio(self.0.inner, ratio);
151        }
152    }
153
154    /// Returns the total size of trash files (files pending deletion) in bytes.
155    pub fn get_total_trash_size(&self) -> u64 {
156        unsafe { ffi::rocksdb_sst_file_manager_get_total_trash_size(self.0.inner) }
157    }
158}
159
160unsafe impl Send for SstFileManagerWrapper {}
161unsafe impl Sync for SstFileManagerWrapper {}