Search in sources :

Example 1 with MetaServiceCode

use of io.dingodb.server.protocol.code.MetaServiceCode in project dingo by dingodb.

the class MetaServiceHandler method onMessage.

private void onMessage(Message message, Channel channel) {
    ByteBuffer buffer = ByteBuffer.wrap(message.toBytes());
    MetaServiceCode code = MetaServiceCode.valueOf(PrimitiveCodec.readZigZagInt(buffer));
    switch(code) {
        case LISTENER_TABLE:
            // todo
            break;
        case REFRESH_TABLES:
            // todo
            break;
        case GET_TABLE:
            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                outputStream.write(encodeZigZagInt(ServerError.OK.getCode()));
                getAndEncodeTableEntry(readString(buffer), outputStream);
                channel.send(SimpleMessage.builder().content(outputStream.toByteArray()).build());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            } catch (NullPointerException e) {
                channel.send(ServerError.TABLE_NOT_FOUND.message());
            }
            break;
        case CREATE_TABLE:
            try {
                String name = readString(buffer);
                TableDefinition definition = TableDefinition.fromJson(readString(buffer));
                metaService.createTable(name, definition);
                channel.send(ServerError.OK.message());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            } catch (DingoException error) {
                channel.send(ServerError.message(error));
            }
            break;
        case DELETE_TABLE:
            try {
                // todo delete table data
                String name = readString(buffer);
                if (metaService.dropTable(name)) {
                    channel.send(ServerError.OK.message());
                } else {
                    channel.send(ServerError.UNKNOWN.message());
                }
            } catch (DingoException error) {
                channel.send(ServerError.message(error));
            }
            break;
        case GET_ALL:
            try {
                Map<String, TableDefinition> tableDefinitions = metaService.getTableDefinitions();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] size = encodeZigZagInt(tableDefinitions.size());
                outputStream.write(encodeZigZagInt(ServerError.OK.getCode()));
                outputStream.write(size);
                outputStream.flush();
                for (String name : tableDefinitions.keySet()) {
                    getAndEncodeTableEntry(name, outputStream);
                }
                channel.send(SimpleMessage.builder().content(outputStream.toByteArray()).build());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            }
            break;
        default:
            channel.send(UNSUPPORTED_CODE.message());
    }
}
Also used : MetaServiceCode(io.dingodb.server.protocol.code.MetaServiceCode) DingoException(io.dingodb.common.error.DingoException) TableDefinition(io.dingodb.common.table.TableDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PrimitiveCodec.encodeString(io.dingodb.common.codec.PrimitiveCodec.encodeString) PrimitiveCodec.readString(io.dingodb.common.codec.PrimitiveCodec.readString) ByteBuffer(java.nio.ByteBuffer)

Aggregations

PrimitiveCodec.encodeString (io.dingodb.common.codec.PrimitiveCodec.encodeString)1 PrimitiveCodec.readString (io.dingodb.common.codec.PrimitiveCodec.readString)1 DingoException (io.dingodb.common.error.DingoException)1 TableDefinition (io.dingodb.common.table.TableDefinition)1 MetaServiceCode (io.dingodb.server.protocol.code.MetaServiceCode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1