rocksolid 2.5.6

An ergonomic, high-level RocksDB wrapper for Rust. Features CF-aware optimistic & pessimistic transactions, advanced routing for merge operators and compaction filters, performance tuning profiles, batching, TTL values, and DAO macros.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// rocksolid/src/tx/optimistic_tx_store.rs

//! Provides the public `RocksDbOptimisticTxnStore` for default Column Family optimistic transactional operations.

use super::cf_optimistic_tx_store::RocksDbCFOptimisticTxnStore;
use crate::bytes::AsBytes;
use crate::config::{BaseCfConfig, MergeOperatorConfig, RecoveryMode, RockSolidMergeOperatorCfConfig};
use crate::error::StoreResult;
use crate::iter::{IterConfig, IterationResult};
use crate::store::DefaultCFOperations;
use crate::tuner::{Tunable, TuningProfile};
use crate::tx::cf_optimistic_tx_store::{CFOptimisticTxnConfig, RocksDbCFOptimisticTxnStoreConfig};
use crate::tx::cf_tx_store::{CFTxConfig, CustomDbAndCfFn, RocksDbTransactionalStoreConfig, TransactionalEngine};
use crate::tx::optimistic_context::OptimisticTransactionContext;
use crate::tx::tx_store::CustomDbAndDefaultCb;
use crate::types::{IterationControlDecision, MergeValue, ValueWithExpiry};
use crate::{CFOperations, RockSolidCompactionFilterRouterConfig, StoreError};

use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;
use std::path::Path;
use std::sync::Arc;

use bytevec::ByteDecodable;
use rocksdb::{DEFAULT_COLUMN_FAMILY_NAME, Direction, OptimisticTransactionDB, Options as RocksDbOptions};
use serde::{Serialize, de::DeserializeOwned};

// --- Configuration for RocksDbOptimisticTxnStore (Default CF focused) ---

/// Configuration for an optimistic transactional RocksDB store focused on the default Column Family.
pub struct RocksDbOptimisticTxnStoreConfig {
  pub path: String,
  pub create_if_missing: bool,
  pub default_cf_tuning_profile: Option<TuningProfile>,
  pub default_cf_merge_operator: Option<MergeOperatorConfig>,
  pub compaction_filter_router: Option<RockSolidCompactionFilterRouterConfig>,
  pub custom_options_default_cf_and_db: CustomDbAndDefaultCb,
  pub recovery_mode: Option<RecoveryMode>,
  pub parallelism: Option<i32>,
  pub enable_statistics: Option<bool>,
}

impl Debug for RocksDbOptimisticTxnStoreConfig {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("RocksDbOptimisticTxnStoreConfig")
      .field("path", &self.path)
      .field("create_if_missing", &self.create_if_missing)
      .field(
        "default_cf_tuning_profile_is_some",
        &self.default_cf_tuning_profile.is_some(),
      )
      .field(
        "default_cf_merge_operator_is_some",
        &self.default_cf_merge_operator.is_some(),
      )
      .field(
        "custom_options_default_cf_and_db_is_some",
        &self.custom_options_default_cf_and_db.is_some(),
      )
      .field("recovery_mode", &self.recovery_mode)
      .field("parallelism", &self.parallelism)
      .field("enable_statistics", &self.enable_statistics)
      .finish()
  }
}

impl Default for RocksDbOptimisticTxnStoreConfig {
  fn default() -> Self {
    Self {
      path: Default::default(),
      create_if_missing: true,
      default_cf_tuning_profile: None,
      default_cf_merge_operator: None,
      compaction_filter_router: None,
      custom_options_default_cf_and_db: None,
      recovery_mode: None,
      parallelism: None,
      enable_statistics: None,
    }
  }
}

impl From<RocksDbOptimisticTxnStoreConfig> for RocksDbTransactionalStoreConfig {
  fn from(cfg: RocksDbOptimisticTxnStoreConfig) -> Self {
    let mut cf_configs = HashMap::new();
    let default_cf_base_config = BaseCfConfig {
      tuning_profile: cfg.default_cf_tuning_profile,
      merge_operator: cfg.default_cf_merge_operator.map(RockSolidMergeOperatorCfConfig::from),
      comparator: None,
      compaction_filter_router: cfg.compaction_filter_router,
    };
    cf_configs.insert(
      rocksdb::DEFAULT_COLUMN_FAMILY_NAME.to_string(),
      CFTxConfig {
        base_config: default_cf_base_config,
      },
    );

    let custom_db_and_all_cf_callback: crate::tx::cf_tx_store::CustomDbAndCfCb =
      if let Some(user_fn) = cfg.custom_options_default_cf_and_db {
        Some(Box::from(
          move |cf_name: &str, db_opts: &mut Tunable<RocksDbOptions>| {
            if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
              user_fn(cf_name, db_opts);
            }
          },
        ))
      } else {
        None
      };

    RocksDbTransactionalStoreConfig {
      path: cfg.path,
      create_if_missing: cfg.create_if_missing,
      db_tuning_profile: None,
      column_family_configs: cf_configs,
      column_families_to_open: vec![rocksdb::DEFAULT_COLUMN_FAMILY_NAME.to_string()],
      custom_options_db_and_cf: custom_db_and_all_cf_callback,
      recovery_mode: cfg.recovery_mode,
      parallelism: cfg.parallelism,
      enable_statistics: cfg.enable_statistics,
      engine: TransactionalEngine::Optimistic,
    }
  }
}

impl From<RocksDbOptimisticTxnStoreConfig> for RocksDbCFOptimisticTxnStoreConfig {
  fn from(cfg: RocksDbOptimisticTxnStoreConfig) -> Self {
    let mut cf_configs = HashMap::new();
    let default_cf_base_config = BaseCfConfig {
      tuning_profile: cfg.default_cf_tuning_profile,
      merge_operator: cfg.default_cf_merge_operator.map(RockSolidMergeOperatorCfConfig::from),
      comparator: None,
      compaction_filter_router: cfg.compaction_filter_router,
    };
    cf_configs.insert(
      rocksdb::DEFAULT_COLUMN_FAMILY_NAME.to_string(),
      CFOptimisticTxnConfig {
        base_config: default_cf_base_config,
      },
    );

    // This translation mirrors the one in tx_store.rs
    let custom_db_and_all_cf_callback: crate::tx::cf_tx_store::CustomDbAndCfCb =
      if let Some(user_fn) = cfg.custom_options_default_cf_and_db {
        let closure = move |cf_name: &str, db_opts: &mut Tunable<RocksDbOptions>| {
          if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
            user_fn(cf_name, db_opts);
          }
        };
        // Explicitly cast the Box<closure> to a Box<dyn Fn(...)>
        Some(Box::new(closure) as Box<CustomDbAndCfFn>)
      } else {
        None
      };

    Self {
      path: cfg.path,
      create_if_missing: cfg.create_if_missing,
      db_tuning_profile: None, // Wrappers don't have a DB profile
      column_family_configs: cf_configs,
      column_families_to_open: vec![rocksdb::DEFAULT_COLUMN_FAMILY_NAME.to_string()],
      custom_options_db_and_cf: custom_db_and_all_cf_callback,
      recovery_mode: cfg.recovery_mode,
      parallelism: cfg.parallelism,
      enable_statistics: cfg.enable_statistics,
    }
  }
}

/// A RocksDB store providing optimistic transactional capabilities on the **default Column Family**.
///
/// This is the recommended entry point for applications that need optimistic concurrency
/// control without the complexity of multiple Column Families.
///
/// Use `transaction_context()` to build and execute transactions. Remember that the application
/// is responsible for retrying transactions that fail due to write conflicts.
#[derive(Debug)]
pub struct RocksDbOptimisticTxnStore {
  pub(crate) cf_store: Arc<RocksDbCFOptimisticTxnStore>,
}

impl RocksDbOptimisticTxnStore {
  /// Opens or creates an optimistic transactional RocksDB database.
  pub fn open(config: RocksDbOptimisticTxnStoreConfig) -> StoreResult<Self> {
    log::info!(
      "RocksDbOptimisticTxnStore: Opening optimistic transactional DB at '{}' for default CF.",
      config.path
    );

    // This is the correct way. The user provides the simple config.
    // We convert it into the CF-aware optimistic config to pass to the CF-aware store's open method.
    let cf_optimistic_config: RocksDbCFOptimisticTxnStoreConfig = config.into();

    let store_impl = RocksDbCFOptimisticTxnStore::open(cf_optimistic_config)?;
    Ok(Self {
      cf_store: Arc::new(store_impl),
    })
  }

  /// Destroys the database files at the given path. Use with extreme caution.
  pub fn destroy(path: &Path, config: RocksDbOptimisticTxnStoreConfig) -> StoreResult<()> {
    // This logic is simpler as `destroy` only needs the path and a few options.
    let cf_optimistic_config = RocksDbCFOptimisticTxnStoreConfig {
      path: config.path,
      create_if_missing: config.create_if_missing,
      db_tuning_profile: config.default_cf_tuning_profile, // close enough for destroy
      column_family_configs: Default::default(),
      column_families_to_open: vec![],
      custom_options_db_and_cf: None,
      recovery_mode: config.recovery_mode,
      parallelism: config.parallelism,
      enable_statistics: config.enable_statistics,
    };
    RocksDbCFOptimisticTxnStore::destroy(path, cf_optimistic_config)
  }

  /// Returns the filesystem path of the database directory.
  pub fn path(&self) -> &str {
    self.cf_store.path()
  }

  /// Provides access to the underlying `RocksDbCFOptimisticTxnStore`.
  /// This can be used if direct CF operations are needed.
  pub fn cf_optimistic_txn_store(&self) -> Arc<RocksDbCFOptimisticTxnStore> {
    self.cf_store.clone()
  }

  /// Returns a thread-safe reference (`Arc`) to the underlying `rocksdb::OptimisticTransactionDB` instance.
  ///
  /// Useful for advanced operations not directly exposed by this library.
  pub fn db_raw(&self) -> Arc<OptimisticTransactionDB> {
    self.cf_store.db_raw()
  }

  /// Creates a standard optimistic transaction context with conflict detection enabled.
  ///
  /// This is the recommended method for most use cases.
  pub fn transaction_context(&self) -> OptimisticTransactionContext<'_> {
    // Delegate to the underlying CF-aware store
    self.cf_store.transaction_context()
  }

  /// Creates a "blind write" optimistic transaction context with conflict detection DISABLED.
  ///
  /// See `RocksDbCFOptimisticTxnStore::blind_transaction_context` for detailed documentation.
  ///
  /// **Use with caution.**
  pub fn blind_transaction_context(&self) -> OptimisticTransactionContext<'_> {
    // Delegate to the underlying CF-aware store
    self.cf_store.blind_transaction_context()
  }
}

impl DefaultCFOperations for RocksDbOptimisticTxnStore {
  // --- Read operations on COMMITTED data (default CF) ---
  fn get<K, V>(&self, key: K) -> StoreResult<Option<V>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: DeserializeOwned + Debug,
  {
    self.cf_store.get(DEFAULT_COLUMN_FAMILY_NAME, key)
  }

  fn get_raw<K>(&self, key: K) -> StoreResult<Option<Vec<u8>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.get_raw(DEFAULT_COLUMN_FAMILY_NAME, key)
  }

  fn get_with_expiry<K, V>(&self, key: K) -> StoreResult<Option<ValueWithExpiry<V>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + DeserializeOwned + Debug,
  {
    self.cf_store.get_with_expiry(DEFAULT_COLUMN_FAMILY_NAME, key)
  }

  fn exists<K>(&self, key: K) -> StoreResult<bool>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.exists(DEFAULT_COLUMN_FAMILY_NAME, key)
  }

  fn multiget<K, V>(&self, keys: &[K]) -> StoreResult<Vec<Option<V>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug + Clone,
    V: DeserializeOwned + Debug,
  {
    self.cf_store.multiget(DEFAULT_COLUMN_FAMILY_NAME, keys)
  }

  fn multiget_raw<K>(&self, keys: &[K]) -> StoreResult<Vec<Option<Vec<u8>>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.multiget_raw(DEFAULT_COLUMN_FAMILY_NAME, keys)
  }

  fn multiget_with_expiry<K, V>(&self, keys: &[K]) -> StoreResult<Vec<Option<ValueWithExpiry<V>>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug + Clone,
    V: Serialize + DeserializeOwned + Debug,
  {
    self.cf_store.multiget_with_expiry(DEFAULT_COLUMN_FAMILY_NAME, keys)
  }

  // --- Write operations directly on store (COMMITTED state, default CF) ---
  fn put<K, V>(&self, key: K, value: &V) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + Debug,
  {
    self.cf_store.put(DEFAULT_COLUMN_FAMILY_NAME, key, value)
  }

  fn put_raw<K>(&self, key: K, raw_val: &[u8]) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.put_raw(DEFAULT_COLUMN_FAMILY_NAME, key, raw_val)
  }

  fn put_with_expiry<K, V>(&self, key: K, val: &V, expire_time: u64) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + DeserializeOwned + Debug,
  {
    self
      .cf_store
      .put_with_expiry(DEFAULT_COLUMN_FAMILY_NAME, key, val, expire_time)
  }

  fn merge<K, PatchVal>(&self, key: K, merge_value: &MergeValue<PatchVal>) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    PatchVal: Serialize + Debug,
  {
    self.cf_store.merge(DEFAULT_COLUMN_FAMILY_NAME, key, merge_value)
  }

  fn merge_raw<K>(&self, key: K, raw_merge_op: &[u8]) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.merge_raw(DEFAULT_COLUMN_FAMILY_NAME, key, raw_merge_op)
  }

  fn merge_with_expiry<K, V>(&self, cf_name: &str, key: K, value: &V, expire_time: u64) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + DeserializeOwned + Debug,
  {
    self.cf_store.merge_with_expiry(cf_name, key, value, expire_time)
  }

  fn delete<K>(&self, key: K) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self.cf_store.delete(DEFAULT_COLUMN_FAMILY_NAME, key)
  }

  fn delete_range<K>(&self, start_key: K, end_key: K) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    self
      .cf_store
      .delete_range(DEFAULT_COLUMN_FAMILY_NAME, start_key, end_key)
  }

  // --- Iterator / Find Operations on COMMITTED data (default CF) ---
  fn iterate<'store_lt, SerKey, OutK, OutV>(
    &'store_lt self,
    config: IterConfig<'store_lt, SerKey, OutK, OutV>,
  ) -> Result<IterationResult<'store_lt, OutK, OutV>, StoreError>
  where
    SerKey: AsBytes + Hash + Eq + PartialEq + Debug,
    OutK: DeserializeOwned + Debug + 'store_lt,
    OutV: DeserializeOwned + Debug + 'store_lt,
  {
    self.cf_store.iterate(config)
  }

  fn find_by_prefix<Key, Val>(&self, prefix: &Key, direction: Direction) -> StoreResult<Vec<(Key, Val)>>
  where
    Key: ByteDecodable + AsBytes + DeserializeOwned + Hash + Eq + PartialEq + Debug + Clone,
    Val: DeserializeOwned + Debug,
  {
    self
      .cf_store
      .find_by_prefix(DEFAULT_COLUMN_FAMILY_NAME, prefix, direction)
  }

  fn find_from<Key, Val, F>(&self, start_key: Key, direction: Direction, control_fn: F) -> StoreResult<Vec<(Key, Val)>>
  where
    Key: ByteDecodable + AsBytes + DeserializeOwned + Hash + Eq + PartialEq + Debug,
    Val: DeserializeOwned + Debug,
    F: FnMut(&[u8], &[u8], usize) -> IterationControlDecision + 'static,
  {
    self
      .cf_store
      .find_from(DEFAULT_COLUMN_FAMILY_NAME, start_key, direction, control_fn)
  }

  fn find_from_with_expire_val<Key, Val, ControlFn>(
    &self,
    start: &Key,
    reverse: bool,
    control_fn: ControlFn,
  ) -> Result<Vec<(Key, ValueWithExpiry<Val>)>, String>
  where
    Key: ByteDecodable + AsBytes + DeserializeOwned + Hash + Eq + PartialEq + Debug + Clone,
    Val: DeserializeOwned + Debug,
    ControlFn: FnMut(&[u8], &[u8], usize) -> IterationControlDecision + 'static,
  {
    self
      .cf_store
      .find_from_with_expire_val(DEFAULT_COLUMN_FAMILY_NAME, start, reverse, control_fn)
  }

  fn find_by_prefix_with_expire_val<Key, Val, ControlFn>(
    &self,
    start: &Key,
    reverse: bool,
    control_fn: ControlFn,
  ) -> Result<Vec<(Key, ValueWithExpiry<Val>)>, String>
  where
    Key: ByteDecodable + AsBytes + DeserializeOwned + Hash + Eq + PartialEq + Debug + Clone,
    Val: DeserializeOwned + Debug,
    ControlFn: FnMut(&[u8], &[u8], usize) -> IterationControlDecision + 'static,
  {
    self
      .cf_store
      .find_by_prefix_with_expire_val(DEFAULT_COLUMN_FAMILY_NAME, start, reverse, control_fn)
  }
}