Search in sources :

Example 1 with TuplesList

use of herddb.utils.TuplesList in project herddb by diennea.

the class ServerSideConnectionPeer method handleOpenScanner.

private void handleOpenScanner(Message message, Channel _channel) {
    String tableSpace = (String) message.parameters.get("tableSpace");
    Long tx = (Long) message.parameters.get("tx");
    long txId = tx != null ? tx : 0;
    String query = (String) message.parameters.get("query");
    String scannerId = (String) message.parameters.get("scannerId");
    int fetchSize = 10;
    if (message.parameters.containsKey("fetchSize")) {
        fetchSize = (Integer) message.parameters.get("fetchSize");
    }
    int maxRows = 0;
    if (message.parameters.containsKey("maxRows")) {
        maxRows = (Integer) message.parameters.get("maxRows");
    }
    List<Object> parameters = (List<Object>) message.parameters.get("params");
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "openScanner txId+" + txId + ", fetchSize " + fetchSize + ", maxRows " + maxRows + "," + query + " with " + parameters);
    }
    try {
        TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, true, true, false, maxRows);
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.log(Level.FINEST, query + " -> " + translatedQuery.plan.mainStatement);
        }
        TransactionContext transactionContext = new TransactionContext(txId);
        if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement || translatedQuery.plan.joinStatements != null) {
            ScanResult scanResult = (ScanResult) server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
            DataScanner dataScanner = scanResult.dataScanner;
            ServerSideScannerPeer scanner = new ServerSideScannerPeer(dataScanner);
            String[] columns = dataScanner.getFieldNames();
            List<DataAccessor> records = dataScanner.consume(fetchSize);
            TuplesList tuplesList = new TuplesList(columns, records);
            boolean last = dataScanner.isFinished();
            LOGGER.log(Level.FINEST, "sending first {0} records to scanner {1} query {2}", new Object[] { records.size(), scannerId, query });
            if (!last) {
                scanners.put(scannerId, scanner);
            }
            _channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
        } else {
            _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unsupported query type for scan " + query + ": PLAN is " + translatedQuery.plan)));
        }
    } catch (DataScannerException | RuntimeException err) {
        LOGGER.log(Level.SEVERE, "error on scanner " + scannerId + ": " + err, err);
        scanners.remove(scannerId);
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Message(herddb.network.Message) DataAccessor(herddb.utils.DataAccessor) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DataScanner(herddb.model.DataScanner) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) ScanStatement(herddb.model.commands.ScanStatement) TuplesList(herddb.utils.TuplesList) DataScannerException(herddb.model.DataScannerException) ScanResult(herddb.model.ScanResult) TranslatedQuery(herddb.sql.TranslatedQuery) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 2 with TuplesList

use of herddb.utils.TuplesList 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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) KeyValue(herddb.network.KeyValue) ArrayList(java.util.ArrayList) List(java.util.List) TuplesList(herddb.utils.TuplesList) HashMap(java.util.HashMap) Map(java.util.Map) MapDataAccessor(herddb.utils.MapDataAccessor) TuplesList(herddb.utils.TuplesList)

Example 3 with TuplesList

use of herddb.utils.TuplesList in project herddb by diennea.

the class RoutedClientSideConnection method executeScan.

ScanResultSet executeScan(String tableSpace, String query, List<Object> params, long tx, int maxRows, int fetchSize) throws HDBException, ClientSideMetadataProviderException {
    Channel _channel = ensureOpen();
    try {
        String scannerId = this.clientId + ":" + SCANNERID_GENERATOR.incrementAndGet();
        Message message = Message.OPEN_SCANNER(clientId, tableSpace, query, scannerId, tx, params, fetchSize, maxRows);
        LOGGER.log(Level.FINEST, "open scanner {0} for query {1}, params {2}", new Object[] { scannerId, query, params });
        Message reply = _channel.sendMessageWithReply(message, timeout);
        if (reply.type == Message.TYPE_ERROR) {
            boolean notLeader = reply.parameters.get("notLeader") != null;
            if (notLeader) {
                this.connection.requestMetadataRefresh();
                throw new RetryRequestException(reply + "");
            }
            throw new HDBException(reply);
        }
        TuplesList data = (TuplesList) reply.parameters.get("data");
        List<DataAccessor> initialFetchBuffer = data.tuples;
        String[] columnNames = data.columnNames;
        boolean last = (Boolean) reply.parameters.get("last");
        long transactionId = (Long) reply.parameters.get("tx");
        // LOGGER.log(Level.SEVERE, "received first " + initialFetchBuffer.size() + " records for query " + query);
        ScanResultSetImpl impl = new ScanResultSetImpl(scannerId, columnNames, initialFetchBuffer, fetchSize, last, transactionId);
        return impl;
    } catch (InterruptedException | TimeoutException err) {
        throw new HDBException(err);
    }
}
Also used : Message(herddb.network.Message) DataAccessor(herddb.utils.DataAccessor) Channel(herddb.network.Channel) RetryRequestException(herddb.client.impl.RetryRequestException) AtomicLong(java.util.concurrent.atomic.AtomicLong) TuplesList(herddb.utils.TuplesList) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with TuplesList

use of herddb.utils.TuplesList in project herddb by diennea.

the class ServerSideConnectionPeer method handleFetchScannerData.

private void handleFetchScannerData(Message message, Channel _channel) {
    String scannerId = (String) message.parameters.get("scannerId");
    int fetchSize = (Integer) message.parameters.get("fetchSize");
    ServerSideScannerPeer scanner = scanners.get(scannerId);
    if (scanner != null) {
        try {
            DataScanner dataScanner = scanner.getScanner();
            List<DataAccessor> records = dataScanner.consume(fetchSize);
            String[] columns = dataScanner.getFieldNames();
            TuplesList tuplesList = new TuplesList(columns, records);
            boolean last = false;
            if (dataScanner.isFinished()) {
                LOGGER.log(Level.FINEST, "unregistering scanner {0}, resultset is finished", scannerId);
                scanners.remove(scannerId);
                last = true;
            }
            // LOGGER.log(Level.SEVERE, "sending " + converted.size() + " records to scanner " + scannerId);
            _channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
        } catch (DataScannerException error) {
            _channel.sendReplyMessage(message, Message.ERROR(null, error).setParameter("scannerId", scannerId));
        }
    } else {
        _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("no such scanner " + scannerId + ", only " + scanners.keySet())).setParameter("scannerId", scannerId));
    }
}
Also used : DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) TuplesList(herddb.utils.TuplesList) DataScannerException(herddb.model.DataScannerException)

Example 5 with TuplesList

use of herddb.utils.TuplesList 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

TuplesList (herddb.utils.TuplesList)6 DataAccessor (herddb.utils.DataAccessor)5 Message (herddb.network.Message)4 ArrayList (java.util.ArrayList)4 MapDataAccessor (herddb.utils.MapDataAccessor)3 HashMap (java.util.HashMap)3 List (java.util.List)3 HerdDBInternalException (herddb.core.HerdDBInternalException)2 DataScanner (herddb.model.DataScanner)2 DataScannerException (herddb.model.DataScannerException)2 DuplicatePrimaryKeyException (herddb.model.DuplicatePrimaryKeyException)2 NotLeaderException (herddb.model.NotLeaderException)2 StatementExecutionException (herddb.model.StatementExecutionException)2 KeyValue (herddb.network.KeyValue)2 RawString (herddb.utils.RawString)2 ByteBuf (io.netty.buffer.ByteBuf)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2