|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2020. 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.collection; |
| 18 | + |
| 19 | +import org.dizitart.no2.Nitrite; |
| 20 | +import org.dizitart.no2.filters.Filter; |
| 21 | +import org.dizitart.no2.index.IndexOptions; |
| 22 | +import org.dizitart.no2.index.IndexType; |
| 23 | +import org.dizitart.no2.store.NitriteMap; |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.dizitart.no2.filters.FluentFilter.where; |
| 31 | +import static org.dizitart.no2.integration.TestUtil.createDb; |
| 32 | +import static org.junit.Assert.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * An index lookup and the fetch of the documents it names are not atomic: a document removed in |
| 36 | + * between must simply be absent from the result, never a null row that a residual filter, or the |
| 37 | + * caller, dereferences. |
| 38 | + * <p> |
| 39 | + * The window is reproduced by removing the document from the collection's map behind the index's |
| 40 | + * back, which is exactly what a concurrent remove looks like to a reader that already holds the ids. |
| 41 | + */ |
| 42 | +public class IndexScanRemovedDocumentTest { |
| 43 | + private Nitrite db; |
| 44 | + private NitriteCollection collection; |
| 45 | + private NitriteId removedId; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + db = createDb(); |
| 50 | + collection = db.getCollection("race"); |
| 51 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "group"); |
| 52 | + for (int n = 1; n <= 3; n++) { |
| 53 | + collection.insert(Document.createDocument("group", "a").put("n", n)); |
| 54 | + } |
| 55 | + removedId = collection.find(where("n").eq(2)).firstOrNull().getId(); |
| 56 | + |
| 57 | + NitriteMap<NitriteId, Document> map = db.getStore().openMap("race", NitriteId.class, Document.class); |
| 58 | + assertNotNull(map.remove(removedId)); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void tearDown() { |
| 63 | + db.close(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testIndexScanWithResidualFilterSkipsRemovedDocument() { |
| 68 | + List<Document> found = collection.find(where("group").eq("a").and(where("n").gte(1))).toList(); |
| 69 | + assertEquals(2, found.size()); |
| 70 | + assertTrue(found.stream().noneMatch(d -> d == null)); |
| 71 | + assertTrue(found.stream().noneMatch(d -> d.getId().equals(removedId))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testIndexScanWithoutResidualFilterSkipsRemovedDocument() { |
| 76 | + List<Document> found = collection.find(where("group").eq("a")).toList(); |
| 77 | + assertEquals(2, found.size()); |
| 78 | + assertTrue(found.stream().noneMatch(d -> d == null)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testIndexScanSizeExcludesRemovedDocument() { |
| 83 | + assertEquals(2, collection.find(where("group").eq("a").and(where("n").gte(1))).size()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testByIdOfRemovedDocumentIsEmpty() { |
| 88 | + assertNull(collection.find(Filter.byId(removedId)).firstOrNull()); |
| 89 | + assertNull(collection.getById(removedId)); |
| 90 | + } |
| 91 | +} |
0 commit comments