Skip to content

Commit 0517146

Browse files
committed
adding support to cache entire graph
with repeated calls, we should not need to derive it from source every time. Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent 8e0f712 commit 0517146

File tree

4 files changed

+100
-7
lines changed

4 files changed

+100
-7
lines changed

cmd/compspec/compspec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func main() {
4646
matchFields := matchCmd.StringList("m", "match", &argparse.Options{Help: "One or more key value pairs to match"})
4747
manifestFile := matchCmd.String("i", "in", &argparse.Options{Required: true, Help: "Input manifest list yaml that contains pairs of images and artifacts"})
4848
printMapping := matchCmd.Flag("p", "print", &argparse.Options{Help: "Print mapping of images to attributes only."})
49-
printGraph := matchCmd.Flag("g", "print-graph", &argparse.Options{Help: "Print schema graph"})
49+
printGraph := matchCmd.Flag("", "print-graph", &argparse.Options{Help: "Print schema graph"})
5050
checkArtifacts := matchCmd.Flag("c", "check-artifacts", &argparse.Options{Help: "Check that all artifacts exist"})
5151
allowFailMatch := matchCmd.Flag("f", "allow-fail", &argparse.Options{Help: "Allow an artifact to be missing (and not included)"})
5252
randomize := matchCmd.Flag("r", "randomize", &argparse.Options{Help: "Shuffle match results in random order"})
5353
single := matchCmd.Flag("s", "single", &argparse.Options{Help: "Only return a single result"})
5454
cachePath := matchCmd.String("", "cache", &argparse.Options{Help: "A path to a cache for artifacts"})
55+
saveGraph := matchCmd.String("", "cache-graph", &argparse.Options{Help: "Load or use a cached graph"})
5556

5657
// Create arguments
5758
options := createCmd.StringList("a", "append", &argparse.Options{Help: "Append one or more custom metadata fields to append"})
@@ -84,6 +85,7 @@ func main() {
8485
*matchFields,
8586
*mediaType,
8687
*cachePath,
88+
*saveGraph,
8789
*printMapping,
8890
*printGraph,
8991
*allowFailMatch,

cmd/compspec/match/match.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func Run(
3838
hostFields []string,
3939
mediaType string,
4040
cachePath string,
41+
graphPath string,
4142
printMapping bool,
4243
printGraph bool,
4344
allowFail bool,
@@ -66,6 +67,17 @@ func Run(
6667
return err
6768
}
6869

70+
// If we are given a graph path and it exists, load it
71+
// This won't fail if it does not exist, but will error
72+
// with another issue
73+
exists := false
74+
if graphPath != "" {
75+
exists, err = g.LoadGraph(graphPath)
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
6981
// Keep check of artifacts missing
7082
missing := 0
7183

@@ -91,11 +103,13 @@ func Run(
91103
}
92104
lookup[item.Name] = compspec
93105

94-
// Add schemas to the graph
95-
for _, schema := range compspec.Metadata.Schemas {
96-
err = g.AddSchema(schema)
97-
if err != nil {
98-
return err
106+
// Add schemas to the graph TODO, allow caching
107+
if !exists {
108+
for _, schema := range compspec.Metadata.Schemas {
109+
err = g.AddSchema(schema)
110+
if err != nil {
111+
return err
112+
}
99113
}
100114
}
101115

@@ -118,6 +132,14 @@ func Run(
118132
}
119133
}
120134

135+
// Save graph if given a file
136+
if graphPath != "" {
137+
err = g.SaveGraph(graphPath)
138+
if err != nil {
139+
return err
140+
}
141+
}
142+
121143
if checkArtifacts {
122144
fmt.Printf("Checking artifacts complete. There were %d artifacts missing.\n", missing)
123145
return nil

docs/usage.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,19 @@ mkdir -p ./cache
243243
./bin/compspec match -i ./examples/check-lammps/manifest.yaml --cache ./cache
244244
```
245245

246+
You can also save the graph to file:
246247

248+
```bash
249+
./bin/compspec match -i ./examples/check-lammps/manifest.yaml --cache ./cache --cache-graph ./cache/lammps-experiment.json
250+
```
251+
252+
You can then use that cached graph later (NOTE this is at your own discretion knowing the schemas needed).
253+
254+
```bash
255+
./bin/compspec match -i ./examples/check-lammps/manifest.yaml --cache ./cache --cache-graph ./cache/lammps-experiment.json
256+
```
257+
258+
Likely we will make tools to visualize it that can just show JGF!
247259

248260
### Match to print Metadata attributes
249261

@@ -604,4 +616,4 @@ The ordering of your list is honored.
604616
## Developer
605617

606618
Note that there is a [developer environment](.devcontainer) that provides a consistent version of Go, etc.
607-
However, it won't work with all extractors. Note that for any command that uses a plugin (e.g., `extract` and `check`)
619+
However, it won't work with all extractors. Note that for any command that uses a plugin (e.g., `extract` and `check`)

pkg/graph/graph.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package graph
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57
"strings"
68

79
"github.com/converged-computing/jsongraph-go/jsongraph/v2/graph"
@@ -222,6 +224,61 @@ func (c *CompatibilityGraph) Match(fields []string) ([]string, error) {
222224
return matches.List(), nil
223225
}
224226

227+
// Load graph from a cached file.
228+
// This assumes you know what you are doing, meaning
229+
// the schemas are not changing
230+
func (c *CompatibilityGraph) LoadGraph(path string) (bool, error) {
231+
exists, err := utils.PathExists(path)
232+
233+
// Some error, do not continue!
234+
if err != nil {
235+
return exists, err
236+
}
237+
// No error, but doesn't exist (will still load)
238+
if !exists {
239+
return exists, nil
240+
}
241+
242+
// If we get here, load in!
243+
fd, err := os.Open(path)
244+
if err != nil {
245+
return exists, err
246+
}
247+
graph := graph.Graph{}
248+
jsonParser := json.NewDecoder(fd)
249+
err = jsonParser.Decode(&graph)
250+
if err != nil {
251+
return exists, err
252+
}
253+
254+
// Go through the edges and add the root nodes
255+
c.Graph = graph
256+
return true, nil
257+
}
258+
259+
// Save graph to a cached file
260+
func (c *CompatibilityGraph) SaveGraph(path string) error {
261+
exists, err := utils.PathExists(path)
262+
if err != nil {
263+
return err
264+
}
265+
// Don't overwrite if exists
266+
if exists {
267+
fmt.Printf("Graph %s already exists, will not overwrite\n", path)
268+
return nil
269+
}
270+
content, err := json.MarshalIndent(c.Graph, "", " ")
271+
if err != nil {
272+
return err
273+
}
274+
fmt.Printf("Saving graph to %s\n", path)
275+
err = os.WriteFile(path, content, 0644)
276+
if err != nil {
277+
return err
278+
}
279+
return nil
280+
}
281+
225282
// AddSchema by url to the graph
226283
func (c *CompatibilityGraph) AddSchema(url string) error {
227284

0 commit comments

Comments
 (0)