Search in sources :

Example 26 with RangePredicate

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

the class LSMInvertedIndexDeletedKeysBTreeMergeCursor method open.

@Override
public void open(ICursorInitialState initialState, ISearchPredicate searchPred) throws HyracksDataException {
    LSMInvertedIndexRangeSearchCursorInitialState lsmInitialState = (LSMInvertedIndexRangeSearchCursorInitialState) initialState;
    cmp = lsmInitialState.getOriginalKeyComparator();
    operationalComponents = lsmInitialState.getOperationalComponents();
    // We intentionally set the lsmHarness to null so that we don't call lsmHarness.endSearch() because we already do that when we merge the inverted indexes.
    lsmHarness = null;
    int numBTrees = operationalComponents.size();
    rangeCursors = new IIndexCursor[numBTrees];
    MultiComparator keyCmp = lsmInitialState.getKeyComparator();
    RangePredicate btreePredicate = new RangePredicate(null, null, true, true, keyCmp, keyCmp);
    ArrayList<IIndexAccessor> btreeAccessors = lsmInitialState.getDeletedKeysBTreeAccessors();
    for (int i = 0; i < numBTrees; i++) {
        rangeCursors[i] = btreeAccessors.get(i).createSearchCursor(false);
        btreeAccessors.get(i).search(rangeCursors[i], btreePredicate);
    }
    setPriorityQueueComparator();
    initPriorityQueue();
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) IIndexAccessor(org.apache.hyracks.storage.common.IIndexAccessor)

Example 27 with RangePredicate

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

the class BTreeSearchCursorTest method createRangePredicate.

public RangePredicate createRangePredicate(int lk, int hk, boolean lowKeyInclusive, boolean highKeyInclusive) throws HyracksDataException {
    // create tuplereferences for search keys
    ITupleReference lowKey = TupleUtils.createIntegerTuple(false, lk);
    ITupleReference highKey = TupleUtils.createIntegerTuple(false, hk);
    IBinaryComparator[] searchCmps = new IBinaryComparator[1];
    searchCmps[0] = PointableBinaryComparatorFactory.of(IntegerPointable.FACTORY).createBinaryComparator();
    MultiComparator searchCmp = new MultiComparator(searchCmps);
    RangePredicate rangePred = new RangePredicate(lowKey, highKey, lowKeyInclusive, highKeyInclusive, searchCmp, searchCmp);
    return rangePred;
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) MultiComparator(org.apache.hyracks.storage.common.MultiComparator) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IBinaryComparator(org.apache.hyracks.api.dataflow.value.IBinaryComparator)

Example 28 with RangePredicate

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

the class BTreeSearchCursorTest method performSearches.

public boolean performSearches(ArrayList<Integer> keys, BTree btree, IBTreeLeafFrame leafFrame, IBTreeInteriorFrame interiorFrame, int minKey, int maxKey, boolean lowKeyInclusive, boolean highKeyInclusive, boolean printExpectedResults) throws Exception {
    ArrayList<Integer> results = new ArrayList<>();
    ArrayList<Integer> expectedResults = new ArrayList<>();
    for (int i = minKey; i < maxKey; i++) {
        for (int j = minKey; j < maxKey; j++) {
            results.clear();
            expectedResults.clear();
            int lowKey = i;
            int highKey = j;
            ITreeIndexCursor rangeCursor = new BTreeRangeSearchCursor(leafFrame, false);
            RangePredicate rangePred = createRangePredicate(lowKey, highKey, lowKeyInclusive, highKeyInclusive);
            ITreeIndexAccessor indexAccessor = btree.createAccessor(TestOperationCallback.INSTANCE, TestOperationCallback.INSTANCE);
            indexAccessor.search(rangeCursor, rangePred);
            try {
                while (rangeCursor.hasNext()) {
                    rangeCursor.next();
                    ITupleReference frameTuple = rangeCursor.getTuple();
                    ByteArrayInputStream inStream = new ByteArrayInputStream(frameTuple.getFieldData(0), frameTuple.getFieldStart(0), frameTuple.getFieldLength(0));
                    DataInput dataIn = new DataInputStream(inStream);
                    Integer res = IntegerSerializerDeserializer.INSTANCE.deserialize(dataIn);
                    results.add(res);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                rangeCursor.close();
            }
            getExpectedResults(expectedResults, keys, lowKey, highKey, lowKeyInclusive, highKeyInclusive);
            if (printExpectedResults) {
                if (expectedResults.size() > 0) {
                    char l, u;
                    if (lowKeyInclusive) {
                        l = '[';
                    } else {
                        l = '(';
                    }
                    if (highKeyInclusive) {
                        u = ']';
                    } else {
                        u = ')';
                    }
                    if (LOGGER.isLoggable(Level.INFO)) {
                        LOGGER.info("RANGE: " + l + " " + lowKey + " , " + highKey + " " + u);
                    }
                    StringBuilder strBuilder = new StringBuilder();
                    for (Integer r : expectedResults) {
                        strBuilder.append(r + " ");
                    }
                    if (LOGGER.isLoggable(Level.INFO)) {
                        LOGGER.info(strBuilder.toString());
                    }
                }
            }
            if (results.size() == expectedResults.size()) {
                for (int k = 0; k < results.size(); k++) {
                    if (!results.get(k).equals(expectedResults.get(k))) {
                        if (LOGGER.isLoggable(Level.INFO)) {
                            LOGGER.info("DIFFERENT RESULTS AT: i=" + i + " j=" + j + " k=" + k);
                            LOGGER.info(results.get(k) + " " + expectedResults.get(k));
                        }
                        return false;
                    }
                }
            } else {
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.info("UNEQUAL NUMBER OF RESULTS AT: i=" + i + " j=" + j);
                    LOGGER.info("RESULTS: " + results.size());
                    LOGGER.info("EXPECTED RESULTS: " + expectedResults.size());
                }
                return false;
            }
        }
    }
    return true;
}
Also used : ITreeIndexCursor(org.apache.hyracks.storage.am.common.api.ITreeIndexCursor) RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) BTreeRangeSearchCursor(org.apache.hyracks.storage.am.btree.impls.BTreeRangeSearchCursor) ArrayList(java.util.ArrayList) DataInputStream(java.io.DataInputStream) ITreeIndexAccessor(org.apache.hyracks.storage.am.common.api.ITreeIndexAccessor) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)

Example 29 with RangePredicate

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

the class BTreeUpdateSearchTest method test01.

// Update scan test on fixed-length tuples.
@Test
public void test01() throws Exception {
    IBufferCache bufferCache = harness.getBufferCache();
    // declare fields
    int fieldCount = 2;
    ITypeTraits[] typeTraits = new ITypeTraits[fieldCount];
    typeTraits[0] = IntegerPointable.TYPE_TRAITS;
    typeTraits[1] = IntegerPointable.TYPE_TRAITS;
    // declare keys
    int keyFieldCount = 1;
    IBinaryComparatorFactory[] cmpFactories = new IBinaryComparatorFactory[keyFieldCount];
    cmpFactories[0] = PointableBinaryComparatorFactory.of(IntegerPointable.FACTORY);
    @SuppressWarnings("rawtypes") ISerializerDeserializer[] recDescSers = { IntegerSerializerDeserializer.INSTANCE, IntegerSerializerDeserializer.INSTANCE };
    TypeAwareTupleWriterFactory tupleWriterFactory = new TypeAwareTupleWriterFactory(typeTraits);
    ITreeIndexFrameFactory leafFrameFactory = new BTreeNSMLeafFrameFactory(tupleWriterFactory);
    ITreeIndexFrameFactory interiorFrameFactory = new BTreeNSMInteriorFrameFactory(tupleWriterFactory);
    ITreeIndexMetadataFrameFactory metaFrameFactory = new LIFOMetaDataFrameFactory();
    IBTreeLeafFrame leafFrame = (IBTreeLeafFrame) leafFrameFactory.createFrame();
    IMetadataPageManager freePageManager = new LinkedMetaDataPageManager(bufferCache, metaFrameFactory);
    BTree btree = new BTree(bufferCache, harness.getFileMapProvider(), freePageManager, interiorFrameFactory, leafFrameFactory, cmpFactories, fieldCount, harness.getFileReference());
    btree.create();
    btree.activate();
    Random rnd = new Random();
    rnd.setSeed(50);
    long start = System.currentTimeMillis();
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("INSERTING INTO TREE");
    }
    ArrayTupleBuilder tb = new ArrayTupleBuilder(fieldCount);
    ArrayTupleReference insertTuple = new ArrayTupleReference();
    ITreeIndexAccessor indexAccessor = btree.createAccessor(TestOperationCallback.INSTANCE, TestOperationCallback.INSTANCE);
    int numInserts = 10000;
    for (int i = 0; i < numInserts; i++) {
        int f0 = rnd.nextInt() % 10000;
        int f1 = 5;
        TupleUtils.createIntegerTuple(tb, insertTuple, f0, f1);
        if (LOGGER.isLoggable(Level.INFO)) {
            if (i % 10000 == 0) {
                long end = System.currentTimeMillis();
                LOGGER.info("INSERTING " + i + " : " + f0 + " " + f1 + " " + (end - start));
            }
        }
        try {
            indexAccessor.insert(insertTuple);
        } catch (HyracksDataException hde) {
            if (hde.getErrorCode() != ErrorCode.DUPLICATE_KEY) {
                hde.printStackTrace();
                throw hde;
            }
        }
    }
    long end = System.currentTimeMillis();
    long duration = end - start;
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("DURATION: " + duration);
    }
    // Update scan.
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("UPDATE SCAN:");
    }
    // Set the cursor to X latch nodes.
    ITreeIndexCursor updateScanCursor = new BTreeRangeSearchCursor(leafFrame, true);
    RangePredicate nullPred = new RangePredicate(null, null, true, true, null, null);
    indexAccessor.search(updateScanCursor, nullPred);
    try {
        while (updateScanCursor.hasNext()) {
            updateScanCursor.next();
            ITupleReference tuple = updateScanCursor.getTuple();
            // Change the value field.
            IntegerPointable.setInteger(tuple.getFieldData(1), tuple.getFieldStart(1), 10);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        updateScanCursor.close();
    }
    // Ordered scan to verify the values.
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("ORDERED SCAN:");
    }
    // Set the cursor to X latch nodes.
    ITreeIndexCursor scanCursor = new BTreeRangeSearchCursor(leafFrame, true);
    indexAccessor.search(scanCursor, nullPred);
    try {
        while (scanCursor.hasNext()) {
            scanCursor.next();
            ITupleReference tuple = scanCursor.getTuple();
            String rec = TupleUtils.printTuple(tuple, recDescSers);
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info(rec);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        scanCursor.close();
    }
    btree.deactivate();
    btree.destroy();
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) BTreeRangeSearchCursor(org.apache.hyracks.storage.am.btree.impls.BTreeRangeSearchCursor) ArrayTupleReference(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleReference) BTree(org.apache.hyracks.storage.am.btree.impls.BTree) BTreeNSMInteriorFrameFactory(org.apache.hyracks.storage.am.btree.frames.BTreeNSMInteriorFrameFactory) ITreeIndexFrameFactory(org.apache.hyracks.storage.am.common.api.ITreeIndexFrameFactory) BTreeNSMLeafFrameFactory(org.apache.hyracks.storage.am.btree.frames.BTreeNSMLeafFrameFactory) ITreeIndexMetadataFrameFactory(org.apache.hyracks.storage.am.common.api.ITreeIndexMetadataFrameFactory) LIFOMetaDataFrameFactory(org.apache.hyracks.storage.am.common.frames.LIFOMetaDataFrameFactory) Random(java.util.Random) IBTreeLeafFrame(org.apache.hyracks.storage.am.btree.api.IBTreeLeafFrame) ITreeIndexCursor(org.apache.hyracks.storage.am.common.api.ITreeIndexCursor) ITypeTraits(org.apache.hyracks.api.dataflow.value.ITypeTraits) TypeAwareTupleWriterFactory(org.apache.hyracks.storage.am.common.tuples.TypeAwareTupleWriterFactory) IBinaryComparatorFactory(org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IMetadataPageManager(org.apache.hyracks.storage.am.common.api.IMetadataPageManager) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) LinkedMetaDataPageManager(org.apache.hyracks.storage.am.common.freepage.LinkedMetaDataPageManager) ITreeIndexAccessor(org.apache.hyracks.storage.am.common.api.ITreeIndexAccessor) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IBufferCache(org.apache.hyracks.storage.common.buffercache.IBufferCache) Test(org.junit.Test) AbstractBTreeTest(org.apache.hyracks.storage.am.btree.util.AbstractBTreeTest)

Example 30 with RangePredicate

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

the class BTreeTestWorker method performOp.

@Override
public void performOp(ITupleReference tuple, TestOperation op) throws HyracksDataException {
    BTree.BTreeAccessor accessor = (BTree.BTreeAccessor) indexAccessor;
    ITreeIndexCursor searchCursor = accessor.createSearchCursor(false);
    ITreeIndexCursor diskOrderScanCursor = accessor.createDiskOrderScanCursor();
    MultiComparator cmp = accessor.getOpContext().getCmp();
    RangePredicate rangePred = new RangePredicate(tuple, tuple, true, true, cmp, cmp);
    switch(op) {
        case INSERT:
            try {
                accessor.insert(tuple);
            } catch (HyracksDataException e) {
                if (e.getErrorCode() != ErrorCode.DUPLICATE_KEY) {
                    // Ignore duplicate keys, since we get random tuples.
                    throw e;
                }
            }
            break;
        case DELETE:
            // Create a tuple reference with only key fields.
            deleteTb.reset();
            for (int i = 0; i < numKeyFields; i++) {
                deleteTb.addField(tuple.getFieldData(i), tuple.getFieldStart(i), tuple.getFieldLength(i));
            }
            deleteTuple.reset(deleteTb.getFieldEndOffsets(), deleteTb.getByteArray());
            try {
                accessor.delete(deleteTuple);
            } catch (HyracksDataException e) {
                if (e.getErrorCode() != ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY) {
                    // Ignore non-existant keys, since we get random tuples.
                    throw e;
                }
            }
            break;
        case UPDATE:
            try {
                accessor.update(tuple);
            } catch (HyracksDataException e) {
                // Ignore non-existant keys, since we get random tuples.
                if (e.getErrorCode() != ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY && e.getErrorCode() != ErrorCode.INDEX_NOT_UPDATABLE) {
                    // Ignore not updateable exception due to numKeys == numFields.
                    throw e;
                }
            }
            break;
        case UPSERT:
            accessor.upsert(tuple);
            // a bigger problem and the test should fail.
            break;
        case POINT_SEARCH:
            searchCursor.reset();
            rangePred.setLowKey(tuple, true);
            rangePred.setHighKey(tuple, true);
            accessor.search(searchCursor, rangePred);
            consumeCursorTuples(searchCursor);
            break;
        case SCAN:
            searchCursor.reset();
            rangePred.setLowKey(null, true);
            rangePred.setHighKey(null, true);
            accessor.search(searchCursor, rangePred);
            consumeCursorTuples(searchCursor);
            break;
        case DISKORDER_SCAN:
            diskOrderScanCursor.reset();
            accessor.diskOrderScan(diskOrderScanCursor);
            consumeCursorTuples(diskOrderScanCursor);
            break;
        default:
            throw new HyracksDataException("Op " + op.toString() + " not supported.");
    }
}
Also used : 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) BTree(org.apache.hyracks.storage.am.btree.impls.BTree) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

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