Repository navigation
Expand file tree
/
Copy pathQuery.java
More file actions
675 lines (621 loc) · 23.1 KB
/
Copy pathQuery.java
File metadata and controls
675 lines (621 loc) · 23.1 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* Copyright 2017 Google Inc.
*
* 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 com.google.firebase.database;
import static com.google.firebase.database.utilities.Utilities.hardAssert;
import com.google.firebase.database.core.ChildEventRegistration;
import com.google.firebase.database.core.EventRegistration;
import com.google.firebase.database.core.Path;
import com.google.firebase.database.core.Repo;
import com.google.firebase.database.core.ValueEventRegistration;
import com.google.firebase.database.core.ZombieEventManager;
import com.google.firebase.database.core.view.QueryParams;
import com.google.firebase.database.core.view.QuerySpec;
import com.google.firebase.database.snapshot.BooleanNode;
import com.google.firebase.database.snapshot.ChildKey;
import com.google.firebase.database.snapshot.DoubleNode;
import com.google.firebase.database.snapshot.EmptyNode;
import com.google.firebase.database.snapshot.Index;
import com.google.firebase.database.snapshot.KeyIndex;
import com.google.firebase.database.snapshot.Node;
import com.google.firebase.database.snapshot.PathIndex;
import com.google.firebase.database.snapshot.PriorityIndex;
import com.google.firebase.database.snapshot.PriorityUtilities;
import com.google.firebase.database.snapshot.StringNode;
import com.google.firebase.database.snapshot.ValueIndex;
import com.google.firebase.database.utilities.Validation;
/**
* The Query class (and its subclass, {@link DatabaseReference}) are used for reading data.
* Listeners are attached, and they will be triggered when the corresponding data changes. <br>
* <br>
* Instances of Query are obtained by calling startAt(), endAt(), or limit() on a DatabaseReference.
*/
public class Query {
/**
* @hide
*/
protected final Repo repo;
/**
* @hide
*/
protected final Path path;
/**
* @hide
*/
protected final QueryParams params;
// we can't use params index, because the default query params have priority index set as
// default,
// but we don't want to allow multiple orderByPriority calls, so track them here
private final boolean orderByCalled;
Query(Repo repo, Path path, QueryParams params, boolean orderByCalled) throws DatabaseException {
hardAssert(params.isValid(), "Validation of queries failed.");
this.repo = repo;
this.path = path;
this.params = params;
this.orderByCalled = orderByCalled;
}
Query(Repo repo, Path path) {
this(repo, path, QueryParams.DEFAULT_PARAMS, false);
}
/**
* This method validates that key index has been called with the correct combination of
* parameters.
*/
private void validateQueryEndpoints(QueryParams params) {
if (params.getIndex().equals(KeyIndex.getInstance())) {
String message =
"You must use startAt(String value), endAt(String value) or "
+ "equalTo(String value) in combination with orderByKey(). Other type of values or "
+ "using the version with 2 parameters is not supported";
if (params.hasStart()) {
Node startNode = params.getIndexStartValue();
ChildKey startName = params.getIndexStartName();
if (startName != ChildKey.getMinName() || !(startNode instanceof StringNode)) {
throw new IllegalArgumentException(message);
}
}
if (params.hasEnd()) {
Node endNode = params.getIndexEndValue();
ChildKey endName = params.getIndexEndName();
if (endName != ChildKey.getMaxName() || !(endNode instanceof StringNode)) {
throw new IllegalArgumentException(message);
}
}
} else if (params.getIndex().equals(PriorityIndex.getInstance())) {
if ((params.hasStart() && !PriorityUtilities.isValidPriority(params.getIndexStartValue()))
|| (params.hasEnd() && !PriorityUtilities.isValidPriority(params.getIndexEndValue()))) {
throw new IllegalArgumentException(
"When using orderByPriority(), values provided to startAt(), "
+ "endAt(), or equalTo() must be valid priorities.");
}
}
}
/**
* This method validates that limit has been called with the correct combination or parameters.
*/
private void validateLimit(QueryParams params) {
if (params.hasStart() && params.hasEnd() && params.hasLimit() && !params.hasAnchoredLimit()) {
throw new IllegalArgumentException(
"Can't combine startAt(), endAt() and limit(). "
+ "Use limitToFirst() or limitToLast() instead");
}
}
/** This method validates that the equalTo call can be made. */
private void validateEqualToCall() {
if (params.hasStart()) {
throw new IllegalArgumentException("Can't call equalTo() and startAt() combined");
}
if (params.hasEnd()) {
throw new IllegalArgumentException("Can't call equalTo() and endAt() combined");
}
}
/** This method validates that only one order by call has been made. */
private void validateNoOrderByCall() {
if (this.orderByCalled) {
throw new IllegalArgumentException("You can't combine multiple orderBy calls!");
}
}
/**
* Add a listener for changes in the data at this location. Each time time the data changes, your
* listener will be called with an immutable snapshot of the data.
*
* @param listener The listener to be called with changes
* @return A reference to the listener provided. Save this to remove the listener later.
*/
public ValueEventListener addValueEventListener(ValueEventListener listener) {
addEventRegistration(new ValueEventRegistration(repo, listener, getSpec()));
return listener;
}
/**
* Add a listener for child events occurring at this location. When child locations are added,
* removed, changed, or moved, the listener will be triggered for the appropriate event.
*
* @param listener The listener to be called with changes
* @return A reference to the listener provided. Save this to remove the listener later.
*/
public ChildEventListener addChildEventListener(ChildEventListener listener) {
addEventRegistration(new ChildEventRegistration(repo, listener, getSpec()));
return listener;
}
/**
* Add a listener for a single change in the data at this location. This listener will be
* triggered once with the value of the data at the location.
*
* @param listener The listener to be called with the data
*/
public void addListenerForSingleValueEvent(final ValueEventListener listener) {
addEventRegistration(
new ValueEventRegistration(
repo,
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// Removing the event listener will also prevent any further calls into
// onDataChange
removeEventListener(this);
listener.onDataChange(snapshot);
}
@Override
public void onCancelled(DatabaseError error) {
listener.onCancelled(error);
}
},
getSpec()));
}
/**
* Remove the specified listener from this location.
*
* @param listener The listener to remove
*/
public void removeEventListener(final ValueEventListener listener) {
if (listener == null) {
throw new NullPointerException("listener must not be null");
}
removeEventRegistration(new ValueEventRegistration(repo, listener, getSpec()));
}
/**
* Remove the specified listener from this location.
*
* @param listener The listener to remove
*/
public void removeEventListener(final ChildEventListener listener) {
if (listener == null) {
throw new NullPointerException("listener must not be null");
}
removeEventRegistration(new ChildEventRegistration(repo, listener, getSpec()));
}
private void removeEventRegistration(final EventRegistration registration) {
ZombieEventManager.getInstance().zombifyForRemove(registration);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.removeEventCallback(registration);
}
});
}
private void addEventRegistration(final EventRegistration listener) {
ZombieEventManager.getInstance().recordEventRegistration(listener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.addEventCallback(listener);
}
});
}
/**
* By calling {@code keepSynced(true)} on a location, the data for that location will
* automatically be downloaded and kept in sync, even when no listeners are attached for that
* location. Additionally, while a location is kept synced, it will not be evicted from the
* persistent disk cache.
*
* @param keepSynced Pass {@code true} to keep this location synchronized, pass {@code false} to
* stop synchronization.
* @since 2.3
*/
public void keepSynced(final boolean keepSynced) {
if (!this.path.isEmpty() && this.path.getFront().equals(ChildKey.getInfoKey())) {
throw new DatabaseException("Can't call keepSynced() on .info paths.");
}
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.keepSynced(getSpec(), keepSynced);
}
});
}
/* Removes all of the event listeners at this location */
/*public void removeAllEventListeners() {
Query.scheduleNow(new Runnable() {
@Override
public void run() {
repo.removeEventCallback(Query.this, null);
}
});
}*/
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to start at, inclusive
* @return A Query with the new constraint
*/
public Query startAt(String value) {
return startAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to start at, inclusive
* @return A Query with the new constraint
*/
public Query startAt(double value) {
return startAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to start at, inclusive
* @return A Query with the new constraint
* @since 2.0
*/
public Query startAt(boolean value) {
return startAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key greater than or equal to the given key.
*
* @param value The priority to start at, inclusive
* @param key The key to start at, inclusive
* @return A Query with the new constraint
*/
public Query startAt(String value, String key) {
Node node =
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
return startAt(node, key);
}
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key greater than or equal to the given key.
*
* @param value The priority to start at, inclusive
* @param key The key name to start at, inclusive
* @return A Query with the new constraint
*/
public Query startAt(double value, String key) {
return startAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key);
}
/**
* Create a query constrained to only return child nodes with a value greater than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key greater than or equal to the given key.
*
* @param value The priority to start at, inclusive
* @param key The key to start at, inclusive
* @return A Query with the new constraint
* @since 2.0
*/
public Query startAt(boolean value, String key) {
return startAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key);
}
private Query startAt(Node node, String key) {
Validation.validateNullableKey(key);
if (!(node.isLeafNode() || node.isEmpty())) {
throw new IllegalArgumentException("Can only use simple values for startAt()");
}
if (params.hasStart()) {
throw new IllegalArgumentException("Can't call startAt() or equalTo() multiple times");
}
ChildKey childKey = key != null ? ChildKey.fromString(key) : null;
QueryParams newParams = params.startAt(node, childKey);
validateLimit(newParams);
validateQueryEndpoints(newParams);
assert newParams.isValid();
return new Query(repo, path, newParams, orderByCalled);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to end at, inclusive
* @return A Query with the new constraint
*/
public Query endAt(String value) {
return endAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to end at, inclusive
* @return A Query with the new constraint
*/
public Query endAt(double value) {
return endAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default.
*
* @param value The value to end at, inclusive
* @return A Query with the new constraint
* @since 2.0
*/
public Query endAt(boolean value) {
return endAt(value, null);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key key less than or equal to the given key.
*
* @param value The value to end at, inclusive
* @param key The key to end at, inclusive
* @return A Query with the new constraint
*/
public Query endAt(String value, String key) {
Node node =
value != null ? new StringNode(value, PriorityUtilities.NullPriority()) : EmptyNode.Empty();
return endAt(node, key);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key less than or equal to the given key.
*
* @param value The value to end at, inclusive
* @param key The key to end at, inclusive
* @return A Query with the new constraint
*/
public Query endAt(double value, String key) {
return endAt(new DoubleNode(value, PriorityUtilities.NullPriority()), key);
}
/**
* Create a query constrained to only return child nodes with a value less than or equal to the
* given value, using the given orderBy directive or priority as default, and additionally only
* child nodes with a key less than or equal to the given key.
*
* @param value The value to end at, inclusive
* @param key The key to end at, inclusive
* @return A Query with the new constraint
* @since 2.0
*/
public Query endAt(boolean value, String key) {
return endAt(new BooleanNode(value, PriorityUtilities.NullPriority()), key);
}
private Query endAt(Node node, String key) {
Validation.validateNullableKey(key);
if (!(node.isLeafNode() || node.isEmpty())) {
throw new IllegalArgumentException("Can only use simple values for endAt()");
}
ChildKey childKey = key != null ? ChildKey.fromString(key) : null;
if (params.hasEnd()) {
throw new IllegalArgumentException("Can't call endAt() or equalTo() multiple times");
}
QueryParams newParams = params.endAt(node, childKey);
validateLimit(newParams);
validateQueryEndpoints(newParams);
assert newParams.isValid();
return new Query(repo, path, newParams, orderByCalled);
}
/**
* Create a query constrained to only return child nodes with the given value.
*
* @param value The value to query for
* @return A query with the new constraint
*/
public Query equalTo(String value) {
validateEqualToCall();
return this.startAt(value).endAt(value);
}
/**
* Create a query constrained to only return child nodes with the given value.
*
* @param value The value to query for
* @return A query with the new constraint
*/
public Query equalTo(double value) {
validateEqualToCall();
return this.startAt(value).endAt(value);
}
/**
* Create a query constrained to only return child nodes with the given value.
*
* @param value The value to query for
* @return A query with the new constraint
* @since 2.0
*/
public Query equalTo(boolean value) {
validateEqualToCall();
return this.startAt(value).endAt(value);
}
/**
* Create a query constrained to only return the child node with the given key and value. Note
* that there is at most one such child as names are unique.
*
* @param value The value to query for
* @param key The key of the child
* @return A query with the new constraint
*/
public Query equalTo(String value, String key) {
validateEqualToCall();
return this.startAt(value, key).endAt(value, key);
}
/**
* Create a query constrained to only return the child node with the given key and value. Note
* that there is at most one such child as keys are unique.
*
* @param value The value to query for
* @param key The key of the child
* @return A query with the new constraint
*/
public Query equalTo(double value, String key) {
validateEqualToCall();
return this.startAt(value, key).endAt(value, key);
}
/**
* Create a query constrained to only return the child node with the given key and value. Note
* that there is at most one such child as keys are unique.
*
* @param value The value to query for
* @param key The name of the child
* @return A query with the new constraint
*/
public Query equalTo(boolean value, String key) {
validateEqualToCall();
return this.startAt(value, key).endAt(value, key);
}
/**
* Create a query with limit and anchor it to the start of the window.
*
* @param limit The maximum number of child nodes to return
* @return A Query with the new constraint
* @since 2.0
*/
public Query limitToFirst(int limit) {
if (limit <= 0) {
throw new IllegalArgumentException("Limit must be a positive integer!");
}
if (params.hasLimit()) {
throw new IllegalArgumentException(
"Can't call limitToLast on query with previously set limit!");
}
return new Query(repo, path, params.limitToFirst(limit), orderByCalled);
}
/**
* Create a query with limit and anchor it to the end of the window.
*
* @param limit The maximum number of child nodes to return
* @return A Query with the new constraint
* @since 2.0
*/
public Query limitToLast(int limit) {
if (limit <= 0) {
throw new IllegalArgumentException("Limit must be a positive integer!");
}
if (params.hasLimit()) {
throw new IllegalArgumentException(
"Can't call limitToLast on query with previously set limit!");
}
return new Query(repo, path, params.limitToLast(limit), orderByCalled);
}
/**
* Create a query in which child nodes are ordered by the values of the specified path.
*
* @param path The path to the child node to use for sorting
* @return A Query with the new constraint
* @since 2.0
*/
public Query orderByChild(String path) {
if (path == null) {
throw new NullPointerException("Key can't be null");
}
if (path.equals("$key") || path.equals(".key")) {
throw new IllegalArgumentException(
"Can't use '" + path + "' as path, please use orderByKey() instead!");
}
if (path.equals("$priority") || path.equals(".priority")) {
throw new IllegalArgumentException(
"Can't use '" + path + "' as path, please use orderByPriority() instead!");
}
if (path.equals("$value") || path.equals(".value")) {
throw new IllegalArgumentException(
"Can't use '" + path + "' as path, please use orderByValue() instead!");
}
Validation.validatePathString(path);
validateNoOrderByCall();
Path indexPath = new Path(path);
if (indexPath.size() == 0) {
throw new IllegalArgumentException("Can't use empty path, use orderByValue() instead!");
}
Index index = new PathIndex(indexPath);
return new Query(repo, this.path, params.orderBy(index), true);
}
/**
* Create a query in which child nodes are ordered by their priorities.
*
* @return A Query with the new constraint
* @since 2.0
*/
public Query orderByPriority() {
validateNoOrderByCall();
QueryParams newParams = params.orderBy(PriorityIndex.getInstance());
validateQueryEndpoints(newParams);
return new Query(repo, path, newParams, true);
}
/**
* Create a query in which child nodes are ordered by their keys.
*
* @return A Query with the new constraint
* @since 2.0
*/
public Query orderByKey() {
validateNoOrderByCall();
QueryParams newParams = this.params.orderBy(KeyIndex.getInstance());
validateQueryEndpoints(newParams);
return new Query(repo, path, newParams, true);
}
/**
* Create a query in which nodes are ordered by their value
*
* @return A Query with the new constraint
* @since 2.2
*/
public Query orderByValue() {
validateNoOrderByCall();
return new Query(repo, path, params.orderBy(ValueIndex.getInstance()), true);
}
/**
* @return A DatabaseReference to this location
*/
public DatabaseReference getRef() {
return new DatabaseReference(repo, getPath());
}
// Need to hide these...
/**
* <strong>For internal use</strong>
*
* @hide
* @return The path to this location
*/
public Path getPath() {
return path;
}
/**
* <strong>For internal use</strong>
*
* @hide
* @return The repo
*/
public Repo getRepo() {
return repo;
}
/**
* <strong>For internal use</strong>
*
* @hide
* @return The constraints
*/
public QuerySpec getSpec() {
return new QuerySpec(path, params);
}
}