dominikbraun/graph-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graph-sql is an SQL storage implementation for graph data structures created with graph.

If you don't have a database running, you may spin up a MariaDB container with a graph database to get started quickly.

docker run -e MARIADB_DATABASE=graph -e MARIADB_ROOT_PASSWORD=root -p 3306:3306 mariadb

The first step is to establish a connection to your database server, more specifically to the actual database schema.

For instance, a connection to the MariaDB container from the example above can be established as follows:

package main

import (
	"database/sql"

	_ ".com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/graph")
	if err != nil {
		panic(err)
	}
}

Use the retrieved sql.DB instance to create a new store.

store := graphsql.New[int, int](db, graphsql.DefaultConfig)

The New function has two type parameters. These are the types of the vertex hashes and vertex values, and they have to be the same as for the graph itself. If you're not familiar with graph's hashing system, check out the concept of hashes.

This example uses a sane default configuration provided by this library, but you can configure it to your needs (see Configuration).

Create all required tables by calling SetupTables. This should only happen once.

if err := store.SetupTables(); err != nil {
	log.Fatal(err)
}

Finally, the store instance can be passed to graph.NewWithStore, which will create a graph backed by this store.

g := graph.NewWithStore(graph.IntHash, store)

A complete program that utilizes a MariaDB database to store a graph of integers may look like as follows:

package main

import (
	"database/sql"
	"log"

	".com/dominikbraun/graph"
	graphsql ".com/dominikbraun/graph-sql"
	_ ".com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/graph")
	if err != nil {
		panic(err)
	}

	store := graphsql.New[int, int](db, graphsql.DefaultConfig)

	if err = store.SetupTables(); err != nil {
		log.Fatal(err)
	}

	g := graph.NewWithStore(graph.IntHash, store)

	// This will persist two vertices and an edge in the database.
	_ = g.AddVertex(1)
	_ = g.AddVertex(2)
	_ = g.AddEdge(1, 2)
}

The table schema created by graph-sql can be configured using a Config passed to New. You can either use the provided DefaultConfig or create your own one.

FieldDescriptionDefault
VerticesTableThe name of the vertices table.vertices
EdgesTableThe name of the edges table.edges
VertexHashTypeThe database type of the vertex hashes.TEXT
VertexValueTypeThe database type of the vertex values.*JSON

*Vertex values are stored as JSON by this library.

ColumnTypeNULLKeyExtra
idBIGINTNoPrimaryAUTO_INCREMENT
hashTEXTYes
valueJSONYes
weightINTYes
attributesJSONYes
ColumnTypeNULLKeyExtra
idBIGINTNoPrimaryAUTO_INCREMENT
source_hashTEXTYes
target_hashTEXTYes
weightINTYes
attributesJSONYes
dataBLOBYes

Check out the graph repository for an overview of built-in graph algorithms and operations.

About

An SQL storage implementation for graph data structures.

Topics

Resources

License

Stars

Watchers

Forks

Languages