Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Reversebits vindex support #4041

Merged
merged 6 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions go/vt/vtgate/vindexes/reverse_bits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2017 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vindexes

import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math/bits"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
)

var (
_ Vindex = (*ReverseBits)(nil)
_ Reversible = (*ReverseBits)(nil)
)

// ReverseBits defines vindex that reverses the bits of a number.
// It's Unique, Reversible and Functional.
type ReverseBits struct {
name string
}

// NewReverseBits creates a new ReverseBits.
func NewReverseBits(name string, m map[string]string) (Vindex, error) {
return &ReverseBits{name: name}, nil
}

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

// Cost returns the cost of this index as 1.
func (vind *ReverseBits) Cost() int {
return 1
}

// IsUnique returns true since the Vindex is unique.
func (vind *ReverseBits) IsUnique() bool {
return true
}

// IsFunctional returns true since the Vindex is functional.
func (vind *ReverseBits) IsFunctional() bool {
return true
}

// Map returns the corresponding KeyspaceId values for the given ids.
func (vind *ReverseBits) Map(cursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, len(ids))
for i, id := range ids {
num, err := sqltypes.ToUint64(id)
if err != nil {
out[i] = key.DestinationNone{}
continue
}
out[i] = key.DestinationKeyspaceID(reverse(num))
}
return out, nil
}

// Verify returns true if ids maps to ksids.
func (vind *ReverseBits) Verify(_ VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
out := make([]bool, len(ids))
for i := range ids {
num, err := sqltypes.ToUint64(ids[i])
if err != nil {
return nil, fmt.Errorf("reverseBits.Verify: %v", err)
}
out[i] = (bytes.Compare(reverse(num), ksids[i]) == 0)
}
return out, nil
}

// ReverseMap returns the ids from ksids.
func (vind *ReverseBits) ReverseMap(_ VCursor, ksids [][]byte) ([]sqltypes.Value, error) {
reverseIds := make([]sqltypes.Value, 0, len(ksids))
for _, keyspaceID := range ksids {
val, err := unreverse(keyspaceID)
if err != nil {
return reverseIds, err
}
reverseIds = append(reverseIds, sqltypes.NewUint64(val))
}
return reverseIds, nil
}

func init() {
Register("reverse_bits", NewReverseBits)
}

func reverse(shardKey uint64) []byte {
reversed := make([]byte, 8)
binary.BigEndian.PutUint64(reversed, bits.Reverse64(shardKey))
return reversed
}

func unreverse(k []byte) (uint64, error) {
if len(k) != 8 {
return 0, fmt.Errorf("invalid keyspace id: %v", hex.EncodeToString(k))
}
return bits.Reverse64(binary.BigEndian.Uint64(k)), nil
}
114 changes: 114 additions & 0 deletions go/vt/vtgate/vindexes/reverse_bits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2017 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vindexes

import (
"reflect"
"strings"
"testing"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
)

var reverseBits Vindex

func init() {
hv, err := CreateVindex("reverse_bits", "rr", map[string]string{"Table": "t", "Column": "c"})
if err != nil {
panic(err)
}
reverseBits = hv
}

func TestReverseBitsCost(t *testing.T) {
if reverseBits.Cost() != 1 {
t.Errorf("Cost(): %d, want 1", reverseBits.Cost())
}
}

func TestReverseBitsString(t *testing.T) {
if strings.Compare("rr", reverseBits.String()) != 0 {
t.Errorf("String(): %s, want hash", reverseBits.String())
}
}

func TestReverseBitsMap(t *testing.T) {
got, err := reverseBits.Map(nil, []sqltypes.Value{
sqltypes.NewInt64(1),
sqltypes.NewInt64(2),
sqltypes.NewInt64(3),
sqltypes.NULL,
sqltypes.NewInt64(4),
sqltypes.NewInt64(5),
sqltypes.NewInt64(6),
})
if err != nil {
t.Error(err)
}
want := []key.Destination{
key.DestinationKeyspaceID([]byte("\x80\x00\x00\x00\x00\x00\x00\x00")),
key.DestinationKeyspaceID([]byte("@\x00\x00\x00\x00\x00\x00\x00")),
key.DestinationKeyspaceID([]byte("\xc0\x00\x00\x00\x00\x00\x00\x00")),
key.DestinationNone{},
key.DestinationKeyspaceID([]byte(" \x00\x00\x00\x00\x00\x00\x00")),
key.DestinationKeyspaceID([]byte("\xa0\x00\x00\x00\x00\x00\x00\x00")),
key.DestinationKeyspaceID([]byte("`\x00\x00\x00\x00\x00\x00\x00")),
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Map(): %#v, want %+v", got, want)
}
}

func TestReverseBitsVerify(t *testing.T) {
ids := []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)}
ksids := [][]byte{[]byte("\x80\x00\x00\x00\x00\x00\x00\x00"), []byte("\x80\x00\x00\x00\x00\x00\x00\x00")}
got, err := reverseBits.Verify(nil, ids, ksids)
if err != nil {
t.Fatal(err)
}
want := []bool{true, false}
if !reflect.DeepEqual(got, want) {
t.Errorf("reverseBits.Verify: %v, want %v", got, want)
}

// Failure test
_, err = reverseBits.Verify(nil, []sqltypes.Value{sqltypes.NewVarBinary("aa")}, [][]byte{nil})
wantErr := "reverseBits.Verify: could not parse value: 'aa'"
if err == nil || err.Error() != wantErr {
t.Errorf("reverseBits.Verify err: %v, want %s", err, wantErr)
}
}

func TestReverseBitsReverseMap(t *testing.T) {
got, err := reverseBits.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x80\x00\x00\x00\x00\x00\x00\x00")})
if err != nil {
t.Error(err)
}
want := []sqltypes.Value{sqltypes.NewUint64(uint64(1))}
if !reflect.DeepEqual(got, want) {
t.Errorf("ReverseMap(): %v, want %v", got, want)
}
}

func TestReverseBitsReverseMapNeg(t *testing.T) {
_, err := reverseBits.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x80\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00")})
want := "invalid keyspace id: 80000000000000008000000000000000"
if err.Error() != want {
t.Error(err)
}
}