io.datakernel.aggregation.AggregationChunkJson Maven / Gradle / Ivy
package io.datakernel.aggregation;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static io.datakernel.util.Preconditions.checkArgument;
import static io.datakernel.util.gson.GsonAdapters.STRING_JSON;
import static io.datakernel.util.gson.GsonAdapters.ofList;
public class AggregationChunkJson extends TypeAdapter {
public static final String ID = "id";
public static final String MIN = "min";
public static final String MAX = "max";
public static final String COUNT = "count";
public static final String MEASURES = "measures";
private final TypeAdapter primaryKeyTypeAdapter;
private final TypeAdapter> stringListAdapter;
private final Set allowedMeasures;
private AggregationChunkJson(TypeAdapter primaryKeyTypeAdapter, TypeAdapter> stringListAdapter, Set allowedMeasures) {
this.primaryKeyTypeAdapter = primaryKeyTypeAdapter;
this.stringListAdapter = stringListAdapter;
this.allowedMeasures = allowedMeasures;
}
public static AggregationChunkJson create(TypeAdapter primaryKeyTypeAdapter, Set allowedMeasures) {
return new AggregationChunkJson(primaryKeyTypeAdapter, ofList(STRING_JSON), allowedMeasures);
}
@Override
public void write(JsonWriter writer, AggregationChunk chunk) throws IOException {
writer.beginObject();
writer.name(ID);
writer.value(chunk.getChunkId());
writer.name(MIN);
primaryKeyTypeAdapter.write(writer, chunk.getMinPrimaryKey());
writer.name(MAX);
primaryKeyTypeAdapter.write(writer, chunk.getMaxPrimaryKey());
writer.name(COUNT);
writer.value(chunk.getCount());
writer.name(MEASURES);
stringListAdapter.write(writer, chunk.getMeasures());
writer.endObject();
}
@Override
public AggregationChunk read(JsonReader reader) throws IOException {
reader.beginObject();
checkArgument(ID.equals(reader.nextName()));
int id = reader.nextInt();
checkArgument(MIN.equals(reader.nextName()));
PrimaryKey from = primaryKeyTypeAdapter.read(reader);
checkArgument(MAX.equals(reader.nextName()));
PrimaryKey to = primaryKeyTypeAdapter.read(reader);
checkArgument(COUNT.equals(reader.nextName()));
int count = reader.nextInt();
checkArgument(MEASURES.equals(reader.nextName()));
List measures = stringListAdapter.read(reader);
List invalidMeasures = getInvalidMeasures(measures);
if (!invalidMeasures.isEmpty()) throw new IOException("Unknown fields: " + invalidMeasures);
reader.endObject();
return AggregationChunk.create(id, measures, from, to, count);
}
private List getInvalidMeasures(List measures) {
List invalidMeasures = new ArrayList<>();
for (String measure : measures) {
if (!allowedMeasures.contains(measure)) {
invalidMeasures.add(measure);
}
}
return invalidMeasures;
}
}