graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
Documentation
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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Streaming Top-K - Memory-efficient ORDER BY + LIMIT (Phase 4: Week 6.5)
//!
//! Maintains only top-K results using a min-heap instead of sorting entire result set.

use crate::exec::result::Row;
use std::cmp::Ordering;
use std::collections::BinaryHeap;

/// Streaming top-K results using min-heap
///
/// **Planned Feature** - Memory-efficient ORDER BY + LIMIT implementation
/// See ROADMAP.md: "Streaming Top-K Operations"
/// Target: v0.2.0 (High Priority)
///
/// This structure maintains only the K highest-scoring results without
/// materializing the entire result set in memory.
///
/// # Phase 4: Memory Optimization
///
/// Without StreamingTopK:
/// ```ignore
/// let mut results = collect_all_results(); // 1M rows in memory
/// results.sort_by(|a, b| score(b).cmp(score(a))); // Sort all 1M
/// results.truncate(10); // Only use 10
/// // Memory wasted: 999,990 rows
/// ```
///
/// With StreamingTopK:
/// ```ignore
/// let mut topk = StreamingTopK::new(10);
/// for row in results_iter {
///     topk.add(row, score); // Maintains heap of size ≤ 10
/// }
/// let results = topk.into_results(); // Already sorted, only 10 rows
/// // Memory used: 10 rows + heap overhead
/// ```
///
/// # Algorithm
///
/// Uses a min-heap where:
/// - Smallest score is at the root
/// - When heap size > K, remove minimum
/// - Result: heap contains K highest scores
///
/// # Performance
///
/// - **Time complexity**: O(N log K) where N = total rows, K = limit
/// - **Space complexity**: O(K) - only K rows in memory
/// - **Improvement over sort**: O(N log N) time, O(N) space
///
/// For N=1M, K=100:
/// - Traditional sort: ~20M comparisons, 1M rows in memory
/// - StreamingTopK: ~20M comparisons, 100 rows in memory (10,000x less)
#[allow(dead_code)]
pub struct StreamingTopK {
    /// Min-heap to maintain top-K results
    /// Root contains the minimum score in the heap
    heap: BinaryHeap<ScoredRow>,

    /// Maximum size (K)
    k: usize,

    /// Total rows processed (for statistics)
    processed_count: usize,

    /// Total rows that would have been kept without limit
    would_keep_count: usize,
}

/// Row with associated score for heap ordering
#[allow(dead_code)]
#[derive(Clone)]
struct ScoredRow {
    row: Row,
    score: f64,
}

impl Ord for ScoredRow {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse ordering for min-heap
        // (BinaryHeap is max-heap by default, we want min-heap)
        other
            .score
            .partial_cmp(&self.score)
            .unwrap_or(Ordering::Equal)
    }
}

impl PartialOrd for ScoredRow {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for ScoredRow {}

impl PartialEq for ScoredRow {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score
    }
}

impl StreamingTopK {
    /// Create a new StreamingTopK with capacity K
    ///
    /// # Arguments
    /// - `k`: Maximum number of results to keep
    ///
    /// # Example
    /// ```ignore
    /// let mut topk = StreamingTopK::new(10); // Keep top 10
    /// for row in rows {
    ///     topk.add(row, score);
    /// }
    /// let top_10 = topk.into_results();
    /// ```
    #[allow(dead_code)] // ROADMAP v0.5.0 - Streaming top-K for memory-efficient result limiting
    pub fn new(k: usize) -> Self {
        Self {
            heap: BinaryHeap::with_capacity(k + 1), // +1 for temporary overflow
            k,
            processed_count: 0,
            would_keep_count: 0,
        }
    }

    /// Add a row to the top-K
    ///
    /// If the heap is full and this score is lower than the minimum,
    /// the row is discarded. Otherwise, it's added and the minimum is removed.
    ///
    /// # Arguments
    /// - `row`: The row to potentially add
    /// - `score`: The relevance score for ranking
    ///
    /// # Time Complexity
    /// - Best case: O(1) if score below minimum
    /// - Worst case: O(log K) for heap operations
    #[allow(dead_code)] // ROADMAP v0.5.0 - Add rows to top-K heap for streaming filtering
    pub fn add(&mut self, row: Row, score: f64) {
        self.processed_count += 1;

        // If heap not full, always add
        if self.heap.len() < self.k {
            self.heap.push(ScoredRow { row, score });
            self.would_keep_count += 1;
            return;
        }

        // Heap is full - check if this score beats minimum
        if let Some(min_item) = self.heap.peek() {
            if score > min_item.score {
                // This row is better than current minimum
                self.heap.push(ScoredRow { row, score });
                self.heap.pop(); // Remove minimum
                self.would_keep_count += 1;
            }
            // else: score <= min, discard this row
        }
    }

    /// Get the current minimum score in the heap
    ///
    /// This is the threshold - any row with score ≤ this can be discarded.
    #[allow(dead_code)] // ROADMAP v0.5.0 - Minimum score threshold for early filtering
    pub fn min_score(&self) -> Option<f64> {
        self.heap.peek().map(|item| item.score)
    }

    /// Get current heap size
    #[allow(dead_code)] // ROADMAP v0.5.0 - Current top-K result count
    pub fn len(&self) -> usize {
        self.heap.len()
    }

    /// Check if heap is empty
    #[allow(dead_code)] // ROADMAP v0.5.0 - Empty check for top-K processing
    pub fn is_empty(&self) -> bool {
        self.heap.is_empty()
    }

    /// Get statistics about processing
    #[allow(dead_code)] // ROADMAP v0.5.0 - Performance statistics for top-K optimization
    pub fn stats(&self) -> TopKStats {
        TopKStats {
            processed_count: self.processed_count,
            kept_count: self.heap.len(),
            would_keep_count: self.would_keep_count,
            k: self.k,
        }
    }

    /// Convert to sorted results (descending by score)
    ///
    /// Consumes the StreamingTopK and returns the top-K rows
    /// sorted in descending order by score.
    ///
    /// # Time Complexity
    /// O(K log K) for sorting K results
    #[allow(dead_code)] // ROADMAP v0.5.0 - Streaming TopK result extraction
    pub fn into_results(self) -> Vec<Row> {
        let mut results: Vec<ScoredRow> = self.heap.into_iter().collect();

        // Sort descending by score
        results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));

        // Extract rows
        results.into_iter().map(|sr| sr.row).collect()
    }

    /// Get results without consuming (clones rows)
    #[allow(dead_code)] // ROADMAP v0.5.0 - Streaming TopK results accessor
    pub fn results(&self) -> Vec<Row> {
        let mut results: Vec<ScoredRow> = self.heap.iter().cloned().collect();

        results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));

        results.into_iter().map(|sr| sr.row).collect()
    }
}

/// Statistics about StreamingTopK processing
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.5.0 - Performance metrics for streaming top-K optimization
pub struct TopKStats {
    /// Total rows processed
    pub processed_count: usize,

    /// Rows currently in heap
    pub kept_count: usize,

    /// Rows that would be kept without limit (for analysis)
    pub would_keep_count: usize,

    /// K parameter
    pub k: usize,
}

impl TopKStats {
    /// Calculate memory savings ratio
    ///
    /// Returns how much memory was saved compared to keeping all rows.
    /// 1.0 = no savings, 0.1 = 90% savings
    #[allow(dead_code)] // ROADMAP v0.5.0 - Memory efficiency metrics for performance analysis
    pub fn memory_savings_ratio(&self) -> f64 {
        if self.processed_count == 0 {
            return 1.0;
        }
        self.kept_count as f64 / self.processed_count as f64
    }

    /// Calculate discard ratio
    ///
    /// Percentage of rows that were discarded.
    #[allow(dead_code)] // ROADMAP v0.5.0 - Discard rate metrics for optimization tuning
    pub fn discard_ratio(&self) -> f64 {
        if self.processed_count == 0 {
            return 0.0;
        }
        (self.processed_count - self.kept_count) as f64 / self.processed_count as f64
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::Value;
    use std::collections::HashMap;

    fn create_test_row(id: usize, score: f64) -> Row {
        let mut values = HashMap::new();
        values.insert("id".to_string(), Value::Number(id as f64));
        values.insert("score".to_string(), Value::Number(score));

        Row {
            values,
            positional_values: vec![],
            source_entities: HashMap::new(),
            text_score: Some(score),
            highlight_snippet: None,
        }
    }

    #[test]
    fn test_streaming_topk_basic() {
        let mut topk = StreamingTopK::new(3);

        // Add rows with different scores
        topk.add(create_test_row(1, 10.0), 10.0);
        topk.add(create_test_row(2, 20.0), 20.0);
        topk.add(create_test_row(3, 5.0), 5.0);
        topk.add(create_test_row(4, 15.0), 15.0);

        let results = topk.into_results();

        assert_eq!(results.len(), 3);

        // Should keep top 3: 20.0, 15.0, 10.0
        let scores: Vec<f64> = results
            .iter()
            .map(|r| r.get_text_score().unwrap())
            .collect();

        assert_eq!(scores[0], 20.0);
        assert_eq!(scores[1], 15.0);
        assert_eq!(scores[2], 10.0);
    }

    #[test]
    fn test_streaming_topk_with_many_rows() {
        let mut topk = StreamingTopK::new(10);

        // Add 100 rows with random scores
        for i in 0..100 {
            let score = (i * 7 % 100) as f64; // Pseudo-random
            topk.add(create_test_row(i, score), score);
        }

        let results = topk.into_results();

        assert_eq!(results.len(), 10);

        // Verify results are sorted descending
        for i in 0..results.len() - 1 {
            let score_i = results[i].get_text_score().unwrap();
            let score_j = results[i + 1].get_text_score().unwrap();
            assert!(score_i >= score_j);
        }
    }

    #[test]
    fn test_streaming_topk_min_score() {
        let mut topk = StreamingTopK::new(3);

        topk.add(create_test_row(1, 10.0), 10.0);
        topk.add(create_test_row(2, 20.0), 20.0);
        topk.add(create_test_row(3, 5.0), 5.0);

        // Min should be 5.0 (smallest in heap)
        assert_eq!(topk.min_score(), Some(5.0));

        // Add higher score, min should update
        topk.add(create_test_row(4, 15.0), 15.0);

        // Now min should be 10.0 (5.0 was removed)
        assert_eq!(topk.min_score(), Some(10.0));
    }

    #[test]
    fn test_streaming_topk_stats() {
        let mut topk = StreamingTopK::new(5);

        for i in 0..20 {
            topk.add(create_test_row(i, i as f64), i as f64);
        }

        let stats = topk.stats();

        assert_eq!(stats.processed_count, 20);
        assert_eq!(stats.kept_count, 5);
        assert_eq!(stats.k, 5);

        // Memory savings: kept 5 out of 20 = 25%
        assert!((stats.memory_savings_ratio() - 0.25).abs() < 0.01);

        // Discard ratio: discarded 15 out of 20 = 75%
        assert!((stats.discard_ratio() - 0.75).abs() < 0.01);
    }

    #[test]
    fn test_streaming_topk_empty() {
        let topk = StreamingTopK::new(10);

        assert!(topk.is_empty());
        assert_eq!(topk.len(), 0);
        assert_eq!(topk.min_score(), None);

        let results = topk.into_results();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_streaming_topk_fewer_than_k() {
        let mut topk = StreamingTopK::new(10);

        // Add only 5 rows when K=10
        for i in 0..5 {
            topk.add(create_test_row(i, i as f64), i as f64);
        }

        let results = topk.into_results();

        assert_eq!(results.len(), 5);
    }

    #[test]
    fn test_streaming_topk_duplicate_scores() {
        let mut topk = StreamingTopK::new(3);

        topk.add(create_test_row(1, 10.0), 10.0);
        topk.add(create_test_row(2, 10.0), 10.0);
        topk.add(create_test_row(3, 10.0), 10.0);
        topk.add(create_test_row(4, 5.0), 5.0);

        let results = topk.into_results();

        assert_eq!(results.len(), 3);

        // Should keep 3 rows with score 10.0
        let scores: Vec<f64> = results
            .iter()
            .map(|r| r.get_text_score().unwrap())
            .collect();

        assert_eq!(scores[0], 10.0);
        assert_eq!(scores[1], 10.0);
        assert_eq!(scores[2], 10.0);
    }
}