#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RocksDbBatchOperation {
Put {
cf: String,
key: Vec<u8>,
value: Vec<u8>,
},
Delete {
cf: String,
key: Vec<u8>,
},
DeleteRange {
cf: String,
start_key: Vec<u8>,
end_key: Vec<u8>,
},
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RocksDbWriteBatch {
operations: Vec<RocksDbBatchOperation>,
}
impl RocksDbWriteBatch {
pub fn with_capacity(capacity: usize) -> Self {
Self {
operations: Vec::with_capacity(capacity),
}
}
pub fn put_cf(&mut self, cf: impl Into<String>, key: impl Into<Vec<u8>>, value: impl Into<Vec<u8>>) {
self.operations.push(RocksDbBatchOperation::Put {
cf: cf.into(),
key: key.into(),
value: value.into(),
});
}
pub fn delete_cf(&mut self, cf: impl Into<String>, key: impl Into<Vec<u8>>) {
self.operations.push(RocksDbBatchOperation::Delete {
cf: cf.into(),
key: key.into(),
});
}
pub fn delete_range_cf(
&mut self,
cf: impl Into<String>,
start_key: impl Into<Vec<u8>>,
end_key: impl Into<Vec<u8>>,
) {
self.operations.push(RocksDbBatchOperation::DeleteRange {
cf: cf.into(),
start_key: start_key.into(),
end_key: end_key.into(),
});
}
pub fn operations(&self) -> &[RocksDbBatchOperation] {
&self.operations
}
pub fn is_empty(&self) -> bool {
self.operations.is_empty()
}
}