Skip to content

Commit 4860613

Browse files
committed
Clean up.
1 parent 6e62711 commit 4860613

File tree

5 files changed

+50
-62
lines changed

5 files changed

+50
-62
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/core/CompositeFilter.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,10 @@
2727
public class CompositeFilter extends Filter {
2828
private final List<Filter> filters;
2929
private final Operator operator;
30-
private boolean isFlatConjunction;
3130

3231
public CompositeFilter(List<Filter> filters, Operator operator) {
3332
this.filters = filters;
3433
this.operator = operator;
35-
36-
// Memoize whether this is a flat conjunction filter.
37-
isFlatConjunction = true;
38-
if (operator != Operator.AND) {
39-
isFlatConjunction = false;
40-
} else {
41-
for (Filter filter : filters) {
42-
if (filter instanceof CompositeFilter) {
43-
isFlatConjunction = false;
44-
}
45-
}
46-
}
4734
}
4835

4936
@Override
@@ -87,13 +74,6 @@ public boolean isDisjunction() {
8774
return operator == Operator.OPERATOR_UNSPECIFIED;
8875
}
8976

90-
/**
91-
* Returns true if this filter is a conjunction of field filters only. Returns false otherwise.
92-
*/
93-
public boolean isFlatConjunction() {
94-
return isFlatConjunction;
95-
}
96-
9777
/**
9878
* Performs a depth-first search to find and return the first FieldFilter in the composite filter
9979
* that satisfies the condition. Returns {@code null} if none of the FieldFilters satisfy the

firebase-firestore/src/main/java/com/google/firebase/firestore/core/DnfTransform.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.google.firebase.firestore.core;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
/**
7+
* Provides utility functions that help with boolean logic transformations needed for handling
8+
* complex filters used in queries.
9+
*/
10+
public class LogicUtils {
11+
/**
12+
* Given a composite filter, returns the list of terms in its disjunctive normal form.
13+
*
14+
* <p>Each element in the return value is one term of the resulting DNF.
15+
*
16+
* <p>For instance, for the input: (A || B) && C
17+
*
18+
* <p>The DNF form is: (A && C) || (B && C)
19+
*
20+
* <p>The return value is a list with two elements:
21+
*
22+
* <p>The first element is a composite filter that performs (A && C).
23+
*
24+
* <p>The second element is a composite filter that performs (B && C).
25+
*
26+
* @param filter the composite filter to calculate DNF transform for.
27+
* @return the terms in the DNF transform.
28+
*/
29+
public static List<Filter> DnfTransform(CompositeFilter filter) {
30+
// TODO(orquery): write the DNF transform algorithm here.
31+
// For now, assume all inputs are of the form AND(A, B, ...). Therefore the resulting DNF form
32+
// is the same as the input.
33+
if (filter.getFilters().isEmpty()) {
34+
return Collections.emptyList();
35+
}
36+
return Collections.singletonList(filter);
37+
}
38+
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteIndexManager.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
import static com.google.firebase.firestore.util.Util.repeatSequence;
2222
import static java.lang.Math.max;
2323

24+
import android.text.TextUtils;
2425
import androidx.annotation.Nullable;
2526
import com.google.common.collect.ObjectArrays;
2627
import com.google.firebase.Timestamp;
2728
import com.google.firebase.database.collection.ImmutableSortedMap;
2829
import com.google.firebase.firestore.auth.User;
2930
import com.google.firebase.firestore.core.Bound;
3031
import com.google.firebase.firestore.core.CompositeFilter;
31-
import com.google.firebase.firestore.core.DnfTransform;
3232
import com.google.firebase.firestore.core.FieldFilter;
3333
import com.google.firebase.firestore.core.Filter;
34+
import com.google.firebase.firestore.core.LogicUtils;
3435
import com.google.firebase.firestore.core.Target;
3536
import com.google.firebase.firestore.index.DirectionalIndexByteEncoder;
3637
import com.google.firebase.firestore.index.FirestoreIndexValueWriter;
@@ -323,7 +324,7 @@ private List<Target> getSubTargets(Target target) {
323324
} else {
324325
// There is an implicit AND operation between all the filters stored in the target.
325326
List<Filter> dnf =
326-
DnfTransform.get(
327+
LogicUtils.DnfTransform(
327328
new CompositeFilter(
328329
target.getFilters(), StructuredQuery.CompositeFilter.Operator.AND));
329330
for (Filter term : dnf) {
@@ -478,20 +479,16 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
478479
bindings.addAll(Arrays.asList(subQueryAndBindings).subList(1, subQueryAndBindings.length));
479480
}
480481

481-
// Construct "subQuery1 UNION subQuery2 UNION ... LIMIT N"
482-
// If there's only one subQuery, just execute the one subQuery.
483-
StringBuilder queryStatement = new StringBuilder();
484-
for (int i = 0; i < subQueries.size(); ++i) {
485-
if (i > 0) {
486-
queryStatement.append(" UNION ");
487-
}
488-
queryStatement.append(subQueries.get(i));
489-
}
490-
if (subQueries.size() > 1) {
491-
queryStatement.append("LIMIT ").append(target.getLimit());
492-
}
482+
String queryString =
483+
subQueries.size() == 1
484+
?
485+
// If there's only one subQuery, just execute the one subQuery.
486+
subQueries.get(0)
487+
:
488+
// Construct "subQuery1 UNION subQuery2 UNION ... LIMIT N"
489+
TextUtils.join(" UNION ", subQueries) + " LIMIT " + target.getLimit();
493490

494-
SQLitePersistence.Query query = db.query(queryStatement.toString()).binding(bindings.toArray());
491+
SQLitePersistence.Query query = db.query(queryString).binding(bindings.toArray());
495492

496493
Set<DocumentKey> result = new HashSet<>();
497494
query.forEach(

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteIndexManagerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static com.google.firebase.firestore.testutil.TestUtil.version;
3232
import static com.google.firebase.firestore.testutil.TestUtil.wrap;
3333
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertNotNull;
3534
import static org.junit.Assert.assertNull;
3635
import static org.junit.Assert.assertTrue;
3736

0 commit comments

Comments
 (0)