Skip to content

Commit

Permalink
Adding binary vindex (vitessio#2078)
Browse files Browse the repository at this point in the history
* Adding binary vindex

* Update binary_test.go

* changing comments, changing cost to 0, removing paranthesis from return statement

* remove fmt import from test
  • Loading branch information
adkhare authored and enisoc committed Sep 23, 2016
1 parent 313d926 commit e96f725
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
60 changes: 60 additions & 0 deletions go/vt/vtgate/vindexes/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vindexes

import (
"bytes"
"fmt"
)

// Binary is a vindex that converts binary bits to a keyspace id.
type Binary struct {
name string
}

// NewBinary creates a new Binary.
func NewBinary(name string, _ map[string]string) (Vindex, error) {
return &Binary{name: name}, nil
}

// String returns the name of the vindex.
func (vind *Binary) String() string {
return vind.name
}

// Cost returns the cost as 1.
func (vind *Binary) Cost() int {
return 0
}

// Verify returns true if id maps to ksid.
func (vind *Binary) Verify(_ VCursor, id interface{}, ksid []byte) (bool, error) {
data, err := getBytes(id)
if err != nil {
return false, fmt.Errorf("Binary.Verify: %v", err)
}
return bytes.Compare(data, ksid) == 0, nil
}

// Map returns the corresponding keyspace id values for the given ids.
func (vind *Binary) Map(_ VCursor, ids []interface{}) ([][]byte, error) {
out := make([][]byte, 0, len(ids))
for _, id := range ids {
data, err := getBytes(id)
if err != nil {
return nil, fmt.Errorf("Binary.Map :%v", err)
}
out = append(out, data)
}
return out, nil
}

// ReverseMap returns the associated id for the ksid.
func (*Binary) ReverseMap(_ VCursor, ksid []byte) (interface{}, error) {
if ksid == nil {
return nil, fmt.Errorf("Binary.ReverseMap: is nil")
}
return []byte(ksid), nil
}

func init() {
Register("binary", NewBinary)
}
60 changes: 60 additions & 0 deletions go/vt/vtgate/vindexes/binary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vindexes

import (
"testing"
"bytes"
)

var binOnlyVindex Vindex

func init() {
binOnlyVindex, _ = CreateVindex("binary", "vch", nil)
}

func TestBinaryCost(t *testing.T) {
if binOnlyVindex.Cost() != 0 {
t.Errorf("Cost(): %d, want 0", binOnlyVindex.Cost())
}
}

func TestBinary(t *testing.T) {
tcases := []struct {
in, out []byte
}{{
in: []byte("test"),
out: []byte("test"),
}, {
in: []byte("test2"),
out: []byte("test2"),
}, {
in: []byte("test3"),
out: []byte("test3"),
}}
for _, tcase := range tcases {
got, err := binOnlyVindex.(Unique).Map(nil, []interface{}{tcase.in})
if err != nil {
t.Error(err)
}
out := []byte(got[0])
if bytes.Compare(tcase.in, out) != 0 {
t.Errorf("Map(%#v): %#v, want %#v", tcase.in, out, tcase.out)
}
ok, err := binOnlyVindex.Verify(nil, tcase.in, tcase.out)
if err != nil {
t.Error(err)
}
if !ok {
t.Errorf("Verify(%#v): false, want true", tcase.in)
}
}
}

func TestBinaryReverseMap(t *testing.T) {
got, err := binOnlyVindex.(Reversible).ReverseMap(nil, []byte("\x00\x00\x00\x00\x00\x00\x00\x01"))
if err != nil {
t.Error(err)
}
if bytes.Compare(got.([]byte), []byte("\x00\x00\x00\x00\x00\x00\x00\x01")) != 0 {
t.Errorf("ReverseMap(): %+v, want %+v", got, []byte("\x00\x00\x00\x00\x00\x00\x00\x01"))
}
}

0 comments on commit e96f725

Please sign in to comment.