59 lines
1.6 KiB
Java
59 lines
1.6 KiB
Java
package org.warp.jcwdb;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import com.esotericsoftware.kryo.Kryo;
|
|
import com.esotericsoftware.kryo.io.Output;
|
|
|
|
import net.openhft.hashing.LongHashFunction;
|
|
|
|
public class DBGenericObjectParser extends DBTypeParserImpl<Object> implements DBTypedObjectParser<Object> {
|
|
private static final LongHashFunction hashFunction = net.openhft.hashing.LongHashFunction.xx();
|
|
private static final Kryo kryo = new Kryo();
|
|
static {
|
|
kryo.setRegistrationRequired(false);
|
|
}
|
|
|
|
private static final DBReader<Object> defaultReader = (i, size) -> {
|
|
return kryo.readClassAndObject(i);
|
|
};
|
|
|
|
public DBReader<Object> getReader() {
|
|
return defaultReader;
|
|
}
|
|
|
|
public DBDataOutput<Object> getWriter(final Object value) {
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
Output tmpO = new Output(baos);
|
|
kryo.writeClassAndObject(tmpO, value);
|
|
tmpO.flush();
|
|
final byte[] bytes = baos.toByteArray();
|
|
final long hash = hashFunction.hashBytes(bytes);
|
|
tmpO.close();
|
|
return DBDataOutput.create((o) -> {
|
|
o.write(bytes);
|
|
}, DBStandardTypes.GENERIC_OBJECT, bytes.length, hash);
|
|
}
|
|
|
|
@Override
|
|
public long calculateHash(Object value) {
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
Output tmpO = new Output(baos);
|
|
kryo.writeClassAndObject(tmpO, value);
|
|
tmpO.flush();
|
|
final byte[] bytes = baos.toByteArray();
|
|
final long hash = hashFunction.hashBytes(bytes);
|
|
tmpO.close();
|
|
return hash;
|
|
}
|
|
|
|
|
|
@Override
|
|
public <U> void registerClass(Class<U> clazz, int id) {
|
|
if (id >= Integer.MAX_VALUE - 100) {
|
|
throw new IndexOutOfBoundsException();
|
|
}
|
|
kryo.register(clazz, id + 100);
|
|
}
|
|
}
|