dagger

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: Apache-2.0 Imports: 4 Imported by: 5

README

dagger GoDoc

dag

dagger is a blazing fast, concurrency safe, mutable, in-memory directed graph implementation with zero dependencies

import "github.com/autom8ter/dagger"

Design:

  • flexibility
  • concurrency safe
  • high performance
  • simple api

Features

  • native graph objects(nodes/edges)
  • typed graph objects(ex: user/pet)
  • labelled nodes & edges
  • depth first search
  • breadth first search
  • topological sort
  • concurrency safe

Example

Setting Nodes

    var g = dagger.NewGraph()
	vm1 := g.SetNode(dagger.Path{
		XID:   "virtual_machine_1",
		XType: "infra",
	}, map[string]interface{}{})
	vm2 := g.SetNode(dagger.Path{
		XID:   "virtual_machine_2",
		XType: "infra",
	}, map[string]interface{}{})
	k8s := g.SetNode(dagger.Path{
		XID:   "kubernetes",
		XType: "infra",
	}, map[string]interface{}{})
	redis := g.SetNode(dagger.Path{
		XID:   "redis",
		XType: "infra",
	}, map[string]interface{}{
		"port": "6379",
	})
	mongo := g.SetNode(dagger.Path{
		XID:   "mongo",
		XType: "infra",
	}, map[string]interface{}{
		"port": "5568",
	})
	httpServer := g.SetNode(dagger.Path{
		XID:   "http",
		XType: "infra",
	}, map[string]interface{}{
		"port": "8080",
	})
	g.RangeNodes("*", func(n dagger.Node) bool {
		t.Logf("found node in graph: %s.%s\n", n.XType, n.XID)
		return true
	})
Ranging Over Nodes
    // iterate over nodes of type user
    g.RangeNodes("users", func(n dagger.Node) bool {
		t.Logf("found user node in graph: %s.%s\n", n.XType, n.XID)
		return true
	})
Setting Edges
    _, err := g.SetEdge(k8s.Path, vm1.Path, dagger.Node{
		Path: dagger.Path{
			XType: "depends_on",
		},
		Attributes: map[string]interface{}{},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = g.SetEdge(k8s.Path, vm2.Path, dagger.Node{
		Path: dagger.Path{
			XType: "depends_on",
		},
		Attributes: map[string]interface{}{},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = g.SetEdge(mongo.Path, redis.Path, dagger.Node{
		Path: dagger.Path{
			XType: "depends_on",
		},
		Attributes: map[string]interface{}{},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = g.SetEdge(httpServer.Path, k8s.Path, dagger.Node{
		Path: dagger.Path{
			XType: "depends_on",
		},
		Attributes: map[string]interface{}{},
	})
	if err != nil {
		t.Fatal(err)
	}
Iterating over Edges
    g.RangeEdgesFrom("*", httpServer.Path, func(e dagger.Edge) bool {
        t.Logf("found edge in graph: %s.%s\n", e.XType, n.XID)
        return true
    })
Iterating over Edges in Reverse
    g.RangeEdgesTo("*", httpServer.Path, func(e dagger.Edge) bool {
        t.Logf("found edge in graph: %s.%s\n", e.XType, n.XID)
		return true
	})
    // iterate over nodes that depend the httpServer depends on
    g.DFS("depends_on", httpServer.Path, func(node dagger.Node) bool {
		i++
		t.Logf("(%v) Reverse DFS: %s", i, node.String())
		return true
	})
    g.BFS("depends_on", httpServer.Path, func(node dagger.Node) bool {
		i++
		t.Logf("(%v) Reverse DFS: %s", i, node.String())
		return true
	})

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes added in v0.14.0

type Attributes map[string]interface{}

func (Attributes) Copy added in v0.14.0

func (m Attributes) Copy() Attributes

Copy creates a replica of the Node

func (Attributes) Del added in v0.14.0

func (m Attributes) Del(key string)

Del deletes the entry from the Attributes by key

func (Attributes) Exists added in v0.14.0

func (m Attributes) Exists(key string) bool

Exists checks for the existance of a key

func (Attributes) Filter added in v0.14.0

func (m Attributes) Filter(filter func(key string, v interface{}) bool) Attributes

Filter returns a Attributes of the node that return true from the filter function

func (Attributes) Get added in v0.14.0

func (m Attributes) Get(key string) interface{}

Get gets an entry from the Attributes by key

func (Attributes) GetBool added in v0.14.0

func (m Attributes) GetBool(key string) bool

GetBool gets an entry from the Attributes by key

func (Attributes) GetInt added in v0.14.0

func (m Attributes) GetInt(key string) int

GetInt gets an entry from the Attributes by key

func (Attributes) GetString added in v0.14.0

func (m Attributes) GetString(key string) string

GetString gets an entry from the Attributes by key

func (Attributes) Range added in v0.14.0

func (m Attributes) Range(iterator func(key string, v interface{}) bool)

Range iterates over the Attributes with the function. If the function returns false, the iteration exits.

func (Attributes) Set added in v0.14.0

func (m Attributes) Set(k string, v interface{})

Set set an entry in the Node

func (Attributes) SetAll added in v0.14.0

func (m Attributes) SetAll(data map[string]interface{})

SetAll set all entries in the Node

type Edge added in v0.1.1

type Edge struct {
	// An edge implements Node because it has an Identifier and attributes
	Node `json:"node"`
	// From returns the root node of the edge
	From Path `json:"from"`
	// To returns the target node of the edge
	To Path `json:"to"`
}

Edge is a relationship between two nodes

type Export added in v0.14.0

type Export struct {
	Nodes []Node `json:"nodes"`
	Edges []Edge `json:"edges"`
}

Export contains an array of nodes and their corresponding edges

type Graph added in v0.14.0

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

Graph is a concurrency safe, mutable, in-memory directed graph

func CustomGraph added in v1.0.1

func CustomGraph(nodes, edges, edgesFrom, edgesTo driver.Index) *Graph

CustomGraph creates a new Graph instance with the given Index interface implementations

func NewGraph added in v0.14.0

func NewGraph() *Graph

NewGraph creates a new in-memory Graph instance

func (*Graph) BFS added in v0.14.0

func (g *Graph) BFS(edgeType string, rootNode Path, fn func(nodePath Node) bool)

BFS executes a depth first search with the rootNode and edge type

func (*Graph) Close added in v0.14.0

func (g *Graph) Close()

Close closes the graph

func (*Graph) DFS added in v0.14.0

func (g *Graph) DFS(edgeType string, rootNode Path, fn func(nodePath Node) bool)

DFS executes a depth first search with the rootNode and edge type

func (*Graph) DelEdge added in v0.14.0

func (g *Graph) DelEdge(id Path)

func (*Graph) DelNode added in v0.14.0

func (g *Graph) DelNode(id Path)

func (*Graph) EdgeTypes added in v0.14.0

func (g *Graph) EdgeTypes() []string

EdgeTypes returns all edge types in the graph

func (*Graph) Export added in v0.14.0

func (g *Graph) Export() *Export

func (*Graph) GetEdge added in v0.14.0

func (g *Graph) GetEdge(id Path) (Edge, bool)

func (*Graph) GetNode added in v0.14.0

func (g *Graph) GetNode(id Path) (Node, bool)

GetNode gets an existing node in the graph

func (*Graph) HasEdge added in v0.14.0

func (g *Graph) HasEdge(id Path) bool

func (*Graph) HasNode added in v0.14.0

func (g *Graph) HasNode(id Path) bool

func (*Graph) Import added in v0.14.0

func (g *Graph) Import(exp *Export) error

Import imports the export instance into the graph

func (*Graph) NodeTypes added in v0.14.0

func (g *Graph) NodeTypes() []string

EdgeTypes returns all node types in the graph

func (*Graph) RangeEdges added in v0.14.0

func (g *Graph) RangeEdges(edgeType string, fn func(e Edge) bool)

RangeEdges ranges over edges until the given function returns false

func (*Graph) RangeEdgesFrom added in v0.14.0

func (g *Graph) RangeEdgesFrom(edgeType string, id Path, fn func(e Edge) bool)

func (*Graph) RangeEdgesTo added in v0.14.0

func (g *Graph) RangeEdgesTo(edgeType string, id Path, fn func(e Edge) bool)

func (*Graph) RangeNodes added in v0.14.0

func (g *Graph) RangeNodes(nodeType string, fn func(n Node) bool)

RangeNodes ranges over nodes of the given type until the given function returns false

func (*Graph) ReverseBFS added in v0.14.0

func (g *Graph) ReverseBFS(edgeType string, rootNode Path, fn func(nodePath Node) bool)

ReverseBFS executes a reverse depth first search with the rootNode and edge type

func (*Graph) ReverseDFS added in v0.14.0

func (g *Graph) ReverseDFS(edgeType string, rootNode Path, fn func(nodePath Node) bool)

ReverseDFS executes a reverse depth first search with the rootNode and edge type

func (*Graph) ReverseTopologicalSort added in v0.14.0

func (g *Graph) ReverseTopologicalSort(nodeType, edgeType string, fn func(node Node) bool)

func (*Graph) SetEdge added in v0.14.0

func (g *Graph) SetEdge(from, to Path, node Node) (Edge, error)

func (*Graph) SetNode added in v0.14.0

func (g *Graph) SetNode(key Path, attr Attributes) Node

SetNode sets a node in the graph

Example
package main

import (
	"fmt"
	"github.com/autom8ter/dagger"
)

func main() {
	var g = dagger.NewGraph()
	g.SetNode(dagger.Path{
		XID:   "virtual_machine_1",
		XType: "infra",
	}, map[string]interface{}{})
	g.SetNode(dagger.Path{
		XID:   "virtual_machine_2",
		XType: "infra",
	}, map[string]interface{}{})
	g.SetNode(dagger.Path{
		XID:   "kubernetes",
		XType: "infra",
	}, map[string]interface{}{})
	g.SetNode(dagger.Path{
		XID:   "redis",
		XType: "infra",
	}, map[string]interface{}{
		"port": "6379",
	})
	g.SetNode(dagger.Path{
		XID:   "mongo",
		XType: "infra",
	}, map[string]interface{}{
		"port": "5568",
	})
	g.SetNode(dagger.Path{
		XID:   "http",
		XType: "infra",
	}, map[string]interface{}{
		"port": "8080",
	})
	g.RangeNodes("*", func(n dagger.Node) bool {
		fmt.Printf("found node in graph: %s.%s\n", n.XType, n.XID)
		return true
	})
}
Output:

func (*Graph) TopologicalSort added in v0.14.0

func (g *Graph) TopologicalSort(nodeType, edgeType string, fn func(node Node) bool)

TopologicalSort executes a topological sort

type Node

type Node struct {
	Path       `json:"path"`
	Attributes Attributes `json:"attributes"`
}

Node is a single entity in the graph.

type Path added in v0.14.0

type Path struct {
	XID   string `json:"xid"`
	XType string `json:"xtype"`
}

Path satisfies primitive.Path interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL