github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/chunk/cassandra/table_client.go (about) 1 package cassandra 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/gocql/gocql" 8 "github.com/pkg/errors" 9 "github.com/prometheus/client_golang/prometheus" 10 11 "github.com/cortexproject/cortex/pkg/chunk" 12 ) 13 14 type tableClient struct { 15 cfg Config 16 session *gocql.Session 17 } 18 19 // NewTableClient returns a new TableClient. 20 func NewTableClient(ctx context.Context, cfg Config, registerer prometheus.Registerer) (chunk.TableClient, error) { 21 session, err := cfg.session("table-manager", registerer) 22 if err != nil { 23 return nil, errors.WithStack(err) 24 } 25 return &tableClient{ 26 cfg: cfg, 27 session: session, 28 }, nil 29 } 30 31 func (c *tableClient) ListTables(ctx context.Context) ([]string, error) { 32 md, err := c.session.KeyspaceMetadata(c.cfg.Keyspace) 33 if err != nil { 34 return nil, errors.WithStack(err) 35 } 36 result := []string{} 37 for name := range md.Tables { 38 result = append(result, name) 39 } 40 return result, nil 41 } 42 43 func (c *tableClient) CreateTable(ctx context.Context, desc chunk.TableDesc) error { 44 query := c.getCreateTableQuery(&desc) 45 err := c.session.Query(query).WithContext(ctx).Exec() 46 return errors.WithStack(err) 47 } 48 49 func (c *tableClient) DeleteTable(ctx context.Context, name string) error { 50 err := c.session.Query(fmt.Sprintf(` 51 DROP TABLE IF EXISTS %s;`, name)).WithContext(ctx).Exec() 52 return errors.WithStack(err) 53 } 54 55 func (c *tableClient) DescribeTable(ctx context.Context, name string) (desc chunk.TableDesc, isActive bool, err error) { 56 return chunk.TableDesc{ 57 Name: name, 58 }, true, nil 59 } 60 61 func (c *tableClient) UpdateTable(ctx context.Context, current, expected chunk.TableDesc) error { 62 return nil 63 } 64 65 func (c *tableClient) Stop() { 66 c.session.Close() 67 } 68 69 func (c *tableClient) getCreateTableQuery(desc *chunk.TableDesc) (query string) { 70 query = fmt.Sprintf(` 71 CREATE TABLE IF NOT EXISTS %s ( 72 hash text, 73 range blob, 74 value blob, 75 PRIMARY KEY (hash, range) 76 )`, desc.Name) 77 if c.cfg.TableOptions != "" { 78 query = fmt.Sprintf("%s WITH %s", query, c.cfg.TableOptions) 79 } 80 return 81 }