本文整理汇总了Golang中github.com/youtube/vitess/go/vt/key.KeyspaceId函数的典型用法代码示例。如果您正苦于以下问题:Golang KeyspaceId函数的具体用法?Golang KeyspaceId怎么用?Golang KeyspaceId使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了KeyspaceId函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAnnotatingExecuteBatchKeyspaceIdsMultipleIds
func TestAnnotatingExecuteBatchKeyspaceIdsMultipleIds(t *testing.T) {
keyspace, shards := setUpSandboxWithTwoShards("TestAnnotatingExecuteBatchKeyspaceIdsMultipleIds")
err := rpcVTGate.ExecuteBatchKeyspaceIds(
context.Background(),
[]proto.BoundKeyspaceIdQuery{
proto.BoundKeyspaceIdQuery{
Sql: "INSERT INTO table () VALUES();",
Keyspace: keyspace,
KeyspaceIds: []key.KeyspaceId{
key.KeyspaceId([]byte{0x10}),
key.KeyspaceId([]byte{0x15}),
},
},
},
pb.TabletType_MASTER,
false,
nil,
&proto.QueryResultList{})
if err != nil {
t.Fatalf("want nil, got %v", err)
}
verifyBatchQueryAnnotatedAsUnfriendly(
t,
1, // expectedNumQueries
shards[0])
}
示例2: eval
func (krval bvcKeyRange) eval(bv interface{}, op Operator, onMismatch bool) bool {
switch op {
case QR_IN:
switch num, status := getuint64(bv); status {
case QR_OK:
k := key.Uint64Key(num).KeyspaceId()
return key.KeyRange(krval).Contains(k)
case QR_OUT_OF_RANGE:
return false
}
// Not a number. Check string.
switch str, status := getstring(bv); status {
case QR_OK:
return key.KeyRange(krval).Contains(key.KeyspaceId(str))
}
case QR_NOTIN:
switch num, status := getuint64(bv); status {
case QR_OK:
k := key.Uint64Key(num).KeyspaceId()
return !key.KeyRange(krval).Contains(k)
case QR_OUT_OF_RANGE:
return true
}
// Not a number. Check string.
switch str, status := getstring(bv); status {
case QR_OK:
return !key.KeyRange(krval).Contains(key.KeyspaceId(str))
}
default:
panic("unexpected:")
}
return onMismatch
}
示例3: strKeyRange
func strKeyRange(start, end string) bvcKeyRange {
kr := key.KeyRange{
Start: key.KeyspaceId(start),
End: key.KeyspaceId(end),
}
return bvcKeyRange(kr)
}
示例4: siBytes
func siBytes(start, end string) *topo.ShardInfo {
return topo.NewShardInfo("keyspace", start+"-"+end, &topo.Shard{
KeyRange: key.KeyRange{
Start: key.KeyspaceId(start),
End: key.KeyspaceId(end),
},
}, 0)
}
示例5: TestKeyspaceIdQuery
func TestKeyspaceIdQuery(t *testing.T) {
reflected, err := bson.Marshal(&reflectKeyspaceIdQuery{
Sql: "query",
BindVariables: map[string]interface{}{"val": int64(1)},
Keyspace: "keyspace",
KeyspaceIds: []kproto.KeyspaceId{kproto.KeyspaceId("10"), kproto.KeyspaceId("18")},
TabletType: "replica",
Session: &commonSession,
})
if err != nil {
t.Error(err)
}
want := string(reflected)
custom := KeyspaceIdQuery{
Sql: "query",
BindVariables: map[string]interface{}{"val": int64(1)},
Keyspace: "keyspace",
KeyspaceIds: []kproto.KeyspaceId{kproto.KeyspaceId("10"), kproto.KeyspaceId("18")},
TabletType: "replica",
Session: &commonSession,
}
encoded, err := bson.Marshal(&custom)
if err != nil {
t.Error(err)
}
got := string(encoded)
if want != got {
t.Errorf("want\n%+v, got\n%+v", want, got)
}
var unmarshalled KeyspaceIdQuery
err = bson.Unmarshal(encoded, &unmarshalled)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(custom, unmarshalled) {
t.Errorf("want \n%+v, got \n%+v", custom, unmarshalled)
}
extra, err := bson.Marshal(&extraKeyspaceIdQuery{})
if err != nil {
t.Error(err)
}
err = bson.Unmarshal(extra, &unmarshalled)
if err != nil {
t.Error(err)
}
}
示例6: Split
// Split will split the rows into subset for each distribution
func (rs *RowSplitter) Split(result [][][]sqltypes.Value, rows [][]sqltypes.Value) error {
if rs.Type == key.KIT_UINT64 {
for _, row := range rows {
v := sqltypes.MakeNumeric(row[rs.ValueIndex].Raw())
i, err := v.ParseUint64()
if err != nil {
return fmt.Errorf("Non numerical value: %v", err)
}
k := key.Uint64Key(i).KeyspaceId()
for i, kr := range rs.KeyRanges {
if kr.Contains(k) {
result[i] = append(result[i], row)
break
}
}
}
} else {
for _, row := range rows {
k := key.KeyspaceId(row[rs.ValueIndex].Raw())
for i, kr := range rs.KeyRanges {
if kr.Contains(k) {
result[i] = append(result[i], row)
break
}
}
}
}
return nil
}
示例7: TestHashAutoFail
func TestHashAutoFail(t *testing.T) {
_, err := vunhash(key.KeyspaceId("aa"))
want := "invalid keyspace id: 6161"
if err == nil || err.Error() != want {
t.Errorf(`vunhash("aa"): %v, want %s`, err, want)
}
}
示例8: TestHashAutoConvert
func TestHashAutoConvert(t *testing.T) {
cases := []struct {
in int64
out string
}{
{1, "\[email protected]\xb4J\xbaK\xd6"},
{0, "\x8c\xa6M\xe9\xc1\xb1#\xa7"},
{11, "\xae\xfcDI\x1c\xfeGL"},
{0x100000000000000, "\r\x9f'\x9b\xa5\xd8r`"},
{0x800000000000000, " \xb9\xe7g\xb2\xfb\x14V"},
{11, "\xae\xfcDI\x1c\xfeGL"},
{0, "\x8c\xa6M\xe9\xc1\xb1#\xa7"},
}
for _, c := range cases {
got := string(vhash(c.in))
want := c.out
if got != want {
t.Errorf("vhash(%d): %#v, want %q", c.in, got, want)
}
back, err := vunhash(key.KeyspaceId(got))
if err != nil {
t.Error(err)
}
if back != c.in {
t.Errorf("vunhash(%q): %d, want %d", got, back, c.in)
}
}
}
示例9: vhash
func vhash(shardKey uint64) key.KeyspaceId {
var keybytes, hashed [8]byte
binary.BigEndian.PutUint64(keybytes[:], shardKey)
encrypter := cipher.NewCBCEncrypter(block3DES, iv3DES)
encrypter.CryptBlocks(hashed[:], keybytes[:])
return key.KeyspaceId(hashed[:])
}
示例10: Verify
// Verify returns true if id and ksid match.
func (Numeric) Verify(_ planbuilder.VCursor, id interface{}, ksid key.KeyspaceId) (bool, error) {
var keybytes [8]byte
num, err := getNumber(id)
if err != nil {
return false, fmt.Errorf("Numeric.Verify: %v", err)
}
binary.BigEndian.PutUint64(keybytes[:], uint64(num))
return key.KeyspaceId(keybytes[:]) == ksid, nil
}
示例11: getShardForKeyspaceID
func getShardForKeyspaceID(allShards []topo.ShardReference, keyspaceID []byte) (string, error) {
if len(allShards) == 0 {
return "", fmt.Errorf("No shards found for this tabletType")
}
for _, shardReference := range allShards {
if shardReference.KeyRange.Contains(key.KeyspaceId(string(keyspaceID))) {
return shardReference.Name, nil
}
}
return "", fmt.Errorf("KeyspaceId %v didn't match any shards %+v", hex.EncodeToString(keyspaceID), allShards)
}
示例12: TestVTGateBuildEntityIds
func TestVTGateBuildEntityIds(t *testing.T) {
shardMap := make(map[string][]key.KeyspaceId)
shardMap["-20"] = []key.KeyspaceId{key.KeyspaceId("0"), key.KeyspaceId("1")}
shardMap["20-40"] = []key.KeyspaceId{key.KeyspaceId("30")}
sql := "select a from table where id=:id"
entityColName := "kid"
bindVar := make(map[string]interface{})
bindVar["id"] = 10
shards, sqls, bindVars := buildEntityIds(shardMap, sql, entityColName, bindVar)
wantShards := []string{"-20", "20-40"}
wantSqls := map[string]string{
"-20": "select a from table where id=:id and kid in (:kid0, :kid1)",
"20-40": "select a from table where id=:id and kid in (:kid0)",
}
wantBindVars := map[string]map[string]interface{}{
"-20": map[string]interface{}{"id": 10, "kid0": key.KeyspaceId("0"), "kid1": key.KeyspaceId("1")},
"20-40": map[string]interface{}{"id": 10, "kid0": key.KeyspaceId("30")},
}
if !reflect.DeepEqual(wantShards, shards) {
t.Errorf("want %+v, got %+v", wantShards, shards)
}
if !reflect.DeepEqual(wantSqls, sqls) {
t.Errorf("want %+v, got %+v", wantSqls, sqls)
}
if !reflect.DeepEqual(wantBindVars, bindVars) {
t.Errorf("want %+v, got %+v", wantBindVars, bindVars)
}
}
示例13: Map
// Map returns the associated keyspae ids for the given ids.
func (Numeric) Map(_ planbuilder.VCursor, ids []interface{}) ([]key.KeyspaceId, error) {
var keybytes [8]byte
out := make([]key.KeyspaceId, 0, len(ids))
for _, id := range ids {
num, err := getNumber(id)
if err != nil {
return nil, fmt.Errorf("Numeric.Map: %v", err)
}
binary.BigEndian.PutUint64(keybytes[:], uint64(num))
out = append(out, key.KeyspaceId(keybytes[:]))
}
return out, nil
}
示例14: TestAnnotatingExecuteBatchKeyspaceIds
func TestAnnotatingExecuteBatchKeyspaceIds(t *testing.T) {
keyspace, shards := setUpSandboxWithTwoShards("TestAnnotatingExecuteBatchKeyspaceIds")
err := rpcVTGate.ExecuteBatchKeyspaceIds(
context.Background(),
[]proto.BoundKeyspaceIdQuery{
proto.BoundKeyspaceIdQuery{
Sql: "INSERT INTO table () VALUES();",
Keyspace: keyspace,
KeyspaceIds: []key.KeyspaceId{key.KeyspaceId([]byte{0x10})},
},
proto.BoundKeyspaceIdQuery{
Sql: "UPDATE table SET col1=1 WHERE col2>3;",
Keyspace: keyspace,
KeyspaceIds: []key.KeyspaceId{key.KeyspaceId([]byte{0x15})},
},
proto.BoundKeyspaceIdQuery{
Sql: "DELETE FROM table WHERE col1==4;",
Keyspace: keyspace,
KeyspaceIds: []key.KeyspaceId{key.KeyspaceId([]byte{0x25})},
},
},
pb.TabletType_MASTER,
false,
nil,
&proto.QueryResultList{})
if err != nil {
t.Fatalf("want nil, got %v", err)
}
verifyBatchQueryAnnotatedWithKeyspaceIds(
t,
[][]byte{[]byte{0x10}, []byte{0x15}},
shards[0])
verifyBatchQueryAnnotatedWithKeyspaceIds(
t,
[][]byte{[]byte{0x25}},
shards[1])
}
示例15: checkWanted
func checkWanted(t *testing.T, got, expected []pair) {
if len(got) != len(expected) {
t.Fatalf("Wrong number of records: expected %v, got %v", len(expected), len(got))
}
for i, wanted := range expected {
if got[i].kid != key.KeyspaceId(wanted.kid) {
t.Errorf("Wrong keyspace_id: expected %#v, got %#v", wanted.kid, got[i].kid)
}
if got[i].line != wanted.line {
t.Errorf("Wrong line: expected %q got %q", wanted.line, got[i].line)
}
}
}