github.com/sequix/cortex@v1.1.6/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  
    10  	"github.com/sequix/cortex/pkg/chunk"
    11  )
    12  
    13  type tableClient struct {
    14  	cfg     Config
    15  	session *gocql.Session
    16  }
    17  
    18  // NewTableClient returns a new TableClient.
    19  func NewTableClient(ctx context.Context, cfg Config) (chunk.TableClient, error) {
    20  	session, err := cfg.session()
    21  	if err != nil {
    22  		return nil, errors.WithStack(err)
    23  	}
    24  	return &tableClient{
    25  		cfg:     cfg,
    26  		session: session,
    27  	}, nil
    28  }
    29  
    30  func (c *tableClient) ListTables(ctx context.Context) ([]string, error) {
    31  	md, err := c.session.KeyspaceMetadata(c.cfg.Keyspace)
    32  	if err != nil {
    33  		return nil, errors.WithStack(err)
    34  	}
    35  	result := []string{}
    36  	for name := range md.Tables {
    37  		result = append(result, name)
    38  	}
    39  	return result, nil
    40  }
    41  
    42  func (c *tableClient) CreateTable(ctx context.Context, desc chunk.TableDesc) error {
    43  	err := c.session.Query(fmt.Sprintf(`
    44  		CREATE TABLE IF NOT EXISTS %s (
    45  			hash text,
    46  			range blob,
    47  			value blob,
    48  			PRIMARY KEY (hash, range)
    49  		)`, desc.Name)).WithContext(ctx).Exec()
    50  	return errors.WithStack(err)
    51  }
    52  
    53  func (c *tableClient) DeleteTable(ctx context.Context, name string) error {
    54  	err := c.session.Query(fmt.Sprintf(`
    55  		DROP TABLE IF EXISTS %s;`, name)).WithContext(ctx).Exec()
    56  	return errors.WithStack(err)
    57  }
    58  
    59  func (c *tableClient) DescribeTable(ctx context.Context, name string) (desc chunk.TableDesc, isActive bool, err error) {
    60  	return chunk.TableDesc{
    61  		Name: name,
    62  	}, true, nil
    63  }
    64  
    65  func (c *tableClient) UpdateTable(ctx context.Context, current, expected chunk.TableDesc) error {
    66  	return nil
    67  }