Search in sources :

Example 6 with SearchRow

use of org.h2.result.SearchRow in project ignite by apache.

the class H2Tree method compare.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
@Override
protected int compare(BPlusIO<SearchRow> io, long pageAddr, int idx, SearchRow row) throws IgniteCheckedException {
    if (inlineSize() == 0)
        return compareRows(getRow(io, pageAddr, idx), row);
    else {
        int off = io.offset(idx);
        int fieldOff = 0;
        int lastIdxUsed = 0;
        for (int i = 0; i < inlineIdxs.size(); i++) {
            InlineIndexHelper inlineIdx = inlineIdxs.get(i);
            Value v2 = row.getValue(inlineIdx.columnIndex());
            if (v2 == null)
                return 0;
            int c = inlineIdx.compare(pageAddr, off + fieldOff, inlineSize() - fieldOff, v2, comp);
            if (c == -2)
                break;
            lastIdxUsed++;
            if (c != 0)
                return c;
            fieldOff += inlineIdx.fullSize(pageAddr, off + fieldOff);
            if (fieldOff > inlineSize())
                break;
        }
        if (lastIdxUsed == cols.length)
            return 0;
        SearchRow rowData = getRow(io, pageAddr, idx);
        for (int i = lastIdxUsed, len = cols.length; i < len; i++) {
            IndexColumn col = cols[i];
            int idx0 = col.column.getColumnId();
            Value v2 = row.getValue(idx0);
            if (v2 == null) {
                // Can't compare further.
                return 0;
            }
            Value v1 = rowData.getValue(idx0);
            int c = compareValues(v1, v2);
            if (c != 0)
                return InlineIndexHelper.fixSort(c, col.sortType);
        }
        return 0;
    }
}
Also used : Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow) IndexColumn(org.h2.table.IndexColumn)

Example 7 with SearchRow

use of org.h2.result.SearchRow in project ignite by apache.

the class GridH2SpatialIndex method remove.

/**
 * {@inheritDoc}
 */
@Override
public GridH2Row remove(SearchRow row) {
    Lock l = lock.writeLock();
    l.lock();
    try {
        checkClosed();
        Value key = row.getValue(KEY_COL);
        assert key != null;
        Long rowId = keyToId.remove(key);
        assert rowId != null;
        GridH2Row oldRow = idToRow.remove(rowId);
        assert oldRow != null;
        final int seg = segmentForRow(row);
        if (!segments[seg].remove(getEnvelope(row, rowId), rowId))
            throw DbException.throwInternalError("row not found");
        rowCnt--;
        return oldRow;
    } finally {
        l.unlock();
    }
}
Also used : Value(org.h2.value.Value) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 8 with SearchRow

use of org.h2.result.SearchRow in project ignite by apache.

the class GridH2SpatialIndex method getEnvelope.

/**
 * @param row Row.
 * @param rowId Row id.
 * @return Envelope.
 */
private SpatialKey getEnvelope(SearchRow row, long rowId) {
    Value v = row.getValue(columnIds[0]);
    Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometry();
    Envelope env = g.getEnvelopeInternal();
    return new SpatialKey(rowId, (float) env.getMinX(), (float) env.getMaxX(), (float) env.getMinY(), (float) env.getMaxY());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ValueGeometry(org.h2.value.ValueGeometry) SpatialKey(org.h2.mvstore.rtree.SpatialKey) ValueGeometry(org.h2.value.ValueGeometry) Value(org.h2.value.Value) Envelope(com.vividsolutions.jts.geom.Envelope)

Example 9 with SearchRow

use of org.h2.result.SearchRow in project ignite by apache.

the class GridH2TableSelfTest method testTable.

/**
     * Simple table test.
     *
     * @throws Exception If failed.
     */
public void testTable() throws Exception {
    // Test insert.
    long x = MAX_X;
    Random rnd = new Random();
    while (x-- > 0) {
        UUID id = UUID.randomUUID();
        GridH2Row row = row(id, System.currentTimeMillis(), rnd.nextBoolean() ? id.toString() : UUID.randomUUID().toString(), rnd.nextInt(100));
        tbl.doUpdate(row, false);
    }
    assertEquals(MAX_X, tbl.getRowCountApproximation());
    assertEquals(MAX_X, tbl.getRowCount(null));
    for (GridH2IndexBase idx : tbl.indexes()) {
        assertEquals(MAX_X, idx.getRowCountApproximation());
        assertEquals(MAX_X, idx.getRowCount(null));
    }
    // Check correct rows order.
    checkOrdered((GridH2TreeIndex) tbl.indexes().get(0), new Comparator<SearchRow>() {

        @Override
        public int compare(SearchRow o1, SearchRow o2) {
            UUID id1 = (UUID) o1.getValue(0).getObject();
            UUID id2 = (UUID) o2.getValue(0).getObject();
            return id1.compareTo(id2);
        }
    });
    checkOrdered((GridH2TreeIndex) tbl.indexes().get(1), new Comparator<SearchRow>() {

        @Override
        public int compare(SearchRow o1, SearchRow o2) {
            Long x1 = (Long) o1.getValue(3).getObject();
            Long x2 = (Long) o2.getValue(3).getObject();
            int c = x2.compareTo(x1);
            if (c != 0)
                return c;
            Timestamp t1 = (Timestamp) o1.getValue(1).getObject();
            Timestamp t2 = (Timestamp) o2.getValue(1).getObject();
            return t1.compareTo(t2);
        }
    });
    checkOrdered((GridH2TreeIndex) tbl.indexes().get(2), new Comparator<SearchRow>() {

        @Override
        public int compare(SearchRow o1, SearchRow o2) {
            String s1 = (String) o1.getValue(2).getObject();
            String s2 = (String) o2.getValue(2).getObject();
            return s2.compareTo(s1);
        }
    });
    // Indexes data consistency.
    ArrayList<? extends Index> idxs = tbl.indexes();
    checkIndexesConsistent((ArrayList<Index>) idxs, null);
    // Check unique index.
    UUID id = UUID.randomUUID();
    UUID id2 = UUID.randomUUID();
    assertTrue(tbl.doUpdate(row(id, System.currentTimeMillis(), id.toString(), rnd.nextInt(100)), false));
    assertTrue(tbl.doUpdate(row(id2, System.currentTimeMillis(), id2.toString(), rnd.nextInt(100)), false));
    // Check index selection.
    checkQueryPlan(conn, "SELECT * FROM T", SCAN_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE ID IS NULL", PK_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE ID = RANDOM_UUID()", PK_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE ID > RANDOM_UUID()", PK_NAME);
    checkQueryPlan(conn, "SELECT * FROM T ORDER BY ID", PK_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE STR IS NULL", STR_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE STR = 'aaaa'", STR_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE STR > 'aaaa'", STR_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T ORDER BY STR DESC", STR_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE X IS NULL", NON_UNIQUE_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE X = 10000", NON_UNIQUE_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T WHERE X > 10000", NON_UNIQUE_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T ORDER BY X DESC", NON_UNIQUE_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T ORDER BY X DESC, T", NON_UNIQUE_IDX_NAME);
    checkQueryPlan(conn, "SELECT * FROM T ORDER BY T, X DESC", SCAN_IDX_NAME);
    // Simple queries.
    Statement s = conn.createStatement();
    ResultSet rs = s.executeQuery("select id from t where x between 0 and 100");
    int i = 0;
    while (rs.next()) i++;
    assertEquals(MAX_X + 2, i);
    // -----
    rs = s.executeQuery("select id from t where t is not null");
    i = 0;
    while (rs.next()) i++;
    assertEquals(MAX_X + 2, i);
    // ----
    int cnt = 10 + rnd.nextInt(25);
    long t = System.currentTimeMillis();
    for (i = 0; i < cnt; i++) {
        id = UUID.randomUUID();
        assertTrue(tbl.doUpdate(row(id, t, id.toString(), 51), false));
    }
    rs = s.executeQuery("select x, id from t where x = 51 limit " + cnt);
    i = 0;
    while (rs.next()) {
        assertEquals(51, rs.getInt(1));
        i++;
    }
    assertEquals(cnt, i);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Index(org.h2.index.Index) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) ValueString(org.h2.value.ValueString) Timestamp(java.sql.Timestamp) ValueTimestamp(org.h2.value.ValueTimestamp) Random(java.util.Random) ValueLong(org.h2.value.ValueLong) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) SearchRow(org.h2.result.SearchRow)

Example 10 with SearchRow

use of org.h2.result.SearchRow 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)

Aggregations

Value (org.h2.value.Value)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheException (javax.cache.CacheException)2 GridH2ValueMessage (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage)2 SearchRow (org.h2.result.SearchRow)2 IndexColumn (org.h2.table.IndexColumn)2 Envelope (com.vividsolutions.jts.geom.Envelope)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 UUID (java.util.UUID)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1