Skip to content

Commit

Permalink
cleanup logs, add unit tests for connections, properly load file for …
Browse files Browse the repository at this point in the history
…saved state
  • Loading branch information
firestar committed Jul 7, 2024
1 parent ca394d0 commit 9712fd6
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 179 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'com.nucleodb'
version = '1.17.1'
version = '1.17.2'

repositories {
mavenCentral()
Expand Down
122 changes: 93 additions & 29 deletions src/integrationTest/java/com/nucleodb/library/ExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.nucleodb.library.database.tables.connection.ConnectionHandler;
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.InvalidConnectionException;
import com.nucleodb.library.database.utils.Serializer;
import com.nucleodb.library.database.utils.exceptions.IncorrectDataEntryClassException;
import com.nucleodb.library.database.utils.exceptions.IncorrectDataEntryObjectException;
import com.nucleodb.library.database.utils.exceptions.MissingDataEntryConstructorsException;
import com.nucleodb.library.models.Author;
import com.nucleodb.library.models.AuthorDE;
import com.nucleodb.library.database.utils.exceptions.ObjectNotSavedException;
import com.nucleodb.library.models.*;
import com.nucleodb.library.mqs.kafka.KafkaConfiguration;
import com.nucleodb.library.mqs.local.LocalConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ExportTest {
NucleoDB nucleoDB;
DataTable<AuthorDE> table;
DataTable<AuthorDE> authorTable;
DataTable<BookDE> bookTable;
ConnectionHandler<WroteConnection> wroteConnections;

@BeforeEach
public void createLocalDB() throws IncorrectDataEntryClassException, MissingDataEntryConstructorsException, IntrospectionException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IncorrectDataEntryObjectException, InterruptedException {
public void createLocalDB() throws IncorrectDataEntryClassException, MissingDataEntryConstructorsException, IntrospectionException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IncorrectDataEntryObjectException, InterruptedException, InvalidConnectionException {
nucleoDB = new NucleoDB(
NucleoDB.DBType.ALL,
c -> {
c.getConnectionConfig().setMqsConfiguration(new KafkaConfiguration());

c.getConnectionConfig().setLoadSaved(true);
c.getConnectionConfig().setJsonExport(true);
c.getConnectionConfig().setSaveChanges(true);
c.getConnectionConfig().setConnectionFileName("./data/connection.dat");
c.getConnectionConfig().setConnectionFileName("./data/"+ c.getConnectionConfig().getLabel()+".dat");
c.getConnectionConfig().setExportInterval(50);
c.getConnectionConfig().setSaveInterval(50);
},
Expand All @@ -48,7 +53,7 @@ public void createLocalDB() throws IncorrectDataEntryClassException, MissingData
c.getDataTableConfig().setLoadSave(true);
c.getDataTableConfig().setSaveChanges(true);
c.getDataTableConfig().setJsonExport(true);
c.getDataTableConfig().setTableFileName("./data/datatable.dat");
c.getDataTableConfig().setTableFileName("./data/"+ c.getDataTableConfig().getTable()+".dat");
c.getDataTableConfig().setExportInterval(50);
c.getDataTableConfig().setSaveInterval(50);
},
Expand All @@ -57,30 +62,74 @@ public void createLocalDB() throws IncorrectDataEntryClassException, MissingData
},
"com.nucleodb.library.models"
);
table = nucleoDB.getTable(Author.class);
System.out.println("STARTED");
table.saveSync(new AuthorDE(new Author("George Orwell", "science-fiction")));
}
authorTable = nucleoDB.getTable(Author.class);
bookTable = nucleoDB.getTable(Book.class);
wroteConnections = nucleoDB.getConnectionHandler(WroteConnection.class);

AuthorDE georgeOrwell = new AuthorDE(new Author("George Orwell", "science-fiction"));
BookDE bookDE = new BookDE(new Book("Nineteen Eighty-Four"));
authorTable.saveSync(georgeOrwell);
bookTable.saveSync(bookDE);
AuthorDE author = authorTable.get("name", "George Orwell").stream().findFirst().get();
BookDE book = bookTable.get("name", "Nineteen Eighty-Four").stream().findFirst().get();
wroteConnections.saveSync(new WroteConnection(author, book));

public AuthorDE copy(AuthorDE authorDE) throws JsonProcessingException {
return Serializer.getObjectMapper().getOm().readValue(Serializer.getObjectMapper().getOm().writeValueAsString(authorDE), AuthorDE.class);
}

@AfterEach
public void deleteEntries() {
System.out.println("DONE");
table.getEntries().stream().map(author -> {
try {
return copy(author);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet()).forEach(author-> {
try {
table.deleteSync(author);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
authorTable
.getEntries()
.stream()
.map(author -> {
try {
return author.copy(AuthorDE.class, true);
} catch (ObjectNotSavedException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toSet()).forEach(author -> {
try {
authorTable.deleteSync(author);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
bookTable
.getEntries()
.stream()
.map(book -> {
try {
return book.copy(BookDE.class, true);
} catch (ObjectNotSavedException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toSet()).forEach(book -> {
try {
bookTable.deleteSync(book);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
wroteConnections
.getAllConnections()
.stream()
.map(c->c.copy(WroteConnection.class,true))
.collect(Collectors.toSet()).forEach(c -> {
try {
wroteConnections.deleteSync(c);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Expand All @@ -91,10 +140,25 @@ public void deleteEntries() {
@Test
public void checkSaving() throws IncorrectDataEntryObjectException, InterruptedException {
AuthorDE edgarAllenPoe = new AuthorDE(new Author("Edgar Allen Poe", "fiction"));
table.saveSync(edgarAllenPoe);
authorTable.saveSync(edgarAllenPoe);
assertEquals(
1,
authorTable.get(
"id",
edgarAllenPoe.getKey(),
null
).size()
);
assertEquals(2, authorTable.getEntries().size());
Thread.sleep(5000);
}
@Test
public void fileSaving() throws IncorrectDataEntryObjectException, InterruptedException {
AuthorDE edgarAllenPoe = new AuthorDE(new Author("Edgar Allen Poe", "fiction"));
authorTable.saveSync(edgarAllenPoe);
assertEquals(
1,
table.get(
authorTable.get(
"id",
edgarAllenPoe.getKey(),
null
Expand Down
30 changes: 30 additions & 0 deletions src/integrationTest/java/com/nucleodb/library/models/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nucleodb.library.models;

import com.nucleodb.library.database.index.TrieIndex;
import com.nucleodb.library.database.index.annotation.Index;
import com.nucleodb.library.database.tables.annotation.Table;

import java.io.Serializable;

@Table(tableName = "bookIT", dataEntryClass = BookDE.class)
public class Book implements Serializable {
private static final long serialVersionUID = 1;
@Index(type = TrieIndex.class)
String name;

public Book() {
}

public Book(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
22 changes: 22 additions & 0 deletions src/integrationTest/java/com/nucleodb/library/models/BookDE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nucleodb.library.models;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.nucleodb.library.database.modifications.Create;
import com.nucleodb.library.database.tables.table.DataEntry;

public class BookDE extends DataEntry<Book> {
public BookDE(Book obj) {
super(obj);
}

public BookDE(Create create) throws ClassNotFoundException, JsonProcessingException {
super(create);
}

public BookDE() {
}

public BookDE(String key) {
super(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.nucleodb.library.models;

import com.nucleodb.library.database.tables.annotation.Conn;
import com.nucleodb.library.database.tables.connection.Connection;

import java.util.Map;

@Conn("IT-WROTE")
public class WroteConnection extends Connection<AuthorDE, BookDE> {
public WroteConnection() {
}

public WroteConnection(AuthorDE from, BookDE to) {
super(from, to);
}

public WroteConnection(AuthorDE from, BookDE to, Map<String, String> metadata) {
super(from, to, metadata);
}
}

1 change: 1 addition & 0 deletions src/main/java/com/nucleodb/library/NucleoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public NucleoDB(DBType dbType, String readToTime, String... packagesToScan) thro
public NucleoDB(DBType dbType, String readToTime, Consumer<ConnectionConsumer> connectionCustomizer, Consumer<DataTableConsumer> dataTableCustomizer, Consumer<LockConfig> lockCustomizer, String... packagesToScan) throws IncorrectDataEntryClassException, MissingDataEntryConstructorsException, IntrospectionException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
startLockManager(lockCustomizer);
startTables(packagesToScan, dbType, readToTime, dataTableCustomizer);

startConnections(packagesToScan, dbType, readToTime, connectionCustomizer);
}
private void startLockManager(Consumer<LockConfig> customizer) throws IntrospectionException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public Object put(@NotNull Object key, @NotNull Object value) {
executorPool.schedule(()->{
LockReference lockReference = (LockReference)this.get(key);
if(lockReference!=null && value instanceof LockReference && lockReference.getRequest().equals(((LockReference)value).getRequest())) {
logger.info( "EXPIRED");
logger.info((String) key);
logger.log(Level.FINEST,"EXPIRED");
logger.log(Level.FINEST,(String) key);
try {
LockReference activeLock = Serializer.getObjectMapper().getOm().readValue(Serializer.getObjectMapper().getOm().writeValueAsString(value), LockReference.class);
activeLock.setLock(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.TreeMap;
import java.util.UUID;

public class Connection<T extends DataEntry, F extends DataEntry> implements Serializable, Comparable<Connection>{
public class Connection<F extends DataEntry, T extends DataEntry> implements Serializable, Comparable<Connection>{
@SkipCopy
private static final long serialVersionUID = 1;

Expand Down Expand Up @@ -60,7 +60,6 @@ public <T extends Connection> T copy(Class<T> clazz, boolean lock) {
this.connectionHandler.getConfig().getLabel(),
uuid
);
System.out.println("unlocked "+lockReference.getRequest());
try {
T obj = Serializer.getObjectMapper().getOm().readValue(Serializer.getObjectMapper().getOm().writeValueAsString(this), clazz);
obj.setRequest(lockReference.getRequest());
Expand All @@ -70,6 +69,7 @@ public <T extends Connection> T copy(Class<T> clazz, boolean lock) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}else{
Expand Down
Loading

0 comments on commit 9712fd6

Please sign in to comment.