use of herddb.utils.MapDataAccessor in project herddb by diennea.
the class MessageUtils method readEncodedSimpleValue.
private static Object readEncodedSimpleValue(byte _opcode, ByteBuf encoded) {
switch(_opcode) {
case OPCODE_NULL_VALUE:
return null;
case OPCODE_STRING_VALUE:
return readUTF8String(encoded);
case OPCODE_INT_VALUE:
return encoded.readInt();
case OPCODE_V_INT_VALUE:
return ByteBufUtils.readVInt(encoded);
case OPCODE_Z_INT_VALUE:
return ByteBufUtils.readZInt(encoded);
case OPCODE_LONG_VALUE:
return encoded.readLong();
case OPCODE_V_LONG_VALUE:
return ByteBufUtils.readVLong(encoded);
case OPCODE_Z_LONG_VALUE:
return ByteBufUtils.readZLong(encoded);
case OPCODE_BOOLEAN_VALUE:
return encoded.readByte() == 1;
case OPCODE_DOUBLE_VALUE:
return ByteBufUtils.readDouble(encoded);
case OPCODE_MAP_VALUE:
{
int len = ByteBufUtils.readVInt(encoded);
Map<Object, Object> ret = new HashMap<>();
for (int i = 0; i < len; i++) {
Object mapkey = readEncodedSimpleValue(encoded);
Object value = readEncodedSimpleValue(encoded);
ret.put(mapkey, value);
}
return ret;
}
case OPCODE_TUPLELIST_VALUE:
{
int numColumns = ByteBufUtils.readVInt(encoded);
String[] columns = new String[numColumns];
for (int i = 0; i < numColumns; i++) {
columns[i] = readUTF8String(encoded);
}
int numRecords = ByteBufUtils.readVInt(encoded);
List<DataAccessor> records = new ArrayList<>(numRecords);
for (int i = 0; i < numRecords; i++) {
Map<String, Object> map = new HashMap<>();
for (int j = 0; j < numColumns; j++) {
Object value = readEncodedSimpleValue(encoded);
map.put(columns[j], value);
}
records.add(new MapDataAccessor(map, columns));
}
return new TuplesList(columns, records);
}
case OPCODE_MAP2_VALUE:
{
Map<Object, Object> ret = new HashMap<>();
while (true) {
byte sniff_opcode = encoded.readByte();
if (sniff_opcode == OPCODE_MAP2_VALUE_END) {
return ret;
}
Object mapkey = readEncodedSimpleValue(sniff_opcode, encoded);
Object value = readEncodedSimpleValue(encoded);
ret.put(mapkey, value);
}
}
case OPCODE_SET_VALUE:
{
int len = ByteBufUtils.readVInt(encoded);
Set<Object> ret = new HashSet<>();
for (int i = 0; i < len; i++) {
Object o = readEncodedSimpleValue(encoded);
ret.add(o);
}
return ret;
}
case OPCODE_LIST_VALUE:
{
int len = ByteBufUtils.readVInt(encoded);
List<Object> ret = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
Object o = readEncodedSimpleValue(encoded);
ret.add(o);
}
return ret;
}
case OPCODE_BYTEARRAY_VALUE:
{
return ByteBufUtils.readArray(encoded);
}
case OPCODE_TIMESTAMP_VALUE:
return new java.sql.Timestamp(ByteBufUtils.readVLong(encoded));
case OPCODE_BYTE_VALUE:
return encoded.readByte();
case OPCODE_KEYVALUE_VALUE:
byte[] key = ByteBufUtils.readArray(encoded);
byte[] value = ByteBufUtils.readArray(encoded);
return new KeyValue(key, value);
default:
throw new RuntimeException("invalid opcode: " + _opcode);
}
}
use of herddb.utils.MapDataAccessor 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