use std::collections::BTreeMap;
use std::ops::ControlFlow;
use std::sync::Arc;
use relate_core::{PropertyMap, Result, Value};
use super::compact_props::json_len;
use super::{CompactProps, Interner, NameCache};
pub(crate) enum PayloadKeyIds<'a> {
Triple([Option<u32>; 3]),
Other(&'a CompactProps),
}
impl PayloadKeyIds<'_> {
pub(crate) fn for_each(&self, mut f: impl FnMut(u32) -> ControlFlow<()>) -> ControlFlow<()> {
match self {
Self::Triple(ids) => {
for id in ids.iter().flatten() {
f(*id)?;
}
}
Self::Other(props) => {
for (kid, _) in props.iter() {
f(kid)?;
}
}
}
ControlFlow::Continue(())
}
pub(crate) fn any(&self, mut f: impl FnMut(u32) -> bool) -> bool {
self.for_each(|kid| {
if f(kid) {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.is_break()
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Payload<P> {
Triple {
id: Arc<str>,
amount: Value,
ts: i64,
},
Other(P),
}
pub(crate) trait PayloadProps: Sized {
fn from_map(interner: &Interner, map: BTreeMap<String, Value>) -> Self;
fn set(&mut self, interner: &Interner, key: &str, value: Value);
fn remove(&mut self, interner: &Interner, key: &str);
fn get(&self, interner: &Interner, key: &str) -> Option<Value>;
fn to_map(&self, interner: &Interner) -> BTreeMap<String, Value>;
}
impl PayloadProps for BTreeMap<String, Value> {
fn from_map(_interner: &Interner, map: BTreeMap<String, Value>) -> Self {
map
}
fn set(&mut self, _interner: &Interner, key: &str, value: Value) {
self.insert(key.to_string(), value);
}
fn remove(&mut self, _interner: &Interner, key: &str) {
BTreeMap::remove(self, key);
}
fn get(&self, _interner: &Interner, key: &str) -> Option<Value> {
BTreeMap::get(self, key).cloned()
}
fn to_map(&self, _interner: &Interner) -> BTreeMap<String, Value> {
self.clone()
}
}
impl PayloadProps for CompactProps {
fn from_map(interner: &Interner, map: BTreeMap<String, Value>) -> Self {
CompactProps::from_map(interner, map)
}
fn set(&mut self, interner: &Interner, key: &str, value: Value) {
self.set(interner.intern(key), value);
}
fn remove(&mut self, interner: &Interner, key: &str) {
if let Some(kid) = interner.get_id(key) {
self.remove(kid);
}
}
fn get(&self, interner: &Interner, key: &str) -> Option<Value> {
self.get(interner.get_id(key)?).cloned()
}
fn to_map(&self, interner: &Interner) -> BTreeMap<String, Value> {
CompactProps::to_map(self, interner)
}
}
pub(crate) type RelPayload = Payload<CompactProps>;
pub(crate) type PayloadForm = Payload<BTreeMap<String, Value>>;
enum TripleWrite<'a> {
Id(&'a str),
Amount(&'a Value),
Ts(i64),
}
impl TripleWrite<'_> {
fn apply_to(self, id: &mut Arc<str>, amount: &mut Value, ts: &mut i64) {
match self {
TripleWrite::Id(s) => *id = Arc::from(s),
TripleWrite::Amount(v) => *amount = v.clone(),
TripleWrite::Ts(i) => *ts = i,
}
}
}
fn triple_write<'a>(key: &str, value: &'a Value) -> Option<TripleWrite<'a>> {
Some(match (TripleField::of(key), value) {
(Some(TripleField::Id), Value::String(s)) => TripleWrite::Id(s),
(Some(TripleField::Amount), Value::Int(_) | Value::Float(_)) => TripleWrite::Amount(value),
(Some(TripleField::Ts), Value::Int(i)) => TripleWrite::Ts(*i),
_ => return None,
})
}
#[derive(Clone, Copy)]
enum TripleField {
Amount,
Id,
Ts,
}
impl TripleField {
const ALL: [TripleField; 3] = [TripleField::Amount, TripleField::Id, TripleField::Ts];
const fn key(self) -> &'static str {
match self {
TripleField::Amount => "amount",
TripleField::Id => "id",
TripleField::Ts => "ts",
}
}
fn of(key: &str) -> Option<TripleField> {
TripleField::ALL
.into_iter()
.find(|field| field.key() == key)
}
}
pub(crate) const TRIPLE_KEYS: [&str; 3] = [
TripleField::Amount.key(),
TripleField::Id.key(),
TripleField::Ts.key(),
];
fn triple_map(id: Arc<str>, amount: Value, ts: i64) -> BTreeMap<String, Value> {
let mut map = BTreeMap::new();
map.insert(TRIPLE_KEYS[0].to_string(), amount);
map.insert(TRIPLE_KEYS[1].to_string(), Value::String(id));
map.insert(TRIPLE_KEYS[2].to_string(), Value::Int(ts));
map
}
struct TripleParts {
id: Arc<str>,
amount: Value,
ts: i64,
}
fn triple_shape<'a>(
len: usize,
mut get: impl FnMut(&str) -> Option<&'a Value>,
) -> Option<TripleParts> {
if len != 3 {
return None;
}
let id = match get(TripleField::Id.key())? {
Value::String(s) => Arc::clone(s),
_ => return None,
};
let amount = get(TripleField::Amount.key())?;
if !matches!(amount, Value::Int(_) | Value::Float(_)) {
return None;
}
let amount = amount.clone();
let ts = match get(TripleField::Ts.key())? {
Value::Int(i) => *i,
_ => return None,
};
Some(TripleParts { id, amount, ts })
}
impl<P: PayloadProps> Payload<P> {
pub(crate) fn from_map(interner: &Interner, map: BTreeMap<String, Value>) -> Self {
match triple_shape(map.len(), |key| map.get(key)) {
Some(parts) => Payload::Triple {
id: parts.id,
amount: parts.amount,
ts: parts.ts,
},
None => Payload::Other(P::from_map(interner, map)),
}
}
pub(crate) fn set(&mut self, interner: &Interner, key: &str, value: Value) {
if value.is_null() {
self.remove(interner, key);
return;
}
if let Payload::Triple { id, amount, ts } = self {
if let Some(write) = triple_write(key, &value) {
return write.apply_to(id, amount, ts);
}
self.promote(interner);
}
if let Payload::Other(props) = self {
props.set(interner, key, value);
}
}
pub(crate) fn remove(&mut self, interner: &Interner, key: &str) {
if matches!(self, Payload::Triple { .. }) {
self.promote(interner);
}
if let Payload::Other(props) = self {
props.remove(interner, key);
}
}
fn promote(&mut self, interner: &Interner) {
let Payload::Triple { id, amount, ts } = self else {
return;
};
*self = Payload::Other(P::from_map(
interner,
triple_map(
std::mem::take(id),
std::mem::replace(amount, Value::Null),
*ts,
),
));
}
pub(crate) fn to_map(&self, interner: &Interner) -> BTreeMap<String, Value> {
match self {
Payload::Triple { id, amount, ts } => triple_map(id.clone(), amount.clone(), *ts),
Payload::Other(props) => props.to_map(interner),
}
}
pub(crate) fn ts(&self, interner: &Interner) -> Option<i64> {
match self {
Payload::Triple { ts, .. } => Some(*ts),
Payload::Other(props) => match props.get(interner, TripleField::Ts.key()) {
Some(Value::Int(i)) => Some(i),
_ => None,
},
}
}
pub(crate) fn get(&self, interner: &Interner, key: &str) -> Option<Value> {
match self {
Payload::Triple { id, amount, ts } => triple_prop(id, amount, *ts, key),
Payload::Other(props) => props.get(interner, key),
}
}
}
fn triple_prop(id: &Arc<str>, amount: &Value, ts: i64, key: &str) -> Option<Value> {
match TripleField::of(key)? {
TripleField::Id => Some(Value::String(id.clone())),
TripleField::Amount => Some(amount.clone()),
TripleField::Ts => Some(Value::Int(ts)),
}
}
impl RelPayload {
pub(crate) fn get_by_id(&self, key: &str, kid: u32) -> Option<Value> {
match self {
Payload::Triple { id, amount, ts } => triple_prop(id, amount, *ts, key),
Payload::Other(props) => props.get(kid).cloned(),
}
}
}
impl Payload<CompactProps> {
pub(crate) fn from_map_ref(
interner: &Interner,
names: &mut NameCache,
map: &PropertyMap,
) -> Self {
match triple_shape(map.len(), |key| map.get(key)) {
Some(parts) => Payload::Triple {
id: parts.id,
amount: parts.amount,
ts: parts.ts,
},
None => Payload::Other(CompactProps::from_map_ref(interner, names, map)),
}
}
pub(crate) fn form(&self, interner: &Interner) -> PayloadForm {
match self {
Payload::Triple { id, amount, ts } => Payload::Triple {
id: id.clone(),
amount: amount.clone(),
ts: *ts,
},
Payload::Other(props) => Payload::Other(props.to_map(interner)),
}
}
pub(crate) fn keys(&self, interner: &Interner) -> Vec<String> {
match self {
Payload::Triple { .. } => TRIPLE_KEYS.iter().map(|k| (*k).into()).collect(),
Payload::Other(props) => props.keys_as_strings(interner),
}
}
pub(crate) fn key_ids(&self, interner: &Interner) -> PayloadKeyIds<'_> {
match self {
Payload::Triple { .. } => {
PayloadKeyIds::Triple(TRIPLE_KEYS.map(|key| interner.get_id(key)))
}
Payload::Other(props) => PayloadKeyIds::Other(props),
}
}
pub(crate) fn for_each_key_value(&self, interner: &Interner, mut f: impl FnMut(u32, &Value)) {
match self {
Payload::Triple { id, amount, ts } => {
let id_value = Value::String(id.clone());
let ts_value = Value::Int(*ts);
let pairs = [
(TRIPLE_KEYS[0], amount),
(TRIPLE_KEYS[1], &id_value),
(TRIPLE_KEYS[2], &ts_value),
];
for (k, v) in pairs {
if let Some(kid) = interner.get_id(k) {
f(kid, v);
}
}
}
Payload::Other(props) => {
for (k, v) in props.iter() {
f(k, v);
}
}
}
}
pub(crate) fn tagged_json_len(&self, interner: &Interner) -> Result<u64> {
match self {
Payload::Triple { id, amount, ts } => json_len(&BorrowedTriple {
id,
amount,
ts: *ts,
}),
Payload::Other(props) => props.tagged_json_len(interner),
}
}
}
pub(super) struct BorrowedTriple<'a> {
pub(super) id: &'a str,
pub(super) amount: &'a Value,
pub(super) ts: i64,
}
impl serde::Serialize for BorrowedTriple<'_> {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(3))?;
map.serialize_entry(TRIPLE_KEYS[0], self.amount)?;
map.serialize_entry(TRIPLE_KEYS[1], &relate_core::tagged_str(self.id))?;
map.serialize_entry(TRIPLE_KEYS[2], &Value::Int(self.ts))?;
map.end()
}
}