Skip to content

Commit a2d74bd

Browse files
authored
Merge f0cfa97 into 94ae2ac
2 parents 94ae2ac + f0cfa97 commit a2d74bd

File tree

5 files changed

+127
-66
lines changed

5 files changed

+127
-66
lines changed

firebase-database/src/main/java/com/google/firebase/database/Query.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,8 @@ public void run() {
290290
*/
291291
@NonNull
292292
public Query startAfter(@Nullable String value) {
293-
if (value != null && params.getIndex().equals(KeyIndex.getInstance())) {
294-
return startAt(PushIdGenerator.successor(value));
295-
}
296-
return startAt(value, ChildKey.getMaxName().asString());
293+
Node node = value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
294+
return startAfter(node, null);
297295
}
298296

299297
/**
@@ -306,7 +304,7 @@ public Query startAfter(@Nullable String value) {
306304
*/
307305
@NonNull
308306
public Query startAfter(double value) {
309-
return startAt(value, ChildKey.getMaxName().asString());
307+
return startAfter(new DoubleNode(value, PriorityUtilities.NullPriority()), null);
310308
}
311309

312310
/**
@@ -319,7 +317,7 @@ public Query startAfter(double value) {
319317
*/
320318
@NonNull
321319
public Query startAfter(boolean value) {
322-
return startAt(value, ChildKey.getMaxName().asString());
320+
return startAfter(new BooleanNode(value, PriorityUtilities.NullPriority()), null);
323321
}
324322

325323
/**
@@ -334,9 +332,6 @@ public Query startAfter(boolean value) {
334332
*/
335333
@NonNull
336334
public Query startAfter(@Nullable String value, @Nullable String key) {
337-
if (value != null && params.getIndex().equals(KeyIndex.getInstance())) {
338-
value = PushIdGenerator.successor(value);
339-
}
340335
Node node =
341336
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
342337
return startAfter(node, key);
@@ -373,7 +368,7 @@ public Query startAfter(boolean value, @Nullable String key) {
373368
}
374369

375370
private Query startAfter(Node node, String key) {
376-
return startAt(node, PushIdGenerator.successor(key));
371+
return startAt(node, key, false);
377372
}
378373

379374
/**
@@ -426,7 +421,7 @@ public Query startAt(boolean value) {
426421
public Query startAt(@Nullable String value, @Nullable String key) {
427422
Node node =
428423
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
429-
return startAt(node, key);
424+
return startAt(node, key, true);
430425
}
431426

432427
/**
@@ -440,7 +435,7 @@ public Query startAt(@Nullable String value, @Nullable String key) {
440435
*/
441436
@NonNull
442437
public Query startAt(double value, @Nullable String key) {
443-
return startAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key);
438+
return startAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key, true);
444439
}
445440

446441
/**
@@ -455,10 +450,10 @@ public Query startAt(double value, @Nullable String key) {
455450
*/
456451
@NonNull
457452
public Query startAt(boolean value, @Nullable String key) {
458-
return startAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key);
453+
return startAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key, true);
459454
}
460455

461-
private Query startAt(Node node, String key) {
456+
private Query startAt(Node node, String key, Boolean isInclusive) {
462457
Validation.validateNullableKey(key);
463458
if (!(node.isLeafNode() || node.isEmpty())) {
464459
throw new IllegalArgumentException(
@@ -478,7 +473,7 @@ private Query startAt(Node node, String key) {
478473
childKey = ChildKey.fromString(key);
479474
}
480475
}
481-
QueryParams newParams = params.startAt(node, childKey);
476+
QueryParams newParams = isInclusive ? params.startAt(node, childKey) : params.startAfter(node, childKey);
482477
validateLimit(newParams);
483478
validateQueryEndpoints(newParams);
484479
hardAssert(newParams.isValid());
@@ -495,10 +490,7 @@ private Query startAt(Node node, String key) {
495490
*/
496491
@NonNull
497492
public Query endBefore(@Nullable String value) {
498-
if (value != null && params.getIndex().equals(KeyIndex.getInstance())) {
499-
return endAt(PushIdGenerator.predecessor(value));
500-
}
501-
return endAt(value, ChildKey.getMinName().asString());
493+
return endBefore(value, null);
502494
}
503495

504496
/**
@@ -511,7 +503,7 @@ public Query endBefore(@Nullable String value) {
511503
*/
512504
@NonNull
513505
public Query endBefore(double value) {
514-
return endAt(value, ChildKey.getMinName().asString());
506+
return endAt(value, null);
515507
}
516508

517509
/**
@@ -524,7 +516,7 @@ public Query endBefore(double value) {
524516
*/
525517
@NonNull
526518
public Query endBefore(boolean value) {
527-
return endAt(value, ChildKey.getMinName().asString());
519+
return endBefore(value, null);
528520
}
529521

530522
/**
@@ -539,9 +531,6 @@ public Query endBefore(boolean value) {
539531
*/
540532
@NonNull
541533
public Query endBefore(@Nullable String value, @Nullable String key) {
542-
if (value != null && params.getIndex().equals(KeyIndex.getInstance())) {
543-
value = PushIdGenerator.predecessor(value);
544-
}
545534
Node node =
546535
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
547536
return endBefore(node, key);
@@ -578,7 +567,7 @@ public Query endBefore(boolean value, @Nullable String key) {
578567
}
579568

580569
private Query endBefore(Node node, String key) {
581-
return endAt(node, PushIdGenerator.predecessor(key));
570+
return endAt(node, key, false);
582571
}
583572

584573
/**
@@ -631,7 +620,7 @@ public Query endAt(boolean value) {
631620
public Query endAt(@Nullable String value, @Nullable String key) {
632621
Node node =
633622
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
634-
return endAt(node, key);
623+
return endAt(node, key, true);
635624
}
636625

637626
/**
@@ -645,7 +634,7 @@ public Query endAt(@Nullable String value, @Nullable String key) {
645634
*/
646635
@NonNull
647636
public Query endAt(double value, @Nullable String key) {
648-
return endAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key);
637+
return endAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key, true);
649638
}
650639

651640
/**
@@ -660,10 +649,10 @@ public Query endAt(double value, @Nullable String key) {
660649
*/
661650
@NonNull
662651
public Query endAt(boolean value, @Nullable String key) {
663-
return endAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key);
652+
return endAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key, true);
664653
}
665654

666-
private Query endAt(Node node, String key) {
655+
private Query endAt(Node node, String key, Boolean isInclusive) {
667656
Validation.validateNullableKey(key);
668657
if (!(node.isLeafNode() || node.isEmpty())) {
669658
throw new IllegalArgumentException("Can only use simple values for endAt()");
@@ -672,7 +661,7 @@ private Query endAt(Node node, String key) {
672661
if (params.hasEnd()) {
673662
throw new IllegalArgumentException("Can't call endAt() or equalTo() multiple times");
674663
}
675-
QueryParams newParams = params.endAt(node, childKey);
664+
QueryParams newParams = isInclusive ? params.endAt(node, childKey) : params.endBefore(node, childKey);
676665
validateLimit(newParams);
677666
validateQueryEndpoints(newParams);
678667
hardAssert(newParams.isValid());

firebase-database/src/main/java/com/google/firebase/database/core/view/QueryParams.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public final class QueryParams {
4141

4242
private static final String INDEX_START_VALUE = "sp";
4343
private static final String INDEX_START_NAME = "sn";
44+
private static final String INDEX_START_IS_INCLUSIVE = "sin";
4445
private static final String INDEX_END_VALUE = "ep";
4546
private static final String INDEX_END_NAME = "en";
47+
private static final String INDEX_END_IS_INCLUSIVE = "ein";
4648
private static final String LIMIT = "l";
4749
private static final String VIEW_FROM = "vf";
4850
private static final String INDEX = "i";
@@ -56,8 +58,10 @@ private static enum ViewFrom {
5658
private ViewFrom viewFrom;
5759
private Node indexStartValue = null;
5860
private ChildKey indexStartName = null;
61+
private boolean indexStartIsInclusive = true;
5962
private Node indexEndValue = null;
6063
private ChildKey indexEndName = null;
64+
private boolean indexEndIsInclusive = true;
6165

6266
private Index index = PriorityIndex.getInstance();
6367

@@ -85,6 +89,10 @@ public ChildKey getIndexStartName() {
8589
}
8690
}
8791

92+
public boolean getIndexStartIsInclusive() {
93+
return indexStartIsInclusive;
94+
}
95+
8896
public boolean hasEnd() {
8997
return indexEndValue != null;
9098
}
@@ -98,7 +106,7 @@ public Node getIndexEndValue() {
98106

99107
public ChildKey getIndexEndName() {
100108
if (!hasEnd()) {
101-
throw new IllegalArgumentException("Cannot get index end name if start has not been set");
109+
throw new IllegalArgumentException("Cannot get index end name if end has not been set");
102110
}
103111
if (indexEndName != null) {
104112
return indexEndName;
@@ -107,6 +115,10 @@ public ChildKey getIndexEndName() {
107115
}
108116
}
109117

118+
public boolean getIndexEndIsInclusive() {
119+
return indexEndIsInclusive;
120+
}
121+
110122
public boolean hasLimit() {
111123
return limit != null;
112124
}
@@ -131,8 +143,10 @@ private QueryParams copy() {
131143
params.limit = limit;
132144
params.indexStartValue = indexStartValue;
133145
params.indexStartName = indexStartName;
146+
params.indexStartIsInclusive = indexStartIsInclusive;
134147
params.indexEndValue = indexEndValue;
135148
params.indexEndName = indexEndName;
149+
params.indexEndIsInclusive = indexEndIsInclusive;
136150
params.viewFrom = viewFrom;
137151
params.index = index;
138152
return params;
@@ -152,26 +166,44 @@ public QueryParams limitToLast(int limit) {
152166
return copy;
153167
}
154168

155-
public QueryParams startAt(Node indexStartValue, ChildKey indexStartName) {
169+
private QueryParams startAt(Node indexStartValue, ChildKey indexStartName, boolean indexStartIsInclusive) {
156170
hardAssert(indexStartValue.isLeafNode() || indexStartValue.isEmpty());
157171
// We can't tolerate longs as query endpoints. See comment in normalizeValue();
158172
hardAssert(!(indexStartValue instanceof LongNode));
159173
QueryParams copy = copy();
160174
copy.indexStartValue = indexStartValue;
161175
copy.indexStartName = indexStartName;
176+
copy.indexStartIsInclusive = indexStartIsInclusive;
162177
return copy;
163178
}
164179

165-
public QueryParams endAt(Node indexEndValue, ChildKey indexEndName) {
180+
public QueryParams startAt(Node indexStartValue, ChildKey indexStartName) {
181+
return startAt(indexStartValue, indexStartName, true);
182+
}
183+
184+
public QueryParams startAfter(Node indexStartValue, ChildKey indexStartName) {
185+
return startAt(indexStartValue, indexStartName, false);
186+
}
187+
188+
public QueryParams endAt(Node indexEndValue, ChildKey indexEndName, boolean indexEndIsInclusive) {
166189
hardAssert(indexEndValue.isLeafNode() || indexEndValue.isEmpty());
167190
// We can't tolerate longs as query endpoints. See comment in normalizeValue();
168191
hardAssert(!(indexEndValue instanceof LongNode));
169192
QueryParams copy = copy();
170193
copy.indexEndValue = indexEndValue;
171194
copy.indexEndName = indexEndName;
195+
copy.indexEndIsInclusive = indexEndIsInclusive;
172196
return copy;
173197
}
174198

199+
public QueryParams endAt(Node indexEndValue, ChildKey indexEndName) {
200+
return endAt(indexEndValue, indexEndName, true);
201+
}
202+
203+
public QueryParams endBefore(Node indexEndValue, ChildKey indexEndName) {
204+
return endAt(indexEndValue, indexEndName, false);
205+
}
206+
175207
public QueryParams orderBy(Index index) {
176208
QueryParams copy = copy();
177209
copy.index = index;
@@ -190,12 +222,14 @@ public Map<String, Object> getWireProtocolParams() {
190222
if (indexStartName != null) {
191223
queryObject.put(INDEX_START_NAME, indexStartName.asString());
192224
}
225+
queryObject.put(INDEX_START_IS_INCLUSIVE, indexStartIsInclusive);
193226
}
194227
if (hasEnd()) {
195228
queryObject.put(INDEX_END_VALUE, indexEndValue.getValue());
196229
if (indexEndName != null) {
197230
queryObject.put(INDEX_END_NAME, indexEndName.asString());
198231
}
232+
queryObject.put(INDEX_END_IS_INCLUSIVE, indexEndIsInclusive);
199233
}
200234
if (limit != null) {
201235
queryObject.put(LIMIT, limit);
@@ -261,6 +295,7 @@ public static QueryParams fromQueryObject(Map<String, Object> map) {
261295
if (indexStartName != null) {
262296
params.indexStartName = ChildKey.fromString(indexStartName);
263297
}
298+
params.indexStartIsInclusive = (Boolean) map.get(INDEX_START_IS_INCLUSIVE);
264299
}
265300

266301
if (map.containsKey(INDEX_END_VALUE)) {
@@ -270,6 +305,7 @@ public static QueryParams fromQueryObject(Map<String, Object> map) {
270305
if (indexEndName != null) {
271306
params.indexEndName = ChildKey.fromString(indexEndName);
272307
}
308+
params.indexEndIsInclusive = (Boolean) map.get(INDEX_END_IS_INCLUSIVE);
273309
}
274310

275311
String viewFrom = (String) map.get(VIEW_FROM);
@@ -337,6 +373,12 @@ public boolean equals(Object o) {
337373
: that.indexStartValue != null) {
338374
return false;
339375
}
376+
if (indexStartIsInclusive != that.indexStartIsInclusive) {
377+
return false;
378+
}
379+
if (indexEndIsInclusive != that.indexEndIsInclusive) {
380+
return false;
381+
}
340382
// viewFrom might be null, but we really want to compare left vs right
341383
if (isViewFromLeft() != that.isViewFromLeft()) {
342384
return false;
@@ -351,8 +393,10 @@ public int hashCode() {
351393
result = 31 * result + (isViewFromLeft() ? 1231 : 1237);
352394
result = 31 * result + (indexStartValue != null ? indexStartValue.hashCode() : 0);
353395
result = 31 * result + (indexStartName != null ? indexStartName.hashCode() : 0);
396+
result = 31 * result + Boolean.valueOf(indexStartIsInclusive).hashCode();
354397
result = 31 * result + (indexEndValue != null ? indexEndValue.hashCode() : 0);
355398
result = 31 * result + (indexEndName != null ? indexEndName.hashCode() : 0);
399+
result = 31 * result + Boolean.valueOf(indexEndIsInclusive).hashCode();
356400
result = 31 * result + (index != null ? index.hashCode() : 0);
357401
return result;
358402
}

0 commit comments

Comments
 (0)