vkurenkov / graphy-db

Graph Database
MIT License
5 stars 4 forks source link
csharp database graph-database windows

GraphyDb

This repository contains a graph database GraphyDb written using .NET Framework (version 4.7).

All classes you need belong to namespace GraphyDb

How to create Nodes, Relations and assign Properties

Open database connection:

DbEngine engine = new DbEngine();

DbEngine implements interface IDisposable to nicely close all open file streams. After finishing your work with DB you should call method Dispose() by yourself or use operator using

using (var engine = new DbEngine()) {
    ...
}

Create new node with label "user" (every node must have a label):

Node user1 = engine.AddNode("user");

Add property "age" (propety keys must be strings) with value 19 to user1:

user1["age"] = 19;

GraphyDb currently supports node and relation properties with one of the following types of values:

Add one more node with label "user" with property "age" set to 20:

Node user2 = engine.AddNode("user");
user2["age"] = 20;

Create relation with label "knows" from user1 to user2:

var relation1 = engine.AddRelation(user1, user2, "knows")

You can also assign properties to relations:

relation1["some_property"] = "zzz";

To commit changes (write them on disk):

engine.SaveChanges();

How to delete Nodes, Relations, properties

You can delete Nodes and Relations via uniform syntax:

engine.Delete(x); // x can be a Node or Relation

Or call method Delete directly on Node or Relation:

x.Delete(); // x can be a Node or Relation

To delete property:

x.DeleteProperty("property_name"); // x can be a Node or Relation

Do not forget to commit changes on disk:

engine.SaveChanges();

How to make queries

5 classes were created for querying:

NodeDescription and RelationDescription encapsulate required search parameters for Nodes and Relations respectively: you can search them by label (place null if you don't want to specify label) and by properties (place empty dictionary if you don't want to specify properties). Also there are shortcuts NodeDescription.Any() and RelationDescription.Any(), which match all Nodes / Relations.

These classes represent results of a Query. nodeSet.Nodes is a HashSet<Node> with found Nodes. relationSet.Relations is a HashSet<Relation> with found Relations

The main class to create a Query. The following examples will help you understand its syntax:

To get all Nodes matching particular nodeDescription:

var query = new Query(engine);

var nodeSet = query.Match(nodeDescription);

query.Execute();

After query.Execute(); nodeSet with be populated with found nodes.

More complex example:

var query = new Query(engine);

var A = query.Match(nodeDescriptionForA);
var x = query.From(relationDescriptionForX);
var B = query.Match(nodeDescriptionForB);
var y = query.To(relationDescriptionForY);
var C = query.Match(nodeDescriptionForC);

query.Execute();

After query.Execute(); objects A, B and C (which are of class NodeSet) will be populated with Nodes; x, y (which are of class RelationSet) will be populated with Relations such that:

A <-x- B -y-> C (x is a relation from B to A and y is a relation from B to C) considering all node and relation descriptions. With this simple syntax you can create as complex queries as you want.

To delete entire database

engine.DropDatabase();