Fixed Tests

This commit is contained in:
Andrea Cavalli 2019-04-20 17:02:57 +02:00
parent 78bcb67c67
commit debe1f70bb
8 changed files with 80 additions and 46 deletions

View File

@ -173,12 +173,14 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
}
}
protected void registerClass(Class<?> type, int id) {
public void registerClass(Class<?> type, int id) {
this.objectsIO.registerClass(type, id);
}
protected DatabaseJava instantiateNewDatabase(Path dataFile, Path blocksMetaFile, Path referencesMetaFile) throws IOException {
return new DatabaseJava(dataFile, blocksMetaFile, referencesMetaFile);
DatabaseJava newDatabaseJava = new DatabaseJava(dataFile, blocksMetaFile, referencesMetaFile);
this.getObjectsIO().getRegisteredClasses().forEach(newDatabaseJava::registerClass);
return newDatabaseJava;
}
@Override
@ -187,7 +189,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
}
@Override
public IObjectsIO getObjectsIO() {
public DatabaseObjectsIO getObjectsIO() {
return objectsIO;
}

View File

@ -443,6 +443,13 @@ public class DatabaseObjectsIO implements IObjectsIO {
serializer.registerClass(type, realId);
}
protected Map<Class<?>, Integer> getRegisteredClasses() {
Map<Class<?>, Integer> registeredClasses = serializer.getRegisteredClasses();
registeredClasses.replaceAll((clazz,id) -> id - 100);
registeredClasses.entrySet().removeIf((entry) -> entry.getValue() < 0);
return registeredClasses;
}
private <T extends EnhancedObject> void preloadEnhancedObjectProperties(T obj, long[] propertyReferences) {
// Declare the variables needed to getBlock the biggest property Id
Method[] unorderedPropertyGetters = obj.getPropertyGetters();
@ -621,7 +628,7 @@ public class DatabaseObjectsIO implements IObjectsIO {
throws IOException {
Object data = loadData_(fieldType, fieldReference);
try {
if (fieldType == DbDataType.OBJECT && data != null) {
if (data != null) {
if (!field.getType().isInstance(data)) {
throw new IOException("There is an attempt to load an object of type " + data.getClass()
+ " into a field of type " + field.getType());
@ -629,7 +636,7 @@ public class DatabaseObjectsIO implements IObjectsIO {
}
FieldUtils.writeField(field, obj, data, true);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
throw new IOException(e);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.strangedb.java.database;
import java.io.IOException;
import java.util.Map;
public interface ISerializer {
<T> Class<T> readClassBytes(byte[] input) throws IOException;
@ -13,4 +14,6 @@ public interface ISerializer {
<T> T readClassAndObjectBytes(byte[] input) throws IOException;
void registerClass(Class<?> type, int id);
Map<Class<?>, Integer> getRegisteredClasses();
}

View File

@ -1,14 +1,12 @@
package it.cavallium.strangedb.java.database;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.KryoDataOutput;
import com.esotericsoftware.kryo.io.Output;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
@ -19,6 +17,7 @@ public class KryoSerializer implements ISerializer {
private ReentrantLock[] locks = new ReentrantLock[KRYO_INSTANCES];
private final Kryo[] kryo = new Kryo[KRYO_INSTANCES];
private AtomicInteger current = new AtomicInteger(0);
private ConcurrentHashMap<Class<?>, Integer> registeredClasses = new ConcurrentHashMap<>(200);
public KryoSerializer() {
for (int i = 0; i < KRYO_INSTANCES; i++) {
@ -85,6 +84,7 @@ public class KryoSerializer implements ISerializer {
@Override
public void registerClass(Class<?> type, int id) {
registeredClasses.put(type, id);
for (int i = 0; i < KRYO_INSTANCES; i++) {
locks[i].lock();
try {
@ -94,4 +94,18 @@ public class KryoSerializer implements ISerializer {
}
}
}
@Override
public Map<Class<?>, Integer> getRegisteredClasses() {
for (int i = 0; i < KRYO_INSTANCES; i++) {
locks[i].lock();
}
try {
return new HashMap<>(registeredClasses);
} finally {
for (int i = 0; i < KRYO_INSTANCES; i++) {
locks[i].unlock();
}
}
}
}

View File

@ -7,6 +7,8 @@ import it.cavallium.strangedb.java.database.IDatabaseTools;
import it.cavallium.strangedb.java.objects.EnhancedObject;
import it.cavallium.strangedb.java.objects.lists.EnhancedObjectStrangeDbList;
import it.cavallium.strangedb.java.objects.lists.ObjectStrangeDbList;
import it.cavallium.strangedb.utils.NSimplestClass;
import it.cavallium.strangedb.utils.NTestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -30,54 +32,56 @@ public class ObjectListTests {
path2 = Files.createTempFile("db-tests-blocks-", ".db");
path3 = Files.createTempFile("db-tests-references-", ".db");
db = new DatabaseJava(path1, path2, path3);
registerClasses();
ListsRoot root = db.loadRoot(ListsRoot::new);
for (int i = 0; i < 5000; i++) {
for (int i = 0; i < 500; i++) {
root.objectList.add(new ObjectItem(i));
root.enhancedObjectList.add(new EnhancedObjectItem(db, i));
}
for (int i = 0; i < 5000; i++) {
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) {
root.objectList.update(i, new ObjectItem(i));
root.enhancedObjectList.update(i, new EnhancedObjectItem(db, i));
}
}
for (int i = 0; i < 5000; i++) {
for (int i = 0; i < 500; i++) {
if (i % 11 == 0) {
root.objectList.set(i, new ObjectItem(i));
root.enhancedObjectList.set(i, new EnhancedObjectItem(db, i));
}
}
for (int i = 5000; i < 6000; i++) {
for (int i = 500; i < 600; i++) {
root.objectList.add(new ObjectItem(0));
root.enhancedObjectList.add(i, new EnhancedObjectItem(db, 0));
}
db.close();
}
private void registerClasses() {
int id = 0;
db.registerClass(EnhancedObjectItem.class, id++);
db.registerClass(ListsRoot.class, id++);
db.registerClass(ObjectItem.class, id++);
}
@Test
public void shouldUpdateClass() throws IOException {
db = new DatabaseJava(path1, path2, path3);
registerClasses();
ListsRoot root = db.loadRoot(ListsRoot::new);
new Thread(() -> {
try {
for (int i = 0; i < 5000; i++) {
String val = root.objectList.get(i).value;
assertEquals(val, "test_" + i);
val = root.enhancedObjectList.get(i).value;
assertEquals(val, "test_" + i);
}
for (int i = 5000; i < 6000; i++) {
assertEquals(root.objectList.get(i).value, "test_" + 0);
assertEquals(root.enhancedObjectList.get(i).value, "test_" + 0);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
for (int i = 0; i < 500; i++) {
String val = root.objectList.get(i).value;
assertEquals(val, "test_" + i);
val = root.enhancedObjectList.get(i).value;
assertEquals(val, "test_" + i);
}
for (int i = 500; i < 600; i++) {
assertEquals(root.objectList.get(i).value, "test_" + 0);
assertEquals(root.enhancedObjectList.get(i).value, "test_" + 0);
}
} catch (IOException e) {
e.printStackTrace();
}
db.close();

View File

@ -310,14 +310,14 @@ public class Performance {
Files.createFile(dbReferencesFile);
db = new DatabaseJava(dbDataFile, dbBlocksFile, dbReferencesFile);
int i = 0;
db.getObjectsIO().registerClass(SimpleEnhancedObject.class, i++);
db.getObjectsIO().registerClass(PreloadedListContainer.class, i++);
db.getObjectsIO().registerClass(DynamicListContainer.class, i++);
db.getObjectsIO().registerClass(EMessage.class, i++);
db.getObjectsIO().registerClass(EMessageContent.class, i++);
db.getObjectsIO().registerClass(EMessageText.class, i++);
db.getObjectsIO().registerClass(EMessageOtherContent.class, i++);
db.getObjectsIO().registerClass(EFormattedText.class, i++);
db.registerClass(SimpleEnhancedObject.class, i++);
db.registerClass(PreloadedListContainer.class, i++);
db.registerClass(DynamicListContainer.class, i++);
db.registerClass(EMessage.class, i++);
db.registerClass(EMessageContent.class, i++);
db.registerClass(EMessageText.class, i++);
db.registerClass(EMessageOtherContent.class, i++);
db.registerClass(EFormattedText.class, i++);
}
public static void deleteDb() throws IOException {

View File

@ -33,9 +33,9 @@ public class QueryTests {
path3 = Files.createTempFile("db-tests-references-", ".db");
db = new DatabaseJava(path1, path2, path3);
int i = 0;
db.getObjectsIO().registerClass(UserFullInfo.class, i++);
db.getObjectsIO().registerClass(User.class, i++);
db.getObjectsIO().registerClass(ListContainer.class, i++);
db.registerClass(UserFullInfo.class, i++);
db.registerClass(User.class, i++);
db.registerClass(ListContainer.class, i++);
}
@Test

View File

@ -43,12 +43,16 @@ public class NTestUtils {
public WrappedDb create(ConsumerWithIO<NTestUtils.WrappedDb> r) throws IOException {
this.r = () -> r.accept(WrappedDb.this);
this.create();
return this;
return this.create();
}
private DatabaseJava openDatabase() throws IOException {
return new DatabaseJava(tempDir.resolve(Paths.get("data.db")), tempDir.resolve(Paths.get("blocks.dat")), tempDir.resolve(Paths.get("references.dat")));
DatabaseJava db = new DatabaseJava(tempDir.resolve(Paths.get("data.db")), tempDir.resolve(Paths.get("blocks.dat")), tempDir.resolve(Paths.get("references.dat")));
int id = 0;
db.registerClass(RootClass.class, id++);
db.registerClass(LongArrayList.class, id++);
db.registerClass(NSimplestClass.class, id++);
return db;
}
public void delete() throws IOException {