Search in sources :

Example 1 with TableManager

use of herddb.core.TableManager in project herddb by diennea.

the class ServerSideConnectionPeer method handlePushTableData.

private void handlePushTableData(Pdu message, Channel channel) {
    try {
        String tableSpace = PduCodec.PushTableData.readTablespace(message);
        String table = PduCodec.PushTableData.readTablename(message);
        long _start = System.currentTimeMillis();
        List<Record> records = new ArrayList<>();
        PduCodec.PushTableData.readRecords(message, (key, value) -> records.add(new Record(Bytes.from_array(key), Bytes.from_array(value))));
        LOGGER.log(Level.INFO, "Received {0} records for restore of table {1} in tableSpace {2}", new Object[] { records.size(), table, tableSpace });
        TableManager tableManager = (TableManager) server.getManager().getTableSpaceManager(tableSpace).getTableManager(table);
        tableManager.writeFromDump(records);
        long _stop = System.currentTimeMillis();
        LOGGER.log(Level.INFO, "Time restore {0} records: data {1} ms", new Object[] { records.size(), _stop - _start });
        ByteBuf res = PduCodec.AckResponse.write(message.messageId);
        channel.sendReplyMessage(message.messageId, res);
    } catch (StatementExecutionException err) {
        ByteBuf res = composeErrorResponse(message.messageId, err);
        channel.sendReplyMessage(message.messageId, res);
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TableManager(herddb.core.TableManager) Record(herddb.model.Record) RawString(herddb.utils.RawString) ByteBuf(io.netty.buffer.ByteBuf) StatementExecutionException(herddb.model.StatementExecutionException)

Example 2 with TableManager

use of herddb.core.TableManager in project herddb by diennea.

the class ServerSideConnectionPeer method handlePushTableData.

private void handlePushTableData(Message message, Channel _channel) {
    try {
        String tableSpace = (String) message.parameters.get("tableSpace");
        String table = (String) message.parameters.get("table");
        List<KeyValue> data = (List<KeyValue>) message.parameters.get("data");
        LOGGER.log(Level.INFO, "Received " + data.size() + " records for restore of table " + table + " in tableSpace " + tableSpace);
        long _start = System.currentTimeMillis();
        List<Record> records = new ArrayList<>(data.size());
        for (KeyValue kv : data) {
            records.add(new Record(Bytes.from_array(kv.key), Bytes.from_array(kv.value)));
        }
        TableManager tableManager = (TableManager) server.getManager().getTableSpaceManager(tableSpace).getTableManager(table);
        tableManager.writeFromDump(records);
        long _stop = System.currentTimeMillis();
        LOGGER.log(Level.INFO, "Time restore " + data.size() + " records: data " + (_stop - _start) + " ms");
        _channel.sendReplyMessage(message, Message.ACK(null));
    } catch (StatementExecutionException err) {
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) KeyValue(herddb.network.KeyValue) Message(herddb.network.Message) ArrayList(java.util.ArrayList) TableManager(herddb.core.TableManager) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) Record(herddb.model.Record) StatementExecutionException(herddb.model.StatementExecutionException)

Example 3 with TableManager

use of herddb.core.TableManager in project herddb by diennea.

the class ReplaceOp method executeAsync.

@Override
public CompletableFuture<StatementExecutionResult> executeAsync(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    TableManager tableManager = (TableManager) tableSpaceManager.getTableManager(tableName);
    final Table table = tableManager.getTable();
    TableContext tableContext = tableManager.buildTableContext();
    long transactionId = transactionContext.transactionId;
    List<DMLStatement> statements = new ArrayList<>();
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            Bytes oldKey = RecordSerializer.serializeIndexKey(row, table, table.getPrimaryKey());
            byte[] oldValue = RecordSerializer.serializeValueRaw(row.toMap(), table, -1);
            DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, oldKey, null);
            statements.add(deleteStatement);
            Record oldRecord = new Record(oldKey, Bytes.from_array(oldValue));
            byte[] newKey = this.keyFunction.computeNewValue(oldRecord, context, tableContext);
            byte[] newValue = this.recordFunction.computeNewValue(oldRecord, context, tableContext);
            DMLStatement insertStatement = new InsertStatement(tableSpace, tableName, new Record(Bytes.from_array(newKey), Bytes.from_array(newValue))).setReturnValues(returnValues);
            statements.add(insertStatement);
        }
        if (statements.isEmpty()) {
            return CompletableFuture.completedFuture(new DMLStatementExecutionResult(transactionId, 0, null, null));
        }
        if (statements.size() == 1) {
            return tableSpaceManager.executeStatementAsync(statements.get(0), context, transactionContext);
        }
        CompletableFuture<StatementExecutionResult> finalResult = new CompletableFuture<>();
        AtomicInteger updateCounts = new AtomicInteger();
        AtomicReference<Bytes> lastKey = new AtomicReference<>();
        AtomicReference<Bytes> lastNewValue = new AtomicReference<>();
        class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {

            int current;

            public ComputeNext(int current) {
                this.current = current;
            }

            @Override
            public void accept(StatementExecutionResult res, Throwable error) {
                if (error != null) {
                    finalResult.completeExceptionally(error);
                    return;
                }
                DMLStatementExecutionResult dml = (DMLStatementExecutionResult) res;
                updateCounts.addAndGet(dml.getUpdateCount());
                if (returnValues) {
                    lastKey.set(dml.getKey());
                    lastNewValue.set(dml.getNewvalue());
                }
                long newTransactionId = res.transactionId;
                if (current == statements.size()) {
                    DMLStatementExecutionResult finalDMLResult = new DMLStatementExecutionResult(newTransactionId, updateCounts.get(), lastKey.get(), lastNewValue.get());
                    finalResult.complete(finalDMLResult);
                    return;
                }
                DMLStatement nextStatement = statements.get(current);
                TransactionContext transactionContext = new TransactionContext(newTransactionId);
                CompletableFuture<StatementExecutionResult> nextPromise = tableSpaceManager.executeStatementAsync(nextStatement, context, transactionContext);
                nextPromise.whenComplete(new ComputeNext(current + 1));
            }
        }
        DMLStatement firstStatement = statements.get(0);
        tableSpaceManager.executeStatementAsync(firstStatement, context, transactionContext).whenComplete(new ComputeNext(1));
        return finalResult;
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) ArrayList(java.util.ArrayList) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) StatementExecutionException(herddb.model.StatementExecutionException) Bytes(herddb.utils.Bytes) CompletableFuture(java.util.concurrent.CompletableFuture) DataScanner(herddb.model.DataScanner) TableContext(herddb.model.TableContext) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) TableManager(herddb.core.TableManager) Record(herddb.model.Record) DataScannerException(herddb.model.DataScannerException) ScanResult(herddb.model.ScanResult) Table(herddb.model.Table) AtomicReference(java.util.concurrent.atomic.AtomicReference) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionContext(herddb.model.TransactionContext) DMLStatement(herddb.model.DMLStatement) BiConsumer(java.util.function.BiConsumer)

Aggregations

TableManager (herddb.core.TableManager)3 Record (herddb.model.Record)3 StatementExecutionException (herddb.model.StatementExecutionException)3 ArrayList (java.util.ArrayList)3 DMLStatement (herddb.model.DMLStatement)1 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)1 DataScanner (herddb.model.DataScanner)1 DataScannerException (herddb.model.DataScannerException)1 NotLeaderException (herddb.model.NotLeaderException)1 ScanResult (herddb.model.ScanResult)1 StatementExecutionResult (herddb.model.StatementExecutionResult)1 Table (herddb.model.Table)1 TableContext (herddb.model.TableContext)1 TransactionContext (herddb.model.TransactionContext)1 DeleteStatement (herddb.model.commands.DeleteStatement)1 InsertStatement (herddb.model.commands.InsertStatement)1 KeyValue (herddb.network.KeyValue)1 Message (herddb.network.Message)1 Bytes (herddb.utils.Bytes)1 DataAccessor (herddb.utils.DataAccessor)1