Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ test.log
/build
!no2-old.db
!no2-v3.db
!no2-v4.3.0.db
.diffblue
infer-out
secring.gpg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
Expand Down Expand Up @@ -622,7 +623,7 @@ public void run() {
log.error("Error in thread", e);
}
}
};
}

Thread t0 = new Thread(new ThreadRunner());
Thread t1 = new Thread(new ThreadRunner());
Expand Down Expand Up @@ -682,6 +683,32 @@ public void testReadOnlyMode() {
deleteDb(fileName);
}

@Test
public void testIssue1162() throws IOException {

// setup no2-v4.3.0.db as a temp file
var databasePath = Files.createTempFile("temp-no2-v4.3.0", ".db");
var templateDb = Objects.requireNonNull(NitriteTest.class.getResourceAsStream("/no2-v4.3.0.db"));
Files.copy(templateDb, databasePath, StandardCopyOption.REPLACE_EXISTING);

var module = MVStoreModule.withConfig()
.filePath(databasePath.toAbsolutePath().toFile())
.build();

var database = Nitrite.builder()
.loadModule(module)
.openOrCreate();

try (database) {
var collection = database.getCollection("myCollection");
assertEquals(1, collection.size());

var firstPerson = collection.find().firstOrNull();
assertNotNull(firstPerson);
assertEquals(1970829645337976832L, firstPerson.getId().getIdValue());
}
}

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand Down
Binary file not shown.
23 changes: 15 additions & 8 deletions nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import lombok.EqualsAndHashCode;
import lombok.Getter;

import org.dizitart.no2.exceptions.InvalidIdException;

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

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

private NitriteId() {
this.idValue = generator.getId();
this._idValue = generator.getId();
}

private NitriteId(String value) {
this.idValue = Long.parseLong(value);
this._idValue = Long.parseLong(value);
}

private NitriteId(long value) {
this.idValue = value;
this._idValue = value;
}

/**
Expand Down Expand Up @@ -115,21 +118,25 @@ public static boolean validId(Object value) {
}
}

public long getIdValue() {
return _idValue;
}

@Override
public int compareTo(NitriteId other) {
return Long.compare(idValue, other.idValue);
return Long.compare(_idValue, other._idValue);
}

@Override
public String toString() {
return ID_PREFIX + idValue + ID_SUFFIX;
return ID_PREFIX + _idValue + ID_SUFFIX;
}

private void writeObject(ObjectOutputStream stream) throws IOException {
stream.writeUTF(Long.toString(idValue));
stream.writeUTF(Long.toString(_idValue));
}

private void readObject(ObjectInputStream stream) throws IOException {
idValue = Long.parseLong(stream.readUTF());
_idValue = Long.parseLong(stream.readUTF());
}
}
Loading