Skip to content

Commit

Permalink
Merge pull request #223 from lorban/issue-222
Browse files Browse the repository at this point in the history
[closes #222] add API to encode/decode struct inside structs
  • Loading branch information
AbfrmBlr authored Dec 8, 2016
2 parents 99e9167 + 27f0c0a commit e9bd3e4
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 176 deletions.
8 changes: 4 additions & 4 deletions runnel/src/main/java/org/terracotta/runnel/Struct.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ List<? extends Field> getRootSubFields() {
* Note: this method is thread-safe.
* @return the encoder.
*/
public StructEncoder encoder() {
return new StructEncoder(root);
public StructEncoder<Void> encoder() {
return new StructEncoder<Void>(root);
}

/**
Expand All @@ -55,8 +55,8 @@ public StructEncoder encoder() {
* @param byteBuffer the byte buffer containing the data to be decoded.
* @return the decoder.
*/
public StructDecoder decoder(ByteBuffer byteBuffer) {
return new StructDecoder(root, new ReadBuffer(byteBuffer));
public StructDecoder<Void> decoder(ByteBuffer byteBuffer) {
return new StructDecoder<Void>(root, new ReadBuffer(byteBuffer));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
/**
* @author Ludovic Orban
*/
public class ArrayDecoder<T> {
public class ArrayDecoder<T, P> {

private final ValueField<T> arrayedField;
private final ReadBuffer readBuffer;
private final StructDecoder parent;
private final P parent;
private final int length;

public ArrayDecoder(ValueField<T> arrayedField, ReadBuffer readBuffer, StructDecoder parent) {
public ArrayDecoder(ValueField<T> arrayedField, ReadBuffer readBuffer, P parent) {
this.arrayedField = arrayedField;
this.parent = parent;
int size = readBuffer.getVlqInt();
Expand All @@ -45,7 +45,7 @@ public T value() {
return arrayedField.decode(readBuffer);
}

public StructDecoder end() {
public P end() {
readBuffer.skipAll();

return parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
/**
* @author Ludovic Orban
*/
public class StructArrayDecoder implements PrimitiveDecodingSupport {
public class StructArrayDecoder<P> implements PrimitiveDecodingSupport {
private final FieldDecoder fieldDecoder;
private final StructDecoder parent;
private final P parent;
private final ReadBuffer arrayReadBuffer;
private final int arrayLength;

private ReadBuffer structReadBuffer;

public StructArrayDecoder(StructField field, ReadBuffer readBuffer, StructDecoder parent) {
public StructArrayDecoder(StructField field, ReadBuffer readBuffer, P parent) {
this.parent = parent;

int arraySize = readBuffer.getVlqInt();
Expand Down Expand Up @@ -100,11 +100,15 @@ public ByteBuffer byteBuffer(String name) {
return fieldDecoder.decodeValue(name, ByteBufferField.class);
}

public StructDecoder<StructArrayDecoder<P>> struct(String name) {
return fieldDecoder.decodeStruct(name, this);
}

public int length() {
return arrayLength;
}

public StructDecoder end() {
public P end() {
arrayReadBuffer.skipAll();
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
* A decoder allows decoding structured data described by a {@link org.terracotta.runnel.Struct}.
* Note: Instances of this class are not thread-safe.
*/
public class StructDecoder implements PrimitiveDecodingSupport {
public class StructDecoder<P> implements PrimitiveDecodingSupport {

private final FieldDecoder fieldDecoder;
private final ReadBuffer readBuffer;
private final StructDecoder parent;
private final P parent;

public StructDecoder(StructField structField, ReadBuffer readBuffer) {
this(structField, readBuffer, null);
}

public StructDecoder(StructField structField, ReadBuffer readBuffer, StructDecoder parent) {
public StructDecoder(StructField structField, ReadBuffer readBuffer, P parent) {
this.parent = parent;
int size = readBuffer.getVlqInt();
this.readBuffer = readBuffer.limit(size);
Expand Down Expand Up @@ -95,39 +95,39 @@ public ByteBuffer byteBuffer(String name) {
}


public ArrayDecoder<Integer> int32s(String name) {
public ArrayDecoder<Integer, StructDecoder<P>> int32s(String name) {
return fieldDecoder.decodeValueArray(name, Int32Field.class, this);
}

public ArrayDecoder<Boolean> bools(String name) {
public ArrayDecoder<Boolean, StructDecoder<P>> bools(String name) {
return fieldDecoder.decodeValueArray(name, BoolField.class, this);
}

public ArrayDecoder<Character> chrs(String name) {
public ArrayDecoder<Character, StructDecoder<P>> chrs(String name) {
return fieldDecoder.decodeValueArray(name, CharField.class, this);
}

public ArrayDecoder<Long> int64s(String name) {
public ArrayDecoder<Long, StructDecoder<P>> int64s(String name) {
return fieldDecoder.decodeValueArray(name, Int64Field.class, this);
}

public ArrayDecoder<Double> fp64s(String name) {
public ArrayDecoder<Double, StructDecoder<P>> fp64s(String name) {
return fieldDecoder.decodeValueArray(name, FloatingPoint64Field.class, this);
}

public ArrayDecoder<String> strings(String name) {
public ArrayDecoder<String, StructDecoder<P>> strings(String name) {
return fieldDecoder.decodeValueArray(name, StringField.class, this);
}

public StructDecoder struct(String name) {
public StructDecoder<StructDecoder<P>> struct(String name) {
return fieldDecoder.decodeStruct(name, this);
}

public StructArrayDecoder structs(String name) {
public StructArrayDecoder<StructDecoder<P>> structs(String name) {
return fieldDecoder.decodeStructArray(name, this);
}

public StructDecoder end() {
public P end() {
if (parent == null) {
throw new IllegalStateException("Cannot end root decoder");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@
/**
* @author Ludovic Orban
*/
public abstract class ArrayEncoder<T> {
public abstract class ArrayEncoder<T, P> {

private final StructEncoder parent;
private final P parent;
private final List<DataHolder> values;

ArrayEncoder(List<DataHolder> values, StructEncoder parent) {
ArrayEncoder(List<DataHolder> values, P parent) {
this.values = values;
this.parent = parent;
}

public ArrayEncoder<T> value(T value) {
public ArrayEncoder<T, P> value(T value) {
DataHolder dataHolder = buildDataHolder(value);
this.values.add(dataHolder);
return this;
}

protected abstract DataHolder buildDataHolder(T value);

public StructEncoder end() {
public P end() {
return parent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,85 +43,92 @@
/**
* @author Ludovic Orban
*/
public class StructArrayEncoder implements PrimitiveEncodingSupport<StructArrayEncoder> {
public class StructArrayEncoder<P> implements PrimitiveEncodingSupport<StructArrayEncoder> {
private static final int ARRAY_INITIAL_SIZE = 16;

private final List<StructDataHolder> values;
private final StructEncoder parent;
private final P parent;
private final FieldSearcher fieldSearcher;
private List<DataHolder> currentData;

StructArrayEncoder(List<StructDataHolder> values, StructEncoder parent, StructField structField) {
StructArrayEncoder(List<StructDataHolder> values, P parent, StructField structField) {
this.values = values;
this.parent = parent;
this.fieldSearcher = structField.getMetadata().fieldSearcher();
this.currentData = new ArrayList<DataHolder>(ARRAY_INITIAL_SIZE);
}

@Override
public StructArrayEncoder bool(String name, boolean value) {
public StructArrayEncoder<P> bool(String name, boolean value) {
BoolField field = fieldSearcher.findField(name, BoolField.class, null);
currentData.add(new BoolDataHolder(value, field.index()));
return this;
}

@Override
public StructArrayEncoder chr(String name, char value) {
public StructArrayEncoder<P> chr(String name, char value) {
CharField field = fieldSearcher.findField(name, CharField.class, null);
currentData.add(new CharDataHolder(value, field.index()));
return this;
}

@Override
public <E> StructArrayEncoder enm(String name, E value) {
public <E> StructArrayEncoder<P> enm(String name, E value) {
EnumField<E> field = (EnumField<E>) fieldSearcher.findField(name, EnumField.class, null);
currentData.add(new EnumDataHolder<E>(value, field.index(), field.getEnumMapping()));
return this;
}

@Override
public StructArrayEncoder int32(String name, int value) {
public StructArrayEncoder<P> int32(String name, int value) {
Int32Field field = fieldSearcher.findField(name, Int32Field.class, null);
currentData.add(new Int32DataHolder(value, field.index()));
return this;
}

@Override
public StructArrayEncoder int64(String name, long value) {
public StructArrayEncoder<P> int64(String name, long value) {
Int64Field field = fieldSearcher.findField(name, Int64Field.class, null);
currentData.add(new Int64DataHolder(value, field.index()));
return this;
}

@Override
public StructArrayEncoder fp64(String name, double value) {
public StructArrayEncoder<P> fp64(String name, double value) {
FloatingPoint64Field field = fieldSearcher.findField(name, FloatingPoint64Field.class, null);
currentData.add(new FloatingPoint64DataHolder(value, field.index()));
return this;
}

@Override
public StructArrayEncoder string(String name, String value) {
public StructArrayEncoder<P> string(String name, String value) {
StringField field = fieldSearcher.findField(name, StringField.class, null);
currentData.add(new StringDataHolder(value, field.index()));
return this;
}

@Override
public StructArrayEncoder byteBuffer(String name, ByteBuffer value) {
public StructArrayEncoder<P> byteBuffer(String name, ByteBuffer value) {
ByteBufferField field = fieldSearcher.findField(name, ByteBufferField.class, null);
currentData.add(new ByteBufferDataHolder(value, field.index()));
return this;
}

public StructArrayEncoder next() {
public StructEncoder<StructArrayEncoder<P>> struct(String name) {
StructField field = fieldSearcher.findField(name, StructField.class, null);
List<DataHolder> values = new ArrayList<DataHolder>();
currentData.add(new StructDataHolder(values, field.index()));
return new StructEncoder<StructArrayEncoder<P>>(field, values, this);
}

public StructArrayEncoder<P> next() {
fieldSearcher.reset();
values.add(new StructDataHolder(currentData, -1));
currentData = new ArrayList<DataHolder>(ARRAY_INITIAL_SIZE);
return this;
}

public StructEncoder end() {
public P end() {
if (!currentData.isEmpty()) {
values.add(new StructDataHolder(currentData, -1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* @author Ludovic Orban
*/
public interface StructArrayEncoderFunction<T> {
public interface StructArrayEncoderFunction<T, P extends PrimitiveEncodingSupport<?>> {

void encode(PrimitiveEncodingSupport<PrimitiveEncodingSupport> encoder, T t);
void encode(P encoder, T t);

}
Loading

0 comments on commit e9bd3e4

Please sign in to comment.