diff --git a/.golangci.yml b/.golangci.yml index ecb324d..9e44243 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,7 +16,6 @@ output: issues: max-same-issues: 1 max-issues-per-linter: 10 - new: true exclude: - Error return value of `.*.Unlock` is not checked - Error return value of `.*.Completed` is not checked diff --git a/cmd/gemini/generators.go b/cmd/gemini/generators.go index 9b4f51d..96a1ffc 100644 --- a/cmd/gemini/generators.go +++ b/cmd/gemini/generators.go @@ -28,7 +28,8 @@ func createGenerators( ) (generators.Generators, error) { partitionRangeConfig := schemaConfig.GetPartitionRangeConfig() - var gs []*generators.Generator + gs := make([]*generators.Generator, 0, len(schema.Tables)) + for id := range schema.Tables { table := schema.Tables[id] pkVariations := table.PartitionKeys.ValueVariationsNumber(&partitionRangeConfig) diff --git a/pkg/generators/statement_generator.go b/pkg/generators/statement_generator.go index 25503a8..1755e51 100644 --- a/pkg/generators/statement_generator.go +++ b/pkg/generators/statement_generator.go @@ -102,7 +102,7 @@ func GetCreateKeyspaces(s *typedef.Schema) (string, string) { } func GetCreateSchema(s *typedef.Schema) []string { - var stmts []string + stmts := make([]string, 0, len(s.Tables)*2) for _, t := range s.Tables { createTypes := GetCreateTypes(t, s.Keyspace) diff --git a/pkg/generators/statement_generator_test.go b/pkg/generators/statement_generator_test.go index 405abb0..1356fa2 100644 --- a/pkg/generators/statement_generator_test.go +++ b/pkg/generators/statement_generator_test.go @@ -179,7 +179,7 @@ func TestGetCreateSchema(t *testing.T) { t.Parallel() got := generators.GetCreateTable(test.table, ks) if diff := cmp.Diff(got, test.want); diff != "" { - t.Fatalf(diff) + t.Fatalf("cmp.Diff failed: %s", diff) } }) } diff --git a/pkg/generators/table.go b/pkg/generators/table.go index 3f9da28..ec3be8f 100644 --- a/pkg/generators/table.go +++ b/pkg/generators/table.go @@ -25,11 +25,10 @@ func GetCreateTable(t *typedef.Table, ks typedef.Keyspace) string { t.RLock() defer t.RUnlock() - var ( - partitionKeys []string - clusteringKeys []string - columns []string - ) + partitionKeys := make([]string, 0, len(t.PartitionKeys)) + clusteringKeys := make([]string, 0, len(t.ClusteringKeys)) + columns := make([]string, 0, len(t.PartitionKeys)+len(t.ClusteringKeys)+len(t.Columns)) + for _, pk := range t.PartitionKeys { partitionKeys = append(partitionKeys, pk.Name) columns = append(columns, fmt.Sprintf("%s %s", pk.Name, pk.Type.CQLDef())) @@ -60,7 +59,8 @@ func GetCreateTypes(t *typedef.Table, keyspace typedef.Keyspace) []string { t.RLock() defer t.RUnlock() - var stmts []string + stmts := make([]string, 0, len(t.Columns)) + for _, column := range t.Columns { c, ok := column.Type.(*typedef.UDTType) if !ok { diff --git a/pkg/jobs/gen_check_stmt.go b/pkg/jobs/gen_check_stmt.go index 3822b40..88ad77f 100644 --- a/pkg/jobs/gen_check_stmt.go +++ b/pkg/jobs/gen_check_stmt.go @@ -32,16 +32,15 @@ func GenCheckStmt( rnd *rand.Rand, p *typedef.PartitionRangeConfig, ) *typedef.Stmt { - n := 0 mvNum := -1 - maxClusteringRels := 0 - numQueryPKs := 0 if len(table.MaterializedViews) > 0 && rnd.Int()%2 == 0 { mvNum = utils.RandInt2(rnd, 0, len(table.MaterializedViews)) } switch mvNum { case -1: + var n int + if len(table.Indexes) > 0 { n = rnd.Intn(5) } else { @@ -51,22 +50,22 @@ func GenCheckStmt( case 0: return genSinglePartitionQuery(s, table, g) case 1: - numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len()) + numQueryPKs := utils.RandInt2(rnd, 1, table.PartitionKeys.Len()) multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len()))) if multiplier > 100 { numQueryPKs = 1 } return genMultiplePartitionQuery(s, table, g, numQueryPKs) case 2: - maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len()) + maxClusteringRels := utils.RandInt2(rnd, 0, table.ClusteringKeys.Len()) return genClusteringRangeQuery(s, table, g, rnd, p, maxClusteringRels) case 3: - numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len()) + numQueryPKs := utils.RandInt2(rnd, 1, table.PartitionKeys.Len()) multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len()))) if multiplier > 100 { numQueryPKs = 1 } - maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len()) + maxClusteringRels := utils.RandInt2(rnd, 0, table.ClusteringKeys.Len()) return genMultiplePartitionClusteringRangeQuery(s, table, g, rnd, p, numQueryPKs, maxClusteringRels) case 4: // Reducing the probability to hit these since they often take a long time to run @@ -79,13 +78,12 @@ func GenCheckStmt( } } default: - n = rnd.Intn(4) - switch n { + switch rnd.Intn(4) { case 0: return genSinglePartitionQueryMv(s, table, g, rnd, p, mvNum) case 1: lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len() - numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys) + numQueryPKs := utils.RandInt2(rnd, 1, lenPartitionKeys) multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys))) if multiplier > 100 { numQueryPKs = 1 @@ -93,17 +91,17 @@ func GenCheckStmt( return genMultiplePartitionQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs) case 2: lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len() - maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys) + maxClusteringRels := utils.RandInt2(rnd, 0, lenClusteringKeys) return genClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, maxClusteringRels) case 3: lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len() - numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys) + numQueryPKs := utils.RandInt2(rnd, 1, lenPartitionKeys) multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys))) if multiplier > 100 { numQueryPKs = 1 } lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len() - maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys) + maxClusteringRels := utils.RandInt2(rnd, 0, lenClusteringKeys) return genMultiplePartitionClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs, maxClusteringRels) } } @@ -285,7 +283,7 @@ func genClusteringRangeQuery( if vs == nil { return nil } - var allTypes []typedef.Type + allTypes := make([]typedef.Type, 0, len(t.PartitionKeys)+maxClusteringRels+1) values := vs.Value.Copy() builder := qb.Select(s.Keyspace.Name + "." + t.Name) @@ -340,7 +338,7 @@ func genClusteringRangeQueryMv( } builder := qb.Select(s.Keyspace.Name + "." + mv.Name) - var allTypes []typedef.Type + allTypes := make([]typedef.Type, 0, len(mv.PartitionKeys)+maxClusteringRels+1) for _, pk := range mv.PartitionKeys { builder = builder.Where(qb.Eq(pk.Name)) allTypes = append(allTypes, pk.Type) diff --git a/pkg/jobs/gen_utils_test.go b/pkg/jobs/gen_utils_test.go index 4c61c48..62e4e46 100644 --- a/pkg/jobs/gen_utils_test.go +++ b/pkg/jobs/gen_utils_test.go @@ -170,7 +170,9 @@ func convertStmtToResults(stmt *typedef.Stmt) *result { types = fmt.Sprintf("%s %s", types, stmt.Types[idx].Name()) } query, names := stmt.Query.ToCql() - var tokens []resultToken + + tokens := make([]resultToken, 0, len(stmt.ValuesWithToken)) + for _, valueToken := range stmt.ValuesWithToken { tokens = append(tokens, resultToken{ Token: fmt.Sprintf("%v", valueToken.Token), diff --git a/pkg/querycache/querycache.go b/pkg/querycache/querycache.go index 6336a9b..0aa755a 100644 --- a/pkg/querycache/querycache.go +++ b/pkg/querycache/querycache.go @@ -137,7 +137,7 @@ func genInsertIfNotExistsStmtCache( } func genUpdateStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache { - var allTypes []typedef.Type + allTypes := make([]typedef.Type, 0, t.PartitionKeys.Len()+t.ClusteringKeys.Len()+t.Columns.Len()) builder := qb.Update(s.Keyspace.Name + "." + t.Name) for _, cdef := range t.Columns { @@ -169,7 +169,8 @@ func genUpdateStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache } func genDeleteStmtCache(s *typedef.Schema, t *typedef.Table) *typedef.StmtCache { - var allTypes []typedef.Type + allTypes := make([]typedef.Type, 0, t.PartitionKeys.Len()+t.ClusteringKeys.Len()) + builder := qb.Delete(s.Keyspace.Name + "." + t.Name) for _, pk := range t.PartitionKeys { builder = builder.Where(qb.Eq(pk.Name)) diff --git a/pkg/store/cqlstore.go b/pkg/store/cqlstore.go index 0a8c4b7..6a2269a 100644 --- a/pkg/store/cqlstore.go +++ b/pkg/store/cqlstore.go @@ -119,19 +119,6 @@ func newSession(cluster *gocql.ClusterConfig, out io.Writer) (*gocql.Session, er return session, nil } -func ignore(err error) bool { - if err == nil { - return true - } - //nolint:errorlint - switch { - case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded): - return true - default: - return false - } -} - func opType(stmt *typedef.Stmt) string { switch stmt.Query.(type) { case *qb.InsertBuilder: diff --git a/pkg/store/helpers.go b/pkg/store/helpers.go index 6758851..8dff43b 100644 --- a/pkg/store/helpers.go +++ b/pkg/store/helpers.go @@ -26,13 +26,15 @@ import ( ) func pks(t *typedef.Table, rows []map[string]any) []string { - var keySet []string + keySet := make([]string, 0, len(rows)) + for _, row := range rows { keys := make([]string, 0, len(t.PartitionKeys)+len(t.ClusteringKeys)) keys = extractRowValues(keys, t.PartitionKeys, row) keys = extractRowValues(keys, t.ClusteringKeys, row) keySet = append(keySet, strings.Join(keys, ", ")) } + return keySet } diff --git a/pkg/tableopts/options.go b/pkg/tableopts/options.go index 8b3bd0b..020706c 100644 --- a/pkg/tableopts/options.go +++ b/pkg/tableopts/options.go @@ -68,7 +68,8 @@ func FromCQL(cql string) (Option, error) { } func CreateTableOptions(tableOptionStrings []string, logger *zap.Logger) []Option { - var tableOptions []Option + tableOptions := make([]Option, 0, len(tableOptionStrings)) + for _, optionString := range tableOptionStrings { o, err := FromCQL(optionString) if err != nil { diff --git a/pkg/testutils/name_mappings.go b/pkg/testutils/name_mappings.go index 3fa9eed..d36bea1 100644 --- a/pkg/testutils/name_mappings.go +++ b/pkg/testutils/name_mappings.go @@ -162,7 +162,8 @@ func getTestSchema(table *typedef.Table) (*typedef.Schema, *typedef.SchemaConfig func createTableOptions(cql string) []tableopts.Option { opt, _ := tableopts.FromCQL(cql) opts := []string{opt.ToCQL()} - var tableOptions []tableopts.Option + + tableOptions := make([]tableopts.Option, 0, len(opts)) for _, optionString := range opts { o, err := tableopts.FromCQL(optionString) diff --git a/pkg/typedef/columns_test.go b/pkg/typedef/columns_test.go index 0bfb3ac..155592f 100644 --- a/pkg/typedef/columns_test.go +++ b/pkg/typedef/columns_test.go @@ -57,12 +57,17 @@ func TestColumnMarshalUnmarshal(t *testing.T) { def typedef.ColumnDef expected string } - var testCases []testCase + + testCases := make([]testCase, 0, len(allSimpleTypes)) + for _, simpleType := range allSimpleTypes { - testCases = append(testCases, testCase{def: typedef.ColumnDef{ - Name: simpleType.Name(), - Type: simpleType, - }, expected: fmt.Sprintf("{\"type\":\"%s\",\"name\":\"%s\"}", simpleType.Name(), simpleType.Name())}) + testCases = append(testCases, testCase{ + def: typedef.ColumnDef{ + Name: simpleType.Name(), + Type: simpleType, + }, + expected: fmt.Sprintf("{\"type\":\"%s\",\"name\":\"%s\"}", simpleType.Name(), simpleType.Name()), + }) } udtTypes := map[string]typedef.SimpleType{} @@ -94,7 +99,7 @@ func TestColumnMarshalUnmarshal(t *testing.T) { fmt.Println(tcase.expected) if diff := cmp.Diff(string(marshaledData), tcase.expected); diff != "" { - t.Errorf(diff) + t.Errorf("Comparison failed: %s", diff) } var unmarshaledDef typedef.ColumnDef err = json.Unmarshal(marshaledData, &unmarshaledDef) @@ -103,7 +108,7 @@ func TestColumnMarshalUnmarshal(t *testing.T) { } if diff := cmp.Diff(tcase.def, unmarshaledDef); diff != "" { - t.Errorf(diff) + t.Errorf("cmp.Diff failed: %s", diff) } }) } diff --git a/pkg/typedef/types_test.go b/pkg/typedef/types_test.go index e15a9e0..779c244 100644 --- a/pkg/typedef/types_test.go +++ b/pkg/typedef/types_test.go @@ -218,6 +218,7 @@ func TestCQLPretty(t *testing.T) { for id := range prettytests { test := prettytests[id] t.Run(test.typ.Name(), func(t *testing.T) { + t.Parallel() builder := bytes.NewBuffer(nil) err := prettyCQL(builder, test.query, test.values, []Type{test.typ}) if err != nil { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0b775d4..956d529 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -72,11 +72,11 @@ func RandIPV4Address(rnd *rand.Rand, v, pos int) string { return strings.Join(blocks, ".") } -func RandInt2(rnd *rand.Rand, min, max int) int { - if max <= min { - return min +func RandInt2(rnd *rand.Rand, minimum, maximum int) int { + if maximum <= minimum { + return minimum } - return min + rnd.Intn(max-min) + return minimum + rnd.Intn(maximum-minimum) } func IgnoreError(fn func() error) {