Search in sources :

Example 1 with SortedSegmentedIndex

use of org.apache.ignite.internal.cache.query.index.sorted.SortedSegmentedIndex 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 SortedSegmentedIndex

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

the class IndexQueryProcessor method indexByName.

/**
 * @return Sorted index found by name.
 * @throws IgniteCheckedException If index not found or specified index doesn't match query criteria.
 */
private SortedSegmentedIndex indexByName(IndexName idxName, IndexQueryDesc idxQryDesc, final Map<String, String> criteriaFlds) throws IgniteCheckedException {
    SortedSegmentedIndex idx = assertSortedIndex(idxProc.index(idxName), idxQryDesc);
    if (idx == null && !QueryUtils.PRIMARY_KEY_INDEX.equals(idxName.idxName())) {
        String normIdxName = QueryUtils.normalizeObjectName(idxName.idxName(), false);
        idxName = new IndexName(idxName.cacheName(), idxName.schemaName(), idxName.tableName(), normIdxName);
        idx = assertSortedIndex(idxProc.index(idxName), idxQryDesc);
    }
    if (idx == null)
        throw failIndexQuery("No index found for name: " + idxName.idxName(), null, idxQryDesc);
    if (!checkIndex(idx, idxName.tableName(), criteriaFlds))
        throw failIndexQuery("Index doesn't match criteria", null, idxQryDesc);
    return idx;
}
Also used : SortedSegmentedIndex(org.apache.ignite.internal.cache.query.index.sorted.SortedSegmentedIndex)

Aggregations

SortedSegmentedIndex (org.apache.ignite.internal.cache.query.index.sorted.SortedSegmentedIndex)2 NoSuchElementException (java.util.NoSuchElementException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IndexRow (org.apache.ignite.internal.cache.query.index.sorted.IndexRow)1 SortedIndexDefinition (org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1