gocassa

package module
v0.0.0-...-ebff9ff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2015 License: MIT Imports: 11 Imported by: 0

README

gocassa

Gocassa is a high-level library on top of gocql.

Compared to gocql it provides query building, adds data binding, and provides easy-to-use "recipe" tables for common query use-cases. Unlike cqlc, it does not use code generation.

Table types
Raw CQL Table

The raw CQL table pretty much lets you write any CQL query. Here is an example:

package main

import(
    "fmt"
    "time"
    
    "github.com/hailocab/gocassa"
)

type Sale struct {
    Id          string
    CustomerId  string
    SellerId    string
    Price       int
    Created     time.Time
}

func main() {
    keySpace, err := gocassa.ConnectToKeySpace("test", []string{"127.0.0.1"}, "", "")
    if err != nil {
        panic(err)
    }
    salesTable := keySpace.Table("sale", Sale{}, gocassa.Keys{
        PartitionKeys: []string{"Id"},
    })
    err = salesTable.Set(Sale{
        Id: "sale-1",
        CustomerId: "customer-1",
        SellerId: "seller-1",
        Price: 42,
        Created: time.Now(),
    }).Run()
    if err != nil {
        panic(err)
    }
    result := &Sale{}
    if err := salesTable.Where(gocassa.Eq("Id", "sale-1")).Query().ReadOne(result).Run(); err != nil {
        panic(err)
    }
    fmt.Println(*result)
}
MapTable

MapTable provides only very simple CRUD functionality:

    salesTable := keySpace.MapTable("sale", "Id", Sale{})
    // …
    result := &Sale{}
    err := salesTable.Read("sale-1", result).Run()
}
MultimapTable

MultimapTable can list rows filtered by equality of a single field (eg. list sales based on their sellerId):

    salesTable := keySpace.MultimapTable("sale", "SellerId", "Id", Sale{})
    // …
    results := &[]Sale{}
    err := salesTable.List("seller-1", nil, 0, results).Run()
TimeSeriesTable

TimeSeriesTable provides an interface to list rows within a time interval:

    salesTable := keySpace.TimeSeriesTable("sale", "Created", "Id", Sale{})
    //...
    results := &[]Sale{}
    err := salesTable.List(yesterdayTime, todayTime, results).Run()
MultiTimeSeriesTable

MultiTimeSeriesTable is like a cross between MultimapTable and TimeSeriesTable. It can list rows within a time interval, and filtered by equality of a single field. The following lists sales in a time interval, by a certain seller:

    salesTable := keySpace.MultiTimeSeriesTable("sale", "SellerId", "Created", "Id", Sale{})
    //...
    results := &[]Sale{}
    err := salesTable.List("seller-1", yesterdayTime, todayTime, results).Run()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	CreateKeySpace(name string) error
	DropKeySpace(name string) error
	KeySpace(name string) KeySpace
}

The Connection interface only exists because one can not connect to a keyspace if it does not exist, thus having a Create on KeySpace is not possible. Use ConnectToKeySpace to acquire an instance of KeySpace.

func Connect

func Connect(nodeIps []string, username, password string) (Connection, error)

Convenience method to connect to

func NewConnection

func NewConnection(q QueryExecutor) Connection

type Filter

type Filter interface {
	// Selection modifiers
	Query() Query
	// Partial update.
	Update(m map[string]interface{}) Op // Probably this is danger zone (can't be implemented efficiently) on a selectuinb with more than 1 document
	UpdateWithOptions(m map[string]interface{}, opts Options) Op
	Delete() Op
}

A Filter is a subset of a Table, filtered by Relations. You can do writes or reads on a filter.

type KeySpace

type KeySpace interface {
	MapTable(tableName, id string, row interface{}) MapTable
	MultimapTable(tableName, fieldToIndexBy, uniqueKey string, row interface{}) MultimapTable
	TimeSeriesTable(tableName, timeField, uniqueKey string, bucketSize time.Duration, row interface{}) TimeSeriesTable
	MultiTimeSeriesTable(tableName, fieldToIndexByField, timeField, uniqueKey string, bucketSize time.Duration, row interface{}) MultiTimeSeriesTable
	Table(tableName string, row interface{}, keys Keys) Table
	DebugMode(bool)
}

func ConnectToKeySpace

func ConnectToKeySpace(keySpace string, nodeIps []string, username, password string) (KeySpace, error)

type Keys

type Keys struct {
	PartitionKeys     []string
	ClusteringColumns []string
}

type MapTable

type MapTable interface {
	SetWithOptions(v interface{}, opts Options) Op
	Set(v interface{}) Op
	UpdateWithOptions(id interface{}, m map[string]interface{}, opts Options) Op
	Update(id interface{}, m map[string]interface{}) Op
	Delete(id interface{}) Op
	Read(id, pointer interface{}) Op
	MultiRead(ids []interface{}, pointerToASlice interface{}) Op
	TableChanger
}

type MultiTimeSeriesTable

type MultiTimeSeriesTable interface {
	// timeField and idField must be present
	SetWithOptions(v interface{}, opts Options) Op
	Set(v interface{}) Op
	UpdateWithOptions(v interface{}, timeStamp time.Time, id interface{}, m map[string]interface{}, opts Options) Op
	Update(v interface{}, timeStamp time.Time, id interface{}, m map[string]interface{}) Op
	Delete(v interface{}, timeStamp time.Time, id interface{}) Op
	Read(v interface{}, timeStamp time.Time, id, pointer interface{}) Op
	List(v interface{}, start, end time.Time, pointerToASlice interface{}) Op
	TableChanger
}

MultiTimeSeries is a cross between TimeSeries and Multimap tables.

type MultimapTable

type MultimapTable interface {
	SetWithOptions(v interface{}, opts Options) Op
	Set(v interface{}) Op
	UpdateWithOptions(v, id interface{}, m map[string]interface{}, opts Options) Op
	Update(v, id interface{}, m map[string]interface{}) Op
	Delete(v, id interface{}) Op
	DeleteAll(v interface{}) Op
	List(v, startId interface{}, limit int, pointerToASlice interface{}) Op
	Read(v, id, pointer interface{}) Op
	MultiRead(v interface{}, ids []interface{}, pointerToASlice interface{}) Op
	TableChanger
}

Multimap lets you list rows based on a field equality, eg. 'list all sales where seller id = v'.

type Op

type Op interface {
	Run() error
	// You do not need this in 95% of the use cases, use Run!
	// Using atmoic batched writes (logged batches in Cassandra terminolohu) comes at a high performance cost!
	RunAtomically() error
	Add(...Op) Op
}

type Options

type Options struct {
	// TTL specifies a duration over which data is valid. It will be truncated to second precision upon statement
	// execution.
	TTL time.Duration
}

Options allows specification of (optional, hah) query parameters.

type Query

type Query interface {
	Read(pointerToASlice interface{}) Op
	ReadOne(pointer interface{}) Op
	Limit(int) Query
}

A Query is a subset of a Table intended to be read

type QueryExecutor

type QueryExecutor interface {
	Query(stmt string, params ...interface{}) ([]map[string]interface{}, error)
	Execute(stmt string, params ...interface{}) error
	ExecuteAtomically(stmt []string, params [][]interface{}) error
}

type Relation

type Relation struct {
	// contains filtered or unexported fields
}

func Eq

func Eq(key string, term interface{}) Relation

func GT

func GT(key string, term interface{}) Relation

func GTE

func GTE(key string, term interface{}) Relation

func In

func In(key string, terms ...interface{}) Relation

func LT

func LT(key string, term interface{}) Relation

func LTE

func LTE(key string, term interface{}) Relation

type RowNotFoundError

type RowNotFoundError struct {
	// contains filtered or unexported fields
}

func (RowNotFoundError) Error

func (r RowNotFoundError) Error() string

type Table

type Table interface {
	// Set Inserts, or Replaces your row with the supplied struct. Be aware that what is not in your struct
	// will be deleted. To only overwrite some of the fields, use Query.Update.
	Set(v interface{}) Op
	SetWithOptions(v interface{}, opts Options) Op
	Where(relations ...Relation) Filter // Because we provide selections
	// Name returns the underlying table name, as stored in C*
	Name() string
	TableChanger
}

type TableChanger

type TableChanger interface {
	Create() error
	CreateStatement() (string, error)
	Recreate() error
}

Danger zone! Do not use this interface unless you really know what you are doing

type TimeSeriesTable

type TimeSeriesTable interface {
	// timeField and idField must be present
	SetWithOptions(v interface{}, opts Options) Op
	Set(v interface{}) Op
	UpdateWithOptions(timeStamp time.Time, id interface{}, m map[string]interface{}, opts Options) Op
	Update(timeStamp time.Time, id interface{}, m map[string]interface{}) Op
	Delete(timeStamp time.Time, id interface{}) Op
	Read(timeStamp time.Time, id, pointer interface{}) Op
	List(start, end time.Time, pointerToASlice interface{}) Op
	TableChanger
}

TimeSeries lets you list rows which have a field value between two date ranges.

Directories

Path Synopsis
This package generates all kind of CQL statements
This package generates all kind of CQL statements
This package provides some punk-rock reflection which is not in the stdlib.
This package provides some punk-rock reflection which is not in the stdlib.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL