Skip to content

Commit 352af93

Browse files
authored
fix(gh-1162): ensure NitriteId serialization is compatible with previous versions (#1164)
1 parent 7f6a037 commit 352af93

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ test.log
452452
/build
453453
!no2-old.db
454454
!no2-v3.db
455+
!no2-v4.3.0.db
455456
.diffblue
456457
infer-out
457458
secring.gpg

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.io.InputStream;
5454
import java.nio.file.Files;
5555
import java.nio.file.Paths;
56+
import java.nio.file.StandardCopyOption;
5657
import java.text.ParseException;
5758
import java.text.SimpleDateFormat;
5859
import java.util.*;
@@ -622,7 +623,7 @@ public void run() {
622623
log.error("Error in thread", e);
623624
}
624625
}
625-
};
626+
}
626627

627628
Thread t0 = new Thread(new ThreadRunner());
628629
Thread t1 = new Thread(new ThreadRunner());
@@ -682,6 +683,32 @@ public void testReadOnlyMode() {
682683
deleteDb(fileName);
683684
}
684685

686+
@Test
687+
public void testIssue1162() throws IOException {
688+
689+
// setup no2-v4.3.0.db as a temp file
690+
var databasePath = Files.createTempFile("temp-no2-v4.3.0", ".db");
691+
var templateDb = Objects.requireNonNull(NitriteTest.class.getResourceAsStream("/no2-v4.3.0.db"));
692+
Files.copy(templateDb, databasePath, StandardCopyOption.REPLACE_EXISTING);
693+
694+
var module = MVStoreModule.withConfig()
695+
.filePath(databasePath.toAbsolutePath().toFile())
696+
.build();
697+
698+
var database = Nitrite.builder()
699+
.loadModule(module)
700+
.openOrCreate();
701+
702+
try (database) {
703+
var collection = database.getCollection("myCollection");
704+
assertEquals(1, collection.size());
705+
706+
var firstPerson = collection.find().firstOrNull();
707+
assertNotNull(firstPerson);
708+
assertEquals(1970829645337976832L, firstPerson.getId().getIdValue());
709+
}
710+
}
711+
685712
@Data
686713
@AllArgsConstructor
687714
@NoArgsConstructor
16 KB
Binary file not shown.

nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import lombok.EqualsAndHashCode;
2020
import lombok.Getter;
21+
2122
import org.dizitart.no2.exceptions.InvalidIdException;
2223

2324
import java.io.IOException;
@@ -48,18 +49,20 @@ public final class NitriteId implements Comparable<NitriteId>, Serializable {
4849
private static final SnowflakeIdGenerator generator = new SnowflakeIdGenerator();
4950

5051
/** The underlying value of the NitriteId. */
51-
private long idValue;
52+
/* WARNING(gh-1162): naming it idValue breaks java serialization for databases created with earlier version of nitrite
53+
* and marking it transient introduces side effects for @EqualsAndHashCode */
54+
private long _idValue;
5255

5356
private NitriteId() {
54-
this.idValue = generator.getId();
57+
this._idValue = generator.getId();
5558
}
5659

5760
private NitriteId(String value) {
58-
this.idValue = Long.parseLong(value);
61+
this._idValue = Long.parseLong(value);
5962
}
6063

6164
private NitriteId(long value) {
62-
this.idValue = value;
65+
this._idValue = value;
6366
}
6467

6568
/**
@@ -115,21 +118,25 @@ public static boolean validId(Object value) {
115118
}
116119
}
117120

121+
public long getIdValue() {
122+
return _idValue;
123+
}
124+
118125
@Override
119126
public int compareTo(NitriteId other) {
120-
return Long.compare(idValue, other.idValue);
127+
return Long.compare(_idValue, other._idValue);
121128
}
122129

123130
@Override
124131
public String toString() {
125-
return ID_PREFIX + idValue + ID_SUFFIX;
132+
return ID_PREFIX + _idValue + ID_SUFFIX;
126133
}
127134

128135
private void writeObject(ObjectOutputStream stream) throws IOException {
129-
stream.writeUTF(Long.toString(idValue));
136+
stream.writeUTF(Long.toString(_idValue));
130137
}
131138

132139
private void readObject(ObjectInputStream stream) throws IOException {
133-
idValue = Long.parseLong(stream.readUTF());
140+
_idValue = Long.parseLong(stream.readUTF());
134141
}
135142
}

0 commit comments

Comments
 (0)