use of herddb.mem.MemoryMetadataStorageManager 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);
assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("t1").isKeyToPageSortedAscending());
{
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"));
}
}
{
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"));
}
}
{
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.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class PrimaryIndexScanRangeTest method scanByPKOfIntsWithNegativeValues.
@Test
public void scanByPKOfIntsWithNegativeValues() 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());
assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("t1").isKeyToPageSortedAscending());
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
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(-3, tuples.get(0).get("n1"));
assertEquals(1, tuples.get(1).get("n1"));
assertEquals(2, tuples.get(2).get("n1"));
}
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class PrimaryIndexScanRangeTest method primaryIndexPrefixScanWithStringsFileTest.
@Test
public void primaryIndexPrefixScanWithStringsFileTest() 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.STRING).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());
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("t1").isKeyToPageSortedAscending());
{
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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("6"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).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, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), 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,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(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("4"), tuples.get(1).get("n1"));
assertEquals(RawString.of("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(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("4"), tuples.get(1).get("n1"));
assertEquals(RawString.of("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(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("5"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("6"), tuples.get(0).get("n1"));
assertEquals(RawString.of("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(RawString.of("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','A',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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).get("n1"));
assertEquals(RawString.of("8"), tuples.get(6).get("n1"));
assertEquals(RawString.of("9"), tuples.get(7).get("n1"));
assertEquals(RawString.of("A"), 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(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).get("n1"));
assertEquals(RawString.of("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(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("A"), 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(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("A"), 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(RawString.of("A"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("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(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
}
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class AutoIncrementTest method testAutoIncrement_other_sintax.
@Test
public void testAutoIncrement_other_sintax() 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 (n1 int auto_increment, s1 string, primary key (n1))", Collections.emptyList());
{
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa")).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consumeAndClose();
assertEquals(6, rows.size());
for (int i = 1; i <= 6; i++) {
int _i = i;
assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(7), result.getKey());
assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList()).getUpdateCount());
}
// the same but in transaction
{
DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(1, res.getUpdateCount());
long tx = res.transactionId;
assertTrue(tx > 0);
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consumeAndClose();
assertEquals(6, rows.size());
for (int i = 8; i <= 13; i++) {
int _i = i;
assertTrue("can't find " + i, rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(14), result.getKey());
assertEquals(tx, res.transactionId);
// transaction not committed
}
// the same but in transaction, multi values as first
{
DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(2, res.getUpdateCount());
long tx = res.transactionId;
assertTrue(tx > 0);
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consumeAndClose();
assertEquals(6, rows.size());
for (int i = 15; i <= 20; i++) {
int _i = i;
assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(21), result.getKey());
assertEquals(tx, res.transactionId);
assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).getUpdateCount());
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class CreateTableNoDefinitionTest method createTable1.
@Test
public void createTable1() throws Exception {
exceptionRule.expect(StatementExecutionException.class);
exceptionRule.expectMessage("A table must have at least 1 column");
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 test", Collections.emptyList());
}
}
Aggregations