use super::error::{ProtocolError, ProtocolResult};
use super::links_operations::{
group, malformed_argument, malformed_reply, named, notification_number, number, numbers_lino,
parse_number, parse_numbers, parse_restriction_of, parse_text_number, parts, restriction_lino,
LinksOperation, Part,
};
use super::protocols::MessageFormat;
use super::server::{error_document, reference_name};
use crate::triplets::{process_triplets_query, Triplet, TripletChange, TripletQuery, Triplets};
use links_notation::binary::{format_document, format_link, LinoDocument};
use links_notation::LiNo;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TripletsOperation {
Count(Vec<Part>),
Each(Vec<Part>),
Create { type_id: u32, from: u32, to: u32 },
Update {
index: u32,
type_id: u32,
from: u32,
to: u32,
},
Delete(u32),
Subscribe(Vec<Part>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TripletsNotification {
pub subscription: u32,
pub changes: Vec<TripletChange>,
}
impl TripletsOperation {
pub fn to_document(&self) -> LinoDocument {
let (name, argument) = match self {
Self::Count(restriction) => ("count", restriction_lino(restriction)),
Self::Each(restriction) => ("each", restriction_lino(restriction)),
Self::Create { type_id, from, to } => ("create", numbers_lino(&[*type_id, *from, *to])),
Self::Update {
index,
type_id,
from,
to,
} => ("update", numbers_lino(&[*index, *type_id, *from, *to])),
Self::Delete(index) => ("delete", number(*index)),
Self::Subscribe(restriction) => ("subscribe", restriction_lino(restriction)),
};
vec![named(name, vec![argument])]
}
pub fn from_document(document: &[LiNo<String>]) -> ProtocolResult<Option<Self>> {
let [LiNo::Link {
id: Some(name),
values,
}] = document
else {
return Ok(None);
};
let [argument] = values.as_slice() else {
return Ok(None);
};
let operation = match name.as_str() {
"count" => Self::Count(parse_restriction_of(argument, 4)?),
"each" => Self::Each(parse_restriction_of(argument, 4)?),
"create" => {
let [type_id, from, to] = parse_numbers::<3>(argument)?;
Self::Create { type_id, from, to }
}
"update" => {
let [index, type_id, from, to] = parse_numbers::<4>(argument)?;
Self::Update {
index,
type_id,
from,
to,
}
}
"delete" => Self::Delete(parse_number(argument)?),
"subscribe" => Self::Subscribe(parse_restriction_of(argument, 4)?),
_ => return Ok(None),
};
Ok(Some(operation))
}
pub fn execute<T: Triplets + ?Sized>(&self, triplets: &mut T) -> anyhow::Result<LinoDocument> {
Ok(match self {
Self::Count(restriction) => {
let count = matching(triplets, restriction).len();
vec![named("count", vec![number(count as u32)])]
}
Self::Each(restriction) => matching(triplets, restriction)
.iter()
.map(triplet_lino)
.collect(),
Self::Create { type_id, from, to } => {
let created = if (*type_id, *from, *to) == (0, 0, 0) {
let index = triplets.create_triplet_point()?;
Triplet::new(index, index, 0, 0)
} else {
let index = triplets.create_triplet(*type_id, *from, *to)?;
Triplet::new(index, *type_id, *from, *to)
};
triplets.save()?;
triplet_changes_document(&[TripletChange {
before: None,
after: Some(created),
}])
}
Self::Update {
index,
type_id,
from,
to,
} => {
let before = triplets.update_triplet(*index, *type_id, *from, *to)?;
triplets.save()?;
triplet_changes_document(&[TripletChange {
before: Some(before),
after: Some(Triplet::new(*index, *type_id, *from, *to)),
}])
}
Self::Delete(index) => {
let deleted = triplets.delete_triplet(*index)?;
triplets.save()?;
let changes: Vec<TripletChange> = deleted
.into_iter()
.map(|triplet| TripletChange {
before: Some(triplet),
after: None,
})
.collect();
triplet_changes_document(&changes)
}
Self::Subscribe(_) => {
anyhow::bail!("subscriptions belong to a connection to a links server")
}
})
}
}
pub fn triplet_query(restriction: &[Part]) -> TripletQuery {
let part = |at: usize| restriction.get(at).copied().flatten();
TripletQuery::new(part(0), part(1), part(2), part(3))
}
pub fn restriction_of_query(query: TripletQuery) -> Vec<Part> {
vec![query.index, query.type_id, query.from, query.to]
}
pub fn matches_triplet(triplet: &Triplet, restriction: &[Part]) -> bool {
restriction.len() <= 4 && triplet_query(restriction).matches(triplet)
}
fn matching<T: Triplets + ?Sized>(triplets: &mut T, restriction: &[Part]) -> Vec<Triplet> {
triplets.each_triplet(triplet_query(restriction))
}
pub fn execute_triplets_request<T: Triplets + ?Sized>(
triplets: &mut T,
document: &[LiNo<String>],
) -> LinoDocument {
execute_observed_triplets_request(triplets, document, &mut Vec::new())
}
pub(super) fn execute_observed_triplets_request<T: Triplets + ?Sized>(
triplets: &mut T,
document: &[LiNo<String>],
changed: &mut Vec<TripletChange>,
) -> LinoDocument {
match try_execute_triplets_request(triplets, document, changed) {
Ok(reply) => reply,
Err(error) => error_document(&format!("{error:#}")),
}
}
fn try_execute_triplets_request<T: Triplets + ?Sized>(
triplets: &mut T,
document: &[LiNo<String>],
changed: &mut Vec<TripletChange>,
) -> anyhow::Result<LinoDocument> {
if let Some(operation) = TripletsOperation::from_document(document)? {
let reply = operation.execute(triplets)?;
if matches!(
operation,
TripletsOperation::Create { .. }
| TripletsOperation::Update { .. }
| TripletsOperation::Delete(_)
) {
changed.extend(
parse_triplet_changes(&reply)?
.into_iter()
.filter(|change| change.before != change.after),
);
}
return Ok(reply);
}
if let Some(operation) = LinksOperation::from_document(document)? {
return operation.execute(triplets);
}
if document.is_empty() {
return triplets
.each_triplet(TripletQuery::any())
.iter()
.map(|triplet| named_triplet_lino(triplets, triplet))
.collect();
}
let changes = process_triplets_query(triplets, &format_document(document))?;
let writes = changes
.iter()
.filter(|change| change.before != change.after);
let before = changed.len();
changed.extend(writes.copied());
if changed.len() != before {
triplets.save()?;
}
changes
.iter()
.map(|change| {
Ok(group(vec![
named_side(triplets, change.before.as_ref())?,
named_side(triplets, change.after.as_ref())?,
]))
})
.collect()
}
fn named_triplet_lino<T: Triplets + ?Sized>(
triplets: &mut T,
triplet: &Triplet,
) -> anyhow::Result<LiNo<String>> {
let mut reference = |id: u32| -> anyhow::Result<String> { reference_name(triplets, id) };
Ok(LiNo::Link {
id: Some(reference(triplet.index)?),
values: vec![
LiNo::Ref(reference(triplet.type_id)?),
LiNo::Ref(reference(triplet.from)?),
LiNo::Ref(reference(triplet.to)?),
],
})
}
fn named_side<T: Triplets + ?Sized>(
triplets: &mut T,
triplet: Option<&Triplet>,
) -> anyhow::Result<LiNo<String>> {
Ok(group(match triplet {
Some(triplet) => vec![named_triplet_lino(triplets, triplet)?],
None => Vec::new(),
}))
}
pub fn triplet_lino(triplet: &Triplet) -> LiNo<String> {
named(
&triplet.index.to_string(),
vec![
number(triplet.type_id),
number(triplet.from),
number(triplet.to),
],
)
}
fn side_lino(triplet: Option<&Triplet>) -> LiNo<String> {
group(triplet.map(triplet_lino).into_iter().collect())
}
pub fn triplet_changes_document(changes: &[TripletChange]) -> LinoDocument {
changes
.iter()
.map(|change| {
group(vec![
side_lino(change.before.as_ref()),
side_lino(change.after.as_ref()),
])
})
.collect()
}
pub fn triplet_notification_document(subscription: u32, changes: &[TripletChange]) -> LinoDocument {
let mut document = vec![named("notify", vec![number(subscription)])];
document.extend(triplet_changes_document(changes));
document
}
pub fn parse_triplet_notification(
document: &[LiNo<String>],
) -> ProtocolResult<TripletsNotification> {
let subscription =
notification_number(document).ok_or_else(|| malformed_reply("notify", document))?;
Ok(TripletsNotification {
subscription,
changes: parse_triplet_changes(&document[1..])?,
})
}
pub fn parse_triplets(document: &[LiNo<String>]) -> ProtocolResult<Vec<Triplet>> {
document.iter().map(parse_triplet).collect()
}
pub fn parse_triplet_changes(document: &[LiNo<String>]) -> ProtocolResult<Vec<TripletChange>> {
document
.iter()
.map(|change| match parts(change) {
[before, after] => Ok(TripletChange {
before: parse_side(before)?,
after: parse_side(after)?,
}),
_ => Err(malformed_reply("change", document)),
})
.collect()
}
fn parse_side(side: &LiNo<String>) -> ProtocolResult<Option<Triplet>> {
match side {
LiNo::Link { id: None, values } if values.is_empty() => Ok(None),
LiNo::Link { id: None, values } if values.len() == 1 => parse_triplet(&values[0]).map(Some),
link @ LiNo::Link { id: Some(_), .. } => parse_triplet(link).map(Some),
_ => Err(malformed_argument("a change side", side)),
}
}
pub fn parse_triplet(link: &LiNo<String>) -> ProtocolResult<Triplet> {
let malformed = || {
ProtocolError::malformed(format!(
"expected (index: type from to), found {}",
format_link(link)
))
};
let LiNo::Link {
id: Some(index),
values,
} = link
else {
return Err(malformed());
};
let values: &[LiNo<String>] = match values.as_slice() {
[LiNo::Link { id: None, values }] => values,
values => values,
};
let [type_id, from, to] = values else {
return Err(malformed());
};
Ok(Triplet::new(
parse_text_number(index)?,
parse_number(type_id)?,
parse_number(from)?,
parse_number(to)?,
))
}
pub fn pack_triplets(document: LinoDocument, format: MessageFormat) -> LinoDocument {
match format {
MessageFormat::Binary(options)
if options.arity.contains(3) && !options.arity.contains(5) =>
{
document.into_iter().map(pack_link).collect()
}
_ => document,
}
}
fn pack_link(link: LiNo<String>) -> LiNo<String> {
match link {
LiNo::Link { id: None, values } => group(values.into_iter().map(pack_link).collect()),
LiNo::Link {
id: Some(id),
values,
} if values.len() == 3 => LiNo::Link {
id: Some(id),
values: vec![group(values)],
},
link => link,
}
}
pub fn canonical_triplets(document: LinoDocument) -> LinoDocument {
document.into_iter().map(unpack_link).collect()
}
fn unpack_link(link: LiNo<String>) -> LiNo<String> {
match link {
LiNo::Link { id: None, values } => group(values.into_iter().map(unpack_link).collect()),
LiNo::Link {
id: Some(id),
mut values,
} if matches!(
values.as_slice(),
[LiNo::Link { id: None, values }] if values.len() == 3
) =>
{
let Some(LiNo::Link { values, .. }) = values.pop() else {
unreachable!("the guard holds one unnamed link");
};
LiNo::Link {
id: Some(id),
values,
}
}
link => link,
}
}