use std::array;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use bytes::Bytes as SharedBytes;
use crossbeam_utils::CachePadded;
use parking_lot::{
RwLock as FairRwLock, RwLockReadGuard as FairRwLockReadGuard,
RwLockWriteGuard as FairRwLockWriteGuard,
};
use rblock::{
RwLock as ReadBiasedRwLock, RwLockReadGuard as ReadBiasedRwLockReadGuard,
RwLockWriteGuard as ReadBiasedRwLockWriteGuard,
};
use crate::config::EvictionPolicy;
use crate::storage::embedded_store::{
EmbeddedKeyRoute, EmbeddedRouteMode, EmbeddedSessionRoute, EmbeddedShard,
assert_valid_shard_count, compute_key_route, compute_session_shard, shift_for,
};
use crate::storage::{hash_key, ttl_now_millis};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SharedEmbeddedLockPolicy {
ReadBiased,
Fair,
}
fn default_lock_policy() -> SharedEmbeddedLockPolicy {
if cfg!(feature = "shared-parking-lot-lock") {
SharedEmbeddedLockPolicy::Fair
} else {
SharedEmbeddedLockPolicy::ReadBiased
}
}
#[derive(Debug, Clone)]
pub struct SharedEmbeddedConfig {
pub total_memory_bytes: Option<usize>,
pub eviction_policy: EvictionPolicy,
pub route_mode: EmbeddedRouteMode,
pub flat_map_capacity_hint: Option<usize>,
pub lock_policy: SharedEmbeddedLockPolicy,
}
impl Default for SharedEmbeddedConfig {
fn default() -> Self {
Self {
total_memory_bytes: None,
eviction_policy: EvictionPolicy::None,
route_mode: EmbeddedRouteMode::FullKey,
flat_map_capacity_hint: None,
lock_policy: default_lock_policy(),
}
}
}
#[derive(Debug)]
pub struct SharedEmbeddedStore<const SHARDS: usize> {
inner: Arc<SharedInner<SHARDS>>,
}
#[derive(Debug)]
struct SharedInner<const SHARDS: usize> {
shards: [CachePadded<SharedShardLock<EmbeddedShard>>; SHARDS],
shift: u32,
route_mode: EmbeddedRouteMode,
}
mod core;
mod guards;
mod lock;
mod point;
mod session;
#[cfg(test)]
mod tests;
pub use guards::{Entry, Ref, RefMut, VacantEntry};
use lock::{SharedReadGuard, SharedShardLock, SharedWriteGuard};