Search in sources :

Example 11 with CheckTuple

use of org.apache.hyracks.storage.am.common.CheckTuple in project asterixdb by apache.

the class LSMInvertedIndexTestUtils method compareActualAndExpectedIndexesRangeSearch.

/**
     * Compares actual and expected indexes using the rangeSearch() method of the inverted-index accessor.
     */
public static void compareActualAndExpectedIndexesRangeSearch(LSMInvertedIndexTestContext testCtx) throws HyracksDataException {
    IInvertedIndex invIndex = (IInvertedIndex) testCtx.getIndex();
    int tokenFieldCount = invIndex.getTokenTypeTraits().length;
    int invListFieldCount = invIndex.getInvListTypeTraits().length;
    IInvertedIndexAccessor invIndexAccessor = (IInvertedIndexAccessor) invIndex.createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    IIndexCursor invIndexCursor = invIndexAccessor.createRangeSearchCursor();
    MultiComparator tokenCmp = MultiComparator.create(invIndex.getTokenCmpFactories());
    IBinaryComparatorFactory[] tupleCmpFactories = new IBinaryComparatorFactory[tokenFieldCount + invListFieldCount];
    for (int i = 0; i < tokenFieldCount; i++) {
        tupleCmpFactories[i] = invIndex.getTokenCmpFactories()[i];
    }
    for (int i = 0; i < invListFieldCount; i++) {
        tupleCmpFactories[tokenFieldCount + i] = invIndex.getInvListCmpFactories()[i];
    }
    MultiComparator tupleCmp = MultiComparator.create(tupleCmpFactories);
    RangePredicate nullPred = new RangePredicate(null, null, true, true, tokenCmp, tokenCmp);
    invIndexAccessor.rangeSearch(invIndexCursor, nullPred);
    // Helpers for generating a serialized inverted-list element from a CheckTuple from the expected index.
    ISerializerDeserializer[] fieldSerdes = testCtx.getFieldSerdes();
    ArrayTupleBuilder expectedBuilder = new ArrayTupleBuilder(fieldSerdes.length);
    ArrayTupleReference expectedTuple = new ArrayTupleReference();
    Iterator<CheckTuple> expectedIter = testCtx.getCheckTuples().iterator();
    // Compare index elements.
    try {
        while (invIndexCursor.hasNext() && expectedIter.hasNext()) {
            invIndexCursor.next();
            ITupleReference actualTuple = invIndexCursor.getTuple();
            CheckTuple expected = expectedIter.next();
            OrderedIndexTestUtils.createTupleFromCheckTuple(expected, expectedBuilder, expectedTuple, fieldSerdes);
            if (tupleCmp.compare(actualTuple, expectedTuple) != 0) {
                fail("Index entries differ for token '" + expected.getField(0) + "'.");
            }
        }
        if (expectedIter.hasNext()) {
            fail("Indexes do not match. Actual index is missing entries.");
        }
        if (invIndexCursor.hasNext()) {
            fail("Indexes do not match. Actual index contains too many entries.");
        }
    } finally {
        invIndexCursor.close();
    }
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) ArrayTupleReference(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference) IBinaryComparatorFactory(org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory) IInvertedIndexAccessor(org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedIndexAccessor) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) CheckTuple(org.apache.hyracks.storage.am.common.CheckTuple) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor) IInvertedIndex(org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedIndex)

Example 12 with CheckTuple

use of org.apache.hyracks.storage.am.common.CheckTuple in project asterixdb by apache.

the class OrderedIndexTestUtils method updateTuples.

@SuppressWarnings("unchecked")
public void updateTuples(IIndexTestContext ictx, int numTuples, Random rnd) throws Exception {
    OrderedIndexTestContext ctx = (OrderedIndexTestContext) ictx;
    int fieldCount = ctx.getFieldCount();
    int keyFieldCount = ctx.getKeyFieldCount();
    // This is a noop because we can only update non-key fields.
    if (fieldCount == keyFieldCount) {
        return;
    }
    ArrayTupleBuilder updateTupleBuilder = new ArrayTupleBuilder(fieldCount);
    ArrayTupleReference updateTuple = new ArrayTupleReference();
    int numCheckTuples = ctx.getCheckTuples().size();
    // Copy CheckTuple references into array, so we can randomly pick from
    // there.
    CheckTuple[] checkTuples = new CheckTuple[numCheckTuples];
    int idx = 0;
    for (CheckTuple checkTuple : ctx.getCheckTuples()) {
        checkTuples[idx++] = checkTuple;
    }
    for (int i = 0; i < numTuples && numCheckTuples > 0; i++) {
        if (LOGGER.isLoggable(Level.INFO)) {
            if ((i + 1) % (numTuples / Math.min(10, numTuples)) == 0) {
                LOGGER.info("Updating Tuple " + (i + 1) + "/" + numTuples);
            }
        }
        int checkTupleIdx = Math.abs(rnd.nextInt() % numCheckTuples);
        CheckTuple checkTuple = checkTuples[checkTupleIdx];
        // Update check tuple's non-key fields.
        for (int j = keyFieldCount; j < fieldCount; j++) {
            Comparable newValue = getRandomUpdateValue(ctx.getFieldSerdes()[j], rnd);
            checkTuple.setField(j, newValue);
        }
        createTupleFromCheckTuple(checkTuple, updateTupleBuilder, updateTuple, ctx.getFieldSerdes());
        ctx.getIndexAccessor().update(updateTuple);
        // Swap with last "valid" CheckTuple.
        CheckTuple tmp = checkTuples[numCheckTuples - 1];
        checkTuples[numCheckTuples - 1] = checkTuple;
        checkTuples[checkTupleIdx] = tmp;
        numCheckTuples--;
    }
}
Also used : CheckTuple(org.apache.hyracks.storage.am.common.CheckTuple) ArrayTupleReference(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)

Example 13 with CheckTuple

use of org.apache.hyracks.storage.am.common.CheckTuple in project asterixdb by apache.

the class OrderedIndexTestUtils method checkDiskOrderScanResult.

@Override
protected boolean checkDiskOrderScanResult(ITupleReference tuple, CheckTuple checkTuple, IIndexTestContext ctx) throws HyracksDataException {
    @SuppressWarnings("unchecked") TreeSet<CheckTuple> checkTuples = (TreeSet<CheckTuple>) ctx.getCheckTuples();
    CheckTuple matchingCheckTuple = checkTuples.floor(checkTuple);
    if (matchingCheckTuple == null) {
        return false;
    }
    compareActualAndExpected(tuple, matchingCheckTuple, ctx.getFieldSerdes());
    return true;
}
Also used : CheckTuple(org.apache.hyracks.storage.am.common.CheckTuple) TreeSet(java.util.TreeSet)

Example 14 with CheckTuple

use of org.apache.hyracks.storage.am.common.CheckTuple in project asterixdb by apache.

the class OrderedIndexTestUtils method insertCheckTuples.

public static void insertCheckTuples(IIndexTestContext ctx, Collection<CheckTuple> checkTuples, boolean filtered) throws HyracksDataException {
    int fieldCount = ctx.getFieldCount();
    int numTuples = checkTuples.size();
    ArrayTupleBuilder tupleBuilder = filtered ? new ArrayTupleBuilder(fieldCount + 1) : new ArrayTupleBuilder(fieldCount);
    ArrayTupleReference tuple = new ArrayTupleReference();
    int c = 1;
    for (CheckTuple checkTuple : checkTuples) {
        if (LOGGER.isLoggable(Level.INFO)) {
            if (c % (numTuples / 10) == 0) {
                LOGGER.info("Inserting Tuple " + c + "/" + numTuples);
            }
        }
        createTupleFromCheckTuple(checkTuple, tupleBuilder, tuple, ctx.getFieldSerdes(), filtered);
        ctx.getIndexAccessor().insert(tuple);
        c++;
    }
}
Also used : CheckTuple(org.apache.hyracks.storage.am.common.CheckTuple) ArrayTupleReference(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)

Example 15 with CheckTuple

use of org.apache.hyracks.storage.am.common.CheckTuple 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)

Aggregations

CheckTuple (org.apache.hyracks.storage.am.common.CheckTuple)18 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)8 TreeSet (java.util.TreeSet)6 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)6 ArrayTupleReference (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference)6 MultiComparator (org.apache.hyracks.storage.common.MultiComparator)4 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)3 RangePredicate (org.apache.hyracks.storage.am.btree.impls.RangePredicate)3 IIndexCursor (org.apache.hyracks.storage.common.IIndexCursor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInput (java.io.DataInput)2 DataInputStream (java.io.DataInputStream)2 IInvertedIndex (org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedIndex)2 IInvertedIndexAccessor (org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedIndexAccessor)2 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)1 GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)1 PermutingTupleReference (org.apache.hyracks.storage.am.common.tuples.PermutingTupleReference)1 IInvertedListCursor (org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedListCursor)1 IToken (org.apache.hyracks.storage.am.lsm.invertedindex.tokenizers.IToken)1 IIndexBulkLoader (org.apache.hyracks.storage.common.IIndexBulkLoader)1