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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#SIZEOF_SHORT . 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: ValueBitSet.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void or(ImmutableBytesWritable ptr) {
    if (schema == null) {
        return;
    }
    if (isVarLength()) {
        int offset = ptr.getOffset() + ptr.getLength() - Bytes.SIZEOF_SHORT;
        short nLongs = Bytes.toShort(ptr.get(), offset);
        offset -= nLongs * Bytes.SIZEOF_LONG;
        for (int i = 0; i < nLongs; i++) {
            bits[i] |= Bytes.toLong(ptr.get(), offset);
            offset += Bytes.SIZEOF_LONG;
        }
        maxSetBit = Math.max(maxSetBit, nLongs * Bytes.SIZEOF_LONG - 1);
    } else {
        long l = Bytes.toShort(ptr.get(), ptr.getOffset() + ptr.getLength() - Bytes.SIZEOF_SHORT);
        bits[0] |= l;
        maxSetBit = Math.max(maxSetBit, BITS_PER_SHORT - 1);
    }
    
}
 
Example 2
Source File: PhTypeUtil.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private static short decodeShort(byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
    int v;
    v = b[o] ^ 0x80; // Flip sign bit back
    for (int i = 1; i < Bytes.SIZEOF_SHORT; i++) {
        v = (v << 8) + (b[o + i] & 0xff);
    }
    return (short) v;
}
 
Example 3
Source File: ValueBitSet.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * @return Max serialization size
 */
public int getEstimatedLength() {
    if (schema == null) {
        return 0;
    }
    return Bytes.SIZEOF_SHORT + (isVarLength() ? (maxSetBit + BITS_PER_LONG) / BITS_PER_LONG * Bytes.SIZEOF_LONG : 0);
}
 
Example 4
Source File: ServerName.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Use this method instantiating a {@link ServerName} from bytes
 * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
 * case where bytes were written by an earlier version of hbase.
 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
 * @return A ServerName instance.
 * @see #getVersionedBytes()
 */
public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
  // Version is a short.
  short version = Bytes.toShort(versionedBytes);
  if (version == VERSION) {
    int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
    return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
  }
  // Presume the bytes were written with an old version of hbase and that the
  // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
  return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
}
 
Example 5
Source File: ValueBitSet.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @return Max serialization size
 */
public int getEstimatedLength() {
    if (schema == null) {
        return 0;
    }
    return Bytes.SIZEOF_SHORT + (isVarLength() ? (maxSetBit + BITS_PER_LONG - 1) / BITS_PER_LONG * Bytes.SIZEOF_LONG : 0);
}
 
Example 6
Source File: HFileReaderImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
private int getKVBufSize() {
  int kvBufSize = KEY_VALUE_LEN_SIZE + currKeyLen + currValueLen;
  if (currTagsLen > 0) {
    kvBufSize += Bytes.SIZEOF_SHORT + currTagsLen;
  }
  return kvBufSize;
}
 
Example 7
Source File: PUnsignedSmallint.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toBytes(Object object) {
  if (object == null) {
    throw newIllegalDataException(this + " may not be null");
  }
  byte[] b = new byte[Bytes.SIZEOF_SHORT];
  toBytes(object, b, 0);
  return b;
}
 
Example 8
Source File: PSmallint.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int encodeShort(short v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
    b[o + 0] = (byte) ((v >> 8) ^ 0x80); // Flip sign bit so that Short is binary comparable
    b[o + 1] = (byte) v;
    return Bytes.SIZEOF_SHORT;
}
 
Example 9
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void writeNewOffsets(byte[] arrayBytes, byte[] newArray, boolean useShortNew,
        boolean useShortPrevious, int newOffsetArrayPosition, int arrayLength, int offsetArrayPosition, int offset,
        int offsetShift, int length) {
    int currentPosition = newOffsetArrayPosition;
    int offsetArrayElementSize = useShortNew ? Bytes.SIZEOF_SHORT : Bytes.SIZEOF_INT;
    if (useShortNew) {
        Bytes.putShort(newArray, currentPosition, (short)(0 - Short.MAX_VALUE));
    } else {
        Bytes.putInt(newArray, currentPosition, 0);
    }

    currentPosition += offsetArrayElementSize;
    boolean nullsAtBeginning = true;
    byte serializationVersion = arrayBytes[offset + length - Bytes.SIZEOF_BYTE];
    for (int arrayIndex = 0; arrayIndex < arrayLength - 1; arrayIndex++) {
        int oldOffset = getOffset(arrayBytes, arrayIndex, useShortPrevious, offsetArrayPosition + offset, serializationVersion);
        if (arrayBytes[offset + oldOffset] == QueryConstants.SEPARATOR_BYTE && nullsAtBeginning) {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset);
            }
        } else {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset + offsetShift - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset + offsetShift);
            }
            nullsAtBeginning = false;
        }
        currentPosition += offsetArrayElementSize;
    }

    Bytes.putInt(newArray, currentPosition, newOffsetArrayPosition);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putInt(newArray, currentPosition, useShortNew ? arrayLength : -arrayLength);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putByte(newArray, currentPosition, arrayBytes[offset + length - 1]);
}
 
Example 10
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static int getOffset(byte[] bytes, int arrayIndex, boolean useShort, int indexOffset) {
    int offset;
    if (useShort) {
        offset = indexOffset + (Bytes.SIZEOF_SHORT * arrayIndex);
        return Bytes.toShort(bytes, offset, Bytes.SIZEOF_SHORT) + Short.MAX_VALUE;
    } else {
        offset = indexOffset + (Bytes.SIZEOF_INT * arrayIndex);
        return Bytes.toInt(bytes, offset, Bytes.SIZEOF_INT);
    }
}
 
Example 11
Source File: DynamicPositionedMutableByteRange.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public PositionedByteRange putShort(short val) {
    ensureCapacity(position + Bytes.SIZEOF_SHORT);
    putShort(position, val);
    position += Bytes.SIZEOF_SHORT;
    return this;
}
 
Example 12
Source File: PhTypeUtil.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private static int encodeShort(short v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
    b[o + 0] = (byte) ((v >> 8) ^ 0x80); // Flip sign bit so that Short is
                                         // binary comparable
    b[o + 1] = (byte) v;
    return Bytes.SIZEOF_SHORT;
}
 
Example 13
Source File: PArrayDataType.java    From phoenix with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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 14
Source File: ByteBufferKeyOnlyKeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
private int getFamilyLengthPosition() {
  return this.offset + Bytes.SIZEOF_SHORT + getRowLength();
}
 
Example 15
Source File: BufferedDataBlockEncoder.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int getRowPosition() {
  return Bytes.SIZEOF_SHORT;
}
 
Example 16
Source File: PUnsignedSmallint.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public Integer getByteSize() {
  return Bytes.SIZEOF_SHORT;
}
 
Example 17
Source File: KeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int getRowOffset() {
  return getKeyOffset() + Bytes.SIZEOF_SHORT;
}
 
Example 18
Source File: PSmallint.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toBytes(Object object) {
  byte[] b = new byte[Bytes.SIZEOF_SHORT];
  toBytes(object, b, 0);
  return b;
}
 
Example 19
Source File: PhTypeUtil.java    From canal-1.1.3 with 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 20
Source File: ResultUtil.java    From phoenix with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Get the offset into the Result byte array to the key.
 * @param r
 * @return
 */
static int getKeyOffset(Result r) {
    // Special case for when Result was instantiated via KeyValue array (no bytes in that case) versus returned from a scanner
    return (r.getBytes() == null ? r.raw()[0].getOffset() : (r.getBytes().getOffset() + Bytes.SIZEOF_INT /* KV length in Result */)) + KeyValue.ROW_OFFSET /* key offset in KV */ + Bytes.SIZEOF_SHORT /* key length */;
}