use rusqlite::{OptionalExtension, params};
use super::engine::{execute, query_one};
use super::ingest::Batch;
use super::labels::encode_ids;
use super::series::Kind;
use crate::limits::Limit;
use crate::sqlite::{Tx, sql_error};
use crate::{Error, Result};
const RESOLVE: &str = "select s.id, n.kind from series s join names n on n.id = s.name where s.identity = ?1";
const SERIES_COUNT: &str = "select series_count from store_state where id = 1";
const PAIR: &str = "select id from label_values where name = ?1 and value = ?2";
const INSERT_PAIR: &str = "insert into label_values (name, value) values (?1, ?2)";
const INSERT_SERIES: &str = "insert into series (identity, name, label_ids) values (?1, ?2, ?3)";
const INSERT_POSTING: &str = "insert into postings (label_id, series_id) values (?1, ?2)";
const COUNT_POSTING: &str = "update label_values set posting_count = posting_count + 1 where id = ?1";
const COUNT_NAME_LABEL: &str = "insert into name_labels (name, label, value, series) values (?1, ?2, ?3, 1)
on conflict (name, label, value) do update set series = series + 1";
const INSERT_STATE: &str = "insert into series_state (series_id, name, oldest, max_seen) values (?1, ?2, ?3, ?3)";
const MORE_SERIES: &str = "update store_state set series_count = series_count + ?1 where id = 1";
const NAME_KIND: &str = "select kind from names where id = ?1";
const SET_KIND: &str = "update names set kind = ?2 where id = ?1";
pub(super) struct Registering {
max_series: i64,
held: Option<i64>,
registered: i64,
last: Vec<(String, String, i64)>,
}
impl Registering {
pub(super) fn new(max_series: i64) -> Registering {
Registering { max_series, held: None, registered: 0, last: Vec::new() }
}
pub(super) fn resolve(&mut self, tx: &Tx<'_>, batch: &Batch) -> Result<(i64, bool)> {
let found = tx
.prepare_cached(RESOLVE)
.and_then(|mut select| {
let row = |row: &rusqlite::Row<'_>| Ok((row.get::<_, i64>(0)?, row.get::<_, Option<String>>(1)?));
select.query_row(params![batch.identity], row).optional()
})
.map_err(|error| sql_error("find a series", error))?;
let Some((id, kind)) = found else { return self.register(tx, batch).map(|id| (id, true)) };
if kind.as_deref() != Some(batch.kind.as_str()) {
return Err(kind_refused(batch.kind, kind.as_deref()));
}
Ok((id, false))
}
pub(super) fn finish(self, tx: &Tx<'_>) -> Result<()> {
if self.registered > 0 {
execute(tx, MORE_SERIES, params![self.registered], "count the series registered")?;
}
Ok(())
}
fn register(&mut self, tx: &Tx<'_>, batch: &Batch) -> Result<i64> {
self.refuse_past_the_bound(tx)?;
settle_kind(tx, batch)?;
let mut ids = self.pairs(tx, batch.kept.pairs())?;
let encoded = encode_ids(&mut ids);
execute(tx, INSERT_SERIES, params![batch.identity, batch.name, encoded], "register a series")?;
let id = tx.last_insert_rowid();
for pair in &ids {
execute(tx, INSERT_POSTING, params![pair, id], "index a series' label")?;
execute(tx, COUNT_POSTING, params![pair], "count a label's series")?;
}
for (label, value) in batch.kept.pairs().iter().filter(|(label, _)| !super::labels::is_own(label)) {
execute(tx, COUNT_NAME_LABEL, params![batch.name, label, value], "count a name's label")?;
}
execute(tx, INSERT_STATE, params![id, batch.name, batch.points[0].at], "start a series' state")?;
self.registered += 1;
Ok(id)
}
fn refuse_past_the_bound(&mut self, tx: &Tx<'_>) -> Result<()> {
let held = match self.held {
Some(held) => held,
None => *self.held.insert(query_one(tx, SERIES_COUNT, params![], "read the count of series")?),
};
let count = held + self.registered;
if count < self.max_series {
return Ok(());
}
let max_series = self.max_series;
let refused = format!("a series past the {max_series} a store holds, its max_series");
let held = Limit { name: "series in a store", bound: max_series as u64 };
Err(held.refuse((count + 1) as u64, refused))
}
fn pairs(&mut self, tx: &Tx<'_>, pairs: &[(String, String)]) -> Result<Vec<i64>> {
let mut last = Vec::with_capacity(pairs.len());
for (label, value) in pairs {
let known = self.last.iter().find(|(known, of, _)| known == label && of == value).map(|(.., id)| *id);
let id = match known {
Some(id) => id,
None => pair(tx, label, value)?,
};
last.push((label.clone(), value.clone(), id));
}
self.last = last;
Ok(self.last.iter().map(|(.., id)| *id).collect())
}
}
fn settle_kind(tx: &Tx<'_>, batch: &Batch) -> Result<()> {
let kind: Option<String> = query_one(tx, NAME_KIND, params![batch.name], "read a name's kind")?;
match kind {
None => execute(tx, SET_KIND, params![batch.name, batch.kind.as_str()], "write a name's kind").map(drop),
Some(kind) if kind == batch.kind.as_str() => Ok(()),
Some(kind) => Err(kind_refused(batch.kind, Some(&kind))),
}
}
fn kind_refused(asked: Kind, kept: Option<&str>) -> Error {
let kept = kept.unwrap_or("none");
Error::invalid(format!("a {asked}, where the name's series are {kept}s: a name has one kind while it has series"))
}
fn pair(tx: &Tx<'_>, label: &str, value: &str) -> Result<i64> {
let found = tx
.prepare_cached(PAIR)
.and_then(|mut select| select.query_row(params![label, value], |row| row.get(0)).optional())
.map_err(|error| sql_error("find a label pair", error))?;
match found {
Some(id) => Ok(id),
None => {
execute(tx, INSERT_PAIR, params![label, value], "write a label pair")?;
Ok(tx.last_insert_rowid())
}
}
}