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
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
//! Export and Import utilities
//!
//! JSON and JSONL export/import for GraphDB and SingleFileDB.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;

use crate::core::single_file::SingleFileDB;
use crate::error::{RayError, Result};
use crate::graph::db::GraphDB;
use crate::graph::definitions::{define_etype, define_label, define_propkey};
use crate::graph::edges::{add_edge, get_edge_props_db};
use crate::graph::iterators::{
  list_edges as graph_list_edges, list_nodes as graph_list_nodes, ListEdgesOptions,
};
use crate::graph::key_index::get_node_key;
use crate::graph::nodes::{create_node, get_node_by_key_db, get_node_props_db, NodeOpts};
use crate::graph::tx::{begin_tx, commit, rollback};
use crate::types::{ETypeId, NodeId, PropKeyId, PropValue};

// =============================================================================
// Types
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportOptions {
  pub include_nodes: bool,
  pub include_edges: bool,
  pub include_schema: bool,
  pub pretty: bool,
}

impl Default for ExportOptions {
  fn default() -> Self {
    Self {
      include_nodes: true,
      include_edges: true,
      include_schema: true,
      pretty: false,
    }
  }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImportOptions {
  pub skip_existing: bool,
  pub batch_size: usize,
}

impl Default for ImportOptions {
  fn default() -> Self {
    Self {
      skip_existing: true,
      batch_size: 1000,
    }
  }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportedPropValue {
  pub r#type: String,
  pub value: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportedNode {
  pub id: u64,
  pub key: Option<String>,
  pub props: HashMap<String, ExportedPropValue>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportedEdge {
  pub src: u64,
  pub dst: u64,
  pub etype: u32,
  pub etype_name: Option<String>,
  pub props: HashMap<String, ExportedPropValue>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExportedSchema {
  pub labels: HashMap<u32, String>,
  pub etypes: HashMap<u32, String>,
  pub prop_keys: HashMap<u32, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportedDatabase {
  pub version: u32,
  pub exported_at: String,
  pub schema: ExportedSchema,
  pub nodes: Vec<ExportedNode>,
  pub edges: Vec<ExportedEdge>,
  pub stats: ExportStats,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportStats {
  pub node_count: usize,
  pub edge_count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportResult {
  pub node_count: usize,
  pub edge_count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImportResult {
  pub node_count: usize,
  pub edge_count: usize,
  pub skipped: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonLine<T> {
  pub r#type: String,
  pub data: Option<T>,
}

// =============================================================================
// PropValue Serialization
// =============================================================================

fn serialize_prop_value(value: &PropValue) -> ExportedPropValue {
  match value {
    PropValue::Null => ExportedPropValue {
      r#type: "null".to_string(),
      value: serde_json::Value::Null,
    },
    PropValue::String(v) => ExportedPropValue {
      r#type: "string".to_string(),
      value: serde_json::Value::String(v.clone()),
    },
    PropValue::I64(v) => ExportedPropValue {
      r#type: "int".to_string(),
      value: serde_json::Value::Number((*v).into()),
    },
    PropValue::F64(v) => ExportedPropValue {
      r#type: "float".to_string(),
      value: serde_json::Value::Number(
        serde_json::Number::from_f64(*v).unwrap_or_else(|| 0.into()),
      ),
    },
    PropValue::Bool(v) => ExportedPropValue {
      r#type: "bool".to_string(),
      value: serde_json::Value::Bool(*v),
    },
    PropValue::VectorF32(v) => ExportedPropValue {
      r#type: "vector".to_string(),
      value: serde_json::Value::Array(
        v.iter()
          .map(|x| {
            serde_json::Value::Number(
              serde_json::Number::from_f64(*x as f64).unwrap_or_else(|| 0.into()),
            )
          })
          .collect(),
      ),
    },
  }
}

fn deserialize_prop_value(value: &ExportedPropValue) -> PropValue {
  match value.r#type.as_str() {
    "null" => PropValue::Null,
    "string" => PropValue::String(value.value.as_str().unwrap_or_default().to_string()),
    "int" => PropValue::I64(value.value.as_i64().unwrap_or_default()),
    "float" => PropValue::F64(value.value.as_f64().unwrap_or_default()),
    "bool" => PropValue::Bool(value.value.as_bool().unwrap_or(false)),
    "vector" => {
      let mut vec = Vec::new();
      if let Some(values) = value.value.as_array() {
        for v in values {
          vec.push(v.as_f64().unwrap_or_default() as f32);
        }
      }
      PropValue::VectorF32(vec)
    }
    _ => PropValue::Null,
  }
}

// =============================================================================
// Schema Helpers
// =============================================================================

fn build_schema_from_delta(delta: &crate::types::DeltaState) -> ExportedSchema {
  let mut schema = ExportedSchema::default();
  for (id, name) in &delta.new_labels {
    schema.labels.insert(*id, name.clone());
  }
  for (id, name) in &delta.new_etypes {
    schema.etypes.insert(*id, name.clone());
  }
  for (id, name) in &delta.new_propkeys {
    schema.prop_keys.insert(*id, name.clone());
  }
  schema
}

fn get_prop_key_name_graph(db: &GraphDB, key_id: PropKeyId) -> String {
  db.get_propkey_name(key_id)
    .unwrap_or_else(|| format!("prop_{key_id}"))
}

fn get_etype_name_graph(db: &GraphDB, etype_id: ETypeId) -> String {
  db.get_etype_name(etype_id)
    .unwrap_or_else(|| format!("etype_{etype_id}"))
}

fn get_prop_key_name_single(db: &SingleFileDB, key_id: PropKeyId) -> String {
  db.get_propkey_name(key_id)
    .unwrap_or_else(|| format!("prop_{key_id}"))
}

fn get_etype_name_single(db: &SingleFileDB, etype_id: ETypeId) -> String {
  db.get_etype_name(etype_id)
    .unwrap_or_else(|| format!("etype_{etype_id}"))
}

// =============================================================================
// Export (GraphDB)
// =============================================================================

pub fn export_to_object_graph(db: &GraphDB, options: ExportOptions) -> Result<ExportedDatabase> {
  let delta = db.delta.read();
  let schema = if options.include_schema {
    build_schema_from_delta(&delta)
  } else {
    ExportedSchema::default()
  };

  let mut nodes = Vec::new();
  let mut edges = Vec::new();

  if options.include_nodes {
    for node_id in graph_list_nodes(db) {
      let key = get_node_key(db.snapshot.as_ref(), &delta, node_id);
      let mut props = HashMap::new();
      if let Some(props_by_id) = get_node_props_db(db, node_id) {
        for (key_id, value) in props_by_id {
          let name = get_prop_key_name_graph(db, key_id);
          props.insert(name, serialize_prop_value(&value));
        }
      }

      nodes.push(ExportedNode {
        id: node_id,
        key,
        props,
      });
    }
  }

  if options.include_edges {
    let options = ListEdgesOptions::default();
    for edge in graph_list_edges(db, options) {
      let mut props = HashMap::new();
      if let Some(props_by_id) = get_edge_props_db(db, edge.src, edge.etype, edge.dst) {
        for (key_id, value) in props_by_id {
          let name = get_prop_key_name_graph(db, key_id);
          props.insert(name, serialize_prop_value(&value));
        }
      }
      edges.push(ExportedEdge {
        src: edge.src,
        dst: edge.dst,
        etype: edge.etype,
        etype_name: Some(get_etype_name_graph(db, edge.etype)),
        props,
      });
    }
  }

  let exported_at = match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
    Ok(duration) => duration.as_secs().to_string(),
    Err(_) => "0".to_string(),
  };

  let node_count = nodes.len();
  let edge_count = edges.len();

  Ok(ExportedDatabase {
    version: 1,
    exported_at,
    schema,
    nodes,
    edges,
    stats: ExportStats {
      node_count,
      edge_count,
    },
  })
}

pub fn export_to_object_single(
  db: &SingleFileDB,
  options: ExportOptions,
) -> Result<ExportedDatabase> {
  let delta = db.delta.read();
  let schema = if options.include_schema {
    build_schema_from_delta(&delta)
  } else {
    ExportedSchema::default()
  };

  let mut nodes = Vec::new();
  let mut edges = Vec::new();

  if options.include_nodes {
    for node_id in db.list_nodes() {
      let key = db.get_node_key(node_id);
      let mut props = HashMap::new();
      if let Some(props_by_id) = db.get_node_props(node_id) {
        for (key_id, value) in props_by_id {
          let name = get_prop_key_name_single(db, key_id);
          props.insert(name, serialize_prop_value(&value));
        }
      }
      nodes.push(ExportedNode {
        id: node_id,
        key,
        props,
      });
    }
  }

  if options.include_edges {
    for edge in db.list_edges(None) {
      let mut props = HashMap::new();
      if let Some(props_by_id) = db.get_edge_props(edge.src, edge.etype, edge.dst) {
        for (key_id, value) in props_by_id {
          let name = get_prop_key_name_single(db, key_id);
          props.insert(name, serialize_prop_value(&value));
        }
      }
      edges.push(ExportedEdge {
        src: edge.src,
        dst: edge.dst,
        etype: edge.etype,
        etype_name: Some(get_etype_name_single(db, edge.etype)),
        props,
      });
    }
  }

  let exported_at = match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
    Ok(duration) => duration.as_secs().to_string(),
    Err(_) => "0".to_string(),
  };

  let node_count = nodes.len();
  let edge_count = edges.len();

  Ok(ExportedDatabase {
    version: 1,
    exported_at,
    schema,
    nodes,
    edges,
    stats: ExportStats {
      node_count,
      edge_count,
    },
  })
}

pub fn export_to_json<P: AsRef<Path>>(
  data: &ExportedDatabase,
  path: P,
  pretty: bool,
) -> Result<ExportResult> {
  let file = File::create(path).map_err(RayError::Io)?;
  let mut writer = BufWriter::new(file);
  if pretty {
    serde_json::to_writer_pretty(&mut writer, data)
      .map_err(|e| RayError::Serialization(e.to_string()))?;
  } else {
    serde_json::to_writer(&mut writer, data).map_err(|e| RayError::Serialization(e.to_string()))?;
  }
  writer.flush().map_err(RayError::Io)?;
  Ok(ExportResult {
    node_count: data.stats.node_count,
    edge_count: data.stats.edge_count,
  })
}

pub fn export_to_jsonl<P: AsRef<Path>>(data: &ExportedDatabase, path: P) -> Result<ExportResult> {
  let file = File::create(path).map_err(RayError::Io)?;
  let mut writer = BufWriter::new(file);

  let header = JsonLine::<serde_json::Value> {
    r#type: "header".to_string(),
    data: Some(serde_json::json!({
      "version": data.version,
      "exportedAt": data.exported_at,
    })),
  };
  writeln!(
    writer,
    "{}",
    serde_json::to_string(&header).map_err(|e| RayError::Serialization(e.to_string()))?
  )
  .map_err(RayError::Io)?;

  let schema = JsonLine {
    r#type: "schema".to_string(),
    data: Some(
      serde_json::to_value(&data.schema).map_err(|e| RayError::Serialization(e.to_string()))?,
    ),
  };
  writeln!(
    writer,
    "{}",
    serde_json::to_string(&schema).map_err(|e| RayError::Serialization(e.to_string()))?
  )
  .map_err(RayError::Io)?;

  for node in &data.nodes {
    let line = JsonLine {
      r#type: "node".to_string(),
      data: Some(serde_json::to_value(node).map_err(|e| RayError::Serialization(e.to_string()))?),
    };
    writeln!(
      writer,
      "{}",
      serde_json::to_string(&line).map_err(|e| RayError::Serialization(e.to_string()))?
    )
    .map_err(RayError::Io)?;
  }

  for edge in &data.edges {
    let line = JsonLine {
      r#type: "edge".to_string(),
      data: Some(serde_json::to_value(edge).map_err(|e| RayError::Serialization(e.to_string()))?),
    };
    writeln!(
      writer,
      "{}",
      serde_json::to_string(&line).map_err(|e| RayError::Serialization(e.to_string()))?
    )
    .map_err(RayError::Io)?;
  }

  writer.flush().map_err(RayError::Io)?;
  Ok(ExportResult {
    node_count: data.stats.node_count,
    edge_count: data.stats.edge_count,
  })
}

// =============================================================================
// Import (GraphDB)
// =============================================================================

pub fn import_from_object_graph(
  db: &GraphDB,
  data: &ExportedDatabase,
  options: ImportOptions,
) -> Result<ImportResult> {
  let mut propkey_name_to_id: HashMap<String, PropKeyId> = HashMap::new();
  let mut etype_name_to_id: HashMap<String, ETypeId> = HashMap::new();

  let mut tx = begin_tx(db)?;
  for name in data.schema.prop_keys.values() {
    let id = db
      .get_propkey_id(name)
      .unwrap_or_else(|| define_propkey(&mut tx, name).unwrap_or(0));
    propkey_name_to_id.insert(name.clone(), id);
  }
  for name in data.schema.etypes.values() {
    let id = db
      .get_etype_id(name)
      .unwrap_or_else(|| define_etype(&mut tx, name).unwrap_or(0));
    etype_name_to_id.insert(name.clone(), id);
  }
  for name in data.schema.labels.values() {
    let _ = db
      .get_label_id(name)
      .unwrap_or_else(|| define_label(&mut tx, name).unwrap_or(0));
  }
  commit(&mut tx)?;

  let mut old_to_new: HashMap<NodeId, NodeId> = HashMap::new();
  let mut node_count = 0usize;
  let mut skipped = 0usize;
  let mut batch_count = 0usize;

  let mut tx = begin_tx(db)?;
  for node in &data.nodes {
    if options.skip_existing {
      if let Some(ref key) = node.key {
        if let Some(existing) = get_node_by_key_db(db, key) {
          old_to_new.insert(node.id as NodeId, existing);
          skipped += 1;
          continue;
        }
      }
    }

    let mut node_opts = NodeOpts::new();
    if let Some(ref key) = node.key {
      node_opts = node_opts.with_key(key);
    }
    let node_id = match create_node(&mut tx, node_opts) {
      Ok(id) => id,
      Err(e) => {
        rollback(&mut tx)?;
        return Err(e);
      }
    };

    for (prop_name, exported_value) in &node.props {
      if let Some(&key_id) = propkey_name_to_id.get(prop_name) {
        let value = deserialize_prop_value(exported_value);
        crate::graph::nodes::set_node_prop(&mut tx, node_id, key_id, value)?;
      }
    }

    old_to_new.insert(node.id as NodeId, node_id);
    node_count += 1;
    batch_count += 1;

    if batch_count >= options.batch_size {
      commit(&mut tx)?;
      tx = begin_tx(db)?;
      batch_count = 0;
    }
  }

  if batch_count > 0 {
    commit(&mut tx)?;
  }

  let mut edge_count = 0usize;
  let mut batch_count = 0usize;
  let mut tx = begin_tx(db)?;
  for edge in &data.edges {
    let src = match old_to_new.get(&(edge.src as NodeId)) {
      Some(id) => *id,
      None => continue,
    };
    let dst = match old_to_new.get(&(edge.dst as NodeId)) {
      Some(id) => *id,
      None => continue,
    };

    let etype_id = edge
      .etype_name
      .as_ref()
      .and_then(|name| etype_name_to_id.get(name).copied())
      .unwrap_or(edge.etype as ETypeId);

    add_edge(&mut tx, src, etype_id, dst)?;
    edge_count += 1;
    batch_count += 1;

    if batch_count >= options.batch_size {
      commit(&mut tx)?;
      tx = begin_tx(db)?;
      batch_count = 0;
    }
  }

  if batch_count > 0 {
    commit(&mut tx)?;
  }

  Ok(ImportResult {
    node_count,
    edge_count,
    skipped,
  })
}

pub fn import_from_object_single(
  db: &SingleFileDB,
  data: &ExportedDatabase,
  options: ImportOptions,
) -> Result<ImportResult> {
  let mut propkey_name_to_id: HashMap<String, PropKeyId> = HashMap::new();
  let mut etype_name_to_id: HashMap<String, ETypeId> = HashMap::new();

  for name in data.schema.prop_keys.values() {
    let id = db
      .get_propkey_id(name)
      .unwrap_or_else(|| db.define_propkey(name).unwrap_or(0));
    propkey_name_to_id.insert(name.clone(), id);
  }
  for name in data.schema.etypes.values() {
    let id = db
      .get_etype_id(name)
      .unwrap_or_else(|| db.define_etype(name).unwrap_or(0));
    etype_name_to_id.insert(name.clone(), id);
  }
  for name in data.schema.labels.values() {
    let _ = db
      .get_label_id(name)
      .unwrap_or_else(|| db.define_label(name).unwrap_or(0));
  }

  let mut old_to_new: HashMap<NodeId, NodeId> = HashMap::new();
  let mut node_count = 0usize;
  let mut skipped = 0usize;
  let mut batch_count = 0usize;

  db.begin(false)?;
  for node in &data.nodes {
    if options.skip_existing {
      if let Some(ref key) = node.key {
        if let Some(existing) = db.get_node_by_key(key) {
          old_to_new.insert(node.id as NodeId, existing);
          skipped += 1;
          continue;
        }
      }
    }

    let node_id = db.create_node(node.key.as_deref())?;
    for (prop_name, exported_value) in &node.props {
      if let Some(&key_id) = propkey_name_to_id.get(prop_name) {
        let value = deserialize_prop_value(exported_value);
        db.set_node_prop(node_id, key_id, value)?;
      }
    }

    old_to_new.insert(node.id as NodeId, node_id);
    node_count += 1;
    batch_count += 1;

    if batch_count >= options.batch_size {
      db.commit()?;
      db.begin(false)?;
      batch_count = 0;
    }
  }

  if batch_count > 0 {
    db.commit()?;
  }

  let mut edge_count = 0usize;
  let mut batch_count = 0usize;
  db.begin(false)?;
  for edge in &data.edges {
    let src = match old_to_new.get(&(edge.src as NodeId)) {
      Some(id) => *id,
      None => continue,
    };
    let dst = match old_to_new.get(&(edge.dst as NodeId)) {
      Some(id) => *id,
      None => continue,
    };

    let etype_id = edge
      .etype_name
      .as_ref()
      .and_then(|name| etype_name_to_id.get(name).copied())
      .unwrap_or(edge.etype as ETypeId);

    db.add_edge(src, etype_id, dst)?;
    edge_count += 1;
    batch_count += 1;

    if batch_count >= options.batch_size {
      db.commit()?;
      db.begin(false)?;
      batch_count = 0;
    }
  }

  if batch_count > 0 {
    db.commit()?;
  }

  Ok(ImportResult {
    node_count,
    edge_count,
    skipped,
  })
}

pub fn import_from_json<P: AsRef<Path>>(path: P) -> Result<ExportedDatabase> {
  let file = File::open(path).map_err(RayError::Io)?;
  let reader = BufReader::new(file);
  let data: ExportedDatabase =
    serde_json::from_reader(reader).map_err(|e| RayError::Serialization(e.to_string()))?;
  Ok(data)
}