1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0
//! Document tree using arena-based allocation.
//!
//! This structure provides better memory locality and simpler
//! lifetime management compared to `Rc<RefCell<PageNode>}`.
use std::collections::HashMap;
use indextree::Arena;
use serde::{Deserialize, Serialize};
use super::node::{NodeId, TreeNode};
use super::structure::{DocumentStructure, StructureNode};
/// Pre-computed index for efficient retrieval operations.
///
/// Built once after the document tree is fully constructed.
/// Provides O(1) access to commonly needed traversal data.
#[derive(Debug, Clone)]
pub struct RetrievalIndex {
/// All leaf nodes in the tree.
leaves: Vec<NodeId>,
/// Nodes grouped by depth level.
/// level_index[0] = root, level_index[1] = level 1 nodes, etc.
level_index: Vec<Vec<NodeId>>,
/// Path from root to each node (inclusive).
path_cache: HashMap<NodeId, Vec<NodeId>>,
/// Siblings for each node (excluding self).
siblings_cache: HashMap<NodeId, Vec<NodeId>>,
/// Structure string to NodeId mapping.
/// e.g., "1.2.3" -> NodeId
structure_index: HashMap<String, NodeId>,
/// Page number to NodeId mapping.
/// Maps each page to the most specific (deepest) node containing it.
page_index: HashMap<usize, NodeId>,
/// NodeId to page range mapping.
node_page_range: HashMap<NodeId, (usize, usize)>,
/// Total node count.
node_count: usize,
/// Maximum depth in the tree.
max_depth: usize,
}
impl RetrievalIndex {
/// Get all leaf nodes.
pub fn leaves(&self) -> &[NodeId] {
&self.leaves
}
/// Get nodes at a specific depth level.
///
/// Returns None if the level doesn't exist.
pub fn level(&self, depth: usize) -> Option<&[NodeId]> {
self.level_index.get(depth).map(|v| v.as_slice())
}
/// Get all levels.
pub fn levels(&self) -> &[Vec<NodeId>] {
&self.level_index
}
/// Get the path from root to a node (inclusive).
///
/// Returns None if the node is not in the index.
pub fn path_to(&self, node: NodeId) -> Option<&[NodeId]> {
self.path_cache.get(&node).map(|v| v.as_slice())
}
/// Get siblings of a node (excluding the node itself).
///
/// Returns None if the node is not in the index or has no siblings.
pub fn siblings(&self, node: NodeId) -> Option<&[NodeId]> {
self.siblings_cache.get(&node).map(|v| v.as_slice())
}
/// Find a node by its structure index.
///
/// # Example
/// ```ignore
/// // Find section 2.1.3
/// let node = index.find_by_structure("2.1.3");
/// ```
pub fn find_by_structure(&self, structure: &str) -> Option<NodeId> {
self.structure_index.get(structure).copied()
}
/// Find the most specific node containing a page number.
///
/// Returns the deepest node whose page range contains the given page.
pub fn find_by_page(&self, page: usize) -> Option<NodeId> {
self.page_index.get(&page).copied()
}
/// Find all nodes whose page range overlaps with the given range.
///
/// This is useful for retrieving all content that spans a range of pages.
///
/// # Example
/// ```ignore
/// // Find all nodes covering pages 10-15
/// let nodes = index.find_nodes_by_page_range(10, 15);
/// ```
pub fn find_nodes_by_page_range(&self, start: usize, end: usize) -> Vec<NodeId> {
let mut result = Vec::new();
for (&node_id, &(node_start, node_end)) in &self.node_page_range {
// Check if ranges overlap: node_start <= end && start <= node_end
if node_start <= end && start <= node_end {
result.push(node_id);
}
}
// Sort by start page for consistent ordering
result.sort_by_key(|&id| self.node_page_range.get(&id).map(|(s, _)| *s).unwrap_or(0));
result
}
/// Get all page numbers covered by a node.
///
/// Returns None if the node has no page information.
pub fn get_pages_for_node(&self, node: NodeId) -> Option<Vec<usize>> {
let (start, end) = self.node_page_range.get(&node)?;
Some((*start..=*end).collect())
}
/// Get the page range for a node.
pub fn page_range(&self, node: NodeId) -> Option<(usize, usize)> {
self.node_page_range.get(&node).copied()
}
/// Get all nodes that are leaves within a page range.
///
/// This returns only leaf nodes (nodes with no children) that
/// overlap with the given page range.
pub fn find_leaves_by_page_range(&self, start: usize, end: usize) -> Vec<NodeId> {
let leaves_set: std::collections::HashSet<NodeId> = self.leaves.iter().copied().collect();
self.find_nodes_by_page_range(start, end)
.into_iter()
.filter(|id| leaves_set.contains(id))
.collect()
}
/// Get the total number of pages in the document.
pub fn total_pages(&self) -> usize {
self.node_page_range
.values()
.map(|(_, end)| *end)
.max()
.unwrap_or(0)
}
/// Get all structure indices.
pub fn structures(&self) -> &HashMap<String, NodeId> {
&self.structure_index
}
/// Get the total number of nodes.
pub fn node_count(&self) -> usize {
self.node_count
}
/// Get the maximum depth in the tree.
pub fn max_depth(&self) -> usize {
self.max_depth
}
/// Get the number of levels.
pub fn level_count(&self) -> usize {
self.level_index.len()
}
}
/// A hierarchical document tree structure.
///
/// Uses an arena-based tree representation for efficient traversal
/// and node manipulation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentTree {
/// The underlying arena storing all nodes.
arena: Arena<TreeNode>,
/// The root node ID.
root_id: NodeId,
/// Cached leaf nodes (rebuilt on demand).
#[serde(skip)]
leaves_cache: Option<Vec<NodeId>>,
}
impl DocumentTree {
/// Create a new document tree with a root node.
pub fn new(title: &str, content: &str) -> Self {
let mut arena = Arena::new();
let root_data = TreeNode {
title: title.to_string(),
structure: String::new(), // Root has no structure index
content: content.to_string(),
summary: String::new(),
depth: 0,
start_index: 1,
end_index: 1,
start_page: None,
end_page: None,
node_id: None,
physical_index: None,
token_count: None,
references: Vec::new(),
routing_keywords: Vec::new(),
question_hints: Vec::new(),
};
let root_id = arena.new_node(root_data);
// Root is initially a leaf
let leaves_cache = Some(vec![NodeId(root_id)]);
Self {
arena,
root_id: NodeId(root_id),
leaves_cache,
}
}
/// Create a document tree from an existing arena and root ID.
///
/// This is useful for deserialization and testing.
pub fn from_raw(arena: Arena<TreeNode>, root_id: NodeId) -> Self {
Self {
arena,
root_id,
leaves_cache: None, // Will be rebuilt on demand
}
}
/// Get the root node ID.
pub fn root(&self) -> NodeId {
self.root_id
}
/// Get a reference to the underlying arena.
pub fn arena(&self) -> &Arena<TreeNode> {
&self.arena
}
/// Get a node by its ID.
///
/// Returns None if the node doesn't exist.
pub fn get(&self, id: NodeId) -> Option<&TreeNode> {
self.arena.get(id.0).map(|n| n.get())
}
/// Get a mutable reference to a node by its ID.
///
/// Returns None if the node doesn't exist.
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut TreeNode> {
self.arena.get_mut(id.0).map(|n| n.get_mut())
}
/// Add a child node to the specified parent.
///
/// Returns the ID of the newly created child node.
/// The structure is automatically calculated based on siblings.
pub fn add_child(&mut self, parent: NodeId, title: &str, content: &str) -> NodeId {
let parent_depth = self.arena.get(parent.0).map(|n| n.get().depth).unwrap_or(0);
let parent_structure = self
.arena
.get(parent.0)
.map(|n| n.get().structure.clone())
.unwrap_or_default();
// Calculate child index (1-based)
let child_index = parent.0.children(&self.arena).count() + 1;
// Calculate structure: parent_structure.child_index
let child_structure = if parent_structure.is_empty() {
child_index.to_string()
} else {
format!("{}.{}", parent_structure, child_index)
};
let child_data = TreeNode {
title: title.to_string(),
structure: child_structure,
content: content.to_string(),
summary: String::new(),
depth: parent_depth + 1,
start_index: 1,
end_index: 1,
start_page: None,
end_page: None,
node_id: None,
physical_index: None,
token_count: None,
references: Vec::new(),
routing_keywords: Vec::new(),
question_hints: Vec::new(),
};
let child_id = self.arena.new_node(child_data);
parent.0.append(child_id, &mut self.arena);
// Update leaves cache
if let Some(ref mut cache) = self.leaves_cache {
// Remove parent from leaves (it's no longer a leaf)
cache.retain(|&id| id != parent);
// Add child to leaves
cache.push(NodeId(child_id));
}
NodeId(child_id)
}
/// Add a child node with page boundaries.
///
/// Returns the ID of the newly created child node.
pub fn add_child_with_pages(
&mut self,
parent: NodeId,
title: &str,
content: &str,
start_page: usize,
end_page: usize,
) -> NodeId {
let child_id = self.add_child(parent, title, content);
if let Some(node) = self.get_mut(child_id) {
node.start_page = Some(start_page);
node.end_page = Some(end_page);
}
child_id
}
/// Check if a node is a leaf (has no children).
pub fn is_leaf(&self, id: NodeId) -> bool {
id.0.children(&self.arena).next().is_none()
}
/// Get the number of children of a node.
///
/// This is more efficient than `children().len()` as it doesn't allocate.
pub fn child_count(&self, id: NodeId) -> usize {
id.0.children(&self.arena).count()
}
/// Get the children of a node as an iterator.
///
/// Use this instead of `children()` when you only need to iterate,
/// as it avoids allocating a Vec.
pub fn children_iter(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
id.0.children(&self.arena).map(NodeId)
}
/// Get the children of a node.
///
/// Returns a Vec for cases where you need owned access to the children.
/// Consider using `children_iter()` if you only need to iterate.
pub fn children(&self, id: NodeId) -> Vec<NodeId> {
self.children_iter(id).collect()
}
/// Get the children of a node plus any resolved cross-reference targets.
///
/// In addition to direct children, this collects `NodeId`s pointed to by
/// resolved references (`node.references[i].target_node`) on the given node.
/// Duplicate node IDs (e.g. a reference that happens to be a child) are
/// de-duplicated so the caller never sees the same node twice.
pub fn children_with_refs(&self, id: NodeId) -> Vec<NodeId> {
let mut result: Vec<NodeId> = self.children_iter(id).collect();
if let Some(node) = self.get(id) {
for r#ref in &node.references {
if let Some(target) = r#ref.target_node {
if !result.contains(&target) {
result.push(target);
}
}
}
}
result
}
/// Get the parent of a node.
///
/// Returns None if the node is the root or doesn't have a parent.
pub fn parent(&self, id: NodeId) -> Option<NodeId> {
id.0.parent(&self.arena).map(NodeId)
}
/// Get the siblings of a node (excluding the node itself).
///
/// Returns an empty iterator for the root node.
pub fn siblings_iter(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
id.0.preceding_siblings(&self.arena)
.chain(id.0.following_siblings(&self.arena))
.map(NodeId)
}
/// Get the ancestors of a node from parent to root.
///
/// Returns an empty iterator for the root node.
pub fn ancestors_iter(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
id.0.ancestors(&self.arena).map(NodeId)
}
/// Get the path from root to a node (inclusive).
///
/// Returns the path as a Vec starting from the root.
pub fn path_from_root(&self, id: NodeId) -> Vec<NodeId> {
let mut path: Vec<NodeId> = self.ancestors_iter(id).collect();
path.reverse();
path.push(id);
path
}
/// Get the depth of a node (root = 0).
pub fn depth(&self, id: NodeId) -> usize {
self.get(id).map(|n| n.depth).unwrap_or(0)
}
/// Get the maximum depth of any node in the tree (root = 0, leaf ≥ 0).
///
/// Uses a single BFS pass. Returns 0 for a single-node tree.
pub fn max_depth(&self) -> usize {
let mut max_d = 0;
let mut stack = vec![(self.root_id, 0usize)];
while let Some((id, d)) = stack.pop() {
max_d = max_d.max(d);
for child in self.children_iter(id) {
stack.push((child, d + 1));
}
}
max_d
}
/// Get the first child of a node.
///
/// Returns None if the node has no children.
pub fn first_child(&self, id: NodeId) -> Option<NodeId> {
self.children_iter(id).next()
}
/// Get the last child of a node.
///
/// Returns None if the node has no children.
pub fn last_child(&self, id: NodeId) -> Option<NodeId> {
self.children_iter(id).last()
}
/// Get all leaf nodes in the tree.
///
/// Uses cached leaves if available, otherwise rebuilds the cache.
pub fn leaves(&self) -> Vec<NodeId> {
if let Some(ref cache) = self.leaves_cache {
return cache.clone();
}
// Rebuild cache on demand
let leaves: Vec<NodeId> = self
.traverse()
.into_iter()
.filter(|id| self.is_leaf(*id))
.collect();
// Note: Can't mutate self here, caller should use rebuild_leaves_cache()
leaves
}
/// Rebuild the leaves cache.
///
/// Call this after deserialization or batch modifications.
pub fn rebuild_leaves_cache(&mut self) {
self.leaves_cache = Some(
self.traverse()
.into_iter()
.filter(|id| self.is_leaf(*id))
.collect(),
);
}
/// Invalidate the leaves cache.
///
/// Called automatically by mutation methods.
pub fn invalidate_leaves_cache(&mut self) {
self.leaves_cache = None;
}
/// Get all nodes in the tree (depth-first order).
pub fn traverse(&self) -> Vec<NodeId> {
let mut result = Vec::new();
let mut stack = vec![self.root_id];
while let Some(id) = stack.pop() {
result.push(id);
// Add children in reverse order for correct DFS order
let mut children: Vec<_> = self.children(id).into_iter().collect();
children.reverse();
stack.extend(children);
}
result
}
/// Get the number of nodes in the tree.
pub fn node_count(&self) -> usize {
self.arena.count()
}
/// Update a node's summary.
pub fn set_summary(&mut self, id: NodeId, summary: &str) {
if let Some(node) = self.get_mut(id) {
node.summary = summary.to_string();
}
}
/// Update a node's content.
pub fn set_content(&mut self, id: NodeId, content: &str) {
if let Some(node) = self.get_mut(id) {
node.content = content.to_string();
}
}
/// Update a node's structure index.
pub fn set_structure(&mut self, id: NodeId, structure: &str) {
if let Some(node) = self.get_mut(id) {
node.structure = structure.to_string();
}
}
/// Set page boundaries for a node.
pub fn set_page_boundaries(&mut self, id: NodeId, start: usize, end: usize) {
if let Some(node) = self.get_mut(id) {
node.start_page = Some(start);
node.end_page = Some(end);
}
}
/// Set line indices for a node.
pub fn set_line_indices(&mut self, id: NodeId, start: usize, end: usize) {
if let Some(node) = self.get_mut(id) {
node.start_index = start;
node.end_index = end;
}
}
/// Get page range for a node.
pub fn page_range(&self, id: NodeId) -> Option<(usize, usize)> {
let node = self.get(id)?;
match (node.start_page, node.end_page) {
(Some(start), Some(end)) => Some((start, end)),
_ => None,
}
}
/// Check if a node contains a specific page.
pub fn contains_page(&self, id: NodeId, page: usize) -> bool {
if let Some((start, end)) = self.page_range(id) {
page >= start && page <= end
} else {
false
}
}
/// Find a node by its structure index.
///
/// This is a convenience method that builds an index if needed.
/// For repeated queries, build a RetrievalIndex once.
pub fn find_by_structure(&self, structure: &str) -> Option<NodeId> {
// Linear search - for repeated use, build RetrievalIndex
for node_id in self.traverse() {
if let Some(node) = self.get(node_id) {
if node.structure == structure {
return Some(node_id);
}
}
}
None
}
/// Find the most specific node containing a page.
///
/// This is a convenience method that builds an index if needed.
/// For repeated queries, build a RetrievalIndex once.
pub fn find_by_page(&self, page: usize) -> Option<NodeId> {
let mut best_match: Option<(NodeId, usize)> = None;
// Find the deepest node containing this page
for node_id in self.traverse() {
if let Some((start, end)) = self.page_range(node_id) {
if page >= start && page <= end {
let depth = self.get(node_id).map(|n| n.depth).unwrap_or(0);
match &best_match {
None => best_match = Some((node_id, depth)),
Some((_, best_depth)) if depth > *best_depth => {
best_match = Some((node_id, depth));
}
_ => {}
}
}
}
}
best_match.map(|(id, _)| id)
}
/// Get all nodes whose page range overlaps with the given range.
pub fn find_nodes_by_page_range(&self, start: usize, end: usize) -> Vec<NodeId> {
self.traverse()
.into_iter()
.filter(|&id| {
if let Some((node_start, node_end)) = self.page_range(id) {
node_start <= end && start <= node_end
} else {
false
}
})
.collect()
}
/// Set the node ID (identifier string).
pub fn set_node_id(&mut self, id: NodeId, node_id: &str) {
if let Some(node) = self.get_mut(id) {
node.node_id = Some(node_id.to_string());
}
}
/// Set the physical index marker.
pub fn set_physical_index(&mut self, id: NodeId, index: &str) {
if let Some(node) = self.get_mut(id) {
node.physical_index = Some(index.to_string());
}
}
/// Update token count for a node.
pub fn set_token_count(&mut self, id: NodeId, count: usize) {
if let Some(node) = self.get_mut(id) {
node.token_count = Some(count);
}
}
/// Set the references for a node.
pub fn set_references(&mut self, id: NodeId, references: Vec<super::reference::NodeReference>) {
if let Some(node) = self.get_mut(id) {
node.references = references;
}
}
/// Export the tree structure to JSON format.
pub fn to_structure_json(&self, doc_name: &str) -> DocumentStructure {
let structure = self.build_structure_nodes(self.root_id);
DocumentStructure {
doc_name: doc_name.to_string(),
structure,
}
}
/// Build a retrieval index for efficient operations.
///
/// This should be called once after the tree is fully constructed.
/// The index provides O(1) access to commonly needed traversal data.
///
/// # Example
///
/// ```ignore
/// let tree = /* build tree */;
/// let index = tree.build_retrieval_index();
///
/// // Fast access to leaves
/// for leaf in index.leaves() {
/// // process leaf
/// }
///
/// // Fast path lookup
/// if let Some(path) = index.path_to(node_id) {
/// // path[0] = root, path[-1] = node_id
/// }
///
/// // Fast structure lookup
/// if let Some(node) = index.find_by_structure("2.1.3") {
/// // Found section 2.1.3
/// }
///
/// // Fast page lookup
/// if let Some(node) = index.find_by_page(42) {
/// // Found node containing page 42
/// }
/// ```
pub fn build_retrieval_index(&self) -> RetrievalIndex {
let mut leaves = Vec::new();
let mut level_index: Vec<Vec<NodeId>> = Vec::new();
let mut path_cache: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
let mut siblings_cache: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
let mut structure_index: HashMap<String, NodeId> = HashMap::new();
let mut page_index: HashMap<usize, NodeId> = HashMap::new();
let mut node_page_range: HashMap<NodeId, (usize, usize)> = HashMap::new();
let mut max_depth = 0;
let node_count = self.node_count();
// BFS to build level index
let mut current_level = vec![self.root_id];
// Initialize root path
path_cache.insert(self.root_id, vec![self.root_id]);
while !current_level.is_empty() {
level_index.push(current_level.clone());
let mut next_level = Vec::new();
for &node_id in ¤t_level {
let children: Vec<NodeId> = self.children(node_id);
// Get node data
if let Some(node) = self.get(node_id) {
max_depth = max_depth.max(node.depth);
// Build structure index
if !node.structure.is_empty() {
structure_index.insert(node.structure.clone(), node_id);
}
// Build page index and page range
if let (Some(start), Some(end)) = (node.start_page, node.end_page) {
node_page_range.insert(node_id, (start, end));
// Map each page to this node (will be overwritten by deeper nodes)
for page in start..=end {
page_index.insert(page, node_id);
}
}
}
// Check if leaf
if children.is_empty() {
leaves.push(node_id);
}
// Build siblings cache for children
if children.len() > 1 {
for (i, &child) in children.iter().enumerate() {
let siblings: Vec<NodeId> = children
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.map(|(_, &c)| c)
.collect();
siblings_cache.insert(child, siblings);
}
}
// Build path cache for children
if let Some(parent_path) = path_cache.get(&node_id).cloned() {
for &child in &children {
let mut child_path = parent_path.clone();
child_path.push(child);
path_cache.insert(child, child_path);
}
}
next_level.extend(children);
}
current_level = next_level;
}
RetrievalIndex {
leaves,
level_index,
path_cache,
siblings_cache,
structure_index,
page_index,
node_page_range,
node_count,
max_depth,
}
}
/// Recursively build structure nodes starting from the given node.
fn build_structure_nodes(&self, node_id: NodeId) -> Vec<StructureNode> {
let children = self.children(node_id);
children
.into_iter()
.enumerate()
.map(|(idx, child_id)| self.node_to_structure(child_id, idx))
.collect()
}
/// Convert a single node to StructureNode format.
fn node_to_structure(&self, node_id: NodeId, _idx: usize) -> StructureNode {
let node = self.get(node_id).cloned().unwrap_or_default();
let children = self.children(node_id);
StructureNode {
title: node.title,
node_id: node
.node_id
.clone()
.unwrap_or_else(|| format!("{:04}", _idx)),
start_index: node.start_index,
end_index: node.end_index,
summary: if node.summary.is_empty() {
None
} else {
Some(node.summary)
},
nodes: children
.into_iter()
.enumerate()
.map(|(i, c)| self.node_to_structure(c, i))
.collect(),
}
}
}
impl Default for DocumentTree {
fn default() -> Self {
Self::new("Root", "")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::reference::{NodeReference, RefType};
#[test]
fn test_children_with_refs_no_references() {
let mut tree = DocumentTree::new("Root", "root content");
let child1 = tree.add_child(tree.root(), "Section 1", "content 1");
let child2 = tree.add_child(tree.root(), "Section 2", "content 2");
let children = tree.children_with_refs(tree.root());
assert_eq!(children.len(), 2);
assert!(children.contains(&child1));
assert!(children.contains(&child2));
}
#[test]
fn test_children_with_refs_deduplicates() {
let mut tree = DocumentTree::new("Root", "root content");
let child = tree.add_child(tree.root(), "Section 1", "content 1");
// Add a reference that points to the same node as an existing child
let refs = vec![NodeReference::resolved(
"see Section 1".to_string(),
"1".to_string(),
RefType::Section,
5,
child,
0.8,
)];
tree.set_references(tree.root(), refs);
let children = tree.children_with_refs(tree.root());
// Should not duplicate
assert_eq!(children.len(), 1);
assert!(children.contains(&child));
}
#[test]
fn test_children_with_refs_unresolved_ignored() {
let mut tree = DocumentTree::new("Root", "root content");
let child = tree.add_child(tree.root(), "Section 1", "content 1");
// Add an unresolved reference (target_node = None)
let refs = vec![NodeReference::new(
"see Section 5".to_string(),
"5".to_string(),
RefType::Section,
5,
)];
tree.set_references(tree.root(), refs);
let children = tree.children_with_refs(tree.root());
// Unresolved reference should not be included
assert_eq!(children.len(), 1);
assert!(children.contains(&child));
}
}