|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2021 Nitrite author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.dizitart.no2.integration.collection; |
| 18 | + |
| 19 | +import org.dizitart.no2.Nitrite; |
| 20 | +import org.dizitart.no2.collection.NitriteCollection; |
| 21 | +import org.dizitart.no2.index.IndexOptions; |
| 22 | +import org.dizitart.no2.index.IndexType; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.util.Collections; |
| 26 | + |
| 27 | +import static org.dizitart.no2.collection.Document.createDocument; |
| 28 | +import static org.dizitart.no2.filters.Filter.and; |
| 29 | +import static org.dizitart.no2.filters.FluentFilter.where; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +/** |
| 33 | + * A query's results must not depend on which indexes happen to exist. |
| 34 | + * {@code field.eq(x)} / {@code field.in(..)} on an array (list) field is |
| 35 | + * defined by the index path as element containment; before the fix the |
| 36 | + * collection-scan path did whole-value equality, so an indexed array-eq that |
| 37 | + * the planner relegated to a collection scan (e.g. when a range filter on |
| 38 | + * another field claimed the index) silently matched nothing. |
| 39 | + */ |
| 40 | +public class ArrayFieldIndexIndependenceTest { |
| 41 | + |
| 42 | + // days 0 & 2 tagged "todo", days 1 & 3 tagged "misc"; created_at = day*1000 |
| 43 | + private NitriteCollection seed(boolean tagsIndex, boolean createdIndex) { |
| 44 | + Nitrite db = Nitrite.builder().fieldSeparator(".").openOrCreate(); |
| 45 | + NitriteCollection c = db.getCollection("array_index_independence"); |
| 46 | + c.remove(org.dizitart.no2.filters.Filter.ALL); |
| 47 | + if (tagsIndex) c.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "tags"); |
| 48 | + if (createdIndex) c.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "created_at"); |
| 49 | + for (long day = 0; day < 4; day++) { |
| 50 | + String tag = (day % 2 == 0) ? "todo" : "misc"; |
| 51 | + c.insert(createDocument("created_at", day * 1000L).put("tags", Collections.singletonList(tag))); |
| 52 | + } |
| 53 | + return c; |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void arrayEqMatchesByContainmentWithoutIndex() { |
| 58 | + NitriteCollection c = seed(false, false); |
| 59 | + assertEquals(2, c.find(where("tags").eq("todo")).size()); |
| 60 | + assertEquals(0, c.find(where("tags").eq("nope")).size()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void arrayEqResultIsIndexIndependent() { |
| 65 | + assertEquals(seed(false, false).find(where("tags").eq("todo")).size(), |
| 66 | + seed(true, false).find(where("tags").eq("todo")).size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void arrayEqCombinedWithRangeWhenBothFieldsIndexed() { |
| 71 | + NitriteCollection c = seed(true, true); |
| 72 | + // day 2 is the only "todo" inside [1000,3000] |
| 73 | + assertEquals(1, c.find(and(where("tags").eq("todo"), where("created_at").between(1000L, 3000L))).size()); |
| 74 | + assertEquals(1, c.find(and(where("created_at").between(1000L, 3000L), where("tags").eq("todo"))).size()); |
| 75 | + assertEquals(1, c.find(and(where("tags").eq("todo"), |
| 76 | + where("created_at").gte(1000L), where("created_at").lte(3000L))).size()); |
| 77 | + assertEquals(1, c.find(and(where("tags").eq("todo"), where("created_at").gte(1000L))).size()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void arrayInMatchesByContainmentOnCollectionScan() { |
| 82 | + NitriteCollection c = seed(false, true); |
| 83 | + assertEquals(2, c.find(where("tags").in("todo", "other")).size()); |
| 84 | + assertEquals(1, c.find(and(where("tags").in("todo"), |
| 85 | + where("created_at").between(1000L, 3000L))).size()); |
| 86 | + } |
| 87 | +} |
0 commit comments