Java Code Examples for org.apache.hadoop.hbase.io.ImmutableBytesWritable#getLength()

The following examples show how to use org.apache.hadoop.hbase.io.ImmutableBytesWritable#getLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[] concat(SortOrder sortOrder, ImmutableBytesWritable... writables) {
    Preconditions.checkNotNull(sortOrder);
    int totalLength = 0;
    for (ImmutableBytesWritable writable : writables) {
        totalLength += writable.getLength();
    }
    byte[] result = new byte[totalLength];
    int offset = 0;
    for (ImmutableBytesWritable array : writables) {
        byte[] bytes = array.get();
        if (sortOrder == SortOrder.DESC) {
            bytes = SortOrder.invert(bytes, array.getOffset(), new byte[array.getLength()], 0, array.getLength());
        }
        System.arraycopy(bytes, array.getOffset(), result, offset, array.getLength());
        offset += array.getLength();
    }
    return result;
}
 
Example 2
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param conn connection that was used for reading/generating value.
 * @param fullTableName fully qualified table name
 * @param value byte value of the columns concatenated as a single byte array. @see {@link #encodeValues(Connection, String, Object[], List)}
 * @param columns list of column names for the columns that have their respective values
 * present in the byte array. The column names should be in the same order as their values are in the byte array.
 * The column name includes both family name, if present, and column name.
 * @return decoded values for each column
 * @throws SQLException
 * 
 */
public static Object[] decodeValues(Connection conn, String fullTableName, byte[] value, List<Pair<String, String>> columns) throws SQLException {
    PTable table = getTable(conn, fullTableName);
    KeyValueSchema kvSchema = buildKeyValueSchema(getPColumns(table, columns));
    ImmutableBytesWritable ptr = new ImmutableBytesWritable(value);
    ValueBitSet valueSet = ValueBitSet.newInstance(kvSchema);
    valueSet.clear();
    valueSet.or(ptr);
    int maxOffset = ptr.getOffset() + ptr.getLength();
    Boolean hasValue;
    kvSchema.iterator(ptr);
    int i = 0;
    List<Object> values = new ArrayList<Object>();
    while(hasValue = kvSchema.next(ptr, i, maxOffset, valueSet) != null) {
        if(hasValue) {
            values.add(kvSchema.getField(i).getDataType().toObject(ptr));
        }
        i++;
    }
    return values.toArray();
}
 
Example 3
Source File: PDataTypeForArraysTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testForVarCharArrayForOneElementArrayWithIndex() {
	String[] strArr = new String[1];
	strArr[0] = "abx";
	PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(
			PDataType.VARCHAR, strArr);
	byte[] bytes = PDataType.VARCHAR_ARRAY.toBytes(arr);
	ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes);
	PArrayDataType.positionAtArrayElement(ptr, 0, PDataType.VARCHAR);
	int offset = ptr.getOffset();
	int length = ptr.getLength();
	byte[] bs = ptr.get();
	byte[] res = new byte[length];
	System.arraycopy(bs, offset, res, 0, length);
	assertEquals("abx", Bytes.toString(res));
}
 
Example 4
Source File: PDataTypeForArraysTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testForVarCharArrayForOddNumberWithIndex5() {
    String[] strArr = new String[5];
    strArr[0] = "abx";
    strArr[1] = "ereref";
    strArr[2] = "random";
    strArr[3] = null;
    strArr[4] = "random12";
    PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(
            PVarchar.INSTANCE, strArr);
    byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes);
    PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize());
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    byte[] bs = ptr.get();
    byte[] res = new byte[length];
    System.arraycopy(bs, offset, res, 0, length);
    assertEquals("random12", Bytes.toString(res));
}
 
Example 5
Source File: ArrayFillFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!getElementExpr().evaluate(tuple, ptr)) {
        return false;
    }
    Object element = getElementExpr().getDataType().toObject(ptr, getElementExpr().getSortOrder(), getElementExpr().getMaxLength(), getElementExpr().getScale());
    if (!getLengthExpr().evaluate(tuple, ptr) || ptr.getLength() == 0) {
        return false;
    }
    int length = (Integer) getLengthExpr().getDataType().toObject(ptr, getLengthExpr().getSortOrder(), getLengthExpr().getMaxLength(), getLengthExpr().getScale());
    if (length <= 0) {
        throw new IllegalArgumentException("Array length should be greater than 0");
    }
    Object[] elements = new Object[length];
    Arrays.fill(elements, element);
    PhoenixArray array = PDataType.instantiatePhoenixArray(getElementExpr().getDataType(), elements);
    //When max length of a char array is not the max length of the element passed in
    if (getElementExpr().getDataType().isFixedWidth() && getMaxLength() != null && getMaxLength() != array.getMaxLength()) {
        array = new PhoenixArray(array, getMaxLength());
    }
    ptr.set(((PArrayDataType) getDataType()).toBytes(array, getElementExpr().getDataType(), getElementExpr().getSortOrder()));
    return true;
}
 
Example 6
Source File: ByteUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[] concat(SortOrder sortOrder, ImmutableBytesWritable... writables) {
    Preconditions.checkNotNull(sortOrder);
    int totalLength = 0;
    for (ImmutableBytesWritable writable : writables) {
        totalLength += writable.getLength();
    }
    byte[] result = new byte[totalLength];
    int totalOffset = 0;
    for (ImmutableBytesWritable array : writables) {
        byte[] bytes = array.get();
        int offset = array.getOffset();
        if (sortOrder == SortOrder.DESC) {
            bytes = SortOrder.invert(bytes, offset, new byte[array.getLength()], 0, array.getLength());
            offset = 0;
        }
        System.arraycopy(bytes, offset, result, totalOffset, array.getLength());
        totalOffset += array.getLength();
    }
    return result;
}
 
Example 7
Source File: ArrayIndexFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
	Expression indexExpr = children.get(1);
	if (!indexExpr.evaluate(tuple, ptr)) {
	  return false;
	} else if (ptr.getLength() == 0) {
	  return true;
	}
	// Use Codec to prevent Integer object allocation
	int index = PInteger.INSTANCE.getCodec().decodeInt(ptr, indexExpr.getSortOrder());
	if(index < 0) {
		throw new ParseException("Index cannot be negative :" + index);
	}
	Expression arrayExpr = children.get(0);
	return PArrayDataType.positionAtArrayElement(tuple, ptr, index, arrayExpr, getDataType(),
       getMaxLength());
}
 
Example 8
Source File: RTrimFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    // Starting from the end of the byte, look for all single bytes at the end of the string
    // that is below SPACE_UTF8 (space and control characters) or above (control chars).
    if (!getStringExpression().evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    byte[] string = ptr.get();
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    
    SortOrder sortOrder = getStringExpression().getSortOrder();
    int i = StringUtil.getFirstNonBlankCharIdxFromEnd(string, offset, length, sortOrder);
    if (i == offset - 1) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
        }
    ptr.set(string, offset, i - offset + 1);
    return true;
}
 
Example 9
Source File: PDataTypeForArraysTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testForVarCharArrayForOddNumberWithIndex6() {
    String[] strArr = new String[6];
    strArr[0] = "abx";
    strArr[1] = "ereref";
    strArr[2] = "random";
    strArr[3] = null;
    strArr[4] = "random12";
    strArr[5] = "random17";
    PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(
            PVarchar.INSTANCE, strArr);
    byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes);
    PArrayDataType.positionAtArrayElement(ptr, 4, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize());
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    byte[] bs = ptr.get();
    byte[] res = new byte[length];
    System.arraycopy(bs, offset, res, 0, length);
    assertEquals("random12", Bytes.toString(res));
}
 
Example 10
Source File: CoalesceFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public CoalesceFunction(List<Expression> children) throws SQLException {
    super(children);

    Expression firstChild = children.get(0);
    Expression secondChild = children.get(1);

    if (ExpressionUtil.isConstant(secondChild)) { // is literal

        ImmutableBytesWritable ptr = new ImmutableBytesPtr();
        secondChild.evaluate(null, ptr);

        if (ptr.getLength()!=0 && !secondChild.getDataType().isCoercibleTo(firstChild.getDataType(), secondChild.getDataType().toObject(ptr))) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH)
                .setMessage(getName() + " expected " + firstChild.getDataType() + ", but got " + secondChild.getDataType())
                .build().buildException();
        }
    } else { // second parameter is expression
        if (!secondChild.getDataType().isCoercibleTo(getDataType())) {
            // cast explicitly
            children.add(1, CoerceExpression.create(secondChild, firstChild.getDataType()));
        }
    }
}
 
Example 11
Source File: InvertFunction.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!getChildExpression().evaluate(tuple, ptr)) { return false; }
    if (ptr.getLength() == 0) { return true; }
    byte[] buf = new byte[ptr.getLength()];
    ColumnModifier.SORT_DESC.apply(ptr.get(), ptr.getOffset(), buf, 0, ptr.getLength());
    ptr.set(buf);
    return true;
}
 
Example 12
Source File: PTableImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(PColumn column, byte[] byteValue) {
    deleteRow = null;
    byte[] family = column.getFamilyName().getBytes();
    byte[] qualifier = column.getName().getBytes();
    PDataType type = column.getDataType();
    // Check null, since some types have no byte representation for null
    boolean isNull = type.isNull(byteValue);
    if (isNull && !getStoreNulls()) {
        if (!column.isNullable()) {
            throw new ConstraintViolationException(name.getString() + "." + column.getName().getString() + " may not be null");
        }
        removeIfPresent(setValues, family, qualifier);
        deleteQuietly(unsetValues, kvBuilder, kvBuilder.buildDeleteColumns(keyPtr, column
                    .getFamilyName().getBytesPtr(), column.getName().getBytesPtr(), ts));
    } else {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable(byteValue == null ?
                HConstants.EMPTY_BYTE_ARRAY : byteValue);
        Integer	maxLength = column.getMaxLength();
    	if (!isNull && type.isFixedWidth() && maxLength != null) {
if (ptr.getLength() <= maxLength) {
                type.pad(ptr, maxLength);
            } else if (ptr.getLength() > maxLength) {
                throw new ConstraintViolationException(name.getString() + "." + column.getName().getString() + " may not exceed " + maxLength + " bytes (" + type.toObject(byteValue) + ")");
            }
    	}
        removeIfPresent(unsetValues, family, qualifier);
        addQuietly(setValues, kvBuilder, kvBuilder.buildPut(keyPtr,
                column.getFamilyName().getBytesPtr(), column.getName().getBytesPtr(),
                ts, ptr));
    }
}
 
Example 13
Source File: ByteUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Expand the key to length bytes using the fillByte to fill the
 * bytes beyond the current key length.
 */
public static void nullPad(ImmutableBytesWritable ptr, int length) {
    if(ptr.getLength() > length) {
        throw new IllegalStateException();
    }
    if (ptr.getLength() == length) {
        return;
    }
    byte[] newBound = new byte[length];
    System.arraycopy(ptr.get(), ptr.getOffset(), newBound, 0, ptr.getLength());
    ptr.set(newBound);
}
 
Example 14
Source File: DateAddExpression.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    long finalResult=0;
    
    for(int i=0;i<children.size();i++) {
        if (!children.get(i).evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        long value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PDecimal.INSTANCE) {
            BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, sortOrder);
            value = bd.multiply(BD_MILLIS_IN_DAY).longValue();
        } else if (type.isCoercibleTo(PLong.INSTANCE)) {
            value = type.getCodec().decodeLong(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY;
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = (long)(type.getCodec().decodeDouble(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY);
        } else {
            value = type.getCodec().decodeLong(ptr, sortOrder);
        }
        finalResult += value;
    }
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    getDataType().getCodec().encodeLong(finalResult, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
 
Example 15
Source File: SaltingUtil.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static byte[] getSaltedKey(ImmutableBytesWritable key, int bucketNum) {
    byte[] keyBytes = new byte[key.getLength()];
    byte saltByte = getSaltingByte(key.get(), key.getOffset() + 1, key.getLength() - 1, bucketNum);
    keyBytes[0] = saltByte;
    System.arraycopy(key.get(), key.getOffset() + 1, keyBytes, 1, key.getLength() - 1);
    return keyBytes;
}
 
Example 16
Source File: PDate.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void coerceBytes(ImmutableBytesWritable ptr, Object object, PDataType actualType,
        Integer maxLength, Integer scale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale,
        SortOrder expectedModifier) {
    // Decrease size of TIMESTAMP to size of DATE and continue coerce
    if (ptr.getLength() > getByteSize()) {
        ptr.set(ptr.get(), ptr.getOffset(), getByteSize());
    }
    super.coerceBytes(ptr, object, actualType, maxLength, scale, actualModifier, desiredMaxLength,
            desiredScale, expectedModifier);
}
 
Example 17
Source File: PArrayDataType.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int getArrayLength(ImmutableBytesWritable ptr,
		PDataType baseType) {
	byte[] bytes = ptr.get();
	if(baseType.isFixedWidth()) {
		return ((ptr.getLength() - (Bytes.SIZEOF_BYTE + Bytes.SIZEOF_INT))/baseType.getByteSize());
	}
	return Bytes.toInt(bytes, ptr.getOffset() + Bytes.SIZEOF_BYTE);
}
 
Example 18
Source File: NotExpression.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Expression create(Expression child, ImmutableBytesWritable ptr) throws SQLException {
    if (child.getDataType() != PBoolean.INSTANCE) {
        throw TypeMismatchException.newException(child.getDataType(), PBoolean.INSTANCE, "NOT");
    }
    if (child.isStateless()) {
        if (!child.evaluate(null, ptr) || ptr.getLength() == 0) {
            return LiteralExpression.newConstant(null, PBoolean.INSTANCE, child.getDeterminism());
        }
        return LiteralExpression.newConstant(!(Boolean) PBoolean.INSTANCE.toObject(ptr), PBoolean.INSTANCE, child.getDeterminism());
    }
    return new NotExpression(child);
}
 
Example 19
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static int getArrayLength(ImmutableBytesWritable ptr, PDataType baseType, Integer maxLength) {
      byte[] bytes = ptr.get();
      if (ptr.getLength() == 0) {
          return 0;
      }
      if (baseType.isFixedWidth()) {
          int elemLength = maxLength == null ? baseType.getByteSize() : maxLength;
          return (ptr.getLength() / elemLength);
      }
      // In case where the number of elements is greater than SHORT.MAX_VALUE we do negate the number of
      // elements. So it is always better to return the absolute value
return (Bytes.toInt(bytes, (ptr.getOffset() + ptr.getLength() - (Bytes.SIZEOF_BYTE + Bytes.SIZEOF_INT))));
  }
 
Example 20
Source File: KeyValueSchema.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * @return byte representation of the KeyValueSchema
 */
public byte[] toBytes(Tuple tuple, Expression[] expressions, ValueBitSet valueSet, ImmutableBytesWritable ptr) {
    int offset = 0;
    int index = 0;
    valueSet.clear();
    int minNullableIndex = getMinNullable();
    byte[] b = new byte[getEstimatedValueLength() + valueSet.getEstimatedLength()];
    List<Field> fields = getFields();
    // We can get away with checking if only nulls are left in the outer loop,
    // since repeating fields will not span the non-null/null boundary.
    for (int i = 0; i < fields.size(); i++) {
        Field field = fields.get(i);
        PDataType type = field.getDataType();
        for (int j = 0; j < field.getCount(); j++) {
            if (expressions[index].evaluate(tuple, ptr) && ptr.getLength() > 0) { // Skip null values
                if (index >= minNullableIndex) {
                    valueSet.set(index - minNullableIndex);
                }
                if (!type.isFixedWidth()) {
                    b = ensureSize(b, offset, offset + getVarLengthBytes(ptr.getLength()));
                    offset = writeVarLengthField(ptr, b, offset);
                } else {
                    int nBytes = ptr.getLength();
                    b = ensureSize(b, offset, offset + nBytes);
                    System.arraycopy(ptr.get(), ptr.getOffset(), b, offset, nBytes);
                    offset += nBytes;
                }
            }
            index++;
        }
    }
    // Add information about which values were set at end of value,
    // so that we can quickly access them without needing to walk
    // through the values using the schema.
    // TODO: if there aren't any non null values, don't serialize anything
    b = ensureSize(b, offset, offset + valueSet.getEstimatedLength());
    offset = valueSet.toBytes(b, offset);

    if (offset == b.length) {
        return b;
    } else {
        byte[] bExact = new byte[offset];
        System.arraycopy(b, 0, bExact, 0, offset);
        return bExact;
    }
}