-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathfilter.go
86 lines (73 loc) · 2.33 KB
/
filter.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
// Copyright 2013-2015 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
// Filter specifies a query filter definition.
type Filter struct {
name string
begin Value
end Value
}
// NewEqualFilter creates a new equality filter instance for query.
func NewEqualFilter(binName string, value interface{}) *Filter {
val := NewValue(value)
return newFilter(binName, val, val)
}
// NewRangeFilter creates a range filter for query.
// Range arguments must be int64 values.
// String ranges are not supported.
func NewRangeFilter(binName string, begin int64, end int64) *Filter {
return newFilter(binName, NewValue(begin), NewValue(end))
}
// Create a filter for query.
// Range arguments must be longs or integers which can be cast to longs.
// String ranges are not supported.
func newFilter(name string, begin Value, end Value) *Filter {
return &Filter{
name: name,
begin: begin,
end: end,
}
}
func (fltr *Filter) estimateSize() (int, error) {
// bin name size(1) + particle type size(1) + begin particle size(4) + end particle size(4) = 10
return len(fltr.name) + fltr.begin.estimateSize() + fltr.end.estimateSize() + 10, nil
}
func (fltr *Filter) write(buf []byte, offset int) (int, error) {
var err error
// Write name.
len := copy(buf[offset+1:], fltr.name)
buf[offset] = byte(len)
offset += len + 1
// Write particle type.
buf[offset] = byte(fltr.begin.GetType())
offset++
// Write filter begin.
len, err = fltr.begin.write(buf, offset+4)
if err != nil {
return -1, err
}
Buffer.Int32ToBytes(int32(len), buf, offset)
offset += len + 4
// Write filter end.
len, err = fltr.end.write(buf, offset+4)
if err != nil {
return -1, err
}
Buffer.Int32ToBytes(int32(len), buf, offset)
offset += len + 4
return offset, nil
}