Search in sources :

Example 1 with BTreeAccessor

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

the class LSMRTree method flush.

@Override
public ILSMDiskComponent flush(ILSMIOOperation operation) throws HyracksDataException {
    LSMRTreeFlushOperation flushOp = (LSMRTreeFlushOperation) operation;
    LSMRTreeMemoryComponent flushingComponent = (LSMRTreeMemoryComponent) flushOp.getFlushingComponent();
    // Renaming order is critical because we use assume ordering when we
    // read the file names when we open the tree.
    // The RTree should be renamed before the BTree.
    // scan the memory RTree
    ITreeIndexAccessor memRTreeAccessor = flushingComponent.getRTree().createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    RTreeSearchCursor rtreeScanCursor = (RTreeSearchCursor) memRTreeAccessor.createSearchCursor(false);
    SearchPredicate rtreeNullPredicate = new SearchPredicate(null, null);
    memRTreeAccessor.search(rtreeScanCursor, rtreeNullPredicate);
    LSMRTreeDiskComponent component = createDiskComponent(componentFactory, flushOp.getTarget(), flushOp.getBTreeTarget(), flushOp.getBloomFilterTarget(), true);
    //count the number of tuples in the buddy btree
    ITreeIndexAccessor memBTreeAccessor = flushingComponent.getBTree().createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    RangePredicate btreeNullPredicate = new RangePredicate(null, null, true, true, null, null);
    IIndexCursor btreeCountingCursor = ((BTreeAccessor) memBTreeAccessor).createCountingSearchCursor();
    memBTreeAccessor.search(btreeCountingCursor, btreeNullPredicate);
    long numBTreeTuples = 0L;
    try {
        while (btreeCountingCursor.hasNext()) {
            btreeCountingCursor.next();
            ITupleReference countTuple = btreeCountingCursor.getTuple();
            numBTreeTuples = IntegerPointable.getInteger(countTuple.getFieldData(0), countTuple.getFieldStart(0));
        }
    } finally {
        btreeCountingCursor.close();
    }
    ILSMDiskComponentBulkLoader componentBulkLoader = createComponentBulkLoader(component, 1.0f, false, numBTreeTuples, false, false);
    ITreeIndexCursor cursor;
    IBinaryComparatorFactory[] linearizerArray = { linearizer };
    TreeTupleSorter rTreeTupleSorter = new TreeTupleSorter(flushingComponent.getRTree().getFileId(), linearizerArray, rtreeLeafFrameFactory.createFrame(), rtreeLeafFrameFactory.createFrame(), flushingComponent.getRTree().getBufferCache(), comparatorFields);
    // BulkLoad the tuples from the in-memory tree into the new disk
    // RTree.
    boolean isEmpty = true;
    try {
        while (rtreeScanCursor.hasNext()) {
            isEmpty = false;
            rtreeScanCursor.next();
            rTreeTupleSorter.insertTupleEntry(rtreeScanCursor.getPageId(), rtreeScanCursor.getTupleOffset());
        }
    } finally {
        rtreeScanCursor.close();
    }
    rTreeTupleSorter.sort();
    cursor = rTreeTupleSorter;
    if (!isEmpty) {
        try {
            while (cursor.hasNext()) {
                cursor.next();
                ITupleReference frameTuple = cursor.getTuple();
                componentBulkLoader.add(frameTuple);
            }
        } finally {
            cursor.close();
        }
    }
    // scan the memory BTree
    IIndexCursor btreeScanCursor = memBTreeAccessor.createSearchCursor(false);
    memBTreeAccessor.search(btreeScanCursor, btreeNullPredicate);
    try {
        while (btreeScanCursor.hasNext()) {
            btreeScanCursor.next();
            ITupleReference frameTuple = btreeScanCursor.getTuple();
            componentBulkLoader.delete(frameTuple);
        }
    } finally {
        btreeScanCursor.close();
    }
    if (component.getLSMComponentFilter() != null) {
        List<ITupleReference> filterTuples = new ArrayList<>();
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMinTuple());
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMaxTuple());
        getFilterManager().updateFilter(component.getLSMComponentFilter(), filterTuples);
        getFilterManager().writeFilter(component.getLSMComponentFilter(), component.getRTree());
    }
    // Note. If we change the filter to write to metadata object, we don't need the if block above
    flushingComponent.getMetadata().copy(component.getMetadata());
    componentBulkLoader.end();
    return component;
}
Also used : ITreeIndexCursor(org.apache.hyracks.storage.am.common.api.ITreeIndexCursor) RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) IBinaryComparatorFactory(org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory) ArrayList(java.util.ArrayList) BTreeAccessor(org.apache.hyracks.storage.am.btree.impls.BTree.BTreeAccessor) ILSMDiskComponentBulkLoader(org.apache.hyracks.storage.am.lsm.common.api.ILSMDiskComponentBulkLoader) RTreeSearchCursor(org.apache.hyracks.storage.am.rtree.impls.RTreeSearchCursor) SearchPredicate(org.apache.hyracks.storage.am.rtree.impls.SearchPredicate) ISearchPredicate(org.apache.hyracks.storage.common.ISearchPredicate) ITreeIndexAccessor(org.apache.hyracks.storage.am.common.api.ITreeIndexAccessor) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor)

Example 2 with BTreeAccessor

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

the class LSMInvertedIndex method flush.

@Override
public ILSMDiskComponent flush(ILSMIOOperation operation) throws HyracksDataException {
    LSMInvertedIndexFlushOperation flushOp = (LSMInvertedIndexFlushOperation) operation;
    // Create an inverted index instance to be bulk loaded.
    LSMInvertedIndexDiskComponent component = createDiskInvIndexComponent(componentFactory, flushOp.getTarget(), flushOp.getDeletedKeysBTreeTarget(), flushOp.getBloomFilterTarget(), true);
    // Create a scan cursor on the BTree underlying the in-memory inverted index.
    LSMInvertedIndexMemoryComponent flushingComponent = (LSMInvertedIndexMemoryComponent) flushOp.getFlushingComponent();
    RangePredicate nullPred = new RangePredicate(null, null, true, true, null, null);
    // Search the deleted keys BTree to calculate the number of elements for BloomFilter
    IIndexAccessor deletedKeysBTreeAccessor = flushingComponent.getDeletedKeysBTree().createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    IIndexCursor btreeCountingCursor = ((BTreeAccessor) deletedKeysBTreeAccessor).createCountingSearchCursor();
    deletedKeysBTreeAccessor.search(btreeCountingCursor, nullPred);
    long numBTreeTuples = 0L;
    try {
        while (btreeCountingCursor.hasNext()) {
            btreeCountingCursor.next();
            ITupleReference countTuple = btreeCountingCursor.getTuple();
            numBTreeTuples = IntegerPointable.getInteger(countTuple.getFieldData(0), countTuple.getFieldStart(0));
        }
    } finally {
        btreeCountingCursor.close();
    }
    ILSMDiskComponentBulkLoader componentBulkLoader = createComponentBulkLoader(component, 1.0f, false, numBTreeTuples, false, false);
    // Create a scan cursor on the deleted keys BTree underlying the in-memory inverted index.
    IIndexCursor deletedKeysScanCursor = deletedKeysBTreeAccessor.createSearchCursor(false);
    deletedKeysBTreeAccessor.search(deletedKeysScanCursor, nullPred);
    try {
        while (deletedKeysScanCursor.hasNext()) {
            deletedKeysScanCursor.next();
            ((LSMInvertedIndexDiskComponentBulkLoader) componentBulkLoader).delete(deletedKeysScanCursor.getTuple());
        }
    } finally {
        deletedKeysScanCursor.close();
    }
    // Scan the in-memory inverted index
    InMemoryInvertedIndexAccessor memInvIndexAccessor = (InMemoryInvertedIndexAccessor) flushingComponent.getInvIndex().createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    BTreeAccessor memBTreeAccessor = memInvIndexAccessor.getBTreeAccessor();
    IIndexCursor scanCursor = memBTreeAccessor.createSearchCursor(false);
    memBTreeAccessor.search(scanCursor, nullPred);
    // Bulk load the disk inverted index from the in-memory inverted index.
    try {
        while (scanCursor.hasNext()) {
            scanCursor.next();
            componentBulkLoader.add(scanCursor.getTuple());
        }
    } finally {
        scanCursor.close();
    }
    if (component.getLSMComponentFilter() != null) {
        List<ITupleReference> filterTuples = new ArrayList<>();
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMinTuple());
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMaxTuple());
        filterManager.updateFilter(component.getLSMComponentFilter(), filterTuples);
        filterManager.writeFilter(component.getLSMComponentFilter(), ((OnDiskInvertedIndex) component.getInvIndex()).getBTree());
    }
    flushingComponent.getMetadata().copy(component.getMetadata());
    componentBulkLoader.end();
    return component;
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) ArrayList(java.util.ArrayList) BTreeAccessor(org.apache.hyracks.storage.am.btree.impls.BTree.BTreeAccessor) ILSMDiskComponentBulkLoader(org.apache.hyracks.storage.am.lsm.common.api.ILSMDiskComponentBulkLoader) InMemoryInvertedIndexAccessor(org.apache.hyracks.storage.am.lsm.invertedindex.inmemory.InMemoryInvertedIndexAccessor) IIndexAccessor(org.apache.hyracks.storage.common.IIndexAccessor) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor)

Example 3 with BTreeAccessor

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

the class LSMBTree method flush.

@Override
public ILSMDiskComponent flush(ILSMIOOperation operation) throws HyracksDataException {
    LSMBTreeFlushOperation flushOp = (LSMBTreeFlushOperation) operation;
    LSMBTreeMemoryComponent flushingComponent = (LSMBTreeMemoryComponent) flushOp.getFlushingComponent();
    IIndexAccessor accessor = flushingComponent.getBTree().createAccessor(NoOpOperationCallback.INSTANCE, NoOpOperationCallback.INSTANCE);
    RangePredicate nullPred = new RangePredicate(null, null, true, true, null, null);
    long numElements = 0L;
    if (hasBloomFilter) {
        //count elements in btree for creating Bloomfilter
        IIndexCursor countingCursor = ((BTreeAccessor) accessor).createCountingSearchCursor();
        accessor.search(countingCursor, nullPred);
        try {
            while (countingCursor.hasNext()) {
                countingCursor.next();
                ITupleReference countTuple = countingCursor.getTuple();
                numElements = IntegerPointable.getInteger(countTuple.getFieldData(0), countTuple.getFieldStart(0));
            }
        } finally {
            countingCursor.close();
        }
    }
    LSMBTreeDiskComponent component = createDiskComponent(componentFactory, flushOp.getTarget(), flushOp.getBloomFilterTarget(), true);
    ILSMDiskComponentBulkLoader componentBulkLoader = createComponentBulkLoader(component, 1.0f, false, numElements, false, false);
    IIndexCursor scanCursor = accessor.createSearchCursor(false);
    accessor.search(scanCursor, nullPred);
    try {
        while (scanCursor.hasNext()) {
            scanCursor.next();
            componentBulkLoader.add(scanCursor.getTuple());
        }
    } finally {
        scanCursor.close();
    }
    if (component.getLSMComponentFilter() != null) {
        List<ITupleReference> filterTuples = new ArrayList<>();
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMinTuple());
        filterTuples.add(flushingComponent.getLSMComponentFilter().getMaxTuple());
        getFilterManager().updateFilter(component.getLSMComponentFilter(), filterTuples);
        getFilterManager().writeFilter(component.getLSMComponentFilter(), component.getBTree());
    }
    // Write metadata from memory component to disk
    // Q. what about the merge operation? how do we resolve conflicts
    // A. Through providing an appropriate ILSMIOOperationCallback
    // Must not reset the metadata before the flush is completed
    // Use the copy of the metadata in the opContext
    // TODO This code should be in the callback and not in the index
    flushingComponent.getMetadata().copy(component.getMetadata());
    componentBulkLoader.end();
    return component;
}
Also used : RangePredicate(org.apache.hyracks.storage.am.btree.impls.RangePredicate) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) ArrayList(java.util.ArrayList) IIndexCursor(org.apache.hyracks.storage.common.IIndexCursor) BTreeAccessor(org.apache.hyracks.storage.am.btree.impls.BTree.BTreeAccessor) ILSMDiskComponentBulkLoader(org.apache.hyracks.storage.am.lsm.common.api.ILSMDiskComponentBulkLoader) IIndexAccessor(org.apache.hyracks.storage.common.IIndexAccessor)

Aggregations

ArrayList (java.util.ArrayList)3 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)3 BTreeAccessor (org.apache.hyracks.storage.am.btree.impls.BTree.BTreeAccessor)3 RangePredicate (org.apache.hyracks.storage.am.btree.impls.RangePredicate)3 ILSMDiskComponentBulkLoader (org.apache.hyracks.storage.am.lsm.common.api.ILSMDiskComponentBulkLoader)3 IIndexCursor (org.apache.hyracks.storage.common.IIndexCursor)3 IIndexAccessor (org.apache.hyracks.storage.common.IIndexAccessor)2 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)1 ITreeIndexAccessor (org.apache.hyracks.storage.am.common.api.ITreeIndexAccessor)1 ITreeIndexCursor (org.apache.hyracks.storage.am.common.api.ITreeIndexCursor)1 InMemoryInvertedIndexAccessor (org.apache.hyracks.storage.am.lsm.invertedindex.inmemory.InMemoryInvertedIndexAccessor)1 RTreeSearchCursor (org.apache.hyracks.storage.am.rtree.impls.RTreeSearchCursor)1 SearchPredicate (org.apache.hyracks.storage.am.rtree.impls.SearchPredicate)1 ISearchPredicate (org.apache.hyracks.storage.common.ISearchPredicate)1