MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / rewrite_segment

Function rewrite_segment

nodedb/src/engine/array/purge/execute.rs:68–243  ·  view source on GitHub ↗

Rewrite one segment according to `action`. Returns the number of tile-versions dropped (i.e. `action.drop_tile_ids.len()`).

(store: &mut ArrayStore, action: &SegmentPurgeAction)

Source from the content-addressed store, hash-verified

66/// Rewrite one segment according to `action`. Returns the number of
67/// tile-versions dropped (i.e. `action.drop_tile_ids.len()`).
68fn rewrite_segment(store: &mut ArrayStore, action: &SegmentPurgeAction) -> Result<u64, ArrayError> {
69 let dropped_count = action.drop_tile_ids.len() as u64;
70
71 // ── Read phase: all borrows from `store` end before this function ──────────
72 // modifies `store`. Collect owned data.
73 let result = {
74 let seg_ref = store
75 .manifest()
76 .segments
77 .iter()
78 .find(|s| s.id == action.segment_id)
79 .ok_or_else(|| ArrayError::SegmentCorruption {
80 detail: format!("purge: segment {} not in manifest", action.segment_id),
81 })?
82 .clone();
83
84 let schema_hash = store.schema_hash();
85 let kek = store.kek().cloned();
86
87 let handle = store.segments.get(&action.segment_id).ok_or_else(|| {
88 ArrayError::SegmentCorruption {
89 detail: format!("purge: no open handle for segment {}", action.segment_id),
90 }
91 })?;
92
93 let reader = handle.reader();
94 let source_tiles: Vec<_> = reader.tiles().to_vec();
95
96 let surviving: Vec<TileId> = source_tiles
97 .iter()
98 .filter(|e| !action.drop_tile_ids.contains(&e.tile_id))
99 .map(|e| e.tile_id)
100 .collect();
101
102 let has_ceiling = !action.emit_ceiling_tiles.is_empty();
103 let is_empty_after_rewrite = surviving.is_empty() && !has_ceiling;
104
105 if is_empty_after_rewrite && dropped_count == 0 {
106 ReadPhaseResult::Noop
107 } else if is_empty_after_rewrite {
108 ReadPhaseResult::RemoveEntirely { seg_ref }
109 } else {
110 // Build new segment bytes from surviving tiles + ceiling tiles.
111 let mut all_output_ids: Vec<TileId> = surviving.clone();
112 for (ceiling_id, _) in &action.emit_ceiling_tiles {
113 all_output_ids.push(*ceiling_id);
114 }
115 all_output_ids.sort();
116
117 let mut writer = SegmentWriter::new(schema_hash);
118 let mut new_min_tile: Option<TileId> = None;
119 let mut new_max_tile: Option<TileId> = None;
120 let mut new_tile_count: u32 = 0;
121
122 for output_id in &all_output_ids {
123 if let Some((_cid, ceiling_tile)) = action
124 .emit_ceiling_tiles
125 .iter()

Callers 1

executeFunction · 0.85

Calls 15

update_boundsFunction · 0.85
snapshotFunction · 0.85
manifestMethod · 0.80
kekMethod · 0.80
tilesMethod · 0.80
collectMethod · 0.80
append_sparseMethod · 0.80
read_tileMethod · 0.80
allocate_segment_idMethod · 0.80
joinMethod · 0.80
rootMethod · 0.80
replace_segmentsMethod · 0.80

Tested by

no test coverage detected