use crate::backend::native::adjacency::Direction;
use crate::backend::native::types::{NativeBackendError, NativeResult, NativeNodeId};
use crate::backend::native::GraphFile;
pub fn delete_cluster(
graph_file: &mut GraphFile,
node_id: NativeNodeId,
direction: Direction,
) -> NativeResult<()> {
use crate::backend::native::node_store::NodeStore;
let mut node_store = NodeStore::new(graph_file);
let mut node = node_store.read_node_v2(node_id)?;
drop(node_store);
let (cluster_offset, cluster_size) = match direction {
Direction::Outgoing => (node.outgoing_cluster_offset, node.outgoing_cluster_size),
Direction::Incoming => (node.incoming_cluster_offset, node.incoming_cluster_size),
};
if cluster_size > 0 && cluster_offset > 0 {
mark_region_free(graph_file, cluster_offset, cluster_size as u64)?;
}
match direction {
Direction::Outgoing => {
node.outgoing_edge_count = 0;
node.outgoing_cluster_offset = 0;
node.outgoing_cluster_size = 0;
}
Direction::Incoming => {
node.incoming_edge_count = 0;
node.incoming_cluster_offset = 0;
node.incoming_cluster_size = 0;
}
}
let mut node_store = NodeStore::new(graph_file);
node_store.write_node_v2(&node)?;
Ok(())
}
fn mark_region_free(graph_file: &mut GraphFile, offset: u64, size: u64) -> NativeResult<()> {
let zero_buffer = vec![0u8; size as usize];
graph_file.write_bytes(offset, &zero_buffer)?;
graph_file.flush()?;
Ok(())
}
pub fn remove_back_references(
graph_file: &mut GraphFile,
deleted_node_id: NativeNodeId,
) -> NativeResult<usize> {
use crate::backend::native::edge_store::EdgeStore;
let mut removed_count = 0;
let edge_count = graph_file.header().edge_count as i64;
for edge_id in 1..=edge_count {
let mut edge_store = EdgeStore::new(graph_file);
if let Ok(edge) = edge_store.read_edge(edge_id) {
if edge.to_id == deleted_node_id {
let _ = edge_store.delete_edge(edge_id);
removed_count += 1;
}
}
}
Ok(removed_count)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_module_exists() {
assert!(true);
}
}