use of herddb.index.SecondaryIndexSeek in project herddb by diennea.
the class BRINIndexManager method scanner.
@Override
protected Stream<Bytes> scanner(IndexOperation operation, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
if (operation instanceof SecondaryIndexSeek) {
SecondaryIndexSeek sis = (SecondaryIndexSeek) operation;
SQLRecordKeyFunction value = sis.value;
byte[] refvalue = value.computeNewValue(null, context, tableContext);
List<Bytes> result = data.search(Bytes.from_array(refvalue));
if (result != null) {
return result.stream();
} else {
return Stream.empty();
}
} else if (operation instanceof SecondaryIndexPrefixScan) {
SecondaryIndexPrefixScan sis = (SecondaryIndexPrefixScan) operation;
SQLRecordKeyFunction value = sis.value;
byte[] refvalue = value.computeNewValue(null, context, tableContext);
Bytes firstKey = Bytes.from_array(refvalue);
Bytes lastKey = firstKey.next();
return data.query(firstKey, lastKey);
} else if (operation instanceof SecondaryIndexRangeScan) {
Bytes firstKey = null;
Bytes lastKey = null;
SecondaryIndexRangeScan sis = (SecondaryIndexRangeScan) operation;
SQLRecordKeyFunction minKey = sis.minValue;
if (minKey != null) {
byte[] refminvalue = minKey.computeNewValue(null, context, tableContext);
firstKey = Bytes.from_array(refminvalue);
}
SQLRecordKeyFunction maxKey = sis.maxValue;
if (maxKey != null) {
byte[] refmaxvalue = maxKey.computeNewValue(null, context, tableContext);
lastKey = Bytes.from_array(refmaxvalue);
}
LOGGER.log(Level.FINE, "range scan on {0}.{1}, from {2} to {1}", new Object[] { index.table, index.name, firstKey, lastKey });
return data.query(firstKey, lastKey);
} else {
throw new UnsupportedOperationException("unsuppported index access type " + operation);
}
}
use of herddb.index.SecondaryIndexSeek in project herddb by diennea.
the class CalcitePlanner method findSecondaryIndexOperation.
private static IndexOperation findSecondaryIndexOperation(AbstractIndexManager index, CompiledSQLExpression where, Table table) throws StatementExecutionException {
IndexOperation secondaryIndexOperation = null;
String[] columnsToMatch = index.getColumnNames();
SQLRecordKeyFunction indexSeekFunction = findIndexAccess(where, columnsToMatch, index.getIndex(), "=", table);
if (indexSeekFunction != null) {
if (indexSeekFunction.isFullPrimaryKey()) {
secondaryIndexOperation = new SecondaryIndexSeek(index.getIndexName(), columnsToMatch, indexSeekFunction);
} else {
secondaryIndexOperation = new SecondaryIndexPrefixScan(index.getIndexName(), columnsToMatch, indexSeekFunction);
}
} else {
SQLRecordKeyFunction rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">=", table);
if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
rangeMin = null;
}
if (rangeMin == null) {
rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">", table);
if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
rangeMin = null;
}
}
SQLRecordKeyFunction rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<=", table);
if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
rangeMax = null;
}
if (rangeMax == null) {
rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<", table);
if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
rangeMax = null;
}
}
if (rangeMin != null || rangeMax != null) {
secondaryIndexOperation = new SecondaryIndexRangeScan(index.getIndexName(), columnsToMatch, rangeMin, rangeMax);
}
}
return secondaryIndexOperation;
}
use of herddb.index.SecondaryIndexSeek in project herddb by diennea.
the class SQLPlanner method findSecondaryIndexOperation.
private static IndexOperation findSecondaryIndexOperation(AbstractIndexManager index, Expression where, Table table) throws StatementExecutionException {
IndexOperation secondaryIndexOperation = null;
String[] columnsToMatch = index.getColumnNames();
SQLRecordKeyFunction indexSeekFunction = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, EqualsTo.class);
if (indexSeekFunction != null) {
if (indexSeekFunction.isFullPrimaryKey()) {
secondaryIndexOperation = new SecondaryIndexSeek(index.getIndexName(), columnsToMatch, indexSeekFunction);
} else {
secondaryIndexOperation = new SecondaryIndexPrefixScan(index.getIndexName(), columnsToMatch, indexSeekFunction);
}
} else {
SQLRecordKeyFunction rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, GreaterThanEquals.class);
if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
rangeMin = null;
}
if (rangeMin == null) {
rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, GreaterThan.class);
if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
rangeMin = null;
}
}
SQLRecordKeyFunction rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, MinorThanEquals.class);
if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
rangeMax = null;
}
if (rangeMax == null) {
rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, MinorThan.class);
if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
rangeMax = null;
}
}
if (rangeMin != null || rangeMax != null) {
secondaryIndexOperation = new SecondaryIndexRangeScan(index.getIndexName(), columnsToMatch, rangeMin, rangeMax);
}
}
return secondaryIndexOperation;
}
use of herddb.index.SecondaryIndexSeek in project herddb by diennea.
the class SimpleIHashIndexRecoveryTest method createRecoveryIndex_withduoblecheckpoint.
@Test
public void createRecoveryIndex_withduoblecheckpoint() 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();
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();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
Index index = Index.builder().onTable(table).type(Index.TYPE_HASH).column("name", ColumnTypes.STRING).build();
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('a','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('b','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('c','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('d','n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('e','n2')", Collections.emptyList());
CreateIndexStatement st3 = new CreateIndexStatement(index);
manager.executeStatement(st3, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(3, scan1.consume().size());
}
manager.checkpoint();
manager.checkpoint();
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
assertTrue(manager.waitForTablespace("tblspace1", 10000));
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(3, scan1.consume().size());
}
}
}
use of herddb.index.SecondaryIndexSeek in project herddb by diennea.
the class SecondaryIndexAccessSuite method dropTableWithIndexesInTransaction.
@Test
public void dropTableWithIndexesInTransaction() 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);
Bytes key = Bytes.from_int(1234);
Bytes value = Bytes.from_long(8888);
Table transacted_table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
CreateTableStatement st_create = new CreateTableStatement(transacted_table);
manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("a", "n1"), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("b", "n1"), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("c", "n2"), TransactionContext.NO_TRANSACTION);
Index index = Index.builder().onTable(transacted_table).type(indexType).column("name", ColumnTypes.STRING).build();
CreateIndexStatement createIndex = new CreateIndexStatement(index);
manager.executeStatement(createIndex, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(2, scan1.consume().size());
}
}
assertEquals(1, manager.getTableSpaceManager("tblspace1").getIndexesOnTable("t1").size());
DropTableStatement dropTable = new DropTableStatement(index.tablespace, index.table, false);
manager.executeStatement(dropTable, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertNull(manager.getTableSpaceManager("tblspace1").getIndexesOnTable("t1"));
}
}
Aggregations