gruphst 0.2.0

An in-memory 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
<div class="text" align="center">
    <img src="https://img.shields.io/github/actions/workflow/status/carvilsi/gruphst/test.yml?logo=github&label=tests" alt="test">
    <p></p>
    <p>GruPHst</p>
    <p>An in-memory graph database</p>
</div> 

---

# GruPHst

An in-memory graph database.
 
Possible to persists on file (just because is something that we always expect from an in-memory databases).

Early state of development with lot of TODOs, just doing nerdy things with Graph Databases while trying to learn some Rust.

[Documentation](https://docs.rs/gruphst/latest/gruphst/)


**To run tests**

`$ cargo test`

To run tests with debug output:

`$ cargo test -- --show-output`

**Install**

Run the following Cargo command in your project directory:

`$ cargo add gruphst`

Or add the following line to your Cargo.toml:

`gruphst = "0.1.0"`


## Nodes


Creates a Node with the given name, the id is generated


```rust
use gruphst::Node;

let node = Node::new("alice node");
```

Updates the name of the Node


```rust
use gruphst::Node;

let mut node = Node::new("alice node");
node.update_name("just alice");
```

### Node Attributes

Set attributes for a Node

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
```

Get attributes for a Node

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
let attr = node.get_attr("Address").unwrap();
```

Returns an Array containing all attribute keys

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
node.set_attr("age", 44);
let keys = node.get_attr_keys();
assert!(keys.contains(&&"age"));
```

Updates the value of an attribute

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
node.set_attr("age", 44);
node.update_attr("age", 55);
```

Updates the value of an attribute or creates a new one if attribute key does not exists

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
node.upsert_attr("age", 44);
assert_eq!(node.get_attr("age").unwrap(), "44");
node.upsert_attr("age", 55);
assert_eq!(node.get_attr("age").unwrap(), "55");
```

Retrieves the lenght of attributes for a Node

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
node.set_attr("Address", "Elm street");
node.set_attr("age", 25);
assert_eq!(node.len_attr(), 2);
```

Checks if attributes for a Node is empty

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
assert!(node.is_empty_attr());
node.set_attr("Address", "Elm street");
node.set_attr("age", 25);
assert!(!node.is_empty_attr());
```

Deletes an attribute for a Node

```rust
use gruphst::Node;

let mut node = Node::new("Alice");
assert!(node.is_empty_attr());
node.set_attr("Address", "Elm street");
assert!(!node.is_empty_attr());
node.del_attr("Address");
assert!(node.is_empty_attr());
```

## Graph

Representation of a Graph, relating two nodes

Creates a Graph, the id is generated

```rust
use gruphst::Node;
use gruphst::Graph;

let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob_graph =
    Graph::new(&alice, "friend of", &bob);
```

Updates the relation for the Graph

```rust
use gruphst::Node;
use gruphst::Graph;

let alice = Node::new("Alice");
let bob = Node::new("Bob");
let mut alice_bob_graph = Graph::new(&alice, "friend of", &bob);

assert_eq!(alice_bob_graph.relation, "friend of");

alice_bob_graph.update_relation("best friends");
assert_eq!(alice_bob_graph.relation, "best friends");
```

Updates the "from" node in Graph

```rust
use gruphst::Node;
use gruphst::Graph;

let mut alice_node = Node::new("alice node");
let bob_node = Node::new("bob node");
let mut graph = Graph::new(&alice_node, "best friends", &bob_node);
assert_eq!(graph.from.name, "alice node");
assert_eq!(graph.to.name, "bob node");
alice_node.update_name("alice");
graph.update_from(&alice_node);
assert_eq!(graph.from.name, "alice");
```

Updates the "to" node in Graph

```rust
use gruphst::Node;
use gruphst::Graph;

let alice_node = Node::new("alice node");
let bob_node = Node::new("bob node");
let mut graph = Graph::new(&alice_node, "best friends", &bob_node);
assert_eq!(graph.from.name, "alice node");
assert_eq!(graph.to.name, "bob node");
let fred_node = Node::new("fred node");
graph.update_to(&fred_node);
assert_eq!(graph.to.name, "fred node");
assert_ne!(graph.to.id, bob_node.id);
```

### Graphs

A colection of Graph

Creates a new collection of Graph elements

```rust
use gruphst::Graphs;

let my_graph = Graphs::new("my_graph");
```

Retrieves the length of the Graphs

```rust
use gruphst::{ Graphs, Node, Graph };

let mut graphs = Graphs::new("lengths");
let alice = Node::new("Alice");
let bob = Node::new("Bob");

graphs.add(&Graph::new(&alice, "friend", &bob));
graphs.add(&Graph::new(&bob, "friend", &alice));

assert_eq!(graphs.len(), 2);
```

Checks if the Graphs is empty

```rust
use gruphst::{ Graphs, Node, Graph };

let mut graphs = Graphs::new("lengths");

assert!(graphs.is_empty());

let alice = Node::new("Alice");
let bob = Node::new("Bob");

graphs.add(&Graph::new(&alice, "friend", &bob));
graphs.add(&Graph::new(&bob, "friend", &alice));

assert!(!graphs.is_empty());
```

Updates the name of the Graphs


```rust
use gruphst::Graphs;

let mut my_graph = Graphs::new("my_graph");
assert_eq!(my_graph.name, "my_graph");

my_graph.update_name("graphy");
assert_eq!(my_graph.name, "graphy");
```

Adds a Graph element to the colection

```rust
use gruphst::{ Graphs, Node, Graph };

let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob_graph = Graph::new(&alice, "friend of", &bob);
let mut my_graph = Graphs::new("my_graph");
my_graph.add(&alice_bob_graph);
```

Returns a collection of Graps elements that matches the relation

```rust
use gruphst::{ Graphs, Node, Graph };

let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob_graph = Graph::new(&alice, "friend of", &bob);
let mut my_graph = Graphs::new("my_graph");
my_graph.add(&alice_bob_graph);

let result_graph = my_graph.find_by_relation("friend of").unwrap();
assert_eq!(result_graph.len(), 1);
```

Returns a Graph that provided id matches with Graph, or From, To Nodes

```rust
use gruphst::{ Graphs, Node, Graph };


let mut my_graph = Graphs::new("friends");
let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob = Graph::new(&alice, "is friend of", &bob);
my_graph.add(&alice_bob);

let alice_fred =
    Graph::new(&alice, "is firend of", &Node::new("Fred"));
my_graph.add(&alice_fred);

let bob_node_id = bob.id;
let res = my_graph.find_by_id(&bob_node_id);
assert_eq!(res.unwrap().to.id, bob_node_id);
```

Deletes the Graph that matches with the provided id

```rust
use gruphst::{ Graphs, Node, Graph };

let mut my_graph = Graphs::new("friends");
let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob = Graph::new(&alice, "is friend of", &bob);
my_graph.add(&alice_bob);

let alice_fred =
    Graph::new(&alice, "is firend of", &Node::new("Fred"));
my_graph.add(&alice_fred);

assert_eq!(my_graph.len(), 2);

my_graph.delete_graph_by_id(alice_bob.id);
assert_eq!(my_graph.len(), 1);
```

Updates the Graphs with the provided one

```rust
use gruphst::{ Graphs, Node, Graph };

let mut my_graphs = Graphs::new("my-graphs");

let alice_node = Node::new("Alice");
let bob_node = Node::new("Bob");
let alice_bob_graph =
    Graph::new(&alice_node, "best friends", &bob_node);
my_graphs.add(&alice_bob_graph);

let fred_node = Node::new("Fred");
let mut alice_fred_graph =
    Graph::new(&alice_node, "super friends", &fred_node);
my_graphs.add(&alice_fred_graph);

assert_eq!(my_graphs.len(), 2);
assert_eq!(my_graphs.graphs[1].relation, "super friends");

alice_fred_graph.update_relation("besties");
my_graphs.update_graph(&alice_fred_graph);

assert_eq!(my_graphs.len(), 2);
let updated_graph = my_graphs.find_by_id(&alice_fred_graph.id);
assert_eq!(updated_graph.unwrap().relation, "besties");
```

### Graphs Stats

Returns stats from Graphs; size in bytes, amount of graph, name, total number of attributes and total amount of Nodes

```rust
use gruphst::{ Graphs, Node, Graph };

let mut my_graphs = Graphs::new("memories");
my_graphs.add(
    &Graph::new(
        &Node::new("Alice"),
        "recalls friendship with",
        &Node::new("Bob")
    )
);

let stats = my_graphs.stats().unwrap();
assert_eq!(stats.mem, 271);
assert_eq!(stats.len, 1);
assert_eq!(stats.name, "memories");
```

### Persistence

#### Save to file

Saves the current Graphs into a file with the Graphs's name

```rust
use gruphst::{ Graphs, Node, Graph };


let mut my_graph = Graphs::new("friends");
let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob = Graph::new(&alice, "is friend of", &bob);
my_graph.add(&alice_bob);

my_graph.persists();
```

#### Load from file

Loads the persisted Graphs on a file

```rust
use gruphst::{ Graphs, Node, Graph };

let mut my_graph = Graphs::new("friends");
let alice = Node::new("Alice");
let bob = Node::new("Bob");
let alice_bob = Graph::new(&alice, "is friend of", &bob);
my_graph.add(&alice_bob);

let _ = my_graph.persists();

let name = my_graph.name;
let file_name = format!("{}.grphst", name);
let loaded_graphs = Graphs::load(&file_name);
match loaded_graphs {
    Ok(loaded_graphs) => {
        assert_eq!(loaded_graphs.name, name);
        assert_eq!(loaded_graphs.graphs[0].relation, alice_bob.relation);
    },
    Err(_) => panic!(),
}
```

## Loging

Enables logging providing a level

```rust
use gruphst::enable_logging;

enable_logging(log::Level::Info);
```
---

Feedback from usage and contributions are very welcome.
Also if you like it, please leave a :star: I would appreciate it ;)