Search in sources :

Example 1 with IndexRow

use of org.apache.ignite.internal.cache.query.index.sorted.IndexRow in project ignite by apache.

the class IndexQueryProcessor method queryLocal.

/**
 * Run query on local node.
 *
 * @return Query result that contains data iterator and related metadata.
 */
public <K, V> IndexQueryResult<K, V> queryLocal(GridCacheContext<K, V> cctx, IndexQueryDesc idxQryDesc, @Nullable IgniteBiPredicate<K, V> filter, IndexingQueryFilter cacheFilter, boolean keepBinary) throws IgniteCheckedException {
    SortedSegmentedIndex idx = findSortedIndex(cctx, idxQryDesc);
    IndexRangeQuery qry = prepareQuery(idx, idxQryDesc);
    GridCursor<IndexRow> cursor = querySortedIndex(cctx, idx, cacheFilter, qry);
    SortedIndexDefinition def = (SortedIndexDefinition) idxProc.indexDefinition(idx.id());
    IndexQueryResultMeta meta = new IndexQueryResultMeta(def, qry.criteria.length);
    // Map IndexRow to Cache Key-Value pair.
    return new IndexQueryResult<>(meta, new GridCloseableIteratorAdapter<IgniteBiTuple<K, V>>() {

        private IgniteBiTuple<K, V> currVal;

        private final CacheObjectContext coctx = cctx.cacheObjectContext();

        /**
         * {@inheritDoc}
         */
        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (currVal != null)
                return true;
            while (currVal == null && cursor.next()) {
                IndexRow r = cursor.get();
                K k = unwrap(r.cacheDataRow().key(), true);
                V v = unwrap(r.cacheDataRow().value(), true);
                if (filter != null) {
                    K k0 = keepBinary ? k : unwrap(r.cacheDataRow().key(), false);
                    V v0 = keepBinary ? v : unwrap(r.cacheDataRow().value(), false);
                    if (!filter.apply(k0, v0))
                        continue;
                }
                currVal = new IgniteBiTuple<>(k, v);
            }
            return currVal != null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected IgniteBiTuple<K, V> onNext() {
            if (currVal == null)
                if (!hasNext())
                    throw new NoSuchElementException();
            IgniteBiTuple<K, V> row = currVal;
            currVal = null;
            return row;
        }

        /**
         */
        private <T> T unwrap(CacheObject o, boolean keepBinary) {
            return (T) CacheObjectUtils.unwrapBinaryIfNeeded(coctx, o, keepBinary, false);
        }
    });
}
Also used : SortedSegmentedIndex(org.apache.ignite.internal.cache.query.index.sorted.SortedSegmentedIndex) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) SortedIndexDefinition(org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with IndexRow

use of org.apache.ignite.internal.cache.query.index.sorted.IndexRow in project ignite by apache.

the class DefragIndexFactory method createIndexSegment.

/**
 * {@inheritDoc}
 */
@Override
protected InlineIndexTree createIndexSegment(GridCacheContext<?, ?> cctx, SortedIndexDefinition def, RootPage rootPage, IoStatisticsHolder stats, InlineRecommender recommender, int segmentNum) throws Exception {
    InlineIndexTree tree = new InlineIndexTree(def, cctx.group(), def.treeName(), offheap, offheap.reuseListForIndex(def.treeName()), newCachePageMemory, // Use old row handler to have access to inline index key types.
    pageIoResolver(), rootPage.pageId().pageId(), rootPage.isAllocated(), oldIdx.inlineSize(), cctx.config().getSqlIndexMaxInlineSize(), def.keyTypeSettings(), null, stats, rowHndFactory, null);
    final MetaPageInfo oldInfo = oldIdx.segment(segmentNum).metaInfo();
    // Set IO wrappers for the new tree.
    BPlusInnerIO<IndexRow> innerIO = (BPlusInnerIO<IndexRow>) wrap(tree.latestInnerIO(), tree.rowHandler());
    BPlusLeafIO<IndexRow> leafIo = (BPlusLeafIO<IndexRow>) wrap(tree.latestLeafIO(), tree.rowHandler());
    tree.setIos(new IOVersions<>(innerIO), new IOVersions<>(leafIo));
    tree.copyMetaInfo(oldInfo);
    tree.enableSequentialWriteMode();
    return tree;
}
Also used : IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) InlineIndexTree(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexTree) BPlusInnerIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO) BPlusLeafIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO) MetaPageInfo(org.apache.ignite.internal.cache.query.index.sorted.MetaPageInfo)

Example 3 with IndexRow

use of org.apache.ignite.internal.cache.query.index.sorted.IndexRow in project ignite by apache.

the class InlineIndexTree method compare.

/**
 * {@inheritDoc}
 */
@Override
protected int compare(BPlusIO<IndexRow> io, long pageAddr, int idx, IndexRow row) throws IgniteCheckedException {
    if (inlineSize == 0) {
        IndexRow currRow = getRow(io, pageAddr, idx);
        int cmp = compareFullRows(currRow, row, 0);
        return cmp == 0 ? mvccCompare(currRow, row) : cmp;
    }
    int fieldOff = 0;
    // Use it when can't compare values (variable length, for example).
    int keyIdx;
    IndexRow currRow = null;
    int off = io.offset(idx);
    List<IndexKeyDefinition> keyDefs = rowHnd.indexKeyDefinitions();
    List<InlineIndexKeyType> keyTypes = rowHandler().inlineIndexKeyTypes();
    for (keyIdx = 0; keyIdx < keyTypes.size(); keyIdx++) {
        try {
            // possible keys for that comparison).
            if (row.key(keyIdx) == null)
                return 0;
            // Other keys are not inlined. Should compare as rows.
            if (keyIdx >= keyTypes.size())
                break;
            int maxSize = inlineSize - fieldOff;
            InlineIndexKeyType keyType = keyTypes.get(keyIdx);
            int cmp = def.rowComparator().compareKey(pageAddr, off + fieldOff, maxSize, row.key(keyIdx), keyType);
            if (cmp == CANT_BE_COMPARE || cmp == COMPARE_UNSUPPORTED)
                break;
            else
                fieldOff += keyType.inlineSize(pageAddr, off + fieldOff);
            if (cmp != 0) {
                IndexKeyDefinition keyDef = keyDefs.get(keyIdx);
                return applySortOrder(cmp, keyDef.order().sortOrder());
            }
        } catch (Exception e) {
            throw new IgniteException("Failed to store new index row.", e);
        }
    }
    if (keyIdx < keyDefs.size()) {
        recommender.recommend(row, inlineSize);
        if (currRow == null)
            currRow = getRow(io, pageAddr, idx);
        int ret = compareFullRows(currRow, row, keyIdx);
        if (ret != 0)
            return ret;
    }
    return mvccCompare((MvccIO) io, pageAddr, idx, row);
}
Also used : IndexKeyDefinition(org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) IgniteException(org.apache.ignite.IgniteException) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 4 with IndexRow

use of org.apache.ignite.internal.cache.query.index.sorted.IndexRow in project ignite by apache.

the class IndexingDefragmentation method defragmentTable.

/**
 * Defragment one given table.
 */
private boolean defragmentTable(CacheGroupContext newCtx, IntMap<LinkMap> mappingByPartition, CheckpointTimeoutLock cpLock, Runnable cancellationChecker, int pageSize, PageMemoryEx oldCachePageMem, PageMemory newCachePageMemory, long cpLockThreshold, AtomicLong lastCpLockTs, TableIndexes indexes) throws IgniteCheckedException {
    cpLock.checkpointReadLock();
    try {
        TreeIterator treeIterator = new TreeIterator(pageSize);
        GridCacheContext<?, ?> cctx = indexes.cctx;
        cancellationChecker.run();
        for (InlineIndex oldIdx : indexes.idxs) {
            InlineIndexRowHandler oldRowHnd = oldIdx.segment(0).rowHandler();
            SortedIndexDefinition idxDef = (SortedIndexDefinition) indexing.indexDefinition(oldIdx.id());
            InlineIndexImpl newIdx = new DefragIndexFactory(newCtx.offheap(), newCachePageMemory, oldIdx).createIndex(cctx, idxDef).unwrap(InlineIndexImpl.class);
            int segments = oldIdx.segmentsCount();
            for (int i = 0; i < segments; ++i) {
                treeIterator.iterate(oldIdx.segment(i), oldCachePageMem, (theTree, io, pageAddr, idx) -> {
                    cancellationChecker.run();
                    if (System.currentTimeMillis() - lastCpLockTs.get() >= cpLockThreshold) {
                        cpLock.checkpointReadUnlock();
                        cpLock.checkpointReadLock();
                        lastCpLockTs.set(System.currentTimeMillis());
                    }
                    assert 1 == io.getVersion() : "IO version " + io.getVersion() + " is not supported by current defragmentation algorithm." + " Please implement copying of tree in a new format.";
                    BPlusIO<IndexRow> h2IO = DefragIndexFactory.wrap(io, oldRowHnd);
                    IndexRow row = theTree.getRow(h2IO, pageAddr, idx);
                    if (row instanceof DefragIndexRowImpl) {
                        DefragIndexRowImpl r = (DefragIndexRowImpl) row;
                        CacheDataRow cacheDataRow = r.cacheDataRow();
                        int partition = cacheDataRow.partition();
                        long link = r.link();
                        LinkMap map = mappingByPartition.get(partition);
                        long newLink = map.get(link);
                        // Use old row handler, as MetaInfo is copied from old tree.
                        DefragIndexRowImpl newRow = DefragIndexRowImpl.create(oldRowHnd, newLink, r, ((MvccIO) io).storeMvccInfo());
                        newIdx.putIndexRow(newRow);
                    }
                    return true;
                });
            }
        }
        return true;
    } catch (Throwable t) {
        newCtx.cacheObjectContext().kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, t));
        throw t;
    } finally {
        cpLock.checkpointReadUnlock();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) LinkMap(org.apache.ignite.internal.processors.cache.persistence.defragmentation.LinkMap) SortedIndexDefinition(org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) DefragIndexRowImpl(org.apache.ignite.internal.cache.query.index.sorted.defragmentation.DefragIndexFactory.DefragIndexRowImpl) InlineIndexRowHandler(org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler) InlineIndexImpl(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl) FailureContext(org.apache.ignite.failure.FailureContext) TreeIterator(org.apache.ignite.internal.processors.cache.persistence.defragmentation.TreeIterator)

Example 5 with IndexRow

use of org.apache.ignite.internal.cache.query.index.sorted.IndexRow in project ignite by apache.

the class InlineIndexImpl method findLast.

/**
 * {@inheritDoc}
 */
@Override
public GridCursor<IndexRow> findLast(int segment, IndexQueryContext qryCtx) throws IgniteCheckedException {
    InlineTreeFilterClosure closure = filterClosure(qryCtx);
    IndexRow found = segments[segment].findLast(closure);
    if (found == null || isExpired(found))
        return IndexValueCursor.EMPTY;
    return new SingleCursor<>(found);
}
Also used : SingleCursor(org.apache.ignite.internal.cache.query.index.SingleCursor) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow)

Aggregations

IndexRow (org.apache.ignite.internal.cache.query.index.sorted.IndexRow)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 H2Cursor (org.apache.ignite.internal.processors.query.h2.H2Cursor)4 SingleCursor (org.apache.ignite.internal.cache.query.index.SingleCursor)3 IndexKeyDefinition (org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition)3 IndexValueCursor (org.apache.ignite.internal.cache.query.index.sorted.IndexValueCursor)3 SortedIndexDefinition (org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition)3 IndexQueryContext (org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext)3 FailureContext (org.apache.ignite.failure.FailureContext)2 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)2 H2Row (org.apache.ignite.internal.processors.query.h2.opt.H2Row)2 QueryContext (org.apache.ignite.internal.processors.query.h2.opt.QueryContext)2 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 IgniteException (org.apache.ignite.IgniteException)1 IndexKeyTypeSettings (org.apache.ignite.internal.cache.query.index.sorted.IndexKeyTypeSettings)1 IndexRowImpl (org.apache.ignite.internal.cache.query.index.sorted.IndexRowImpl)1