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
//! Graph introspection APIs for debugging and observability.
//!
//! This module provides structured introspection capabilities for SQLiteGraph,
//! enabling developers and AI agents to inspect internal graph state for debugging
//! and observability purposes.
//!
//! # What is GraphIntrospection?
//!
//! [`GraphIntrospection`] provides a JSON-serializable snapshot of graph state,
//! designed for both human debugging and LLM consumption. It exposes internal
//! metrics that are otherwise difficult to access:
//!
//! - **Backend type**: SQLite vs Native backend
//! - **Node/edge counts**: Graph size metrics
//! - **Cache statistics**: Hit ratios and entry counts
//! - **File sizes**: Database and WAL file sizes
//! - **Memory usage**: In-memory vs file-based detection
//!
//! # Key Types
//!
//! - [`GraphIntrospection`] - Comprehensive introspection snapshot
//! - [`EdgeCount`] - Edge count with exact/estimate/unavailable states
//! - [`IntrospectError`] - Introspection-specific errors
//!
//! # Usage for Debugging
//!
//! ## Basic Introspection
//!
//! ```rust,ignore
//! use sqlitegraph::{open_graph, GraphConfig};
//!
//! let graph = open_graph("my_graph.db", &GraphConfig::sqlite())?;
//! let intro = graph.introspect()?;
//!
//! println!("Backend: {}", intro.backend_type);
//! println!("Nodes: {}", intro.node_count);
//! println!("Edges: {:?}", intro.edge_count);
//! println!("Cache hit ratio: {:.2}%", intro.cache_stats.hit_ratio().unwrap_or(0.0));
//! ```
//!
//! ## Cache Performance Analysis
//!
//! ```rust,ignore
//! let intro = graph.introspect()?;
//!
//! match intro.cache_stats.hit_ratio() {
//! Some(ratio) if ratio < 50.0 => {
//! println!("Warning: Low cache hit ratio ({:.1}%)", ratio);
//! println!("Consider adjusting cache size or workload");
//! }
//! Some(ratio) => {
//! println!("Good cache performance: {:.1}% hit ratio", ratio);
//! }
//! None => {
//! println!("No cache activity yet");
//! }
//! }
//! ```
//!
//! # Edge Count Strategy
//!
//! The [`EdgeCount`] enum provides **adaptive edge counting** based on graph size:
//!
//! ## Exact Count (< 10K edges)
//!
//! For small to medium graphs, edges are counted exactly:
//!
//! ```rust,ignore
//! match intro.edge_count {
//! EdgeCount::Exact(count) => {
//! println!("Graph has {} edges", count);
//! }
//! _ => {}
//! }
//! ```
//!
//! ## Sampled Estimate (≥ 10K edges)
//!
//! For large graphs, edges are estimated via sampling to avoid expensive scans:
//!
//! ```rust,ignore
//! match intro.edge_count {
//! EdgeCount::Estimate { count, min, max, sample_size } => {
//! println!("Estimated {} edges (95% CI: {}-{})", count, min, max);
//! println!("Based on {} node sample", sample_size);
//! }
//! _ => {}
//! }
//! ```
//!
//! ### Estimation Algorithm
//!
//! - **Sample size**: 1000 nodes (or all nodes if smaller)
//! - **Confidence interval**: 95% via binomial proportion
//! - **Accuracy**: Typically ±5% for uniform degree distributions
//! - **Cost**: O(sample_size) vs O(V) for exact count
//!
//! ## Unavailable (Backend-Specific)
//!
//! Some backends may not support edge counting:
//!
//! ```rust,ignore
//! match intro.edge_count {
//! EdgeCount::Unavailable => {
//! println!("Edge counting not available for this backend");
//! }
//! _ => {}
//! }
//! ```
//!
//! # File Size Detection
//!
//! Introspection provides **file size metrics** for file-based databases:
//!
//! ## Database File Size
//!
//! ```rust,ignore
//! if let Some(size) = intro.file_size {
//! println!("Database file: {} MB", size / 1_048_576);
//! } else {
//! println!("In-memory database (no file)");
//! }
//! ```
//!
//! ## WAL File Size
//!
//! ```rust,ignore
//! if let Some(wal_size) = intro.wal_size {
//! println!("WAL file: {} MB", wal_size / 1_048_576);
//! if wal_size > 100_000_000 {
//! println!("Warning: Large WAL - consider checkpoint");
//! }
//! }
//! ```
//!
//! # JSON Serialization for LLMs
//!
//! The introspection data structure is fully JSON-serializable for LLM consumption:
//!
//! ```rust,ignore
//! use serde_json;
//!
//! let intro = graph.introspect()?;
//! let json = serde_json::to_string_pretty(&intro)?;
//!
//! // Pass to LLM for analysis
//! let analysis = llm.analyze(&json)?;
//! ```
//!
//! Example JSON output:
//!
//! ```json
//! {
//! "backend_type": "sqlite",
//! "node_count": 10000,
//! "edge_count": {
//! "Estimate": {
//! "count": 45000,
//! "min": 44000,
//! "max": 46000,
//! "sample_size": 1000
//! }
//! },
//! "cache_stats": {
//! "hits": 85000,
//! "misses": 15000,
//! "entries": 5000
//! },
//! "file_size": 10485760,
//! "wal_size": 524288,
//! "is_in_memory": false
//! }
//! ```
//!
//! # Performance Considerations
//!
//! - **Introspection cost**: O(sample_size) for edge estimation, O(1) for other metrics
//! - **Cache stats**: Aggregated from atomic counters (no locking)
//! - **File sizes**: Cached `stat()` calls (negligible overhead)
//! - **Safe for production**: Minimal performance impact
use Serialize;
use Path;
use crateCacheStats;
use crateSqliteGraphError;
/// Comprehensive introspection data for a graph instance.
///
/// This struct provides a JSON-serializable snapshot of graph state,
/// designed for both human debugging and LLM consumption.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{open_graph, GraphConfig};
///
/// let graph = open_graph("my_graph.db", &GraphConfig::sqlite())?;
/// let intro = graph.introspect()?;
///
/// println!("Backend: {}", intro.backend_type);
/// println!("Nodes: {}", intro.node_count);
/// println!("Cache hit ratio: {:.2}%", intro.cache_stats.hit_ratio());
///
/// // Serialize to JSON for LLM consumption
/// let json = serde_json::to_string_pretty(&intro)?;
/// ```
/// Edge count representation.
///
/// Provides either an exact count or an estimate for large graphs
/// where counting would be prohibitively expensive.
/// Introspection-specific errors.
/// Get file size for a database path.
/// Get WAL file size for a database path.