dagger

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2020 License: Apache-2.0 Imports: 1 Imported by: 5

README

dagger GoDoc

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

import "github.com/autom8ter/dagger"

Example

        coleman = dagger.NewNode("user", fmt.Sprintf("cword_%v", time.Now().UnixNano()), map[string]interface{}{
   		"name": "coleman",
   	})
   	tyler = dagger.NewNode("user", fmt.Sprintf("twash_%v", time.Now().UnixNano()), map[string]interface{}{
   		"name": "tyler",
   	})
   	sarah = dagger.NewNode("user", fmt.Sprintf("swash_%v", time.Now().UnixNano()), map[string]interface{}{
   		"name": "sarah",
   	})
   	lacee = dagger.NewNode("user", fmt.Sprintf("ljans_%v", time.Now().UnixNano()), map[string]interface{}{
   		"name": "lacee",
   	})
   	// random id will be generated if one isn't provided
   	charlie = dagger.NewNode("dog", "", map[string]interface{}{
   		"name":   "charlie",
   		"weight": 25,
   	})
   
   	if err := coleman.Connect(tyler, "friend", true); err != nil {
   		exitErr(err)
   	}
   	if err := sarah.Connect(lacee, "friend", true); err != nil {
   		exitErr(err)
   	}
   	if err := coleman.Connect(lacee, "fiance", true); err != nil {
   		exitErr(err)
   	}
   	if err := tyler.Connect(lacee, "wife", true); err != nil {
   		exitErr(err)
   	}
   	if err := coleman.Connect(charlie, "pet", false); err != nil {
   		exitErr(err)
   	}
   	if err := lacee.Connect(charlie, "pet", false); err != nil {
   		exitErr(err)
   	}
   	if err := charlie.Connect(lacee, "owner", false); err != nil {
   		exitErr(err)
   	}
   	if err := charlie.Connect(coleman, "owner", false); err != nil {
   		exitErr(err)
   	}
   	charlie.Patch(map[string]interface{}{
   		"weight": 19,
   	})
   	if charlie.GetInt("weight") != 19 {
   		exit("expected charlie's weight to be 19!")
   	}
   	// check to make sure edge is patched
   	coleman.EdgesFrom(func(e *primitive.Edge) bool {
   		if e.Type() == "pet" && e.GetString("name") == "charlie" {
   			if e.To.GetInt("weight") != 19 {
   				exit("failed to patch charlie's weight")
   			}
   		}
   		return true
   	})
   	if coleman.GetString("name") != "coleman" {
   		exit("expected name to be coleman")
   	}
   	// remove from graph
   	charlie.Remove()
   	// no longer in graph
   	if dagger.HasNode(charlie) {
   		exit("failed to delete node - (charlie)")
   	}
   	// check to make sure edge no longer exists(cascade)
   	coleman.EdgesFrom(func(e *primitive.Edge) bool {
   		if e.Type() == "pet" && e.GetString("name") == "charlie" {
   			exit("failed to delete node - (charlie)")
   		}
   		return true
   	})
   	// check to make sure edge no longer exists(cascade)
   	lacee.EdgesFrom(func(e *primitive.Edge) bool {
   		if e.Type() == "pet" && e.GetString("name") == "charlie" {
   			exit("failed to delete node - (charlie)")
   		}
   		return true
   	})

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close closes the global graph instance

func EdgeTypes

func EdgeTypes() []string

EdgeTypes returns the types of relationships/edges/connections in the graph

func HasNode

func HasNode(id primitive.TypedID) bool

HasNode returns true if a node with the typed ID exists in the graph

func NodeTypes

func NodeTypes() []string

NodeTypes returns the types of nodes in the graph

func RangeEdgeTypes

func RangeEdgeTypes(edgeType primitive.Type, fn func(e *primitive.Edge) bool)

RangeEdgeTypes iterates over edges/connections of a given type until the iterator returns false

func RangeEdges

func RangeEdges(fn func(e *primitive.Edge) bool)

RangeEdges iterates over all edges/connections until the iterator returns false

func RangeNodeTypes

func RangeNodeTypes(typ primitive.Type, fn func(n *Node) bool)

RangeNodeTypes iterates over nodes of a given type until the iterator returns false

func RangeNodes

func RangeNodes(fn func(n *Node) bool)

RangeNodes iterates over all nodes until the iterator returns false

Types

type Node

type Node struct {
	primitive.TypedID
}

Node is the most basic element in the graph. Node's may be connected with one another via edges to represent relationships

func GetNode

func GetNode(id primitive.TypedID) (*Node, bool)

GetNode gets a node from the graph

func NewNode

func NewNode(nodeType, id string, attributes map[string]interface{}) *Node

NewNode creates a new node in the global, in-memory graph. If an id is not provided, a random uuid will be assigned.

Example
package main

import (
	"fmt"
	"github.com/autom8ter/dagger"
	"github.com/autom8ter/dagger/primitive"
	"log"
	"time"
)

func main() {
	coleman = dagger.NewNode("user", fmt.Sprintf("cword_%v", time.Now().UnixNano()), map[string]interface{}{
		"name": "coleman",
	})
	tyler = dagger.NewNode("user", fmt.Sprintf("twash_%v", time.Now().UnixNano()), map[string]interface{}{
		"name": "tyler",
	})
	sarah = dagger.NewNode("user", fmt.Sprintf("swash_%v", time.Now().UnixNano()), map[string]interface{}{
		"name": "sarah",
	})
	lacee = dagger.NewNode("user", fmt.Sprintf("ljans_%v", time.Now().UnixNano()), map[string]interface{}{
		"name": "lacee",
	})
	// random id will be generated if one isn't provided
	charlie = dagger.NewNode("dog", "", map[string]interface{}{
		"name":   "charlie",
		"weight": 25,
	})

	if err := coleman.Connect(tyler, "friend", true); err != nil {
		exitErr(err)
	}
	if err := sarah.Connect(lacee, "friend", true); err != nil {
		exitErr(err)
	}
	if err := coleman.Connect(lacee, "fiance", true); err != nil {
		exitErr(err)
	}
	if err := tyler.Connect(lacee, "wife", true); err != nil {
		exitErr(err)
	}
	if err := coleman.Connect(charlie, "pet", false); err != nil {
		exitErr(err)
	}
	if err := lacee.Connect(charlie, "pet", false); err != nil {
		exitErr(err)
	}
	if err := charlie.Connect(lacee, "owner", false); err != nil {
		exitErr(err)
	}
	if err := charlie.Connect(coleman, "owner", false); err != nil {
		exitErr(err)
	}
	charlie.Patch(map[string]interface{}{
		"weight": 19,
	})
	if charlie.GetInt("weight") != 19 {
		exit("expected charlie's weight to be 19!")
	}
	// check to make sure edge is patched
	coleman.EdgesFrom(func(e *primitive.Edge) bool {
		if e.Type() == "pet" && e.GetString("name") == "charlie" {
			if e.To.GetInt("weight") != 19 {
				exit("failed to patch charlie's weight")
			}
		}
		return true
	})
	if coleman.GetString("name") != "coleman" {
		exit("expected name to be coleman")
	}
	// remove from graph
	charlie.Remove()
	// no longer in graph
	if dagger.HasNode(charlie) {
		exit("failed to delete node - (charlie)")
	}
	// check to make sure edge no longer exists(cascade)
	coleman.EdgesFrom(func(e *primitive.Edge) bool {
		if e.Type() == "pet" && e.GetString("name") == "charlie" {
			exit("failed to delete node - (charlie)")
		}
		return true
	})
	// check to make sure edge no longer exists(cascade)
	lacee.EdgesFrom(func(e *primitive.Edge) bool {
		if e.Type() == "pet" && e.GetString("name") == "charlie" {
			exit("failed to delete node - (charlie)")
		}
		return true
	})
}

func exit(msg string) {
	log.Fatal(msg)
}

func exitErr(err error) {
	log.Fatal(err)
}

func (*Node) Connect

func (n *Node) Connect(node *Node, relationship string, mutual bool) error

Connect creates a connection/edge between the two nodes with the given relationship type if mutual = true, the connection is doubly linked - (facebook is mutual, instagram is not)

func (*Node) Del added in v0.1.0

func (n *Node) Del(key string)

Del deletes the entry from the Node by key

func (*Node) EdgesFrom

func (n *Node) EdgesFrom(fn func(e *primitive.Edge) bool)

EdgesFrom returns connections/edges that stem from the node/vertex

func (*Node) EdgesTo

func (n *Node) EdgesTo(fn func(e *primitive.Edge) bool)

EdgesTo returns connections/edges that point toward the node/vertex

func (*Node) FromJSON added in v0.1.0

func (n *Node) FromJSON(bits []byte) error

FromJSON encodes the node with the given JSON bytes

func (*Node) Get added in v0.1.0

func (n *Node) Get(key string) interface{}

Get gets an empty interface value(any value type) from the nodes attributes(if it exists)

func (*Node) GetBool added in v0.1.0

func (n *Node) GetBool(key string) bool

GetBool gets a bool value from the nodes attributes(if it exists)

func (*Node) GetInt added in v0.1.0

func (n *Node) GetInt(key string) int

GetInt gets an int value from the nodes attributes(if it exists)

func (*Node) GetString added in v0.1.0

func (n *Node) GetString(key string) string

GetString gets a string value from the nodes attributes(if it exists)

func (*Node) JSON

func (n *Node) JSON() ([]byte, error)

JSON returns the node as JSON bytes

func (*Node) Patch

func (n *Node) Patch(data map[string]interface{})

Patch patches the node attributes with the given data

func (*Node) Range

func (n *Node) Range(fn func(key string, value interface{}) bool)

Range iterates over the nodes attributes until the iterator returns false

func (*Node) Remove

func (n *Node) Remove()

Remove permenently removes the node from the graph

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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