All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.modeshape.jcr.query.engine.process.BufferingSequence Maven / Gradle / Ivy

There is a newer version: 5.4.1.Final
Show newest version
/*
 * ModeShape (http://www.modeshape.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modeshape.jcr.query.engine.process;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import org.mapdb.BTreeKeySerializer;
import org.mapdb.Serializer;
import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.cache.CachedNode;
import org.modeshape.jcr.cache.CachedNodeSupplier;
import org.modeshape.jcr.index.local.MapDB.KeySerializerWithComparator;
import org.modeshape.jcr.query.BufferManager;
import org.modeshape.jcr.query.BufferManager.DistinctBuffer;
import org.modeshape.jcr.query.BufferManager.SortingBuffer;
import org.modeshape.jcr.query.NodeSequence;
import org.modeshape.jcr.query.RowExtractors.ExtractFromRow;
import org.modeshape.jcr.query.engine.process.BufferedRows.BufferedRow;
import org.modeshape.jcr.query.engine.process.BufferedRows.BufferedRowFactory;
import org.modeshape.jcr.query.model.TypeSystem.TypeFactory;

/**
 * @author Randall Hauch ([email protected])
 */
public abstract class BufferingSequence extends DelegatingSequence {

    protected static final Logger logger = Logger.getLogger(BufferingSequence.class);
    protected static final boolean trace = logger.isTraceEnabled();

    protected final SortingBuffer buffer;
    protected final BufferedRowFactory rowFactory;
    protected final ExtractFromRow extractor;
    protected final CachedNodeSupplier cache;
    protected final int width;
    protected final String workspaceName;
    protected final AtomicLong remainingRowCount = new AtomicLong();

    @SuppressWarnings( "unchecked" )
    protected BufferingSequence( String workspaceName,
                                 NodeSequence delegate,
                                 ExtractFromRow extractor,
                                 BufferManager bufferMgr,
                                 CachedNodeSupplier nodeCache,
                                 boolean pack,
                                 boolean useHeap,
                                 boolean allowDuplicates ) {
        super(delegate);
        assert extractor != null;
        this.workspaceName = workspaceName;
        this.width = delegate.width();
        this.cache = nodeCache;
        this.extractor = extractor;

        // Set up the row factory based upon the width of the delegate sequence...
        this.rowFactory = BufferedRows.serializer(nodeCache, width);

        // Set up the buffer ...
        SortingBuffer buffer = null;
        TypeFactory keyType = extractor.getType();
        if (allowDuplicates) {
            @SuppressWarnings( "rawtypes" )
            Serializer keySerializer = (Serializer>)bufferMgr.serializerFor(keyType);
            buffer = bufferMgr.createSortingWithDuplicatesBuffer(keySerializer, extractor.getType().getComparator(),
                                                                 (BufferedRowFactory)rowFactory).keepSize(true)
                              .useHeap(useHeap).make();
        } else {
            BTreeKeySerializer keySerializer = (BTreeKeySerializer)bufferMgr.bTreeKeySerializerFor(keyType, pack);
            if (keySerializer instanceof KeySerializerWithComparator) {
                keySerializer = ((KeySerializerWithComparator)keySerializer).withComparator(extractor.getType()
                                                                                                             .getComparator());
            }
            buffer = bufferMgr.createSortingBuffer(keySerializer, (BufferedRowFactory)rowFactory).keepSize(true)
                              .useHeap(useHeap).make();
        }
        this.buffer = buffer;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    protected long rowCount() {
        return buffer.size();
    }

    protected BufferedRow createRow( Batch currentRow ) {
        return rowFactory.createRow(currentRow);
    }

    /**
     * Load all of the rows from the supplied sequence into the buffer.
     * 
     * @param sequence the node sequence; may not be null
     * @param extractor the extractor for the sortable value; may not be null
     * @param rowsWithNullKey the buffer into which should be placed all rows for which the extracted key value is null; may be
     *        null if these are not to be kept
     * @return the size of the first batch, or 0 if there are no rows found
     */
    protected int loadAll( NodeSequence sequence,
                           ExtractFromRow extractor,
                           DistinctBuffer rowsWithNullKey ) {
        // Put all of the batches from the sequence into the buffer
        Batch batch = sequence.nextBatch();
        int batchSize = 0;
        Object value = null;
        while (batch != null && batchSize == 0) {
            while (batch.hasNext()) {
                batch.nextRow();
                value = extractor.getValueInRow(batch);
                if (value instanceof Object[]) {
                    // Put each of the values in the buffer ...
                    for (Object v : (Object[])value) {
                        buffer.put(v, createRow(batch));
                    }
                } else if (value != null) {
                    buffer.put(value, createRow(batch));
                } else if (rowsWithNullKey != null) {
                    rowsWithNullKey.addIfAbsent(createRow(batch));
                }
                ++batchSize;
            }
            batch = sequence.nextBatch();
        }
        while (batch != null) {
            while (batch.hasNext()) {
                batch.nextRow();
                value = extractor.getValueInRow(batch);
                if (value instanceof Object[]) {
                    // Put each of the values in the buffer ...
                    for (Object v : (Object[])value) {
                        buffer.put(v, createRow(batch));
                    }
                } else if (value != null) {
                    buffer.put(value, createRow(batch));
                } else if (rowsWithNullKey != null) {
                    rowsWithNullKey.addIfAbsent(createRow(batch));
                }
            }
            batch = sequence.nextBatch();
        }
        return batchSize;
    }

    protected Batch batchFrom( final Iterator rows,
                               final long maxBatchSize ) {
        if (rows == null || !rows.hasNext()) return null;
        if (maxBatchSize == 0 || remainingRowCount.get() <= 0) return NodeSequence.emptyBatch(workspaceName, this.width);
        final long rowsInBatch = Math.min(maxBatchSize, remainingRowCount.get());
        return new Batch() {
            private BufferedRow current;

            @Override
            public int width() {
                return width;
            }

            @Override
            public long rowCount() {
                return rowsInBatch;
            }

            @Override
            public String getWorkspaceName() {
                return workspaceName;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return remainingRowCount.get() > 0 && rows.hasNext();
            }

            @Override
            public void nextRow() {
                current = rows.next();
                remainingRowCount.decrementAndGet();
            }

            @Override
            public CachedNode getNode() {
                return current.getNode();
            }

            @Override
            public CachedNode getNode( int index ) {
                return current.getNode(index);
            }

            @Override
            public float getScore() {
                return current.getScore();
            }

            @Override
            public float getScore( int index ) {
                return current.getScore(index);
            }

            @Override
            public String toString() {
                return "(buffered-batch size=" + rowsInBatch + " )";
            }
        };
    }

    @Override
    public void close() {
        try {
            super.close();
        } finally {
            buffer.close();
        }
    }

}