forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
132 lines (112 loc) · 4.54 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
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
// 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 (
"fmt"
ParticleType "github.com/aerospike/aerospike-client-go/types/particle_type"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
// Filter specifies a query filter definition.
type Filter struct {
name string
idxType IndexCollectionType
valueParticleType int
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, ICT_DEFAULT, val.GetType(), 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 {
vBegin, vEnd := NewValue(begin), NewValue(end)
return newFilter(binName, ICT_DEFAULT, vBegin.GetType(), vBegin, vEnd)
}
// NewContainsFilter creates a contains filter for query on collection index.
func NewContainsFilter(binName string, indexCollectionType IndexCollectionType, value interface{}) *Filter {
v := NewValue(value)
return newFilter(binName, indexCollectionType, v.GetType(), v, v)
}
// NewContainsRangeFilter creates a contains filter for query on ranges of data in a collection index.
func NewContainsRangeFilter(binName string, indexCollectionType IndexCollectionType, begin, end int64) *Filter {
vBegin, vEnd := NewValue(begin), NewValue(end)
return newFilter(binName, indexCollectionType, vBegin.GetType(), vBegin, vEnd)
}
// NewGeoWithinRegionFilter creates a geospatial "within region" filter for query.
// Argument must be a valid GeoJSON region.
func NewGeoWithinRegionFilter(binName, region string) *Filter {
v := NewStringValue(region)
return newFilter(binName, ICT_DEFAULT, ParticleType.GEOJSON, v, v)
}
// NewGeoRegionsContainingPointFilter creates a geospatial "containing point" filter for query.
// Argument must be a valid GeoJSON point.
func NewGeoRegionsContainingPointFilter(binName, point string) *Filter {
v := NewStringValue(point)
return newFilter(binName, ICT_DEFAULT, ParticleType.GEOJSON, v, v)
}
// NewGeoWithinRadiusFilter creates a geospatial "within radius" filter for query.
// Arguments must be valid longitude/latitude/radius (meters) values.
func NewGeoWithinRadiusFilter(binName string, lng, lat, radius float64) *Filter {
rgnStr := fmt.Sprintf("{ \"type\": \"AeroCircle\", "+"\"coordinates\": [[%.8f, %.8f], %f] }", lng, lat, radius)
return newFilter(binName, ICT_DEFAULT, ParticleType.GEOJSON, NewValue(rgnStr), NewValue(rgnStr))
}
// 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, indexCollectionType IndexCollectionType, valueParticleType int, begin Value, end Value) *Filter {
return &Filter{
name: name,
idxType: indexCollectionType,
valueParticleType: valueParticleType,
begin: begin,
end: end,
}
}
// IndexType return filter's index type.
func (fltr *Filter) IndexCollectionType() IndexCollectionType {
return fltr.idxType
}
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.valueParticleType)
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
}