|
| 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 | + |
| 18 | +package org.dizitart.no2.integration.collection; |
| 19 | + |
| 20 | +import org.dizitart.no2.Nitrite; |
| 21 | +import org.dizitart.no2.collection.Document; |
| 22 | +import org.dizitart.no2.collection.NitriteCollection; |
| 23 | +import org.dizitart.no2.filters.FluentFilter; |
| 24 | +import org.dizitart.no2.index.IndexOptions; |
| 25 | +import org.dizitart.no2.index.IndexType; |
| 26 | +import org.dizitart.no2.mvstore.MVStoreModule; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 32 | +import java.util.concurrent.CyclicBarrier; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | + |
| 36 | +/** |
| 37 | + * Regression test for <a href="https://github.com/nitrite/nitrite-java/issues/1315">Issue 1315</a>. |
| 38 | + * <p> |
| 39 | + * On 5.3.0 every close left an empty legacy index map behind, and the first finds on a |
| 40 | + * reopened file database raced to migrate and drop it, failing with a |
| 41 | + * {@code NullPointerException} in {@code NitriteMVStore.removeMap}. Fixed by #1295 and #1309. |
| 42 | + */ |
| 43 | +public class Issue1315Test { |
| 44 | + |
| 45 | + private static Nitrite open(File file) { |
| 46 | + return Nitrite.builder() |
| 47 | + .loadModule(MVStoreModule.withConfig().filePath(file.getPath()).build()) |
| 48 | + .openOrCreate(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void firstUseOfANonUniqueIndexFromSeveralThreads() throws Exception { |
| 53 | + File file = File.createTempFile("nitrite-legacy-race", ".db"); |
| 54 | + file.delete(); |
| 55 | + try { |
| 56 | + Nitrite db = open(file); |
| 57 | + NitriteCollection items = db.getCollection("items"); |
| 58 | + items.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "state"); |
| 59 | + items.insert(Document.createDocument("state", "new")); |
| 60 | + db.close(); |
| 61 | + |
| 62 | + int threads = 4, openings = 100; |
| 63 | + List<Throwable> failures = new CopyOnWriteArrayList<>(); |
| 64 | + for (int opening = 0; opening < openings; opening++) { |
| 65 | + Nitrite reopened = open(file); |
| 66 | + NitriteCollection collection = reopened.getCollection("items"); |
| 67 | + CyclicBarrier start = new CyclicBarrier(threads); |
| 68 | + Thread[] workers = new Thread[threads]; |
| 69 | + for (int i = 0; i < threads; i++) { |
| 70 | + workers[i] = new Thread(() -> { |
| 71 | + try { |
| 72 | + start.await(); |
| 73 | + assertEquals(1, collection.find(FluentFilter.where("state").eq("new")).toList().size()); |
| 74 | + } catch (Throwable t) { |
| 75 | + failures.add(t); |
| 76 | + } |
| 77 | + }); |
| 78 | + workers[i].start(); |
| 79 | + } |
| 80 | + for (Thread worker : workers) worker.join(); |
| 81 | + reopened.close(); |
| 82 | + } |
| 83 | + assertEquals(failures.toString(), 0, failures.size()); |
| 84 | + } finally { |
| 85 | + file.delete(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments