Search in sources :

Example 1 with BaseRegionScanner

use of org.apache.phoenix.coprocessor.BaseRegionScanner in project phoenix by apache.

the class SpillableGroupByCache method getScanner.

@Override
public RegionScanner getScanner(final RegionScanner s) {
    final Iterator<Entry<ImmutableBytesWritable, Aggregator[]>> cacheIter = new EntryIterator();
    // scanner using the spillable implementation
    return new BaseRegionScanner(s) {

        @Override
        public void close() throws IOException {
            try {
                s.close();
            } finally {
                // Always close gbCache and swallow possible Exceptions
                Closeables.closeQuietly(SpillableGroupByCache.this);
            }
        }

        @Override
        public boolean next(List<Cell> results) throws IOException {
            if (!cacheIter.hasNext()) {
                return false;
            }
            Map.Entry<ImmutableBytesWritable, Aggregator[]> ce = cacheIter.next();
            ImmutableBytesWritable key = ce.getKey();
            Aggregator[] aggs = ce.getValue();
            byte[] value = aggregators.toBytes(aggs);
            if (logger.isDebugEnabled()) {
                logger.debug("Adding new distinct group: " + Bytes.toStringBinary(key.get(), key.getOffset(), key.getLength()) + " with aggregators " + aggs.toString() + " value = " + Bytes.toStringBinary(value));
            }
            results.add(KeyValueUtil.newKeyValue(key.get(), key.getOffset(), key.getLength(), SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length));
            return cacheIter.hasNext();
        }
    };
}
Also used : Entry(java.util.Map.Entry) CacheEntry(org.apache.phoenix.cache.aggcache.SpillManager.CacheEntry) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Aggregator(org.apache.phoenix.expression.aggregator.Aggregator) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with BaseRegionScanner

use of org.apache.phoenix.coprocessor.BaseRegionScanner in project phoenix by apache.

the class NonAggregateRegionScannerFactory method getOffsetScanner.

private RegionScanner getOffsetScanner(final RegionScanner s, final OffsetResultIterator iterator, final boolean isLastScan) throws IOException {
    final Tuple firstTuple;
    final Region region = getRegion();
    region.startRegionOperation();
    try {
        Tuple tuple = iterator.next();
        if (tuple == null && !isLastScan) {
            List<KeyValue> kvList = new ArrayList<KeyValue>(1);
            KeyValue kv = new KeyValue(QueryConstants.OFFSET_ROW_KEY_BYTES, QueryConstants.OFFSET_FAMILY, QueryConstants.OFFSET_COLUMN, PInteger.INSTANCE.toBytes(iterator.getRemainingOffset()));
            kvList.add(kv);
            Result r = new Result(kvList);
            firstTuple = new ResultTuple(r);
        } else {
            firstTuple = tuple;
        }
    } catch (Throwable t) {
        ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), t);
        return null;
    } finally {
        region.closeRegionOperation();
    }
    return new BaseRegionScanner(s) {

        private Tuple tuple = firstTuple;

        @Override
        public boolean isFilterDone() {
            return tuple == null;
        }

        @Override
        public boolean next(List<Cell> results) throws IOException {
            try {
                if (isFilterDone()) {
                    return false;
                }
                for (int i = 0; i < tuple.size(); i++) {
                    results.add(tuple.getValue(i));
                }
                tuple = iterator.next();
                return !isFilterDone();
            } catch (Throwable t) {
                ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), t);
                return false;
            }
        }

        @Override
        public void close() throws IOException {
            try {
                s.close();
            } finally {
                try {
                    if (iterator != null) {
                        iterator.close();
                    }
                } catch (SQLException e) {
                    ServerUtil.throwIOException(getRegion().getRegionInfo().getRegionNameAsString(), e);
                }
            }
        }
    };
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) SQLException(java.sql.SQLException) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) ArrayList(java.util.ArrayList) Region(org.apache.hadoop.hbase.regionserver.Region) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) ArrayList(java.util.ArrayList) Tuple(org.apache.phoenix.schema.tuple.Tuple) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) Result(org.apache.hadoop.hbase.client.Result)

Example 3 with BaseRegionScanner

use of org.apache.phoenix.coprocessor.BaseRegionScanner in project phoenix by apache.

the class NonAggregateRegionScannerFactory method getTopNScanner.

/**
 *  Return region scanner that does TopN.
 *  We only need to call startRegionOperation and closeRegionOperation when
 *  getting the first Tuple (which forces running through the entire region)
 *  since after this everything is held in memory
 */
private RegionScanner getTopNScanner(RegionCoprocessorEnvironment env, final RegionScanner s, final OrderedResultIterator iterator, ImmutableBytesPtr tenantId) throws Throwable {
    final Tuple firstTuple;
    TenantCache tenantCache = GlobalCache.getTenantCache(env, tenantId);
    long estSize = iterator.getEstimatedByteSize();
    final MemoryManager.MemoryChunk chunk = tenantCache.getMemoryManager().allocate(estSize);
    final Region region = getRegion();
    region.startRegionOperation();
    try {
        // Once we return from the first call to next, we've run through and cached
        // the topN rows, so we no longer need to start/stop a region operation.
        firstTuple = iterator.next();
        // Now that the topN are cached, we can resize based on the real size
        long actualSize = iterator.getByteSize();
        chunk.resize(actualSize);
    } catch (Throwable t) {
        ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), t);
        return null;
    } finally {
        region.closeRegionOperation();
    }
    return new BaseRegionScanner(s) {

        private Tuple tuple = firstTuple;

        @Override
        public boolean isFilterDone() {
            return tuple == null;
        }

        @Override
        public boolean next(List<Cell> results) throws IOException {
            try {
                if (isFilterDone()) {
                    return false;
                }
                for (int i = 0; i < tuple.size(); i++) {
                    results.add(tuple.getValue(i));
                }
                tuple = iterator.next();
                return !isFilterDone();
            } catch (Throwable t) {
                ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), t);
                return false;
            }
        }

        @Override
        public void close() throws IOException {
            try {
                s.close();
            } finally {
                try {
                    if (iterator != null) {
                        iterator.close();
                    }
                } catch (SQLException e) {
                    ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), e);
                } finally {
                    chunk.close();
                }
            }
        }
    };
}
Also used : TenantCache(org.apache.phoenix.cache.TenantCache) SQLException(java.sql.SQLException) Region(org.apache.hadoop.hbase.regionserver.Region) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) ArrayList(java.util.ArrayList) MemoryManager(org.apache.phoenix.memory.MemoryManager) Tuple(org.apache.phoenix.schema.tuple.Tuple) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple)

Aggregations

List (java.util.List)3 BaseRegionScanner (org.apache.phoenix.coprocessor.BaseRegionScanner)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Region (org.apache.hadoop.hbase.regionserver.Region)2 ResultTuple (org.apache.phoenix.schema.tuple.ResultTuple)2 Tuple (org.apache.phoenix.schema.tuple.Tuple)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 KeyValue (org.apache.hadoop.hbase.KeyValue)1 Result (org.apache.hadoop.hbase.client.Result)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 TenantCache (org.apache.phoenix.cache.TenantCache)1 CacheEntry (org.apache.phoenix.cache.aggcache.SpillManager.CacheEntry)1 Aggregator (org.apache.phoenix.expression.aggregator.Aggregator)1 MemoryManager (org.apache.phoenix.memory.MemoryManager)1