forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding binary vindex (vitessio#2078)
* 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
Showing
2 changed files
with
120 additions
and
0 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
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) | ||
} |
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,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")) | ||
} | ||
} |