-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce KeyFormat that uses a full 8 bytes for int64 values and avoids
string manipulatio/scanning Signed-off-by: Silas Davis <[email protected]>
- Loading branch information
Silas Davis
committed
Aug 30, 2018
1 parent
17e8f36
commit ca4bc10
Showing
8 changed files
with
230 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package iavl | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
// Provides a fixed-width lexicographically sortable []byte key format | ||
type KeyFormat struct { | ||
prefix byte | ||
layout []int | ||
length int | ||
} | ||
|
||
// Create a []byte key format based on a single byte prefix and fixed width key segments each of whose length is | ||
// specified by by the corresponding element of layout | ||
func NewKeyFormat(prefix byte, layout ...int) *KeyFormat { | ||
// For prefix | ||
length := 1 | ||
for _, l := range layout { | ||
length += int(l) | ||
} | ||
return &KeyFormat{ | ||
prefix: prefix, | ||
layout: layout, | ||
length: length, | ||
} | ||
} | ||
|
||
// Format the byte segments into the key format - will panic if the segment lengths to do match the layout. | ||
func (kf *KeyFormat) KeyBytes(segments ...[]byte) []byte { | ||
key := make([]byte, kf.length) | ||
key[0] = kf.prefix | ||
n := 1 | ||
for i, s := range segments { | ||
l := kf.layout[i] | ||
if len(s) > l { | ||
panic(fmt.Errorf("length of segment %X provided to KeyFormat.Key() is longer than the %d bytes "+ | ||
"required by layout for segment %d", s, l, i)) | ||
} | ||
copy(key[n+(l-len(s)):n+l], s) | ||
n += l | ||
} | ||
return key[:n] | ||
} | ||
|
||
// Format the args passed into the key format - will panic if the arguments passed to not match the length | ||
// of the segment to which they correspond. When called with no arguments returns the raw prefix (useful as a start | ||
// element of the entire keys space when sorted lexicographically) | ||
func (kf *KeyFormat) Key(args ...interface{}) []byte { | ||
if len(args) > len(kf.layout) { | ||
panic(fmt.Errorf("KeyFormat.Key() is provided with %d args but format only has %d segments", | ||
len(args), len(kf.layout))) | ||
} | ||
segments := make([][]byte, len(args)) | ||
for i, a := range args { | ||
segments[i] = format(a) | ||
} | ||
return kf.KeyBytes(segments...) | ||
} | ||
|
||
// Reads out the bytes associated with each segment of the key format from key | ||
func (kf *KeyFormat) ScanBytes(key []byte) [][]byte { | ||
segments := make([][]byte, len(kf.layout)) | ||
n := 1 | ||
for i, l := range kf.layout { | ||
end := n + l | ||
if end > len(key) { | ||
return segments[:i] | ||
} | ||
segments[i] = key[n:end] | ||
n = end | ||
} | ||
return segments | ||
} | ||
|
||
// Extracts the segments into the values pointed to by each of args. Each arg must be a pointer to int64, uint64, or | ||
// []byte, and the width of the args must match layout. | ||
func (kf *KeyFormat) Scan(key []byte, args ...interface{}) { | ||
segments := kf.ScanBytes(key) | ||
if len(args) > len(segments) { | ||
panic(fmt.Errorf("KeyFormat.Scan() is provided with %d args but format only has %d segments", | ||
len(args), len(segments))) | ||
} | ||
for i, a := range args { | ||
scan(a, segments[i]) | ||
} | ||
} | ||
|
||
// Return the prefix as a string | ||
func (kf *KeyFormat) Prefix() string { | ||
return string([]byte{kf.prefix}) | ||
} | ||
|
||
func scan(a interface{}, value []byte) { | ||
switch v := a.(type) { | ||
case *int64: | ||
*v = int64(binary.BigEndian.Uint64(value)) | ||
case *uint64: | ||
*v = binary.BigEndian.Uint64(value) | ||
case *[]byte: | ||
*v = value | ||
default: | ||
panic(fmt.Errorf("KeyFormat.scan() does not support scanning value of type %T: %v", a, a)) | ||
} | ||
} | ||
|
||
func format(a interface{}) []byte { | ||
switch v := a.(type) { | ||
case uint64: | ||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, v) | ||
return bs | ||
case uint: | ||
return format(uint64(v)) | ||
case int64: | ||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(v)) | ||
return bs | ||
case int: | ||
return format(int64(v)) | ||
case []byte: | ||
return v | ||
default: | ||
panic(fmt.Errorf("KeyFormat.format() does not support formatting value of type %T: %v", a, a)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package iavl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyFormatBytes(t *testing.T) { | ||
kf := NewKeyFormat(byte('e'), 8, 8, 8) | ||
assert.Equal(t, []byte{'e', 0, 0, 0, 0, 0, 1, 2, 3}, kf.KeyBytes([]byte{1, 2, 3})) | ||
assert.Equal(t, []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8}, kf.KeyBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8})) | ||
assert.Equal(t, []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 2, 2, 3, 3}, | ||
kf.KeyBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{1, 1, 2, 2, 3, 3})) | ||
assert.Equal(t, []byte{'e'}, kf.KeyBytes()) | ||
} | ||
|
||
func TestKeyFormat(t *testing.T) { | ||
kf := NewKeyFormat(byte('e'), 8, 8, 8) | ||
key := []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 1, 144} | ||
var a, b, c int64 = 100, 200, 400 | ||
assert.Equal(t, key, kf.Key(a, b, c)) | ||
|
||
var ao, bo, co = new(int64), new(int64), new(int64) | ||
kf.Scan(key, ao, bo, co) | ||
assert.Equal(t, a, *ao) | ||
assert.Equal(t, b, *bo) | ||
assert.Equal(t, c, *co) | ||
|
||
bs := new([]byte) | ||
kf.Scan(key, ao, bo, bs) | ||
assert.Equal(t, a, *ao) | ||
assert.Equal(t, b, *bo) | ||
assert.Equal(t, []byte{0, 0, 0, 0, 0, 0, 1, 144}, *bs) | ||
|
||
assert.Equal(t, []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200}, kf.Key(a, b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.