Skip to content

Commit

Permalink
schema: Add support for clustering key columns
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Jul 19, 2018
1 parent 73931af commit fae8ad9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
14 changes: 11 additions & 3 deletions cmd/gemini/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ func run(cmd *cobra.Command, args []string) {
})
schemaBuilder.Table(gemini.Table{
Name: "data",
PrimaryKey: gemini.ColumnDef{
Name: "pk",
Type: "int",
PartitionKeys: []gemini.ColumnDef{
gemini.ColumnDef{
Name: "pk",
Type: "int",
},
},
ClusteringKeys: []gemini.ColumnDef{
gemini.ColumnDef{
Name: "ck",
Type: "int",
},
},
Columns: []gemini.ColumnDef{
gemini.ColumnDef{
Expand Down
43 changes: 33 additions & 10 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gemini
import (
"fmt"
"math/rand"
"strings"
)

type Keyspace struct {
Expand All @@ -15,9 +16,10 @@ type ColumnDef struct {
}

type Table struct {
Name string
PrimaryKey ColumnDef
Columns []ColumnDef
Name string
PartitionKeys []ColumnDef
ClusteringKeys []ColumnDef
Columns []ColumnDef
}

type Schema interface {
Expand All @@ -40,22 +42,43 @@ func (s *schema) GetDropSchema() []string {

func (s *schema) GetCreateSchema() []string {
createKeyspace := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}", s.keyspace.Name)
createTable := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (", s.keyspace.Name, s.table.Name)
createTable += fmt.Sprintf("%s %s PRIMARY KEY", s.table.PrimaryKey.Name, s.table.PrimaryKey.Type)
partitionKeys := []string{}
clusteringKeys := []string{}
columns := []string{}
for _, pk := range s.table.PartitionKeys {
partitionKeys = append(partitionKeys, pk.Name)
columns = append(columns, fmt.Sprintf("%s %s", pk.Name, pk.Type))
}
for _, ck := range s.table.ClusteringKeys {
clusteringKeys = append(clusteringKeys, ck.Name)
columns = append(columns, fmt.Sprintf("%s %s", ck.Name, ck.Type))
}
for _, cdef := range s.table.Columns {
createTable += fmt.Sprintf(", %s %s", cdef.Name, cdef.Type)
columns = append(columns, fmt.Sprintf("%s %s", cdef.Name, cdef.Type))
}
createTable += ")"
createTable := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY ((%s), %s))", s.keyspace.Name, s.table.Name, strings.Join(columns, ","), strings.Join(partitionKeys, ","), strings.Join(clusteringKeys, ","))
return []string{
createKeyspace,
createTable,
}
}

func (s *schema) GenMutateOp() string {
pk := rand.Intn(100)
value := rand.Intn(100)
return fmt.Sprintf("INSERT INTO %s.%s (%s, %s) VALUES (%d, %d)", s.keyspace.Name, s.table.Name, s.table.PrimaryKey.Name, s.table.Columns[0].Name, pk, value)
columns := []string{}
values := []string{}
for _, pk := range s.table.PartitionKeys {
columns = append(columns, pk.Name)
values = append(values, fmt.Sprintf("%d", rand.Intn(100)))
}
for _, pk := range s.table.ClusteringKeys {
columns = append(columns, pk.Name)
values = append(values, fmt.Sprintf("%d", rand.Intn(100)))
}
for _, cdef := range s.table.Columns {
columns = append(columns, cdef.Name)
values = append(values, fmt.Sprintf("%d", rand.Intn(100)))
}
return fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (%s)", s.keyspace.Name, s.table.Name, strings.Join(columns, ","), strings.Join(values, ","))
}

func (s *schema) GenCheckOp() string {
Expand Down

0 comments on commit fae8ad9

Please sign in to comment.