Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Expose the murmur3 partitioning function directly #49

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"unicode"

"github.com/gocql/gocql/internal/lru"
"github.com/gocql/gocql/internal/murmur"
)

// Session is the interface used by users to interact with the database.
Expand Down Expand Up @@ -2122,6 +2123,42 @@ func createRoutingKey(routingKeyInfo *routingKeyInfo, values []interface{}) ([]b
return routingKey, nil
}

var identityIndexes = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

// Murmur3Token computes a murmur3 token for the given values.
func Murmur3Token(types []TypeInfo, values []interface{}) (int64, error) {
if len(values) == 0 {
return 0, fmt.Errorf("gocql: no values provided")
}

if len(types) != len(values) {
return 0, fmt.Errorf("gocql: types and values length mismatch")
}

var indexes []int
if n := len(types); n <= len(identityIndexes) {
indexes = identityIndexes[:n]
} else {
indexes = make([]int, n)
for i := range indexes {
indexes[i] = i
}
}

rki := &routingKeyInfo{
types: types,
indexes: indexes,
names: make([]string, len(types)), // used in error messages
}

routingKey, err := createRoutingKey(rki, values)
if err != nil {
return 0, err
}

return murmur.Murmur3H1(routingKey), nil
}

func (b *Batch) borrowForExecution() {
// empty, because Batch has no equivalent of Query.Release()
// that would race with speculative executions.
Expand Down
105 changes: 105 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,108 @@ func TestIsUseStatement(t *testing.T) {
}
}
}

func TestMurmur3Token(t *testing.T) {
tests := map[string]struct {
types []TypeInfo
values []interface{}
expected int64
err string
}{
"8351-2882-581-20036": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: []interface{}{
int16(8351),
int16(2882),
int16(581),
int16(20036),
},
expected: -9223371846324820981,
},
"3852-744-522-20116": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: []interface{}{
int16(3852),
int16(744),
int16(522),
int16(20116),
},
expected: -9223370757649630452,
},
"5212-813-19933-0": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: []interface{}{
int16(5212),
int16(813),
int16(19933),
int16(0),
},
expected: 5363655924167674765,
},
"empty": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: nil,
err: "gocql: no values provided",
},
"mismatched length": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: []interface{}{
int16(5212),
int16(813),
int16(19933),
int16(0),
},
err: "gocql: types and values length mismatch",
},
"invalid value": {
types: []TypeInfo{
NewNativeType(protoVersion4, TypeSmallInt, ""),
NewNativeType(protoVersion4, TypeSmallInt, ""),
},
values: []interface{}{
int16(5212),
"hello there",
},
err: "can not marshal string into smallint: strconv.ParseInt: parsing \"hello there\": invalid syntax",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, err := Murmur3Token(test.types, test.values)
if test.err != "" {
if err == nil {
t.Fatalf("expected error %q, got nil", test.err)
}
if gotErr := err.Error(); gotErr != test.err {
t.Fatalf("expected error %q, got %q", test.err, gotErr)
}
} else {
if err != nil {
t.Fatalf("unexpected error %q", err)
}
if got != test.expected {
t.Fatalf("expected %v, got %v", test.expected, got)
}
}
})
}
}
Loading