Skip to content

Commit

Permalink
Merge pull request #328 from scylladb/dk/implement_stmt_cache
Browse files Browse the repository at this point in the history
feat(generators): impelemt statement cache
  • Loading branch information
dkropachev authored May 30, 2023
2 parents e771939 + 361fa69 commit 38eff35
Show file tree
Hide file tree
Showing 21 changed files with 490 additions and 126 deletions.
7 changes: 6 additions & 1 deletion pkg/builders/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package builders

import (
"github.com/scylladb/gemini/pkg/querycache"
"github.com/scylladb/gemini/pkg/testschema"
"github.com/scylladb/gemini/pkg/typedef"
)
Expand Down Expand Up @@ -49,7 +50,11 @@ func (s *schemaBuilder) Table(table *testschema.Table) SchemaBuilder {
}

func (s *schemaBuilder) Build() *testschema.Schema {
return &testschema.Schema{Keyspace: s.keyspace, Tables: s.tables}
out := &testschema.Schema{Keyspace: s.keyspace, Tables: s.tables}
for id := range s.tables {
s.tables[id].Init(out, querycache.New(out))
}
return out
}

func NewSchemaBuilder() SchemaBuilder {
Expand Down
4 changes: 4 additions & 0 deletions pkg/coltypes/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (ct *BagType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []int
return []interface{}{out}
}

func (ct *BagType) LenValue() int {
return 1
}

func (ct *BagType) Indexable() bool {
return false
}
4 changes: 4 additions & 0 deletions pkg/coltypes/simple_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (st SimpleType) CQLHolder() string {
return "?"
}

func (st SimpleType) LenValue() int {
return 1
}

func (st SimpleType) CQLPretty(query string, value []interface{}) (string, int) {
if len(value) == 0 {
return query, 0
Expand Down
8 changes: 8 additions & 0 deletions pkg/coltypes/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ func (t *TupleType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []in
}
return out
}

func (t *TupleType) LenValue() int {
out := 0
for _, tp := range t.Types {
out += tp.LenValue()
}
return out
}
12 changes: 12 additions & 0 deletions pkg/coltypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"golang.org/x/exp/rand"

"github.com/scylladb/gemini/pkg/typedef"
"github.com/scylladb/gemini/pkg/utils"
)

const (
Expand Down Expand Up @@ -144,6 +145,10 @@ func (mt *MapType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []int
return []interface{}{vals}
}

func (mt *MapType) LenValue() int {
return 1
}

func (mt *MapType) CQLDef() string {
if mt.Frozen {
return "frozen<map<" + mt.KeyType.CQLDef() + "," + mt.ValueType.CQLDef() + ">>"
Expand Down Expand Up @@ -176,9 +181,16 @@ func (ct *CounterType) CQLPretty(query string, value []interface{}) (string, int
}

func (ct *CounterType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []interface{} {
if utils.UnderTest {
return []interface{}{r.Int63()}
}
return []interface{}{atomic.AddInt64(&ct.Value, 1)}
}

func (ct *CounterType) LenValue() int {
return 1
}

func (ct *CounterType) CQLDef() string {
return "counter"
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/coltypes/udt.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ func (t *UDTType) GenValue(r *rand.Rand, p *typedef.PartitionRangeConfig) []inte
}
return []interface{}{vals}
}

func (t *UDTType) LenValue() int {
return 1
}
17 changes: 14 additions & 3 deletions pkg/generators/schema_mutation_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package generators

import (
"testing"

"github.com/scylladb/gemini/pkg/utils"
)

var (
Expand Down Expand Up @@ -51,6 +53,8 @@ var (
)

func TestGenInsertStmt(t *testing.T) {
utils.SetUnderTest()
t.Parallel()
expected := initExpected(t, "insert.json", genInsertStmtCases, *updateExpected)
if *updateExpected {
defer expected.updateExpected(t)
Expand All @@ -59,7 +63,7 @@ func TestGenInsertStmt(t *testing.T) {
caseName := genInsertStmtCases[idx]
t.Run(caseName,
func(subT *testing.T) {
schema, prc, gen, rnd, useLWT, _ := getAllForTestStmt(subT, genInsertStmtCases[idx])
schema, prc, gen, rnd, useLWT, _ := getAllForTestStmt(subT, caseName)
stmt, err := genInsertStmt(schema, schema.Tables[0], gen.Get(), rnd, prc, useLWT)
validateStmt(subT, stmt, err)
expected.CompareOrStore(subT, caseName, stmt)
Expand All @@ -68,6 +72,8 @@ func TestGenInsertStmt(t *testing.T) {
}

func TestGenUpdateStmt(t *testing.T) {
utils.SetUnderTest()
t.Parallel()
expected := initExpected(t, "update.json", genUpdateStmtCases, *updateExpected)
if *updateExpected {
defer expected.updateExpected(t)
Expand All @@ -76,7 +82,7 @@ func TestGenUpdateStmt(t *testing.T) {
caseName := genUpdateStmtCases[idx]
t.Run(caseName,
func(subT *testing.T) {
schema, prc, gen, rnd, _, _ := getAllForTestStmt(subT, genUpdateStmtCases[idx])
schema, prc, gen, rnd, _, _ := getAllForTestStmt(subT, caseName)
stmt, err := genUpdateStmt(schema, schema.Tables[0], gen.Get(), rnd, prc)
validateStmt(subT, stmt, err)
expected.CompareOrStore(subT, caseName, stmt)
Expand All @@ -85,6 +91,8 @@ func TestGenUpdateStmt(t *testing.T) {
}

func TestGenDeleteRows(t *testing.T) {
utils.SetUnderTest()
t.Parallel()
expected := initExpected(t, "delete.json", genDeleteStmtCases, *updateExpected)
if *updateExpected {
defer expected.updateExpected(t)
Expand All @@ -93,7 +101,7 @@ func TestGenDeleteRows(t *testing.T) {
caseName := genDeleteStmtCases[idx]
t.Run(caseName,
func(subT *testing.T) {
schema, prc, gen, rnd, _, _ := getAllForTestStmt(subT, genDeleteStmtCases[idx])
schema, prc, gen, rnd, _, _ := getAllForTestStmt(subT, caseName)
stmt, err := genDeleteRows(schema, schema.Tables[0], gen.Get(), rnd, prc)
validateStmt(subT, stmt, err)
expected.CompareOrStore(subT, caseName, stmt)
Expand All @@ -102,6 +110,7 @@ func TestGenDeleteRows(t *testing.T) {
}

func BenchmarkGenInsertStmt(t *testing.B) {
utils.SetUnderTest()
for idx := range genInsertStmtCases {
caseName := genInsertStmtCases[idx]
t.Run(caseName,
Expand All @@ -116,6 +125,7 @@ func BenchmarkGenInsertStmt(t *testing.B) {
}

func BenchmarkGenUpdateStmt(t *testing.B) {
utils.SetUnderTest()
for idx := range genUpdateStmtCases {
caseName := genUpdateStmtCases[idx]
t.Run(caseName,
Expand All @@ -130,6 +140,7 @@ func BenchmarkGenUpdateStmt(t *testing.B) {
}

func BenchmarkGenDeleteRows(t *testing.B) {
utils.SetUnderTest()
for idx := range genDeleteStmtCases {
caseName := genDeleteStmtCases[idx]
t.Run(caseName,
Expand Down
Loading

0 comments on commit 38eff35

Please sign in to comment.