Skip to content

Commit 19b4afd

Browse files
author
Adrian Todt
committed
Optimized queries.
1 parent 2cfa153 commit 19b4afd

7 files changed

Lines changed: 71 additions & 55 deletions

File tree

src/main/java/com/rethinkdb/ast/Query.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.rethinkdb.ast;
22

3-
import com.rethinkdb.RethinkDB;
43
import com.rethinkdb.gen.exc.ReqlRuntimeError;
54
import com.rethinkdb.gen.proto.QueryType;
65
import com.rethinkdb.model.OptArgs;
76
import com.rethinkdb.utils.Internals;
7+
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
@@ -22,21 +22,20 @@
2222
public class Query {
2323
private static final Logger LOGGER = LoggerFactory.getLogger(Query.class);
2424

25-
public final QueryType type;
25+
public final @NotNull QueryType type;
2626
public final long token;
27-
public final OptArgs globalOptions;
28-
2927
public final @Nullable ReqlAst term;
28+
public final @Nullable OptArgs globalOptions;
3029

31-
public Query(QueryType type, long token, @Nullable ReqlAst term, OptArgs globalOptions) {
30+
public Query(@NotNull QueryType type, long token, @Nullable ReqlAst term, @Nullable OptArgs globalOptions) {
3231
this.type = type;
3332
this.token = token;
3433
this.term = term;
3534
this.globalOptions = globalOptions;
3635
}
3736

38-
public Query(QueryType type, long token) {
39-
this(type, token, null, new OptArgs());
37+
public Query(@NotNull QueryType type, long token) {
38+
this(type, token, null, null);
4039
}
4140

4241
public ByteBuffer serialize() {
@@ -47,8 +46,8 @@ public ByteBuffer serialize() {
4746
if (term != null) {
4847
list.add(term.build());
4948
}
50-
if (!globalOptions.isEmpty()) {
51-
list.add(ReqlAst.buildOptarg(globalOptions));
49+
if (globalOptions != null && !globalOptions.isEmpty()) {
50+
list.add(ReqlAst.buildToMap(globalOptions));
5251
}
5352
String json = Internals.getInternalMapper().writeValueAsString(list);
5453
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);

src/main/java/com/rethinkdb/ast/ReqlAst.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.rethinkdb.net.Connection;
1313
import com.rethinkdb.net.Result;
1414
import com.rethinkdb.utils.Types;
15+
import org.jetbrains.annotations.NotNull;
16+
import org.jetbrains.annotations.Nullable;
1517

1618
import java.util.*;
1719
import java.util.Map.Entry;
@@ -22,33 +24,27 @@
2224
* Base class for all ReQL queries.
2325
*/
2426
public class ReqlAst {
25-
protected final TermType termType;
26-
protected final Arguments args;
27-
protected final OptArgs optargs;
28-
29-
protected ReqlAst(TermType termType, Arguments args, OptArgs optargs) {
30-
if (termType == null) {
31-
throw new ReqlDriverError("termType can't be null!");
32-
}
27+
protected final @NotNull TermType termType;
28+
protected final @Nullable Arguments args;
29+
protected final @Nullable OptArgs optargs;
30+
31+
protected ReqlAst(@NotNull TermType termType, @Nullable Arguments args, @Nullable OptArgs optargs) {
32+
//if (termType == null) {
33+
// throw new ReqlDriverError("termType can't be null!");
34+
//}
3335
this.termType = termType;
34-
this.args = args != null ? args : new Arguments();
35-
this.optargs = optargs != null ? optargs : new OptArgs();
36-
}
37-
38-
public static Map<String, Object> buildOptarg(OptArgs opts) {
39-
Map<String, Object> result = new LinkedHashMap<>(opts.size());
40-
opts.forEach((name, arg) -> result.put(name, arg.build()));
41-
return result;
36+
this.args = args;
37+
this.optargs = optargs;
4238
}
4339

4440
protected Object build() {
45-
// Create a JSON object from the Ast
41+
// Create a JSON object from the AST
4642
// set initial capacity to max size possible, avoids resizing
4743
List<Object> list = new ArrayList<>(3);
4844
list.add(termType.value);
49-
list.add(args.isEmpty() ? Collections.emptyList() : args.stream().map(ReqlAst::build).collect(Collectors.toList()));
50-
if (optargs.size() > 0) {
51-
list.add(buildOptarg(optargs));
45+
list.add(args == null || args.isEmpty() ? Collections.emptyList() : args.stream().map(ReqlAst::build).collect(Collectors.toList()));
46+
if (optargs != null && !optargs.isEmpty()) {
47+
list.add(buildToMap(optargs));
5248
}
5349
return list;
5450
}
@@ -1726,15 +1722,17 @@ private void astToString(StringBuilder builder, String name, String indent, bool
17261722
builder.append('(').append(length).append(length != 1 ? " bytes" : " byte").append(")");
17271723
}
17281724
builder.append('\n');
1729-
Iterator<ReqlAst> argsIterator = args.iterator();
1730-
while (argsIterator.hasNext()) {
1731-
ReqlAst arg = argsIterator.next();
1732-
arg.astToString(builder, null,
1733-
indent + (tail ? " " : "│ "),
1734-
!argsIterator.hasNext() && optargs.isEmpty());
1725+
if (args != null) {
1726+
Iterator<ReqlAst> argsIterator = args.iterator();
1727+
while (argsIterator.hasNext()) {
1728+
ReqlAst arg = argsIterator.next();
1729+
arg.astToString(builder, null,
1730+
indent + (tail ? " " : "│ "),
1731+
!argsIterator.hasNext() && (optargs == null || optargs.isEmpty()));
1732+
}
17351733
}
17361734

1737-
if (!optargs.isEmpty()) {
1735+
if (optargs != null && !optargs.isEmpty()) {
17381736
builder.append(indent).append(tail ? " " : "│ ").append("└── ").append("<optArgs>: \n");
17391737
Iterator<Entry<String, ReqlAst>> optIterator = optargs.entrySet().iterator();
17401738
while (optIterator.hasNext()) {
@@ -1746,6 +1744,12 @@ private void astToString(StringBuilder builder, String name, String indent, bool
17461744
}
17471745
}
17481746

1747+
static Map<String, Object> buildToMap(OptArgs opts) {
1748+
Map<String, Object> result = new LinkedHashMap<>(opts.size());
1749+
opts.forEach((name, arg) -> result.put(name, arg.build()));
1750+
return result;
1751+
}
1752+
17491753
private static <T> T handleAtom(Result<T> result) {
17501754
if (!result.responseType().equals(ResponseType.SUCCESS_ATOM)) {
17511755
throw new IllegalStateException("result is not an atom.");

src/main/java/com/rethinkdb/gen/model/TopLevel.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
package com.rethinkdb.gen.model;
77

8-
import com.rethinkdb.ast.ReqlAst;
98
import com.rethinkdb.model.Arguments;
109
import com.rethinkdb.model.MapObject;
1110
import com.rethinkdb.gen.ast.Error;
1211
import com.rethinkdb.gen.ast.*;
1312
import com.rethinkdb.gen.exc.ReqlDriverError;
1413
import com.rethinkdb.utils.Internals;
1514

16-
import java.util.Arrays;
1715
import java.util.ArrayList;
1816
import java.util.Collections;
1917
import java.util.List;
@@ -29,7 +27,7 @@ public ReqlExpr row(Object... values) {
2927
" Use lambda syntax instead");
3028
}
3129

32-
public static Object pathspec(Object... path) {
30+
public Object pathspec(Object... path) {
3331
if (path.length < 2) {
3432
throw new ReqlDriverError("r.pathspec(...) requires at least two parameters.");
3533
}

src/main/java/com/rethinkdb/model/OptArgs.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ public OptArgs with(String key, List<Object> value) {
2323
}
2424

2525
public static OptArgs fromMap(Map<String, ReqlAst> map) {
26-
OptArgs oa = new OptArgs();
27-
oa.putAll(map);
28-
return oa;
26+
if (map == null) {
27+
return new OptArgs();
28+
}
29+
OptArgs args = new OptArgs();
30+
args.putAll(map);
31+
return args;
2932
}
3033

3134
public static OptArgs of(String key, Object val) {

src/main/java/com/rethinkdb/net/Result.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Result(Connection connection,
119119
this.firstRes = firstRes;
120120
this.fetchMode = fetchMode;
121121
this.typeRef = typeRef;
122-
fmt = new Internals.FormatOptions(query.globalOptions);
122+
this.fmt = Internals.parseFormatOptions(query.globalOptions);
123123
this.unwrapLists = unwrapLists;
124124
currentResponse.set(firstRes);
125125
handleFirstResponse();

src/main/java/com/rethinkdb/utils/Internals.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,20 +272,34 @@ public static SSLContext readCertFile(@NotNull InputStream certFile) {
272272
}
273273
}
274274

275+
public static FormatOptions parseFormatOptions(OptArgs args) {
276+
if (args == null) return FormatOptions.DEFAULT;
277+
278+
Datum time_format = (Datum) args.get("time_format");
279+
boolean rawTime = time_format != null && "raw".equals(time_format.datum);
280+
281+
Datum binary_format = (Datum) args.get("binary_format");
282+
boolean rawBinary = binary_format != null && "raw".equals(binary_format.datum);
283+
284+
Datum group_format = (Datum) args.get("group_format");
285+
boolean rawGroups = group_format != null && "raw".equals(group_format.datum);
286+
287+
if (rawTime || rawBinary || rawGroups) {
288+
return new FormatOptions(rawTime, rawGroups, rawBinary);
289+
}
290+
return FormatOptions.DEFAULT;
291+
}
292+
275293
public static class FormatOptions {
294+
public static final FormatOptions DEFAULT = new FormatOptions(false, false, false);
276295
public final boolean rawTime;
277296
public final boolean rawGroups;
278297
public final boolean rawBinary;
279298

280-
public FormatOptions(OptArgs args) {
281-
Datum time_format = (Datum) args.get("time_format");
282-
this.rawTime = time_format != null && "raw".equals(time_format.datum);
283-
284-
Datum binary_format = (Datum) args.get("binary_format");
285-
this.rawBinary = binary_format != null && "raw".equals(binary_format.datum);
286-
287-
Datum group_format = (Datum) args.get("group_format");
288-
this.rawGroups = group_format != null && "raw".equals(group_format.datum);
299+
public FormatOptions(boolean rawTime, boolean rawGroups, boolean rawBinary) {
300+
this.rawTime = rawTime;
301+
this.rawGroups = rawGroups;
302+
this.rawBinary = rawBinary;
289303
}
290304
}
291305
}

templates/TopLevel.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<%page args="all_terms" />
22
package com.rethinkdb.gen.model;
33

4-
import com.rethinkdb.ast.ReqlAst;
54
import com.rethinkdb.model.Arguments;
65
import com.rethinkdb.model.MapObject;
76
import com.rethinkdb.gen.ast.Error;
87
import com.rethinkdb.gen.ast.*;
98
import com.rethinkdb.gen.exc.ReqlDriverError;
109
import com.rethinkdb.utils.Internals;
1110

12-
import java.util.Arrays;
1311
import java.util.ArrayList;
1412
import java.util.Collections;
1513
import java.util.List;
@@ -25,7 +23,7 @@ public ReqlExpr row(Object... values) {
2523
" Use lambda syntax instead");
2624
}
2725

28-
public static Object pathspec(Object... path) {
26+
public Object pathspec(Object... path) {
2927
if (path.length < 2) {
3028
throw new ReqlDriverError("r.pathspec(...) requires at least two parameters.");
3129
}

0 commit comments

Comments
 (0)