Search in sources :

Example 16 with SecondaryIndexSeek

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);
    }
}
Also used : Bytes(herddb.utils.Bytes) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan) SQLRecordKeyFunction(herddb.sql.SQLRecordKeyFunction)

Example 17 with SecondaryIndexSeek

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;
}
Also used : IndexOperation(herddb.index.IndexOperation) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan)

Example 18 with SecondaryIndexSeek

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;
}
Also used : MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) IndexOperation(herddb.index.IndexOperation) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan)

Example 19 with SecondaryIndexSeek

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());
        }
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) FileDataStorageManager(herddb.file.FileDataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 20 with SecondaryIndexSeek

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"));
    }
}
Also used : Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) Bytes(herddb.utils.Bytes) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) DropTableStatement(herddb.model.commands.DropTableStatement) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)26 DataScanner (herddb.model.DataScanner)23 Index (herddb.model.Index)23 Table (herddb.model.Table)23 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)23 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)23 CreateTableStatement (herddb.model.commands.CreateTableStatement)23 ScanStatement (herddb.model.commands.ScanStatement)23 TranslatedQuery (herddb.sql.TranslatedQuery)23 Test (org.junit.Test)21 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)14 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)14 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)14 FileCommitLogManager (herddb.file.FileCommitLogManager)9 FileDataStorageManager (herddb.file.FileDataStorageManager)9 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)9 Path (java.nio.file.Path)9 Bytes (herddb.utils.Bytes)7 SecondaryIndexPrefixScan (herddb.index.SecondaryIndexPrefixScan)5 TransactionContext (herddb.model.TransactionContext)5