|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.model; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.util.Assert.hardAssert; |
| 18 | + |
| 19 | +import com.google.firebase.firestore.core.FieldFilter; |
| 20 | +import com.google.firebase.firestore.core.Filter; |
| 21 | +import com.google.firebase.firestore.core.OrderBy; |
| 22 | +import com.google.firebase.firestore.core.Target; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +/** |
| 31 | + * A light query planner for Firestore. |
| 32 | + * |
| 33 | + * <p>This class matches a {@link FieldIndex} against a Firestore Query {@link Target}. It |
| 34 | + * determines whether a given index can be used to serve the specified target. |
| 35 | + * |
| 36 | + * <p>Unlike the backend, the SDK only maintains two different index kinds and does not distinguish |
| 37 | + * between ascending and descending indices. Instead of ordering query results by their index order, |
| 38 | + * the SDK re-orders all query results locally, which reduces the number of indices it needs to |
| 39 | + * maintain. |
| 40 | + * |
| 41 | + * <p>The following table showcases some possible index configurations: |
| 42 | + * |
| 43 | + * <table> |
| 44 | + * <thead> |
| 45 | + * <tr> |
| 46 | + * <td>Query</td> |
| 47 | + * <td>Index</td> |
| 48 | + * </tr> |
| 49 | + * </thead> |
| 50 | + * <tbody> |
| 51 | + * <tr> |
| 52 | + * <td>where("a", "==", "a").where("b", "==", "b")</td> |
| 53 | + * <td>a ORDERED, b ORDERED</td> |
| 54 | + * </tr> |
| 55 | + * <tr> |
| 56 | + * <td>where("a", "==", "a").where("b", "==", "b")</td> |
| 57 | + * <td>a ORDERED</td> |
| 58 | + * </tr> |
| 59 | + * <tr> |
| 60 | + * <td>where("a", "==", "a").where("b", "==", "b")</td> |
| 61 | + * <td>b ORDERED</td> |
| 62 | + * </tr> |
| 63 | + * <tr> |
| 64 | + * <td>where("a", ">=", "a").orderBy("a").orderBy("b")</td> |
| 65 | + * <td>a ORDERED, b ORDERED</td> |
| 66 | + * </tr> |
| 67 | + * <tr> |
| 68 | + * <td>where("a", ">=", "a").orderBy("a").orderBy("b")</td> |
| 69 | + * <td>a ORDERED</td> |
| 70 | + * </tr> |
| 71 | + * <tr> |
| 72 | + * <td>where("a", "array-contains", "a").orderBy("b")</td> |
| 73 | + * <td>a CONTAINS, b ORDERED</td> |
| 74 | + * </tr> |
| 75 | + * <tr> |
| 76 | + * <td>where("a", "array-contains", "a").orderBy("b")</td> |
| 77 | + * <td>a CONTAINS</td> |
| 78 | + * </tr> |
| 79 | + * </tbody> |
| 80 | + * </table> |
| 81 | + */ |
| 82 | +public class TargetIndexMatcher { |
| 83 | + // The collection ID of the query target. |
| 84 | + private final String collectionId; |
| 85 | + |
| 86 | + // The list of filters per field. A target can have duplicate filters for a field. |
| 87 | + private final Map<FieldPath, List<FieldFilter>> fieldFilterFields = new HashMap<>(); |
| 88 | + |
| 89 | + // The list of orderBy fields in the query target. |
| 90 | + private final Set<FieldPath> orderByFields = new HashSet<>(); |
| 91 | + |
| 92 | + public TargetIndexMatcher(Target target) { |
| 93 | + collectionId = |
| 94 | + target.getCollectionGroup() != null |
| 95 | + ? target.getCollectionGroup() |
| 96 | + : target.getPath().getLastSegment(); |
| 97 | + |
| 98 | + for (Filter filter : target.getFilters()) { |
| 99 | + hardAssert(filter instanceof FieldFilter, "Only FieldFilters are supported"); |
| 100 | + List<FieldFilter> currentFilters = fieldFilterFields.get(filter.getField()); |
| 101 | + if (currentFilters == null) { |
| 102 | + currentFilters = new ArrayList<>(); |
| 103 | + fieldFilterFields.put(filter.getField(), currentFilters); |
| 104 | + } |
| 105 | + currentFilters.add((FieldFilter) filter); |
| 106 | + } |
| 107 | + |
| 108 | + for (OrderBy orderBy : target.getOrderBy()) { |
| 109 | + orderByFields.add(orderBy.getField()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns whether the index can be used to serve the TargetIndexMatcher's target. |
| 115 | + * |
| 116 | + * @throws AssertionError if the index is for a different collection |
| 117 | + */ |
| 118 | + public boolean servedByIndex(FieldIndex index) { |
| 119 | + hardAssert(index.getCollectionId().equals(collectionId), "Collection IDs do not match"); |
| 120 | + for (int i = 0; i < index.segmentCount(); ++i) { |
| 121 | + if (!canUseSegment(index.getSegment(i))) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + private boolean canUseSegment(FieldIndex.Segment segment) { |
| 129 | + List<FieldFilter> filters = fieldFilterFields.get(segment.getFieldPath()); |
| 130 | + if (filters != null) { |
| 131 | + for (FieldFilter filter : filters) { |
| 132 | + switch (filter.getOperator()) { |
| 133 | + case ARRAY_CONTAINS: |
| 134 | + case ARRAY_CONTAINS_ANY: |
| 135 | + if (segment.getKind().equals(FieldIndex.Segment.Kind.CONTAINS)) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + break; |
| 139 | + default: |
| 140 | + if (segment.getKind().equals(FieldIndex.Segment.Kind.ORDERED)) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return orderByFields.contains(segment.getFieldPath()) |
| 148 | + && segment.getKind().equals(FieldIndex.Segment.Kind.ORDERED); |
| 149 | + } |
| 150 | +} |
0 commit comments