Use array instead of hash for backend.ID

Since backend.ID is always a slice of constant length, use an array
instead of a slice. Mostly, arrays behave as slices, except that an
array cannot be nil, so use `*backend.ID` insteaf of `backend.ID` in
places where the absence of an ID is possible (e.g. for the Subtree of a
Node, which may not present when the node is a file node).

This change allows to directly use backend.ID as the the key for a map,
so that arbitrary data structures (e.g. a Set implemented as a
map[backend.ID]struct{}) can easily be formed.
This commit is contained in:
Alexander Neumann
2015-07-25 17:05:45 +02:00
parent 2fa6124545
commit 5cdcc99eba
31 changed files with 244 additions and 208 deletions

View File

@@ -23,10 +23,7 @@ const hashSize = sha256.Size
// Hash returns the ID for data.
func Hash(data []byte) ID {
h := hashData(data)
id := make([]byte, IDSize)
copy(id, h[:])
return id
return hashData(data)
}
// Find loads the list of all blobs of type t and searches for names which

View File

@@ -11,39 +11,55 @@ import (
const IDSize = hashSize
// ID references content within a repository.
type ID []byte
type ID [IDSize]byte
// ParseID converts the given string to an ID.
func ParseID(s string) (ID, error) {
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
return ID{}, err
}
if len(b) != IDSize {
return nil, errors.New("invalid length for hash")
return ID{}, errors.New("invalid length for hash")
}
return ID(b), nil
id := ID{}
copy(id[:], b)
return id, nil
}
func (id ID) String() string {
return hex.EncodeToString(id)
return hex.EncodeToString(id[:])
}
const shortStr = 4
func (id ID) Str() string {
// Str returns the shortened string version of id.
func (id *ID) Str() string {
if id == nil {
return "[nil]"
}
if id.IsNull() {
return "[null]"
}
return hex.EncodeToString(id[:shortStr])
}
// IsNull returns true iff id only consists of null bytes.
func (id ID) IsNull() bool {
var nullID ID
return id == nullID
}
// Equal compares an ID to another other.
func (id ID) Equal(other ID) bool {
return bytes.Equal(id, other)
return id == other
}
// EqualString compares this ID to another one, given as a string.
@@ -53,12 +69,15 @@ func (id ID) EqualString(other string) (bool, error) {
return false, err
}
return id.Equal(ID(s)), nil
id2 := ID{}
copy(id2[:], s)
return id == id2, nil
}
// Compare compares this ID to another one, returning -1, 0, or 1.
func (id ID) Compare(other ID) int {
return bytes.Compare(other, id)
return bytes.Compare(other[:], id[:])
}
func (id ID) MarshalJSON() ([]byte, error) {
@@ -72,8 +91,7 @@ func (id *ID) UnmarshalJSON(b []byte) error {
return err
}
*id = make([]byte, IDSize)
_, err = hex.Decode(*id, []byte(s))
_, err = hex.Decode(id[:], []byte(s))
if err != nil {
return err
}
@@ -82,10 +100,7 @@ func (id *ID) UnmarshalJSON(b []byte) error {
}
func IDFromData(d []byte) ID {
hash := hashData(d)
id := make([]byte, IDSize)
copy(id, hash[:])
return id
return hashData(d)
}
type IDs []ID

16
backend/id_int_test.go Normal file
View File

@@ -0,0 +1,16 @@
package backend
import "testing"
func TestIDMethods(t *testing.T) {
var id ID
if id.Str() != "[null]" {
t.Errorf("ID.Str() returned wrong value, want %v, got %v", "[null]", id.Str())
}
var pid *ID
if pid.Str() != "[nil]" {
t.Errorf("ID.Str() returned wrong value, want %v, got %v", "[nil]", pid.Str())
}
}