Search in sources :

Example 11 with GridH2Row

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.

the class GridH2TableSelfTest method testIndexFindFirstOrLast.

/**
     * @throws Exception If failed.
     */
public void testIndexFindFirstOrLast() throws Exception {
    Index index = tbl.getIndexes().get(2);
    assertTrue(index instanceof GridH2TreeIndex);
    assertTrue(index.canGetFirstOrLast());
    //find first on empty data
    Cursor cursor = index.findFirstOrLast(null, true);
    assertFalse(cursor.next());
    assertNull(cursor.get());
    //find last on empty data
    cursor = index.findFirstOrLast(null, false);
    assertFalse(cursor.next());
    assertNull(cursor.get());
    //fill with data
    int rows = 100;
    long t = System.currentTimeMillis();
    Random rnd = new Random();
    UUID min = null;
    UUID max = null;
    for (int i = 0; i < rows; i++) {
        UUID id = UUID.randomUUID();
        if (min == null || id.compareTo(min) < 0)
            min = id;
        if (max == null || id.compareTo(max) > 0)
            max = id;
        GridH2Row row = row(id, t++, id.toString(), rnd.nextInt(100));
        ((GridH2TreeIndex) index).put(row);
    }
    //find first
    cursor = index.findFirstOrLast(null, true);
    assertTrue(cursor.next());
    assertEquals(min, cursor.get().getValue(0).getObject());
    assertFalse(cursor.next());
    //find last
    cursor = index.findFirstOrLast(null, false);
    assertTrue(cursor.next());
    assertEquals(max, cursor.get().getValue(0).getObject());
    assertFalse(cursor.next());
}
Also used : Random(java.util.Random) Index(org.h2.index.Index) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) Cursor(org.h2.index.Cursor) UUID(java.util.UUID)

Example 12 with GridH2Row

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.

the class H2TreeIndex method find.

/**
 * {@inheritDoc}
 */
@Override
public Cursor find(Session ses, SearchRow lower, SearchRow upper) {
    try {
        IndexingQueryCacheFilter filter = partitionFilter(threadLocalFilter());
        int seg = threadLocalSegment();
        H2Tree tree = treeForRead(seg);
        if (indexType.isPrimaryKey() && lower != null && upper != null && tree.compareRows(lower, upper) == 0) {
            GridH2Row row = tree.findOne(lower, filter);
            return (row == null) ? EMPTY_CURSOR : new SingleRowCursor(row);
        } else {
            GridCursor<GridH2Row> cursor = tree.find(lower, upper, filter);
            return new H2Cursor(cursor);
        }
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) H2Cursor(org.apache.ignite.internal.processors.query.h2.H2Cursor) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) IndexingQueryCacheFilter(org.apache.ignite.spi.indexing.IndexingQueryCacheFilter) SingleRowCursor(org.h2.index.SingleRowCursor)

Example 13 with GridH2Row

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.

the class H2TreeIndex method doFind0.

/**
 * {@inheritDoc}
 */
@Override
protected H2Cursor doFind0(IgniteTree t, @Nullable SearchRow first, boolean includeFirst, @Nullable SearchRow last, IndexingQueryFilter filter) {
    try {
        IndexingQueryCacheFilter pf = partitionFilter(filter);
        GridCursor<GridH2Row> range = t.find(first, last, pf);
        if (range == null)
            range = GridH2IndexBase.EMPTY_CURSOR;
        return new H2Cursor(range);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) H2Cursor(org.apache.ignite.internal.processors.query.h2.H2Cursor) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) IndexingQueryCacheFilter(org.apache.ignite.spi.indexing.IndexingQueryCacheFilter)

Example 14 with GridH2Row

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.

the class GridH2Table method remove.

/**
 * Remove row.
 *
 * @param row Row.
 * @return {@code True} if was removed.
 * @throws IgniteCheckedException If failed.
 */
public boolean remove(CacheDataRow row) throws IgniteCheckedException {
    GridH2Row row0 = desc.createRow(row);
    lock(false);
    try {
        ensureNotDestroyed();
        boolean rmv = pk().removex(row0);
        if (rmv) {
            for (int i = pkIndexPos + 1, len = idxs.size(); i < len; i++) {
                Index idx = idxs.get(i);
                if (idx instanceof GridH2IndexBase)
                    ((GridH2IndexBase) idx).removex(row0);
            }
            if (!tmpIdxs.isEmpty()) {
                for (GridH2IndexBase idx : tmpIdxs.values()) idx.removex(row0);
            }
            size.decrement();
        }
        return rmv;
    } finally {
        unlock(false);
    }
}
Also used : Index(org.h2.index.Index) SpatialIndex(org.h2.index.SpatialIndex) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)

Example 15 with GridH2Row

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Row in project ignite by apache.

the class IgniteH2Indexing method dynamicIndexCreate.

/**
 * {@inheritDoc}
 */
@Override
public void dynamicIndexCreate(final String schemaName, final String tblName, final QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
    // Locate table.
    H2Schema schema = schemas.get(schemaName);
    H2TableDescriptor desc = (schema != null ? schema.tableByName(tblName) : null);
    if (desc == null)
        throw new IgniteCheckedException("Table not found in internal H2 database [schemaName=" + schemaName + ", tblName=" + tblName + ']');
    GridH2Table h2Tbl = desc.table();
    // Create index.
    final GridH2IndexBase h2Idx = desc.createUserIndex(idxDesc);
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        // Populate index with existing cache data.
        final GridH2RowDescriptor rowDesc = h2Tbl.rowDescriptor();
        SchemaIndexCacheVisitorClosure clo = new SchemaIndexCacheVisitorClosure() {

            @Override
            public void apply(CacheDataRow row) throws IgniteCheckedException {
                GridH2Row h2Row = rowDesc.createRow(row);
                h2Idx.putx(h2Row);
            }
        };
        cacheVisitor.visit(clo);
        // At this point index is in consistent state, promote it through H2 SQL statement, so that cached
        // prepared statements are re-built.
        String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, ifNotExists);
        executeSql(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) SchemaIndexCacheVisitorClosure(org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Aggregations

GridH2Row (org.apache.ignite.internal.processors.query.h2.opt.GridH2Row)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Index (org.h2.index.Index)5 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)4 IndexingQueryCacheFilter (org.apache.ignite.spi.indexing.IndexingQueryCacheFilter)3 Random (java.util.Random)2 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2 H2Cursor (org.apache.ignite.internal.processors.query.h2.H2Cursor)2 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)2 InlineIndexHelper (org.apache.ignite.internal.processors.query.h2.database.InlineIndexHelper)2 Cursor (org.h2.index.Cursor)2 SingleRowCursor (org.h2.index.SingleRowCursor)2 SearchRow (org.h2.result.SearchRow)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 HashSet (java.util.HashSet)1