Skip to content

Commit

Permalink
schema: Separate value range generation to genValueRange
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Feb 12, 2019
1 parent 7586d2a commit c458b61
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ func genValue(columnType string, p *PartitionRange, values []interface{}) []inte
values = append(values, randRange(p.Min, p.Max))
case "bigint":
values = append(values, rand.Int63())
case "int_range":
start := randRange(p.Min, p.Max)
end := start + randRange(p.Min, p.Max)
values = append(values, start)
values = append(values, end)
case "blob", "uuid":
r, _ := uuid.NewRandom()
values = append(values, r.String())
Expand All @@ -145,6 +140,19 @@ func genValue(columnType string, p *PartitionRange, values []interface{}) []inte
return values
}

func genValueRange(columnType string, p *PartitionRange, values []interface{}) []interface{} {
switch columnType {
case "int":
start := randRange(p.Min, p.Max)
end := start + randRange(p.Min, p.Max)
values = append(values, start)
values = append(values, end)
default:
panic(fmt.Sprintf("generate value range: not supported type %s", columnType))
}
return values
}

func (s *Schema) GetCreateSchema() []string {
createKeyspace := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}", s.Keyspace.Name)

Expand Down Expand Up @@ -215,7 +223,7 @@ func (s *Schema) GenDeleteRows(t Table, p *PartitionRange) *Stmt {
if len(t.ClusteringKeys) == 1 {
for _, ck := range t.ClusteringKeys {
relations = append(relations, fmt.Sprintf("%s >= ? AND %s <= ?", ck.Name, ck.Name))
values = genValue(ck.Type+"_range", p, values)
values = genValueRange(ck.Type, p, values)
}
}
query := fmt.Sprintf("DELETE FROM %s.%s WHERE %s", s.Keyspace.Name, t.Name, strings.Join(relations, " AND "))
Expand Down Expand Up @@ -297,7 +305,7 @@ func (s *Schema) genClusteringRangeQuery(t Table, p *PartitionRange) *Stmt {
}
for _, ck := range t.ClusteringKeys {
relations = append(relations, fmt.Sprintf("%s > ? AND %s < ?", ck.Name, ck.Name))
values = genValue(ck.Type+"_range", p, values)
values = genValueRange(ck.Type, p, values)
}
query := fmt.Sprintf("SELECT * FROM %s.%s WHERE %s", s.Keyspace.Name, t.Name, strings.Join(relations, " AND "))
return &Stmt{
Expand All @@ -322,7 +330,7 @@ func (s *Schema) genMultiplePartitionClusteringRangeQuery(t Table, p *PartitionR
}
for _, ck := range t.ClusteringKeys {
relations = append(relations, fmt.Sprintf("%s >= ? AND %s <= ?", ck.Name, ck.Name))
values = genValue(ck.Type+"_range", p, values)
values = genValueRange(ck.Type, p, values)
}
query := fmt.Sprintf("SELECT * FROM %s.%s WHERE %s ORDER BY %s", s.Keyspace.Name, t.Name, strings.Join(relations, " AND "), strings.Join(pkNames, ","))
return &Stmt{
Expand Down

0 comments on commit c458b61

Please sign in to comment.