use of herddb.utils.DataAccessor 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));
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method updateSingleRowWithPredicate.
@Test
public void updateSingleRowWithPredicate() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.q1_MESSAGE (MSG_ID bigint primary key,status int, lastbouncecategory tinyint)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.q1_MESSAGE(msg_id,status, lastbouncecategory) values(?,?,?)", Arrays.asList(2, 1, null)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.q1_MESSAGE(msg_id,status, lastbouncecategory) values(?,?,?)", Arrays.asList(3, 1, null)).getUpdateCount());
try (DataScanner resultSet = scan(manager, "SELECT * FROM tblspace1.q1_MESSAGE", Collections.emptyList())) {
List<DataAccessor> consume = resultSet.consume();
for (DataAccessor tuple : consume) {
System.out.println("tuple1:" + tuple.toMap());
}
}
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.q1_MESSAGE set status=?, lastbouncecategory=null where MSG_ID=? and (status = 1 or status=5)", Arrays.asList(4, 2)).getUpdateCount());
try (DataScanner resultSet = scan(manager, "SELECT * FROM tblspace1.q1_MESSAGE", Collections.emptyList())) {
List<DataAccessor> consume = resultSet.consume();
for (DataAccessor tuple : consume) {
System.out.println("tuple2:" + tuple.toMap());
}
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method limitsTest.
@Test
public void limitsTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
// scan performed at "scan time"
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
}
if (manager.getPlanner() instanceof SQLPlanner) {
try (DataScanner scan1 = scan(manager, "SELECT TOP 1 * FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
}
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1,1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey2"), result.get(0).get("k1"));
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1,2", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
assertEquals(RawString.of("mykey2"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(1).get("k1"));
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(2).get("k1"));
assertEquals(RawString.of("mykey4"), result.get(3).get("k1"));
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 10,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(0, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 4,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(0, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 3,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey4"), result.get(0).get("k1"));
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 3", Collections.emptyList(), 2, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Arrays.asList(3), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 1,2", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
if (manager.getPlanner() instanceof CalcitePlanner) {
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?,?", Arrays.asList(1, 2), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? LIMIT 2,?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? ORDER BY k1 LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT k1, count(*) FROM tblspace1.tsql " + "WHERE k1 <> ? GROUP BY k1 ORDER BY k1 LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Arrays.asList(0), TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
fail();
} catch (MissingJDBCParameterException err) {
assertEquals(1, err.getIndex());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "ORDER BY k1 LIMIT 3", Collections.emptyList(), 5, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "ORDER BY k1 ", Collections.emptyList(), 2, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method limitsAggregatesTest.
@Test
public void limitsAggregatesTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
// scan performed after aggregation
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 " + "FROM tblspace1.tsql " + "GROUP BY k1 " + "ORDER BY k1 DESC", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
assertEquals(RawString.of("mykey4"), result.get(0).get("k1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
assertEquals(RawString.of("mykey3"), result.get(1).get("k1"));
assertEquals(Long.valueOf(1), result.get(1).get("cc"));
assertEquals(RawString.of("mykey2"), result.get(2).get("k1"));
assertEquals(Long.valueOf(1), result.get(2).get("cc"));
assertEquals(RawString.of("mykey"), result.get(3).get("k1"));
assertEquals(Long.valueOf(1), result.get(3).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 1,1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as alias,k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 1,2", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(Long.valueOf(1), result.get(0).get("alias"));
assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
assertEquals(Long.valueOf(1), result.get(1).get("alias"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
assertEquals(RawString.of("mykey4"), result.get(0).get("k1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
assertEquals(RawString.of("mykey3"), result.get(1).get("k1"));
assertEquals(Long.valueOf(1), result.get(1).get("cc"));
assertEquals(RawString.of("mykey2"), result.get(2).get("k1"));
assertEquals(Long.valueOf(1), result.get(2).get("cc"));
assertEquals(RawString.of("mykey"), result.get(3).get("k1"));
assertEquals(Long.valueOf(1), result.get(3).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*),k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 10,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(0, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*),k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 4,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(0, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1 ORDER BY k1 DESC LIMIT 3,10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,n1 FROM tblspace1.tsql GROUP BY n1 ORDER BY cc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertNull(result.get(0).get("n1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
assertEquals(Integer.valueOf(1), result.get(1).get("n1"));
assertEquals(Long.valueOf(1), result.get(1).get("cc"));
assertEquals(Integer.valueOf(2), result.get(2).get("n1"));
assertEquals(Long.valueOf(2), result.get(2).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,n1" + " FROM tblspace1.tsql" + " WHERE n1 is not null" + " GROUP BY n1" + " ORDER BY cc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
for (DataAccessor ac : result) {
System.out.println("ac:" + ac.toMap());
}
assertEquals(Integer.valueOf(1), result.get(0).get("n1"));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
assertEquals(Integer.valueOf(2), result.get(1).get("n1"));
assertEquals(Long.valueOf(2), result.get(1).get("cc"));
assertEquals(2, result.size());
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method insertFromSelect.
@Test
public void insertFromSelect() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.tsql2 (k2 string primary key,n2 int,s2 string,t2 timestamp)", Collections.emptyList());
java.sql.Timestamp tt1 = new java.sql.Timestamp(System.currentTimeMillis());
java.sql.Timestamp tt2 = new java.sql.Timestamp(System.currentTimeMillis() + 60000);
java.sql.Timestamp tt3 = new java.sql.Timestamp(System.currentTimeMillis() + 120000);
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?)", Arrays.asList("mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k2,t2,n2)" + "(select k1,t1,n1 from tblspace1.tsql)", Collections.emptyList()).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k2,n2,t2 FROM tblspace1.tsql2 ORDER BY n2 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k2"));
assertEquals(RawString.of("mykey"), res.get(1).get("k2"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n2"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n2"));
assertEquals(tt2, res.get(0).get("t2"));
assertEquals(tt1, res.get(1).get("t2"));
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?)", Arrays.asList("mykey3", Integer.valueOf(1236), tt1)).getUpdateCount());
DMLStatementExecutionResult executeUpdateInTransaction = executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k2,t2,n2)" + "(select k1,t1,n1 from tblspace1.tsql where n1=?)", Arrays.asList(1236), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(1, executeUpdateInTransaction.getUpdateCount());
assertTrue(executeUpdateInTransaction.transactionId > 0);
try (DataScanner scan = scan(manager, "SELECT k2,n2,t2 FROM tblspace1.tsql2 ORDER BY n2 desc", Collections.emptyList(), new TransactionContext(executeUpdateInTransaction.transactionId))) {
assertEquals(3, scan.consume().size());
}
manager.executeStatement(new RollbackTransactionStatement("tblspace1", executeUpdateInTransaction.transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT k2,n2,t2 FROM tblspace1.tsql2 ORDER BY n2 desc", Collections.emptyList())) {
assertEquals(2, scan.consume().size());
}
DMLStatementExecutionResult executeUpdateInTransaction2 = executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k2,t2,n2)" + "(select k1,t1,n1 from tblspace1.tsql where n1=?)", Arrays.asList(1236), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(1, executeUpdateInTransaction2.getUpdateCount());
assertTrue(executeUpdateInTransaction2.transactionId > 0);
manager.executeStatement(new CommitTransactionStatement("tblspace1", executeUpdateInTransaction2.transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT k2,n2,t2 FROM tblspace1.tsql2 ORDER BY n2 desc", Collections.emptyList())) {
assertEquals(3, scan.consume().size());
}
if (manager.getPlanner() instanceof SQLPlanner) {
DMLStatementExecutionResult executeUpdateWithParameters = executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k2,t2,n2)" + "(select ?,?,n1 from tblspace1.tsql where n1=?)", Arrays.asList("mykey5", tt3, 1236), TransactionContext.NO_TRANSACTION);
assertEquals(1, executeUpdateWithParameters.getUpdateCount());
assertTrue(executeUpdateWithParameters.transactionId == 0);
try (DataScanner scan = scan(manager, "SELECT k2,n2,t2 " + "FROM tblspace1.tsql2 " + "WHERE t2 = ?", Arrays.asList(tt3))) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
assertEquals(Integer.valueOf(1236), all.get(0).get("n2"));
assertEquals(tt3, all.get(0).get("t2"));
assertEquals(RawString.of("mykey5"), all.get(0).get("k2"));
}
}
}
}
Aggregations