graph

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package graph implements a graph data structure for the mesh network.

Index

Constants

This section is empty.

Variables

View Source
var EdgesPrefix = storage.RegistryPrefix.ForString("edges")

EdgesPrefix is where edges are stored in the database. edges are indexed by their source and target node IDs in the format /registry/edges/<source>/<target>.

View Source
var ErrEdgeNotFound = graph.ErrEdgeNotFound

ErrEdgeNotFound is returned when an edge is not found.

View Source
var ErrEmptyNodeID = errors.New("node ID must not be empty")

ErrEmptyNodeID is returned when a node ID is empty.

View Source
var ErrInvalidNodeID = errors.New("node ID is invalid")

ErrInvalidNodeID is returned when a node ID is invalid.

View Source
var NodesPrefix = storage.RegistryPrefix.ForString("nodes")

NodesPrefix is where nodes are stored in the database. nodes are indexed by their ID in the format /registry/nodes/<id>.

Functions

func NewTestGraph

func NewTestGraph() (Graph, *GraphStore, error)

NewTestGraph is an alias for creating a new graph with in-memory storage. It also returns the underlying storage instance.

Types

type AdjacencyMap

type AdjacencyMap map[NodeID]EdgeMap

AdjacencyMap is a map of node names to a map of node names to edges.

func BuildAdjacencyMap

func BuildAdjacencyMap(g Graph) (AdjacencyMap, error)

BuildAdjacencyMap returns the adjacency map for the graph.

func (AdjacencyMap) DeepEqual

func (a AdjacencyMap) DeepEqual(b AdjacencyMap) bool

DeepEqual returns true if the given AdjacencyMap is equal to this AdjacencyMap.

type Edge

type Edge graph.Edge[NodeID]

Edge is the graph.Edge implementation for the mesh network.

func (Edge) DeepEqual

func (e Edge) DeepEqual(other Edge) bool

DeepEqual returns true if the given Edge is equal to this Edge.

func (Edge) ToMeshEdge

func (e Edge) ToMeshEdge(source, target NodeID) MeshEdge

ToMeshEdge converts an Edge to a MeshEdge.

type EdgeMap

type EdgeMap map[NodeID]Edge

EdgeMap is a map of node names to edges.

func (EdgeMap) DeepEqual

func (e EdgeMap) DeepEqual(other EdgeMap) bool

DeepEqual returns true if the given EdgeMap is equal to this EdgeMap.

type Graph

type Graph graph.Graph[NodeID, MeshNode]

Graph is the graph.Graph implementation for the mesh network.

func NewGraph

func NewGraph(st storage.MeshStorage) Graph

NewGraph creates a new Graph instance.

func NewGraphWithStore added in v0.8.1

func NewGraphWithStore(st storage.MeshStorage, store Store) Graph

NewGraphWithStore creates a new Graph instance with the given store.

type GraphStore

type GraphStore struct {
	storage.MeshStorage
	// contains filtered or unexported fields
}

GraphStore implements the Store.

func (*GraphStore) AddEdge

func (g *GraphStore) AddEdge(sourceNode, targetNode NodeID, edge graph.Edge[NodeID]) error

AddEdge should add an edge between the vertices with the given source and target hashes.

If either vertex doesn't exit, ErrVertexNotFound should be returned for the respective vertex. If the edge already exists, ErrEdgeAlreadyExists should be returned.

func (*GraphStore) AddVertex

func (g *GraphStore) AddVertex(nodeID NodeID, node MeshNode, props graph.VertexProperties) error

AddVertex should add the given vertex with the given hash value and vertex properties to the graph. If the vertex already exists, it is up to you whether ErrVertexAlreadyExists or no error should be returned.

func (*GraphStore) Edge

func (g *GraphStore) Edge(sourceNode, targetNode NodeID) (graph.Edge[NodeID], error)

Edge should return the edge joining the vertices with the given hash values. It should exclusively look for an edge between the source and the target vertex, not vice versa. The graph implementation does this for undirected graphs itself.

Note that unlike Graph.Edge, this function is supposed to return an Edge[K], i.e. an edge that only contains the vertex hashes instead of the vertices themselves.

If the edge doesn't exist, ErrEdgeNotFound should be returned.

func (*GraphStore) ListEdges

func (g *GraphStore) ListEdges() ([]graph.Edge[NodeID], error)

ListEdges should return all edges in the graph in a slice.

func (*GraphStore) ListVertices

func (g *GraphStore) ListVertices() ([]NodeID, error)

ListVertices should return all vertices in the graph in a slice.

func (*GraphStore) RemoveEdge

func (g *GraphStore) RemoveEdge(sourceNode, targetNode NodeID) error

RemoveEdge should remove the edge between the vertices with the given source and target hashes.

If either vertex doesn't exist, it is up to you whether ErrVertexNotFound or no error should be returned. If the edge doesn't exist, it is up to you whether ErrEdgeNotFound or no error should be returned.

func (*GraphStore) RemoveVertex

func (g *GraphStore) RemoveVertex(nodeID NodeID) error

RemoveVertex should remove the vertex with the given hash value. If the vertex doesn't exist, ErrVertexNotFound should be returned. If the vertex has edges to other vertices, ErrVertexHasEdges should be returned.

func (*GraphStore) UpdateEdge

func (g *GraphStore) UpdateEdge(sourceNode, targetNode NodeID, edge graph.Edge[NodeID]) error

UpdateEdge should update the edge between the given vertices with the data of the given Edge instance. If the edge doesn't exist, ErrEdgeNotFound should be returned.

func (*GraphStore) Vertex

func (g *GraphStore) Vertex(nodeID NodeID) (node MeshNode, props graph.VertexProperties, err error)

Vertex should return the vertex and vertex properties with the given hash value. If the vertex doesn't exist, ErrVertexNotFound should be returned.

func (*GraphStore) VertexCount

func (g *GraphStore) VertexCount() (int, error)

VertexCount should return the number of vertices in the graph. This should be equal to the length of the slice returned by ListVertices.

type MeshEdge

type MeshEdge struct{ *v1.MeshEdge }

MeshEdge wraps a mesh edge.

func (MeshEdge) AsGraphEdge

func (e MeshEdge) AsGraphEdge() graph.Edge[NodeID]

AsGraphEdge converts a MeshEdge to a graph.Edge.

func (MeshEdge) EdgeProperties

func (e MeshEdge) EdgeProperties() graph.EdgeProperties

EdgeProperties returns the edge's properties.

func (MeshEdge) MarshalJSON

func (e MeshEdge) MarshalJSON() ([]byte, error)

MarshalJSON marshals a MeshEdge to JSON.

func (MeshEdge) PutInto

func (e MeshEdge) PutInto(g Graph) error

PutInto puts the MeshEdge into the given graph.

func (MeshEdge) SourceID

func (e MeshEdge) SourceID() NodeID

SourceID returns the source node's ID.

func (MeshEdge) TargetID

func (e MeshEdge) TargetID() NodeID

TargetID returns the target node's ID.

func (MeshEdge) ToEdge

func (e MeshEdge) ToEdge() Edge

ToEdge converts a MeshEdge to an Edge.

func (*MeshEdge) UnmarshalJSON

func (e *MeshEdge) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a MeshEdge from JSON.

type MeshNode

type MeshNode struct{ *v1.MeshNode }

MeshNode wraps a mesh node.

func (MeshNode) DNSPort

func (n MeshNode) DNSPort() uint16

DNSPort returns the node's DNS port.

func (MeshNode) HasFeature

func (n MeshNode) HasFeature(feature v1.Feature) bool

HasFeature returns true if the node has the given feature.

func (MeshNode) MarshalJSON

func (n MeshNode) MarshalJSON() ([]byte, error)

MarshalJSON marshals the node to JSON.

func (MeshNode) NodeID

func (n MeshNode) NodeID() NodeID

NodeID returns the node's ID.

func (MeshNode) PortFor

func (n MeshNode) PortFor(feature v1.Feature) uint16

PortFor returns the port for the given feature, or 0 if the feature is not available on this node.

func (MeshNode) PrivateAddrV4

func (n MeshNode) PrivateAddrV4() netip.Prefix

PrivateAddrV4 returns the node's private IPv4 address. Be sure to check if the returned Addr IsValid.

func (MeshNode) PrivateAddrV6

func (n MeshNode) PrivateAddrV6() netip.Prefix

PrivateAddrV6 returns the node's private IPv6 address. Be sure to check if the returned Addr IsValid.

func (MeshNode) PrivateDNSAddrV4

func (n MeshNode) PrivateDNSAddrV4() netip.AddrPort

PrivateDNSAddrV4 returns the private IPv4 address for the node's DNS server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateDNSAddrV6

func (n MeshNode) PrivateDNSAddrV6() netip.AddrPort

PrivateDNSAddrV6 returns the private IPv6 address for the node's DNS server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateRPCAddrV4

func (n MeshNode) PrivateRPCAddrV4() netip.AddrPort

PrivateRPCAddrV4 returns the private IPv4 address for the node's RPC server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateRPCAddrV6

func (n MeshNode) PrivateRPCAddrV6() netip.AddrPort

PrivateRPCAddrV6 returns the private IPv6 address for the node's RPC server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateStorageAddrV4

func (n MeshNode) PrivateStorageAddrV4() netip.AddrPort

PrivateStorageAddrV4 returns the private IPv4 address for the node's raft listener. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateStorageAddrV6

func (n MeshNode) PrivateStorageAddrV6() netip.AddrPort

PrivateStorageAddrV6 returns the private IPv6 address for the node's raft listener. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateTURNAddrV4

func (n MeshNode) PrivateTURNAddrV4() netip.AddrPort

PrivateTURNAddrV4 returns the private IPv4 address for the node's TURN server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PrivateTURNAddrV6

func (n MeshNode) PrivateTURNAddrV6() netip.AddrPort

PrivateTURNAddrV6 returns the private IPv6 address for the node's TURN server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PublicDNSAddr

func (n MeshNode) PublicDNSAddr() netip.AddrPort

PublicDNSAddr returns the public address for the node's DNS server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) PublicRPCAddr

func (n MeshNode) PublicRPCAddr() netip.AddrPort

PublicRPCAddr returns the public address for the node's RPC server. Be sure to check if the returned AddrPort IsValid.

func (MeshNode) RPCPort

func (n MeshNode) RPCPort() uint16

RPCPort returns the node's RPC port.

func (MeshNode) StoragePort

func (n MeshNode) StoragePort() uint16

StoragePort returns the node's Storage port.

func (MeshNode) TURNPort

func (n MeshNode) TURNPort() uint16

TURNPort returns the node's TURN port.

func (*MeshNode) UnmarshalJSON

func (n *MeshNode) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the node from JSON.

type NodeID

type NodeID string

NodeID is the type of a node ID.

func (NodeID) Bytes added in v0.8.0

func (id NodeID) Bytes() []byte

Bytes returns the byte representation of the node ID.

func (NodeID) IsEmpty added in v0.8.0

func (id NodeID) IsEmpty() bool

IsEmpty returns true if the node ID is empty.

func (NodeID) String

func (id NodeID) String() string

String returns the string representation of the node ID.

type Store added in v0.8.1

type Store graph.Store[NodeID, MeshNode]

Store is the graph.Store implementation for the mesh network.

Jump to

Keyboard shortcuts

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