forked from tsuna/gohbase
/
query.go
153 lines (140 loc) · 4.53 KB
/
query.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2017 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package hrpc
import (
"errors"
"math"
"time"
"github.com/glados28/gohbase/filter"
"github.com/glados28/gohbase/pb"
)
// baseQuery bundles common fields that can be provided for quering requests: Scans and Gets
type baseQuery struct {
families map[string][]string
filter *pb.Filter
fromTimestamp uint64
toTimestamp uint64
maxVersions uint32
storeLimit uint32
storeOffset uint32
}
// newBaseQuery return baseQuery with all default values
func newBaseQuery() baseQuery {
return baseQuery{
storeLimit: DefaultMaxResultsPerColumnFamily,
fromTimestamp: MinTimestamp,
toTimestamp: MaxTimestamp,
maxVersions: DefaultMaxVersions,
}
}
func (bq *baseQuery) setFamilies(families map[string][]string) {
bq.families = families
}
func (bq *baseQuery) setFilter(filter *pb.Filter) {
bq.filter = filter
}
func (bq *baseQuery) setTimeRangeUint64(from, to uint64) {
bq.fromTimestamp = from
bq.toTimestamp = to
}
func (bq *baseQuery) setMaxVersions(versions uint32) {
bq.maxVersions = versions
}
func (bq *baseQuery) setMaxResultsPerColumnFamily(maxresults uint32) {
bq.storeLimit = maxresults
}
func (bq *baseQuery) setResultOffset(offset uint32) {
bq.storeOffset = offset
}
// Families option adds families constraint to a Scan or Get request.
func Families(f map[string][]string) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
c.setFamilies(f)
return nil
}
return errors.New("'Families' option can only be used with Get or Scan request")
}
}
// Filters option adds filters constraint to a Scan or Get request.
func Filters(f filter.Filter) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
pbF, err := f.ConstructPBFilter()
if err != nil {
return err
}
c.setFilter(pbF)
return nil
}
return errors.New("'Filters' option can only be used with Get or Scan request")
}
}
// TimeRange is used as a parameter for request creation. Adds TimeRange constraint to a request.
// It will get values in range [from, to[ ('to' is exclusive).
func TimeRange(from, to time.Time) func(Call) error {
return TimeRangeUint64(uint64(from.UnixNano()/1e6), uint64(to.UnixNano()/1e6))
}
// TimeRangeUint64 is used as a parameter for request creation.
// Adds TimeRange constraint to a request.
// from and to should be in milliseconds
// // It will get values in range [from, to[ ('to' is exclusive).
func TimeRangeUint64(from, to uint64) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
if from >= to {
// or equal is becuase 'to' is exclusive
return errors.New("'from' timestamp is greater or equal to 'to' timestamp")
}
c.setTimeRangeUint64(from, to)
return nil
}
return errors.New("'TimeRange' option can only be used with Get or Scan request")
}
}
// MaxVersions is used as a parameter for request creation.
// Adds MaxVersions constraint to a request.
func MaxVersions(versions uint32) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
if versions > math.MaxInt32 {
return errors.New("'MaxVersions' exceeds supported number of versions")
}
c.setMaxVersions(versions)
return nil
}
return errors.New("'MaxVersions' option can only be used with Get or Scan request")
}
}
// MaxResultsPerColumnFamily is an option for Get or Scan requests that sets the maximum
// number of cells returned per column family in a row.
func MaxResultsPerColumnFamily(maxresults uint32) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
if maxresults > math.MaxInt32 {
return errors.New(
"'MaxResultsPerColumnFamily' exceeds supported number of value results")
}
c.setMaxResultsPerColumnFamily(maxresults)
return nil
}
return errors.New(
"'MaxResultsPerColumnFamily' option can only be used with Get or Scan request")
}
}
// ResultOffset is a option for Scan or Get requests that sets the offset for cells
// within a column family.
func ResultOffset(offset uint32) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
if offset > math.MaxInt32 {
return errors.New("'ResultOffset' exceeds supported offset value")
}
c.setResultOffset(offset)
return nil
}
return errors.New("'ResultOffset' option can only be used with Get or Scan request")
}
}