Reimplement query serializer

This commit is contained in:
Andrea Cavalli 2021-06-20 01:09:06 +02:00
parent 924dbf6bf6
commit 03d3afa317
2 changed files with 79 additions and 1 deletions

View File

@ -0,0 +1,35 @@
package it.cavallium.dbengine.client;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.cavallium.data.generator.nativedata.Int52;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class IntOpenHashSetJsonAdapter extends com.squareup.moshi.JsonAdapter<IntOpenHashSet> {
@Nullable
@Override
public IntOpenHashSet fromJson(@NotNull JsonReader reader) throws IOException {
var intOpenHashSet = new IntOpenHashSet();
while (reader.hasNext()) {
intOpenHashSet.add(reader.nextInt());
}
return intOpenHashSet;
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable IntOpenHashSet value) throws IOException {
if (value == null) {
writer.nullValue();
} else {
writer.beginArray();
for (int integer : value) {
writer.value(integer);
}
writer.endArray();
}
}
}

View File

@ -1,21 +1,43 @@
package it.cavallium.dbengine.client.query; package it.cavallium.dbengine.client.query;
import com.squareup.moshi.JsonAdapter;
import it.cavallium.dbengine.client.IntOpenHashSetJsonAdapter;
import it.cavallium.dbengine.client.query.current.CurrentVersion; import it.cavallium.dbengine.client.query.current.CurrentVersion;
import it.cavallium.dbengine.client.query.current.data.IBasicType; import it.cavallium.dbengine.client.query.current.data.IBasicType;
import it.cavallium.dbengine.client.query.current.data.IType; import it.cavallium.dbengine.client.query.current.data.IType;
import it.unimi.dsi.fastutil.booleans.BooleanList;
import it.unimi.dsi.fastutil.bytes.ByteList;
import it.unimi.dsi.fastutil.chars.CharList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.fastutil.shorts.ShortList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.warp.commonutils.moshi.BooleanListJsonAdapter;
import org.warp.commonutils.moshi.ByteListJsonAdapter;
import org.warp.commonutils.moshi.CharListJsonAdapter;
import org.warp.commonutils.moshi.IntListJsonAdapter;
import org.warp.commonutils.moshi.LongListJsonAdapter;
import org.warp.commonutils.moshi.MoshiPolymorphic; import org.warp.commonutils.moshi.MoshiPolymorphic;
import org.warp.commonutils.moshi.ShortListJsonAdapter;
public class QueryMoshi extends MoshiPolymorphic<IType> { public class QueryMoshi extends MoshiPolymorphic<IType> {
private final Set<Class<IType>> abstractClasses; private final Set<Class<IType>> abstractClasses;
private final Set<Class<IType>> concreteClasses; private final Set<Class<IType>> concreteClasses;
private final Map<Class<?>, JsonAdapter<?>> extraAdapters;
@SuppressWarnings({"unchecked", "RedundantCast", "rawtypes"}) @SuppressWarnings({"unchecked", "RedundantCast", "rawtypes"})
public QueryMoshi() { public QueryMoshi() {
super(true, true);
HashSet<Class<IType>> abstractClasses = new HashSet<>(); HashSet<Class<IType>> abstractClasses = new HashSet<>();
HashSet<Class<IType>> concreteClasses = new HashSet<>(); HashSet<Class<IType>> concreteClasses = new HashSet<>();
// Add all super types with their implementations
for (var superTypeClass : CurrentVersion.getSuperTypeClasses()) { for (var superTypeClass : CurrentVersion.getSuperTypeClasses()) {
for (Class<? extends IBasicType> superTypeSubtypesClass : CurrentVersion.getSuperTypeSubtypesClasses( for (Class<? extends IBasicType> superTypeSubtypesClass : CurrentVersion.getSuperTypeSubtypesClasses(
superTypeClass)) { superTypeClass)) {
@ -23,8 +45,29 @@ public class QueryMoshi extends MoshiPolymorphic<IType> {
} }
abstractClasses.add((Class<IType>) (Class) superTypeClass); abstractClasses.add((Class<IType>) (Class) superTypeClass);
} }
// Add IBasicType with all basic types
abstractClasses.add((Class<IType>) (Class) IBasicType.class);
for (BasicType basicType : BasicType.values()) {
concreteClasses.add((Class<IType>) (Class) CurrentVersion.VERSION.getClass(basicType));
}
this.abstractClasses = abstractClasses; this.abstractClasses = abstractClasses;
this.concreteClasses = concreteClasses; this.concreteClasses = concreteClasses;
Map<Class<?>, JsonAdapter<?>> extraAdapters = new HashMap<>();
extraAdapters.put(BooleanList.class, new BooleanListJsonAdapter());
extraAdapters.put(ByteList.class, new ByteListJsonAdapter());
extraAdapters.put(ShortList.class, new ShortListJsonAdapter());
extraAdapters.put(CharList.class, new CharListJsonAdapter());
extraAdapters.put(IntList.class, new IntListJsonAdapter());
extraAdapters.put(LongList.class, new LongListJsonAdapter());
extraAdapters.put(IntOpenHashSet.class, new IntOpenHashSetJsonAdapter());
this.extraAdapters = Collections.unmodifiableMap(extraAdapters);
}
@Override
public Map<Class<?>, JsonAdapter<?>> getExtraAdapters() {
return extraAdapters;
} }
@Override @Override
@ -39,6 +82,6 @@ public class QueryMoshi extends MoshiPolymorphic<IType> {
@Override @Override
protected boolean shouldIgnoreField(String fieldName) { protected boolean shouldIgnoreField(String fieldName) {
return false; return fieldName.contains("$");
} }
} }