groph

package module
Version: v0.0.0-...-40a3109 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2017 License: MIT Imports: 3 Imported by: 0

README

groph

A WIP simple database library written in Go

This is more like a research project around an idea I had of making queries on a simple using callbacks than a serious simple database ready for production use.

You can load data in the graph by usin JSON or YAML data in "triples" format or a more advanced "extended" format:

Downloading and installing

go get -u github.com/sayden/groph

Quickstart

TODO

Simple format

An example YAML simple format file:

- vertex: start
  edge: goes to
  d_vertex: B

- vertex: A
  edge: goes to
  d_vertex: finish

- vertex: A
  edge: goes to
  d_vertex: B

- vertex: B
  edge: goes to
  d_vertex: finish

- vertex: B
  edge: goes to
  d_vertex: A

- vertex: finish
  edge: goes to
  d_vertex: start

As you can see you just have to declare the source vertex, the destination vertex and an ID for the edge that connects them.

The JSON equivalent file would be like this:

[
  { "vertex": "start", "edge": "goes to", "d_vertex": "B" },
  { "vertex": "A", "edge": "goes to", "d_vertex": "finish" },
  { "vertex": "A", "edge": "goes to", "d_vertex": "B" },
  { "vertex": "B", "edge": "goes to", "d_vertex": "finish" },
  { "vertex": "B", "edge": "goes to", "d_vertex": "A" },
  { "vertex": "finish", "edge": "goes to", "d_vertex": "start" }
]

Extended format

In the case of the extended format:

- vertex:
    id: start
    data:
      description: starting point
  edge:
    id: goes to
    data:
      description: data in some edge
    weight: 52.3
  d_vertex:
    id: A

- vertex:
    id: A
  edge:
    id: goes to
    weight: 52.3
  d_vertex:
    id: finish

- vertex:
    id: A
  edge:
    id: goes to
  d_vertex:
    id: B

- vertex:
    id: B
  edge:
    id: goes to
  d_vertex:
    id: A

- vertex:
    id: B
  edge:
    id: goes to
    weight: 52.3
  d_vertex:
    id: finish

In the extended format each vertex and edge can contain a "data" key with any value that you can consider inside. In this case the ID field is mandatory on each node of the YAML / JSON.

And the JSON equivalent:

[
  {
    "vertex":  { "id": "start",   "data":{ "description":"starting point"    }                },
    "edge":    { "id": "goes to", "data":{ "description":"data in some edge" }, "weight":52.3 },
    "d_vertex":{ "id": "A"                                                                    }
  },
  {
    "vertex":  { "id": "A" },
    "edge":    { "id": "goes to", "weight":52.3 },
    "d_vertex":{ "id": "finish"}
  },
  {
    "vertex":  { "id": "A" },
    "edge":    { "id": "goes to" },
    "d_vertex":{ "id": "B"}
  },
  {
    "vertex":  { "id": "B" },
    "edge":    { "id": "goes to" },
    "d_vertex":{ "id": "A"}
  },
  {
    "vertex":  { "id": "B" },
    "edge":    { "id": "goes to", "weight":52.3 },
    "d_vertex":{ "id": "finish"}
  }
]

If you want a rock-solid simple database written in Go check Cayley

Documentation

Overview

Package groph is a simple graph database that can be loaded / stored from / to JSON and YAML files.

Refer to package parser to instructions about how to load a file as a graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintV

func PrintV(v *Vertex)

Types

type AnyData

type AnyData struct {
	Data interface{} `json:"data, omitempty"`
	ID   interface{} `json:"id, omitempty"`
}

AnyData is a generic type to hold data within a vertex or an edge. One of its uses in the library is to parse unknown incoming data from a JSON or YAML file

func (*AnyData) GetData

func (a *AnyData) GetData() interface{}

func (*AnyData) GetID

func (a *AnyData) GetID() interface{}

func (*AnyData) SetData

func (a *AnyData) SetData(d interface{})

func (*AnyData) SetID

func (a *AnyData) SetID(i interface{})

type Data

type Data interface {
	GetData() interface{}
	SetData(interface{})
	GetID() interface{}
	SetID(interface{})
}

type Edge

type Edge struct {
	Data     `json:"data, omitempty"`
	PointsTo *Vertex `json:"PointsTo, omitempty"`
	From     *Vertex `json:"From, omitempty"`
	Weight   float64 `json:"weight, omitempty"`
}

func (*Edge) Points

func (e *Edge) Points(v *Vertex) bool

func (*Edge) String

func (e *Edge) String() string

type Edges

type Edges []*Edge

func (Edges) Each

func (es Edges) Each(f func(*Edge))

func (Edges) Filter

func (es Edges) Filter(f func(*Edge) bool) (res Edges)

func (Edges) Fold

func (es Edges) Fold(init interface{}, f func(a interface{}, b *Edge) interface{}) (cur interface{})

func (Edges) FromVertices

func (es Edges) FromVertices() Vertices

func (Edges) Map

func (es Edges) Map(f func(*Edge) *Edge) (res Edges)

func (Edges) MapT

func (es Edges) MapT(f func(*Edge) Data) (res *Results)

func (Edges) MapV

func (es Edges) MapV(f func(*Edge) *Vertex) (res Vertices)

func (Edges) PointsToVertices

func (es Edges) PointsToVertices() Vertices

func (Edges) Size

func (es Edges) Size() int

type Graph

type Graph struct {
	StartVertex *Vertex
	IndexMap    map[interface{}]*Vertex
}

Graph is the main type to do queries and stores a vertex to be used as the starting point of any query. At the same time it maintains a map of known vertices

func New

func New() *Graph

New just returns an initialized graph

func (*Graph) AddConnection

func (g *Graph) AddConnection(s, t *Vertex, e *Edge)

AddConnection connects two existing vertices with the provided edge

func (*Graph) All

func (g *Graph) All(f func(*Vertex))

All is a map-like function for each vertex in a graph

func (*Graph) BreadthFirst

func (g *Graph) BreadthFirst(id interface{}, f func(*Vertex))

func (*Graph) Filter

func (g *Graph) Filter(f func(*Vertex) bool) (vs Vertices)

func (*Graph) Find

func (g *Graph) Find(id interface{}) (*Vertex, error)

Find returns the vertex with the provided ID or a not found error

func (*Graph) In

func (g *Graph) In(v *Vertex) []*Edge

In returns the edges that points to current vertex

func (*Graph) InWhereEdge

func (g *Graph) InWhereEdge(f func(*Edge) bool) (res []*Edge)

InWhereEdge traverses the entire graph and returns all inner edges that passes the filter function

func (*Graph) InWhereVertex

func (g *Graph) InWhereVertex(f func(*Vertex) bool) (res []*Edge)

InWhereVertex returns all inner edges that matches the filter function

func (*Graph) NewEdge

func (g *Graph) NewEdge(d Data, weight float64) *Edge

func (*Graph) NewVertex

func (g *Graph) NewVertex(d Data) *Vertex

NewVertex returns an initialized vertex with the provided data if it doesn't exists already or a pointer to the already existing one. Any contents incoming in 'd' are lost if the Vertex is found. If you want to update use 'NewVertexWithUpdate' instead.

func (*Graph) NewVertexWithUpdate

func (g *Graph) NewVertexWithUpdate(d Data) *Vertex

NewVertexWithUpdate returns an initialized vertex with the provided data if it doesn't exists already or a pointer to the already existing one with the contents updated with the incoming data

func (*Graph) Out

func (g *Graph) Out(v *Vertex) Edges

func (*Graph) OutWhereEdge

func (g *Graph) OutWhereEdge(filterFunc func(*Edge) bool) (edges Edges)

OutWhereEdge returns edges that matches specified filter

func (*Graph) OutWhereVertex

func (g *Graph) OutWhereVertex(f func(*Vertex) bool) (edges Edges)

OutWhereVertex returns edges that passes the provided filter

func (*Graph) RootVertex

func (g *Graph) RootVertex() (v *Vertex, err error)

func (*Graph) SaveToDisk

func (g *Graph) SaveToDisk(filePath string) error

TODO SaveToDisk

func (*Graph) SetRootVertex

func (g *Graph) SetRootVertex(r *Vertex)

SetRootVertex changes the current root vertex. This is useful to initiate some specific searches from a particular vertex

func (*Graph) ShortestPath

func (g *Graph) ShortestPath(id interface{}) (reversed Vertices, totalCost float64, err error)

func (*Graph) ShortestPathWithVertex

func (g *Graph) ShortestPathWithVertex(t *Vertex) (reversed []*Vertex, totalCost float64, err error)

func (*Graph) Traverse

func (g *Graph) Traverse(f func(*Vertex))

type Queue

type Queue []*Edge

func NewQueue

func NewQueue() *Queue

func (*Queue) Pop

func (q *Queue) Pop() (ret *Edge, err error)

func (*Queue) Push

func (q *Queue) Push(v *Edge)

type Results

type Results struct {
	// contains filtered or unexported fields
}

func NewResults

func NewResults() *Results

func (*Results) AddIfNotExists

func (r *Results) AddIfNotExists(i Data)

func (*Results) Edges

func (r *Results) Edges() (res Edges)

func (*Results) Get

func (r *Results) Get(i interface{}) Data

func (*Results) GetEdge

func (r *Results) GetEdge(i interface{}) *Edge

func (*Results) GetVertex

func (r *Results) GetVertex(i interface{}) *Vertex

func (*Results) Vertices

func (r *Results) Vertices() (res Vertices)

type Vertex

type Vertex struct {
	Data `json:"data, omitempty"`
	// contains filtered or unexported fields
}

func (*Vertex) InEdges

func (v *Vertex) InEdges() Edges

func (*Vertex) OutEdges

func (v *Vertex) OutEdges() Edges

OutEdges returns the edges of the pointing vertex

func (*Vertex) String

func (v *Vertex) String() string

type Vertices

type Vertices []*Vertex

func (Vertices) Each

func (vs Vertices) Each(f func(v *Vertex))

func (Vertices) Filter

func (vs Vertices) Filter(f func(v *Vertex) bool) (res Vertices)

func (Vertices) FlatMap

func (vs Vertices) FlatMap(f func(v *Vertex) Vertices) (res Vertices)

func (Vertices) Fold

func (vs Vertices) Fold(init interface{}, f func(a interface{}, b *Vertex) interface{}) (cur interface{})

func (Vertices) InEdges

func (vs Vertices) InEdges() Edges

func (Vertices) Map

func (vs Vertices) Map(f func(v *Vertex) *Vertex) (res Vertices)

func (Vertices) MapE

func (vs Vertices) MapE(f func(v *Vertex) *Edge) (res Edges)

func (Vertices) MapT

func (vs Vertices) MapT(f func(v *Vertex) Data) (res *Results)

func (Vertices) OutEdges

func (vs Vertices) OutEdges() Edges

func (Vertices) Size

func (vs Vertices) Size() int

type VerticesSlice

type VerticesSlice []Vertices

Directories

Path Synopsis
examples
Package parser provides function to read JSON and YAML files and convert them to a graph value.
Package parser provides function to read JSON and YAML files and convert them to a graph value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL