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);
}
}
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);
}
}
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);
}
}
Aggregations