Search in sources :

Example 21 with RangePredicate

use of org.apache.hyracks.storage.am.btree.impls.RangePredicate in project asterixdb by apache.

the class OrderedIndexTestUtils method checkRangeSearch.

@SuppressWarnings("unchecked")
public void checkRangeSearch(IIndexTestContext ctx, ITupleReference lowKey, ITupleReference highKey, boolean lowKeyInclusive, boolean highKeyInclusive) throws Exception {
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Testing Range Search.");
    }
    MultiComparator lowKeyCmp = BTreeUtils.getSearchMultiComparator(ctx.getComparatorFactories(), lowKey);
    MultiComparator highKeyCmp = BTreeUtils.getSearchMultiComparator(ctx.getComparatorFactories(), highKey);
    IIndexCursor searchCursor = ctx.getIndexAccessor().createSearchCursor(false);
    RangePredicate rangePred = new RangePredicate(lowKey, highKey, lowKeyInclusive, highKeyInclusive, lowKeyCmp, highKeyCmp);
    ctx.getIndexAccessor().search(searchCursor, rangePred);
    // Get the subset of elements from the expected set within given key
    // range.
    CheckTuple lowKeyCheck = createCheckTupleFromTuple(lowKey, ctx.getFieldSerdes(), lowKeyCmp.getKeyFieldCount());
    CheckTuple highKeyCheck = createCheckTupleFromTuple(highKey, ctx.getFieldSerdes(), highKeyCmp.getKeyFieldCount());
    SortedSet<CheckTuple> expectedSubset = null;
    if (lowKeyCmp.getKeyFieldCount() < ctx.getKeyFieldCount() || highKeyCmp.getKeyFieldCount() < ctx.getKeyFieldCount()) {
        // Searching on a key prefix (low key or high key or both).
        expectedSubset = getPrefixExpectedSubset((TreeSet<CheckTuple>) ctx.getCheckTuples(), lowKeyCheck, highKeyCheck);
    } else {
        // Searching on all key fields.
        expectedSubset = ((TreeSet<CheckTuple>) ctx.getCheckTuples()).subSet(lowKeyCheck, lowKeyInclusive, highKeyCheck, highKeyInclusive);
    }
    Iterator<CheckTuple> checkIter = expectedSubset.iterator();
    int actualCount = 0;
    try {
        while (searchCursor.hasNext()) {
            if (!checkIter.hasNext()) {
                fail("Range search returned more answers than expected.\nExpected: " + expectedSubset.size());
            }
            searchCursor.next();
            CheckTuple expectedTuple = checkIter.next();
            ITupleReference tuple = searchCursor.getTuple();
            compareActualAndExpected(tuple, expectedTuple, ctx.getFieldSerdes());
            actualCount++;
        }
        if (actualCount < expectedSubset.size()) {
            fail("Range search returned fewer answers than expected.\nExpected: " + expectedSubset.size() + "\nActual  : " + actualCount);
        }
    } finally {
        searchCursor.close();
    }
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) CheckTuple(org.apache.hyracks.storage.am.common.CheckTuple) TreeSet(java.util.TreeSet) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor)

Example 22 with RangePredicate

use of org.apache.hyracks.storage.am.btree.impls.RangePredicate in project asterixdb by apache.

the class MetadataNode method searchIndex.

private <ResultType> void searchIndex(JobId jobId, IMetadataIndex index, ITupleReference searchKey, IValueExtractor<ResultType> valueExtractor, List<ResultType> results) throws MetadataException, HyracksDataException, RemoteException {
    IBinaryComparatorFactory[] comparatorFactories = index.getKeyBinaryComparatorFactory();
    if (index.getFile() == null) {
        throw new MetadataException("No file for Index " + index.getDataverseName() + "." + index.getIndexName());
    }
    String resourceName = index.getFile().getRelativePath();
    IIndex indexInstance = datasetLifecycleManager.get(resourceName);
    datasetLifecycleManager.open(resourceName);
    IIndexAccessor indexAccessor = indexInstance.createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    ITreeIndexCursor rangeCursor = (ITreeIndexCursor) indexAccessor.createSearchCursor(false);
    IBinaryComparator[] searchCmps = null;
    MultiComparator searchCmp = null;
    RangePredicate rangePred = null;
    if (searchKey != null) {
        searchCmps = new IBinaryComparator[searchKey.getFieldCount()];
        for (int i = 0; i < searchKey.getFieldCount(); i++) {
            searchCmps[i] = comparatorFactories[i].createBinaryComparator();
        }
        searchCmp = new MultiComparator(searchCmps);
    }
    rangePred = new RangePredicate(searchKey, searchKey, true, true, searchCmp, searchCmp);
    indexAccessor.search(rangeCursor, rangePred);
    try {
        while (rangeCursor.hasNext()) {
            rangeCursor.next();
            ResultType result = valueExtractor.getValue(jobId, rangeCursor.getTuple());
            if (result != null) {
                results.add(result);
            }
        }
    } finally {
        rangeCursor.close();
    }
    datasetLifecycleManager.close(resourceName);
}
Also used : IIndex(org.apache.hyracks.storage.common.IIndex) ITreeIndexCursor(org.apache.hyracks.storage.am.common.api.ITreeIndexCursor) RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) IBinaryComparatorFactory(org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory) IBinaryComparator(org.apache.hyracks.api.dataflow.value.IBinaryComparator) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) IIndexAccessor(org.apache.hyracks.storage.common.IIndexAccessor)

Example 23 with RangePredicate

use of org.apache.hyracks.storage.am.btree.impls.RangePredicate in project asterixdb by apache.

the class MetadataNode method printMetadata.

// Debugging Method
public String printMetadata() {
    StringBuilder sb = new StringBuilder();
    try {
        IMetadataIndex index = MetadataPrimaryIndexes.DATAVERSE_DATASET;
        String resourceName = index.getFile().toString();
        IIndex indexInstance = datasetLifecycleManager.get(resourceName);
        datasetLifecycleManager.open(resourceName);
        IIndexAccessor indexAccessor = indexInstance.createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
        ITreeIndexCursor rangeCursor = (ITreeIndexCursor) indexAccessor.createSearchCursor(false);
        RangePredicate rangePred = null;
        rangePred = new RangePredicate(null, null, true, true, null, null);
        indexAccessor.search(rangeCursor, rangePred);
        try {
            while (rangeCursor.hasNext()) {
                rangeCursor.next();
                sb.append(TupleUtils.printTuple(rangeCursor.getTuple(), new ISerializerDeserializer[] { SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING) }));
            }
        } finally {
            rangeCursor.close();
        }
        datasetLifecycleManager.close(resourceName);
        index = MetadataPrimaryIndexes.DATASET_DATASET;
        indexInstance = datasetLifecycleManager.get(resourceName);
        datasetLifecycleManager.open(resourceName);
        indexAccessor = indexInstance.createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
        rangeCursor = (ITreeIndexCursor) indexAccessor.createSearchCursor(false);
        rangePred = null;
        rangePred = new RangePredicate(null, null, true, true, null, null);
        indexAccessor.search(rangeCursor, rangePred);
        try {
            while (rangeCursor.hasNext()) {
                rangeCursor.next();
                sb.append(TupleUtils.printTuple(rangeCursor.getTuple(), new ISerializerDeserializer[] { SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING), SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING) }));
            }
        } finally {
            rangeCursor.close();
        }
        datasetLifecycleManager.close(resourceName);
        index = MetadataPrimaryIndexes.INDEX_DATASET;
        indexInstance = datasetLifecycleManager.get(resourceName);
        datasetLifecycleManager.open(resourceName);
        indexAccessor = indexInstance.createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
        rangeCursor = (ITreeIndexCursor) indexAccessor.createSearchCursor(false);
        rangePred = null;
        rangePred = new RangePredicate(null, null, true, true, null, null);
        indexAccessor.search(rangeCursor, rangePred);
        try {
            while (rangeCursor.hasNext()) {
                rangeCursor.next();
                sb.append(TupleUtils.printTuple(rangeCursor.getTuple(), new ISerializerDeserializer[] { SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING), SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING), SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING) }));
            }
        } finally {
            rangeCursor.close();
        }
        datasetLifecycleManager.close(resourceName);
    } catch (Exception e) {
        // Debugging method
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : IIndex(org.apache.hyracks.storage.common.IIndex) ITreeIndexCursor(org.apache.hyracks.storage.am.common.api.ITreeIndexCursor) RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) IMetadataIndex(org.apache.asterix.metadata.api.IMetadataIndex) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) IIndexAccessor(org.apache.hyracks.storage.common.IIndexAccessor) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) ACIDException(org.apache.asterix.common.exceptions.ACIDException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) RemoteException(java.rmi.RemoteException)

Example 24 with RangePredicate

use of org.apache.hyracks.storage.am.btree.impls.RangePredicate in project asterixdb by apache.

the class LSMInvertedIndexTestWorker method performOp.

@Override
public void performOp(ITupleReference tuple, TestOperation op) throws HyracksDataException {
    LSMInvertedIndexAccessor accessor = (LSMInvertedIndexAccessor) indexAccessor;
    IIndexCursor searchCursor = accessor.createSearchCursor(false);
    IIndexCursor rangeSearchCursor = accessor.createRangeSearchCursor();
    RangePredicate rangePred = new RangePredicate(null, null, true, true, null, null);
    IBinaryTokenizerFactory tokenizerFactory = invIndex.getTokenizerFactory();
    int searchModifierIndex = Math.abs(rnd.nextInt()) % TEST_SEARCH_MODIFIERS.length;
    InvertedIndexSearchPredicate searchPred = new InvertedIndexSearchPredicate(tokenizerFactory.createTokenizer(), TEST_SEARCH_MODIFIERS[searchModifierIndex]);
    switch(op) {
        case INSERT:
            {
                insert(accessor, tuple);
                break;
            }
        case DELETE:
            {
                // Randomly pick a document from the corpus to delete.
                if (!documentCorpus.isEmpty()) {
                    int docIndex = Math.abs(rnd.nextInt()) % documentCorpus.size();
                    ITupleReference deleteTuple = documentCorpus.get(docIndex);
                    accessor.delete(deleteTuple);
                    // Swap tupleIndex with last element.
                    documentCorpus.set(docIndex, documentCorpus.get(documentCorpus.size() - 1));
                    documentCorpus.remove(documentCorpus.size() - 1);
                } else {
                    // No existing documents to delete, treat this case as an insert.
                    insert(accessor, tuple);
                }
                break;
            }
        case POINT_SEARCH:
            {
                searchCursor.reset();
                searchPred.setQueryTuple(tuple);
                searchPred.setQueryFieldIndex(0);
                try {
                    accessor.search(searchCursor, searchPred);
                    consumeCursorTuples(searchCursor);
                } catch (HyracksDataException e) {
                    // Ignore.
                    if (e.getErrorCode() != ErrorCode.OCCURRENCE_THRESHOLD_PANIC_EXCEPTION) {
                        throw e;
                    }
                }
                break;
            }
        case SCAN:
            {
                rangeSearchCursor.reset();
                accessor.rangeSearch(rangeSearchCursor, rangePred);
                consumeCursorTuples(rangeSearchCursor);
                break;
            }
        case MERGE:
            {
                accessor.scheduleMerge(NoOpIOOperationCallbackFactory.INSTANCE.createIoOpCallback(), invIndex.getImmutableComponents());
                break;
            }
        default:
            throw new HyracksDataException("Op " + op.toString() + " not supported.");
    }
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) IBinaryTokenizerFactory(org.apache.hyracks.storage.am.lsm.invertedindex.tokenizers.IBinaryTokenizerFactory) InvertedIndexSearchPredicate(org.apache.hyracks.storage.am.lsm.invertedindex.search.InvertedIndexSearchPredicate) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor) LSMInvertedIndexAccessor(org.apache.hyracks.storage.am.lsm.invertedindex.impls.LSMInvertedIndexAccessor) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 25 with RangePredicate

use of org.apache.hyracks.storage.am.btree.impls.RangePredicate in project asterixdb by apache.

the class LSMInvertedIndexSearchCursor method open.

@Override
public void open(ICursorInitialState initialState, ISearchPredicate searchPred) throws HyracksDataException {
    LSMInvertedIndexSearchCursorInitialState lsmInitState = (LSMInvertedIndexSearchCursorInitialState) initialState;
    harness = lsmInitState.getLSMHarness();
    operationalComponents = lsmInitState.getOperationalComponents();
    indexAccessors = lsmInitState.getIndexAccessors();
    opCtx = lsmInitState.getOpContext();
    accessorIndex = 0;
    this.searchPred = searchPred;
    this.searchCallback = lsmInitState.getSearchOperationCallback();
    // For searching the deleted-keys BTrees.
    deletedKeysBTreeAccessors = lsmInitState.getDeletedKeysBTreeAccessors();
    deletedKeysBTreeCursors = new IIndexCursor[deletedKeysBTreeAccessors.size()];
    for (int i = 0; i < operationalComponents.size(); i++) {
        ILSMComponent component = operationalComponents.get(i);
        if (component.getType() == LSMComponentType.MEMORY) {
            // No need for a bloom filter for the in-memory BTree.
            deletedKeysBTreeCursors[i] = deletedKeysBTreeAccessors.get(i).createSearchCursor(false);
        } else {
            deletedKeysBTreeCursors[i] = new BloomFilterAwareBTreePointSearchCursor((IBTreeLeafFrame) lsmInitState.getgetDeletedKeysBTreeLeafFrameFactory().createFrame(), false, ((LSMInvertedIndexDiskComponent) operationalComponents.get(i)).getBloomFilter());
        }
    }
    MultiComparator keyCmp = lsmInitState.getKeyComparator();
    keySearchPred = new RangePredicate(null, null, true, true, keyCmp, keyCmp);
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) IBTreeLeafFrame(org.apache.hyracks.storage.am.btree.api.IBTreeLeafFrame) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) ILSMComponent(org.apache.hyracks.storage.am.lsm.common.api.ILSMComponent) BloomFilterAwareBTreePointSearchCursor(org.apache.hyracks.storage.am.lsm.common.impls.BloomFilterAwareBTreePointSearchCursor)

Aggregations

RangePredicate (org.apache.hyracks.storage.am.btree.impls.RangePredicate)32 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)18 IIndexCursor (org.apache.hyracks.storage.common.IIndexCursor)15 MultiComparator (org.apache.hyracks.storage.common.MultiComparator)13 IIndexAccessor (org.apache.hyracks.storage.common.IIndexAccessor)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 ArrayList (java.util.ArrayList)7 ITreeIndexAccessor (org.apache.hyracks.storage.am.common.api.ITreeIndexAccessor)7 ITreeIndexCursor (org.apache.hyracks.storage.am.common.api.ITreeIndexCursor)7 ILSMComponent (org.apache.hyracks.storage.am.lsm.common.api.ILSMComponent)7 IBTreeLeafFrame (org.apache.hyracks.storage.am.btree.api.IBTreeLeafFrame)6 BTreeRangeSearchCursor (org.apache.hyracks.storage.am.btree.impls.BTreeRangeSearchCursor)6 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)5 ArrayTupleReference (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference)5 BTree (org.apache.hyracks.storage.am.btree.impls.BTree)5 ILSMDiskComponentBulkLoader (org.apache.hyracks.storage.am.lsm.common.api.ILSMDiskComponentBulkLoader)5 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)4 AMutableString (org.apache.asterix.om.base.AMutableString)3 AString (org.apache.asterix.om.base.AString)3 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)3