-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcassandra_service.go
64 lines (55 loc) · 1.97 KB
/
cassandra_service.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
package main
import (
"fmt"
"github.com/gocql/gocql"
)
type CassandraService struct {
Client *gocql.Session
}
// TODO: Add auth for cassandra
func NewCassandraSession() *CassandraService {
cluster := gocql.NewCluster(Config.Cassandra.Hosts)
cluster.ProtoVersion = Config.Cassandra.ProtoVersion
q := fmt.Sprintf("create keyspace if not exists %s_%s with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", Config.Cassandra.Keyspace, GoEnv)
session, err := cluster.CreateSession()
err = session.Query(q).Exec()
session.Close()
cluster.Keyspace = fmt.Sprintf("%s_%s", Config.Cassandra.Keyspace, GoEnv)
cluster.Consistency = gocql.Quorum
session, err = cluster.CreateSession()
perror(initializeCassandra(session))
perror(err)
logger.Log(kv{"fn": "cassandra_service", "msg": fmt.Sprintf("Connecting to host '%s'\n", Config.Cassandra.Hosts)})
logger.Log(kv{"fn": "cassandra_service", "msg": fmt.Sprintf("Cassandra.namespace '%s_%s'\n", Config.Cassandra.Keyspace, GoEnv)})
return &CassandraService{Client: session}
}
func initializeCassandra(session *gocql.Session) error {
// projects table
q := fmt.Sprintf("create table if not exists projects (name text PRIMARY KEY, oids SET<text>);")
err := session.Query(q).Exec()
if err != nil {
return err
}
// create an index so we can search on oids
q = fmt.Sprintf("create index if not exists on projects(oids);")
err = session.Query(q).Exec()
if err != nil {
return err
}
// Oids table
q = fmt.Sprintf("create table if not exists oids(oid text primary key, size bigint);")
session.Query(q).Exec()
if err != nil {
return err
}
// user management
q = fmt.Sprintf("create table if not exists users(username text primary key, password text);")
return session.Query(q).Exec()
}
func DropCassandra(session *gocql.Session) error {
config := Config.Cassandra
m := fmt.Sprintf("%s_%s", config.Keyspace, GoEnv)
q := fmt.Sprintf("drop keyspace %s;", m)
c := NewCassandraSession().Client
return c.Query(q).Exec()
}