Search in sources :

Example 1 with HazelcastJsonValueCodec

use of com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec in project hazelcast by hazelcast.

the class SqlPageCodec method encode.

@SuppressWarnings({ "unchecked", "checkstyle:CyclomaticComplexity", "checkstyle:MethodLength" })
public static void encode(ClientMessage clientMessage, SqlPage sqlPage) {
    clientMessage.add(BEGIN_FRAME.copy());
    // Write the "last" flag.
    byte[] content = new byte[] { (byte) (sqlPage.isLast() ? 1 : 0) };
    clientMessage.add(new ClientMessage.Frame(content));
    // Write column types.
    List<SqlColumnType> columnTypes = sqlPage.getColumnTypes();
    List<Integer> columnTypeIds = new ArrayList<>(columnTypes.size());
    for (SqlColumnType columnType : columnTypes) {
        columnTypeIds.add(columnType.getId());
    }
    ListIntegerCodec.encode(clientMessage, columnTypeIds);
    // Write columns.
    for (int i = 0; i < sqlPage.getColumnCount(); i++) {
        SqlColumnType columnType = columnTypes.get(i);
        Iterable<?> column = sqlPage.getColumnValuesForServer(i);
        switch(columnType) {
            case VARCHAR:
                ListMultiFrameCodec.encodeContainsNullable(clientMessage, (Iterable<String>) column, StringCodec::encode);
                break;
            case BOOLEAN:
                ListCNBooleanCodec.encode(clientMessage, (Iterable<Boolean>) column);
                break;
            case TINYINT:
                ListCNByteCodec.encode(clientMessage, (Iterable<Byte>) column);
                break;
            case SMALLINT:
                ListCNShortCodec.encode(clientMessage, (Iterable<Short>) column);
                break;
            case INTEGER:
                ListCNIntegerCodec.encode(clientMessage, (Iterable<Integer>) column);
                break;
            case BIGINT:
                ListCNLongCodec.encode(clientMessage, (Iterable<Long>) column);
                break;
            case REAL:
                ListCNFloatCodec.encode(clientMessage, (Iterable<Float>) column);
                break;
            case DOUBLE:
                ListCNDoubleCodec.encode(clientMessage, (Iterable<Double>) column);
                break;
            case DATE:
                ListCNLocalDateCodec.encode(clientMessage, (Iterable<LocalDate>) column);
                break;
            case TIME:
                ListCNLocalTimeCodec.encode(clientMessage, (Iterable<LocalTime>) column);
                break;
            case TIMESTAMP:
                ListCNLocalDateTimeCodec.encode(clientMessage, (Iterable<LocalDateTime>) column);
                break;
            case TIMESTAMP_WITH_TIME_ZONE:
                ListCNOffsetDateTimeCodec.encode(clientMessage, (Iterable<OffsetDateTime>) column);
                break;
            case DECIMAL:
                ListMultiFrameCodec.encode(clientMessage, (Iterable<BigDecimal>) column, BigDecimalCodec::encodeNullable);
                break;
            case NULL:
                int size = 0;
                for (Object ignore : column) {
                    size++;
                }
                byte[] sizeBuffer = new byte[FixedSizeTypesCodec.INT_SIZE_IN_BYTES];
                FixedSizeTypesCodec.encodeInt(sizeBuffer, 0, size);
                clientMessage.add(new ClientMessage.Frame(sizeBuffer));
                break;
            case OBJECT:
                assert SqlPage.convertToData(columnType);
                ListMultiFrameCodec.encode(clientMessage, (Iterable<Data>) column, DataCodec::encodeNullable);
                break;
            case JSON:
                ListMultiFrameCodec.encodeContainsNullable(clientMessage, (Iterable<HazelcastJsonValue>) column, HazelcastJsonValueCodec::encode);
                break;
            default:
                throw new IllegalStateException("Unknown type " + columnType);
        }
    }
    clientMessage.add(END_FRAME.copy());
}
Also used : LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) SqlColumnType(com.hazelcast.sql.SqlColumnType) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) Data(com.hazelcast.internal.serialization.Data) BigDecimal(java.math.BigDecimal) OffsetDateTime(java.time.OffsetDateTime) HazelcastJsonValueCodec(com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec)

Aggregations

ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)1 HazelcastJsonValueCodec (com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec)1 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)1 Data (com.hazelcast.internal.serialization.Data)1 SqlColumnType (com.hazelcast.sql.SqlColumnType)1 BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ArrayList (java.util.ArrayList)1