From 1f2aae64976f97541a60ab73710c1bf1afb483a1 Mon Sep 17 00:00:00 2001 From: Spencer Kimball Date: Fri, 3 Oct 2014 18:03:38 -0400 Subject: [PATCH 1/2] Address Ben's feedback from PR #100 --- storage/engine/keys.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/storage/engine/keys.go b/storage/engine/keys.go index 43bd3bd07dd7..8f2a8fc8f7bb 100644 --- a/storage/engine/keys.go +++ b/storage/engine/keys.go @@ -32,26 +32,24 @@ type Key []byte // MakeKey makes a new key which is the concatenation of the // given inputs, in order. -func MakeKey(prefix Key, keys ...Key) Key { - suffix := []byte(nil) - for _, k := range keys { - suffix = append(suffix, []byte(k)...) +func MakeKey(keys ...Key) Key { + byteSlices := make([][]byte, len(keys)) + for i, k := range keys { + byteSlices[i] = []byte(k) } - return Key(bytes.Join([][]byte{prefix, suffix}, []byte{})) + return Key(bytes.Join(byteSlices, nil)) } -// MakeLocalKey concatenates KeyLocalPrefix with the specified key -// value, verifying in the process that the prefix has the length of -// KeyLocalPrefixLength and that this length in bytes is a multiple of -// seven. -func MakeLocalKey(prefix Key, keys ...Key) Key { - if KeyLocalPrefixLength%7 != 0 { - log.Fatalf("local key prefix is not a multiple of 7: %d", KeyLocalPrefixLength) +// MakeLocalKey is a simple passthrough to MakeKey, with verification +// that the first key has length KeyLocalPrefixLength. +func MakeLocalKey(keys ...Key) Key { + if len(keys) == 0 { + log.Fatal("no key components specified in call to MakeLocalKey") } - if len(prefix) != KeyLocalPrefixLength { - log.Fatalf("local key prefix length must be %d: %q", KeyLocalPrefixLength, prefix) + if len(keys[0]) != KeyLocalPrefixLength { + log.Fatal("local key prefix length must be %d: %q", KeyLocalPrefixLength, keys[0]) } - return MakeKey(prefix, keys...) + return MakeKey(keys...) } // Address returns the address for the key, used to lookup the range @@ -72,8 +70,8 @@ func (k Key) Address() Key { return k[KeyLocalPrefixLength:] } -// DecodeKey returns a Key initialized by decoding two binary-encoded -// prefixes from the passed in bytes slice. Any leftover bytes are +// DecodeKey returns a Key initialized by decoding a binary-encoded +// prefix from the passed in bytes slice. Any leftover bytes are // returned. func DecodeKey(b []byte) ([]byte, Key) { if len(b) == 0 { @@ -208,6 +206,12 @@ func ValidateRangeMetaKey(key Key) error { return nil } +func init() { + if KeyLocalPrefixLength%7 != 0 { + log.Fatal("local key prefix is not a multiple of 7: %d", KeyLocalPrefixLength) + } +} + // Constants for system-reserved keys in the KV map. var ( // KeyMin is a minimum key value which sorts before all other keys. From 2364daf8caba092a82eed16cae474a45255fcce0 Mon Sep 17 00:00:00 2001 From: Spencer Kimball Date: Sun, 5 Oct 2014 15:29:06 -0400 Subject: [PATCH 2/2] address Tobias' comments --- storage/engine/keys.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/storage/engine/keys.go b/storage/engine/keys.go index 8f2a8fc8f7bb..45038bf72856 100644 --- a/storage/engine/keys.go +++ b/storage/engine/keys.go @@ -47,7 +47,7 @@ func MakeLocalKey(keys ...Key) Key { log.Fatal("no key components specified in call to MakeLocalKey") } if len(keys[0]) != KeyLocalPrefixLength { - log.Fatal("local key prefix length must be %d: %q", KeyLocalPrefixLength, keys[0]) + log.Fatalf("local key prefix length must be %d: %q", KeyLocalPrefixLength, keys[0]) } return MakeKey(keys...) } @@ -93,11 +93,10 @@ func (k Key) Next() Key { return MakeKey(k, Key{0}) } -// PrefixEnd determines the end key given key as a prefix, that is -// the key that sorts precisely behind all keys starting with prefix: -// "1" is added to the final byte and the carry propagated. The -// special cases of nil and KeyMin ("") always returns KeyMax -// ("\xff"). +// PrefixEnd determines the end key given key as a prefix, that is the +// key that sorts precisely behind all keys starting with prefix: "1" +// is added to the final byte and the carry propagated. The special +// cases of nil and KeyMin always returns KeyMax. func (k Key) PrefixEnd() Key { if len(k) == 0 { return KeyMax