Search in sources :

Example 1 with BufferAggregator

use of io.druid.query.aggregation.BufferAggregator in project druid by druid-io.

the class BaseTopNAlgorithm method makeBufferAggregators.

protected static BufferAggregator[] makeBufferAggregators(Cursor cursor, List<AggregatorFactory> aggregatorSpecs) {
    BufferAggregator[] aggregators = new BufferAggregator[aggregatorSpecs.size()];
    int aggregatorIndex = 0;
    for (AggregatorFactory spec : aggregatorSpecs) {
        aggregators[aggregatorIndex] = spec.factorizeBuffered(cursor);
        ++aggregatorIndex;
    }
    return aggregators;
}
Also used : AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) BufferAggregator(io.druid.query.aggregation.BufferAggregator)

Example 2 with BufferAggregator

use of io.druid.query.aggregation.BufferAggregator in project druid by druid-io.

the class OffheapIncrementalIndex method addToFacts.

@Override
protected Integer addToFacts(AggregatorFactory[] metrics, boolean deserializeComplexMetrics, boolean reportParseExceptions, InputRow row, AtomicInteger numEntries, TimeAndDims key, ThreadLocal<InputRow> rowContainer, Supplier<InputRow> rowSupplier) throws IndexSizeExceededException {
    ByteBuffer aggBuffer;
    int bufferIndex;
    int bufferOffset;
    synchronized (this) {
        final Integer priorIndex = facts.getPriorIndex(key);
        if (null != priorIndex) {
            final int[] indexAndOffset = indexAndOffsets.get(priorIndex);
            bufferIndex = indexAndOffset[0];
            bufferOffset = indexAndOffset[1];
            aggBuffer = aggBuffers.get(bufferIndex).get();
        } else {
            if (metrics.length > 0 && getAggs()[0] == null) {
                // note: creation of Aggregators is done lazily when at least one row from input is available
                // so that FilteredAggregators could be initialized correctly.
                rowContainer.set(row);
                for (int i = 0; i < metrics.length; i++) {
                    final AggregatorFactory agg = metrics[i];
                    getAggs()[i] = agg.factorizeBuffered(makeColumnSelectorFactory(agg, rowSupplier, deserializeComplexMetrics));
                }
                rowContainer.set(null);
            }
            bufferIndex = aggBuffers.size() - 1;
            ByteBuffer lastBuffer = aggBuffers.isEmpty() ? null : aggBuffers.get(aggBuffers.size() - 1).get();
            int[] lastAggregatorsIndexAndOffset = indexAndOffsets.isEmpty() ? null : indexAndOffsets.get(indexAndOffsets.size() - 1);
            if (lastAggregatorsIndexAndOffset != null && lastAggregatorsIndexAndOffset[0] != bufferIndex) {
                throw new ISE("last row's aggregate's buffer and last buffer index must be same");
            }
            bufferOffset = aggsTotalSize + (lastAggregatorsIndexAndOffset != null ? lastAggregatorsIndexAndOffset[1] : 0);
            if (lastBuffer != null && lastBuffer.capacity() - bufferOffset >= aggsTotalSize) {
                aggBuffer = lastBuffer;
            } else {
                ResourceHolder<ByteBuffer> bb = bufferPool.take();
                aggBuffers.add(bb);
                bufferIndex = aggBuffers.size() - 1;
                bufferOffset = 0;
                aggBuffer = bb.get();
            }
            for (int i = 0; i < metrics.length; i++) {
                getAggs()[i].init(aggBuffer, bufferOffset + aggOffsetInBuffer[i]);
            }
            // Last ditch sanity checks
            if (numEntries.get() >= maxRowCount && facts.getPriorIndex(key) == null) {
                throw new IndexSizeExceededException("Maximum number of rows [%d] reached", maxRowCount);
            }
            final Integer rowIndex = indexIncrement.getAndIncrement();
            // note that indexAndOffsets must be updated before facts, because as soon as we update facts
            // concurrent readers get hold of it and might ask for newly added row
            indexAndOffsets.add(new int[] { bufferIndex, bufferOffset });
            final Integer prev = facts.putIfAbsent(key, rowIndex);
            if (null == prev) {
                numEntries.incrementAndGet();
            } else {
                throw new ISE("WTF! we are in sychronized block.");
            }
        }
    }
    rowContainer.set(row);
    for (int i = 0; i < metrics.length; i++) {
        final BufferAggregator agg = getAggs()[i];
        synchronized (agg) {
            try {
                agg.aggregate(aggBuffer, bufferOffset + aggOffsetInBuffer[i]);
            } catch (ParseException e) {
                // "aggregate" can throw ParseExceptions if a selector expects something but gets something else.
                if (reportParseExceptions) {
                    throw new ParseException(e, "Encountered parse error for aggregator[%s]", getMetricAggs()[i].getName());
                } else {
                    log.debug(e, "Encountered parse error, skipping aggregator[%s].", getMetricAggs()[i].getName());
                }
            }
        }
    }
    rowContainer.set(null);
    return numEntries.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ISE(io.druid.java.util.common.ISE) ParseException(io.druid.java.util.common.parsers.ParseException) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) ByteBuffer(java.nio.ByteBuffer) BufferAggregator(io.druid.query.aggregation.BufferAggregator)

Example 3 with BufferAggregator

use of io.druid.query.aggregation.BufferAggregator in project druid by druid-io.

the class OffheapIncrementalIndex method getMetricFloatValue.

@Override
public float getMetricFloatValue(int rowOffset, int aggOffset) {
    BufferAggregator agg = getAggs()[aggOffset];
    int[] indexAndOffset = indexAndOffsets.get(rowOffset);
    ByteBuffer bb = aggBuffers.get(indexAndOffset[0]).get();
    return agg.getFloat(bb, indexAndOffset[1] + aggOffsetInBuffer[aggOffset]);
}
Also used : BufferAggregator(io.druid.query.aggregation.BufferAggregator) ByteBuffer(java.nio.ByteBuffer)

Example 4 with BufferAggregator

use of io.druid.query.aggregation.BufferAggregator in project druid by druid-io.

the class OffheapIncrementalIndex method getMetricLongValue.

@Override
public long getMetricLongValue(int rowOffset, int aggOffset) {
    BufferAggregator agg = getAggs()[aggOffset];
    int[] indexAndOffset = indexAndOffsets.get(rowOffset);
    ByteBuffer bb = aggBuffers.get(indexAndOffset[0]).get();
    return agg.getLong(bb, indexAndOffset[1] + aggOffsetInBuffer[aggOffset]);
}
Also used : BufferAggregator(io.druid.query.aggregation.BufferAggregator) ByteBuffer(java.nio.ByteBuffer)

Example 5 with BufferAggregator

use of io.druid.query.aggregation.BufferAggregator in project druid by druid-io.

the class OffheapIncrementalIndex method getMetricObjectValue.

@Override
public Object getMetricObjectValue(int rowOffset, int aggOffset) {
    BufferAggregator agg = getAggs()[aggOffset];
    int[] indexAndOffset = indexAndOffsets.get(rowOffset);
    ByteBuffer bb = aggBuffers.get(indexAndOffset[0]).get();
    return agg.get(bb, indexAndOffset[1] + aggOffsetInBuffer[aggOffset]);
}
Also used : BufferAggregator(io.druid.query.aggregation.BufferAggregator) ByteBuffer(java.nio.ByteBuffer)

Aggregations

BufferAggregator (io.druid.query.aggregation.BufferAggregator)6 ByteBuffer (java.nio.ByteBuffer)4 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)3 ISE (io.druid.java.util.common.ISE)1 ParseException (io.druid.java.util.common.parsers.ParseException)1 ColumnSelectorFactory (io.druid.segment.ColumnSelectorFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1