Update CodecsExample.java, Codecs.java, and 2 more files...
This commit is contained in:
parent
151884b772
commit
527f8afea5
@ -11,7 +11,8 @@ import it.cavallium.dbengine.database.collections.SubStageGetterSingleBytes;
|
|||||||
import it.cavallium.dbengine.database.disk.LLLocalDatabaseConnection;
|
import it.cavallium.dbengine.database.disk.LLLocalDatabaseConnection;
|
||||||
import it.cavallium.dbengine.database.serialization.Codec;
|
import it.cavallium.dbengine.database.serialization.Codec;
|
||||||
import it.cavallium.dbengine.database.serialization.CodecSerializer;
|
import it.cavallium.dbengine.database.serialization.CodecSerializer;
|
||||||
import it.cavallium.dbengine.database.serialization.Codecs;
|
import it.cavallium.dbengine.database.serialization.CodecsArray;
|
||||||
|
import it.cavallium.dbengine.database.serialization.CodecsStandard;
|
||||||
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -91,7 +92,7 @@ public class CodecsExample {
|
|||||||
|
|
||||||
private static SubStageGetterSingle<CurrentCustomType> getNewSubStageGetter() {
|
private static SubStageGetterSingle<CurrentCustomType> getNewSubStageGetter() {
|
||||||
var newCodec = new NewCustomTypeCodecV2();
|
var newCodec = new NewCustomTypeCodecV2();
|
||||||
var newCodecs = new Codecs<CurrentCustomType>();
|
var newCodecs = new CodecsStandard<CurrentCustomType>();
|
||||||
newCodecs.registerCodec(1, new NewCustomTypeCodecV1());
|
newCodecs.registerCodec(1, new NewCustomTypeCodecV1());
|
||||||
newCodecs.registerCodec(2, newCodec);
|
newCodecs.registerCodec(2, newCodec);
|
||||||
var newSerializer = new CodecSerializer<>(newCodecs, newCodec, 2, true);
|
var newSerializer = new CodecSerializer<>(newCodecs, newCodec, 2, true);
|
||||||
@ -100,7 +101,7 @@ public class CodecsExample {
|
|||||||
|
|
||||||
private static SubStageGetterSingle<OldCustomType> getOldSubStageGetter() {
|
private static SubStageGetterSingle<OldCustomType> getOldSubStageGetter() {
|
||||||
var oldCodec = new OldCustomTypeCodec();
|
var oldCodec = new OldCustomTypeCodec();
|
||||||
var oldCodecs = new Codecs<OldCustomType>();
|
var oldCodecs = new CodecsArray<OldCustomType>();
|
||||||
oldCodecs.registerCodec(1, oldCodec);
|
oldCodecs.registerCodec(1, oldCodec);
|
||||||
var oldSerializer = new CodecSerializer<>(oldCodecs, oldCodec, 1, true);
|
var oldSerializer = new CodecSerializer<>(oldCodecs, oldCodec, 1, true);
|
||||||
return new SubStageGetterSingle<>(oldSerializer);
|
return new SubStageGetterSingle<>(oldSerializer);
|
||||||
|
@ -1,29 +1,8 @@
|
|||||||
package it.cavallium.dbengine.database.serialization;
|
package it.cavallium.dbengine.database.serialization;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
public interface Codecs<A> {
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
||||||
import org.warp.commonutils.concurrency.atomicity.NotAtomic;
|
|
||||||
|
|
||||||
@NotAtomic
|
void registerCodec(int id, Codec<A> serializer);
|
||||||
public class Codecs<A> {
|
|
||||||
|
|
||||||
private final Int2ObjectMap<Codec<A>> codecs;
|
Codec<A> getCodec(int id);
|
||||||
|
|
||||||
public Codecs() {
|
|
||||||
codecs = new Int2ObjectOpenHashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCodec(int id, Codec<A> serializer) {
|
|
||||||
if (codecs.put(id, serializer) != null) {
|
|
||||||
throw new IllegalArgumentException("Codec " + id + " already registered!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Codec<A> getCodec(int id) {
|
|
||||||
var codec = codecs.get(id);
|
|
||||||
if (codec == null) {
|
|
||||||
throw new UnsupportedOperationException("Unsupported codec " + id);
|
|
||||||
}
|
|
||||||
return codec;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package it.cavallium.dbengine.database.serialization;
|
||||||
|
|
||||||
|
import org.warp.commonutils.concurrency.atomicity.NotAtomic;
|
||||||
|
|
||||||
|
@NotAtomic
|
||||||
|
public class CodecsArray<A> implements Codecs<A> {
|
||||||
|
|
||||||
|
private final Codec<A>[] codecs;
|
||||||
|
|
||||||
|
public CodecsArray() {
|
||||||
|
//noinspection unchecked
|
||||||
|
codecs = new Codec[256];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void registerCodec(int id, Codec<A> serializer) {
|
||||||
|
if (codecs[id] != null) {
|
||||||
|
throw new IllegalArgumentException("Codec " + id + " already registered!");
|
||||||
|
}
|
||||||
|
codecs[id] = serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<A> getCodec(int id) {
|
||||||
|
var codec = codecs[id];
|
||||||
|
if (codec == null) {
|
||||||
|
throw new UnsupportedOperationException("Unsupported codec " + id);
|
||||||
|
}
|
||||||
|
return codec;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package it.cavallium.dbengine.database.serialization;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
|
import org.warp.commonutils.concurrency.atomicity.NotAtomic;
|
||||||
|
|
||||||
|
@NotAtomic
|
||||||
|
public class CodecsStandard<A> implements Codecs<A> {
|
||||||
|
|
||||||
|
private final Int2ObjectMap<Codec<A>> codecs;
|
||||||
|
|
||||||
|
public CodecsStandard() {
|
||||||
|
codecs = new Int2ObjectOpenHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerCodec(int id, Codec<A> serializer) {
|
||||||
|
if (codecs.put(id, serializer) != null) {
|
||||||
|
throw new IllegalArgumentException("Codec " + id + " already registered!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<A> getCodec(int id) {
|
||||||
|
var codec = codecs.get(id);
|
||||||
|
if (codec == null) {
|
||||||
|
throw new UnsupportedOperationException("Unsupported codec " + id);
|
||||||
|
}
|
||||||
|
return codec;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user