Java Code Examples for org.apache.hadoop.hbase.util.Bytes#SIZEOF_BYTE

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#SIZEOF_BYTE . These examples are extracted from open source projects. 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 Project: canal-1.1.3   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedByte(byte v, byte[] b, int o) {
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putByte(b, o, v);
    return Bytes.SIZEOF_BYTE;
}
 
Example 2
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encodeByte(byte v, byte[] b, int o) {
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putByte(b, o, v);
  return Bytes.SIZEOF_BYTE;
}
 
Example 3
Source Project: hbase   File: CodecPerformance.java    License: Apache License 2.0 5 votes vote down vote up
static int getRoughSize(final Cell [] cells) {
  int size = 0;
  for (Cell c: cells) {
    size += c.getRowLength() + c.getFamilyLength() + c.getQualifierLength() + c.getValueLength();
    size += Bytes.SIZEOF_LONG + Bytes.SIZEOF_BYTE;
  }
  return size;
}
 
Example 4
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 5
Source Project: canal   File: PhTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedByte(byte v, byte[] b, int o) {
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putByte(b, o, v);
    return Bytes.SIZEOF_BYTE;
}
 
Example 6
Source Project: hbase   File: PrivateCellUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static int writeFlatKey(Cell cell, OutputStream out) throws IOException {
  short rowLen = cell.getRowLength();
  byte fLen = cell.getFamilyLength();
  int qLen = cell.getQualifierLength();
  // Using just one if/else loop instead of every time checking before writing every
  // component of cell
  if (cell instanceof ByteBufferExtendedCell) {
    StreamUtils.writeShort(out, rowLen);
    ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getRowByteBuffer(),
      ((ByteBufferExtendedCell) cell).getRowPosition(), rowLen);
    out.write(fLen);
    ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getFamilyByteBuffer(),
      ((ByteBufferExtendedCell) cell).getFamilyPosition(), fLen);
    ByteBufferUtils
      .copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(),
        ((ByteBufferExtendedCell) cell).getQualifierPosition(), qLen);
  } else {
    StreamUtils.writeShort(out, rowLen);
    out.write(cell.getRowArray(), cell.getRowOffset(), rowLen);
    out.write(fLen);
    out.write(cell.getFamilyArray(), cell.getFamilyOffset(), fLen);
    out.write(cell.getQualifierArray(), cell.getQualifierOffset(), qLen);
  }
  StreamUtils.writeLong(out, cell.getTimestamp());
  out.write(cell.getTypeByte());
  return Bytes.SIZEOF_SHORT + rowLen + Bytes.SIZEOF_BYTE + fLen + qLen + Bytes.SIZEOF_LONG
    + Bytes.SIZEOF_BYTE;
}
 
Example 7
Source Project: phoenix   File: PArrayDataType.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isRowKeyOrderOptimized(boolean isFixedWidth, SortOrder sortOrder, byte[] buf, int offset, int length) {
    if (length == 0 || sortOrder == SortOrder.ASC || isFixedWidth) {
        return true;
    }
    int offsetToHeaderOffset = offset + length - Bytes.SIZEOF_BYTE - Bytes.SIZEOF_INT * 2;
    int offsetToSeparatorByte = Bytes.readAsInt(buf, offsetToHeaderOffset, Bytes.SIZEOF_INT) - 1;
    return buf[offsetToSeparatorByte] == QueryConstants.DESC_SEPARATOR_BYTE;
}
 
Example 8
Source Project: canal-1.1.3   File: PhTypeUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static byte[] toBytes(Object v, PhType phType) {
    if (v == null) return null;
    byte[] b = null;
    if (phType == PhType.DEFAULT) {
        PhType phType1 = PhType.getType(v.getClass());
        if (phType1 != null && phType1 != PhType.DEFAULT) {
            toBytes(v, phType1);
        }
    } else if (phType == PhType.INTEGER) {
        b = new byte[Bytes.SIZEOF_INT];
        encodeInt(((Number) v).intValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_INT) {
        b = new byte[Bytes.SIZEOF_INT];
        encodeUnsignedInt(((Number) v).intValue(), b, 0);
    } else if (phType == PhType.BIGINT) {
        b = new byte[Bytes.SIZEOF_LONG];
        encodeLong(((Number) v).longValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_LONG) {
        b = new byte[Bytes.SIZEOF_LONG];
        encodeUnsignedLong(((Number) v).longValue(), b, 0);
    } else if (phType == PhType.SMALLINT) {
        b = new byte[Bytes.SIZEOF_SHORT];
        encodeShort(((Number) v).shortValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_SMALLINT) {
        b = new byte[Bytes.SIZEOF_SHORT];
        encodeUnsignedShort(((Number) v).shortValue(), b, 0);
    } else if (phType == PhType.TINYINT) {
        b = new byte[Bytes.SIZEOF_BYTE];
        encodeByte(((Number) v).byteValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_TINYINT) {
        b = new byte[Bytes.SIZEOF_BYTE];
        encodeUnsignedByte(((Number) v).byteValue(), b, 0);
    } else if (phType == PhType.FLOAT) {
        b = new byte[Bytes.SIZEOF_FLOAT];
        encodeFloat(((Number) v).floatValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_FLOAT) {
        b = new byte[Bytes.SIZEOF_FLOAT];
        encodeUnsignedFloat(((Number) v).floatValue(), b, 0);
    } else if (phType == PhType.DOUBLE) {
        b = new byte[Bytes.SIZEOF_DOUBLE];
        encodeDouble(((Number) v).doubleValue(), b, 0);
    } else if (phType == PhType.UNSIGNED_DOUBLE) {
        b = new byte[Bytes.SIZEOF_DOUBLE];
        encodeUnsignedDouble(((Number) v).doubleValue(), b, 0);
    } else if (phType == PhType.BOOLEAN) {
        if ((Boolean) v) {
            b = new byte[] { 1 };
        } else {
            b = new byte[] { 0 };
        }
    } else if (phType == PhType.TIME || phType == PhType.DATE) {
        b = new byte[Bytes.SIZEOF_LONG];
        encodeDate(v, b, 0);
    } else if (phType == PhType.TIMESTAMP) {
        b = new byte[Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT];
        encodeTimestamp(v, b, 0);
    } else if (phType == PhType.UNSIGNED_TIME || phType == PhType.UNSIGNED_DATE) {
        b = new byte[Bytes.SIZEOF_LONG];
        encodeUnsignedDate(v, b, 0);
    } else if (phType == PhType.UNSIGNED_TIMESTAMP) {
        b = new byte[Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT];
        encodeUnsignedTimestamp(v, b, 0);
    } else if (phType == PhType.VARBINARY) {
        b = (byte[]) v;
    } else if (phType == PhType.VARCHAR) {
        b = Bytes.toBytes(v.toString());
    } else if (phType == PhType.DECIMAL) {
        if (v instanceof BigDecimal) {
            b = encodeDecimal(v);
        } else if (v instanceof Number) {
            b = encodeDecimal(new BigDecimal(v.toString()));
        }
    }
    return b;
}
 
Example 9
Source Project: phoenix   File: PTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int encodeByte(byte v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE);
  b[o] = (byte) (v ^ 0x80); // Flip sign bit so that Short is binary comparable
  return Bytes.SIZEOF_BYTE;
}
 
Example 10
public static void positionAtArrayElement(ImmutableBytesWritable ptr, int arrayIndex, PDataType baseDataType) {
	byte[] bytes = ptr.get();
	int initPos = ptr.getOffset();
	int noOfElements = 0;
	noOfElements = Bytes.toInt(bytes, ptr.getOffset() + Bytes.SIZEOF_BYTE, Bytes.SIZEOF_INT);
	int noOFElementsSize = Bytes.SIZEOF_INT;
	if(arrayIndex >= noOfElements) {
		throw new IndexOutOfBoundsException(
				"Invalid index "
						+ arrayIndex
						+ " specified, greater than the no of elements in the array: "
						+ noOfElements);
	}
	boolean useShort = true;
	int baseSize = Bytes.SIZEOF_SHORT;
	if (noOfElements < 0) {
		noOfElements = -noOfElements;
		baseSize = Bytes.SIZEOF_INT;
		useShort = false;
	}

	if (baseDataType.getByteSize() == null) {
		int offset = ptr.getOffset() + noOFElementsSize + Bytes.SIZEOF_BYTE;
		int indexOffset = Bytes.toInt(bytes, offset) + ptr.getOffset();
		int valArrayPostion = offset + Bytes.SIZEOF_INT;
		offset += Bytes.SIZEOF_INT;
		int currOff = 0;
		if (noOfElements > 1) {
			while (offset <= (initPos+ptr.getLength())) {
				int nextOff = 0;
				// Skip those many offsets as given in the arrayIndex
				// If suppose there are 5 elements in the array and the arrayIndex = 3
				// This means we need to read the 4th element of the array
				// So inorder to know the length of the 4th element we will read the offset of 4th element and the offset of 5th element.
				// Subtracting the offset of 5th element and 4th element will give the length of 4th element
				// So we could just skip reading the other elements.
				if(useShort) {
					// If the arrayIndex is already the last element then read the last before one element and the last element
					offset = indexOffset + (Bytes.SIZEOF_SHORT * arrayIndex);
					if (arrayIndex == (noOfElements - 1)) {
						currOff = Bytes.toShort(bytes, offset, baseSize) + Short.MAX_VALUE;
						nextOff = indexOffset;
						offset += baseSize;
					} else {
						currOff = Bytes.toShort(bytes, offset, baseSize) + Short.MAX_VALUE;
						offset += baseSize;
						nextOff = Bytes.toShort(bytes, offset, baseSize) + Short.MAX_VALUE;
						offset += baseSize;
					}
				} else {
					// If the arrayIndex is already the last element then read the last before one element and the last element
					offset = indexOffset + (Bytes.SIZEOF_INT * arrayIndex);
					if (arrayIndex == (noOfElements - 1)) {
						currOff = Bytes.toInt(bytes, offset, baseSize);
						nextOff = indexOffset;
						offset += baseSize;
					} else {
						currOff = Bytes.toInt(bytes, offset, baseSize);
						offset += baseSize;
						nextOff = Bytes.toInt(bytes, offset, baseSize);
						offset += baseSize;
					}
				}
				int elementLength = nextOff - currOff;
				ptr.set(bytes, currOff + initPos, elementLength);
				break;
			}
		} else {
			ptr.set(bytes, valArrayPostion + initPos, indexOffset - valArrayPostion);
		}
	} else {
		ptr.set(bytes,
				ptr.getOffset() + arrayIndex * baseDataType.getByteSize()
						+ noOFElementsSize + Bytes.SIZEOF_BYTE, baseDataType.getByteSize());
	}
}
 
Example 11
Source Project: phoenix   File: PTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Integer getByteSize() {
  return Bytes.SIZEOF_BYTE;
}
 
Example 12
Source Project: phoenix   File: PTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toBytes(Object object) {
  byte[] b = new byte[Bytes.SIZEOF_BYTE];
  toBytes(object, b, 0);
  return b;
}
 
Example 13
Source Project: phoenix   File: PTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int encodeByte(byte v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE);
  b[o] = (byte) (v ^ 0x80); // Flip sign bit so that Short is binary comparable
  return Bytes.SIZEOF_BYTE;
}
 
Example 14
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Integer getByteSize() {
  return Bytes.SIZEOF_BYTE;
}
 
Example 15
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Integer getByteSize() {
  return Bytes.SIZEOF_BYTE;
}
 
Example 16
Source Project: hbase   File: RawByte.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int encodedLength(Byte val) {
  return Bytes.SIZEOF_BYTE;
}
 
Example 17
Source Project: phoenix   File: PUnsignedTinyint.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toBytes(Object object) {
  byte[] b = new byte[Bytes.SIZEOF_BYTE];
  toBytes(object, b, 0);
  return b;
}
 
Example 18
Source Project: hbase   File: ByteBufferKeyValue.java    License: Apache License 2.0 4 votes vote down vote up
public int getFamilyPosition(int familyLengthPosition) {
  return familyLengthPosition + Bytes.SIZEOF_BYTE;
}
 
Example 19
Source Project: phoenix   File: PArrayDataTypeDecoder.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean positionAtArrayElement(ImmutableBytesWritable ptr, int arrayIndex, PDataType baseDataType,
        Integer byteSize) {
    byte[] bytes = ptr.get();
    int initPos = ptr.getOffset();
    if (!baseDataType.isFixedWidth()) {
    	byte serializationVersion = bytes[ptr.getOffset() + ptr.getLength() - Bytes.SIZEOF_BYTE];
        int noOfElements = Bytes.toInt(bytes,
                (ptr.getOffset() + ptr.getLength() - (Bytes.SIZEOF_BYTE + Bytes.SIZEOF_INT)), Bytes.SIZEOF_INT);
        boolean useShort = true;
        if (noOfElements < 0) {
            noOfElements = -noOfElements;
            useShort = false;
        }
        if (arrayIndex >= noOfElements) {
            ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
            return false;
        }

        int indexOffset = Bytes.toInt(bytes,
                (ptr.getOffset() + ptr.getLength() - (Bytes.SIZEOF_BYTE + 2 * Bytes.SIZEOF_INT))) + ptr.getOffset();
        // Skip those many offsets as given in the arrayIndex
        // If suppose there are 5 elements in the array and the arrayIndex = 3
        // This means we need to read the 4th element of the array
        // So inorder to know the length of the 4th element we will read the offset of 4th element and the
        // offset of 5th element.
        // Subtracting the offset of 5th element and 4th element will give the length of 4th element
        // So we could just skip reading the other elements.
        int currOffset = PArrayDataType.getSerializedOffset(bytes, arrayIndex, useShort, indexOffset, serializationVersion);
        if (currOffset<0) {
        	ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
            return false;
        }
        int elementLength = 0;
        if (arrayIndex == (noOfElements - 1)) {
            // in the original IMMUTABLE_SERIALIZATION_VERSION (v1), for nulls we store
            // (separatorByte, #_of_nulls) in the data. Because of the separatorByte, we can't
            // distinguish between nulls and actual data values that start with the separator
            // byte. We do a hack here to limit the damage by checking offsets - if the prior
            // offset had a length of 0, then we know we're storing 2 or more nulls. However, we
            // still can't fix the case distinguishing a single null from a short value. There
            // are two kinds of separatorByte, so the results will be potentially incorrect for
            // 2 short values that correspond to (separatorByte, 1)
            if (serializationVersion == PArrayDataType.IMMUTABLE_SERIALIZATION_VERSION) {
                elementLength = indexOffset - (currOffset + initPos);
                if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) {
                    elementLength = 0;
                }
            } else {
                int separatorBytes =  serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 3 : 0;
                elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0 : indexOffset
                        - (currOffset + initPos) - separatorBytes;
            }
        } else {
            if (serializationVersion == PArrayDataType.IMMUTABLE_SERIALIZATION_VERSION) {
                elementLength = PArrayDataType.getOffset(bytes, arrayIndex + 1,
                    useShort, indexOffset, serializationVersion)
                        - currOffset;
                if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) {
                    elementLength = 0;
                }
            } else {
                int separatorByte =
                        serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION
                                ? 1
                                : 0;
                elementLength =
                        isSeparatorByte(bytes, initPos, currOffset)
                                && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION
                                        ? 0
                                        : PArrayDataType.getOffset(bytes, arrayIndex + 1,
                                            useShort, indexOffset, serializationVersion)
                                                - currOffset - separatorByte;
            }
        }
        ptr.set(bytes, currOffset + initPos, elementLength);
    } else {
        int elemByteSize = (byteSize == null ? baseDataType.getByteSize() : byteSize);
        int offset = arrayIndex * elemByteSize;
        if (offset >= ptr.getLength()) {
            ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        } else {
            ptr.set(bytes, ptr.getOffset() + offset, elemByteSize);
        }
    }
    return true;
}
 
Example 20
Source Project: hbase   File: DiffKeyDeltaEncoder.java    License: Apache License 2.0 4 votes vote down vote up
private void decode(boolean isFirst) {
  byte flag = currentBuffer.get();
  byte type = 0;
  if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
    if (!isFirst) {
      type = current.keyBuffer[current.keyLength - Bytes.SIZEOF_BYTE];
    }
    current.keyLength = ByteBuff.readCompressedInt(currentBuffer);
  }
  if ((flag & FLAG_SAME_VALUE_LENGTH) == 0) {
    current.valueLength = ByteBuff.readCompressedInt(currentBuffer);
  }
  current.lastCommonPrefix = ByteBuff.readCompressedInt(currentBuffer);

  current.ensureSpaceForKey();

  if (current.lastCommonPrefix < Bytes.SIZEOF_SHORT) {
    // length of row is different, copy everything except family

    // copy the row size
    currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
        Bytes.SIZEOF_SHORT - current.lastCommonPrefix);
    current.rowLengthWithSize = Bytes.toShort(current.keyBuffer, 0) +
        Bytes.SIZEOF_SHORT;

    // copy the rest of row
    currentBuffer.get(current.keyBuffer, Bytes.SIZEOF_SHORT,
        current.rowLengthWithSize - Bytes.SIZEOF_SHORT);

    // copy the column family
    System.arraycopy(familyNameWithSize, 0, current.keyBuffer,
        current.rowLengthWithSize, familyNameWithSize.length);

    // copy the qualifier
    currentBuffer.get(current.keyBuffer,
        current.rowLengthWithSize + familyNameWithSize.length,
        current.keyLength - current.rowLengthWithSize -
        familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
  } else if (current.lastCommonPrefix < current.rowLengthWithSize) {
    // we have to copy part of row and qualifier,
    // but column family is in right place

    // before column family (rest of row)
    currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
        current.rowLengthWithSize - current.lastCommonPrefix);

    // after column family (qualifier)
    currentBuffer.get(current.keyBuffer,
        current.rowLengthWithSize + familyNameWithSize.length,
        current.keyLength - current.rowLengthWithSize -
        familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
  } else {
    // copy just the ending
    currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
        current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH -
        current.lastCommonPrefix);
  }

  // timestamp
  int pos = current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH;
  int timestampFitInBytes = 1 +
      ((flag & MASK_TIMESTAMP_LENGTH) >>> SHIFT_TIMESTAMP_LENGTH);
  long timestampOrDiff = ByteBuff.readLong(currentBuffer, timestampFitInBytes);
  if ((flag & FLAG_TIMESTAMP_SIGN) != 0) {
    timestampOrDiff = -timestampOrDiff;
  }
  if ((flag & FLAG_TIMESTAMP_IS_DIFF) == 0) { // it is timestamp
    current.timestamp = timestampOrDiff;
  } else { // it is diff
    current.timestamp = current.timestamp - timestampOrDiff;
  }
  Bytes.putLong(current.keyBuffer, pos, current.timestamp);
  pos += Bytes.SIZEOF_LONG;

  // type
  if ((flag & FLAG_SAME_TYPE) == 0) {
    currentBuffer.get(current.keyBuffer, pos, Bytes.SIZEOF_BYTE);
  } else if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
    current.keyBuffer[pos] = type;
  }

  current.valueOffset = currentBuffer.position();
  currentBuffer.skip(current.valueLength);

  if (includesTags()) {
    decodeTags();
  }
  if (includesMvcc()) {
    current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
  } else {
    current.memstoreTS = 0;
  }
  current.nextKvOffset = currentBuffer.position();
}