This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from gocassa/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.go
123 lines (111 loc) · 3.42 KB
/
options.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gocassa
import (
"context"
"time"
"github.com/gocql/gocql"
)
type ColumnDirection bool
const (
ASC ColumnDirection = false
DESC = true
)
func (d ColumnDirection) String() string {
switch d {
case ASC:
return "ASC"
case DESC:
return "DESC"
default:
return ""
}
}
// ClusteringOrderColumn specifies a clustering column and whether its
// clustering order is ASC or DESC.
type ClusteringOrderColumn struct {
Direction ColumnDirection
Column string
}
func (c ClusteringOrderColumn) Field() string {
return c.Column
}
// Options can contain table or statement specific options.
// The reason for this is because statement specific (TTL, Limit) options make sense as table level options
// (eg. have default TTL for every Update without specifying it all the time)
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
// Limit query result set
Limit int
// TableName overrides the default internal table name. When naming a table 'users' the internal table name becomes 'users_someTableSpecificMetaInformation'.
TableName string
// ClusteringOrder specifies the clustering order during table creation. If empty, it is omitted and the defaults are used.
ClusteringOrder []ClusteringOrderColumn
// Indicates if allow filtering should be appended at the end of the query
AllowFiltering bool
// Select allows you to do partial reads, ie. retrieve only a subset of fields
Select []string
// Consistency specifies the consistency level. If nil, it is considered not set
Consistency *gocql.Consistency
// Setting CompactStorage to true enables table creation with compact storage
CompactStorage bool
// Compressor specifies the compressor (if any) to use on a newly created table
Compressor string
// Context allows a request context to passed, which is propagated to the QueryExecutor
Context context.Context
}
// Merge returns a new Options which is a right biased merge of the two initial Options.
func (o Options) Merge(neu Options) Options {
ret := Options{
TTL: o.TTL,
Limit: o.Limit,
TableName: o.TableName,
ClusteringOrder: o.ClusteringOrder,
Select: o.Select,
CompactStorage: o.CompactStorage,
Compressor: o.Compressor,
Context: o.Context,
}
if neu.TTL != time.Duration(0) {
ret.TTL = neu.TTL
}
if neu.Limit != 0 {
ret.Limit = neu.Limit
}
if len(neu.TableName) > 0 {
ret.TableName = neu.TableName
}
if neu.ClusteringOrder != nil {
ret.ClusteringOrder = neu.ClusteringOrder
}
if neu.AllowFiltering {
ret.AllowFiltering = neu.AllowFiltering
}
if len(neu.Select) > 0 {
ret.Select = neu.Select
}
if neu.Consistency != nil {
ret.Consistency = neu.Consistency
}
if neu.CompactStorage {
ret.CompactStorage = neu.CompactStorage
}
if len(neu.Compressor) > 0 {
ret.Compressor = neu.Compressor
}
// Take the latest context added, so it can be overridden
if neu.Context != nil {
ret.Context = neu.Context
}
return ret
}
// AppendClusteringOrder adds a clustering order. If there already clustering orders, the new one is added to the end.
func (o Options) AppendClusteringOrder(column string, direction ColumnDirection) Options {
col := ClusteringOrderColumn{
Column: column,
Direction: direction,
}
co := append(o.ClusteringOrder, col)
withOrder := Options{ClusteringOrder: co}
return o.Merge(withOrder)
}