Skip to content

Commit 36df63a

Browse files
committed
fix(gh-1162): ensure NitriteId serialization is compatible with previous versions
1 parent 9806e58 commit 36df63a

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.dizitart.no2.collection;
1818

1919
import lombok.EqualsAndHashCode;
20-
import lombok.Getter;
2120
import org.dizitart.no2.exceptions.InvalidIdException;
2221

2322
import java.io.IOException;
@@ -41,25 +40,25 @@
4140
* @see NitriteCollection#getById(NitriteId)
4241
* @since 1.0
4342
*/
44-
@Getter
4543
@EqualsAndHashCode
4644
public final class NitriteId implements Comparable<NitriteId>, Serializable {
4745
private static final long serialVersionUID = 1477462375L;
4846
private static final SnowflakeIdGenerator generator = new SnowflakeIdGenerator();
4947

5048
/** The underlying value of the NitriteId. */
51-
private long idValue;
49+
// WARNING(gh-1162): naming it idValue breaks java serialization for databases created with earlier version of nitrite
50+
private long _idValue;
5251

5352
private NitriteId() {
54-
this.idValue = generator.getId();
53+
this._idValue = generator.getId();
5554
}
5655

5756
private NitriteId(String value) {
58-
this.idValue = Long.parseLong(value);
57+
this._idValue = Long.parseLong(value);
5958
}
6059

6160
private NitriteId(long value) {
62-
this.idValue = value;
61+
this._idValue = value;
6362
}
6463

6564
/**
@@ -115,21 +114,25 @@ public static boolean validId(Object value) {
115114
}
116115
}
117116

117+
public long getIdValue() {
118+
return _idValue;
119+
}
120+
118121
@Override
119122
public int compareTo(NitriteId other) {
120-
return Long.compare(idValue, other.idValue);
123+
return Long.compare(_idValue, other._idValue);
121124
}
122125

123126
@Override
124127
public String toString() {
125-
return ID_PREFIX + idValue + ID_SUFFIX;
128+
return ID_PREFIX + _idValue + ID_SUFFIX;
126129
}
127130

128131
private void writeObject(ObjectOutputStream stream) throws IOException {
129-
stream.writeUTF(Long.toString(idValue));
132+
stream.writeUTF(Long.toString(_idValue));
130133
}
131134

132135
private void readObject(ObjectInputStream stream) throws IOException {
133-
idValue = Long.parseLong(stream.readUTF());
136+
_idValue = Long.parseLong(stream.readUTF());
134137
}
135138
}

0 commit comments

Comments
 (0)