manifoldb 0.1.4

A multi-paradigm embedded database for graph, vector, and relational data
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
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! Metrics and observability for `ManifoldDB`.
//!
//! This module provides comprehensive metrics collection for monitoring database
//! performance and health. Metrics are collected using atomic counters for thread-safe,
//! low-overhead instrumentation.
//!
//! # Key Metrics
//!
//! - **Query metrics**: Latency histograms, queries per second, error counts
//! - **Transaction metrics**: Commit/rollback counts, active transactions
//! - **Cache metrics**: Hit/miss ratios, evictions, invalidations
//! - **Vector search metrics**: HNSW search times, nodes visited
//! - **Storage metrics**: Database size, table counts
//!
//! # Integration with `metrics` crate
//!
//! This module integrates with the [`metrics`] crate for Prometheus-compatible
//! metric export. You can install a metrics recorder (like `metrics-exporter-prometheus`)
//! to export metrics via HTTP.
//!
//! # Example
//!
//! ```ignore
//! use manifoldb::Database;
//!
//! let db = Database::in_memory()?;
//!
//! // Perform some operations...
//! db.execute("INSERT INTO users (name) VALUES ('Alice')")?;
//! db.query("SELECT * FROM users")?;
//!
//! // Get a metrics snapshot
//! let snapshot = db.metrics();
//! println!("Queries executed: {}", snapshot.queries.total_queries);
//! println!("Cache hit rate: {:?}", snapshot.cache.hit_rate());
//! println!("Transactions committed: {}", snapshot.transactions.commits);
//! ```

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

// Re-export the metrics crate for integration
pub use ::metrics;

/// Global metrics instance for the database.
///
/// This is thread-safe and can be shared across all database operations.
#[derive(Debug)]
pub struct DatabaseMetrics {
    /// Query execution metrics.
    pub queries: QueryMetrics,
    /// Transaction metrics.
    pub transactions: TransactionMetrics,
    /// Vector search metrics.
    pub vector: VectorMetrics,
    /// Storage metrics.
    pub storage: StorageMetrics,
}

impl DatabaseMetrics {
    /// Create a new metrics instance.
    #[must_use]
    pub fn new() -> Self {
        Self {
            queries: QueryMetrics::new(),
            transactions: TransactionMetrics::new(),
            vector: VectorMetrics::new(),
            storage: StorageMetrics::new(),
        }
    }

    /// Record a query execution with its duration.
    pub fn record_query(&self, duration: Duration, success: bool) {
        self.queries.record_query(duration, success);

        // Also emit to the metrics crate for Prometheus integration
        let duration_secs = duration.as_secs_f64();
        ::metrics::histogram!("manifoldb_query_duration_seconds").record(duration_secs);
        ::metrics::counter!("manifoldb_queries_total").increment(1);
        if !success {
            ::metrics::counter!("manifoldb_query_errors_total").increment(1);
        }
    }

    /// Record a transaction commit.
    pub fn record_commit(&self, duration: Duration) {
        self.transactions.record_commit(duration);

        ::metrics::counter!("manifoldb_transactions_committed_total").increment(1);
        ::metrics::histogram!("manifoldb_transaction_commit_duration_seconds")
            .record(duration.as_secs_f64());
    }

    /// Record a transaction rollback.
    pub fn record_rollback(&self) {
        self.transactions.record_rollback();
        ::metrics::counter!("manifoldb_transactions_rolled_back_total").increment(1);
    }

    /// Record a vector search operation.
    pub fn record_vector_search(&self, duration: Duration, nodes_visited: u64) {
        self.vector.record_search(duration, nodes_visited);

        ::metrics::histogram!("manifoldb_vector_search_duration_seconds")
            .record(duration.as_secs_f64());
        ::metrics::histogram!("manifoldb_vector_search_nodes_visited").record(nodes_visited as f64);
    }

    /// Update storage size metric.
    pub fn update_storage_size(&self, size_bytes: u64) {
        self.storage.update_size(size_bytes);
        ::metrics::gauge!("manifoldb_storage_size_bytes").set(size_bytes as f64);
    }

    /// Get a point-in-time snapshot of all metrics.
    #[must_use]
    pub fn snapshot(&self) -> MetricsSnapshot {
        MetricsSnapshot {
            queries: self.queries.snapshot(),
            transactions: self.transactions.snapshot(),
            vector: self.vector.snapshot(),
            storage: self.storage.snapshot(),
            cache: None, // Will be filled in by Database::metrics()
        }
    }

    /// Reset all metrics to zero.
    pub fn reset(&self) {
        self.queries.reset();
        self.transactions.reset();
        self.vector.reset();
        // Storage size is not reset, as it represents current state
    }
}

impl Default for DatabaseMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Query execution metrics.
#[derive(Debug)]
pub struct QueryMetrics {
    /// Total number of queries executed.
    total_queries: AtomicU64,
    /// Number of failed queries.
    failed_queries: AtomicU64,
    /// Total query execution time in nanoseconds.
    total_duration_ns: AtomicU64,
    /// Minimum query duration in nanoseconds.
    min_duration_ns: AtomicU64,
    /// Maximum query duration in nanoseconds.
    max_duration_ns: AtomicU64,
    /// Histogram buckets for query latency (in microseconds).
    /// Buckets: [<100us, <1ms, <10ms, <100ms, <1s, >=1s]
    histogram_buckets: [AtomicU64; 6],
}

impl QueryMetrics {
    const BUCKET_THRESHOLDS_US: [u64; 5] = [
        100,       // 100 microseconds
        1_000,     // 1 millisecond
        10_000,    // 10 milliseconds
        100_000,   // 100 milliseconds
        1_000_000, // 1 second
    ];

    /// Create a new query metrics instance.
    #[must_use]
    pub fn new() -> Self {
        Self {
            total_queries: AtomicU64::new(0),
            failed_queries: AtomicU64::new(0),
            total_duration_ns: AtomicU64::new(0),
            min_duration_ns: AtomicU64::new(u64::MAX),
            max_duration_ns: AtomicU64::new(0),
            histogram_buckets: [
                AtomicU64::new(0),
                AtomicU64::new(0),
                AtomicU64::new(0),
                AtomicU64::new(0),
                AtomicU64::new(0),
                AtomicU64::new(0),
            ],
        }
    }

    /// Record a query execution.
    pub fn record_query(&self, duration: Duration, success: bool) {
        self.total_queries.fetch_add(1, Ordering::Relaxed);
        if !success {
            self.failed_queries.fetch_add(1, Ordering::Relaxed);
        }

        let duration_ns = duration.as_nanos() as u64;
        self.total_duration_ns.fetch_add(duration_ns, Ordering::Relaxed);

        // Update min (using compare-and-swap loop)
        let mut current_min = self.min_duration_ns.load(Ordering::Relaxed);
        while duration_ns < current_min {
            match self.min_duration_ns.compare_exchange_weak(
                current_min,
                duration_ns,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_min = actual,
            }
        }

        // Update max
        let mut current_max = self.max_duration_ns.load(Ordering::Relaxed);
        while duration_ns > current_max {
            match self.max_duration_ns.compare_exchange_weak(
                current_max,
                duration_ns,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_max = actual,
            }
        }

        // Update histogram bucket
        let duration_us = duration.as_micros() as u64;
        let bucket_idx = Self::BUCKET_THRESHOLDS_US
            .iter()
            .position(|&threshold| duration_us < threshold)
            .unwrap_or(5);
        self.histogram_buckets[bucket_idx].fetch_add(1, Ordering::Relaxed);
    }

    /// Get a snapshot of query metrics.
    #[must_use]
    pub fn snapshot(&self) -> QueryMetricsSnapshot {
        let total = self.total_queries.load(Ordering::Relaxed);
        let total_duration_ns = self.total_duration_ns.load(Ordering::Relaxed);
        let min_ns = self.min_duration_ns.load(Ordering::Relaxed);
        let max_ns = self.max_duration_ns.load(Ordering::Relaxed);

        QueryMetricsSnapshot {
            total_queries: total,
            failed_queries: self.failed_queries.load(Ordering::Relaxed),
            avg_duration: if total > 0 {
                Duration::from_nanos(total_duration_ns / total)
            } else {
                Duration::ZERO
            },
            min_duration: if min_ns == u64::MAX {
                None
            } else {
                Some(Duration::from_nanos(min_ns))
            },
            max_duration: if max_ns == 0 && total == 0 {
                None
            } else {
                Some(Duration::from_nanos(max_ns))
            },
            histogram: [
                self.histogram_buckets[0].load(Ordering::Relaxed),
                self.histogram_buckets[1].load(Ordering::Relaxed),
                self.histogram_buckets[2].load(Ordering::Relaxed),
                self.histogram_buckets[3].load(Ordering::Relaxed),
                self.histogram_buckets[4].load(Ordering::Relaxed),
                self.histogram_buckets[5].load(Ordering::Relaxed),
            ],
        }
    }

    /// Reset query metrics.
    pub fn reset(&self) {
        self.total_queries.store(0, Ordering::Relaxed);
        self.failed_queries.store(0, Ordering::Relaxed);
        self.total_duration_ns.store(0, Ordering::Relaxed);
        self.min_duration_ns.store(u64::MAX, Ordering::Relaxed);
        self.max_duration_ns.store(0, Ordering::Relaxed);
        for bucket in &self.histogram_buckets {
            bucket.store(0, Ordering::Relaxed);
        }
    }
}

impl Default for QueryMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Transaction metrics.
#[derive(Debug)]
pub struct TransactionMetrics {
    /// Number of committed transactions.
    commits: AtomicU64,
    /// Number of rolled back transactions.
    rollbacks: AtomicU64,
    /// Total commit duration in nanoseconds.
    total_commit_duration_ns: AtomicU64,
    /// Number of currently active transactions.
    active_transactions: AtomicU64,
}

impl TransactionMetrics {
    /// Create a new transaction metrics instance.
    #[must_use]
    pub fn new() -> Self {
        Self {
            commits: AtomicU64::new(0),
            rollbacks: AtomicU64::new(0),
            total_commit_duration_ns: AtomicU64::new(0),
            active_transactions: AtomicU64::new(0),
        }
    }

    /// Record a transaction start.
    pub fn record_start(&self) {
        self.active_transactions.fetch_add(1, Ordering::Relaxed);
        ::metrics::gauge!("manifoldb_active_transactions")
            .set(self.active_transactions.load(Ordering::Relaxed) as f64);
    }

    /// Record a transaction commit.
    pub fn record_commit(&self, duration: Duration) {
        self.commits.fetch_add(1, Ordering::Relaxed);
        self.total_commit_duration_ns.fetch_add(duration.as_nanos() as u64, Ordering::Relaxed);
        self.active_transactions.fetch_sub(1, Ordering::Relaxed);
        ::metrics::gauge!("manifoldb_active_transactions")
            .set(self.active_transactions.load(Ordering::Relaxed) as f64);
    }

    /// Record a transaction rollback.
    pub fn record_rollback(&self) {
        self.rollbacks.fetch_add(1, Ordering::Relaxed);
        self.active_transactions.fetch_sub(1, Ordering::Relaxed);
        ::metrics::gauge!("manifoldb_active_transactions")
            .set(self.active_transactions.load(Ordering::Relaxed) as f64);
    }

    /// Record a transaction end without explicit commit/rollback (e.g., drop).
    pub fn record_end(&self) {
        self.active_transactions.fetch_sub(1, Ordering::Relaxed);
        ::metrics::gauge!("manifoldb_active_transactions")
            .set(self.active_transactions.load(Ordering::Relaxed) as f64);
    }

    /// Get a snapshot of transaction metrics.
    #[must_use]
    pub fn snapshot(&self) -> TransactionMetricsSnapshot {
        let commits = self.commits.load(Ordering::Relaxed);
        let total_duration_ns = self.total_commit_duration_ns.load(Ordering::Relaxed);

        TransactionMetricsSnapshot {
            commits,
            rollbacks: self.rollbacks.load(Ordering::Relaxed),
            active: self.active_transactions.load(Ordering::Relaxed),
            avg_commit_duration: if commits > 0 {
                Duration::from_nanos(total_duration_ns / commits)
            } else {
                Duration::ZERO
            },
        }
    }

    /// Reset transaction metrics.
    pub fn reset(&self) {
        self.commits.store(0, Ordering::Relaxed);
        self.rollbacks.store(0, Ordering::Relaxed);
        self.total_commit_duration_ns.store(0, Ordering::Relaxed);
        // Don't reset active_transactions as it represents current state
    }
}

impl Default for TransactionMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Vector search metrics.
#[derive(Debug)]
pub struct VectorMetrics {
    /// Total number of vector searches.
    total_searches: AtomicU64,
    /// Total search duration in nanoseconds.
    total_duration_ns: AtomicU64,
    /// Total nodes visited during searches.
    total_nodes_visited: AtomicU64,
    /// Minimum search duration in nanoseconds.
    min_duration_ns: AtomicU64,
    /// Maximum search duration in nanoseconds.
    max_duration_ns: AtomicU64,
}

impl VectorMetrics {
    /// Create a new vector metrics instance.
    #[must_use]
    pub fn new() -> Self {
        Self {
            total_searches: AtomicU64::new(0),
            total_duration_ns: AtomicU64::new(0),
            total_nodes_visited: AtomicU64::new(0),
            min_duration_ns: AtomicU64::new(u64::MAX),
            max_duration_ns: AtomicU64::new(0),
        }
    }

    /// Record a vector search operation.
    pub fn record_search(&self, duration: Duration, nodes_visited: u64) {
        self.total_searches.fetch_add(1, Ordering::Relaxed);
        let duration_ns = duration.as_nanos() as u64;
        self.total_duration_ns.fetch_add(duration_ns, Ordering::Relaxed);
        self.total_nodes_visited.fetch_add(nodes_visited, Ordering::Relaxed);

        // Update min
        let mut current_min = self.min_duration_ns.load(Ordering::Relaxed);
        while duration_ns < current_min {
            match self.min_duration_ns.compare_exchange_weak(
                current_min,
                duration_ns,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_min = actual,
            }
        }

        // Update max
        let mut current_max = self.max_duration_ns.load(Ordering::Relaxed);
        while duration_ns > current_max {
            match self.max_duration_ns.compare_exchange_weak(
                current_max,
                duration_ns,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_max = actual,
            }
        }
    }

    /// Get a snapshot of vector metrics.
    #[must_use]
    pub fn snapshot(&self) -> VectorMetricsSnapshot {
        let total = self.total_searches.load(Ordering::Relaxed);
        let total_duration_ns = self.total_duration_ns.load(Ordering::Relaxed);
        let total_nodes = self.total_nodes_visited.load(Ordering::Relaxed);
        let min_ns = self.min_duration_ns.load(Ordering::Relaxed);
        let max_ns = self.max_duration_ns.load(Ordering::Relaxed);

        VectorMetricsSnapshot {
            total_searches: total,
            avg_duration: if total > 0 {
                Duration::from_nanos(total_duration_ns / total)
            } else {
                Duration::ZERO
            },
            min_duration: if min_ns == u64::MAX {
                None
            } else {
                Some(Duration::from_nanos(min_ns))
            },
            max_duration: if max_ns == 0 && total == 0 {
                None
            } else {
                Some(Duration::from_nanos(max_ns))
            },
            avg_nodes_visited: if total > 0 { total_nodes / total } else { 0 },
            total_nodes_visited: total_nodes,
        }
    }

    /// Reset vector metrics.
    pub fn reset(&self) {
        self.total_searches.store(0, Ordering::Relaxed);
        self.total_duration_ns.store(0, Ordering::Relaxed);
        self.total_nodes_visited.store(0, Ordering::Relaxed);
        self.min_duration_ns.store(u64::MAX, Ordering::Relaxed);
        self.max_duration_ns.store(0, Ordering::Relaxed);
    }
}

impl Default for VectorMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Storage metrics.
#[derive(Debug)]
pub struct StorageMetrics {
    /// Current database size in bytes.
    size_bytes: AtomicU64,
    /// Number of tables.
    table_count: AtomicU64,
}

impl StorageMetrics {
    /// Create a new storage metrics instance.
    #[must_use]
    pub fn new() -> Self {
        Self { size_bytes: AtomicU64::new(0), table_count: AtomicU64::new(0) }
    }

    /// Update the storage size.
    pub fn update_size(&self, size_bytes: u64) {
        self.size_bytes.store(size_bytes, Ordering::Relaxed);
    }

    /// Update the table count.
    pub fn update_table_count(&self, count: u64) {
        self.table_count.store(count, Ordering::Relaxed);
        ::metrics::gauge!("manifoldb_table_count").set(count as f64);
    }

    /// Get a snapshot of storage metrics.
    #[must_use]
    pub fn snapshot(&self) -> StorageMetricsSnapshot {
        StorageMetricsSnapshot {
            size_bytes: self.size_bytes.load(Ordering::Relaxed),
            table_count: self.table_count.load(Ordering::Relaxed),
        }
    }
}

impl Default for StorageMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// A point-in-time snapshot of all database metrics.
#[derive(Debug, Clone)]
pub struct MetricsSnapshot {
    /// Query metrics snapshot.
    pub queries: QueryMetricsSnapshot,
    /// Transaction metrics snapshot.
    pub transactions: TransactionMetricsSnapshot,
    /// Vector search metrics snapshot.
    pub vector: VectorMetricsSnapshot,
    /// Storage metrics snapshot.
    pub storage: StorageMetricsSnapshot,
    /// Cache metrics snapshot (optional, filled by Database).
    pub cache: Option<CacheMetricsSnapshot>,
}

impl std::fmt::Display for MetricsSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "ManifoldDB Metrics")?;
        writeln!(f, "==================")?;
        writeln!(f)?;
        writeln!(f, "Queries:")?;
        writeln!(f, "  Total: {}", self.queries.total_queries)?;
        writeln!(f, "  Failed: {}", self.queries.failed_queries)?;
        writeln!(f, "  Avg Duration: {:?}", self.queries.avg_duration)?;
        if let Some(min) = self.queries.min_duration {
            writeln!(f, "  Min Duration: {min:?}")?;
        }
        if let Some(max) = self.queries.max_duration {
            writeln!(f, "  Max Duration: {max:?}")?;
        }
        writeln!(f)?;
        writeln!(f, "Transactions:")?;
        writeln!(f, "  Commits: {}", self.transactions.commits)?;
        writeln!(f, "  Rollbacks: {}", self.transactions.rollbacks)?;
        writeln!(f, "  Active: {}", self.transactions.active)?;
        writeln!(f, "  Avg Commit Duration: {:?}", self.transactions.avg_commit_duration)?;
        writeln!(f)?;
        writeln!(f, "Vector Search:")?;
        writeln!(f, "  Total Searches: {}", self.vector.total_searches)?;
        writeln!(f, "  Avg Duration: {:?}", self.vector.avg_duration)?;
        writeln!(f, "  Avg Nodes Visited: {}", self.vector.avg_nodes_visited)?;
        writeln!(f)?;
        writeln!(f, "Storage:")?;
        writeln!(f, "  Size: {} bytes", self.storage.size_bytes)?;
        writeln!(f, "  Tables: {}", self.storage.table_count)?;
        if let Some(ref cache) = self.cache {
            writeln!(f)?;
            writeln!(f, "Cache:")?;
            writeln!(f, "  Hits: {}", cache.hits)?;
            writeln!(f, "  Misses: {}", cache.misses)?;
            if let Some(rate) = cache.hit_rate() {
                writeln!(f, "  Hit Rate: {rate:.1}%")?;
            }
            writeln!(f, "  Evictions: {}", cache.evictions)?;
            writeln!(f, "  Invalidations: {}", cache.invalidations)?;
        }
        Ok(())
    }
}

/// Query metrics snapshot.
#[derive(Debug, Clone)]
pub struct QueryMetricsSnapshot {
    /// Total queries executed.
    pub total_queries: u64,
    /// Failed queries.
    pub failed_queries: u64,
    /// Average query duration.
    pub avg_duration: Duration,
    /// Minimum query duration.
    pub min_duration: Option<Duration>,
    /// Maximum query duration.
    pub max_duration: Option<Duration>,
    /// Latency histogram buckets.
    /// Buckets: [<100us, <1ms, <10ms, <100ms, <1s, >=1s]
    pub histogram: [u64; 6],
}

impl QueryMetricsSnapshot {
    /// Get queries per second based on total queries and average duration.
    ///
    /// Note: This is an approximation. For accurate QPS, you need to track
    /// queries over a specific time window.
    #[must_use]
    pub fn success_rate(&self) -> Option<f64> {
        if self.total_queries == 0 {
            None
        } else {
            Some(
                ((self.total_queries - self.failed_queries) as f64 / self.total_queries as f64)
                    * 100.0,
            )
        }
    }

    /// Get the percentage of queries in each latency bucket.
    #[must_use]
    pub fn histogram_percentages(&self) -> [f64; 6] {
        if self.total_queries == 0 {
            return [0.0; 6];
        }
        let total = self.total_queries as f64;
        [
            self.histogram[0] as f64 / total * 100.0,
            self.histogram[1] as f64 / total * 100.0,
            self.histogram[2] as f64 / total * 100.0,
            self.histogram[3] as f64 / total * 100.0,
            self.histogram[4] as f64 / total * 100.0,
            self.histogram[5] as f64 / total * 100.0,
        ]
    }
}

/// Transaction metrics snapshot.
#[derive(Debug, Clone)]
pub struct TransactionMetricsSnapshot {
    /// Number of commits.
    pub commits: u64,
    /// Number of rollbacks.
    pub rollbacks: u64,
    /// Currently active transactions.
    pub active: u64,
    /// Average commit duration.
    pub avg_commit_duration: Duration,
}

impl TransactionMetricsSnapshot {
    /// Get the total number of completed transactions.
    #[must_use]
    pub fn total_completed(&self) -> u64 {
        self.commits + self.rollbacks
    }

    /// Get the commit success rate as a percentage.
    #[must_use]
    pub fn commit_rate(&self) -> Option<f64> {
        let total = self.total_completed();
        if total == 0 {
            None
        } else {
            Some((self.commits as f64 / total as f64) * 100.0)
        }
    }
}

/// Vector search metrics snapshot.
#[derive(Debug, Clone)]
pub struct VectorMetricsSnapshot {
    /// Total searches performed.
    pub total_searches: u64,
    /// Average search duration.
    pub avg_duration: Duration,
    /// Minimum search duration.
    pub min_duration: Option<Duration>,
    /// Maximum search duration.
    pub max_duration: Option<Duration>,
    /// Average nodes visited per search.
    pub avg_nodes_visited: u64,
    /// Total nodes visited across all searches.
    pub total_nodes_visited: u64,
}

/// Storage metrics snapshot.
#[derive(Debug, Clone)]
pub struct StorageMetricsSnapshot {
    /// Current database size in bytes.
    pub size_bytes: u64,
    /// Number of tables.
    pub table_count: u64,
}

impl StorageMetricsSnapshot {
    /// Get the size in a human-readable format.
    #[must_use]
    pub fn size_human_readable(&self) -> String {
        let size = self.size_bytes;
        if size < 1024 {
            format!("{size} B")
        } else if size < 1024 * 1024 {
            format!("{:.1} KB", size as f64 / 1024.0)
        } else if size < 1024 * 1024 * 1024 {
            format!("{:.1} MB", size as f64 / (1024.0 * 1024.0))
        } else {
            format!("{:.1} GB", size as f64 / (1024.0 * 1024.0 * 1024.0))
        }
    }
}

/// Cache metrics snapshot.
///
/// This mirrors the structure from `cache::MetricsSnapshot` but is exposed
/// in the metrics module for a unified API.
#[derive(Debug, Clone)]
pub struct CacheMetricsSnapshot {
    /// Number of cache hits.
    pub hits: u64,
    /// Number of cache misses.
    pub misses: u64,
    /// Number of LRU evictions.
    pub evictions: u64,
    /// Number of invalidations.
    pub invalidations: u64,
}

impl CacheMetricsSnapshot {
    /// Create from the cache module's snapshot.
    #[must_use]
    pub fn from_cache_snapshot(s: crate::cache::MetricsSnapshot) -> Self {
        Self {
            hits: s.hits,
            misses: s.misses,
            evictions: s.evictions,
            invalidations: s.invalidations,
        }
    }

    /// Get the total number of lookups.
    #[must_use]
    pub fn total_lookups(&self) -> u64 {
        self.hits + self.misses
    }

    /// Get the hit rate as a percentage.
    #[must_use]
    pub fn hit_rate(&self) -> Option<f64> {
        let total = self.total_lookups();
        if total == 0 {
            None
        } else {
            Some((self.hits as f64 / total as f64) * 100.0)
        }
    }

    /// Get the miss rate as a percentage.
    #[must_use]
    pub fn miss_rate(&self) -> Option<f64> {
        self.hit_rate().map(|hr| 100.0 - hr)
    }
}

/// A guard for timing operations.
///
/// Records the duration when dropped.
pub struct TimingGuard<F: FnOnce(Duration)> {
    start: Instant,
    callback: Option<F>,
}

impl<F: FnOnce(Duration)> TimingGuard<F> {
    /// Create a new timing guard.
    #[must_use]
    pub fn new(callback: F) -> Self {
        Self { start: Instant::now(), callback: Some(callback) }
    }

    /// Finish timing and return the duration.
    pub fn finish(mut self) -> Duration {
        let duration = self.start.elapsed();
        if let Some(cb) = self.callback.take() {
            cb(duration);
        }
        duration
    }
}

impl<F: FnOnce(Duration)> Drop for TimingGuard<F> {
    fn drop(&mut self) {
        if let Some(cb) = self.callback.take() {
            cb(self.start.elapsed());
        }
    }
}

/// Create a timing guard for query execution.
#[must_use]
pub fn time_query(metrics: Arc<DatabaseMetrics>) -> TimingGuard<impl FnOnce(Duration)> {
    TimingGuard::new(move |duration| {
        metrics.record_query(duration, true);
    })
}

/// Create a timing guard for transaction commits.
#[must_use]
pub fn time_commit(metrics: Arc<DatabaseMetrics>) -> TimingGuard<impl FnOnce(Duration)> {
    TimingGuard::new(move |duration| {
        metrics.record_commit(duration);
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_query_metrics_basic() {
        let metrics = QueryMetrics::new();

        metrics.record_query(Duration::from_micros(50), true);
        metrics.record_query(Duration::from_millis(5), true);
        metrics.record_query(Duration::from_millis(50), false);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.total_queries, 3);
        assert_eq!(snapshot.failed_queries, 1);
        assert!(snapshot.min_duration.is_some());
        assert!(snapshot.max_duration.is_some());
    }

    #[test]
    fn test_query_metrics_histogram() {
        let metrics = QueryMetrics::new();

        // <100us bucket
        metrics.record_query(Duration::from_micros(50), true);
        // <1ms bucket
        metrics.record_query(Duration::from_micros(500), true);
        // <10ms bucket
        metrics.record_query(Duration::from_millis(5), true);
        // <100ms bucket
        metrics.record_query(Duration::from_millis(50), true);
        // <1s bucket
        metrics.record_query(Duration::from_millis(500), true);
        // >=1s bucket
        metrics.record_query(Duration::from_secs(2), true);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.histogram, [1, 1, 1, 1, 1, 1]);
    }

    #[test]
    fn test_transaction_metrics() {
        let metrics = TransactionMetrics::new();

        metrics.record_start();
        metrics.record_start();
        assert_eq!(metrics.snapshot().active, 2);

        metrics.record_commit(Duration::from_millis(10));
        metrics.record_rollback();

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.commits, 1);
        assert_eq!(snapshot.rollbacks, 1);
        assert_eq!(snapshot.active, 0);
    }

    #[test]
    fn test_vector_metrics() {
        let metrics = VectorMetrics::new();

        metrics.record_search(Duration::from_micros(100), 50);
        metrics.record_search(Duration::from_micros(200), 100);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.total_searches, 2);
        assert_eq!(snapshot.avg_nodes_visited, 75);
        assert_eq!(snapshot.total_nodes_visited, 150);
    }

    #[test]
    fn test_storage_metrics() {
        let metrics = StorageMetrics::new();

        metrics.update_size(1024 * 1024); // 1 MB
        metrics.update_table_count(5);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.size_bytes, 1024 * 1024);
        assert_eq!(snapshot.table_count, 5);
        assert_eq!(snapshot.size_human_readable(), "1.0 MB");
    }

    #[test]
    fn test_database_metrics_snapshot() {
        let metrics = DatabaseMetrics::new();

        metrics.record_query(Duration::from_millis(5), true);
        metrics.record_commit(Duration::from_millis(2));
        metrics.record_vector_search(Duration::from_micros(100), 25);
        metrics.update_storage_size(2048);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.queries.total_queries, 1);
        assert_eq!(snapshot.transactions.commits, 1);
        assert_eq!(snapshot.vector.total_searches, 1);
        assert_eq!(snapshot.storage.size_bytes, 2048);
    }

    #[test]
    fn test_metrics_reset() {
        let metrics = DatabaseMetrics::new();

        metrics.record_query(Duration::from_millis(5), true);
        metrics.record_commit(Duration::from_millis(2));

        metrics.reset();

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.queries.total_queries, 0);
        assert_eq!(snapshot.transactions.commits, 0);
    }

    #[test]
    fn test_timing_guard() {
        let recorded = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let recorded_clone = Arc::clone(&recorded);

        {
            let _guard = TimingGuard::new(move |_duration| {
                recorded_clone.store(true, Ordering::SeqCst);
            });
            std::thread::sleep(Duration::from_millis(1));
        }

        assert!(recorded.load(Ordering::SeqCst));
    }

    #[test]
    fn test_cache_metrics_snapshot() {
        let cache_snapshot =
            crate::cache::MetricsSnapshot { hits: 100, misses: 20, evictions: 5, invalidations: 3 };

        let snapshot = CacheMetricsSnapshot::from_cache_snapshot(cache_snapshot);
        assert_eq!(snapshot.hits, 100);
        assert_eq!(snapshot.misses, 20);
        assert_eq!(snapshot.total_lookups(), 120);

        let hit_rate = snapshot.hit_rate().unwrap();
        assert!((hit_rate - 83.33).abs() < 0.1);
    }

    #[test]
    fn test_metrics_display() {
        let metrics = DatabaseMetrics::new();
        metrics.record_query(Duration::from_millis(5), true);
        let snapshot = metrics.snapshot();

        let display = format!("{snapshot}");
        assert!(display.contains("ManifoldDB Metrics"));
        assert!(display.contains("Queries:"));
        assert!(display.contains("Total: 1"));
    }
}