-
Notifications
You must be signed in to change notification settings - Fork 784
/
table_client.go
81 lines (69 loc) · 1.96 KB
/
table_client.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
package cassandra
import (
"context"
"fmt"
"github.com/gocql/gocql"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/cortexproject/cortex/pkg/chunk"
)
type tableClient struct {
cfg Config
session *gocql.Session
}
// NewTableClient returns a new TableClient.
func NewTableClient(ctx context.Context, cfg Config, registerer prometheus.Registerer) (chunk.TableClient, error) {
session, err := cfg.session("table-manager", registerer)
if err != nil {
return nil, errors.WithStack(err)
}
return &tableClient{
cfg: cfg,
session: session,
}, nil
}
func (c *tableClient) ListTables(ctx context.Context) ([]string, error) {
md, err := c.session.KeyspaceMetadata(c.cfg.Keyspace)
if err != nil {
return nil, errors.WithStack(err)
}
result := []string{}
for name := range md.Tables {
result = append(result, name)
}
return result, nil
}
func (c *tableClient) CreateTable(ctx context.Context, desc chunk.TableDesc) error {
query := c.getCreateTableQuery(&desc)
err := c.session.Query(query).WithContext(ctx).Exec()
return errors.WithStack(err)
}
func (c *tableClient) DeleteTable(ctx context.Context, name string) error {
err := c.session.Query(fmt.Sprintf(`
DROP TABLE IF EXISTS %s;`, name)).WithContext(ctx).Exec()
return errors.WithStack(err)
}
func (c *tableClient) DescribeTable(ctx context.Context, name string) (desc chunk.TableDesc, isActive bool, err error) {
return chunk.TableDesc{
Name: name,
}, true, nil
}
func (c *tableClient) UpdateTable(ctx context.Context, current, expected chunk.TableDesc) error {
return nil
}
func (c *tableClient) Stop() {
c.session.Close()
}
func (c *tableClient) getCreateTableQuery(desc *chunk.TableDesc) (query string) {
query = fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
hash text,
range blob,
value blob,
PRIMARY KEY (hash, range)
)`, desc.Name)
if c.cfg.TableOptions != "" {
query = fmt.Sprintf("%s WITH %s", query, c.cfg.TableOptions)
}
return
}