List of usage examples for org.apache.hadoop.io WritableUtils readVInt
public static int readVInt(DataInput stream) throws IOException
From source file:org.apache.phoenix.cache.aggcache.SpillManager.java
License:Apache License
private Aggregator[] getAggregators(byte[] data) throws IOException { DataInputStream input = null; try {//from www . jav a 2 s .c om input = new DataInputStream(new ByteArrayInputStream(data)); // key length int keyLength = WritableUtils.readVInt(input); int vIntKeyLength = WritableUtils.getVIntSize(keyLength); ImmutableBytesPtr ptr = new ImmutableBytesPtr(data, vIntKeyLength, keyLength); // value length input.skip(keyLength); int valueLength = WritableUtils.readVInt(input); int vIntValLength = WritableUtils.getVIntSize(keyLength); KeyValue keyValue = KeyValueUtil.newKeyValue(ptr.get(), ptr.getOffset(), ptr.getLength(), QueryConstants.SINGLE_COLUMN_FAMILY, QueryConstants.SINGLE_COLUMN, QueryConstants.AGG_TIMESTAMP, data, vIntKeyLength + keyLength + vIntValLength, valueLength); Tuple result = new SingleKeyValueTuple(keyValue); TupleUtil.getAggregateValue(result, ptr); KeyValueSchema schema = aggregators.getValueSchema(); ValueBitSet tempValueSet = ValueBitSet.newInstance(schema); tempValueSet.clear(); tempValueSet.or(ptr); int i = 0, maxOffset = ptr.getOffset() + ptr.getLength(); SingleAggregateFunction[] funcArray = aggregators.getFunctions(); Aggregator[] sAggs = new Aggregator[funcArray.length]; Boolean hasValue; schema.iterator(ptr); while ((hasValue = schema.next(ptr, i, maxOffset, tempValueSet)) != null) { SingleAggregateFunction func = funcArray[i]; sAggs[i++] = hasValue ? func.newServerAggregator(conf, ptr) : func.newServerAggregator(conf); } return sAggs; } finally { Closeables.closeQuietly(input); } }
From source file:org.apache.phoenix.coprocessor.GroupedAggregateRegionObserver.java
License:Apache License
private List<Expression> deserializeGroupByExpressions(byte[] expressionBytes, int offset) throws IOException { List<Expression> expressions = new ArrayList<Expression>(3); ByteArrayInputStream stream = new ByteArrayInputStream(expressionBytes); try {/*from w w w . j a v a2 s. c o m*/ DataInputStream input = new DataInputStream(stream); while (true) { try { int expressionOrdinal = WritableUtils.readVInt(input); Expression expression = ExpressionType.values()[expressionOrdinal].newInstance(); expression.readFields(input); if (offset != 0) { IndexUtil.setRowKeyExpressionOffset(expression, offset); } expressions.add(expression); } catch (EOFException e) { break; } } } finally { stream.close(); } return expressions; }
From source file:org.apache.phoenix.coprocessor.UngroupedAggregateRegionObserver.java
License:Apache License
private static List<Expression> deserializeExpressions(byte[] b) { ByteArrayInputStream stream = new ByteArrayInputStream(b); try {/*from w w w . ja va 2s.com*/ DataInputStream input = new DataInputStream(stream); int size = WritableUtils.readVInt(input); List<Expression> selectExpressions = Lists.newArrayListWithExpectedSize(size); for (int i = 0; i < size; i++) { ExpressionType type = ExpressionType.values()[WritableUtils.readVInt(input)]; Expression selectExpression = type.newInstance(); selectExpression.readFields(input); selectExpressions.add(selectExpression); } return selectExpressions; } catch (IOException e) { throw new RuntimeException(e); } finally { try { stream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.apache.phoenix.execute.TupleProjector.java
License:Apache License
public static TupleProjector deserializeProjectorFromScan(Scan scan) { byte[] proj = scan.getAttribute(SCAN_PROJECTOR); if (proj == null) { return null; }//from ww w.j a v a2s . c o m ByteArrayInputStream stream = new ByteArrayInputStream(proj); try { DataInputStream input = new DataInputStream(stream); KeyValueSchema schema = new KeyValueSchema(); schema.readFields(input); int count = WritableUtils.readVInt(input); Expression[] expressions = new Expression[count]; for (int i = 0; i < count; i++) { int ordinal = WritableUtils.readVInt(input); expressions[i] = ExpressionType.values()[ordinal].newInstance(); expressions[i].readFields(input); } return new TupleProjector(schema, expressions); } catch (IOException e) { throw new RuntimeException(e); } finally { try { stream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.apache.phoenix.expression.aggregator.DistinctValueWithCountClientAggregator.java
License:Apache License
@Override public void aggregate(Tuple tuple, ImmutableBytesWritable ptr) { if (tuple instanceof SingleKeyValueTuple) { // Case when scanners do look ahead and re-aggregate result row.The result is already available in the ptr PDataType resultDataType = getResultDataType(); cachedResult = resultDataType.toObject(ptr, resultDataType, sortOrder); } else {// ww w . java 2s. c om InputStream is; try { if (Bytes.equals(ptr.get(), ptr.getOffset(), 1, DistinctValueWithCountServerAggregator.COMPRESS_MARKER, 0, 1)) { // This reads the uncompressed length from the front of the compressed input int uncompressedLength = Snappy.getUncompressedLength(ptr.get(), ptr.getOffset() + 1); byte[] uncompressed = new byte[uncompressedLength]; // This will throw CorruptionException, a RuntimeException if the snappy data is invalid. // We're making a RuntimeException out of a checked IOException below so assume it's ok // to let any CorruptionException escape. Snappy.uncompress(ptr.get(), ptr.getOffset() + 1, ptr.getLength() - 1, uncompressed, 0); is = new ByteArrayInputStream(uncompressed, 0, uncompressedLength); } else { is = new ByteArrayInputStream(ptr.get(), ptr.getOffset() + 1, ptr.getLength() - 1); } DataInputStream in = new DataInputStream(is); int mapSize = WritableUtils.readVInt(in); for (int i = 0; i < mapSize; i++) { int keyLen = WritableUtils.readVInt(in); byte[] keyBytes = new byte[keyLen]; in.read(keyBytes, 0, keyLen); ImmutableBytesPtr key = new ImmutableBytesPtr(keyBytes); int value = WritableUtils.readVInt(in); Integer curCount = valueVsCount.get(key); if (curCount == null) { valueVsCount.put(key, value); } else { valueVsCount.put(key, curCount + value); } totalCount += value; } } catch (IOException ioe) { throw new RuntimeException(ioe); // Impossible as we're using a ByteArrayInputStream } } if (buffer == null) { initBuffer(); } }
From source file:org.apache.phoenix.expression.aggregator.ServerAggregators.java
License:Apache License
/** * Deserialize aggregators from the serialized byte array representation * @param b byte array representation of a list of Aggregators * @param conf Server side configuration used by HBase * @return newly instantiated Aggregators instance *///from ww w . j a v a2 s. com public static ServerAggregators deserialize(byte[] b, Configuration conf) { if (b == null) { return ServerAggregators.EMPTY_AGGREGATORS; } ByteArrayInputStream stream = new ByteArrayInputStream(b); try { DataInputStream input = new DataInputStream(stream); int minNullableIndex = WritableUtils.readVInt(input); int len = WritableUtils.readVInt(input); Aggregator[] aggregators = new Aggregator[len]; Expression[] expressions = new Expression[len]; SingleAggregateFunction[] functions = new SingleAggregateFunction[len]; for (int i = 0; i < aggregators.length; i++) { SingleAggregateFunction aggFunc = (SingleAggregateFunction) ExpressionType.values()[WritableUtils .readVInt(input)].newInstance(); aggFunc.readFields(input, conf); functions[i] = aggFunc; aggregators[i] = aggFunc.getAggregator(); expressions[i] = aggFunc.getAggregatorExpression(); } return new ServerAggregators(functions, aggregators, expressions, minNullableIndex); } catch (IOException e) { throw new RuntimeException(e); } finally { try { stream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.apache.phoenix.expression.ArrayConstructorExpression.java
License:Apache License
@Override public void readFields(DataInput input) throws IOException { super.readFields(input); boolean rowKeyOrderOptimizable = false; int baseTypeOrdinal = WritableUtils.readVInt(input); if (baseTypeOrdinal < 0) { rowKeyOrderOptimizable = true;/* w w w . j av a 2 s. c o m*/ baseTypeOrdinal = -(baseTypeOrdinal + 1); } init(PDataType.values()[baseTypeOrdinal], rowKeyOrderOptimizable); }
From source file:org.apache.phoenix.expression.BaseCompoundExpression.java
License:Apache License
@Override public void readFields(DataInput input) throws IOException { int len = WritableUtils.readVInt(input); List<Expression> children = new ArrayList<Expression>(len); for (int i = 0; i < len; i++) { Expression child = ExpressionType.values()[WritableUtils.readVInt(input)].newInstance(); child.readFields(input);/*w ww.j a v a2 s .co m*/ children.add(child); } init(children); }
From source file:org.apache.phoenix.expression.BaseSingleExpression.java
License:Apache License
@Override public void readFields(DataInput input) throws IOException { Expression expression = ExpressionType.values()[WritableUtils.readVInt(input)].newInstance(); expression.readFields(input);/*from w w w .j ava 2 s.c o m*/ children = ImmutableList.of(expression); }
From source file:org.apache.phoenix.expression.CaseExpression.java
License:Apache License
@Override public void readFields(DataInput input) throws IOException { super.readFields(input); this.returnType = PDataType.values()[WritableUtils.readVInt(input)]; }