Skip to content

Commit d184387

Browse files
avelanariusshawjef3
andcommitted
Add support for BYPASS CACHE to QueryBuilder
Add support for adding BYPASS CACHE clause to SELECT queries. Fixes #155 Co-authored-by: Jeff Shaw <[email protected]>
1 parent d2c1513 commit d184387

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

query-builder/src/main/java/com/datastax/oss/driver/api/querybuilder/select/Select.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,12 @@ default Select orderBy(@NonNull String columnName, @NonNull ClusteringOrder orde
225225
*/
226226
@NonNull
227227
Select usingTimeout(@NonNull BindMarker timeout);
228+
229+
/**
230+
* Adds an BYPASS CACHE clause to this query.
231+
*
232+
* <p>This method is idempotent, calling it multiple times will only add a single clause.
233+
*/
234+
@NonNull
235+
Select bypassCache();
228236
}

query-builder/src/main/java/com/datastax/oss/driver/internal/querybuilder/select/DefaultSelect.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class DefaultSelect implements SelectFrom, Select {
5757
private final Object limit;
5858
private final Object perPartitionLimit;
5959
private final boolean allowsFiltering;
60+
private final boolean bypassCache;
6061
private final Object timeout;
6162

6263
public DefaultSelect(@Nullable CqlIdentifier keyspace, @NonNull CqlIdentifier table) {
@@ -72,6 +73,7 @@ public DefaultSelect(@Nullable CqlIdentifier keyspace, @NonNull CqlIdentifier ta
7273
null,
7374
null,
7475
false,
76+
false,
7577
null);
7678
}
7779

@@ -94,6 +96,7 @@ public DefaultSelect(
9496
@Nullable Object limit,
9597
@Nullable Object perPartitionLimit,
9698
boolean allowsFiltering,
99+
boolean bypassCache,
97100
@Nullable Object timeout) {
98101
this.groupByClauses = groupByClauses;
99102
this.orderings = orderings;
@@ -111,6 +114,7 @@ public DefaultSelect(
111114
this.limit = limit;
112115
this.perPartitionLimit = perPartitionLimit;
113116
this.allowsFiltering = allowsFiltering;
117+
this.bypassCache = bypassCache;
114118
Preconditions.checkArgument(
115119
timeout == null || timeout instanceof CqlDuration || timeout instanceof BindMarker,
116120
"TIMEOUT value must be a BindMarker or a CqlDuration");
@@ -132,6 +136,7 @@ public SelectFrom json() {
132136
limit,
133137
perPartitionLimit,
134138
allowsFiltering,
139+
bypassCache,
135140
timeout);
136141
}
137142

@@ -150,6 +155,7 @@ public SelectFrom distinct() {
150155
limit,
151156
perPartitionLimit,
152157
allowsFiltering,
158+
bypassCache,
153159
timeout);
154160
}
155161

@@ -210,6 +216,7 @@ public Select withSelectors(@NonNull ImmutableList<Selector> newSelectors) {
210216
limit,
211217
perPartitionLimit,
212218
allowsFiltering,
219+
bypassCache,
213220
timeout);
214221
}
215222

@@ -239,6 +246,7 @@ public Select withRelations(@NonNull ImmutableList<Relation> newRelations) {
239246
limit,
240247
perPartitionLimit,
241248
allowsFiltering,
249+
bypassCache,
242250
timeout);
243251
}
244252

@@ -268,6 +276,7 @@ public Select withGroupByClauses(@NonNull ImmutableList<Selector> newGroupByClau
268276
limit,
269277
perPartitionLimit,
270278
allowsFiltering,
279+
bypassCache,
271280
timeout);
272281
}
273282

@@ -297,6 +306,7 @@ public Select withOrderings(@NonNull ImmutableMap<CqlIdentifier, ClusteringOrder
297306
limit,
298307
perPartitionLimit,
299308
allowsFiltering,
309+
bypassCache,
300310
timeout);
301311
}
302312

@@ -316,6 +326,7 @@ public Select limit(int limit) {
316326
limit,
317327
perPartitionLimit,
318328
allowsFiltering,
329+
bypassCache,
319330
timeout);
320331
}
321332

@@ -334,6 +345,7 @@ public Select limit(@Nullable BindMarker bindMarker) {
334345
bindMarker,
335346
perPartitionLimit,
336347
allowsFiltering,
348+
bypassCache,
337349
timeout);
338350
}
339351

@@ -354,6 +366,7 @@ public Select perPartitionLimit(int perPartitionLimit) {
354366
limit,
355367
perPartitionLimit,
356368
allowsFiltering,
369+
bypassCache,
357370
timeout);
358371
}
359372

@@ -372,6 +385,7 @@ public Select perPartitionLimit(@Nullable BindMarker bindMarker) {
372385
limit,
373386
bindMarker,
374387
allowsFiltering,
388+
bypassCache,
375389
timeout);
376390
}
377391

@@ -390,8 +404,29 @@ public Select allowFiltering() {
390404
limit,
391405
perPartitionLimit,
392406
true,
407+
bypassCache,
408+
timeout);
409+
}
410+
411+
@NonNull
412+
@Override
413+
public Select bypassCache() {
414+
return new DefaultSelect(
415+
keyspace,
416+
table,
417+
isJson,
418+
isDistinct,
419+
selectors,
420+
relations,
421+
groupByClauses,
422+
orderings,
423+
limit,
424+
perPartitionLimit,
425+
allowsFiltering,
426+
true,
393427
timeout);
394428
}
429+
395430
@NonNull
396431
@Override
397432
public Select usingTimeout(@NonNull final CqlDuration timeout) {
@@ -407,6 +442,7 @@ public Select usingTimeout(@NonNull final CqlDuration timeout) {
407442
limit,
408443
perPartitionLimit,
409444
allowsFiltering,
445+
bypassCache,
410446
timeout);
411447
}
412448

@@ -425,6 +461,7 @@ public Select usingTimeout(@NonNull BindMarker timeout) {
425461
limit,
426462
perPartitionLimit,
427463
allowsFiltering,
464+
bypassCache,
428465
timeout);
429466
}
430467

@@ -482,6 +519,10 @@ public String asCql() {
482519
builder.append(" ALLOW FILTERING");
483520
}
484521

522+
if (bypassCache) {
523+
builder.append(" BYPASS CACHE");
524+
}
525+
485526
if (timeout != null) {
486527
builder.append(" USING TIMEOUT ");
487528
if (timeout instanceof BindMarker) {
@@ -575,6 +616,10 @@ public boolean allowsFiltering() {
575616
return allowsFiltering;
576617
}
577618

619+
public boolean bypassesCache() {
620+
return bypassCache;
621+
}
622+
578623
@Nullable
579624
public Object getTimeout() {
580625
return timeout;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2022 ScyllaDB
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.querybuilder.select;
17+
18+
import static com.datastax.oss.driver.api.querybuilder.Assertions.assertThat;
19+
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.selectFrom;
20+
21+
import org.junit.Test;
22+
23+
public class SelectBypassCacheTest {
24+
@Test
25+
public void should_generate_bypass_cache() {
26+
assertThat(selectFrom("foo").all().bypassCache()).hasCql("SELECT * FROM foo BYPASS CACHE");
27+
}
28+
29+
@Test
30+
public void should_use_single_bypass_cache_if_called_multiple_times() {
31+
assertThat(selectFrom("foo").all().bypassCache().bypassCache())
32+
.hasCql("SELECT * FROM foo BYPASS CACHE");
33+
}
34+
}

0 commit comments

Comments
 (0)