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.serialization.Codec;
|
||||
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 java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@ -91,7 +92,7 @@ public class CodecsExample {
|
||||
|
||||
private static SubStageGetterSingle<CurrentCustomType> getNewSubStageGetter() {
|
||||
var newCodec = new NewCustomTypeCodecV2();
|
||||
var newCodecs = new Codecs<CurrentCustomType>();
|
||||
var newCodecs = new CodecsStandard<CurrentCustomType>();
|
||||
newCodecs.registerCodec(1, new NewCustomTypeCodecV1());
|
||||
newCodecs.registerCodec(2, newCodec);
|
||||
var newSerializer = new CodecSerializer<>(newCodecs, newCodec, 2, true);
|
||||
@ -100,7 +101,7 @@ public class CodecsExample {
|
||||
|
||||
private static SubStageGetterSingle<OldCustomType> getOldSubStageGetter() {
|
||||
var oldCodec = new OldCustomTypeCodec();
|
||||
var oldCodecs = new Codecs<OldCustomType>();
|
||||
var oldCodecs = new CodecsArray<OldCustomType>();
|
||||
oldCodecs.registerCodec(1, oldCodec);
|
||||
var oldSerializer = new CodecSerializer<>(oldCodecs, oldCodec, 1, true);
|
||||
return new SubStageGetterSingle<>(oldSerializer);
|
||||
|
@ -1,29 +1,8 @@
|
||||
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;
|
||||
public interface Codecs<A> {
|
||||
|
||||
@NotAtomic
|
||||
public class Codecs<A> {
|
||||
void registerCodec(int id, Codec<A> serializer);
|
||||
|
||||
private final Int2ObjectMap<Codec<A>> codecs;
|
||||
|
||||
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;
|
||||
}
|
||||
Codec<A> getCodec(int id);
|
||||
}
|
||||
|
@ -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…
Reference in New Issue
Block a user