Skip to content

Commit

Permalink
Merge pull request #105 from cockroachdb/spencerkimball/address-bens-…
Browse files Browse the repository at this point in the history
…comments

Address Ben's feedback from PR #100
  • Loading branch information
spencerkimball committed Oct 5, 2014
2 parents 2dcd82b + 2364daf commit 380ba8a
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions storage/engine/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Fatalf("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
Expand All @@ -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 {
Expand All @@ -95,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
Expand Down Expand Up @@ -208,6 +205,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.
Expand Down

0 comments on commit 380ba8a

Please sign in to comment.