Search in sources :

Example 6 with Cursor

use of org.h2.index.Cursor in project ignite by apache.

the class GridMergeIndex method getRowCount.

/**
 * {@inheritDoc}
 */
@Override
public long getRowCount(Session ses) {
    Cursor c = find(ses, null, null);
    long cnt = 0;
    while (c.next()) cnt++;
    return cnt;
}
Also used : Cursor(org.h2.index.Cursor)

Example 7 with Cursor

use of org.h2.index.Cursor 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 8 with Cursor

use of org.h2.index.Cursor in project ignite by apache.

the class GridH2TreeIndex method findFirstOrLast.

/** {@inheritDoc} */
@Override
public Cursor findFirstOrLast(Session ses, boolean first) {
    try {
        int seg = threadLocalSegment();
        IgniteTree t = treeForRead(seg);
        GridH2Row row = (GridH2Row) (first ? t.findFirst() : t.findLast());
        return new SingleRowCursor(row);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTree(org.apache.ignite.internal.util.IgniteTree) SingleRowCursor(org.h2.index.SingleRowCursor)

Example 9 with Cursor

use of org.h2.index.Cursor 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 10 with Cursor

use of org.h2.index.Cursor in project ignite by apache.

the class GridMergeIndexIterator method advance.

/**
 * Advance iterator.
 */
private void advance() {
    next = null;
    try {
        boolean hasNext = false;
        while (cursor == null || !(hasNext = cursor.next())) {
            if (idxIter.hasNext())
                cursor = idxIter.next().findInStream(null, null);
            else {
                releaseIfNeeded();
                break;
            }
        }
        if (hasNext) {
            Row row = cursor.get();
            int cols = row.getColumnCount();
            List<Object> res = new ArrayList<>(cols);
            for (int c = 0; c < cols; c++) res.add(row.getValue(c).getObject());
            next = res;
        }
    } catch (Exception e) {
        releaseIfNeeded();
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) Row(org.h2.result.Row) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Cursor (org.h2.index.Cursor)5 SingleRowCursor (org.h2.index.SingleRowCursor)5 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)3 GridH2Row (org.apache.ignite.internal.processors.query.h2.opt.GridH2Row)3 GridCursor (org.apache.ignite.internal.util.lang.GridCursor)3 Index (org.h2.index.Index)3 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 H2Cursor (org.apache.ignite.internal.processors.query.h2.H2Cursor)2 Row (org.h2.result.Row)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 Lock (java.util.concurrent.locks.Lock)1