graph

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package graph provides core data structures and interfaces for the gograph database. It includes Node, Relationship, and Property types along with utilities for indexing and adjacency list management.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Package graph provides core data structures and interfaces for the gograph database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodePropertyValue

func EncodePropertyValue(v PropertyValue) string

EncodePropertyValue encodes a PropertyValue to a string for indexing.

func GenerateID

func GenerateID(prefix string) string

GenerateID generates a unique ID with the given prefix. It uses an atomic counter to ensure uniqueness across concurrent operations.

func SetIDCounter

func SetIDCounter(counter uint64)

SetIDCounter sets the ID counter to a specific value. This is primarily useful for testing purposes.

Types

type AdjacencyList

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

AdjacencyList manages the adjacency relationships between nodes. It provides methods to build and query relationships between graph nodes.

func NewAdjacencyList

func NewAdjacencyList(store *storage.DB) *AdjacencyList

NewAdjacencyList creates a new AdjacencyList instance.

func (*AdjacencyList) AddRelationship

func (adj *AdjacencyList) AddRelationship(m Mutator, rel *Relationship) error

AddRelationship creates adjacency entries for a relationship within a mutation context.

func (*AdjacencyList) GetAllRelated

func (adj *AdjacencyList) GetAllRelated(nodeID string) ([]string, error)

GetAllRelated returns all Relationship IDs related to the given node, regardless of type. This is critical for efficient `Detach Delete`.

func (*AdjacencyList) GetRelatedNodes

func (adj *AdjacencyList) GetRelatedNodes(nodeID, relType string, direction Direction) ([]string, error)

GetRelatedNodes returns the IDs of nodes related to the given node with the specified relationship type and direction.

func (*AdjacencyList) RemoveRelationship

func (adj *AdjacencyList) RemoveRelationship(m Mutator, rel *Relationship) error

RemoveRelationship removes adjacency entries for a relationship within a mutation context.

type Direction

type Direction string

Direction represents the direction of a relationship in a graph.

const (
	DirectionOutgoing Direction = "outgoing"
	DirectionIncoming Direction = "incoming"
	DirectionBoth     Direction = "both"
)

func ParseDirection

func ParseDirection(s string) (Direction, error)

ParseDirection parses a direction string and returns the corresponding Direction. Supported formats: "outgoing", "->", "out" for outgoing; "incoming", "<-", "in" for incoming; "both", "-" for both.

func (Direction) String

func (d Direction) String() string

String returns the string representation of the direction.

type Index

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

Index manages label and property indexes for efficient node lookups.

func NewIndex

func NewIndex(store *storage.DB) *Index

NewIndex creates a new Index instance for the given storage.

func (*Index) BuildLabelIndex

func (idx *Index) BuildLabelIndex(m Mutator, node *Node) error

BuildLabelIndex creates index entries for all labels on a node.

func (*Index) BuildPropertyIndex

func (idx *Index) BuildPropertyIndex(m Mutator, node *Node) error

BuildPropertyIndex creates index entries for all properties on a node.

func (*Index) LookupByLabel

func (idx *Index) LookupByLabel(label string) ([]string, error)

LookupByLabel returns all node IDs that have the given label.

func (*Index) LookupByProperty

func (idx *Index) LookupByProperty(label, propName, propValue string) ([]string, error)

LookupByProperty returns all node IDs that have the given label and property value.

func (*Index) RemoveLabelIndex

func (idx *Index) RemoveLabelIndex(m Mutator, node *Node) error

RemoveLabelIndex removes index entries for all labels on a node.

func (*Index) RemovePropertyIndex

func (idx *Index) RemovePropertyIndex(m Mutator, node *Node) error

RemovePropertyIndex removes index entries for all properties on a node.

type Mutator

type Mutator interface {
	Put(key, value []byte) error
	Delete(key []byte) error
}

Mutator defines the interface for modifying storage, satisfied by *tx.Transaction.

type Node

type Node struct {
	ID         string
	Labels     []string
	Properties map[string]PropertyValue
}

Node represents a graph node with an ID, labels, and properties.

func NewNode

func NewNode(labels []string, properties map[string]interface{}) *Node

NewNode creates a new Node with the given labels and properties.

func (*Node) GetProperty

func (n *Node) GetProperty(key string) (PropertyValue, bool)

GetProperty returns the property value and true if it exists, or zero value and false.

func (*Node) HasLabel

func (n *Node) HasLabel(label string) bool

HasLabel returns true if the node has the given label.

func (*Node) RemoveLabel

func (n *Node) RemoveLabel(label string)

RemoveLabel removes a label from the node.

func (*Node) RemoveProperty

func (n *Node) RemoveProperty(key string)

RemoveProperty removes a property from the node.

func (*Node) SetProperty

func (n *Node) SetProperty(key string, value PropertyValue)

SetProperty sets a property value on the node.

type PropertyType

type PropertyType string

PropertyType represents the type of a property value.

const (
	PropertyTypeString PropertyType = "string"
	PropertyTypeInt    PropertyType = "int"
	PropertyTypeFloat  PropertyType = "float"
	PropertyTypeBool   PropertyType = "bool"
)

type PropertyValue

type PropertyValue struct {
	String *string
	Int    *int64
	Float  *float64
	Bool   *bool
}

PropertyValue represents a typed property value that can hold string, int, float, or bool.

func NewBoolProperty

func NewBoolProperty(v bool) PropertyValue

NewBoolProperty creates a PropertyValue holding a bool.

func NewFloatProperty

func NewFloatProperty(v float64) PropertyValue

NewFloatProperty creates a PropertyValue holding a float64.

func NewIntProperty

func NewIntProperty(v int64) PropertyValue

NewIntProperty creates a PropertyValue holding an int64.

func NewStringProperty

func NewStringProperty(v string) PropertyValue

NewStringProperty creates a PropertyValue holding a string.

func ToPropertyValue

func ToPropertyValue(v interface{}) PropertyValue

ToPropertyValue converts a Go value to a PropertyValue.

func (PropertyValue) BoolValue

func (p PropertyValue) BoolValue() bool

BoolValue returns the bool value, or false if not a bool type.

func (PropertyValue) FloatValue

func (p PropertyValue) FloatValue() float64

FloatValue returns the float value, or 0 if not a float type.

func (PropertyValue) IntValue

func (p PropertyValue) IntValue() int64

IntValue returns the int value, or 0 if not an int type.

func (PropertyValue) StringValue

func (p PropertyValue) StringValue() string

StringValue returns the string value, or empty string if not a string type.

func (PropertyValue) Type

func (p PropertyValue) Type() PropertyType

Type returns the type of the property value.

type Relationship

type Relationship struct {
	ID          string
	StartNodeID string
	EndNodeID   string
	Type        string
	Properties  map[string]PropertyValue
}

Relationship represents a directed relationship between two nodes in a graph.

func NewRelationship

func NewRelationship(startNodeID, endNodeID, relType string, properties map[string]interface{}) *Relationship

NewRelationship creates a new Relationship with the given parameters.

func (*Relationship) GetProperty

func (r *Relationship) GetProperty(key string) (PropertyValue, bool)

GetProperty returns the property value and true if it exists, or zero value and false.

func (*Relationship) RemoveProperty

func (r *Relationship) RemoveProperty(key string)

RemoveProperty removes a property from the relationship.

func (*Relationship) SetProperty

func (r *Relationship) SetProperty(key string, value PropertyValue)

SetProperty sets a property value on the relationship.

Jump to

Keyboard shortcuts

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