Skip to content

Commit 2e0b69e

Browse files
authored
Fix for #990 (#991)
* #990 is fixed * test case updated * remove migration test for in-memory mvstore adapter * test cases fixed
1 parent c0404b5 commit 2e0b69e

File tree

9 files changed

+82
-29
lines changed

9 files changed

+82
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ captures/
451451
test.log
452452
/build
453453
!no2-old.db
454+
!no2-v3.db
454455
.diffblue
455456
infer-out
456457
secring.gpg

nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/MVStoreUtils.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
package org.dizitart.no2.mvstore;
1818

1919
import lombok.extern.slf4j.Slf4j;
20-
import org.dizitart.no2.common.meta.Attributes;
2120
import org.dizitart.no2.exceptions.InvalidOperationException;
2221
import org.dizitart.no2.exceptions.NitriteIOException;
2322
import org.dizitart.no2.mvstore.compat.v1.UpgradeUtil;
24-
import org.h2.mvstore.MVMap;
2523
import org.h2.mvstore.MVStore;
2624
import org.h2.mvstore.MVStoreException;
2725

2826
import java.io.File;
27+
import java.util.Map;
2928

30-
import static org.dizitart.no2.common.Constants.META_MAP_NAME;
31-
import static org.dizitart.no2.common.Constants.STORE_INFO;
3229
import static org.dizitart.no2.common.util.StringUtils.isNullOrEmpty;
3330

3431
/**
@@ -38,6 +35,10 @@
3835
@Slf4j(topic = "nitrite-mvstore")
3936
@SuppressWarnings("ALL")
4037
class MVStoreUtils {
38+
private static final String OLD_DATABASE_FORMAT = "Old database format detected.";
39+
private static final String OLD_STORE_FORMAT = "The write format 1 is smaller than the supported format";
40+
private static final Integer OLD_DATABASE_FORMAT_VERSION = 29062024;
41+
4142
private MVStoreUtils() {
4243
}
4344

@@ -48,7 +49,10 @@ static MVStore openOrCreate(MVStoreConfig storeConfig) {
4849
File dbFile = !isNullOrEmpty(storeConfig.filePath()) ? new File(storeConfig.filePath()) : null;
4950
try {
5051
store = builder.open();
51-
testForMigration(store);
52+
if (dbFile != null) {
53+
// if the store is file based, test for migration
54+
testForMigration(store);
55+
}
5256
} catch (MVStoreException me) {
5357
if (me.getMessage().contains("file is locked")) {
5458
throw new NitriteIOException("Database is already opened in other process");
@@ -105,7 +109,7 @@ static MVStore openOrCreate(MVStoreConfig storeConfig) {
105109
}
106110

107111
private static boolean isCompatibilityError(Exception e) {
108-
return e.getMessage().contains("The write format 1 is smaller than the supported format");
112+
return e.getMessage().contains(OLD_DATABASE_FORMAT) || e.getMessage().contains(OLD_STORE_FORMAT);
109113
}
110114

111115
private static MVStore.Builder createBuilder(MVStoreConfig mvStoreConfig) {
@@ -210,22 +214,23 @@ private static void switchFiles(File newFile, File orgFile) {
210214

211215
private static void testForMigration(MVStore store) {
212216
if (store != null) {
213-
if (store.hasMap(STORE_INFO)) {
214-
return;
217+
Map<String, Object> storeHeader = store.getStoreHeader();
218+
if (storeHeader == null || storeHeader.isEmpty()) {
219+
throw new MVStoreException(OLD_DATABASE_FORMAT_VERSION, OLD_DATABASE_FORMAT);
215220
}
216221

217-
MVStore.TxCounter txCounter = store.registerVersionUsage();
218-
MVMap<String, Attributes> metaMap = store.openMap(META_MAP_NAME);
219-
try {
220-
// fire one operation to trigger compatibility issue
221-
// if no exception thrown, then the database is compatible
222-
metaMap.remove("MigrationTest");
223-
} catch (IllegalStateException e) {
224-
store.close();
225-
throw e;
226-
} finally {
227-
if (!store.isClosed()) {
228-
store.deregisterVersionUsage(txCounter);
222+
if (storeHeader.containsKey("format")) {
223+
Object rawFormatValue = storeHeader.get("format");
224+
if (rawFormatValue instanceof Integer) {
225+
int format = (int) rawFormatValue;
226+
if (format < 3) {
227+
throw new MVStoreException(OLD_DATABASE_FORMAT_VERSION, OLD_DATABASE_FORMAT);
228+
}
229+
} else {
230+
int format = Integer.parseInt((String) storeHeader.get("format"));
231+
if (format < 3) {
232+
throw new MVStoreException(OLD_DATABASE_FORMAT_VERSION, OLD_DATABASE_FORMAT);
233+
}
229234
}
230235
}
231236
}

nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/UpgradeUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ private static Document document(Compat.Document value) {
190190
Document document = Document.createDocument();
191191
for (Map.Entry<String, Object> entry : value.entrySet()) {
192192
Object val = entry.getValue();
193-
Object migratedVal = migrateValue(val);
193+
Object migratedVal;
194+
if (DOC_ID.equals(entry.getKey())) {
195+
migratedVal = String.valueOf(val);
196+
} else {
197+
migratedVal = migrateValue(val);
198+
}
194199
document.put(entry.getKey(), migratedVal);
195200
}
196201
return document;

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/NitriteTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,44 @@ public void testIssue193() throws InterruptedException {
436436
pool.shutdown();
437437
}
438438

439+
@Test
440+
public void testUpdateDocumentsFromVersion3() throws IOException {
441+
if (Files.exists(Paths.get(System.getProperty("java.io.tmpdir") + File.separator + "no2-v3.db"))) {
442+
Files.delete(Paths.get(System.getProperty("java.io.tmpdir") + File.separator + "no2-v3.db"));
443+
}
444+
445+
InputStream stream = ClassLoader.getSystemResourceAsStream("no2-v3.db");
446+
if (stream == null) {
447+
stream = ClassLoader.getSystemClassLoader().getResourceAsStream("no2-v3.db");
448+
}
449+
assert stream != null;
450+
451+
Files.copy(stream, Paths.get(System.getProperty("java.io.tmpdir") + File.separator + "no2-v3.db"));
452+
453+
String oldDbFile = System.getProperty("java.io.tmpdir") + File.separator + "no2-v3.db";
454+
Nitrite db = TestUtil.createDb(oldDbFile);
455+
NitriteCollection testCollection = db.getCollection("test");
456+
Document document = testCollection.find().firstOrNull();
457+
document.put("name", "new-name");
458+
459+
boolean exception = false;
460+
try {
461+
testCollection.update(document);
462+
} catch (Exception e) {
463+
exception = true;
464+
log.error("Error while updating document", e);
465+
}
466+
assertFalse(exception);
467+
468+
db.close();
469+
470+
try {
471+
Files.deleteIfExists(Paths.get(oldDbFile));
472+
} catch (IOException e) {
473+
log.error("Error while deleting db", e);
474+
}
475+
}
476+
439477
@Test
440478
public void testReadCompatibility() throws IOException {
441479
// ******* Old DB Creation Code Start *********
@@ -531,7 +569,11 @@ public void testReadCompatibility() throws IOException {
531569

532570
db.close();
533571

534-
//TODO: CHeck reopen with repo. That should also fails too.
572+
try {
573+
Files.deleteIfExists(Paths.get(oldDbFile));
574+
} catch (IOException e) {
575+
log.error("Error while deleting db", e);
576+
}
535577
}
536578

537579
@Test

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/integration/collection/BaseCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class BaseCollectionTest {
6464
public Retry retry = new Retry(3);
6565

6666
@Parameterized.Parameters(name = "InMemory = {0}, Secured = {1}, " +
67-
"Compressed = {2}, AutoCommit = {3}, AutoCompact = {4}")
67+
"Compressed = {2}, AutoCommit = {3}")
6868
public static Collection<Object[]> data() {
6969
return Arrays.asList(new Object[][]{
7070
{false, false, false, false},

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/integration/collection/CollectionCompoundIndexNegativeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testCreateUniqueMultiKeyIndexOnArray() {
5757
public void testCreateOnInvalidField() {
5858
insert();
5959
// multiple null value will be created
60-
collection.createIndex( "my-value", "lastName");
60+
collection.createIndex("my-value", "lastName");
6161
}
6262

6363
@Test(expected = IndexingException.class)

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/MVStoreUtilsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public void testOpenOrCreate() {
3333
assertEquals(0, actualOpenOrCreateResult.getUnsavedMemory());
3434
assertEquals(0, actualOpenOrCreateResult.getStoreVersion());
3535
assertEquals(0, actualOpenOrCreateResult.getRetentionTime());
36-
assertEquals(2, actualOpenOrCreateResult.getMetaMap().size());
36+
assertEquals(0, actualOpenOrCreateResult.getMetaMap().size());
3737
assertEquals(Long.MAX_VALUE, actualOpenOrCreateResult.getMaxPageSize());
38-
assertEquals(1, actualOpenOrCreateResult.getMapNames().size());
38+
assertEquals(0, actualOpenOrCreateResult.getMapNames().size());
3939
assertEquals(48, actualOpenOrCreateResult.getKeysPerPage());
4040
assertEquals(0, actualOpenOrCreateResult.getAutoCommitMemory());
4141
}
@@ -50,9 +50,9 @@ public void testOpenOrCreate2() {
5050
assertEquals(0, actualOpenOrCreateResult.getUnsavedMemory());
5151
assertEquals(0, actualOpenOrCreateResult.getStoreVersion());
5252
assertEquals(0, actualOpenOrCreateResult.getRetentionTime());
53-
assertEquals(2, actualOpenOrCreateResult.getMetaMap().size());
53+
assertEquals(0, actualOpenOrCreateResult.getMetaMap().size());
5454
assertEquals(Long.MAX_VALUE, actualOpenOrCreateResult.getMaxPageSize());
55-
assertEquals(1, actualOpenOrCreateResult.getMapNames().size());
55+
assertEquals(0, actualOpenOrCreateResult.getMapNames().size());
5656
assertEquals(48, actualOpenOrCreateResult.getKeysPerPage());
5757
assertEquals(0, actualOpenOrCreateResult.getAutoCommitMemory());
5858
}

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/mvstore/NitriteMVStoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testOpenOrCreate() {
3838
nitriteMVStore.openOrCreate();
3939
assertFalse(nitriteMVStore.isReadOnly());
4040
assertFalse(nitriteMVStore.isClosed());
41-
assertTrue(nitriteMVStore.hasUnsavedChanges());
41+
assertFalse(nitriteMVStore.hasUnsavedChanges());
4242
}
4343

4444
@Test
16 KB
Binary file not shown.

0 commit comments

Comments
 (0)