forked from RobinUS2/presto-bloomfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra_backend.go
49 lines (41 loc) · 1.1 KB
/
cassandra_backend.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
package main
import (
"github.com/gocql/gocql"
"fmt"
"log"
)
type CassandraConf struct {
ProtoVersion int
Keyspace string
Table string
Hosts []string
Consistency string
}
type CassandraBackend struct {
session *gocql.Session
config CassandraConf
}
func (b CassandraBackend) Put(k []byte, v []byte) (bool, error) {
err := b.session.Query(fmt.Sprintf(`INSERT INTO %s (key, value) VALUES (?, ?)`, b.config.Table), k, v).Exec()
return err == nil, err;
}
func (b CassandraBackend) Get(k []byte) ([]byte, error) {
var v []byte;
err := b.session.Query(fmt.Sprintf(`SELECT value FROM %s WHERE key = ? LIMIT 1`, b.config.Table), k).Scan(&v)
return v, err
}
func newCassandraBackend(conf *Conf) IBackend {
// Connect to the cluster
cluster := gocql.NewCluster(conf.Cassandra.Hosts...)
cluster.Keyspace = conf.Cassandra.Keyspace
cluster.Consistency = gocql.ParseConsistency(conf.Cassandra.Consistency)
cluster.ProtoVersion = conf.Cassandra.ProtoVersion
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
return CassandraBackend{
session: session,
config: conf.Cassandra,
}
}