use std::fmt;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use super::named;
use crate::aio::{Store, run};
use crate::cancel;
use crate::kv::{Key, TxCounters};
use crate::pool::Pool;
use crate::transaction::sealed::Sealed;
use crate::{Result, Transaction, TxHandle};
#[must_use = "counters open with open()"]
pub struct CountersBuilder {
builder: crate::kv::CountersBuilder,
pool: Arc<Pool>,
}
impl Store {
pub fn counters(&self, name: &str) -> CountersBuilder {
CountersBuilder { builder: self.blocking().counters(name), pool: Arc::clone(self.pool()) }
}
}
impl CountersBuilder {
pub fn ttl(self, ttl: Duration) -> Self {
CountersBuilder { builder: self.builder.ttl(ttl), ..self }
}
pub fn flush_every(self, span: Duration) -> Self {
CountersBuilder { builder: self.builder.flush_every(span), ..self }
}
pub fn open(self) -> Result<Counters> {
Ok(Counters { counters: self.builder.open()?, pool: self.pool })
}
}
pub struct Counters {
counters: crate::kv::Counters,
pool: Arc<Pool>,
}
#[derive(Clone, Copy)]
enum Runs {
Written,
InMemory,
Copy,
}
impl Counters {
pub fn add(&self, key: impl Key, n: i64) -> impl Future<Output = Result<i64>> + Send + 'static {
let (counters, key, pool, runs) = (self.counters.clone(), key.text().into_owned(), self.pool(), self.runs());
let (tokens, what) = (cancel::captured(), named(counters.scope.shown_key(&key), "an add"));
async move {
match runs {
Runs::Written => {
run::write(&pool, what, move |answer| counters.add_then(&key, n, move |sum| answer.send(sum))).await
}
Runs::InMemory => tokens.run(None, || run::step(&pool, what, move || counters.add(&*key, n))).await,
Runs::Copy if counters.queued_on_a_copy() => {
run::blocking(&pool, None, what, move || counters.add(&*key, n)).await
}
Runs::Copy => run::passed_on(what, move || counters.add(&*key, n)).await,
}
}
}
pub fn get(&self, key: impl Key) -> impl Future<Output = Result<i64>> + Send + 'static {
let (counters, key, pool, runs) = (self.counters.clone(), key.text().into_owned(), self.pool(), self.runs());
let (tokens, what) = (cancel::captured(), named(counters.scope.shown_key(&key), "a get"));
async move {
match runs {
Runs::Written | Runs::InMemory => {
tokens.run(None, || run::point(&pool, what, move || counters.get(&*key))).await
}
Runs::Copy => tokens.run(None, || run::read(&pool, what, move || counters.get(&*key))).await,
}
}
}
pub fn delete(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'static {
let (counters, key, pool, runs) = (self.counters.clone(), key.text().into_owned(), self.pool(), self.runs());
let (tokens, what) = (cancel::captured(), named(counters.scope.shown_key(&key), "a delete"));
async move {
match runs {
Runs::Written => {
run::write(&pool, what, move |answer| counters.delete_then(&key, move |found| answer.send(found)))
.await
}
Runs::InMemory => tokens.run(None, || run::step(&pool, what, move || counters.delete(&*key))).await,
Runs::Copy => run::passed_on(what, move || counters.delete(&*key)).await,
}
}
}
pub fn clear(&self) -> impl Future<Output = Result<()>> + Send + 'static {
let (counters, pool, runs) = (self.counters.clone(), self.pool(), self.runs());
let what = named(counters.scope.shown_branch(), "a clear");
async move {
match runs {
Runs::Written => {
run::write(&pool, what, move |answer| counters.clear_then(move |cleared| answer.send(cleared)))
.await
}
Runs::InMemory => run::blocking(&pool, None, what, move || counters.clear()).await,
Runs::Copy => run::passed_on(what, move || counters.clear()).await,
}
}
}
pub fn under(&self, owner: impl Key) -> Counters {
Counters { counters: self.counters.under(owner), pool: self.pool() }
}
pub fn blocking(&self) -> &crate::kv::Counters {
&self.counters
}
fn pool(&self) -> Arc<Pool> {
Arc::clone(&self.pool)
}
fn runs(&self) -> Runs {
if self.counters.on_a_copy() {
Runs::Copy
} else if self.counters.in_memory() {
Runs::InMemory
} else {
Runs::Written
}
}
}
impl From<crate::kv::Counters> for Counters {
fn from(counters: crate::kv::Counters) -> Counters {
let pool = counters.scope.pool();
Counters { counters, pool }
}
}
impl Clone for Counters {
fn clone(&self) -> Self {
Counters { counters: self.counters.clone(), pool: self.pool() }
}
}
impl fmt::Debug for Counters {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.counters, f)
}
}
impl fmt::Debug for CountersBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
impl Sealed for Counters {}
impl TxHandle for Counters {
type InTx<'h> = TxCounters<'h>;
fn in_tx<'h>(&'h self, tx: &'h Transaction<'h>) -> TxCounters<'h> {
self.counters.in_tx(tx)
}
}
#[cfg(test)]
#[path = "counters_tests.rs"]
mod tests;