use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::time::{Duration, Instant};
use super::kept::Kept;
use super::purge::Purge;
use super::settings::Settings;
use crate::{Error, Result};
#[derive(Clone)]
pub(crate) struct Data {
pub(crate) id: i64,
pub(crate) settings: Arc<Settings>,
pub(crate) kept: Arc<Kept>,
pub(crate) purge: Arc<Mutex<Option<Purge>>>,
}
impl std::fmt::Debug for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Data").field("id", &self.id).finish_non_exhaustive()
}
}
impl Data {
pub(crate) fn new(id: i64, settings: Settings) -> Data {
Data { id, settings: Arc::new(settings), kept: Arc::default(), purge: Arc::default() }
}
pub(crate) fn with(&self, settings: Settings) -> Data {
Data { settings: Arc::new(settings), ..self.clone() }
}
}
#[derive(Clone, Debug)]
pub(crate) struct Current {
pub(crate) serving: Data,
pub(crate) beside: Option<Beside>,
}
#[derive(Clone, Debug)]
pub(crate) struct Beside {
pub(crate) data: Data,
pub(crate) failed: bool,
}
impl Current {
pub(crate) fn written(&self) -> impl Iterator<Item = &Data> {
let beside = self.beside.as_ref().filter(|beside| !beside.failed).map(|beside| &beside.data);
std::iter::once(&self.serving).chain(beside)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Status {
Ready,
Rebuilding {
done: f64,
},
Failed {
error: String,
},
}
pub(crate) struct OpenIndex {
pub(crate) name: String,
current: RwLock<Arc<Current>>,
pub(crate) sets: AtomicU64,
pub(crate) manual: AtomicBool,
dropped: AtomicBool,
pub(crate) began: Mutex<Option<Instant>>,
settled: (Mutex<()>, Condvar),
}
impl std::fmt::Debug for OpenIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OpenIndex").field("name", &self.name).finish_non_exhaustive()
}
}
impl OpenIndex {
pub(crate) fn new(name: &str, current: Current) -> OpenIndex {
OpenIndex {
name: name.to_owned(),
current: RwLock::new(Arc::new(current)),
sets: AtomicU64::new(0),
manual: AtomicBool::new(false),
dropped: AtomicBool::new(false),
began: Mutex::new(None),
settled: (Mutex::new(()), Condvar::new()),
}
}
pub(crate) fn current(&self) -> Arc<Current> {
Arc::clone(&self.hold())
}
pub(crate) fn hold(&self) -> RwLockReadGuard<'_, Arc<Current>> {
self.current.read().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn change(&self) -> RwLockWriteGuard<'_, Arc<Current>> {
self.current.write().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn id(&self) -> i64 {
self.hold().serving.id
}
#[cfg(test)]
pub(crate) fn settings(&self) -> Arc<Settings> {
Arc::clone(&self.hold().serving.settings)
}
pub(crate) fn refuse_dropped(&self) -> Result<()> {
if self.dropped.load(Ordering::Acquire) {
return Err(Error::invalid("the index was dropped"));
}
Ok(())
}
pub(crate) fn mark_dropped(&self) {
self.dropped.store(true, Ordering::Release);
self.settle();
}
pub(crate) fn settle(&self) {
let _held = self.settled.0.lock().unwrap_or_else(PoisonError::into_inner);
self.settled.1.notify_all();
}
pub(crate) fn wait_settled(&self, span: Duration) {
let held = self.settled.0.lock().unwrap_or_else(PoisonError::into_inner);
drop(self.settled.1.wait_timeout(held, span).unwrap_or_else(PoisonError::into_inner));
}
}