use super::super::{
collect_root_node_ids_from_records_into, NodeRecord, NodeSlotUpdate, SlotTable,
SlotWriteSession,
};
use crate::{AnchorId, NodeId};
impl SlotTable {
fn collect_subtree_node_records(&mut self, group_anchor: AnchorId) -> Vec<NodeRecord> {
let Some(group_index) = self.active_group_index(group_anchor) else {
log::error!(
"slot table ignored root-node collection for stale group anchor {group_anchor:?}"
);
return Vec::new();
};
let Some(subtree_range) =
self.repair_group_subtree_range_at_index(group_index, "root-node collection")
else {
log::error!(
"slot table ignored root-node collection for malformed subtree at group index {group_index}"
);
return Vec::new();
};
for index in subtree_range.as_range() {
self.repair_group_node_len_to_storage(index, "root-node collection");
}
let mut nodes = Vec::new();
for index in subtree_range.as_range() {
nodes.extend(self.group_node_records_at(index).iter().copied());
}
self.repair_group_subtree_node_count_from_storage(group_index, "root-node collection");
nodes
}
pub(in crate::slot) fn collect_subtree_root_node_ids(
&mut self,
group_anchor: AnchorId,
) -> Vec<NodeId> {
let nodes = self.collect_subtree_node_records(group_anchor);
let mut root_nodes = Vec::new();
collect_root_node_ids_from_records_into(&nodes, &mut root_nodes);
root_nodes
}
}
impl SlotWriteSession<'_> {
pub(crate) fn record_node_with_parent(
&mut self,
id: NodeId,
generation: u32,
parent_id: Option<NodeId>,
) -> NodeSlotUpdate {
let Some(frame) = self.state.group_stack.last_mut() else {
log::error!(
"slot writer record_node_with_parent called with an empty group stack; id={id}"
);
return NodeSlotUpdate::Inserted { id, generation };
};
let group_anchor = frame.group_anchor;
let result = self.table.record_node_at_cursor(
group_anchor,
frame.node_cursor,
id,
parent_id,
generation,
);
frame.advance_node_cursor();
result
}
pub(crate) fn current_node_record(&self) -> Option<(NodeId, u32)> {
let frame = self.state.group_stack.last()?;
self.table
.node_identity_at_cursor(frame.group_anchor, frame.node_cursor)
}
}