Skip to content

Commit

Permalink
Table and statement level options
Browse files Browse the repository at this point in the history
  • Loading branch information
crufter committed Mar 30, 2015
1 parent 1fc20d5 commit fd18588
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 35 deletions.
5 changes: 5 additions & 0 deletions interfaces.go
Expand Up @@ -43,6 +43,7 @@ type MapTable interface {
Delete(id interface{}) Op
Read(id, pointer interface{}) Op
MultiRead(ids []interface{}, pointerToASlice interface{}) Op
WithOptions(Options) MapTable
TableChanger
}

Expand All @@ -61,6 +62,7 @@ type MultimapTable interface {
List(v, startId interface{}, limit int, pointerToASlice interface{}) Op
Read(v, id, pointer interface{}) Op
MultiRead(v interface{}, ids []interface{}, pointerToASlice interface{}) Op
WithOptions(Options) MultimapTable
TableChanger
}

Expand All @@ -78,6 +80,7 @@ type TimeSeriesTable interface {
Delete(timeStamp time.Time, id interface{}) Op
Read(timeStamp time.Time, id, pointer interface{}) Op
List(start, end time.Time, pointerToASlice interface{}) Op
WithOptions(Options) TimeSeriesTable
TableChanger
}

Expand All @@ -95,6 +98,7 @@ type MultiTimeSeriesTable interface {
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
WithOptions(Options) MultiTimeSeriesTable
TableChanger
}

Expand Down Expand Up @@ -171,6 +175,7 @@ type Table interface {
// Where accepts a bunch of realtions and returns a filter. See the documentation for Relation and Filter to understand what that means.
Where(relations ...Relation) Filter // Because we provide selections
// Name returns the underlying table name, as stored in C*
WithOptions(Options) Table
TableChanger
}

Expand Down
1 change: 1 addition & 0 deletions keyspace.go
Expand Up @@ -43,6 +43,7 @@ func (k *k) table(name string, entity interface{}, fieldSource map[string]interf
return &t{
keySpace: k,
info: ti,
options: &Options{},
}
}

Expand Down
27 changes: 17 additions & 10 deletions map_table.go
Expand Up @@ -5,22 +5,29 @@ type mapT struct {
idField string
}

func (o *mapT) Update(id interface{}, m map[string]interface{}) Op {
return o.Where(Eq(o.idField, id)).Update(m)
func (m *mapT) Update(id interface{}, ma map[string]interface{}) Op {
return m.Where(Eq(m.idField, id)).Update(ma)
}

func (o *mapT) UpdateWithOptions(id interface{}, m map[string]interface{}, opts Options) Op {
return o.Where(Eq(o.idField, id)).UpdateWithOptions(m, opts)
func (m *mapT) UpdateWithOptions(id interface{}, ma map[string]interface{}, opts Options) Op {
return m.Where(Eq(m.idField, id)).UpdateWithOptions(ma, opts)
}

func (o *mapT) Delete(id interface{}) Op {
return o.Where(Eq(o.idField, id)).Delete()
func (m *mapT) Delete(id interface{}) Op {
return m.Where(Eq(m.idField, id)).Delete()
}

func (o *mapT) Read(id, pointer interface{}) Op {
return o.Where(Eq(o.idField, id)).Query().ReadOne(pointer)
func (m *mapT) Read(id, pointer interface{}) Op {
return m.Where(Eq(m.idField, id)).Query().ReadOne(pointer)
}

func (o *mapT) MultiRead(ids []interface{}, pointerToASlice interface{}) Op {
return o.Where(In(o.idField, ids...)).Query().Read(pointerToASlice)
func (m *mapT) MultiRead(ids []interface{}, pointerToASlice interface{}) Op {
return m.Where(In(m.idField, ids...)).Query().Read(pointerToASlice)
}

func (m *mapT) WithOptions(o Options) MapTable {
return &mapT{
t: m.t.WithOptions(o).(*t),
idField: m.idField,
}
}
4 changes: 2 additions & 2 deletions modifiers.go
Expand Up @@ -50,7 +50,7 @@ func ListSetAtIndex(index int, value interface{}) Modifier {
}
}

// ListRemove removes all elements from a list having a particular value
// ListRemove removes all elements from a list having a particular value
func ListRemove(value interface{}) Modifier {
return Modifier{
op: modifierListRemove,
Expand Down Expand Up @@ -133,7 +133,7 @@ func (m Modifier) cql(name string) (string, []interface{}) {
if val > 0 {
str = fmt.Sprintf("%v = %v + %v", name, name, printElem(val))
} else {
str = fmt.Sprintf("%v = %v - %v", name, name, printElem(val * -1))
str = fmt.Sprintf("%v = %v - %v", name, name, printElem(val*-1))
}
}
return str, vals
Expand Down
40 changes: 24 additions & 16 deletions multimap_table.go
Expand Up @@ -6,34 +6,42 @@ type multimapT struct {
idField string
}

func (o *multimapT) Update(field, id interface{}, m map[string]interface{}) Op {
return o.Where(Eq(o.fieldToIndexBy, field), Eq(o.idField, id)).Update(m)
func (mm *multimapT) Update(field, id interface{}, m map[string]interface{}) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field), Eq(mm.idField, id)).Update(m)
}

func (o *multimapT) UpdateWithOptions(field, id interface{}, m map[string]interface{}, opts Options) Op {
return o.Where(Eq(o.fieldToIndexBy, field), Eq(o.idField, id)).UpdateWithOptions(m, opts)
func (mm *multimapT) UpdateWithOptions(field, id interface{}, m map[string]interface{}, opts Options) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field), Eq(mm.idField, id)).UpdateWithOptions(m, opts)
}

func (o *multimapT) Delete(field, id interface{}) Op {
return o.Where(Eq(o.fieldToIndexBy, field), Eq(o.idField, id)).Delete()
func (mm *multimapT) Delete(field, id interface{}) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field), Eq(mm.idField, id)).Delete()
}

func (o *multimapT) DeleteAll(field interface{}) Op {
return o.Where(Eq(o.fieldToIndexBy, field)).Delete()
func (mm *multimapT) DeleteAll(field interface{}) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field)).Delete()
}

func (o *multimapT) Read(field, id, pointer interface{}) Op {
return o.Where(Eq(o.fieldToIndexBy, field), Eq(o.idField, id)).Query().ReadOne(pointer)
func (mm *multimapT) Read(field, id, pointer interface{}) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field), Eq(mm.idField, id)).Query().ReadOne(pointer)
}

func (o *multimapT) MultiRead(field interface{}, ids []interface{}, pointerToASlice interface{}) Op {
return o.Where(Eq(o.fieldToIndexBy, field), In(o.idField, ids...)).Query().Read(pointerToASlice)
func (mm *multimapT) MultiRead(field interface{}, ids []interface{}, pointerToASlice interface{}) Op {
return mm.Where(Eq(mm.fieldToIndexBy, field), In(mm.idField, ids...)).Query().Read(pointerToASlice)
}

func (o *multimapT) List(field, startId interface{}, limit int, pointerToASlice interface{}) Op {
rels := []Relation{Eq(o.fieldToIndexBy, field)}
func (mm *multimapT) List(field, startId interface{}, limit int, pointerToASlice interface{}) Op {
rels := []Relation{Eq(mm.fieldToIndexBy, field)}
if startId != nil {
rels = append(rels, GTE(o.idField, startId))
rels = append(rels, GTE(mm.idField, startId))
}
return mm.Where(rels...).Query().Limit(limit).Read(pointerToASlice)
}

func (mm *multimapT) WithOptions(o Options) MultimapTable {
return &multimapT{
t: mm.t.WithOptions(o).(*t),
fieldToIndexBy: mm.fieldToIndexBy,
idField: mm.idField,
}
return o.Where(rels...).Query().Limit(limit).Read(pointerToASlice)
}
9 changes: 9 additions & 0 deletions multitimeseries_table.go
Expand Up @@ -65,3 +65,12 @@ func (o *multiTimeSeriesT) List(v interface{}, startTime time.Time, endTime time
}
return o.Where(Eq(o.indexField, v), In(bucketFieldName, buckets...), GTE(o.timeField, startTime), LTE(o.timeField, endTime)).Query().Read(pointerToASlice)
}

func (o *multiTimeSeriesT) WithOptions(opt Options) MultiTimeSeriesTable {
return &multiTimeSeriesT{
t: o.t.WithOptions(opt).(*t),
indexField: o.indexField,
idField: o.idField,
bucketSize: o.bucketSize,
}
}
25 changes: 24 additions & 1 deletion options.go
Expand Up @@ -8,5 +8,28 @@ import (
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
TTL time.Duration
Limit int
}

func TTL(t time.Duration) Options {
return Options{
TTL: t,
}
}

func (o Options) SetTTL(t time.Duration) Options {
o.TTL = t
return o
}

func Limit(i int) Options {
return Options{
Limit: i,
}
}

func (o Options) SetLimit(i int) Options {
o.Limit = i
return o
}
10 changes: 5 additions & 5 deletions query.go
Expand Up @@ -5,12 +5,12 @@ import (
)

type query struct {
f filter
limit int
f filter
options Options
}

func (q *query) Limit(i int) Query {
q.limit = i
q.options = q.options.SetLimit(i)
return q
}

Expand Down Expand Up @@ -74,8 +74,8 @@ func (q *query) generateOrderBy() (string, []interface{}) {
}

func (q *query) generateLimit() (string, []interface{}) {
if q.limit < 1 {
if q.options.Limit < 1 {
return "", []interface{}{}
}
return "LIMIT ?", []interface{}{q.limit}
return "LIMIT ?", []interface{}{q.options.Limit}
}
11 changes: 10 additions & 1 deletion table.go
Expand Up @@ -13,6 +13,7 @@ import (
type t struct {
keySpace *k
info *tableInfo
options *Options
}

// Contains mostly analyzed information about the entity
Expand Down Expand Up @@ -176,7 +177,7 @@ func (t t) SetWithOptions(i interface{}, opts Options) Op {
}

func (t t) Set(row interface{}) Op {
return t.SetWithOptions(row, Options{})
return t.SetWithOptions(row, *t.options)
}

func (t t) Create() error {
Expand Down Expand Up @@ -210,3 +211,11 @@ func (t t) CreateStatement() (string, error) {
func (t t) Name() string {
return t.info.name
}

func (table t) WithOptions(o Options) Table {
return t{
keySpace: table.keySpace,
info: table.info,
options: &o,
}
}
9 changes: 9 additions & 0 deletions timeseries_table.go
Expand Up @@ -65,3 +65,12 @@ func (o *timeSeriesT) List(startTime time.Time, endTime time.Time, pointerToASli
}
return o.Where(In(bucketFieldName, buckets...), GTE(o.timeField, startTime), LTE(o.timeField, endTime)).Query().Read(pointerToASlice)
}

func (o *timeSeriesT) WithOptions(opt Options) TimeSeriesTable {
return &timeSeriesT{
t: o.t.WithOptions(opt).(*t),
timeField: o.timeField,
idField: o.idField,
bucketSize: o.bucketSize,
}
}

0 comments on commit fd18588

Please sign in to comment.