use of herddb.model.TransactionContext in project herddb by diennea.
the class ThetaJoinOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
transactionContext = new TransactionContext(resLeft.transactionId);
ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
final long resTransactionId = resRight.transactionId;
final String[] fieldNamesFromLeft = resLeft.dataScanner.getFieldNames();
final String[] fieldNamesFromRight = resRight.dataScanner.getFieldNames();
final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
Enumerable<DataAccessor> result = EnumerableDefaults.thetaJoin(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), predicate(resultProjection, context), resultProjection, generateNullsOnLeft, generateNullsOnRight);
EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resTransactionId, fieldNames, columns, result);
return new ScanResult(resTransactionId, joinedScanner);
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class PrimaryIndexScanRangeTest method primaryIndexPrefixScanFileTest.
@Test
public void primaryIndexPrefixScanFileTest() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new FileDataStorageManager(dataPath), 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("n1", ColumnTypes.INTEGER).column("n2", ColumnTypes.INTEGER).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("n1").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('a',1,5,'n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('b',2,5,'n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('c',3,6,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('d',5,7,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('e',6,5,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('f',7,7,'n2')", Collections.emptyList());
performBasicPlannerTests(manager);
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("t1").getKeyToPageIndex().isSortedAscending());
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(3, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
assertEquals(6, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertNull(scan.getComparator());
assertNotNull(scan.getLimits());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(3, tuples.get(0).get("n1"));
assertEquals(6, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(3, tuples.get(0).get("n1"));
assertEquals(5, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(6, tuples.get(0).get("n1"));
assertEquals(7, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(1, tuples.size());
assertEquals(7, tuples.get(0).get("n1"));
}
}
// add a record in the middle of the sorted recordset
DMLStatementExecutionResult res = TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',4,6,'n2')", Collections.emptyList(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = res.transactionId;
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(4, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
assertEquals(4, tuples.get(2).get("n1"));
assertEquals(6, tuples.get(3).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(6, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
assertEquals(4, tuples.get(2).get("n1"));
assertEquals(5, tuples.get(3).get("n1"));
assertEquals(6, tuples.get(4).get("n1"));
assertEquals(7, tuples.get(5).get("n1"));
}
}
System.out.println("QUAAAAAAAA");
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
}
}
System.out.println("QUIIIIII");
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
for (DataAccessor tuple : tuples) {
System.out.println("tuple: " + tuple.toMap());
}
assertEquals(3, tuples.size());
assertEquals(3, tuples.get(0).get("n1"));
assertEquals(4, tuples.get(1).get("n1"));
assertEquals(5, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(3, tuples.size());
assertEquals(3, tuples.get(0).get("n1"));
assertEquals(4, tuples.get(1).get("n1"));
assertEquals(6, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(3, tuples.get(0).get("n1"));
assertEquals(4, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(5, tuples.get(0).get("n1"));
assertEquals(6, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(6, tuples.get(0).get("n1"));
assertEquals(7, tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 5,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(1, tuples.size());
assertEquals(7, tuples.get(0).get("n1"));
}
}
// add other records in the context of the transaction
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',8,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',9,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',10,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(9, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
assertEquals(4, tuples.get(2).get("n1"));
assertEquals(5, tuples.get(3).get("n1"));
assertEquals(6, tuples.get(4).get("n1"));
assertEquals(7, tuples.get(5).get("n1"));
assertEquals(8, tuples.get(6).get("n1"));
assertEquals(9, tuples.get(7).get("n1"));
assertEquals(10, tuples.get(8).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 7", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(7, tuples.size());
assertEquals(2, tuples.get(0).get("n1"));
assertEquals(3, tuples.get(1).get("n1"));
assertEquals(4, tuples.get(2).get("n1"));
assertEquals(5, tuples.get(3).get("n1"));
assertEquals(6, tuples.get(4).get("n1"));
assertEquals(7, tuples.get(5).get("n1"));
assertEquals(8, tuples.get(6).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 " + "limit 200", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(8, tuples.get(0).get("n1"));
assertEquals(9, tuples.get(1).get("n1"));
assertEquals(10, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(8, tuples.get(0).get("n1"));
assertEquals(9, tuples.get(1).get("n1"));
assertEquals(10, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 desc", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertFalse(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(10, tuples.get(0).get("n1"));
assertEquals(9, tuples.get(1).get("n1"));
assertEquals(8, tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(2, tuples.size());
assertEquals(8, tuples.get(0).get("n1"));
assertEquals(9, tuples.get(1).get("n1"));
}
}
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class RawSQLTest method multiInsert.
@Test
public void multiInsert() 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());
java.sql.Timestamp tt1 = new java.sql.Timestamp(System.currentTimeMillis());
java.sql.Timestamp tt2 = new java.sql.Timestamp(System.currentTimeMillis() + 60000);
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k1,n1,t1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
}
execute(manager, "CREATE TABLE tblspace1.tsql2 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql2 ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
// auto-transaction
execute(manager, "CREATE TABLE tblspace1.tsql3 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
DMLStatementExecutionResult resInsert = executeUpdate(manager, "INSERT INTO tblspace1.tsql3(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(2, resInsert.getUpdateCount());
assertTrue(resInsert.transactionId > 0);
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql3 ORDER BY n1 desc", Collections.emptyList(), 0, new TransactionContext(resInsert.transactionId))) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class RestartPendingTransactionTest method recoverUpdate.
@Test
public void recoverUpdate() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
Bytes key1 = Bytes.from_string("k1");
Bytes key2 = Bytes.from_string("k2");
Bytes key3 = Bytes.from_string("k3");
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key1, key1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key3, key3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
manager.executeStatement(new UpdateStatement("tblspace1", table.name, new ConstValueRecordFunction(key2.data), new ConstValueRecordFunction(key3.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
long tx2 = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
manager.executeStatement(new UpdateStatement("tblspace1", table.name, new ConstValueRecordFunction(key2.data), new ConstValueRecordFunction(key3.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx2));
manager.executeStatement(new CommitTransactionStatement("tblspace1", tx2), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// transactions which contains the update will be replayed at reboot
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
GetResult result = manager.get(new GetStatement("tblspace1", "t1", key1, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class RestartPendingTransactionTest method recoverDeleteInTransaction.
@Test
public void recoverDeleteInTransaction() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
Bytes key1 = Bytes.from_string("k1");
Bytes key2 = Bytes.from_string("k2");
Bytes key3 = Bytes.from_string("k3");
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key1, key1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key3, key3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
manager.executeStatement(new DeleteStatement("tblspace1", table.name, key2, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
manager.executeStatement(new CommitTransactionStatement("tblspace1", tx), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// transaction which contains the update will be replayed at reboot
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
GetResult result = manager.get(new GetStatement("tblspace1", "t1", key1, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
}
}
Aggregations