-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
121 lines (109 loc) · 3.15 KB
/
types.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"github.com/gocql/gocql"
"log"
)
var CONNECTOR *CassandraConnector
type CassandraObject interface {
Insert() error
Delete() error
}
type CassandraConnector struct {
Cluster *gocql.ClusterConfig
Session *gocql.Session
}
/*
Any Consistency = 1 + iota
One
Two
Three
Quorum
All
LocalQuorum
EachQuorum
Serial
LocalSerial
LocalOne
*/
var CONSISTENCY = [...]string{"Any", "One", "Two", "Three", "Quorum", "All", "LocalQuorum", "EachQuorum", "Serial", "LocalSerial", "LocalOne"}
func NewCassandraAuthConnector(k string, u string, p string, hosts ...string) *CassandraConnector {
cluster := gocql.NewCluster(hosts...)
cluster.Keyspace = k
cluster.Compressor = gocql.SnappyCompressor{}
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: u,
Password: p,
}
session, err := cluster.CreateSession()
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 2}
if err != nil {
log.Panicf("Cassandra Auth Connection error: %s", err)
}
return &CassandraConnector{
Cluster: cluster,
Session: session,
}
}
func NewCassandraConnector(k string, hosts ...string) *CassandraConnector {
cluster := gocql.NewCluster(hosts...)
cluster.Keyspace = k
cluster.Compressor = gocql.SnappyCompressor{}
session, err := cluster.CreateSession()
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 2}
if err != nil {
log.Panicf("Cassandra Connection error: %s", err)
}
return &CassandraConnector{
Cluster: cluster,
Session: session,
}
}
func (conn *CassandraConnector) WriteWithConsistency() {
for i, _ := range gocql.ConsistencyNames {
if gocql.Consistency(i).String() == "default" {
// Default will fall under one of the others to be tested, skip it
continue
}
log.Printf("Consistency %s", gocql.Consistency(i))
query := conn.Session.Query(`INSERT INTO data (id, data) VALUES (?, ?)`,
gocql.TimeUUID(),
"hello world")
query.Consistency(gocql.Consistency(i))
err := query.Exec()
if err != nil {
log.Printf("Error executing query! %s", err)
continue
}
lat := query.Latency()
log.Printf("Query Successfull! Time: %d ms (%d ns)", int(lat/1000000), lat)
}
}
func (conn *CassandraConnector) ReadWithConsistency() {
// First write something we know so we can read
uuid := gocql.TimeUUID()
err := conn.Session.Query(`INSERT INTO data (id, data) VALUES (?, ?)`,
uuid,
"hello world").Exec()
if err != nil {
log.Fatalln("Cannot write to cluster! Read operation aborted...", err)
}
// Something to store the results
var id gocql.UUID
var text string
for i, _ := range gocql.ConsistencyNames {
if gocql.Consistency(i).String() == "default" || gocql.Consistency(i).String() == "any" || gocql.Consistency(i).String() == "eachquorum" {
// This are only supported for writes!
continue
}
log.Printf("Consistency %s", gocql.Consistency(i))
query := conn.Session.Query(`SELECT id, data FROM data WHERE id = ? LIMIT 1`,
uuid).Consistency(gocql.Consistency(i))
err = query.Scan(&id, &text)
if err != nil {
log.Printf("Error executing query! %s", err)
continue
}
lat := query.Latency()
log.Printf("Query Successfull! Time: %d ms (%d ns)", int(lat/1000000), lat)
}
}