strangedb/src/main/java/org/warp/jcwdb/ann/DatabaseManager.java

505 lines
18 KiB
Java

package org.warp.jcwdb.ann;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.chars.CharArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.warp.jcwdb.Cleanable;
import org.warp.jcwdb.Cleaner;
import org.warp.jcwdb.FileIndexManager;
import java.io.ByteArrayOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class DatabaseManager implements Cleanable {
public static final long MAX_LOADED_INDICES = 100000;
private final DBObjectIndicesManager objectIndicesManager;
private final FileIndexManager indices;
private final Cleaner cleaner;
private final JCWDatabase jcwDatabase;
private DBObject loadedRootObject = null;
private final Kryo kryo = new Kryo();
private volatile boolean closed;
DatabaseManager(JCWDatabase jcwDatabase, Path dataFile, Path metadataFile, boolean registrationRequired) throws IOException {
this.jcwDatabase = jcwDatabase;
kryo.setRegistrationRequired(registrationRequired);
registerDefaultClasses();
this.indices = new FileIndexManager(dataFile, metadataFile);
if (!indices.has(0)) {
allocateNullValue();
}
this.objectIndicesManager = new DBObjectIndicesManager(this.indices);
this.cleaner = new Cleaner(this);
this.cleaner.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
DatabaseManager.this.close();
} catch (Exception e) {
e.printStackTrace();
}
}));
}
private void registerDefaultClasses() {
int id = -90;
registerClass(boolean[].class, id++);
registerClass(byte[].class, id++);
registerClass(short[].class, id++);
registerClass(char[].class, id++);
registerClass(int[].class, id++);
registerClass(long[].class, id++);
registerClass(Boolean[].class, id++);
registerClass(Byte[].class, id++);
registerClass(Short[].class, id++);
registerClass(Character[].class, id++);
registerClass(Integer[].class, id++);
registerClass(Long[].class, id++);
registerClass(String.class, id++);
registerClass(String[].class, id++);
registerClass(Boolean.class, id++);
registerClass(Byte.class, id++);
registerClass(Short.class, id++);
registerClass(Character.class, id++);
registerClass(Integer.class, id++);
registerClass(Class.class, id++);
registerClass(Object.class, id++);
registerClass(Object[].class, id++);
registerClass(Long.class, id++);
registerClass(String.class, id++);
registerClass(String[].class, id++);
registerClass(boolean[][].class, id++);
registerClass(byte[][].class, id++);
registerClass(short[][].class, id++);
registerClass(char[][].class, id++);
registerClass(int[][].class, id++);
registerClass(long[][].class, id++);
registerClass(String[][].class, id++);
registerClass(List.class, id++);
registerClass(ArrayList.class, id++);
registerClass(LinkedList.class, id++);
registerClass(Set.class, id++);
registerClass(HashSet.class, id++);
registerClass(LinkedHashSet.class, id++);
registerClass(Map.class, id++);
registerClass(HashMap.class, id++);
registerClass(LinkedHashMap.class, id++);
registerClass(TreeMap.class, id++);
registerClass(BooleanArrayList.class, id++);
registerClass(ByteArrayList.class, id++);
registerClass(ShortArrayList.class, id++);
registerClass(CharArrayList.class, id++);
registerClass(IntArrayList.class, id++);
registerClass(LongArrayList.class, id++);
registerClass(TreeSet.class, id++);
registerClass(SortedSet.class, id++);
registerClass(SortedMap.class, id++);
}
@SuppressWarnings("unchecked")
public <T extends DBObject> T loadRoot(Class<T> rootType) throws IOException {
if (loadedRootObject != null) {
throw new RuntimeException("Root already set!");
}
if (isDBObjectNull(0)) {
try {
T root = rootType.getConstructor(JCWDatabase.class).newInstance(this.jcwDatabase);
loadedRootObject = root;
return root;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IOError(e);
}
} else {
T root = (T) loadDBObject(rootType, 0);
loadedRootObject = root;
return root;
}
}
public void close() throws IOException {
if (!closed) {
closed = true;
DatabaseManager.this.cleaner.stop();
if (loadedRootObject != null) {
loadedRootObject.writeToDisk(0);
}
indices.close();
}
}
public void preloadDBObject(DBObject obj, DBObjectIndicesManager.DBObjectInfo objectInfo) throws IOException {
preloadDBObjectFields(obj, objectInfo.getFields());
preloadDBObjectProperties(obj, objectInfo.getProperties());
}
private void preloadDBObjectFields(DBObject obj, long[] fieldUIDs) throws IOException {
// Declare the variables needed to get the biggest field Id
Field[] unorderedFields = getFields(obj);
// Find the biggest field Id
int biggestFieldId = getBiggestFieldId(unorderedFields);
// Declare the other variables
Field[] fields = new Field[biggestFieldId + 1];
DBDataType[] orderedFieldTypes = new DBDataType[biggestFieldId + 1];
// Load all fields metadata and load them
for (Field field : unorderedFields) {
DBField fieldAnnotation = field.getAnnotation(DBField.class);
int fieldId = fieldAnnotation.id();
DBDataType fieldType = fieldAnnotation.type();
loadField(obj, field, fieldType, fieldUIDs[fieldId]);
fields[fieldId] = field;
orderedFieldTypes[fieldId] = fieldType;
}
// Set fields metadata
obj.setFields(fields, orderedFieldTypes, fieldUIDs);
}
private void preloadDBObjectProperties(DBObject obj, long[] propertyUIDs) {
// Declare the variables needed to get the biggest property Id
Method[] unorderedPropertyGetters = getPropertyGetters(obj);
Method[] unorderedPropertySetters = getPropertySetters(obj);
// Find the biggest property Id
int biggestGetter = getBiggestPropertyGetterId(unorderedPropertyGetters);
int biggestSetter = getBiggestPropertySetterId(unorderedPropertySetters);
int biggestPropertyId = biggestGetter > biggestSetter ? biggestGetter : biggestSetter;
for (Method property : unorderedPropertySetters) {
DBPropertySetter fieldAnnotation = property.getAnnotation(DBPropertySetter.class);
int propertyId = fieldAnnotation.id();
if (propertyId > biggestPropertyId) {
biggestPropertyId = propertyId;
}
}
// Declare the other variables
DBDataType[] propertyTypes = new DBDataType[biggestPropertyId + 1];
Method[] propertyGetters = new Method[biggestPropertyId + 1];
Method[] propertySetters = new Method[biggestPropertyId + 1];
Map<String, DBPropertySetter> setterMethods = new LinkedHashMap<>();
Map<String, DBPropertyGetter> getterMethods = new LinkedHashMap<>();
// Load the properties metadata
for (Method property : unorderedPropertyGetters) {
DBPropertyGetter propertyAnnotation = property.getAnnotation(DBPropertyGetter.class);
int propertyId = propertyAnnotation.id();
DBDataType propertyType = propertyAnnotation.type();
propertyTypes[propertyId] = propertyType;
propertyGetters[propertyId] = property;
getterMethods.put(property.getName(), propertyAnnotation);
}
for (Method property : unorderedPropertySetters) {
DBPropertySetter propertyAnnotation = property.getAnnotation(DBPropertySetter.class);
int propertyId = propertyAnnotation.id();
DBDataType propertyType = propertyAnnotation.type();
propertyTypes[propertyId] = propertyType;
propertySetters[propertyId] = property;
setterMethods.put(property.getName(), propertyAnnotation);
}
// Set properties metadata
obj.setProperties(propertyGetters, propertySetters, propertyTypes, propertyUIDs, setterMethods, getterMethods);
}
Method[] getPropertyGetters(DBObject obj) {
return MethodUtils.getMethodsWithAnnotation(obj.getClass(), DBPropertyGetter.class);
}
Method[] getPropertySetters(DBObject obj) {
return MethodUtils.getMethodsWithAnnotation(obj.getClass(), DBPropertySetter.class);
}
protected Field[] getFields(DBObject obj) {
return FieldUtils.getFieldsWithAnnotation(obj.getClass(), DBField.class);
}
int getBiggestPropertyGetterId(Method[] unorderedPropertyGetters) {
int biggestPropertyId = -1;
for (Method property : unorderedPropertyGetters) {
DBPropertyGetter fieldAnnotation = property.getAnnotation(DBPropertyGetter.class);
int propertyId = fieldAnnotation.id();
if (propertyId > biggestPropertyId) {
biggestPropertyId = propertyId;
}
}
return biggestPropertyId;
}
int getBiggestPropertySetterId(Method[] unorderedPropertySetters) {
int biggestPropertyId = -1;
for (Method property : unorderedPropertySetters) {
DBPropertySetter fieldAnnotation = property.getAnnotation(DBPropertySetter.class);
int propertyId = fieldAnnotation.id();
if (propertyId > biggestPropertyId) {
biggestPropertyId = propertyId;
}
}
return biggestPropertyId;
}
protected int getBiggestFieldId(Field[] unorderedFields) {
int biggestFieldId = -1;
for (Field field : unorderedFields) {
DBField fieldAnnotation = field.getAnnotation(DBField.class);
int propertyId = fieldAnnotation.id();
if (propertyId > biggestFieldId) {
biggestFieldId = propertyId;
}
}
return biggestFieldId;
}
public void loadProperty(DBObject obj, int propertyId, Method property, DBDataType propertyType, long propertyUID) throws IOException {
loadData(propertyType, propertyUID, property::getReturnType, (data) -> obj.setLoadedProperty(propertyId, data));
}
public void loadField(DBObject obj, Field field, DBDataType fieldType, long fieldUID) throws IOException {
loadData(fieldType, fieldUID, field::getType, (data) -> {
try {
FieldUtils.writeField(field, obj, data, true);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
}
@SuppressWarnings("unchecked")
private void loadData(DBDataType propertyType, long dataUID, Supplier<Class<?>> returnType, Consumer result) throws IOException {
switch (propertyType) {
case DATABASE_OBJECT:
DBObject fieldDBObjectValue = loadDBObject((Class<? extends DBObject>) returnType.get(), dataUID);
//System.err.println("Loading data DBObj " + dataUID + ":" + fieldDBObjectValue);
result.accept(fieldDBObjectValue);
return;
case OBJECT:
Object fieldObjectValue = loadObject(dataUID);
//System.err.println("Loading data Obj " + dataUID + ":" + fieldObjectValue);
result.accept(fieldObjectValue);
return;
case UID_LIST:
LongArrayList fieldListObjectValue = loadListObject(dataUID);
//System.err.println("Loading data LOb " + dataUID + ":" + fieldListObjectValue);
result.accept(fieldListObjectValue);
return;
case BOOLEAN:
boolean fieldBooleanValue = loadBoolean(dataUID);
//System.err.println("Loading data Boo " + dataUID + ":" + fieldBooleanValue);
result.accept(fieldBooleanValue);
return;
case BYTE:
byte fieldByteValue = loadByte(dataUID);
//System.err.println("Loading data Byt " + dataUID + ":" + fieldByteValue);
result.accept(fieldByteValue);
return;
case SHORT:
short fieldShortValue = loadShort(dataUID);
//System.err.println("Loading data Shr " + dataUID + ":" + fieldShortValue);
result.accept(fieldShortValue);
return;
case CHAR:
char fieldCharValue = loadChar(dataUID);
//System.err.println("Loading data Chr " + dataUID + ":" + fieldCharValue);
result.accept(fieldCharValue);
return;
case INTEGER:
int fieldIntValue = loadInt(dataUID);
//System.err.println("Loading data Int " + dataUID + ":" + fieldIntValue);
result.accept(fieldIntValue);
return;
case LONG:
long fieldLongValue = loadLong(dataUID);
//System.err.println("Loading data Lng " + dataUID + ":" + fieldLongValue);
result.accept(fieldLongValue);
return;
default:
throw new NullPointerException("Unknown data type");
}
}
public <T extends DBObject> T loadDBObject(Class<T> type, long propertyUID) throws IOException {
try {
DBObjectIndicesManager.DBObjectInfo objectInfo = readUIDs(propertyUID);
if (objectInfo == null) return null;
return type.getDeclaredConstructor(JCWDatabase.class, DBObjectIndicesManager.DBObjectInfo.class).newInstance(jcwDatabase, objectInfo);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IOException(e);
}
}
private boolean isDBObjectNull(long uid) {
try {
return !objectIndicesManager.has(uid) || objectIndicesManager.get(uid) == null;
} catch (IOException ex) {
throw new IOError(ex);
}
}
@SuppressWarnings("unchecked")
public <T> T loadObject(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? null : (T) kryo.readClassAndObject(i));
}
private LongArrayList loadListObject(long uid) throws IOException {
return indices.get(uid, (i, size) -> {
if (size == 0) return null;
LongArrayList list = new LongArrayList();
int listSize = i.readVarInt(true);
for (int li = 0; li < listSize; li++) {
list.add(i.readVarLong(true));
}
return list;
});
}
public boolean loadBoolean(long uid) throws IOException {
return indices.get(uid, (i, size) -> size != 0 && i.readBoolean());
}
public byte loadByte(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? 0 : i.readByte());
}
public short loadShort(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? 0 : i.readShort());
}
public char loadChar(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? 0 : i.readChar());
}
public int loadInt(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? 0 : i.readInt());
}
public long loadLong(long uid) throws IOException {
return indices.get(uid, (i, size) -> size == 0 ? 0 : i.readLong());
}
/**
*
* @param uid
* @return
*/
public DBObjectIndicesManager.DBObjectInfo readUIDs(long uid) {
try {
return objectIndicesManager.get(uid);
} catch (IOException e) {
throw new IOError(e);
}
}
public boolean exists(long uid) {
return objectIndicesManager.has(uid);
}
public void writeObjectInfo(long uid, long[] fieldUIDs, long[] propertyUIDs) throws IOException {
//System.err.println("Saving obj. " + uid);
this.objectIndicesManager.set(uid, fieldUIDs, propertyUIDs);
}
private void writeObjectInfoNull(long uid) throws IOException {
this.objectIndicesManager.setNull(uid);
}
public <T> void writeObjectProperty(long uid, DBDataType propertyType, T loadedPropertyValue) throws IOException {
switch (propertyType) {
case BOOLEAN:
indices.set(uid, 1, (o) -> o.writeBoolean(loadedPropertyValue == null ? false : (boolean) loadedPropertyValue));
//System.err.println("Saving data Boo " + uid + ":" + loadedPropertyValue);
break;
case BYTE:
indices.set(uid, Byte.BYTES, (o) -> o.writeByte(loadedPropertyValue == null ? 0 : (byte) loadedPropertyValue));
//System.err.println("Saving data Byt " + uid + ":" + loadedPropertyValue);
break;
case SHORT:
indices.set(uid, Short.BYTES, (o) -> o.writeShort(loadedPropertyValue == null ? 0 : (short) loadedPropertyValue));
//System.err.println("Saving data Shr " + uid + ":" + loadedPropertyValue);
break;
case CHAR:
indices.set(uid, Character.BYTES, (o) -> o.writeChar(loadedPropertyValue == null ? 0 : (char) loadedPropertyValue));
//System.err.println("Saving data Chr " + uid + ":" + loadedPropertyValue);
break;
case INTEGER:
indices.set(uid, Integer.BYTES, (o) -> o.writeInt(loadedPropertyValue == null ? 0 : (int) loadedPropertyValue));
//System.err.println("Saving data Int " + uid + ":" + loadedPropertyValue);
break;
case LONG:
indices.set(uid, Long.BYTES, (o) -> o.writeLong(loadedPropertyValue == null ? 0 : (long) loadedPropertyValue));
//System.err.println("Saving data Lng " + uid + ":" + loadedPropertyValue);
break;
case OBJECT:
Output baosOutput = new Output(new ByteArrayOutputStream());
kryo.writeClassAndObject(baosOutput, loadedPropertyValue);
//System.err.println("Saving data Obj " + uid + ":" + loadedPropertyValue);
if (loadedPropertyValue instanceof Class) {
System.out.println();
}
byte[] out = baosOutput.toBytes();
indices.set(uid, out.length, o -> o.write(out, 0, out.length));
break;
case UID_LIST:
if (loadedPropertyValue == null) {
indices.set(uid, 0, (o) -> {});
} else {
LongArrayList list = (LongArrayList) loadedPropertyValue;
final int listSize = list.size();
Output baosListOutput = new Output(Long.BYTES * 100, Long.BYTES * (listSize > 100 ? listSize : 100));
baosListOutput.writeVarInt(listSize, true);
for (int i = 0; i < listSize; i++) {
baosListOutput.writeVarLong(list.getLong(i), true);
}
//System.err.println("Saving data LOb " + uid + ":" + loadedPropertyValue);
byte[] outList = baosListOutput.toBytes();
indices.set(uid, outList.length, o -> o.write(outList, 0, outList.length));
}
break;
case DATABASE_OBJECT:
//System.err.println("Saving data DBObj " + uid + ":" + loadedPropertyValue);
if (loadedPropertyValue == null) {
writeObjectInfoNull(uid);
} else {
((DBObject) loadedPropertyValue).writeToDisk(uid);
}
break;
}
}
public void registerClass(Class<?> clazz, int id) {
kryo.register(clazz, 100 + id);
}
public long allocateNullValue() {
return indices.add(0);
}
@Override
public long clean() {
return 0;//indices.clean();
}
public long[] allocateNewUIDs(int quantity) {
long[] ids = new long[quantity];
for (int i = 0; i < quantity; i++) {
ids[i] = allocateNullValue();
}
return ids;
}
}