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
//! Provides the core CF-aware transactional store, `RocksDbCFTxnStore`.

use crate::bytes::AsBytes;
use crate::config::{BaseCfConfig, RecoveryMode};
use crate::error::{StoreError, StoreResult};
use crate::iter::{IterConfig, IterationResult};
use crate::iter::helpers::{IterationHelper, GeneralFactory, PrefixFactory};
use crate::serialization::{deserialize_kv, deserialize_value, serialize_key, serialize_value};
use crate::tuner::{Tunable, TuningProfile};
use crate::tx::{internal};
use crate::types::{IterationControlDecision, MergeValue, ValueWithExpiry};
use crate::{deserialize_kv_expiry, implement_cf_operations_for_transactional_store};

use bytevec::ByteDecodable;
use rocksdb::{
  DB as StandardDB, // For destroy
  Direction,
  Options as RocksDbOptions,
  Transaction,
  TransactionDB,
  TransactionDBOptions,
  TransactionOptions,
  WriteOptions as RocksDbWriteOptions,
  ReadOptions,
};
use serde::{Serialize, de::DeserializeOwned};
use std::hash::Hash;
use std::{collections::HashMap, fmt::Debug, path::Path, sync::Arc};

// --- Configuration for RocksDbCFTxnStore ---

pub type CustomDbAndCfFn = dyn Fn(&str, &mut Tunable<RocksDbOptions>) + Send + Sync + 'static;
pub type CustomDbAndCfCb = Option<Box<CustomDbAndCfFn>>;

/// Defines the transactional database engine to use.
pub enum TransactionalEngine {
  Pessimistic(TransactionDBOptions),
  Optimistic,
}

impl Debug for TransactionalEngine {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Pessimistic(_) => f.debug_tuple("Pessimistic").field(&"<TransactionDBOptions>").finish(),
      Self::Optimistic => write!(f, "Optimistic"),
    }
  }
}

impl Default for TransactionalEngine {
  fn default() -> Self {
    TransactionalEngine::Pessimistic(TransactionDBOptions::default())
  }
}

/// Per-CF configuration specific to transactional stores, building upon BaseCfConfig.
#[derive(Clone, Debug, Default)]
pub struct CFTxConfig {
  /// Base configuration like tuning profile and merge operator.
  pub base_config: BaseCfConfig,
  // Future: Add transaction-specific CF options here if any (e.g., snapshot requirements per CF)
}

/// Unified internal configuration for a CF-aware transactional RocksDB store.
/// This struct drives the opening of either a Pessimistic or Optimistic database.
#[derive(Default)]
pub struct RocksDbTransactionalStoreConfig {
  pub path: String,
  pub create_if_missing: bool,
  pub db_tuning_profile: Option<TuningProfile>,
  pub column_family_configs: HashMap<String, CFTxConfig>,
  pub column_families_to_open: Vec<String>,
  pub custom_options_db_and_cf: CustomDbAndCfCb,
  // DB-wide hard settings
  pub recovery_mode: Option<RecoveryMode>,
  pub parallelism: Option<i32>,
  pub enable_statistics: Option<bool>,
  // Engine-specific options
  pub engine: TransactionalEngine,
}

impl Debug for RocksDbTransactionalStoreConfig {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("RocksDbTransactionalStoreConfig")
      .field("path", &self.path)
      .field("create_if_missing", &self.create_if_missing)
      .field("db_tuning_profile_is_some", &self.db_tuning_profile.is_some())
      .field("column_family_configs_count", &self.column_family_configs.len())
      .field("column_families_to_open", &self.column_families_to_open)
      .field(
        "custom_options_db_and_cf_is_some",
        &self.custom_options_db_and_cf.is_some(),
      )
      .field("recovery_mode", &self.recovery_mode)
      .field("parallelism", &self.parallelism)
      .field("enable_statistics", &self.enable_statistics)
      .field("engine", &self.engine)
      .finish()
  }
}

// --- RocksDbCFTxnStore Definition ---
/// The core Column Family (CF)-aware transactional key-value store.
///
/// This store provides methods to interact with a `rocksdb::TransactionDB`,
/// allowing operations to target specific Column Families both for committed reads
/// and for operations within an explicit transaction.
pub struct RocksDbCFTxnStore {
  db: Arc<TransactionDB>,
  cf_names: HashMap<String, ()>, // Stores names of opened CFs for quick check
  path: String,
}

impl Debug for RocksDbCFTxnStore {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("RocksDbCFTxnStore")
      .field("path", &self.path)
      .field("db", &"<Arc<rocksdb::TransactionDB>>")
      .field("cf_names", &self.cf_names.keys().collect::<Vec<&String>>())
      .finish()
  }
}

impl RocksDbCFTxnStore {
  /// Opens or creates a CF-aware transactional RocksDB database.
  pub fn open(cfg: RocksDbTransactionalStoreConfig) -> StoreResult<Self> {
    let cf_names_map = cfg
      .column_families_to_open
      .iter()
      .map(|name| (name.clone(), ()))
      .collect();
    let path = cfg.path.clone();

    let db_arc = match internal::_open_db_internal(cfg)? {
      either::Either::Left(db) => db,
      either::Either::Right(_) => {
        return Err(StoreError::InvalidConfiguration(
          "Configured for Optimistic engine, but tried to open as Pessimistic".to_string(),
        ));
      }
    };

    Ok(Self {
      db: db_arc,
      cf_names: cf_names_map,
      path,
    })
  }

  /// Destroys the transactional database files at the given path.
  pub fn destroy(path: &Path, cfg: RocksDbTransactionalStoreConfig) -> StoreResult<()> {
    log::warn!("Destroying RocksDB TransactionDB at path: {}", path.display());

    // Use the same internal helper that `_open_db_internal` uses.
    let final_opts = internal::_build_db_wide_options(
      path.to_str().unwrap_or(""),
      Some(cfg.create_if_missing),
      cfg.parallelism,
      cfg.recovery_mode,
      cfg.enable_statistics,
      &cfg.db_tuning_profile,
    );

    // TransactionDB does not have its own destroy method. Use StandardDB::destroy.
    StandardDB::destroy(&final_opts, path).map_err(StoreError::RocksDb)
  }

  pub fn path(&self) -> &str {
    &self.path
  }

  /// Retrieves a shared, bound column family handle.
  pub(crate) fn get_cf_handle<'s>(&'s self, cf_name: &str) -> StoreResult<Arc<rocksdb::BoundColumnFamily<'s>>> {
    if !self.cf_names.contains_key(cf_name) {
      // Also check if it's default, as default might not be in cf_names if only named cfs were listed
      // but default is always accessible if db is open.
      // However, for safety, if we rely on cf_names, then default must be listed if used.
      return Err(StoreError::UnknownCf(cf_name.to_string()));
    }
    self
      .db
      .cf_handle(cf_name) // TransactionDB has cf_handle
      .ok_or_else(|| StoreError::UnknownCf(format!("CF '{}' configured but handle not found.", cf_name)))
  }

  /// Returns a raw Arc to the TransactionDB.
  pub fn db_txn_raw(&self) -> Arc<TransactionDB> {
    self.db.clone()
  }

  // --- Transaction Management ---
  pub fn begin_transaction(&self, write_options: Option<RocksDbWriteOptions>) -> Transaction<'_, TransactionDB> {
    let wo = write_options.or_else(|| Some(RocksDbWriteOptions::default()));
    // TransactionDBOptions for individual transaction behavior. Can be customized.
    let txn_beh_opts = TransactionOptions::default();
    self.db.transaction_opt(wo.as_ref().unwrap(), &txn_beh_opts)
  }

  pub fn execute_transaction<F, R>(&self, write_options: Option<RocksDbWriteOptions>, operation: F) -> StoreResult<R>
  where
    F: FnOnce(&Transaction<'_, TransactionDB>) -> StoreResult<R>,
  {
    let txn = self.begin_transaction(write_options);
    match operation(&txn) {
      Ok(result) => {
        txn.commit().map_err(StoreError::RocksDb)?;
        Ok(result)
      }
      Err(e) => {
        if let Err(rollback_err) = txn.rollback() {
          // Rollback if operation fails
          log::error!("Failed to rollback txn after error [{}]: {}", e, rollback_err);
        }
        Err(e)
      }
    }
  }

  // --- CF-Aware operations WITHIN a Transaction ---
  // These methods take an explicit `txn: &Transaction` argument.

  pub fn get_in_txn<'txn, K, V>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
  ) -> StoreResult<Option<V>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: DeserializeOwned + Debug,
  {
    let ser_key = serialize_key(key)?;
    let opt_bytes = if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.get_pinned(ser_key)?
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.get_pinned_cf(&handle, ser_key)?
    };
    opt_bytes.map_or(Ok(None), |val_bytes| deserialize_value(&val_bytes).map(Some))
  }

  pub fn get_raw_in_txn<'txn, K>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
  ) -> StoreResult<Option<Vec<u8>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    let ser_key = serialize_key(key)?;
    let res = if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.get_pinned(ser_key)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.get_pinned_cf(&handle, ser_key)
    };
    res.map(|opt| opt.map(|p| p.to_vec())).map_err(StoreError::RocksDb)
  }

  pub fn get_with_expiry_in_txn<'txn, K, V>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
  ) -> StoreResult<Option<ValueWithExpiry<V>>>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + DeserializeOwned + Debug,
  {
    self
      .get_raw_in_txn(txn, cf_name, key)?
      .map_or(Ok(None), |bytes| ValueWithExpiry::from_slice(&bytes).map(Some))
  }

  pub fn exists_in_txn<'txn, K>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
  ) -> StoreResult<bool>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    let ser_key = serialize_key(key)?;
    let res = if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.get_pinned(ser_key)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.get_pinned_cf(&handle, ser_key)
    };
    res.map(|opt| opt.is_some()).map_err(StoreError::RocksDb)
  }

  pub fn put_in_txn_cf<'txn, K, V>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
    value: &V,
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + Debug,
  {
    let ser_key = serialize_key(key)?;
    let ser_val = serialize_value(value)?;
    if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.put(ser_key, ser_val)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.put_cf(&handle, ser_key, ser_val)
    }
    .map_err(StoreError::RocksDb)
  }

  pub fn put_raw_in_txn<'txn, K>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
    raw_value: &[u8],
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    let ser_key = serialize_key(key)?;
    if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.put(ser_key, raw_value)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.put_cf(&handle, ser_key, raw_value)
    }
    .map_err(StoreError::RocksDb)
  }

  pub fn put_with_expiry_in_txn<'txn, K, V>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
    value: &V,
    expire_time: u64,
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    V: Serialize + DeserializeOwned + Debug,
  {
    let vwe = ValueWithExpiry::from_value(expire_time, value)?;
    self.put_raw_in_txn(txn, cf_name, key, &vwe.serialize_for_storage())
  }

  pub fn delete_in_txn<'txn, K>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    let ser_key = serialize_key(key)?;
    if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.delete(ser_key)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.delete_cf(&handle, ser_key)
    }
    .map_err(StoreError::RocksDb)
  }

  pub fn merge_in_txn<'txn, K, PatchVal>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
    merge_value: &MergeValue<PatchVal>,
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
    PatchVal: Serialize + Debug,
  {
    let ser_key = serialize_key(key)?;
    let ser_merge_op = serialize_value(merge_value)?;
    if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.merge(ser_key, ser_merge_op)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.merge_cf(&handle, ser_key, ser_merge_op)
    }
    .map_err(StoreError::RocksDb)
  }

  pub fn merge_raw_in_txn<'txn, K>(
    &self,
    txn: &'txn Transaction<'_, TransactionDB>,
    cf_name: &str,
    key: K,
    raw_merge_operand: &[u8],
  ) -> StoreResult<()>
  where
    K: AsBytes + Hash + Eq + PartialEq + Debug,
  {
    let ser_key = serialize_key(key)?;
    if cf_name == rocksdb::DEFAULT_COLUMN_FAMILY_NAME {
      txn.merge(ser_key, raw_merge_operand)
    } else {
      let handle = self.get_cf_handle(cf_name)?;
      txn.merge_cf(&handle, ser_key, raw_merge_operand)
    }
    .map_err(StoreError::RocksDb)
  }
}


implement_cf_operations_for_transactional_store!(RocksDbCFTxnStore);