about summary refs log tree commit diff
path: root/tools/nixery
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-09-30T10·34+0100
committerVincent Ambo <github@tazj.in>2019-10-03T12·21+0100
commite60805c9b213a4054afe84d29c16058277014d0b (patch)
tree7f16b4fa775dda87ac7d5c73a6212b4d296ca493 /tools/nixery
parent9c3c622403cec6e1bf7e4b9aecfbc8bea1e1f6fd (diff)
feat(server): Introduce function to hash contents of a layer
This creates a cache key which can be used to check if a layer has
already been built.
Diffstat (limited to 'tools/nixery')
-rw-r--r--tools/nixery/server/layers/grouping.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/tools/nixery/server/layers/grouping.go b/tools/nixery/server/layers/grouping.go
index e8666bf4dd0e..f8259ab989ff 100644
--- a/tools/nixery/server/layers/grouping.go
+++ b/tools/nixery/server/layers/grouping.go
@@ -103,9 +103,12 @@
 package layers
 
 import (
+	"crypto/sha1"
+	"fmt"
 	"log"
 	"regexp"
 	"sort"
+	"strings"
 
 	"gonum.org/v1/gonum/graph/flow"
 	"gonum.org/v1/gonum/graph/simple"
@@ -141,6 +144,13 @@ type Layer struct {
 	mergeRating uint64
 }
 
+// Hash the contents of a layer to create a deterministic identifier that can be
+// used for caching.
+func (l *Layer) Hash() string {
+	sum := sha1.Sum([]byte(strings.Join(l.Contents, ":")))
+	return fmt.Sprintf("%x", sum)
+}
+
 func (a Layer) merge(b Layer) Layer {
 	a.Contents = append(a.Contents, b.Contents...)
 	a.mergeRating += b.mergeRating
@@ -272,6 +282,9 @@ func groupLayer(dt *flow.DominatorTree, root *closure) Layer {
 		children = append(children, dt.DominatedBy(child.ID())...)
 	}
 
+	// Contents are sorted to ensure that hashing is consistent
+	sort.Strings(contents)
+
 	return Layer{
 		Contents: contents,
 		// TODO(tazjin): The point of this is to factor in