use crate::cache_mode::CacheMode;
use crate::lock_mode::LockMode;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadOptions {
pub lock_mode: LockMode,
pub cache_mode: Option<CacheMode>,
}
impl ReadOptions {
pub fn new() -> Self {
Self { lock_mode: LockMode::Default, cache_mode: None }
}
pub fn with_lock_mode(mut self, lock_mode: LockMode) -> Self {
self.lock_mode = lock_mode;
self
}
pub fn with_cache_mode(mut self, cache_mode: CacheMode) -> Self {
self.cache_mode = Some(cache_mode);
self
}
pub fn read_uncommitted() -> Self {
Self { lock_mode: LockMode::ReadUncommitted, cache_mode: None }
}
pub fn read_modify_write() -> Self {
Self { lock_mode: LockMode::Rmw, cache_mode: None }
}
pub fn evict_after_read() -> Self {
Self {
lock_mode: LockMode::Default,
cache_mode: Some(CacheMode::EvictLn),
}
}
}
impl Default for ReadOptions {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let opts = ReadOptions::new();
assert_eq!(opts.lock_mode, LockMode::Default);
assert_eq!(opts.cache_mode, None);
}
#[test]
fn test_with_lock_mode() {
let opts = ReadOptions::new().with_lock_mode(LockMode::Rmw);
assert_eq!(opts.lock_mode, LockMode::Rmw);
}
#[test]
fn test_with_cache_mode() {
let opts = ReadOptions::new().with_cache_mode(CacheMode::KeepHot);
assert_eq!(opts.cache_mode, Some(CacheMode::KeepHot));
}
#[test]
fn test_read_uncommitted() {
let opts = ReadOptions::read_uncommitted();
assert_eq!(opts.lock_mode, LockMode::ReadUncommitted);
assert_eq!(opts.cache_mode, None);
}
#[test]
fn test_read_modify_write() {
let opts = ReadOptions::read_modify_write();
assert_eq!(opts.lock_mode, LockMode::Rmw);
}
#[test]
fn test_evict_after_read() {
let opts = ReadOptions::evict_after_read();
assert_eq!(opts.cache_mode, Some(CacheMode::EvictLn));
}
#[test]
fn test_default() {
let opts = ReadOptions::default();
assert_eq!(opts.lock_mode, LockMode::Default);
assert_eq!(opts.cache_mode, None);
}
#[test]
fn test_clone() {
let opts1 = ReadOptions::read_uncommitted();
let opts2 = opts1.clone();
assert_eq!(opts1, opts2);
}
#[test]
fn test_builder_chain() {
let opts = ReadOptions::new()
.with_lock_mode(LockMode::ReadCommitted)
.with_cache_mode(CacheMode::EvictBin);
assert_eq!(opts.lock_mode, LockMode::ReadCommitted);
assert_eq!(opts.cache_mode, Some(CacheMode::EvictBin));
}
#[test]
fn test_equality() {
let opts1 = ReadOptions::new();
let opts2 = ReadOptions::default();
assert_eq!(opts1, opts2);
}
#[test]
fn test_debug() {
let opts = ReadOptions::read_uncommitted();
let debug = format!("{:?}", opts);
assert!(debug.contains("lock_mode"));
assert!(debug.contains("cache_mode"));
}
}