From 20c8a856a2365e39367dc2101ea967176f873171 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 22 Jan 2024 13:57:34 -0500 Subject: [PATCH] lang: funcs: dage: Add a Graph method to improve the API This also adds it to the GraphAPI so that users of it can pull out a graph when needed. It's not likely to be used by the dage engine consumers. --- lang/funcs/dage/dage.go | 8 ++++++++ lang/funcs/dage/txn.go | 2 +- lang/funcs/dage/txn_test.go | 4 ++++ lang/interfaces/func.go | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lang/funcs/dage/dage.go b/lang/funcs/dage/dage.go index df231a1b..5f588ac5 100644 --- a/lang/funcs/dage/dage.go +++ b/lang/funcs/dage/dage.go @@ -504,6 +504,14 @@ func (obj *Engine) FindEdge(f1, f2 interfaces.Func) *interfaces.FuncEdge { return fe } +// Graph returns a copy of the contained graph. +func (obj *Engine) Graph() *pgraph.Graph { + //obj.graphMutex.Lock() // XXX: should this be a RLock? + //defer obj.graphMutex.Unlock() // XXX: should this be an RUnlock? + + return obj.graph.Copy() +} + // Lock must be used before modifying the running graph. Make sure to Unlock // when done. // XXX: should Lock take a context if we want to bail mid-way? diff --git a/lang/funcs/dage/txn.go b/lang/funcs/dage/txn.go index 41a669cc..07ecd61a 100644 --- a/lang/funcs/dage/txn.go +++ b/lang/funcs/dage/txn.go @@ -510,7 +510,7 @@ func (obj *graphTxn) commit() error { //gv := &pgraph.Graphviz{ // Filename: fmt.Sprintf("/tmp/txn-graphviz-%d.dot", d), // Graphs: map[*pgraph.Graph]*pgraph.GraphvizOpts{ - // engine.graph: nil, + // obj.Graph(): nil, // }, //} //if err := gv.Exec(); err != nil { diff --git a/lang/funcs/dage/txn_test.go b/lang/funcs/dage/txn_test.go index d615eead..e406c177 100644 --- a/lang/funcs/dage/txn_test.go +++ b/lang/funcs/dage/txn_test.go @@ -115,6 +115,10 @@ func (obj *testGraphAPI) FindEdge(f1, f2 interfaces.Func) *interfaces.FuncEdge { return fe } +func (obj *testGraphAPI) Graph() *pgraph.Graph { + return obj.graph.Copy() +} + type testNullFunc struct { name string } diff --git a/lang/interfaces/func.go b/lang/interfaces/func.go index f0ec2a6b..781d839c 100644 --- a/lang/interfaces/func.go +++ b/lang/interfaces/func.go @@ -262,6 +262,9 @@ type GraphAPI interface { HasVertex(Func) bool FindEdge(Func, Func) *FuncEdge LookupEdge(*FuncEdge) (Func, Func, bool) + + // Graph returns a copy of the current graph. + Graph() *pgraph.Graph } // Txn is the interface that the engine graph API makes available so that