Skip to content

Commit

Permalink
1) conversation fixes
Browse files Browse the repository at this point in the history
2) remake ColumnRaw to string instead []byte
  • Loading branch information
illia-li committed Dec 21, 2023
1 parent d04ca41 commit 49104c5
Show file tree
Hide file tree
Showing 34 changed files with 122 additions and 151 deletions.
2 changes: 1 addition & 1 deletion pkg/store/comp/compare_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/comp/compare_info_mv_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/comp/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
12 changes: 5 additions & 7 deletions pkg/store/comp/utils_4test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -214,16 +214,14 @@ func rndSameRowSV(columns, columnLen int) (sv.RowSV, sv.RowSV) {
}

func rndSameRawSV(columnLen int) (sv.ColumnRaw, sv.ColumnRaw) {
out1 := []byte(utils.RandString(rnd, columnLen))
out2 := make(sv.ColumnRaw, columnLen)
copy(out2, out1)
out1 := sv.ColumnRaw(utils.RandString(rnd, columnLen))
out2 := out1
return out1, out2
}

func rndSameRaw(columnLen int) (mv.ColumnRaw, mv.ColumnRaw) {
out1 := []byte(utils.RandString(rnd, columnLen))
out2 := make(mv.ColumnRaw, columnLen)
copy(out2, out1)
out1 := mv.ColumnRaw(utils.RandString(rnd, columnLen))
out2 := out1
return out1, out2
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/store/cqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ func (cs *cqlStore) doMutate(ctx context.Context, builder qb.Builder, ts time.Ti
return nil
}

func (cs *cqlStore) loadSV(ctx context.Context, builder qb.Builder, values []interface{}) (sv.Result, error) {
func (cs *cqlStore) loadSingleVersion(ctx context.Context, builder qb.Builder, values []interface{}) (sv.Result, error) {
query, _ := builder.ToCql()
iter := cs.session.Query(query, values...).WithContext(ctx).Iter()
cs.ops.WithLabelValues(cs.system, opType(builder)).Inc()
return sv.GetResult(iter), iter.Close()
}

func (cs *cqlStore) loadMV(ctx context.Context, builder qb.Builder, values []interface{}) (mv.Result, error) {
func (cs *cqlStore) loadMultiVersion(ctx context.Context, builder qb.Builder, values []interface{}) (mv.Result, error) {
query, _ := builder.ToCql()
iter := cs.session.Query(query, values...).WithContext(ctx).Iter()
cs.ops.WithLabelValues(cs.system, opType(builder)).Inc()
return mv.GetResult(iter), iter.Close()
}

func (cs *cqlStore) loadVerCheck(ctx context.Context, builder qb.Builder, values []interface{}) (mv.Result, error) {
func (cs *cqlStore) loadCheckVersion(ctx context.Context, builder qb.Builder, values []interface{}) (mv.Result, error) {
query, _ := builder.ToCql()
iter := cs.session.Query(query, values...).WithContext(ctx).Iter()
cs.ops.WithLabelValues(cs.system, opType(builder)).Inc()
Expand Down
8 changes: 3 additions & 5 deletions pkg/store/mv/c_list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,8 +35,7 @@ func (l List) ToString(colInfo gocql.TypeInfo) string {
for idx := range l {
out += fmt.Sprintf("%d:%s;", idx, l[idx].ToString(listInfo.Elem))
}
out = out[:len(out)-1]
return out + ">"
return out[:len(out)-1] + ">"
}

func (l List) ToStringRaw() string {
Expand All @@ -47,8 +46,7 @@ func (l List) ToStringRaw() string {
for idx := range l {
out += fmt.Sprintf("%d:%s;", idx, l[idx].ToStringRaw())
}
out = out[:len(out)-1]
return out + ">"
return out[:len(out)-1] + ">"
}

func (l List) EqualColumn(colT interface{}) bool {
Expand Down
10 changes: 6 additions & 4 deletions pkg/store/mv/c_list_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,7 +43,8 @@ func TestList_UnmarshalCQL(t *testing.T) {
expected, data := rndDataElems(elems, maxElemLen, old, true)
// List initialization.
testColumn := make(List, 1)
testColumn[0] = &ColumnRaw{}
tmp := ColumnRaw("")
testColumn[0] = &tmp
// Unmarshall.
if old {
err = testColumn.unmarshalOld(gocql.CollectionType{}, data)
Expand All @@ -56,7 +57,7 @@ func TestList_UnmarshalCQL(t *testing.T) {
t.Fatalf("%s error:%s", errorMsg, err)
}
// With correction needed, because List and Map initialization required fist elem
if len(expected) == 0 && len(testColumn) == 1 && testColumn[0].EqualColumn(ColumnRaw{}) {
if len(expected) == 0 && len(testColumn) == 1 && testColumn[0].EqualColumn(ColumnRaw("")) {
continue
}
if len(testColumn) != len(expected) {
Expand All @@ -83,8 +84,9 @@ func TestList_Equal(t *testing.T) {
if !testColumn1.EqualElem(&testColumn2) {
t.Fatal("List.EqualElem should return true")
}
tmp := ColumnRaw("123")
testColumn2 = []Elem{
&ColumnRaw{1, 2, 3},
&tmp,
}
// EqualColumn test on unequal
if testColumn1.EqualColumn(testColumn2) {
Expand Down
8 changes: 3 additions & 5 deletions pkg/store/mv/c_map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,8 +35,7 @@ func (m Map) ToString(colInfo gocql.TypeInfo) string {
for idx := range m.Keys {
out += fmt.Sprintf("%s:%s;", m.Keys[idx].ToString(mapInfo.Key), m.Values[idx].ToString(mapInfo.Elem))
}
out = out[:len(out)-1]
return out + ")"
return out[:len(out)-1] + ">"
}

func (m Map) ToStringRaw() string {
Expand All @@ -47,8 +46,7 @@ func (m Map) ToStringRaw() string {
for idx := range m.Keys {
out += fmt.Sprintf("key%d%s:value%s;", idx, m.Keys[idx].ToStringRaw(), m.Values[idx].ToStringRaw())
}
out = out[:len(out)-1]
return out + ")"
return out[:len(out)-1] + ">"
}

func (m Map) EqualColumn(colT interface{}) bool {
Expand Down
13 changes: 8 additions & 5 deletions pkg/store/mv/c_map_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,8 +45,10 @@ func TestMap_UnmarshalCQL(t *testing.T) {
// Map initialization.
testColumn.Keys = make([]Elem, 1)
testColumn.Values = make([]Elem, 1)
testColumn.Keys[0] = &ColumnRaw{}
testColumn.Values[0] = &ColumnRaw{}
tmpKey := ColumnRaw("")
tmpValue := ColumnRaw("")
testColumn.Keys[0] = &tmpKey
testColumn.Values[0] = &tmpValue
// Unmarshall.
if old {
err = testColumn.oldUnmarshalCQL(gocql.CollectionType{}, data)
Expand Down Expand Up @@ -95,8 +97,9 @@ func TestMap_Equal(t *testing.T) {
}

// Corrupt values
tmp := ColumnRaw("123")
testColumn2.Values = []Elem{
&ColumnRaw{1, 2, 3},
&tmp,
}
// EqualColumn test on unequal
if testColumn1.EqualColumn(testColumn2) {
Expand All @@ -110,7 +113,7 @@ func TestMap_Equal(t *testing.T) {
// Corrupt keys
testColumn1.Values, testColumn2.Values = rndSameElems(testCeases[i].elems, testCeases[i].elemLen)
testColumn2.Keys = []Elem{
&ColumnRaw{1, 2, 3},
&tmp,
}
// EqualColumn test on unequal
if testColumn1.EqualColumn(testColumn2) {
Expand Down
45 changes: 13 additions & 32 deletions pkg/store/mv/c_raw.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,19 +16,20 @@ package mv

import (
"fmt"
"unsafe"

"github.com/gocql/gocql"
)

// ColumnRaw for most cases.
type ColumnRaw []byte
type ColumnRaw string

func (col ColumnRaw) ToString(colInfo gocql.TypeInfo) string {
if len(col) == 0 {
return ""
}
tmpVal := colInfo.New()
if err := gocql.Unmarshal(colInfo, col, tmpVal); err != nil {
if err := gocql.Unmarshal(colInfo, unsafe.Slice(unsafe.StringData((string)(col)), len(col)), tmpVal); err != nil {
panic(err)
}
out := fmt.Sprintf("%v", dereference(tmpVal))
Expand All @@ -49,63 +50,43 @@ func (col ColumnRaw) ToString(colInfo gocql.TypeInfo) string {
}

func (col ColumnRaw) ToStringRaw() string {
return fmt.Sprint(col)
return fmt.Sprint(unsafe.Slice(unsafe.StringData((string)(col)), len(col)))
}

func (col ColumnRaw) EqualColumn(colT interface{}) bool {
col2, ok := colT.(ColumnRaw)
if len(col) != len(col2) || !ok {
// Columns len are different - means columns are unequal
if !ok {
return false
}
if len(col) < 1 {
// Columns len==0 and same - means columns are equal
// Conditions "<" or ">" works faster that "=="
return true
}
for idx := range col {
if col[idx] != col2[idx] {
return false
}
}
return true
return col == col2
}

func (col ColumnRaw) EqualElem(colT interface{}) bool {
col2, ok := colT.(*ColumnRaw)
if len(col) != len(*col2) || !ok {
if !ok {
// Columns len are different - means columns are unequal
return false
}
if len(col) < 1 {
// Columns len==0 and same - means columns are equal
// Conditions "<" or ">" works faster that "=="
return true
}
for idx := range col {
if col[idx] != (*col2)[idx] {
return false
}
}
return true
return col == *col2
}

func (col ColumnRaw) NewSameColumn() Column {
return ColumnRaw{}
return ColumnRaw("")
}

func (col ColumnRaw) ToUnmarshal() interface{} {
return &col
}

func (col ColumnRaw) NewSameElem() Elem {
return &ColumnRaw{}
tmp := ColumnRaw("")
return &tmp
}

func (col *ColumnRaw) UnmarshalCQL(_ gocql.TypeInfo, data []byte) error {
if len(data) > 0 {
// Puts data without copying
*col = data
*col = (ColumnRaw)(unsafe.String(&data[0], len(data)))
}
return nil
}
18 changes: 9 additions & 9 deletions pkg/store/mv/c_raw_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -25,28 +25,28 @@ import (

func TestColumnRaw_UnmarshalCQL(t *testing.T) {
errorMsg := "wrong ColumnRaw.UnmarshalCQL work:"
testColumn := make(ColumnRaw, 0)
testColumn := ColumnRaw("")

testsCount := 1000
for i := 0; i < testsCount; i++ {
expected := utils.RandBytes(rnd, rnd.Intn(1000))
if i == 0 {
expected = ColumnRaw{}
expected = make([]byte, 0)
}
_ = testColumn.UnmarshalCQL(nil, expected)
if !reflect.DeepEqual(expected, ([]byte)(testColumn)) {
t.Fatalf("%s\nreceived:%+v \nexpected:%+v", errorMsg, testColumn, expected)
}
testColumn = make(ColumnRaw, 0)
testColumn = ColumnRaw("")
}
}

func TestColumnRaw_Equal(t *testing.T) {
testColumn1 := make(ColumnRaw, 0)
testColumn2 := make(ColumnRaw, 0)
testColumn1 := ColumnRaw("")
testColumn2 := ColumnRaw("")
tests := []ColumnRaw{
utils.RandBytes(rnd, rnd.Intn(1000)),
[]byte{},
ColumnRaw(utils.RandBytes(rnd, rnd.Intn(1000))),
ColumnRaw(""),
}
for i := range tests {
testColumn1 = tests[i]
Expand All @@ -59,7 +59,7 @@ func TestColumnRaw_Equal(t *testing.T) {
if !testColumn1.EqualElem(&testColumn2) {
t.Fatal("ColumnRaw.EqualElem should return true")
}
testColumn2 = utils.RandBytes(rnd, rnd.Intn(30))
testColumn2 = ColumnRaw(utils.RandBytes(rnd, rnd.Intn(30)))
// EqualColumn test on unequal
if testColumn1.EqualColumn(testColumn2) {
t.Fatal("ColumnRaw.EqualColumn should return false")
Expand Down
8 changes: 3 additions & 5 deletions pkg/store/mv/c_tuple.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 ScyllaDB
// Copyright 2023 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,7 @@ func (t Tuple) ToString(colInfo gocql.TypeInfo) string {
for i, elem := range tuple.Elems {
out += fmt.Sprintf("%d:%s;", i, t[i].ToString(elem))
}
out = out[:len(out)-1]
return out + ">"
return out[:len(out)-1] + ">"
}

func (t Tuple) ToStringRaw() string {
Expand All @@ -44,8 +43,7 @@ func (t Tuple) ToStringRaw() string {
for i := range t {
out += fmt.Sprintf("%d:%s;", i, t[i].ToStringRaw())
}
out = out[:len(out)-1]
return out + ">"
return out[:len(out)-1] + ">"
}

func (t Tuple) EqualColumn(colT interface{}) bool {
Expand Down
Loading

0 comments on commit 49104c5

Please sign in to comment.