dependency

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package dependency constructs the project dependency DAG, translating path strings to node IDs.

Index

Constants

This section is empty.

Variables

View Source
var ErrCycle = errors.New("graph: dependency cycle")

ErrCycle is returned by Builder.Build when the edge set forms a cycle.

Functions

func Build

func Build(w *types.Workspace, opts ...types.GraphOption) (*types.Graph, error)

Build constructs the dependency graph for the workspace.

The observer from w.GraphObserver() is composed with any WithGraphObserver options. Cycles fail with ErrCycle. An unregistered dependency fails the build: every missing dep is collected and returned as *UnregisteredDepError.

Types

type AffectedPath

type AffectedPath struct {
	Seed  ID
	Chain []ID
}

AffectedPath is one chain from a seed node to a target node, in seed-first order.

type BuildOption

type BuildOption func(*buildCfg)

BuildOption configures a Build call.

func WithObserver

func WithObserver(o Observer) BuildOption

WithObserver attaches obs to the resulting Graph (query → OnQuery; build → OnBuild).

type BuildStats

type BuildStats struct {
	Nodes    int
	Edges    int
	Duration time.Duration
}

BuildStats is emitted once per successful Builder.Build.

type Builder

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

Builder accumulates nodes and edges; not safe for concurrent use.

func New

func New() *Builder

New returns an empty Builder.

func (*Builder) AddEdge

func (b *Builder) AddEdge(from, to ID) error

AddEdge records a directed edge from→to (silently deduplicates); cycle detection is in Build.

func (*Builder) AddNode

func (b *Builder) AddNode(path string) ID

AddNode registers path and returns its stable builder ID. Idempotent.

func (*Builder) Build

func (b *Builder) Build(opts ...BuildOption) (*Graph, error)

Build detects cycles (Kahn's algorithm) and returns an immutable Graph. Builder must not be reused.

type Graph

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

Graph is an immutable DAG; all methods are safe for concurrent use.

func (*Graph) BlastRadius

func (g *Graph) BlastRadius() []int32

BlastRadius returns, for each node, the count of nodes that can transitively reach it.

func (*Graph) ID

func (g *Graph) ID(path string) (ID, bool)

ID returns the Graph ID for path, or NoID if not found.

func (*Graph) Len

func (g *Graph) Len() int

Len returns the number of nodes.

func (*Graph) NCCD

func (g *Graph) NCCD() float64

NCCD computes Normalized CCD = CCD / CCD(balanced binary tree of same size).

func (*Graph) NearCycles

func (g *Graph) NearCycles(ctx context.Context, maxDepth int) []NearCycle

NearCycles returns pairs where adding From→To would close a cycle of length ≤ maxDepth.

func (*Graph) Nodes

func (g *Graph) Nodes() iter.Seq[ID]

Nodes iterates over all IDs in lexicographic order.

func (*Graph) Path

func (g *Graph) Path(id ID) (string, bool)

Path returns the path for id, or ("", false) if out of range.

func (*Graph) PathsFromSeeds

func (g *Graph) PathsFromSeeds(target ID, seeds []ID, out []AffectedPath) []AffectedPath

PathsFromSeeds returns the shortest seed→target chain for each reachable seed, sorted by seed ID.

func (*Graph) Predecessors

func (g *Graph) Predecessors(id ID) iter.Seq[ID]

Predecessors iterates over direct dependents of id.

func (*Graph) Reachable

func (g *Graph) Reachable(u, v ID) bool

Reachable reports whether u can reach v (O(1) after closure build).

func (*Graph) ReverseClosure

func (g *Graph) ReverseClosure(dst []ID, seeds []ID) []ID

ReverseClosure returns every node that can reach any seed, sorted by ID. Uses BFS for small graphs and the bitset closure for large ones.

func (*Graph) Successors

func (g *Graph) Successors(id ID) iter.Seq[ID]

Successors iterates over direct dependencies of id.

func (*Graph) TopoOrder

func (g *Graph) TopoOrder() []ID

TopoOrder returns a copy of the Kahn order (dependents before deps). Kahn starts from the in-degree-zero root, so a dependent sorts ahead of the dependencies it points at (see TestBuild_TopoSortDepsBeforeDependents).

type ID

type ID int32

ID identifies a node; stable for the lifetime of a Graph, assigned in lexicographic order. Do not use a Builder ID to index a Graph (reassigned at Build).

const NoID ID = -1

NoID is the zero value returned when a lookup fails.

type NearCycle

type NearCycle struct {
	From, To ID
	BackPath []ID // [To, ..., From]
}

NearCycle describes an ordered pair where adding the edge From→To would create a cycle of length ≤ maxDepth.

type NoopObserver

type NoopObserver struct{}

NoopObserver discards every event.

func (NoopObserver) OnBuild

func (NoopObserver) OnBuild(BuildStats)

func (NoopObserver) OnError

func (NoopObserver) OnError(error)

func (NoopObserver) OnQuery

func (NoopObserver) OnQuery(QueryEvent)

type Observer

type Observer interface {
	OnBuild(BuildStats)
	OnQuery(QueryEvent)
	OnError(error)
}

Observer receives structured events from a Graph; implementations must be concurrency-safe.

type QueryEvent

type QueryEvent struct {
	Op          string // method name
	Nodes       int    // |V|
	Seeds       int    // seed-set size, or 0 when N/A
	Strategy    string // "bfs" or "bitset"
	ResultCount int    // closure size, path count, or pair count
	Duration    time.Duration
}

QueryEvent is emitted once per top-level query method.

Jump to

Keyboard shortcuts

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