-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_options.go
127 lines (114 loc) · 3.79 KB
/
bucket_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
124
125
126
127
// Copyright 2017 Cameron Bergoon
// Licensed under the LGPLv3, see LICENCE file for details.
package stitchdb
import (
"strconv"
"github.com/juju/errors"
)
//BucketOptions holds bucket metadata.
type BucketOptions struct {
system bool //Indicates that this bucket is the system bucket.
btdeg int //Dergee of the B-Tree; used to optimize performance based on use case.
geo bool //Indicates if the bucket is geo enabled or not.
georincl bool //Indicates if the range of radius searches are inclusive or exclusive.
time bool //Indicates if the bucket is time series enabled. Todo: Implement
dims int //Number of dimensions the geo functionality will utilize.
}
//System sets the system option.
func System(b *BucketOptions) error {
b.system = true
return nil
}
//Geo enables geo-location functionality.
func Geo(b *BucketOptions) error {
b.geo = true
return nil
}
//GeoRangeIsInclusive enables inclusive range checks.
func GeoRangeIsInclusive(b *BucketOptions) error {
b.georincl = true
return nil
}
//Time enable the time series functionality.
func Time(b *BucketOptions) error {
b.time = true
return nil
}
//Dims sets the number of dimensions that will be utilized.
func Dims(dims int) func(*BucketOptions) error {
return func(b *BucketOptions) error {
b.dims = dims
return nil
}
}
//BTreeDegree sets the degree of the trees ued for the bucket.
func BTreeDegree(degree int) func(*BucketOptions) error {
return func(b *BucketOptions) error {
b.btdeg = degree
return nil
}
}
//NewBucketOptions creates a new bucket options using the provided option modifiers.
func NewBucketOptions(options ...func(*BucketOptions) error) (*BucketOptions, error) {
c := &BucketOptions{}
for _, option := range options {
err := option(c)
if err != nil {
return nil, errors.New("error: bucket_options: could not create bucket options")
}
}
return c, nil
}
//bucketOptionsCreateStmt returns the statement that represents the bucket options.
func (b *BucketOptions) bucketOptionsCreateStmt() []byte {
var cbuf []byte
cbuf = append(cbuf, strconv.Itoa(b.btdeg)...)
cbuf = append(cbuf, ':')
cbuf = append(cbuf, strconv.Itoa(boolToInt(b.system))...)
cbuf = append(cbuf, ':')
cbuf = append(cbuf, strconv.Itoa(boolToInt(b.geo))...)
cbuf = append(cbuf, ':')
cbuf = append(cbuf, strconv.Itoa(boolToInt(b.georincl))...)
cbuf = append(cbuf, ':')
cbuf = append(cbuf, strconv.Itoa(boolToInt(b.time))...)
cbuf = append(cbuf, ':')
cbuf = append(cbuf, strconv.Itoa(b.dims)...)
return cbuf
}
//NewBucketOptionsFromStmt returns bucket options representing the options portion of the statement. Returns an error if the
//bucket statement could not be parsed.
func NewBucketOptionsFromStmt(stmt []string) (*BucketOptions, error) {
btdeg, err := strconv.ParseInt(stmt[1], 10, 64)
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
system, err := strconv.ParseBool(stmt[2])
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
geo, err := strconv.ParseBool(stmt[3])
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
georincl, err := strconv.ParseBool(stmt[4])
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
time, err := strconv.ParseBool(stmt[5])
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
dims, err := strconv.ParseInt(stmt[6], 10, 64)
if err != nil {
return nil, errors.Annotate(err, "error: bucket_optiona: failed to parse bucket options")
}
opts := &BucketOptions{
btdeg: int(btdeg),
system: system,
geo: geo,
georincl: georincl,
time: time,
dims: int(dims),
}
return opts, nil
}