forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creator.go
99 lines (90 loc) · 2.51 KB
/
creator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cassandra
import (
"fmt"
"github.com/gocql/gocql"
"log"
"strings"
"time"
)
// Map of user specified strings to gocql consistency settings
var consistencyMapping = map[string]gocql.Consistency{
"ALL": gocql.All,
"ANY": gocql.Any,
"QUORUM": gocql.Quorum,
"ONE": gocql.One,
"TWO": gocql.Two,
"THREE": gocql.Three,
}
type dbCreator struct {
globalSession *gocql.Session
clientSession *gocql.Session
consistencyLevel string
hosts string
replicationFactor int
writeTimeout time.Duration
}
func (d *dbCreator) Init() {
cluster := gocql.NewCluster(strings.Split(d.hosts, ",")...)
cluster.Consistency = consistencyMapping[d.consistencyLevel]
cluster.ProtoVersion = 4
cluster.Timeout = 10 * time.Second
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
d.globalSession = session
}
func (d *dbCreator) DBExists(dbName string) bool {
iter := d.globalSession.Query(fmt.Sprintf("SELECT keyspace_name FROM system_schema.keyspaces;")).Iter()
defer iter.Close()
row := ""
for iter.Scan(&row) {
if row == dbName {
return true
}
}
return false
}
func (d *dbCreator) RemoveOldDB(dbName string) error {
if err := d.globalSession.Query(fmt.Sprintf("drop keyspace if exists %s;", dbName)).Exec(); err != nil {
return err
}
return nil
}
func (d *dbCreator) CreateDB(dbName string) error {
defer d.globalSession.Close()
replicationConfiguration := fmt.Sprintf("{ 'class': 'SimpleStrategy', 'replication_factor': %d }", d.replicationFactor)
if err := d.globalSession.Query(fmt.Sprintf("create keyspace %s with replication = %s;", dbName, replicationConfiguration)).Exec(); err != nil {
return err
}
for _, cassandraTypename := range []string{"bigint", "float", "double", "boolean", "blob"} {
q := fmt.Sprintf(`CREATE TABLE %s.series_%s (
series_id text,
timestamp_ns bigint,
value %s,
PRIMARY KEY (series_id, timestamp_ns)
)
WITH COMPACT STORAGE;`,
dbName, cassandraTypename, cassandraTypename)
if err := d.globalSession.Query(q).Exec(); err != nil {
return err
}
}
return nil
}
func (d *dbCreator) PostCreateDB(dbName string) error {
cluster := gocql.NewCluster(strings.Split(d.hosts, ",")...)
cluster.Keyspace = dbName
cluster.Timeout = d.writeTimeout
cluster.Consistency = consistencyMapping[d.consistencyLevel]
cluster.ProtoVersion = 4
session, err := cluster.CreateSession()
if err != nil {
return err
}
d.clientSession = session
return nil
}
func (d *dbCreator) Close() {
d.clientSession.Close()
}