kitedb 0.2.2

High-performance embedded 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Metrics types for Python bindings

use crate::metrics as core_metrics;
use pyo3::prelude::*;

/// Cache layer metrics (single layer - property, traversal, or query)
#[pyclass(name = "CacheLayerMetrics")]
#[derive(Debug, Clone)]
pub struct CacheLayerMetrics {
  #[pyo3(get)]
  pub hits: i64,
  #[pyo3(get)]
  pub misses: i64,
  #[pyo3(get)]
  pub hit_rate: f64,
  #[pyo3(get)]
  pub size: i64,
  #[pyo3(get)]
  pub max_size: i64,
  #[pyo3(get)]
  pub utilization_percent: f64,
}

#[pymethods]
impl CacheLayerMetrics {
  fn __repr__(&self) -> String {
    format!(
      "CacheLayerMetrics(hit_rate={:.2}%, size={}/{})",
      self.hit_rate * 100.0,
      self.size,
      self.max_size
    )
  }
}

impl From<core_metrics::CacheLayerMetrics> for CacheLayerMetrics {
  fn from(metrics: core_metrics::CacheLayerMetrics) -> Self {
    CacheLayerMetrics {
      hits: metrics.hits,
      misses: metrics.misses,
      hit_rate: metrics.hit_rate,
      size: metrics.size,
      max_size: metrics.max_size,
      utilization_percent: metrics.utilization_percent,
    }
  }
}

/// Cache metrics (all cache layers)
#[pyclass(name = "CacheMetrics")]
#[derive(Debug, Clone)]
pub struct CacheMetrics {
  #[pyo3(get)]
  pub enabled: bool,
  #[pyo3(get)]
  pub property_cache: CacheLayerMetrics,
  #[pyo3(get)]
  pub traversal_cache: CacheLayerMetrics,
  #[pyo3(get)]
  pub query_cache: CacheLayerMetrics,
}

#[pymethods]
impl CacheMetrics {
  fn __repr__(&self) -> String {
    format!(
      "CacheMetrics(enabled={}, property={:.1}%, traversal={:.1}%, query={:.1}%)",
      self.enabled,
      self.property_cache.hit_rate * 100.0,
      self.traversal_cache.hit_rate * 100.0,
      self.query_cache.hit_rate * 100.0
    )
  }
}

impl From<core_metrics::CacheMetrics> for CacheMetrics {
  fn from(metrics: core_metrics::CacheMetrics) -> Self {
    CacheMetrics {
      enabled: metrics.enabled,
      property_cache: metrics.property_cache.into(),
      traversal_cache: metrics.traversal_cache.into(),
      query_cache: metrics.query_cache.into(),
    }
  }
}

/// Data metrics (node/edge counts)
#[pyclass(name = "DataMetrics")]
#[derive(Debug, Clone)]
pub struct DataMetrics {
  #[pyo3(get)]
  pub node_count: i64,
  #[pyo3(get)]
  pub edge_count: i64,
  #[pyo3(get)]
  pub delta_nodes_created: i64,
  #[pyo3(get)]
  pub delta_nodes_deleted: i64,
  #[pyo3(get)]
  pub delta_edges_added: i64,
  #[pyo3(get)]
  pub delta_edges_deleted: i64,
  #[pyo3(get)]
  pub snapshot_generation: i64,
  #[pyo3(get)]
  pub max_node_id: i64,
  #[pyo3(get)]
  pub schema_labels: i64,
  #[pyo3(get)]
  pub schema_etypes: i64,
  #[pyo3(get)]
  pub schema_prop_keys: i64,
}

#[pymethods]
impl DataMetrics {
  fn __repr__(&self) -> String {
    format!(
      "DataMetrics(nodes={}, edges={}, gen={})",
      self.node_count, self.edge_count, self.snapshot_generation
    )
  }
}

impl From<core_metrics::DataMetrics> for DataMetrics {
  fn from(metrics: core_metrics::DataMetrics) -> Self {
    DataMetrics {
      node_count: metrics.node_count,
      edge_count: metrics.edge_count,
      delta_nodes_created: metrics.delta_nodes_created,
      delta_nodes_deleted: metrics.delta_nodes_deleted,
      delta_edges_added: metrics.delta_edges_added,
      delta_edges_deleted: metrics.delta_edges_deleted,
      snapshot_generation: metrics.snapshot_generation,
      max_node_id: metrics.max_node_id,
      schema_labels: metrics.schema_labels,
      schema_etypes: metrics.schema_etypes,
      schema_prop_keys: metrics.schema_prop_keys,
    }
  }
}

/// MVCC metrics
#[pyclass(name = "MvccMetrics")]
#[derive(Debug, Clone)]
pub struct MvccMetrics {
  #[pyo3(get)]
  pub enabled: bool,
  #[pyo3(get)]
  pub active_transactions: i64,
  #[pyo3(get)]
  pub versions_pruned: i64,
  #[pyo3(get)]
  pub gc_runs: i64,
  #[pyo3(get)]
  pub min_active_timestamp: i64,
  #[pyo3(get)]
  pub committed_writes_size: i64,
  #[pyo3(get)]
  pub committed_writes_pruned: i64,
}

#[pymethods]
impl MvccMetrics {
  fn __repr__(&self) -> String {
    format!(
      "MvccMetrics(enabled={}, active_tx={}, gc_runs={})",
      self.enabled, self.active_transactions, self.gc_runs
    )
  }
}

impl From<core_metrics::MvccMetrics> for MvccMetrics {
  fn from(metrics: core_metrics::MvccMetrics) -> Self {
    MvccMetrics {
      enabled: metrics.enabled,
      active_transactions: metrics.active_transactions,
      versions_pruned: metrics.versions_pruned,
      gc_runs: metrics.gc_runs,
      min_active_timestamp: metrics.min_active_timestamp,
      committed_writes_size: metrics.committed_writes_size,
      committed_writes_pruned: metrics.committed_writes_pruned,
    }
  }
}

/// MVCC stats (from stats())
#[pyclass(name = "MvccStats")]
#[derive(Debug, Clone)]
pub struct MvccStats {
  #[pyo3(get)]
  pub active_transactions: i64,
  #[pyo3(get)]
  pub min_active_ts: i64,
  #[pyo3(get)]
  pub versions_pruned: i64,
  #[pyo3(get)]
  pub gc_runs: i64,
  #[pyo3(get)]
  pub last_gc_time: i64,
  #[pyo3(get)]
  pub committed_writes_size: i64,
  #[pyo3(get)]
  pub committed_writes_pruned: i64,
}

#[pymethods]
impl MvccStats {
  fn __repr__(&self) -> String {
    format!(
      "MvccStats(active_tx={}, gc_runs={}, versions_pruned={})",
      self.active_transactions, self.gc_runs, self.versions_pruned
    )
  }
}

/// Memory metrics
#[pyclass(name = "MemoryMetrics")]
#[derive(Debug, Clone)]
pub struct MemoryMetrics {
  #[pyo3(get)]
  pub delta_estimate_bytes: i64,
  #[pyo3(get)]
  pub cache_estimate_bytes: i64,
  #[pyo3(get)]
  pub snapshot_bytes: i64,
  #[pyo3(get)]
  pub total_estimate_bytes: i64,
}

#[pymethods]
impl MemoryMetrics {
  /// Get memory in human-readable format
  fn human_readable(&self) -> String {
    let bytes = self.total_estimate_bytes;
    if bytes < 1024 {
      format!("{bytes} B")
    } else if bytes < 1024 * 1024 {
      let kb = bytes as f64 / 1024.0;
      format!("{kb:.1} KB")
    } else if bytes < 1024 * 1024 * 1024 {
      let mb = bytes as f64 / (1024.0 * 1024.0);
      format!("{mb:.1} MB")
    } else {
      let gb = bytes as f64 / (1024.0 * 1024.0 * 1024.0);
      format!("{gb:.2} GB")
    }
  }

  fn __repr__(&self) -> String {
    format!(
      "MemoryMetrics(total={}, delta={}, cache={}, snapshot={})",
      self.human_readable(),
      self.delta_estimate_bytes,
      self.cache_estimate_bytes,
      self.snapshot_bytes
    )
  }
}

impl From<core_metrics::MemoryMetrics> for MemoryMetrics {
  fn from(metrics: core_metrics::MemoryMetrics) -> Self {
    MemoryMetrics {
      delta_estimate_bytes: metrics.delta_estimate_bytes,
      cache_estimate_bytes: metrics.cache_estimate_bytes,
      snapshot_bytes: metrics.snapshot_bytes,
      total_estimate_bytes: metrics.total_estimate_bytes,
    }
  }
}

/// Database metrics (complete snapshot)
#[pyclass(name = "DatabaseMetrics")]
#[derive(Debug, Clone)]
pub struct DatabaseMetrics {
  #[pyo3(get)]
  pub path: String,
  #[pyo3(get)]
  pub is_single_file: bool,
  #[pyo3(get)]
  pub read_only: bool,
  #[pyo3(get)]
  pub data: DataMetrics,
  #[pyo3(get)]
  pub cache: CacheMetrics,
  #[pyo3(get)]
  pub mvcc: Option<MvccMetrics>,
  #[pyo3(get)]
  pub memory: MemoryMetrics,
  #[pyo3(get)]
  pub collected_at: i64,
}

#[pymethods]
impl DatabaseMetrics {
  fn __repr__(&self) -> String {
    format!(
      "DatabaseMetrics(path='{}', nodes={}, edges={}, memory={})",
      self.path,
      self.data.node_count,
      self.data.edge_count,
      self.memory.human_readable()
    )
  }
}

impl From<core_metrics::DatabaseMetrics> for DatabaseMetrics {
  fn from(metrics: core_metrics::DatabaseMetrics) -> Self {
    DatabaseMetrics {
      path: metrics.path,
      is_single_file: metrics.is_single_file,
      read_only: metrics.read_only,
      data: metrics.data.into(),
      cache: metrics.cache.into(),
      mvcc: metrics.mvcc.map(Into::into),
      memory: metrics.memory.into(),
      collected_at: metrics.collected_at_ms,
    }
  }
}

/// Health check entry
#[pyclass(name = "HealthCheckEntry")]
#[derive(Debug, Clone)]
pub struct HealthCheckEntry {
  #[pyo3(get)]
  pub name: String,
  #[pyo3(get)]
  pub passed: bool,
  #[pyo3(get)]
  pub message: String,
}

#[pymethods]
impl HealthCheckEntry {
  fn __repr__(&self) -> String {
    let status = if self.passed { "PASS" } else { "FAIL" };
    format!(
      "HealthCheckEntry({}: {} - {})",
      self.name, status, self.message
    )
  }

  fn __bool__(&self) -> bool {
    self.passed
  }
}

impl From<core_metrics::HealthCheckEntry> for HealthCheckEntry {
  fn from(entry: core_metrics::HealthCheckEntry) -> Self {
    HealthCheckEntry {
      name: entry.name,
      passed: entry.passed,
      message: entry.message,
    }
  }
}

/// Health check result
#[pyclass(name = "HealthCheckResult")]
#[derive(Debug, Clone)]
pub struct HealthCheckResult {
  #[pyo3(get)]
  pub healthy: bool,
  #[pyo3(get)]
  pub checks: Vec<HealthCheckEntry>,
}

#[pymethods]
impl HealthCheckResult {
  /// Get count of passed checks
  fn passed_count(&self) -> usize {
    self.checks.iter().filter(|c| c.passed).count()
  }

  /// Get count of failed checks
  fn failed_count(&self) -> usize {
    self.checks.iter().filter(|c| !c.passed).count()
  }

  /// Get list of failed checks
  fn failed_checks(&self) -> Vec<HealthCheckEntry> {
    self.checks.iter().filter(|c| !c.passed).cloned().collect()
  }

  fn __repr__(&self) -> String {
    format!(
      "HealthCheckResult(healthy={}, passed={}/{})",
      self.healthy,
      self.passed_count(),
      self.checks.len()
    )
  }

  fn __bool__(&self) -> bool {
    self.healthy
  }
}

impl From<core_metrics::HealthCheckResult> for HealthCheckResult {
  fn from(result: core_metrics::HealthCheckResult) -> Self {
    HealthCheckResult {
      healthy: result.healthy,
      checks: result.checks.into_iter().map(Into::into).collect(),
    }
  }
}

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

  #[test]
  fn test_memory_metrics_human_readable() {
    let metrics = MemoryMetrics {
      delta_estimate_bytes: 1000,
      cache_estimate_bytes: 2000,
      snapshot_bytes: 3000,
      total_estimate_bytes: 1024 * 1024 * 50, // 50 MB
    };
    assert_eq!(metrics.human_readable(), "50.0 MB");
  }

  #[test]
  fn test_health_check_result_counts() {
    let result = HealthCheckResult {
      healthy: false,
      checks: vec![
        HealthCheckEntry {
          name: "check1".to_string(),
          passed: true,
          message: "ok".to_string(),
        },
        HealthCheckEntry {
          name: "check2".to_string(),
          passed: false,
          message: "failed".to_string(),
        },
      ],
    };
    assert_eq!(result.passed_count(), 1);
    assert_eq!(result.failed_count(), 1);
    assert_eq!(result.failed_checks().len(), 1);
  }
}