use of herddb.network.KeyValue in project herddb by diennea.
the class MessageUtils method writeEncodedSimpleValue.
@SuppressWarnings("rawtypes")
private static void writeEncodedSimpleValue(ByteBuf output, Object o) {
if (o == null) {
output.writeByte(OPCODE_NULL_VALUE);
} else if (o instanceof String) {
output.writeByte(OPCODE_STRING_VALUE);
writeUTF8String(output, (String) o);
} else if (o instanceof RawString) {
output.writeByte(OPCODE_STRING_VALUE);
writeUTF8String(output, (RawString) o);
} else if (o instanceof java.sql.Timestamp) {
output.writeByte(OPCODE_TIMESTAMP_VALUE);
ByteBufUtils.writeVLong(output, ((java.sql.Timestamp) o).getTime());
} else if (o instanceof java.lang.Byte) {
output.writeByte(OPCODE_BYTE_VALUE);
output.writeByte(((Byte) o));
} else if (o instanceof KeyValue) {
KeyValue kv = (KeyValue) o;
output.writeByte(OPCODE_KEYVALUE_VALUE);
ByteBufUtils.writeArray(output, kv.key);
ByteBufUtils.writeArray(output, kv.value);
} else if (o instanceof Integer) {
int i = (int) o;
if (i < 0) {
if (i < WRITE_MIN_Z_INT_LIMIT) {
output.writeByte(OPCODE_INT_VALUE);
output.writeInt(i);
} else {
output.writeByte(OPCODE_Z_INT_VALUE);
ByteBufUtils.writeZInt(output, i);
}
} else {
if (i > WRITE_MAX_V_INT_LIMIT) {
output.writeByte(OPCODE_INT_VALUE);
output.writeInt(i);
} else {
output.writeByte(OPCODE_V_INT_VALUE);
ByteBufUtils.writeVInt(output, i);
}
}
} else if (o instanceof Long) {
long l = (long) o;
if (l < 0) {
if (l < WRITE_MIN_Z_LONG_LIMIT) {
output.writeByte(OPCODE_LONG_VALUE);
output.writeLong(l);
} else {
output.writeByte(OPCODE_Z_LONG_VALUE);
ByteBufUtils.writeZLong(output, l);
}
} else {
if (l > WRITE_MAX_V_LONG_LIMIT) {
output.writeByte(OPCODE_LONG_VALUE);
output.writeLong(l);
} else {
output.writeByte(OPCODE_V_LONG_VALUE);
ByteBufUtils.writeVLong(output, l);
}
}
} else if (o instanceof Boolean) {
output.writeByte(OPCODE_BOOLEAN_VALUE);
output.writeByte(((Boolean) o).booleanValue() ? 1 : 0);
} else if (o instanceof Double) {
output.writeByte(OPCODE_DOUBLE_VALUE);
ByteBufUtils.writeDouble(output, ((Double) o).doubleValue());
} else if (o instanceof Set) {
Set set = (Set) o;
output.writeByte(OPCODE_SET_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Object o2 : set) {
writeEncodedSimpleValue(output, o2);
}
} else if (o instanceof List) {
List set = (List) o;
output.writeByte(OPCODE_LIST_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Object o2 : set) {
writeEncodedSimpleValue(output, o2);
}
} else if (o instanceof Map) {
Map set = (Map) o;
output.writeByte(OPCODE_MAP_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Map.Entry entry : (Iterable<Map.Entry>) set.entrySet()) {
writeEncodedSimpleValue(output, entry.getKey());
writeEncodedSimpleValue(output, entry.getValue());
}
} else if (o instanceof DataAccessor) {
DataAccessor set = (DataAccessor) o;
output.writeByte(OPCODE_MAP2_VALUE);
// number of entries is not known
set.forEach((key, value) -> {
writeEncodedSimpleValue(output, key);
writeEncodedSimpleValue(output, value);
});
output.writeByte(OPCODE_MAP2_VALUE_END);
} else if (o instanceof TuplesList) {
TuplesList set = (TuplesList) o;
output.writeByte(OPCODE_TUPLELIST_VALUE);
final int numColumns = set.columnNames.length;
ByteBufUtils.writeVInt(output, numColumns);
for (String columnName : set.columnNames) {
writeUTF8String(output, columnName);
}
ByteBufUtils.writeVInt(output, set.tuples.size());
for (DataAccessor da : set.tuples) {
IntHolder currentColumn = new IntHolder();
da.forEach((String key, Object value) -> {
String expectedColumnName = set.columnNames[currentColumn.value];
while (!key.equals(expectedColumnName)) {
// nulls are not returned for some special accessors, lie DataAccessorForFullRecord
writeEncodedSimpleValue(output, null);
currentColumn.value++;
expectedColumnName = set.columnNames[currentColumn.value];
}
writeEncodedSimpleValue(output, value);
currentColumn.value++;
});
// fill with nulls
while (currentColumn.value < numColumns) {
writeEncodedSimpleValue(output, null);
currentColumn.value++;
}
if (currentColumn.value > numColumns) {
throw new RuntimeException("unexpected number of columns " + currentColumn.value + " > " + numColumns);
}
}
} else if (o instanceof byte[]) {
byte[] set = (byte[]) o;
output.writeByte(OPCODE_BYTEARRAY_VALUE);
ByteBufUtils.writeArray(output, set);
} else {
throw new RuntimeException("unsupported class " + o.getClass());
}
}
Aggregations