Documentation ¶
Overview ¶
Package bingo packs values into composite keys, using a byte encoding that preserves the sort order of the original values.
Many of the encoding formats are the same as those used in HBase's OrderedBytes class, which were in turn modeled after encoding formats defined by SQLite4 and Orderly.
Index ¶
- func MustPack(vals ...interface{}) []byte
- func MustPackTo(b []byte, vals ...interface{}) int
- func Pack(vals ...interface{}) ([]byte, error)
- func PackTo(b []byte, vals ...interface{}) (n int, err error)
- func PackedSize(vals []interface{}) (int, error)
- func Unpack(b []byte, dests ...interface{}) error
- func UnpackIndex(b []byte, idx int, dest interface{}) error
- func WritePackedTo(w io.Writer, vals ...interface{}) (n int, err error)
- type Schema
- func (s Schema) MustPack(vals ...interface{}) []byte
- func (s Schema) MustPackTo(b []byte, vals ...interface{}) int
- func (s Schema) Pack(vals ...interface{}) ([]byte, error)
- func (s Schema) PackTo(b []byte, vals ...interface{}) (n int, err error)
- func (s Schema) WritePackedTo(w io.Writer, vals ...interface{}) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustPack ¶
func MustPack(vals ...interface{}) []byte
MustPack encodes the values passed, returning the resulting byte slice. Panics on error.
func MustPackTo ¶
MustPackTo encodes the values passed into the provided byte slice, returning the number of bytes written. Panics on error.
func Pack ¶
Pack encodes the values passed, returning the resulting byte slice. This requires 1 heap alloc for the return value.
Example ¶
package main import ( "bytes" "fmt" "sort" "github.com/iancmcc/bingo" ) func main() { // Pack some values into keys a, _ := bingo.Pack(1, "b", float32(4.2)) b, _ := bingo.Pack(1, "b", float32(3.1)) c, _ := bingo.Pack(1, "a", float32(7.5)) // Sort the resulting byte slices packed := [][]byte{a, b, c} sort.SliceStable(packed, func(p, q int) bool { return bytes.Compare(packed[p], packed[q]) < 0 }) // Unpack again and witness the glorious order var ( first int second string third float32 ) for _, bs := range packed { bingo.Unpack(bs, &first, &second, &third) fmt.Println(first, second, third) } }
Output: 1 a 7.5 1 b 3.1 1 b 4.2
func PackTo ¶
PackTo encodes the values passed into the provided byte slice, returning the number of bytes written.
Example ¶
package main import ( "time" "github.com/iancmcc/bingo" ) func main() { // Pack requires an allocation for the resulting byte array, but you can // also pack to a byte array you already have values := []interface{}{1, "a", time.Now()} // Get the size you'll need size, _ := bingo.PackedSize(values) dest := make([]byte, size) bingo.PackTo(dest, values...) }
Output:
func PackedSize ¶
PackedSize returns the number of bytes required to pack the values passed
func UnpackIndex ¶
UnpackIndex unpacks the idx'th value from b into the target provided.
func WritePackedTo ¶
WritePackedTo encodes the values passed and writes the result to the io.Writer specified. Note: this requires 1 heap alloc for the intermediate byte array.
Example ¶
package main import ( "bytes" "fmt" "github.com/iancmcc/bingo" ) func main() { var buf bytes.Buffer bingo.WritePackedTo(&buf, 1, "a") var ( first int second string ) bingo.Unpack(buf.Bytes(), &first, &second) fmt.Println(first, second) }
Output: 1 a
Types ¶
type Schema ¶
type Schema uint64
Schema captures whether fields of a key should be encoded in inverse or natural order.
func WithDesc ¶
WithDesc returns a Schema that will produce packed keys with the indicated values encoded to sort in descending order.
Example ¶
package main import ( "bytes" "fmt" "sort" "github.com/iancmcc/bingo" ) func main() { // Create a schema that packs the second value in descending order schema := bingo.WithDesc(false, true, false) // Pack some values into keys a, _ := schema.Pack(1, "b", float32(4.2)) b, _ := schema.Pack(1, "b", float32(3.1)) c, _ := schema.Pack(1, "a", float32(7.5)) // Sort the resulting byte slices packed := [][]byte{a, b, c} sort.SliceStable(packed, func(p, q int) bool { return bytes.Compare(packed[p], packed[q]) < 0 }) // Unpack to see the order var ( first int second string third float32 ) for _, bs := range packed { bingo.Unpack(bs, &first, &second, &third) fmt.Println(first, second, third) } }
Output: 1 b 3.1 1 b 4.2 1 a 7.5
func (Schema) MustPack ¶
MustPack encodes the values passed, returning the resulting byte slice and panicking on error.
func (Schema) MustPackTo ¶
MustPackTo encodes the values passed into the provided byte slice, returning the number of bytes written. Panics on error.