-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
TokenFilter.java
317 lines (270 loc) · 10.4 KB
/
TokenFilter.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.cassandra.cql3.restrictions;
import java.nio.ByteBuffer;
import java.util.*;
import com.google.common.collect.BoundType;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import org.apache.cassandra.schema.ColumnMetadata;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.cql3.QueryOptions;
import org.apache.cassandra.cql3.functions.Function;
import org.apache.cassandra.cql3.statements.Bound;
import org.apache.cassandra.db.filter.RowFilter;
import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.index.IndexRegistry;
import org.apache.cassandra.service.ClientState;
import static org.apache.cassandra.cql3.statements.Bound.END;
import static org.apache.cassandra.cql3.statements.Bound.START;
/**
* <code>Restriction</code> decorator used to merge non-token restriction and token restriction on partition keys.
*
* <p>If all partition key columns have non-token restrictions and do not need filtering, they take precedence
* when calculating bounds, incusiveness etc (see CASSANDRA-12149).</p>
*/
final class TokenFilter implements PartitionKeyRestrictions
{
/**
* The decorated restriction
*/
private final PartitionKeyRestrictions restrictions;
/**
* The restriction on the token
*/
private final TokenRestriction tokenRestriction;
/**
* Partitioner to manage tokens, extracted from tokenRestriction metadata.
*/
private final IPartitioner partitioner;
public boolean hasIN()
{
return isOnToken() ? false : restrictions.hasIN();
}
public boolean hasContains()
{
return isOnToken() ? false : restrictions.hasContains();
}
public boolean hasOnlyEqualityRestrictions()
{
return isOnToken() ? false : restrictions.hasOnlyEqualityRestrictions();
}
@Override
public Set<Restriction> getRestrictions(ColumnMetadata columnDef)
{
Set<Restriction> set = new HashSet<>();
set.addAll(restrictions.getRestrictions(columnDef));
set.addAll(tokenRestriction.getRestrictions(columnDef));
return set;
}
@Override
public boolean isOnToken()
{
// if all partition key columns have non-token restrictions and do not need filtering,
// we can simply use the token range to filter those restrictions and then ignore the token range
return needFiltering(tokenRestriction.metadata) || restrictions.size() < tokenRestriction.size();
}
public TokenFilter(PartitionKeyRestrictions restrictions, TokenRestriction tokenRestriction)
{
this.restrictions = restrictions;
this.tokenRestriction = tokenRestriction;
this.partitioner = tokenRestriction.metadata.partitioner;
}
@Override
public List<ByteBuffer> values(QueryOptions options, ClientState state) throws InvalidRequestException
{
return filter(restrictions.values(options, state), options, state);
}
@Override
public PartitionKeyRestrictions mergeWith(Restriction restriction) throws InvalidRequestException
{
if (restriction.isOnToken())
return new TokenFilter(restrictions, (TokenRestriction) tokenRestriction.mergeWith(restriction));
return new TokenFilter(restrictions.mergeWith(restriction), tokenRestriction);
}
@Override
public boolean isInclusive(Bound bound)
{
return isOnToken() ? tokenRestriction.isInclusive(bound) : restrictions.isInclusive(bound);
}
@Override
public boolean hasBound(Bound bound)
{
return isOnToken() ? tokenRestriction.hasBound(bound) : restrictions.hasBound(bound);
}
@Override
public List<ByteBuffer> bounds(Bound bound, QueryOptions options) throws InvalidRequestException
{
return isOnToken() ? tokenRestriction.bounds(bound, options) : restrictions.bounds(bound, options);
}
/**
* Filter the values returned by the restriction.
*
* @param values the values returned by the decorated restriction
* @param options the query options
* @param state the client state
* @return the values matching the token restriction
* @throws InvalidRequestException if the request is invalid
*/
private List<ByteBuffer> filter(List<ByteBuffer> values, QueryOptions options, ClientState state) throws InvalidRequestException
{
RangeSet<Token> rangeSet = tokenRestriction.hasSlice() ? toRangeSet(tokenRestriction, options)
: toRangeSet(tokenRestriction.values(options, state));
return filterWithRangeSet(rangeSet, values);
}
/**
* Filter out the values for which the tokens are not included within the specified range.
*
* @param tokens the tokens range
* @param values the restricted values
* @return the values for which the tokens are not included within the specified range.
*/
private List<ByteBuffer> filterWithRangeSet(RangeSet<Token> tokens, List<ByteBuffer> values)
{
List<ByteBuffer> remaining = new ArrayList<>();
for (ByteBuffer value : values)
{
Token token = partitioner.getToken(value);
if (!tokens.contains(token))
continue;
remaining.add(value);
}
return remaining;
}
/**
* Converts the specified list into a range set.
*
* @param buffers the token restriction values
* @return the range set corresponding to the specified list
*/
private RangeSet<Token> toRangeSet(List<ByteBuffer> buffers)
{
ImmutableRangeSet.Builder<Token> builder = ImmutableRangeSet.builder();
for (ByteBuffer buffer : buffers)
builder.add(Range.singleton(deserializeToken(buffer)));
return builder.build();
}
/**
* Converts the specified slice into a range set.
*
* @param slice the slice to convert
* @param options the query option
* @return the range set corresponding to the specified slice
* @throws InvalidRequestException if the request is invalid
*/
private RangeSet<Token> toRangeSet(TokenRestriction slice, QueryOptions options) throws InvalidRequestException
{
if (slice.hasBound(START))
{
Token start = deserializeToken(slice.bounds(START, options).get(0));
BoundType startBoundType = toBoundType(slice.isInclusive(START));
if (slice.hasBound(END))
{
BoundType endBoundType = toBoundType(slice.isInclusive(END));
Token end = deserializeToken(slice.bounds(END, options).get(0));
if (start.equals(end) && (BoundType.OPEN == startBoundType || BoundType.OPEN == endBoundType))
return ImmutableRangeSet.of();
if (start.compareTo(end) <= 0)
return ImmutableRangeSet.of(Range.range(start,
startBoundType,
end,
endBoundType));
return ImmutableRangeSet.<Token> builder()
.add(Range.upTo(end, endBoundType))
.add(Range.downTo(start, startBoundType))
.build();
}
return ImmutableRangeSet.of(Range.downTo(start,
startBoundType));
}
Token end = deserializeToken(slice.bounds(END, options).get(0));
return ImmutableRangeSet.of(Range.upTo(end, toBoundType(slice.isInclusive(END))));
}
/**
* Deserializes the token corresponding to the specified buffer.
*
* @param buffer the buffer
* @return the token corresponding to the specified buffer
*/
private Token deserializeToken(ByteBuffer buffer)
{
return partitioner.getTokenFactory().fromByteArray(buffer);
}
private static BoundType toBoundType(boolean inclusive)
{
return inclusive ? BoundType.CLOSED : BoundType.OPEN;
}
@Override
public ColumnMetadata getFirstColumn()
{
return restrictions.getFirstColumn();
}
@Override
public ColumnMetadata getLastColumn()
{
return restrictions.getLastColumn();
}
@Override
public List<ColumnMetadata> getColumnDefs()
{
return restrictions.getColumnDefs();
}
@Override
public void addFunctionsTo(List<Function> functions)
{
restrictions.addFunctionsTo(functions);
}
@Override
public boolean hasSupportingIndex(IndexRegistry indexRegistry)
{
return restrictions.hasSupportingIndex(indexRegistry);
}
@Override
public void addRowFilterTo(RowFilter filter, IndexRegistry indexRegistry, QueryOptions options)
{
restrictions.addRowFilterTo(filter, indexRegistry, options);
}
@Override
public boolean isEmpty()
{
return restrictions.isEmpty();
}
@Override
public int size()
{
return restrictions.size();
}
@Override
public boolean needFiltering(TableMetadata table)
{
return restrictions.needFiltering(table);
}
@Override
public boolean hasUnrestrictedPartitionKeyComponents(TableMetadata table)
{
return restrictions.hasUnrestrictedPartitionKeyComponents(table);
}
@Override
public boolean hasSlice()
{
return restrictions.hasSlice();
}
}