use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method prepareQuery.
long prepareQuery(String tableSpace, String query) throws HDBException, ClientSideMetadataProviderException {
long existing = preparedStatements.getQueryId(tableSpace, query);
if (existing != 0) {
return existing;
}
Channel channel = ensureOpen();
try {
long requestId = channel.generateRequestId();
ByteBuf message = PduCodec.PrepareStatement.write(requestId, tableSpace, query);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, 0);
} else if (reply.type != Pdu.TYPE_PREPARE_STATEMENT_RESULT) {
throw new HDBException(reply);
}
long statementId = PduCodec.PrepareStatementResult.readStatementId(reply);
preparedStatements.registerQueryId(tableSpace, query, statementId);
return statementId;
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method rollbackTransaction.
@Override
public void rollbackTransaction(String tableSpace, long tx) throws HDBException, ClientSideMetadataProviderException {
Channel channel = ensureOpen();
try {
long requestId = channel.generateRequestId();
ByteBuf message = PduCodec.TxCommand.write(requestId, PduCodec.TxCommand.TX_COMMAND_ROLLBACK_TRANSACTION, tx, tableSpace);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, 0);
// not possible
return;
} else if (reply.type != Pdu.TYPE_TX_COMMAND_RESULT) {
throw new HDBException(reply);
}
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method performAuthentication.
private void performAuthentication(Channel channel, String serverHostname) throws Exception {
// no need to perform auth in "local" mode
String mode = connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_STANDALONE);
if (ClientConfiguration.PROPERTY_MODE_LOCAL.equals(mode) && channel.isLocalChannel()) {
return;
}
SaslNettyClient saslNettyClient = new SaslNettyClient(connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_USERNAME, ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_PASSWORD, ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT), serverHostname);
byte[] firstToken = new byte[0];
if (saslNettyClient.hasInitialResponse()) {
firstToken = saslNettyClient.evaluateChallenge(new byte[0]);
}
long requestId = channel.generateRequestId();
Pdu saslResponse = channel.sendMessageWithPduReply(requestId, PduCodec.SaslTokenMessageRequest.write(requestId, SaslUtils.AUTH_DIGEST_MD5, firstToken), timeout);
try {
for (int i = 0; i < 100; i++) {
byte[] responseToSendToServer;
switch(saslResponse.type) {
case Pdu.TYPE_SASL_TOKEN_SERVER_RESPONSE:
byte[] token = PduCodec.SaslTokenServerResponse.readToken(saslResponse);
responseToSendToServer = saslNettyClient.evaluateChallenge(token);
requestId = channel.generateRequestId();
saslResponse.close();
saslResponse = channel.sendMessageWithPduReply(requestId, PduCodec.SaslTokenMessageToken.write(requestId, responseToSendToServer), timeout);
if (saslNettyClient.isComplete()) {
LOGGER.finest("SASL auth completed with success");
return;
}
break;
case Pdu.TYPE_ERROR:
throw new Exception("Server returned ERROR during SASL negotiation, Maybe authentication failure (" + PduCodec.ErrorResponse.readError(saslResponse) + ")");
default:
throw new Exception("Unexpected server response during SASL negotiation (" + saslResponse + ")");
}
}
} finally {
saslResponse.close();
}
throw new Exception("SASL negotiation took too many steps");
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method executeUpdates.
public List<DMLResult> executeUpdates(String tableSpace, String query, long tx, boolean returnValues, boolean usePreparedStatement, List<List<Object>> batch) throws HDBException, ClientSideMetadataProviderException {
try {
Channel channel = ensureOpen();
long requestId = channel.generateRequestId();
long statementId = usePreparedStatement ? prepareQuery(tableSpace, query) : 0;
query = statementId > 0 ? "" : query;
ByteBuf message = PduCodec.ExecuteStatements.write(requestId, tableSpace, query, tx, returnValues, statementId, batch);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, statementId);
// not possible, handleGenericError always throws an error
return Collections.emptyList();
} else if (reply.type != Pdu.TYPE_EXECUTE_STATEMENTS_RESULT) {
throw new HDBException(reply);
}
long transactionId = PduCodec.ExecuteStatementsResult.readTx(reply);
List<Long> updateCounts = PduCodec.ExecuteStatementsResult.readUpdateCounts(reply);
int numResults = updateCounts.size();
List<DMLResult> results = new ArrayList<>(numResults);
PduCodec.ListOfListsReader resultRecords = PduCodec.ExecuteStatementsResult.startResultRecords(reply);
int numResultRecords = resultRecords.getNumLists();
for (int i = 0; i < numResults; i++) {
Map<RawString, Object> newvalue = null;
Object key = null;
if (numResultRecords > 0) {
PduCodec.ObjectListReader list = resultRecords.nextList();
newvalue = readParametersListAsMap(list);
if (newvalue != null) {
key = newvalue.get(RAWSTRING_KEY);
}
}
long updateCount = updateCounts.get(i);
DMLResult res = new DMLResult(updateCount, key, newvalue, transactionId);
results.add(res);
}
return results;
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method beginTransaction.
@Override
public long beginTransaction(String tableSpace) throws HDBException, ClientSideMetadataProviderException {
Channel channel = ensureOpen();
try {
long requestId = channel.generateRequestId();
ByteBuf message = PduCodec.TxCommand.write(requestId, PduCodec.TxCommand.TX_COMMAND_BEGIN_TRANSACTION, 0, tableSpace);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, 0);
// not possible
return -1;
} else if (reply.type == Pdu.TYPE_TX_COMMAND_RESULT) {
long tx = PduCodec.TxCommandResult.readTx(reply);
if (tx <= 0) {
throw new HDBException("Server did not create a new transaction");
}
return tx;
} else {
throw new HDBException(reply);
}
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
Aggregations