use of herddb.utils.RawString in project herddb by diennea.
the class RecordSerializer method serializeValue.
public static void serializeValue(Object v, int type, ExtendedDataOutputStream oo) throws IOException {
switch(type) {
case ColumnTypes.BYTEARRAY:
oo.writeArray((byte[]) v);
break;
case ColumnTypes.INTEGER:
if (v instanceof Integer) {
oo.writeInt((Integer) v);
} else if (v instanceof Number) {
oo.writeInt(((Number) v).intValue());
} else {
oo.writeInt(Integer.parseInt(v.toString()));
}
break;
case ColumnTypes.LONG:
if (v instanceof Integer) {
oo.writeLong((Integer) v);
} else if (v instanceof Number) {
oo.writeLong(((Number) v).longValue());
} else {
oo.writeLong(Long.parseLong(v.toString()));
}
break;
case ColumnTypes.STRING:
if (v instanceof RawString) {
RawString rs = (RawString) v;
oo.writeArray(rs.data);
} else {
oo.writeArray(Bytes.string_to_array(v.toString()));
}
break;
case ColumnTypes.TIMESTAMP:
if (!(v instanceof java.sql.Timestamp)) {
throw new IllegalArgumentException("bad value type for column " + type + ": required java.sql.Timestamp, but was " + v.getClass() + ", toString of value is " + v);
}
oo.writeLong(((java.sql.Timestamp) v).getTime());
break;
case ColumnTypes.BOOLEAN:
if (v instanceof Boolean) {
oo.writeBoolean((Boolean) v);
} else {
oo.writeBoolean(Boolean.parseBoolean(v.toString()));
}
break;
case ColumnTypes.DOUBLE:
if (v instanceof Integer) {
oo.writeDouble((Integer) v);
} else if (v instanceof Number) {
oo.writeDouble(((Number) v).doubleValue());
} else {
oo.writeDouble(Double.parseDouble(v.toString()));
}
break;
default:
throw new IllegalArgumentException("bad column type " + type);
}
}
use of herddb.utils.RawString in project herddb by diennea.
the class Tuple method serialize.
public static VisibleByteArrayOutputStream serialize(DataAccessor tuple, Column[] columns) throws IOException {
VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
try (ExtendedDataOutputStream eoo = new ExtendedDataOutputStream(oo)) {
int i = 0;
String[] fieldNames = tuple.getFieldNames();
for (String fieldName : fieldNames) {
if (!columns[i].name.toLowerCase().equals(fieldName)) {
throw new IOException("invalid schema for tuple " + Arrays.toString(fieldNames) + " <> " + Arrays.toString(columns));
}
Object value = tuple.get(fieldName);
if (value == null) {
eoo.writeVInt(ColumnTypes.NULL);
} else {
byte columnType;
if (value instanceof String) {
columnType = ColumnTypes.STRING;
} else if (value instanceof RawString) {
columnType = ColumnTypes.STRING;
} else if (value instanceof Integer) {
columnType = ColumnTypes.INTEGER;
} else if (value instanceof Long) {
columnType = ColumnTypes.LONG;
} else if (value instanceof java.sql.Timestamp) {
columnType = ColumnTypes.TIMESTAMP;
} else if (value instanceof Double) {
columnType = ColumnTypes.DOUBLE;
} else if (value instanceof Boolean) {
columnType = ColumnTypes.BOOLEAN;
} else if (value instanceof byte[]) {
columnType = ColumnTypes.BYTEARRAY;
} else {
throw new IOException("unsupported class " + value.getClass());
}
RecordSerializer.serializeTypeAndValue(value, columnType, eoo);
}
i++;
}
}
return oo;
}
use of herddb.utils.RawString 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());
}
}
use of herddb.utils.RawString in project herddb by diennea.
the class MessageUtilsTest method testEncodeMessage.
@Test
public void testEncodeMessage() {
final String clientId = "2331";
System.out.println("encodeMessage");
ByteBuf buffer = Unpooled.buffer();
Map<String, Object> payload = new HashMap<>();
payload.put("string", "value");
payload.put("int", 1234);
payload.put("long", 12345L);
payload.put("rawstring", RawString.of("value"));
payload.put("list", Arrays.asList("foo", "bar"));
payload.put("set", new HashSet<>(Arrays.asList("foo", "bar")));
String[] colNames = { "one", "null", "two", "notfound" };
List<DataAccessor> records = new ArrayList<>();
Map<String, Object> record1 = new HashMap<>();
record1.put("one", 1234);
record1.put("two", RawString.of("test"));
records.add(new MapDataAccessor(record1, colNames));
Map<String, Object> record2 = new HashMap<>();
record2.put("one", 2234);
record2.put("two", RawString.of("test2"));
record2.put("null", null);
records.add(new MapDataAccessor(record2, colNames));
TuplesList tl = new TuplesList(colNames, records);
payload.put("data", tl);
Message m = new Message(clientId, 1234, payload);
m.assignMessageId();
m.setReplyMessageId("2343");
MessageUtils.encodeMessage(buffer, m);
Message read = MessageUtils.decodeMessage(buffer);
assertEquals(read.clientId, m.clientId);
assertEquals(read.messageId, m.messageId);
assertEquals(read.replyMessageId, m.replyMessageId);
assertEquals(read.type, m.type);
assertEquals(read.parameters.size(), m.parameters.size());
read.parameters.forEach((String k, Object v) -> {
Object o = m.parameters.get(k);
assertEquals(o, v);
});
TuplesList tl2 = (TuplesList) read.parameters.get("data");
assertEquals(4, tl2.tuples.get(0).getValues().length);
assertArrayEquals(colNames, tl2.tuples.get(0).getFieldNames());
assertEquals(4, tl2.tuples.get(1).getValues().length);
assertArrayEquals(colNames, tl2.tuples.get(1).getFieldNames());
}
Aggregations