use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::rc::{Rc, Weak};
use day_model::{ApplyField, Keyed, ModelId, Op, Store, announce, field_id};
use crate::{
ColumnValue, ContainerInner, DbError, DbErrorKind, DirtyRow, Model, ModelContainer, Row,
SqlType, TableName, Value, ident, key_param, value_to_handle,
};
pub struct One<M: ?Sized> {
id: Option<u64>,
_m: PhantomData<fn() -> M>,
}
impl<M: ?Sized> Clone for One<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M: ?Sized> Copy for One<M> {}
impl<M: ?Sized> Default for One<M> {
fn default() -> Self {
One {
id: None,
_m: PhantomData,
}
}
}
impl<M: ?Sized> PartialEq for One<M> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<M: ?Sized> Eq for One<M> {}
impl<M: ?Sized> std::hash::Hash for One<M> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl<M: ?Sized> std::fmt::Debug for One<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.id {
Some(h) => write!(f, "One({:?})", ModelId::<M>::from_handle(h)),
None => f.write_str("One(unset)"),
}
}
}
impl<M: ?Sized> One<M> {
pub fn to(id: impl Into<ModelId<M>>) -> Self {
One {
id: Some(id.into().handle()),
_m: PhantomData,
}
}
pub const fn none() -> Self {
One {
id: None,
_m: PhantomData,
}
}
pub fn id(self) -> Option<ModelId<M>> {
self.id.map(ModelId::from_handle)
}
pub(crate) fn from_handle(h: u64) -> Self {
One {
id: Some(h),
_m: PhantomData,
}
}
pub(crate) fn from_handle_opt(h: Option<u64>) -> Self {
One {
id: h,
_m: PhantomData,
}
}
pub(crate) fn handle_opt(self) -> Option<u64> {
self.id
}
}
impl<M: Model> ColumnValue for One<M> {
const SQL_TYPE: SqlType = M::KEY_SQL;
fn to_sqlite_value(&self) -> Value {
match self.id {
Some(h) => key_param(h),
None => Value::Null,
}
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
match value_to_handle(&v) {
Some(h) => Ok(One::from_handle(h)),
None => Err(DbError::new(
DbErrorKind::Decode,
format!("a stored reference would not read as a key: {v:?}"),
)),
}
}
}
impl<M: Model> crate::Col<One<M>> {
pub fn is(self, id: impl Into<ModelId<M>>) -> crate::Pred {
self.eq(One::<M>::from_handle(id.into().handle()))
}
pub fn is_one_of(self, ids: impl IntoIterator<Item = impl Into<ModelId<M>>>) -> crate::Pred {
self.is_in(
ids.into_iter()
.map(|i| One::<M>::from_handle(i.into().handle()))
.collect::<Vec<_>>(),
)
}
pub fn is_unset(self) -> crate::Pred {
self.is_null()
}
pub fn is_set(self) -> crate::Pred {
self.is_not_null()
}
}
impl<M: Model> crate::Col<One<M>> {
pub fn any(self, inner: crate::Pred) -> crate::Pred {
self.quantified(crate::Quant::Any, inner)
}
pub fn none(self, inner: crate::Pred) -> crate::Pred {
self.quantified(crate::Quant::None, inner)
}
fn quantified(self, quant: crate::Quant, inner: crate::Pred) -> crate::Pred {
crate::Pred::Related {
owner: self.owner,
field: self.field,
target: <M as Model>::TABLE,
quant,
inner: Box::new(inner),
}
}
}
impl<M: Model> crate::Col<Option<One<M>>> {
pub fn any(self, inner: crate::Pred) -> crate::Pred {
self.quantified(crate::Quant::Any, inner)
}
pub fn none(self, inner: crate::Pred) -> crate::Pred {
self.quantified(crate::Quant::None, inner)
}
fn quantified(self, quant: crate::Quant, inner: crate::Pred) -> crate::Pred {
crate::Pred::Related {
owner: self.owner,
field: self.field,
target: <M as Model>::TABLE,
quant,
inner: Box::new(inner),
}
}
}
impl<M: Model> crate::Col<Option<One<M>>> {
pub fn is(self, id: impl Into<ModelId<M>>) -> crate::Pred {
self.eq(Some(One::<M>::from_handle(id.into().handle())))
}
pub fn is_one_of(self, ids: impl IntoIterator<Item = impl Into<ModelId<M>>>) -> crate::Pred {
self.is_in(
ids.into_iter()
.map(|i| Some(One::<M>::from_handle(i.into().handle())))
.collect::<Vec<_>>(),
)
}
pub fn is_unset(self) -> crate::Pred {
self.is_null()
}
pub fn is_set(self) -> crate::Pred {
self.is_not_null()
}
}
pub struct Many<M: ?Sized> {
_m: PhantomData<fn() -> M>,
}
impl<M: ?Sized> Clone for Many<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M: ?Sized> Copy for Many<M> {}
impl<M: ?Sized> Default for Many<M> {
fn default() -> Self {
Many { _m: PhantomData }
}
}
impl<M: ?Sized> PartialEq for Many<M> {
fn eq(&self, _: &Self) -> bool {
true
}
}
impl<M: ?Sized> Eq for Many<M> {}
impl<M: ?Sized> std::fmt::Debug for Many<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Many")
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DeleteRule {
Nullify,
Cascade,
Deny,
}
impl DeleteRule {
pub(crate) fn sql(self) -> &'static str {
match self {
DeleteRule::Nullify => "SET NULL",
DeleteRule::Cascade => "CASCADE",
DeleteRule::Deny => "RESTRICT",
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct RelationDef {
pub field: &'static str,
pub target_table: &'static str,
pub inverse: &'static str,
pub delete: DeleteRule,
pub ordered: Option<&'static str>,
pub join: Option<&'static str>,
}
pub struct Registrar<'c> {
pub(crate) container: &'c ModelContainer,
pub(crate) error: Option<DbError>,
}
impl Registrar<'_> {
fn fail(&mut self, message: String) {
if self.error.is_none() {
self.error = Some(DbError::new(DbErrorKind::Schema, message));
}
}
}
pub(crate) struct ToOneRel {
pub(crate) container: Weak<ContainerInner>,
pub(crate) parent_store: u64,
pub(crate) parent_table: &'static str,
pub(crate) parent_field: &'static str,
pub(crate) parent_key_col: &'static str,
pub(crate) child_store: u64,
pub(crate) child_table: &'static str,
pub(crate) child_sql: String,
pub(crate) child_key_col: &'static str,
pub(crate) fk_field: &'static str,
pub(crate) fk_col: &'static str,
pub(crate) delete: DeleteRule,
pub(crate) ordered: Option<&'static str>,
pub(crate) ord_col: Option<&'static str>,
read_fk: Box<dyn Fn(u64) -> Option<u64>>,
write_fk: Box<dyn Fn(u64, Option<u64>) -> bool>,
delete_child: Box<dyn Fn(u64)>,
materialize: Materialize,
read_ord_cached: Box<dyn Fn(u64) -> Option<f64>>,
pub(crate) write_ord: Box<dyn Fn(u64, f64) -> bool>,
memo: RefCell<HashMap<u64, Vec<u64>>>,
memo_parent: RefCell<HashMap<u64, u64>>,
}
impl ToOneRel {
fn with_container<R>(&self, f: impl FnOnce(&ModelContainer) -> R) -> Option<R> {
self.container.upgrade().map(|inner| {
let c = ModelContainer { inner };
f(&c)
})
}
pub(crate) fn children_of(&self, parent: u64) -> Vec<u64> {
if self.with_container(|c| c.dirty_state_of(self.parent_store, parent))
== Some(Some(DirtyRow::Delete))
{
return Vec::new();
}
self.children_of_raw(parent)
}
fn children_of_raw(&self, parent: u64) -> Vec<u64> {
if let Some(hit) = self.memo.borrow().get(&parent) {
return hit.clone();
}
let Some(mut ids) = self.with_container(|c| {
let order = ident(self.ord_col.unwrap_or(self.child_key_col));
c.select_id_column(
&format!(
"SELECT {k} FROM {t} WHERE {fk} = ? ORDER BY {order}, {k}",
k = ident(self.child_key_col),
t = self.child_sql,
fk = ident(self.fk_col),
),
&[key_param(parent)],
)
}) else {
return Vec::new();
};
self.overlay_children(parent, &mut ids);
let mut memo = self.memo.borrow_mut();
let mut back = self.memo_parent.borrow_mut();
for id in &ids {
back.insert(*id, parent);
}
memo.insert(parent, ids.clone());
ids
}
fn overlay_children(&self, parent: u64, ids: &mut Vec<u64>) {
let Some(dirty) = self.with_container(|c| c.dirty_rows_of(self.child_store)) else {
return;
};
for (child, state) in dirty {
let is_mine = state != DirtyRow::Delete && (self.read_fk)(child) == Some(parent);
let at = ids.iter().position(|c| *c == child);
match (is_mine, at) {
(false, Some(i)) => {
ids.remove(i);
}
(true, None) => {
let ord = (self.read_ord_cached)(child).unwrap_or(f64::INFINITY);
let at = if self.ordered.is_some() {
ids.iter()
.position(|c| (self.read_ord_cached)(*c).unwrap_or(0.0) > ord)
.unwrap_or(ids.len())
} else {
ids.partition_point(|c| *c < child)
};
ids.insert(at, child);
}
_ => {}
}
}
}
fn reconciling(&self) -> bool {
self.with_container(|c| c.inner.reconciling.get())
.unwrap_or(false)
}
pub(crate) fn parent_of(&self, child: u64) -> Option<u64> {
if let Some(p) = self.memo_parent.borrow().get(&child) {
return Some(*p);
}
if let Some(p) = (self.read_fk)(child) {
return Some(p);
}
self.with_container(|c| {
c.select_id_column(
&format!(
"SELECT {fk} FROM {t} WHERE {k} = ?",
fk = ident(self.fk_col),
t = self.child_sql,
k = ident(self.child_key_col),
),
&[key_param(child)],
)
.into_iter()
.next()
})
.flatten()
}
pub(crate) fn read_order(&self, child: u64) -> f64 {
if let Some(v) = (self.read_ord_cached)(child) {
return v;
}
let Some(col) = self.ord_col else { return 0.0 };
self.with_container(|c| {
c.select_real(
&format!(
"SELECT {} FROM {t} WHERE {k} = ?",
ident(col),
t = self.child_sql,
k = ident(self.child_key_col),
),
&[key_param(child)],
)
})
.flatten()
.unwrap_or(0.0)
}
pub(crate) fn set_fk(&self, child: u64, parent: Option<u64>) -> bool {
(self.materialize)(&[child]);
(self.write_fk)(child, parent)
}
fn announce_parent(&self, parent: u64) {
announce(
&[self.parent_store, parent, field_id(self.parent_field)],
self.parent_field,
);
}
fn forget(&self, parent: u64) {
if let Some(children) = self.memo.borrow_mut().remove(&parent) {
let mut back = self.memo_parent.borrow_mut();
for c in children {
back.remove(&c);
}
}
}
pub(crate) fn invalidate_all(&self) {
self.memo.borrow_mut().clear();
self.memo_parent.borrow_mut().clear();
}
pub(crate) fn child_added(&self, child: u64) {
if let Some(p) = (self.read_fk)(child) {
self.forget(p);
self.announce_parent(p);
}
}
pub(crate) fn child_removed(&self, child: u64) {
let memo = self.memo_parent.borrow().get(&child).copied();
let parent = if memo.is_none() && self.reconciling() {
None
} else {
memo.or_else(|| self.parent_of(child))
};
if let Some(p) = parent {
self.forget(p);
self.announce_parent(p);
}
}
pub(crate) fn reparent(&self, child: u64) {
let new = (self.read_fk)(child);
let memo = self.memo_parent.borrow().get(&child).copied();
let old = if memo.is_none() && self.reconciling() {
None
} else {
memo.or_else(|| self.former_parent_file(child))
};
if let Some(p) = old {
self.forget(p);
}
if let Some(p) = new
&& Some(p) != old
{
self.forget(p);
}
self.announce_parent_set(old, new);
}
fn announce_parent_set(&self, a: Option<u64>, b: Option<u64>) {
if let Some(p) = a {
self.announce_parent(p);
}
if let Some(p) = b
&& Some(p) != a
{
self.announce_parent(p);
}
}
fn former_parent_file(&self, child: u64) -> Option<u64> {
self.with_container(|c| {
c.select_id_column(
&format!(
"SELECT {fk} FROM {t} WHERE {k} = ?",
fk = ident(self.fk_col),
t = self.child_sql,
k = ident(self.child_key_col),
),
&[key_param(child)],
)
.into_iter()
.next()
})
.flatten()
}
pub(crate) fn order_changed(&self, child: u64) {
if let Some(p) = self.parent_of(child) {
self.forget(p);
self.announce_parent(p);
}
}
pub(crate) fn parent_deleted(&self, container: &ModelContainer, parent: u64) {
if container.engine_cascade_allowed()
&& matches!(self.delete, DeleteRule::Cascade | DeleteRule::Nullify)
{
self.forget(parent);
container.note_engine_cascade(self.child_table);
let pending: Vec<u64> = container
.dirty_rows_of(self.child_store)
.into_iter()
.filter(|(c, state)| {
*state == DirtyRow::Insert && (self.read_fk)(*c) == Some(parent)
})
.map(|(c, _)| c)
.collect();
for child in pending {
match self.delete {
DeleteRule::Cascade => (self.delete_child)(child),
_ => {
(self.write_fk)(child, None);
}
}
}
return;
}
let children = self.children_of_raw(parent);
self.forget(parent);
if children.is_empty() {
return;
}
match self.delete {
DeleteRule::Cascade => {
if day_model::record_capture_active() {
(self.materialize)(&children);
}
for child in children {
(self.delete_child)(child);
}
}
DeleteRule::Nullify => {
(self.materialize)(&children);
for child in children {
(self.write_fk)(child, None);
}
}
DeleteRule::Deny => {
container.inner.error.set(Some(format!(
"deny: `{}` was deleted while `{}` rows still reference it — use \
ModelContainer::delete for deny relations",
self.parent_field, self.fk_field
)));
}
}
}
}
fn fk_of<P: Model, C: Model>(c: &C, inverse: &str) -> Option<u64> {
let any = ApplyField::read_field(c, inverse)?;
if let Some(one) = any.downcast_ref::<One<P>>() {
return one.handle_opt();
}
if let Some(opt) = any.downcast_ref::<Option<One<P>>>() {
return opt.and_then(One::handle_opt);
}
None
}
pub fn wire_to_many<P: Model, C: Model>(
reg: &mut Registrar<'_>,
field: &'static str,
inverse: &'static str,
delete: DeleteRule,
ordered: Option<&'static str>,
) {
let container = reg.container.clone();
let Some(parent_store) = container.try_cache::<P>() else {
reg.fail(format!(
"relation `{}.{field}` wired before `{}` attached",
P::TABLE,
P::TABLE
));
return;
};
let Some(child_store) = container.try_cache::<C>() else {
reg.fail(format!(
"relation `{}.{field}` targets `{}`, which is not in this container's schema!",
P::TABLE,
C::TABLE
));
return;
};
let Some(col) = C::COLUMNS.iter().find(|c| c.field == inverse) else {
reg.fail(format!(
"relation `{}.{field}` names inverse `{inverse}`, which is not a persisted \
field of `{}`",
P::TABLE,
C::TABLE
));
return;
};
if delete == DeleteRule::Nullify && col.not_null {
reg.fail(format!(
"relation `{}.{field}` is delete = \"nullify\", but `{}.{inverse}` is a required \
`One<…>` — make it `Option<One<…>>`, or use cascade/deny",
P::TABLE,
C::TABLE
));
return;
}
let mut ord_col = None;
if let Some(ord) = ordered {
if let Some(c) = C::COLUMNS
.iter()
.find(|c| c.field == ord && c.sql == SqlType::Real)
{
ord_col = Some(c.name);
} else {
reg.fail(format!(
"relation `{}.{field}` is ordered by `{ord}`, which is not a REAL (`f64`) \
field of `{}` — the order column is a real, visible field of the child",
P::TABLE,
C::TABLE
));
return;
}
}
let read_fk = Box::new(move |h: u64| {
child_store.with_untracked(|k| k.get(h).and_then(|c| fk_of::<P, C>(c, inverse)))
});
let write_fk = Box::new(move |h: u64, parent: Option<u64>| {
if child_store.write_field(h, inverse, One::<P>::from_handle_opt(parent)) {
return true;
}
let opt: Option<One<P>> = parent.map(One::<P>::from_handle);
child_store.write_field(h, inverse, opt)
});
let delete_child = Box::new(move |h: u64| {
child_store.restructure("cascade", Op::Delete, h, |k| {
k.remove(h);
});
});
let mat_container = container.clone();
let materialize = Box::new(move |keys: &[u64]| {
let _ = mat_container.ensure_resident::<C>(keys);
});
let read_ord_cached: Box<dyn Fn(u64) -> Option<f64>> = match ordered {
Some(ord) => Box::new(move |h| {
child_store.with_untracked(|k| {
k.get(h)
.and_then(|c| ApplyField::read_field(c, ord))
.and_then(|v| v.downcast_ref::<f64>().copied())
})
}),
None => Box::new(|_| None),
};
let write_ord: Box<dyn Fn(u64, f64) -> bool> = match ordered {
Some(ord) => Box::new(move |h, v| child_store.write_field(h, ord, v)),
None => Box::new(|_, _| false),
};
let rel = Rc::new(ToOneRel {
container: Rc::downgrade(&container.inner),
parent_store: parent_store.store_id(),
parent_table: P::TABLE,
parent_field: field,
parent_key_col: P::KEY,
child_store: child_store.store_id(),
child_table: C::TABLE,
child_sql: TableName::of::<C>().sql(),
child_key_col: C::KEY,
fk_field: inverse,
fk_col: col.name,
delete,
ordered,
ord_col,
read_fk,
write_fk,
delete_child,
materialize,
read_ord_cached,
write_ord,
memo: RefCell::new(HashMap::new()),
memo_parent: RefCell::new(HashMap::new()),
});
container.inner.relations.borrow_mut().push(rel);
}
#[derive(Clone, Default, PartialEq, Debug)]
pub(crate) struct JoinRow {
pub(crate) parent: u64,
pub(crate) child: u64,
pub(crate) position: f64,
}
impl day_model::Identified for JoinRow {
fn key(&self) -> day_model::Key {
day_model::Key::Pair(self.parent, self.child)
}
}
impl ApplyField for JoinRow {
fn apply_field(&mut self, label: &str, value: &dyn std::any::Any) -> bool {
match (label, value.downcast_ref::<f64>()) {
("position", Some(v)) => {
self.position = *v;
true
}
_ => false,
}
}
fn read_field(&self, label: &str) -> Option<Rc<dyn std::any::Any>> {
match label {
"position" => Some(Rc::new(self.position)),
_ => None,
}
}
}
pub(crate) struct JoinRel {
pub(crate) container: Weak<ContainerInner>,
pub(crate) join_table: &'static str,
pub(crate) join_sql: String,
pub(crate) parent_col: &'static str,
pub(crate) child_col: &'static str,
pub(crate) a_store: u64,
pub(crate) a_table: &'static str,
pub(crate) a_field: &'static str,
pub(crate) a_key_col: &'static str,
pub(crate) a_delete: DeleteRule,
pub(crate) b_store: u64,
pub(crate) b_table: &'static str,
pub(crate) b_key_col: &'static str,
pub(crate) b_field: Cell<Option<&'static str>>,
pub(crate) b_delete: Cell<DeleteRule>,
pub(crate) join_store: u64,
pub(crate) ordered: bool,
store: Store<Keyed<JoinRow>>,
delete_a: Box<dyn Fn(u64)>,
delete_b: Box<dyn Fn(u64)>,
memo_fwd: RefCell<HashMap<u64, Vec<u64>>>,
memo_rev: RefCell<HashMap<u64, Vec<u64>>>,
}
impl JoinRel {
fn with_container<R>(&self, f: impl FnOnce(&ModelContainer) -> R) -> Option<R> {
self.container.upgrade().map(|inner| {
let c = ModelContainer { inner };
f(&c)
})
}
pub(crate) fn members_of(&self, key: u64, forward: bool) -> Vec<u64> {
{
let memo = if forward {
&self.memo_fwd
} else {
&self.memo_rev
};
if let Some(hit) = memo.borrow().get(&key) {
return hit.clone();
}
}
let own_store = if forward { self.a_store } else { self.b_store };
if self.with_container(|c| c.dirty_state_of(own_store, key)) == Some(Some(DirtyRow::Delete))
{
return Vec::new();
}
self.members_of_raw(key, forward)
}
fn members_of_raw(&self, key: u64, forward: bool) -> Vec<u64> {
let (own_col, other_col) = if forward {
(self.parent_col, self.child_col)
} else {
(self.child_col, self.parent_col)
};
let (other_col, own_col) = (ident(other_col), ident(own_col));
let order = if self.ordered && forward {
format!("position, {other_col}")
} else {
other_col.clone()
};
let Some(mut ids) = self.with_container(|c| {
c.select_id_column(
&format!(
"SELECT {other_col} FROM {t} WHERE {own_col} = ? ORDER BY {order}",
t = self.join_sql,
),
&[key_param(key)],
)
}) else {
return Vec::new();
};
self.overlay_members(key, forward, &mut ids);
let memo = if forward {
&self.memo_fwd
} else {
&self.memo_rev
};
memo.borrow_mut().insert(key, ids.clone());
ids
}
fn overlay_members(&self, key: u64, forward: bool, ids: &mut Vec<u64>) {
let other_store = if forward { self.b_store } else { self.a_store };
ids.retain(|id| {
self.with_container(|c| c.dirty_state_of(other_store, *id))
!= Some(Some(DirtyRow::Delete))
});
let Some(dirty) = self.with_container(|c| c.dirty_rows_of(self.join_store)) else {
return;
};
for (handle, state) in dirty {
let Some(day_model::Key::Pair(p, c)) = day_model::Key::of_handle(handle) else {
continue;
};
let (own, other) = if forward { (p, c) } else { (c, p) };
if own != key {
continue;
}
let at = ids.iter().position(|m| *m == other);
let other_deleted = self.with_container(|c| c.dirty_state_of(other_store, other))
== Some(Some(DirtyRow::Delete));
match (state != DirtyRow::Delete && !other_deleted, at) {
(false, Some(i)) => {
ids.remove(i);
}
(true, None) => {
if self.ordered && forward {
let pos = self.position_of(p, c);
let at = ids
.iter()
.position(|m| self.position_of(key, *m) > pos)
.unwrap_or(ids.len());
ids.insert(at, other);
} else {
let at = ids.partition_point(|m| *m < other);
ids.insert(at, other);
}
}
_ => {}
}
}
}
pub(crate) fn holders_of(&self, key: u64, forward: bool) -> Vec<u64> {
self.members_of(key, !forward)
}
pub(crate) fn position_of(&self, parent: u64, child: u64) -> f64 {
let cached = self.store.with_untracked(|k| {
k.get(day_model::Key::Pair(parent, child).handle())
.map(|r| r.position)
});
if let Some(p) = cached {
return p;
}
if !self.ordered {
return 0.0;
}
self.with_container(|c| {
c.select_real(
&format!(
"SELECT position FROM {t} WHERE {pc} = ? AND {cc} = ?",
t = self.join_sql,
pc = ident(self.parent_col),
cc = ident(self.child_col),
),
&[key_param(parent), key_param(child)],
)
})
.flatten()
.unwrap_or(0.0)
}
fn is_member(&self, parent: u64, child: u64) -> bool {
let handle = day_model::Key::Pair(parent, child).handle();
if let Some(state) = self.with_container(|c| c.dirty_state_of(self.join_store, handle))
&& let Some(state) = state
{
return state != DirtyRow::Delete;
}
if self.store.with_untracked(|k| k.get(handle).is_some()) {
return true;
}
let fresh = self.with_container(|c| {
c.dirty_state_of(self.a_store, parent) == Some(DirtyRow::Insert)
|| c.dirty_state_of(self.b_store, child) == Some(DirtyRow::Insert)
});
if fresh == Some(true) {
return false;
}
self.with_container(|c| {
!c.select_id_column(
&format!(
"SELECT 1 FROM {t} WHERE {pc} = ? AND {cc} = ?",
t = self.join_sql,
pc = ident(self.parent_col),
cc = ident(self.child_col),
),
&[key_param(parent), key_param(child)],
)
.is_empty()
})
.unwrap_or(false)
}
fn announce_pair(&self, parent: u64, child: u64) {
announce(
&[self.a_store, parent, field_id(self.a_field)],
self.a_field,
);
if let Some(bf) = self.b_field.get() {
announce(&[self.b_store, child, field_id(bf)], bf);
}
}
fn forget_pair(&self, parent: u64, child: u64) {
self.memo_fwd.borrow_mut().remove(&parent);
self.memo_rev.borrow_mut().remove(&child);
}
pub(crate) fn invalidate_all(&self) {
self.memo_fwd.borrow_mut().clear();
self.memo_rev.borrow_mut().clear();
}
pub(crate) fn row_added(&self, handle: u64) {
if let Some(day_model::Key::Pair(p, c)) = day_model::Key::of_handle(handle) {
self.forget_pair(p, c);
self.announce_pair(p, c);
}
}
pub(crate) fn row_removed(&self, handle: u64) {
if let Some(day_model::Key::Pair(p, c)) = day_model::Key::of_handle(handle) {
self.forget_pair(p, c);
self.announce_pair(p, c);
}
}
pub(crate) fn row_moved(&self, handle: u64) {
if let Some(day_model::Key::Pair(p, c)) = day_model::Key::of_handle(handle) {
self.forget_pair(p, c);
self.announce_pair(p, c);
}
}
fn forget_side(&self, is_a: bool, key: u64) {
let (own, others) = if is_a {
(&self.memo_fwd, &self.memo_rev)
} else {
(&self.memo_rev, &self.memo_fwd)
};
own.borrow_mut().remove(&key);
others
.borrow_mut()
.retain(|_, members| !members.contains(&key));
}
pub(crate) fn side_deleted(&self, is_a: bool, key: u64) {
let engine = self
.with_container(super::ModelContainer::engine_cascade_allowed)
.unwrap_or(false);
let rule = if is_a {
self.a_delete
} else {
self.b_delete.get()
};
if engine {
self.forget_side(is_a, key);
self.with_container(|c| c.note_engine_cascade(self.join_table));
if rule != DeleteRule::Cascade {
return;
}
}
let others = self.members_of_raw(key, is_a);
if others.is_empty() {
return;
}
let capture = day_model::record_capture_active();
for other in others.iter().filter(|_| !engine) {
let (p, c) = if is_a { (key, *other) } else { (*other, key) };
let handle = day_model::Key::Pair(p, c).handle();
if capture && self.store.with_untracked(|k| k.get(handle).is_none()) {
let position = self.position_of(p, c);
self.store.populate(vec![JoinRow {
parent: p,
child: c,
position,
}]);
}
self.store.restructure("unlink", Op::Delete, handle, |k| {
k.remove(handle);
});
}
if rule == DeleteRule::Cascade {
for other in others {
let holders = self.holders_of(other, is_a);
let still_held = holders.iter().any(|h| *h != key);
if !still_held {
if is_a {
(self.delete_b)(other);
} else {
(self.delete_a)(other);
}
}
}
}
}
fn link(&self, parent: u64, child: u64, position: f64) -> bool {
if self.is_member(parent, child) {
return false; }
let handle = day_model::Key::Pair(parent, child).handle();
self.store.restructure("link", Op::Insert, handle, |k| {
k.push(JoinRow {
parent,
child,
position,
});
});
true
}
fn unlink(&self, parent: u64, child: u64) -> bool {
if !self.is_member(parent, child) {
return false;
}
let handle = day_model::Key::Pair(parent, child).handle();
if day_model::record_capture_active()
&& self.store.with_untracked(|k| k.get(handle).is_none())
{
let position = self.position_of(parent, child);
self.store.populate(vec![JoinRow {
parent,
child,
position,
}]);
}
self.store.restructure("unlink", Op::Delete, handle, |k| {
k.remove(handle);
});
true
}
}
pub fn wire_join<P: Model, C: Model>(
reg: &mut Registrar<'_>,
field: &'static str,
join_table: &'static str,
ordered: Option<&'static str>,
delete: DeleteRule,
) {
let container = reg.container.clone();
let (Some(parent_store), Some(child_store)) =
(container.try_cache::<P>(), container.try_cache::<C>())
else {
reg.fail(format!(
"relation `{}.{field}` targets `{}`, which is not in this container's schema!",
P::TABLE,
C::TABLE
));
return;
};
{
let joins = container.inner.joins.borrow();
if let Some(existing) = joins.iter().find(|j| j.join_table == join_table) {
let ok = existing.a_store == child_store.store_id()
&& existing.b_store == parent_store.store_id();
if !ok {
let msg = format!(
"join table `{join_table}` is declared by `{}.{field}` and by another \
relation over different models — a join table belongs to exactly one pair",
P::TABLE
);
drop(joins);
reg.fail(msg);
return;
}
if existing.b_field.get().is_some() {
let msg = format!(
"join table `{join_table}` is declared twice on `{}` — one field per side",
P::TABLE
);
drop(joins);
reg.fail(msg);
return;
}
if ordered.is_some() && !existing.ordered {
let msg = format!(
"relation `{}.{field}` asks for order, but `{join_table}`'s other side \
declared it unordered — a membership has ONE position, so order is \
declared on one side only",
P::TABLE
);
drop(joins);
reg.fail(msg);
return;
}
existing.b_field.set(Some(field));
existing.b_delete.set(delete);
return;
}
}
if ordered.is_some_and(|o| o != "position") {
reg.fail(format!(
"relation `{}.{field}` is a join relation: its order lives on the join row, so \
`ordered` takes no field name (a child can sit at different positions under \
different parents)",
P::TABLE
));
return;
}
let ordered = ordered.is_some();
let parent_col: &'static str = Box::leak(format!("{}_id", singular(P::TABLE)).into_boxed_str());
let child_col: &'static str = Box::leak(format!("{}_id", singular(C::TABLE)).into_boxed_str());
if parent_col == child_col {
reg.fail(format!(
"relation `{}.{field}`: a self-join needs distinct column names, which the \
derived `{parent_col}` pair cannot give",
P::TABLE
));
return;
}
if let Err(e) =
container.ensure_join_table::<P, C>(join_table, parent_col, child_col, ordered, delete)
{
reg.error.get_or_insert(e);
return;
}
let store = Store::new(Keyed::new(Vec::new()));
let delete_a = Box::new(move |h: u64| {
parent_store.restructure("cascade", Op::Delete, h, |k| {
k.remove(h);
});
});
let delete_b = Box::new(move |h: u64| {
child_store.restructure("cascade", Op::Delete, h, |k| {
k.remove(h);
});
});
let rel = Rc::new(JoinRel {
container: Rc::downgrade(&container.inner),
join_table,
join_sql: ident(join_table),
parent_col,
child_col,
a_store: parent_store.store_id(),
a_table: P::TABLE,
a_field: field,
a_key_col: P::KEY,
a_delete: delete,
b_store: child_store.store_id(),
b_table: C::TABLE,
b_key_col: C::KEY,
b_field: Cell::new(None),
b_delete: Cell::new(DeleteRule::Nullify),
join_store: store.store_id(),
ordered,
store,
delete_a,
delete_b,
memo_fwd: RefCell::new(HashMap::new()),
memo_rev: RefCell::new(HashMap::new()),
});
container.register_join_hooks(join_table, parent_col, child_col, ordered, store);
container.inner.joins.borrow_mut().push(rel);
}
fn singular(table: &str) -> String {
if let Some(stem) = table.strip_suffix("ies") {
return format!("{stem}y");
}
for suffix in ["ses", "xes", "zes", "ches", "shes"] {
if let Some(stem) = table.strip_suffix(suffix) {
return format!("{stem}{}", &suffix[..suffix.len() - 2]);
}
}
table.strip_suffix('s').unwrap_or(table).to_string()
}
day_reactive::tls_group! {
static CONTAINERS: RefCell<Vec<Weak<ContainerInner>>> = const { RefCell::new(Vec::new()) };
}
pub(crate) fn container_for_store(store: u64) -> Option<ModelContainer> {
CONTAINERS.with(|c| {
c.borrow()
.iter()
.filter_map(Weak::upgrade)
.find(|inner| inner.tables.borrow().contains_key(&store))
.map(|inner| ModelContainer { inner })
})
}
pub(crate) fn register_container(inner: &Rc<ContainerInner>) {
CONTAINERS.with(|c| {
let mut v = c.borrow_mut();
v.retain(|w| w.strong_count() > 0);
v.push(Rc::downgrade(inner));
});
}
enum Wired {
ToOne(Rc<ToOneRel>),
Join(Rc<JoinRel>, bool),
}
fn find_relation(parent_store: u64, field: &str) -> Option<Wired> {
CONTAINERS.with(|c| {
c.borrow()
.iter()
.filter_map(Weak::upgrade)
.find_map(|inner| {
if let Some(r) = inner
.relations
.borrow()
.iter()
.find(|r| r.parent_store == parent_store && r.parent_field == field)
{
return Some(Wired::ToOne(r.clone()));
}
inner.joins.borrow().iter().find_map(|r| {
if r.a_store == parent_store && r.a_field == field {
Some(Wired::Join(r.clone(), true))
} else if r.b_store == parent_store && r.b_field.get() == Some(field) {
Some(Wired::Join(r.clone(), false))
} else {
None
}
})
})
})
}
type Materialize = Box<dyn Fn(&[u64])>;
type ReadOrder = Box<dyn Fn(u64) -> f64>;
type WriteOrder = Box<dyn Fn(u64, f64) -> bool>;
struct Placement {
ordered: bool,
read_ord: ReadOrder,
write_ord: WriteOrder,
is_member: bool,
}
pub struct RelationRef<S: Copy + 'static, P: 'static, T: 'static> {
field: day_model::Field<S, P, Many<T>>,
}
impl<S: Copy + 'static, P: 'static, T: 'static> Clone for RelationRef<S, P, T> {
fn clone(&self) -> Self {
*self
}
}
impl<S: Copy + 'static, P: 'static, T: 'static> Copy for RelationRef<S, P, T> {}
impl<S: day_model::Source<P>, P: 'static, T: 'static> RelationRef<S, P, T> {
pub fn new(field: day_model::Field<S, P, Many<T>>) -> Self {
RelationRef { field }
}
fn resolve(&self) -> Option<(Wired, u64)> {
let mut parts = Vec::new();
day_model::Source::components(self.field, &mut parts);
if parts.len() < 2 {
return None;
}
find_relation(parts[0], self.field.label()).map(|r| (r, parts[1]))
}
fn members(&self) -> Vec<u64> {
match self.resolve() {
Some((Wired::ToOne(r), p)) => r.children_of(p),
Some((Wired::Join(r, forward), p)) => r.members_of(p, forward),
None => Vec::new(),
}
}
pub fn ids(&self) -> Vec<ModelId<T>> {
self.field.with(|_| ());
self.members()
.into_iter()
.map(ModelId::from_handle)
.collect()
}
pub fn count(&self) -> usize {
self.field.with(|_| ());
self.members().len()
}
pub fn is_empty(&self) -> bool {
self.count() == 0
}
pub fn contains(&self, id: impl Into<ModelId<T>>) -> bool {
let h = id.into().handle();
self.field.with(|_| ());
self.members().contains(&h)
}
pub fn add(&self, child: impl Into<ModelId<T>>) -> bool {
let h = child.into().handle();
match self.resolve() {
Some((Wired::ToOne(r), parent)) => {
if !r.set_fk(h, Some(parent)) {
return false;
}
if r.ordered.is_some() {
let last = r
.children_of(parent)
.iter()
.filter(|c| **c != h)
.map(|c| r.read_order(*c))
.fold(f64::NEG_INFINITY, f64::max);
let next = if last.is_finite() { last + 1.0 } else { 1.0 };
(r.write_ord)(h, next);
}
true
}
Some((Wired::Join(r, forward), key)) => {
let (a, b) = if forward { (key, h) } else { (h, key) };
let next = if r.ordered {
let last = r
.members_of(a, true)
.iter()
.map(|c| r.position_of(a, *c))
.fold(f64::NEG_INFINITY, f64::max);
if last.is_finite() { last + 1.0 } else { 1.0 }
} else {
0.0
};
r.link(a, b, next)
}
None => false,
}
}
pub fn insert_at(&self, child: impl Into<ModelId<T>>, index: usize) -> bool {
let h = child.into().handle();
let Some((wired, parent)) = self.resolve() else {
return false;
};
let Placement {
ordered,
read_ord,
write_ord,
is_member,
} = match &wired {
Wired::ToOne(r) => {
let (r1, r2) = (r.clone(), r.clone());
Placement {
ordered: r.ordered.is_some(),
read_ord: Box::new(move |c| r1.read_order(c)),
write_ord: Box::new(move |c, v| {
(r2.materialize)(&[c]);
(r2.write_ord)(c, v)
}),
is_member: r.parent_of(h) == Some(parent) && {
r.children_of(parent).contains(&h)
},
}
}
Wired::Join(r, forward) => {
let (r1, r2) = (r.clone(), r.clone());
Placement {
ordered: r.ordered && *forward,
read_ord: Box::new(move |c| r1.position_of(parent, c)),
write_ord: Box::new(move |c, v| {
let handle = day_model::Key::Pair(parent, c).handle();
if r2.store.with_untracked(|k| k.get(handle).is_none()) {
let position = r2.position_of(parent, c);
r2.store.populate(vec![JoinRow {
parent,
child: c,
position,
}]);
}
r2.store.write_field(handle, "position", v)
}),
is_member: r.members_of(parent, *forward).contains(&h),
}
}
};
if !ordered {
return false;
}
if !is_member && !self.add(ModelId::<T>::from_handle(h)) {
return false;
}
let siblings: Vec<u64> = match &wired {
Wired::ToOne(r) => r.children_of(parent),
Wired::Join(r, forward) => r.members_of(parent, *forward),
}
.into_iter()
.filter(|c| *c != h)
.collect();
let index = index.min(siblings.len());
let prev = index.checked_sub(1).and_then(|i| siblings.get(i).copied());
let next = siblings.get(index).copied();
let ord = match (prev.map(&read_ord), next.map(&read_ord)) {
(None, None) => 1.0,
(Some(a), None) => a + 1.0,
(None, Some(b)) => b - 1.0,
(Some(a), Some(b)) => {
let mid = a + (b - a) / 2.0;
if mid > a && mid < b {
mid
} else {
for (i, c) in siblings.iter().enumerate() {
write_ord(*c, (i + 1) as f64);
}
index as f64 + 0.5
}
}
};
write_ord(h, ord)
}
pub fn move_to(&self, child: impl Into<ModelId<T>>, index: usize) -> bool {
self.insert_at(child, index)
}
pub fn remove(&self, child: impl Into<ModelId<T>>) -> bool {
let h = child.into().handle();
match self.resolve() {
Some((Wired::ToOne(r), p)) => {
if !r.children_of(p).contains(&h) {
return false; }
r.set_fk(h, None)
}
Some((Wired::Join(r, forward), key)) => {
let (a, b) = if forward { (key, h) } else { (h, key) };
r.unlink(a, b)
}
None => false,
}
}
pub fn clear(&self) {
match self.resolve() {
Some((Wired::ToOne(r), p)) => {
let children = r.children_of(p);
(r.materialize)(&children);
for child in children {
(r.write_fk)(child, None);
}
}
Some((Wired::Join(r, forward), key)) => {
for other in r.members_of(key, forward) {
let (a, b) = if forward { (key, other) } else { (other, key) };
r.unlink(a, b);
}
}
None => {}
}
}
}
impl ModelContainer {
pub(crate) fn relations_on_change(&self, change: &day_model::Change) {
let Some(&store) = change.components.first() else {
return;
};
if self.inner.relations.borrow().is_empty() && self.inner.joins.borrow().is_empty() {
return;
}
let rels: Vec<Rc<ToOneRel>> = self.inner.relations.borrow().iter().cloned().collect();
for rel in rels {
if rel.child_store == store {
match (change.components.get(1).copied(), change.op) {
(Some(k), _) if k == day_model::STRUCTURE => {}
(Some(child), Op::Set) if change.components.len() >= 3 => {
if change.label == rel.fk_field {
rel.reparent(child);
} else if Some(change.label) == rel.ordered {
rel.order_changed(child);
}
}
(Some(child), Op::Insert) if change.components.len() == 2 => {
rel.child_added(child);
}
(Some(child), Op::Delete) if change.components.len() == 2 => {
rel.child_removed(child);
}
_ => {}
}
}
if rel.parent_store == store
&& change.components.len() == 2
&& change.op == Op::Delete
&& change.components[1] != day_model::STRUCTURE
{
rel.parent_deleted(self, change.components[1]);
}
}
let joins: Vec<Rc<JoinRel>> = self.inner.joins.borrow().iter().cloned().collect();
for rel in joins {
let Some(&key) = change.components.get(1) else {
continue;
};
if key == day_model::STRUCTURE {
continue;
}
if rel.join_store == store {
match change.op {
Op::Insert if change.components.len() == 2 => rel.row_added(key),
Op::Delete if change.components.len() == 2 => rel.row_removed(key),
Op::Set if change.components.len() >= 3 && change.label == "position" => {
rel.row_moved(key);
}
_ => {}
}
}
if change.op == Op::Delete && change.components.len() == 2 {
if rel.a_store == store {
rel.side_deleted(true, key);
} else if rel.b_store == store {
rel.side_deleted(false, key);
}
}
}
}
pub(crate) fn invalidate_relation_memos(&self) {
for r in self.inner.relations.borrow().iter() {
r.invalidate_all();
}
for j in self.inner.joins.borrow().iter() {
j.invalidate_all();
}
}
pub(crate) fn invalidate_relation_memos_of(&self, tables: &[&'static str]) {
for r in self.inner.relations.borrow().iter() {
if tables.contains(&r.parent_table) || tables.contains(&r.child_table) {
r.invalidate_all();
}
}
for j in self.inner.joins.borrow().iter() {
if tables.contains(&j.join_table)
|| tables.contains(&j.a_table)
|| tables.contains(&j.b_table)
{
j.invalidate_all();
}
}
}
pub fn try_cache<M: Model>(&self) -> Option<Store<Keyed<M>>> {
self.inner
.stores
.borrow()
.get(&std::any::TypeId::of::<M>())
.and_then(|a| a.downcast_ref::<Store<Keyed<M>>>())
.copied()
}
pub fn delete<M: Model>(&self, id: impl Into<ModelId<M>>) -> Result<(), DbError> {
let h = id.into().handle();
let store = self.cache::<M>();
let rels: Vec<Rc<ToOneRel>> = self.inner.relations.borrow().iter().cloned().collect();
for r in rels
.iter()
.filter(|r| r.parent_store == store.store_id() && r.delete == DeleteRule::Deny)
{
let n = r.children_of(h).len();
if n > 0 {
return Err(DbError::new(
DbErrorKind::Deny,
format!(
"`{}` row still referenced by {n} `{}` row(s) through `{}` — \
delete or reparent them first",
M::TABLE,
r.fk_field,
r.parent_field
),
));
}
}
let joins: Vec<Rc<JoinRel>> = self.inner.joins.borrow().iter().cloned().collect();
for r in joins.iter().filter(|r| {
(r.a_store == store.store_id() && r.a_delete == DeleteRule::Deny)
|| (r.b_store == store.store_id() && r.b_delete.get() == DeleteRule::Deny)
}) {
let forward = r.a_store == store.store_id();
let n = r.members_of(h, forward).len();
if n > 0 {
return Err(DbError::new(
DbErrorKind::Deny,
format!(
"`{}` row still holds {n} membership(s) through `{}` — unlink them \
first",
M::TABLE,
if forward {
r.a_field
} else {
r.b_field.get().unwrap_or(r.a_field)
}
),
));
}
}
if day_model::record_capture_active() {
let _ = self.ensure_resident::<M>(&[h]);
}
store.restructure("delete", Op::Delete, h, |k| {
k.remove(h);
});
Ok(())
}
pub(crate) fn ensure_join_table<P: Model, C: Model>(
&self,
table: &str,
parent_col: &str,
child_col: &str,
ordered: bool,
delete: DeleteRule,
) -> Result<(), DbError> {
let position = if ordered {
", position REAL NOT NULL"
} else {
""
};
let index = ident(&format!("day_idx_{table}_{child_col}"));
let (table, parent_col, child_col) = (ident(table), ident(parent_col), ident(child_col));
let body = format!(
"CREATE TABLE IF NOT EXISTS {table} (\
{parent_col} {} NOT NULL REFERENCES {}({}) ON DELETE {} DEFERRABLE INITIALLY DEFERRED, \
{child_col} {} NOT NULL REFERENCES {}({}) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED\
{position}, PRIMARY KEY ({parent_col}, {child_col}))",
P::KEY_SQL.ddl(),
TableName::of::<P>().sql(),
ident(P::KEY),
if delete == DeleteRule::Deny {
"RESTRICT"
} else {
"CASCADE"
},
C::KEY_SQL.ddl(),
TableName::of::<C>().sql(),
ident(C::KEY),
);
if self.conn().execute(&format!("{body} STRICT"), &[]).is_err() {
self.conn().execute(&body, &[])?;
}
self.conn().execute(
&format!("CREATE INDEX IF NOT EXISTS {index} ON {table}({child_col})"),
&[],
)?;
Ok(())
}
#[expect(
clippy::float_cmp,
reason = "an unchanged check: the old and the new position are compared exactly, as stored"
)]
pub(crate) fn register_join_hooks(
&self,
table: &'static str,
parent_col: &'static str,
child_col: &'static str,
ordered: bool,
store: Store<Keyed<JoinRow>>,
) {
let mut columns = vec![parent_col.to_string(), child_col.to_string()];
let mut column_names = vec![parent_col, child_col];
let mut fields = vec![parent_col.to_string(), child_col.to_string()];
if ordered {
columns.push("position".into());
column_names.push("position");
fields.push("position".into());
}
let to_row = move |r: &JoinRow| {
let mut v = vec![key_param(r.parent), key_param(r.child)];
if ordered {
v.push(Value::Real(r.position));
}
v
};
let decode = move |r: &Vec<Value>| -> Result<JoinRow, DbError> {
match (
value_to_handle(&Row::get(r, 0)),
value_to_handle(&Row::get(r, 1)),
) {
(Some(p), Some(c)) => Ok(JoinRow {
parent: p,
child: c,
position: if ordered {
Row::get(r, 2).as_real().unwrap_or(0.0)
} else {
0.0
},
}),
_ => Err(DbError::new(
DbErrorKind::Decode,
format!("`{table}` holds a membership whose ids will not read as keys"),
)),
}
};
let (pc, cc) = (ident(parent_col), ident(child_col));
let hooks = crate::TableHooks {
table,
name: TableName::plain(table),
key_cols: vec![parent_col.to_string(), child_col.to_string()],
key_where: Rc::new(move |h| match day_model::Key::of_handle(h) {
Some(day_model::Key::Pair(p, c)) => (
format!("{pc} = ? AND {cc} = ?"),
vec![key_param(p), key_param(c)],
),
_ => (format!("{pc} IS NULL"), Vec::new()),
}),
columns,
column_names,
fields,
lazy: vec![false; if ordered { 3 } else { 2 }],
settle: Rc::new(|_| Vec::new()),
fts: None,
fts_key: "rowid",
spatial: None,
row_for: Rc::new(move |h| store.with_untracked(|k| k.get(h).map(to_row))),
all_rows: Rc::new(move || {
store.with_untracked(|k| {
k.items()
.iter()
.map(|r| (day_model::Identified::handle(r), to_row(r)))
.collect()
})
}),
resident_keys: Rc::new(move || store.with_untracked(day_model::Keyed::keys)),
resident_len: Rc::new(move || store.with_untracked(day_model::Keyed::len)),
is_resident: Rc::new(move |h| store.with_untracked(|k| k.get(h).is_some())),
absorb: Rc::new(move |raw| {
let mut rows = Vec::with_capacity(raw.len());
for r in &raw {
rows.push(decode(r)?);
}
let keys = rows
.iter()
.map(day_model::Identified::handle)
.collect::<Vec<_>>();
store.populate(rows);
Ok(keys)
}),
refresh: Rc::new(move |raw| {
let mut fresh: Vec<JoinRow> = Vec::with_capacity(raw.len());
for r in &raw {
fresh.push(decode(r)?);
}
let existing: Vec<u64> = store.with_untracked(day_model::Keyed::keys);
let arriving: std::collections::HashSet<u64> =
fresh.iter().map(day_model::Identified::handle).collect();
let mut changed = false;
day_model::with_author(ModelContainer::EXTERNAL_AUTHOR, || {
for row in fresh {
let h = day_model::Identified::handle(&row);
match store.with_untracked(|k| k.get(h).cloned()) {
None => {
changed = true;
store.restructure("external", Op::Insert, h, |k| k.push(row));
}
Some(old) if ordered && old.position != row.position => {
changed = true;
store.write_field(h, "position", row.position);
}
Some(_) => {}
}
}
for gone in existing.into_iter().filter(|h| !arriving.contains(h)) {
changed = true;
store.restructure("external", Op::Delete, gone, |k| {
k.remove(gone);
});
}
});
Ok(changed)
}),
#[cfg(not(target_arch = "wasm32"))]
dispose_cache: Rc::new(move || store.dispose_contents()),
evict: Rc::new(move |candidates, want| {
let doomed: Vec<u64> = candidates
.iter()
.copied()
.filter(|h| !store.is_observed(*h))
.take(want)
.collect();
store.depopulate_many(&doomed)
}),
watch_undo: Rc::new(move |stack: &day_model::UndoStack| stack.watch(store)),
};
self.inner
.tables
.borrow_mut()
.insert(store.store_id(), hooks);
}
}