|
| 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.collection.operation; |
| 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.common.Fields; |
| 24 | +import org.dizitart.no2.mvstore.MVStoreModule; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.util.UUID; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +/** |
| 36 | + * The index dirty marker is crash-recovery state, so it has to survive a restart. |
| 37 | + */ |
| 38 | +public class IndexDirtyMarkerDurabilityTest { |
| 39 | + private static final String COLLECTION = "dirty-marker-test"; |
| 40 | + private static final Fields FIELDS = Fields.withNames("value"); |
| 41 | + |
| 42 | + private String filePath; |
| 43 | + private Nitrite db; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + filePath = System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID() + ".db"; |
| 48 | + db = openDb(); |
| 49 | + |
| 50 | + NitriteCollection collection = db.getCollection(COLLECTION); |
| 51 | + collection.insert(Document.createDocument("value", 1)); |
| 52 | + collection.createIndex("value"); |
| 53 | + |
| 54 | + // make sure the meta map is on disk and its page is clean again, so that only a |
| 55 | + // later write of the marker itself can change what the file holds |
| 56 | + db.commit(); |
| 57 | + } |
| 58 | + |
| 59 | + @After |
| 60 | + public void tearDown() { |
| 61 | + if (db != null && !db.isClosed()) { |
| 62 | + db.close(); |
| 63 | + } |
| 64 | + File file = new File(filePath); |
| 65 | + if (file.exists() && !file.delete()) { |
| 66 | + file.deleteOnExit(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private Nitrite openDb() { |
| 71 | + return Nitrite.builder() |
| 72 | + .loadModule(MVStoreModule.withConfig().filePath(filePath).build()) |
| 73 | + .openOrCreate(); |
| 74 | + } |
| 75 | + |
| 76 | + private boolean reopenAndReadDirtyMarker() { |
| 77 | + db.close(); |
| 78 | + db = openDb(); |
| 79 | + try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) { |
| 80 | + return indexManager.isDirtyIndex(FIELDS); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testMarkerSurvivesRestartWhenIndexingStarted() { |
| 86 | + try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) { |
| 87 | + indexManager.beginIndexing(FIELDS); |
| 88 | + } |
| 89 | + |
| 90 | + assertTrue("an index that was left mid-build must still read as dirty after a restart", |
| 91 | + reopenAndReadDirtyMarker()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testMarkerIsClearedAcrossRestartWhenIndexingCompleted() { |
| 96 | + try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) { |
| 97 | + indexManager.beginIndexing(FIELDS); |
| 98 | + indexManager.endIndexing(FIELDS); |
| 99 | + } |
| 100 | + |
| 101 | + assertFalse("a completed index must not read as dirty after a restart", |
| 102 | + reopenAndReadDirtyMarker()); |
| 103 | + } |
| 104 | +} |
0 commit comments