Skip to content

geekskool/graphology-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphology-go

Graphology-go is a persistent in-memory graph database written in Go lang. It uses Gremlin type queries to query the Graph database.

Getting Started

Installing

To start using graphology-go, install Go and run go get:

$ go get github.com/geekskool/graphology-go

This will retrieve the library and place it in your $GOPATH

Using graphology-go

Import graphology-go

import "github.com/geekskool/graphology-go"

This will import graphology library into your package

Setting up database

We have to set root directory for db files

error := graphology.SetPath("/path/to/database/")

We can not create or open a database without setting a path. Returns error if it fails to set a path.

Creating a new empty database

Factory function for creating an empty graph database

graph, err := graphology.CreateGraph("dbname")

The dbname is the name of the newly created graph database. The database will be persisted in a file named as dbname.db in the location set using setPath api.

Opening an existing database

This will restore(retrieve) a database from a db file

graph, err = graphology.Open("dbname")

This will return a graph database with the graph data from the db file.

Listing all available databases

List all the database available in the set path.

dblst := graphology.ListAllDBs()

Creating a new node and set values for the node

vertex := CreateVertex()

Creates a variable of Vertex type , initializes its fields and returns Vertex. You have to set the values for the node

vertex.SetVertexValues(id:"saturn", in :nil, out:nil, lab:[]string{"titan"}, props:map[string]anything{"age": 10000})

Set Values for the fields of the Vertex

Add a node to the graph

id, err := graph.AddVertex(vertex)

Takes a Vertex variable as an argument and returns an ID of the inserted node. You can also add array of Vertices to the Graph.

ids, errs := graph.AddVertices(vertices)

Takes an vertices variable which is an array of Vertex ([]Vertex) as an argument and returns an array of ID's of the inserted nodes.

Creating a new edge and set values for the edge

edge := CreateEdge()

Creates a variable of Edge type , initializes its fields and returns Edge. You have to set the values for the Edge

edge.SetEdgeValues( head:v2.Vname, tail:v4.Vname, lab:"lives", props:map[string]anything{"reason": "loves fresh breezes"})

Set Values for the fields of the edge. edge id is automatically generated

Adding an edge/relationship to the graph

id, err := graph.AddEdge(edge)

Takes Edge variable as an argument and returns an ID of the inserted edge. You can also add array of Edges to the Graph.

ids, errs := graph.AddEdges(edges)

Takes an edges variable which is an array of Edge ([]Edge) as an argument and returns an array of ID's of the inserted edges.

Saving the database

Saves the database as dbname.db in the path set.

err := graph.Save()

Initializing a query

To initialize a query use a graph.V() function :

query := graph.V("name")

Sets the query to start with some Vertex with name "name" and returns a query so that we can chain the queries.

Available Queries

To search all the nodes connected with outgoing edges with the specified relation relationship_type, use query.Out() function.

query = query.Out("relationship_type")

To search all the nodes connected with incoming edges with the specified relation relationship_type, use query.In() function.

query = query.In("relationship_type")

To search all the nodes connected with both outgoing and incoming edges with the specified relation relationship_type, use query.Both() function.

query = query.Both("relationship_type")

To only get the unique nodes, use query.Unique() function.

query = query.Unique()

To only get specified number of nodes, use query.Take() function. It takes a number as an argument.

query = query.Take(limit)

To get all the results except a specific node, use query.Except() function. It takes a node name as an argument.

query = query.Except("node_name")

To filter out results with a specified condition, use query.Except() function. It takes a function which takes a vertex as an arguement and returns a bool.

filterFunc = func (v Vertex) bool{
    //do what ever you want with vertex
    //return a bool
}
query = query.Filter(filterFunc)

To get the results from the query, use query.Values() function.

results := query.Values()

The results is an array of the matched vertices.

Structures of Vertex and Edge

Vertex

//structure to hold vertex data
type Vertex struct {
    Id    string
    Label []string
    In    []string
    Out   []string
    Props map[string]anything
}

Edge

//structure to hold edge data
type Edge struct {
    Id    string
    Label string
    Tail  string
    Head  string
    Props map[string]anything
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages