dagger

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2020 License: Apache-2.0 Imports: 3 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
  • global state
    • see primitive to manage graph state manually
  • 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
  • concurrency safe
  • import graph from JSON blob
  • export graph to JSON blob

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(sarah, "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 *dagger.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 *dagger.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 *dagger.Edge) bool {
   		if e.Type() == "pet" && e.GetString("name") == "charlie" {
   			exit("failed to delete node - (charlie)")
   		}
   		return true
   	})
   	fmt.Printf("registered node types = %v", dagger.NodeTypes())
   	fmt.Printf("registered edge types = %v", dagger.EdgeTypes())
   	dagger.RangeNodes(func(n *dagger.Node) bool {
   		bits, err := n.JSON()
   		if err != nil {
   			exitErr(err)
   		}
   		fmt.Printf("\nfound node = %v\n", string(bits))
   		n.EdgesFrom(func(e *dagger.Edge) bool {
   			bits, err := e.JSON()
   			if err != nil {
   				exitErr(err)
   			}
   			fmt.Println(string(bits))
   			return true
   		})
   		n.EdgesTo(func(e *dagger.Edge) bool {
   			bits, err := e.JSON()
   			if err != nil {
   				exitErr(err)
   			}
   			fmt.Println(string(bits))
   			return true
   		})
   		return true
   	})

Output:


registered node types = [dog user]registered edge types = [fiance friend owner pet wife]
found node = {"_id":"cword_1603581855577975000","_type":"user","name":"coleman"}
{"node":{"_id":"aa91881a-1e23-9b44-e059-afe6880816a9","_type":"friend"},"from":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"}}
{"node":{"_id":"6ec23de7-9584-964b-79ed-ace7ee06112d","_type":"fiance"},"from":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"}}
{"node":{"_id":"7b86436f-28df-fff1-c198-8093397cc400","_type":"pet"},"from":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"},"to":{"_id":"85fb2e74-b125-243a-2ac8-8f13b1513824","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"1da7e6e1-197f-c65e-a6b9-335298b9c076","_type":"friend"},"from":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"}}
{"node":{"_id":"1bbfe7e0-6bf1-bca3-55e3-0281e80663b7","_type":"fiance"},"from":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"}}

found node = {"_id":"twash_1603581855577992000","_type":"user","name":"tyler"}
{"node":{"_id":"1da7e6e1-197f-c65e-a6b9-335298b9c076","_type":"friend"},"from":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"}}
{"node":{"_id":"2d6b7738-a549-5c8b-1a82-36e79ead4bc5","_type":"wife"},"from":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"}}
{"node":{"_id":"aa91881a-1e23-9b44-e059-afe6880816a9","_type":"friend"},"from":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"}}
{"node":{"_id":"3bfd44b4-3c73-0471-81be-1a09ff56e813","_type":"wife"},"from":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"}}

found node = {"_id":"cword_1603581855576640000","_type":"user","name":"coleman"}
{"node":{"_id":"001d526f-b4f0-6ac4-6d6f-063144400f3a","_type":"friend"},"from":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"}}
{"node":{"_id":"8c145e86-07c1-10f2-2062-6ba194277878","_type":"fiance"},"from":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"}}
{"node":{"_id":"b674a411-949d-6c6f-4279-985d46ab0b64","_type":"pet"},"from":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"},"to":{"_id":"60a2c505-fcc1-a649-66ac-df3378bba2ec","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"6d10e57f-bdee-7fe7-aa4d-ec1368775e09","_type":"friend"},"from":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"}}
{"node":{"_id":"3fbb68b7-a8f3-3313-b349-29596cf9a958","_type":"fiance"},"from":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"}}

found node = {"_id":"twash_1603581855578157000","_type":"user","name":"tyler"}
{"node":{"_id":"5371ab6e-8b8b-a2df-d12e-ecf852a083f7","_type":"wife"},"from":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"}}
{"node":{"_id":"87c893c3-2959-da05-a390-f0ca062ed90d","_type":"friend"},"from":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"}}
{"node":{"_id":"a793eeb7-4e35-5b06-b51f-916bf89d30ee","_type":"friend"},"from":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"}}
{"node":{"_id":"ab051d4e-b3a4-7d97-2d00-53b01c92bcbb","_type":"wife"},"from":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"}}

found node = {"_id":"swash_1603581855578158000","_type":"user","name":"sarah"}
{"node":{"_id":"e6671030-cb2a-80b8-5453-55991b87a1f6","_type":"friend"},"from":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"}}
{"node":{"_id":"ab051d4e-b3a4-7d97-2d00-53b01c92bcbb","_type":"wife"},"from":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"}}
{"node":{"_id":"5371ab6e-8b8b-a2df-d12e-ecf852a083f7","_type":"wife"},"from":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"}}
{"node":{"_id":"572ce11b-9c6e-06d0-876b-2cb43d4f9261","_type":"friend"},"from":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"}}

found node = {"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"}
{"node":{"_id":"99ed2178-e43c-ac5f-18be-a65855b35a6e","_type":"friend"},"from":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"}}
{"node":{"_id":"3fbb68b7-a8f3-3313-b349-29596cf9a958","_type":"fiance"},"from":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"}}
{"node":{"_id":"3ae8d958-2cc7-e9e5-fcbc-13b313418167","_type":"pet"},"from":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"},"to":{"_id":"60a2c505-fcc1-a649-66ac-df3378bba2ec","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"c0bb7318-a05a-1c96-4491-453645130c92","_type":"friend"},"from":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"}}
{"node":{"_id":"8c145e86-07c1-10f2-2062-6ba194277878","_type":"fiance"},"from":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"}}

found node = {"_id":"swash_1603581855577993000","_type":"user","name":"sarah"}
{"node":{"_id":"be911a98-106f-ca4b-1d6e-941715cda021","_type":"friend"},"from":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"}}
{"node":{"_id":"3bfd44b4-3c73-0471-81be-1a09ff56e813","_type":"wife"},"from":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"}}
{"node":{"_id":"4233d57b-3c32-d551-b904-90c5aee6f126","_type":"friend"},"from":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"}}
{"node":{"_id":"2d6b7738-a549-5c8b-1a82-36e79ead4bc5","_type":"wife"},"from":{"_id":"twash_1603581855577992000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"}}

found node = {"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"}
{"node":{"_id":"1bbfe7e0-6bf1-bca3-55e3-0281e80663b7","_type":"fiance"},"from":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"}}
{"node":{"_id":"bb244d0c-751b-9e71-5838-caa21d6c97ad","_type":"pet"},"from":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"},"to":{"_id":"85fb2e74-b125-243a-2ac8-8f13b1513824","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"4233d57b-3c32-d551-b904-90c5aee6f126","_type":"friend"},"from":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"}}
{"node":{"_id":"be911a98-106f-ca4b-1d6e-941715cda021","_type":"friend"},"from":{"_id":"swash_1603581855577993000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"}}
{"node":{"_id":"6ec23de7-9584-964b-79ed-ace7ee06112d","_type":"fiance"},"from":{"_id":"cword_1603581855577975000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855577994000","_type":"user","name":"lacee"}}

found node = {"_id":"twash_1603581855576673000","_type":"user","name":"tyler"}
{"node":{"_id":"39d8cf0b-ca55-da86-9579-9dc3e8dcb442","_type":"wife"},"from":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"}}
{"node":{"_id":"6d10e57f-bdee-7fe7-aa4d-ec1368775e09","_type":"friend"},"from":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"}}
{"node":{"_id":"001d526f-b4f0-6ac4-6d6f-063144400f3a","_type":"friend"},"from":{"_id":"cword_1603581855576640000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"}}
{"node":{"_id":"34b7bbd9-534c-31e8-219f-b53246ff0ed4","_type":"wife"},"from":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"}}

found node = {"_id":"swash_1603581855576675000","_type":"user","name":"sarah"}
{"node":{"_id":"34b7bbd9-534c-31e8-219f-b53246ff0ed4","_type":"wife"},"from":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"},"to":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"}}
{"node":{"_id":"c0bb7318-a05a-1c96-4491-453645130c92","_type":"friend"},"from":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"}}
{"node":{"_id":"39d8cf0b-ca55-da86-9579-9dc3e8dcb442","_type":"wife"},"from":{"_id":"twash_1603581855576673000","_type":"user","name":"tyler"},"to":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"}}
{"node":{"_id":"99ed2178-e43c-ac5f-18be-a65855b35a6e","_type":"friend"},"from":{"_id":"ljans_1603581855576676000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855576675000","_type":"user","name":"sarah"}}

found node = {"_id":"cword_1603581855578144000","_type":"user","name":"coleman"}
{"node":{"_id":"bfb8e908-01b7-2c5b-553b-15e2496bdd6d","_type":"pet"},"from":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"},"to":{"_id":"45d4829f-c351-ba1a-662a-289de972dcef","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"a793eeb7-4e35-5b06-b51f-916bf89d30ee","_type":"friend"},"from":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"},"to":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"}}
{"node":{"_id":"48300cc8-ac82-ab8f-7c00-4f3423e3f0b6","_type":"fiance"},"from":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"}}
{"node":{"_id":"87c893c3-2959-da05-a390-f0ca062ed90d","_type":"friend"},"from":{"_id":"twash_1603581855578157000","_type":"user","name":"tyler"},"to":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"}}
{"node":{"_id":"ffa5d8a0-e981-1836-7f83-a846c271dfe1","_type":"fiance"},"from":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"}}

found node = {"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"}
{"node":{"_id":"572ce11b-9c6e-06d0-876b-2cb43d4f9261","_type":"friend"},"from":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"},"to":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"}}
{"node":{"_id":"ffa5d8a0-e981-1836-7f83-a846c271dfe1","_type":"fiance"},"from":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"},"to":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"}}
{"node":{"_id":"7bf404ca-efe4-5d3e-81b8-297f9a4f3f2f","_type":"pet"},"from":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"},"to":{"_id":"45d4829f-c351-ba1a-662a-289de972dcef","_type":"dog","name":"charlie","weight":19}}
{"node":{"_id":"e6671030-cb2a-80b8-5453-55991b87a1f6","_type":"friend"},"from":{"_id":"swash_1603581855578158000","_type":"user","name":"sarah"},"to":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"}}
{"node":{"_id":"48300cc8-ac82-ab8f-7c00-4f3423e3f0b6","_type":"fiance"},"from":{"_id":"cword_1603581855578144000","_type":"user","name":"coleman"},"to":{"_id":"ljans_1603581855578159000","_type":"user","name":"lacee"}}

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 EdgeCount added in v0.1.1

func EdgeCount() int

EdgeCount returns the total number of edges in the graph

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 NodeCount added in v0.1.1

func NodeCount() int

NodeCount returns the total number of nodes 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 *Edge) bool)

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

func RangeEdges

func RangeEdges(fn func(e *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 Edge added in v0.1.1

type Edge struct {
	primitive.TypedID
}

func NewEdge added in v0.1.1

func NewEdge(edgeType, id string, attributes map[string]interface{}, from, to *Node) (*Edge, error)

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

func (*Edge) Del added in v0.1.1

func (e *Edge) Del(key string)

Del deletes the entry from the edge by key

func (*Edge) From added in v0.1.1

func (e *Edge) From() *Node

From returns the node that points to the node returned by To()

func (*Edge) FromJSON added in v0.1.1

func (e *Edge) FromJSON(bits []byte) error

FromJSON encodes the edge with the given JSON bytes

func (*Edge) Get added in v0.1.1

func (e *Edge) Get(key string) interface{}

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

func (*Edge) GetBool added in v0.1.1

func (e *Edge) GetBool(key string) bool

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

func (*Edge) GetInt added in v0.1.1

func (e *Edge) GetInt(key string) int

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

func (*Edge) GetString added in v0.1.1

func (e *Edge) GetString(key string) string

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

func (*Edge) JSON added in v0.1.1

func (e *Edge) JSON() ([]byte, error)

JSON returns the edge as JSON bytes

func (*Edge) Patch added in v0.1.1

func (e *Edge) Patch(data map[string]interface{})

Patch patches the edge attributes with the given data

func (*Edge) Range added in v0.1.1

func (e *Edge) Range(fn func(key string, value interface{}) bool)

Range iterates over the edges attributes until the iterator returns false

func (*Edge) To added in v0.1.1

func (e *Edge) To() *Node

To returns the node that is being pointed to by From()

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"
	"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(sarah, "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 *dagger.Edge) bool {
		if e.Type() == "pet" {
			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 *dagger.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 *dagger.Edge) bool {
		if e.Type() == "pet" && e.GetString("name") == "charlie" {
			exit("failed to delete node - (charlie)")
		}
		return true
	})
	fmt.Printf("registered node types = %v\n", dagger.NodeTypes())
	fmt.Printf("registered edge types = %v\n", dagger.EdgeTypes())

}

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

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

registered node types = [dog user]
registered edge types = [fiance friend owner pet wife]

func (*Node) Connect

func (n *Node) Connect(nodeID primitive.TypedID, 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(edge *Edge) bool)

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

func (*Node) EdgesTo

func (n *Node) EdgesTo(fn func(e *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