-
Notifications
You must be signed in to change notification settings - Fork 197
/
partition_filter.go
123 lines (105 loc) · 4.3 KB
/
partition_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
// Copyright 2014-2022 Aerospike, Inc.
//
// Portions may be licensed to Aerospike, Inc. under one or more contributor
// license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
//
// 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 (
"bytes"
"encoding/gob"
kvs "github.com/aerospike/aerospike-client-go/v7/proto/kvs"
"github.com/aerospike/aerospike-client-go/v7/types"
)
// PartitionFilter is used in scan/queries. This filter is also used as a cursor.
//
// If a previous scan/query returned all records specified by a PartitionFilter instance, a
// future scan/query using the same PartitionFilter instance will only return new records added
// after the last record read (in digest order) in each partition in the previous scan/query.
type PartitionFilter struct {
Begin int
Count int
Digest []byte
// Partitions encapsulates the cursor for the progress of the scan/query to be used for pagination.
Partitions []*PartitionStatus
Done bool
Retry bool
}
// NewPartitionFilterAll creates a partition filter that
// reads all the partitions.
func NewPartitionFilterAll() *PartitionFilter {
return newPartitionFilter(0, _PARTITIONS)
}
// NewPartitionFilterById creates a partition filter by partition id.
// Partition id is between 0 - 4095
func NewPartitionFilterById(partitionId int) *PartitionFilter {
return newPartitionFilter(partitionId, 1)
}
// NewPartitionFilterByRange creates a partition filter by partition range.
// begin partition id is between 0 - 4095
// count is the number of partitions, in the range of 1 - 4096 inclusive.
func NewPartitionFilterByRange(begin, count int) *PartitionFilter {
return newPartitionFilter(begin, count)
}
// NewPartitionFilterByKey returns records after the key's digest in the partition containing the digest.
// Records in all other partitions are not included. The digest is used to determine
// order and this is not the same as userKey order.
//
// This method only works for scan or query with nil filter (primary index query).
// This method does not work for a secondary index query because the digest alone
// is not sufficient to determine a cursor in a secondary index query.
func NewPartitionFilterByKey(key *Key) *PartitionFilter {
return &PartitionFilter{Begin: key.PartitionId(), Count: 1, Digest: key.Digest()}
}
func newPartitionFilter(begin, count int) *PartitionFilter {
return &PartitionFilter{Begin: begin, Count: count}
}
// IsDone returns - if using ScanPolicy.MaxRecords or QueryPolicy,MaxRecords -
// if the previous paginated scans with this partition filter instance return all records?
func (pf *PartitionFilter) IsDone() bool {
return pf.Done
}
// EncodeCursor encodes and returns the cursor for the partition filter.
// This cursor can be persisted and reused later for pagination via PartitionFilter.DecodeCursor.
func (pf *PartitionFilter) EncodeCursor() ([]byte, Error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(pf.Partitions)
if err != nil {
return nil, newError(types.PARAMETER_ERROR, err.Error())
}
return buf.Bytes(), nil
}
// Decodes and sets the cursor for the partition filter using the output of PartitionFilter.EncodeCursor.
func (pf *PartitionFilter) DecodeCursor(b []byte) Error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
var parts []*PartitionStatus
if err := dec.Decode(&parts); err != nil {
return newError(types.PARSE_ERROR, err.Error())
}
pf.Partitions = parts
return nil
}
func (pf *PartitionFilter) grpc() *kvs.PartitionFilter {
begin := uint32(pf.Begin)
ps := make([]*kvs.PartitionStatus, len(pf.Partitions))
for i := range pf.Partitions {
ps[i] = pf.Partitions[i].grpc()
}
return &kvs.PartitionFilter{
Begin: &begin,
Count: uint32(pf.Count),
Digest: pf.Digest,
PartitionStatuses: ps,
Retry: true,
}
}