use std::fmt;
use std::future::Future;
use bytes::Bytes;
use crate::{Error, PartInfo};
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TableName {
pub database: String,
pub table: String,
}
impl TableName {
pub fn new(database: impl Into<String>, table: impl Into<String>) -> TableName {
TableName { database: database.into(), table: table.into() }
}
pub fn quoted(&self) -> String {
format!("{}.{}", crate::stock::quote_identifier(&self.database), crate::stock::quote_identifier(&self.table))
}
}
impl fmt::Display for TableName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.database, self.table)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Outcome<T> {
Committed(T),
Duplicate,
SchemaChanged,
Fenced,
ClaimLost,
Refused(String),
}
impl<T> Outcome<T> {
pub fn committed(self) -> Option<T> {
match self {
Outcome::Committed(value) => Some(value),
_ => None,
}
}
pub fn is_committed(&self) -> bool {
matches!(self, Outcome::Committed(_))
}
pub fn name(&self) -> &'static str {
match self {
Outcome::Committed(_) => "committed",
Outcome::Duplicate => "duplicate",
Outcome::SchemaChanged => "schema changed",
Outcome::Fenced => "fenced",
Outcome::ClaimLost => "claim lost",
Outcome::Refused(_) => "refused",
}
}
}
#[derive(Clone, Debug)]
pub struct Insert<'a> {
pub table: &'a TableName,
pub format: &'a str,
pub token: &'a str,
pub data: Bytes,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommittedPart {
pub table: TableName,
pub part: PartInfo,
pub rows: u64,
pub bytes: u64,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Inserted {
pub parts: Option<Vec<CommittedPart>>,
pub counter_conflicts: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Budget {
pub max_merges: usize,
}
impl Default for Budget {
fn default() -> Budget {
Budget { max_merges: 16 }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MergeCandidate {
pub partition_id: String,
pub sources: Vec<PartInfo>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PlannedMerge {
pub candidate: MergeCandidate,
pub rows: u64,
pub bytes: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Merged {
pub part: PartInfo,
pub rows: Option<u64>,
pub bytes: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Remaining {
pub part: PartInfo,
pub reason: String,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Housekept {
pub removed: Vec<PartInfo>,
pub remaining: Vec<Remaining>,
pub temporary_directories: u64,
pub blobs_removed: u64,
pub blobs_remain: bool,
}
pub trait Writer {
fn insert(&self, insert: Insert<'_>) -> impl Future<Output = Result<Outcome<Inserted>, Error>> + Send;
fn plan(&self, table: &TableName, budget: &Budget) -> impl Future<Output = Result<Vec<PlannedMerge>, Error>> + Send;
fn merge(&self, table: &TableName, candidate: &MergeCandidate) -> impl Future<Output = Result<Outcome<Merged>, Error>> + Send;
fn housekeep(&self, table: &TableName) -> impl Future<Output = Result<Outcome<Housekept>, Error>> + Send;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_names_quote_each_part() {
let name = TableName::new("d", "ev`ents");
assert_eq!(name.quoted(), "`d`.`ev\\`ents`");
assert_eq!(name.to_string(), "d.ev`ents");
}
#[test]
fn outcomes_name_themselves_and_yield_what_they_committed() {
assert_eq!(Outcome::Committed(7).committed(), Some(7));
assert_eq!(Outcome::<u8>::Duplicate.committed(), None);
let all: [Outcome<u8>; 6] =
[Outcome::Committed(1), Outcome::Duplicate, Outcome::SchemaChanged, Outcome::Fenced, Outcome::ClaimLost, Outcome::Refused("why".into())];
let names: Vec<&str> = all.iter().map(Outcome::name).collect();
assert_eq!(names, ["committed", "duplicate", "schema changed", "fenced", "claim lost", "refused"]);
assert_eq!(all.iter().filter(|outcome| outcome.is_committed()).count(), 1);
}
}