Skip to content

Commit

Permalink
fix version, modified date for entries
Browse files Browse the repository at this point in the history
  • Loading branch information
firestar committed Dec 20, 2023
1 parent 4641f3b commit 304d730
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'com.nucleodb'
version = '1.13.16'
version = '1.13.17'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ public void modify(Modification mod, Object modification) throws ExecutionExcept
config.getConnectionClass()
);
conn.setVersion(u.getVersion());
conn.setModified(u.getTime());
conn.setMetadata(connectionTmp.getMetadata());
this.changed = new Date().getTime();
consumerResponse(conn, u.getChangeUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DataEntry<T> implements Serializable, Comparable<DataEntry> {
@SkipCopy
private static final long serialVersionUID = 1;
public String key;
public long version = -1;
public long version = 0;
private JsonNode reference;
public T data;
private transient String tableName;
Expand Down Expand Up @@ -137,4 +137,12 @@ public static Object cast(DataEntry dataEntry, Class<?> clazz) throws JsonProces
public Object cast(Class<?> clazz) throws JsonProcessingException {
return new ObjectMapper().readValue(new ObjectMapper().writeValueAsString(this), clazz);
}

public void setCreated(Instant created) {
this.created = created;
}

public void setModified(Instant modified) {
this.modified = modified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ public void modify(Modification mod, Object modification) {
} else {
de.setReference(u.getChangesPatch().apply(de.getReference()));
de.setVersion(u.getVersion());
de.setModified(u.getTime());
de.setData(Serializer.getObjectMapper().getOm().readValue(de.getReference().toString(), de.getData().getClass()));
//System.out.println(Serializer.getObjectMapper().getOm().writeValueAsString(de.getData()));
u.getOperations().forEach(op -> {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/nucleodb/library/NucleoDBReadToTime.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.nucleodb.library;

import com.nucleodb.library.database.tables.table.DataEntry;
import com.nucleodb.library.database.tables.table.DataEntryProjection;
import com.nucleodb.library.database.tables.table.DataTable;
import com.nucleodb.library.database.utils.Pagination;
import com.nucleodb.library.database.utils.Serializer;
import com.nucleodb.library.database.utils.exceptions.IncorrectDataEntryClassException;
import com.nucleodb.library.database.utils.exceptions.IncorrectDataEntryObjectException;
Expand All @@ -13,7 +16,9 @@
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -79,4 +84,41 @@ public void saveTest() throws IncorrectDataEntryObjectException, InterruptedExce
assertTrue(new File(table.getConfig().getTableFileName()).exists());
new File(table.getConfig().getTableFileName()).delete();
}



@Test
public void modifyDateCheck() throws IncorrectDataEntryObjectException, InterruptedException, IncorrectDataEntryClassException, MissingDataEntryConstructorsException, IntrospectionException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
NucleoDB nucleoDB = new NucleoDB(
NucleoDB.DBType.NO_LOCAL,
c -> c.setMqsConfiguration(new LocalConfiguration()),
c -> c.setMqsConfiguration(new LocalConfiguration()),
"com.nucleodb.library.helpers.models"
);
DataTable table = nucleoDB.getTable(Author.class);
table.saveSync( new AuthorDE(new Author("George Orwell", "science-fiction")));
Optional<DataEntry> savedAuthor = table.get("name", "George Orwell", new DataEntryProjection(new Pagination(0, 1))).stream().findFirst();
Instant created = null;
if(savedAuthor.isPresent()){
created = savedAuthor.get().getCreated();
}
assertNotNull(created);
Thread.sleep(4000);
for (AuthorDE author : table.get("name", "George Orwell", new DataEntryProjection(){{
setWritable(true);
}}).stream().map(de->(AuthorDE)de).collect(Collectors.toSet())) {
author.getData().setAreaOfInterest("sci-fi");
table.saveSync(author);
}
Optional<DataEntry> modifiedAuthor = table.get("name", "George Orwell", new DataEntryProjection(new Pagination(0, 1))).stream().findFirst();
Instant modified = null;
Instant createdModified = null;
if(modifiedAuthor.isPresent()){
createdModified = modifiedAuthor.get().getCreated();
modified = modifiedAuthor.get().getModified();
assertEquals(1, modifiedAuthor.get().getVersion());
}
assertEquals(created, createdModified);
assertTrue(created.plusSeconds(2).isBefore(modified));
}
}

0 comments on commit 304d730

Please sign in to comment.