Search in sources :

Example 6 with KeyValue

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());
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) MapDataAccessor(herddb.utils.MapDataAccessor) Set(java.util.Set) HashMap(java.util.HashMap) KeyValue(herddb.network.KeyValue) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IntHolder(herddb.utils.IntHolder) List(java.util.List) Message(herddb.network.Message) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ByteBufUtils(herddb.utils.ByteBufUtils) RawString(herddb.utils.RawString) TuplesList(herddb.utils.TuplesList) KeyValue(herddb.network.KeyValue) Set(java.util.Set) HashSet(java.util.HashSet) RawString(herddb.utils.RawString) DataAccessor(herddb.utils.DataAccessor) MapDataAccessor(herddb.utils.MapDataAccessor) RawString(herddb.utils.RawString) IntHolder(herddb.utils.IntHolder) ArrayList(java.util.ArrayList) List(java.util.List) TuplesList(herddb.utils.TuplesList) TuplesList(herddb.utils.TuplesList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

KeyValue (herddb.network.KeyValue)6 TuplesList (herddb.utils.TuplesList)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Message (herddb.network.Message)4 HashMap (java.util.HashMap)4 Map (java.util.Map)3 DumpedLogEntry (herddb.backup.DumpedLogEntry)2 NotLeaderException (herddb.model.NotLeaderException)2 Record (herddb.model.Record)2 StatementExecutionException (herddb.model.StatementExecutionException)2 DataAccessor (herddb.utils.DataAccessor)2 MapDataAccessor (herddb.utils.MapDataAccessor)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TimeoutException (java.util.concurrent.TimeoutException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BackupFileConstants (herddb.backup.BackupFileConstants)1 DumpedTableMetadata (herddb.backup.DumpedTableMetadata)1 RetryRequestException (herddb.client.impl.RetryRequestException)1