Function terminus_store::store::open_directory_store
source · pub fn open_directory_store<P: Into<PathBuf>>(path: P) -> Store
Expand description
Open a store that stores its data in the given directory.
Examples found in repository?
examples/create_graph.rs (line 13)
7 8 9 10 11 12 13 14 15 16 17 18
async fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
println!("usage: {} <path> <graph_name>", args[0]);
} else {
// open a store at the given path. the directory has to exist.
let store = open_directory_store(&args[1]);
// then create a graph. if the graph already exists, this will error.
store.create(&args[2]).await.unwrap();
}
}
More examples
examples/print_graph.rs (line 9)
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
async fn print_graph(store_path: &str, graph: &str) -> io::Result<()> {
let store = open_directory_store(store_path);
let graph = store
.open(graph)
.await?
.expect(&format!("expected graph {} to exist", graph));
match graph.head().await? {
Some(layer) => {
for id_triple in layer.triples() {
// triples are retrieved in their id form. For printing,
// we need the string form. The conversion happens here.
let triple = layer
.id_triple_to_string(&id_triple)
.expect("expected id triple to be mapable to string");
println!(
"{}, {}, {} {:?}",
triple.subject,
triple.predicate,
match triple.object {
ObjectType::Node(_) => "node",
ObjectType::Value(_) => "value",
},
match triple.object {
ObjectType::Node(n) => String::make_entry(&n),
ObjectType::Value(v) => v,
}
);
}
}
None => {}
}
Ok(())
}
examples/write_to_graph.rs (line 55)
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
async fn process_commands(store_path: &str, graph: &str) -> io::Result<()> {
let store = open_directory_store(store_path);
let graph = store
.open(graph)
.await?
.expect(&format!("expected graph {} to exist", graph));
// There are two types of builders. One creates a new base layer,
// which has no parent. The other creates a child layer, which has
// another layer as its parent.
let builder = match graph.head().await? {
Some(layer) => layer.open_write().await?,
None => store.create_base_layer().await?,
};
let mut stdin = io::BufReader::new(io::stdin()).lines();
while let Some(line) = stdin.next_line().await? {
let segment = line.trim();
if segment.len() == 0 {
continue;
}
let command = parse_command(segment).await?;
// add all the input data into the builder.
// The builder keeps an in-memory list of added and removed
// triples. If the same triple is added and removed on the
// same builder, it is a no-op. This is even the case when it
// is then later re-added on the same builder.
//
// Since no io is happening, adding triples to the builder is
// not a future.
match command {
Command::Add(triple) => builder.add_value_triple(triple)?,
Command::Remove(triple) => builder.remove_value_triple(triple)?,
}
}
// When commit is called, the builder writes its data to
// persistent storage.
let layer = builder.commit().await?;
// While a layer exists now, it's not yet attached to anything,
// and is therefore unusable unless you know the exact identifier
// of the layer itself. To make this the graph data, we have to
// set the grap head to this layer.
graph.set_head(&layer).await?;
println!(
"Added: {}, removed: {}",
layer.triple_layer_addition_count().await?,
layer.triple_layer_removal_count().await?
);
Ok(())
}