Skip to content

Commit

Permalink
feat(kzip): change Unit.Digest() to return hex string representation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysachs committed Aug 29, 2019
1 parent b9e9c01 commit f81e09b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 51 deletions.
14 changes: 3 additions & 11 deletions kythe/go/platform/kcd/kcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ type Unit interface {
// canonicalization is unit-dependent, and may safely be a no-op.
Canonicalize()

// Digest writes a unique representation of the unit to w sufficient to
// generate a content-addressable digest.
Digest(w io.Writer)
// Digest produces a unique string representation of a unit sufficient to
// serve as a content-addressable digest.
Digest() string
}

// Index represents the indexable terms of a compilation.
Expand All @@ -261,14 +261,6 @@ func HexDigest(data []byte) string {
return hex.EncodeToString(sum[:])
}

// UnitDigest computes a hex-encoded SHA256 digest of the data in a compilation
// unit.
func UnitDigest(u Unit) string {
sha := sha256.New()
u.Digest(sha)
return hex.EncodeToString(sha.Sum(nil)[:])
}

// IsValidDigest reports whether s is valid as a digest computed by the
// HexDigest function. It does not check whether s could have actually been
// generated by the hash function, only the structure of the value.
Expand Down
13 changes: 0 additions & 13 deletions kythe/go/platform/kcd/kcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package kcd

import (
"io"
"regexp"
"testing"
"time"
Expand All @@ -35,21 +34,9 @@ func TestHexDigest(t *testing.T) {
if got := HexDigest([]byte(input)); got != want {
t.Errorf("HexDigest(%q): got %q, want %q", input, got, want)
}

u := stubUnit{bits: input}
if got := UnitDigest(u); got != want {
t.Errorf("UnitDigest(%q): got %q, want %q", input, got, want)
}
}
}

type stubUnit struct {
bits string
Unit
}

func (s stubUnit) Digest(w io.Writer) { w.Write([]byte(s.bits)) }

func regexps(exprs ...string) (res []*regexp.Regexp) {
for _, expr := range exprs {
res = append(res, regexp.MustCompile(expr))
Expand Down
11 changes: 7 additions & 4 deletions kythe/go/platform/kcd/kythe/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package kythe // import "kythe.io/kythe/go/platform/kcd/kythe"

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"sort"
"strings"

Expand Down Expand Up @@ -94,15 +95,16 @@ func (u Unit) Canonicalize() {
}

// Digest satisfies part of the kcd.Unit interface.
func (u Unit) Digest(w io.Writer) {
func (u Unit) Digest() string {
sha := sha256.New()
pb := u.Proto
if pb == nil {
pb = new(apb.CompilationUnit)
}
put := func(tag string, ss ...string) {
fmt.Fprintln(w, tag)
fmt.Fprintln(sha, tag)
for _, s := range ss {
fmt.Fprint(w, s, "\x00")
fmt.Fprint(sha, s, "\x00")
}
}
putv := func(tag string, v *spb.VName) {
Expand All @@ -124,6 +126,7 @@ func (u Unit) Digest(w io.Writer) {
for _, d := range pb.Details {
put("DET", d.TypeUrl, string(d.Value))
}
return hex.EncodeToString(sha.Sum(nil)[:])
}

// ConvertUnit reports whether v can be converted to a Kythe kcd.Unit, and if
Expand Down
23 changes: 4 additions & 19 deletions kythe/go/platform/kcd/kythe/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package kythe

import (
"bytes"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -129,7 +128,7 @@ func keys(v interface{}) (keys []string) {
}

func TestDigest(t *testing.T) {
const empty = "CU\n\x00\x00\x00\x00\x00ARG\nOUT\n\x00SRC\nCWD\n\x00CTX\n\x00"
const empty = "56bf5044e1b5c4c1cc7c4b131ac2fb979d288460e63352b10eef80ca35bd0a7b"
tests := []struct {
unit *apb.CompilationUnit
want string
Expand All @@ -144,12 +143,7 @@ func TestDigest(t *testing.T) {
Language: "L",
},
Argument: []string{"a1", "a2"},
}, "CU\nS\x00C\x00\x00P\x00L\x00" +
"ARG\na1\x00a2\x00" +
"OUT\n\x00" +
"SRC\n" +
"CWD\n\x00" +
"CTX\n\x00",
}, "e9e170dcfca53c8126755bbc8b703994dedd3af32584291e01fba164ab5d3f32",
},
{&apb.CompilationUnit{
RequiredInput: []*apb.CompilationUnit_FileInput{{
Expand All @@ -165,20 +159,11 @@ func TestDigest(t *testing.T) {
TypeUrl: "type",
Value: []byte("nasaldemons"),
}},
}, "CU\n\x00\x00\x00\x00\x00" +
"RI\nRIS\x00\x00\x00\x00\x00" +
"IN\npath\x00digest\x00" +
"ARG\n" +
"OUT\nblah\x00" +
"SRC\nCWD\n\x00CTX\n\x00" +
"ENV\nfeefie\x00fofum\x00" +
"DET\ntype\x00nasaldemons\x00",
}, "bb761979683e7c268e967eb5bcdedaa7fa5d1d472b0826b00b69acafbaad7ee6",
},
}
for _, test := range tests {
var buf bytes.Buffer
Unit{Proto: test.unit}.Digest(&buf)
got := buf.String()
got := Unit{Proto: test.unit}.Digest()
if got != test.want {
t.Errorf("Digest: got %q, want %q\nInput: %+v", got, test.want, test.unit)
}
Expand Down
2 changes: 1 addition & 1 deletion kythe/go/platform/kcd/memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (db *DB) WriteUnit(_ context.Context, revision, corpus, formatKey string, u
if err != nil {
return "", err
}
digest := kcd.UnitDigest(unit)
digest := unit.Digest()
if db.Unit == nil {
db.Unit = make(map[string]Unit)
}
Expand Down
4 changes: 1 addition & 3 deletions kythe/go/platform/kzip/kzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,7 @@ var toJSON = &jsonpb.Marshaler{OrigName: true}
func (w *Writer) AddUnit(cu *apb.CompilationUnit, index *apb.IndexedCompilation_Index) (string, error) {
unit := kythe.Unit{Proto: cu}
unit.Canonicalize()
hash := sha256.New()
unit.Digest(hash)
digest := hex.EncodeToString(hash.Sum(nil))
digest := unit.Digest()

w.mu.Lock()
defer w.mu.Unlock()
Expand Down

0 comments on commit f81e09b

Please sign in to comment.