Search in sources :

Example 1 with LSMBTreeTupleReference

use of org.apache.hyracks.storage.am.lsm.btree.tuples.LSMBTreeTupleReference in project asterixdb by apache.

the class LSMBTree method insert.

private boolean insert(ITupleReference tuple, LSMBTreeOpContext ctx) throws HyracksDataException {
    LSMBTreePointSearchCursor searchCursor = ctx.getInsertSearchCursor();
    IIndexCursor memCursor = ctx.getMemCursor();
    RangePredicate predicate = (RangePredicate) ctx.getSearchPredicate();
    predicate.setHighKey(tuple);
    predicate.setLowKey(tuple);
    if (needKeyDupCheck) {
        // first check the inmemory component
        ctx.getCurrentMutableBTreeAccessor().search(memCursor, predicate);
        try {
            if (memCursor.hasNext()) {
                memCursor.next();
                LSMBTreeTupleReference lsmbtreeTuple = (LSMBTreeTupleReference) memCursor.getTuple();
                if (!lsmbtreeTuple.isAntimatter()) {
                    throw HyracksDataException.create(ErrorCode.DUPLICATE_KEY);
                } else {
                    memCursor.close();
                    ctx.getCurrentMutableBTreeAccessor().upsertIfConditionElseInsert(tuple, AntimatterAwareTupleAcceptor.INSTANCE);
                    return true;
                }
            }
        } finally {
            memCursor.close();
        }
        // TODO: Can we just remove the above code that search the mutable
        // component and do it together with the search call below? i.e. instead
        // of passing false to the lsmHarness.search(), we pass true to include
        // the mutable component?
        // the key was not in the inmemory component, so check the disk
        // components
        // This is a hack to avoid searching the current active mutable component twice. It is critical to add it back once the search is over.
        ILSMComponent firstComponent = ctx.getComponentHolder().remove(0);
        search(ctx, searchCursor, predicate);
        try {
            if (searchCursor.hasNext()) {
                throw HyracksDataException.create(ErrorCode.DUPLICATE_KEY);
            }
        } finally {
            searchCursor.close();
            // Add the current active mutable component back
            ctx.getComponentHolder().add(0, firstComponent);
        }
    }
    ctx.getCurrentMutableBTreeAccessor().upsertIfConditionElseInsert(tuple, AntimatterAwareTupleAcceptor.INSTANCE);
    return true;
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) LSMBTreeTupleReference(org.apache.hyracks.storage.am.lsm.btree.tuples.LSMBTreeTupleReference) ILSMComponent(org.apache.hyracks.storage.am.lsm.common.api.ILSMComponent) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor)

Aggregations

RangePredicate (org.apache.hyracks.storage.am.btree.impls.RangePredicate)1 LSMBTreeTupleReference (org.apache.hyracks.storage.am.lsm.btree.tuples.LSMBTreeTupleReference)1 ILSMComponent (org.apache.hyracks.storage.am.lsm.common.api.ILSMComponent)1 IIndexCursor (org.apache.hyracks.storage.common.IIndexCursor)1