public static int getNanos(byte[] bytes, int offset) { int val = (int) LazyBinaryUtils.readVLongFromByteArray(bytes, offset); if (val < 0) { val = -val - 1; } int len = (int) Math.floor(Math.log10(val)) + 1; // Reverse the value int tmp = 0; while (val != 0) { tmp *= 10; tmp += val % 10; val /= 10; } val = tmp; if (len < 9) { val *= Math.pow(10, 9 - len); } return val; }
@Override public void readFields(DataInput dataInput) throws IOException { dataInput.readFully(internalBytes, 0, 4); if (hasDecimalOrSecondVInt(internalBytes[0])) { dataInput.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { dataInput.readFully(internalBytes, 5, len - 1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); Preconditions.checkState(vlong >= -1000000000 && vlong <= 999999999, "Invalid nanos value for a TimestampTZWritable: " + vlong + ", expected to be between -1000000000 and 999999999."); if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. dataInput.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { dataInput.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; offset = 0; timestampTZEmpty = true; bytesEmpty = false; }
public void readFields(DataInput in) throws IOException { in.readFully(internalBytes, 0, 4); if (TimestampWritableV2.hasDecimalOrSecondVInt(internalBytes[0])) { in.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { in.readFully(internalBytes, 5, len-1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); if (vlong < -1000000000 || vlong > 999999999) { throw new IOException( "Invalid first vint value (encoded nanoseconds) of a TimestampWritableV2: " + vlong + ", expected to be between -1000000000 and 999999999."); // Note that -1000000000 is a valid value corresponding to a nanosecond timestamp // of 999999999, because if the second VInt is present, we use the value // (-reversedNanoseconds - 1) as the second VInt. } if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. in.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { in.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; this.offset = 0; }
public void readFields(DataInput in) throws IOException { in.readFully(internalBytes, 0, 4); if (TimestampWritable.hasDecimalOrSecondVInt(internalBytes[0])) { in.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { in.readFully(internalBytes, 5, len-1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); if (vlong < -1000000000 || vlong > 999999999) { throw new IOException( "Invalid first vint value (encoded nanoseconds) of a TimestampWritable: " + vlong + ", expected to be between -1000000000 and 999999999."); // Note that -1000000000 is a valid value corresponding to a nanosecond timestamp // of 999999999, because if the second VInt is present, we use the value // (-reversedNanoseconds - 1) as the second VInt. } if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. in.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { in.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; this.offset = 0; }
public static long getSeconds(byte[] bytes, int offset) { int firstVInt = bytesToInt(bytes, offset); if (firstVInt >= 0 || !hasSecondVInt(bytes[offset + 4])) { return firstVInt & ~DECIMAL_OR_SECOND_VINT_FLAG; } return ((long) (firstVInt & ~DECIMAL_OR_SECOND_VINT_FLAG)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
public static int getNanos(byte[] bytes, int offset) { int val = (int) LazyBinaryUtils.readVLongFromByteArray(bytes, offset); if (val < 0) { val = -val - 1; } int len = (int) Math.floor(Math.log10(val)) + 1; // Reverse the value int tmp = 0; while (val != 0) { tmp *= 10; tmp += val % 10; val /= 10; } val = tmp; if (len < 9) { val *= Math.pow(10, 9 - len); } return val; }
@Override public void readFields(DataInput dataInput) throws IOException { dataInput.readFully(internalBytes, 0, 4); if (hasDecimalOrSecondVInt(internalBytes[0])) { dataInput.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { dataInput.readFully(internalBytes, 5, len - 1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); Preconditions.checkState(vlong >= -1000000000 && vlong <= 999999999, "Invalid nanos value for a TimestampTZWritable: " + vlong + ", expected to be between -1000000000 and 999999999."); if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. dataInput.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { dataInput.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; offset = 0; timestampTZEmpty = true; bytesEmpty = false; }
public void readFields(DataInput in) throws IOException { in.readFully(internalBytes, 0, 4); if (TimestampWritable.hasDecimalOrSecondVInt(internalBytes[0])) { in.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { in.readFully(internalBytes, 5, len-1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); if (vlong < -1000000000 || vlong > 999999999) { throw new IOException( "Invalid first vint value (encoded nanoseconds) of a TimestampWritable: " + vlong + ", expected to be between -1000000000 and 999999999."); // Note that -1000000000 is a valid value corresponding to a nanosecond timestamp // of 999999999, because if the second VInt is present, we use the value // (-reversedNanoseconds - 1) as the second VInt. } if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. in.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { in.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; this.offset = 0; }
public void readFields(DataInput in) throws IOException { in.readFully(internalBytes, 0, 4); if (TimestampWritable.hasDecimalOrSecondVInt(internalBytes[0])) { in.readFully(internalBytes, 4, 1); int len = (byte) WritableUtils.decodeVIntSize(internalBytes[4]); if (len > 1) { in.readFully(internalBytes, 5, len-1); } long vlong = LazyBinaryUtils.readVLongFromByteArray(internalBytes, 4); if (vlong < -1000000000 || vlong > 999999999) { throw new IOException( "Invalid first vint value (encoded nanoseconds) of a TimestampWritable: " + vlong + ", expected to be between -1000000000 and 999999999."); // Note that -1000000000 is a valid value corresponding to a nanosecond timestamp // of 999999999, because if the second VInt is present, we use the value // (-reversedNanoseconds - 1) as the second VInt. } if (vlong < 0) { // This indicates there is a second VInt containing the additional bits of the seconds // field. in.readFully(internalBytes, 4 + len, 1); int secondVIntLen = (byte) WritableUtils.decodeVIntSize(internalBytes[4 + len]); if (secondVIntLen > 1) { in.readFully(internalBytes, 5 + len, secondVIntLen - 1); } } } currentBytes = internalBytes; this.offset = 0; }
public static long getSeconds(byte[] bytes, int offset) { int firstVInt = bytesToInt(bytes, offset); if (firstVInt >= 0 || !hasSecondVInt(bytes[offset + 4])) { return firstVInt & ~DECIMAL_OR_SECOND_VINT_FLAG; } return ((long) (firstVInt & ~DECIMAL_OR_SECOND_VINT_FLAG)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
/** * Gets seconds stored as integer at bytes[offset] * @param bytes * @param offset * @return the number of seconds */ public static long getSeconds(byte[] bytes, int offset) { int lowest31BitsOfSecondsAndFlag = bytesToInt(bytes, offset); if (lowest31BitsOfSecondsAndFlag >= 0 || // the "has decimal or second VInt" flag is not set !hasSecondVInt(bytes[offset + 4])) { // The entire seconds field is stored in the first 4 bytes. return lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK; } // We compose the seconds field from two parts. The lowest 31 bits come from the first four // bytes. The higher-order bits come from the second VInt that follows the nanos field. return ((long) (lowest31BitsOfSecondsAndFlag & LOWEST_31_BITS_OF_SEC_MASK)) | (LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])) << 31); }
public static void setTimestamp(Timestamp t, byte[] bytes, int offset) { boolean hasDecimalOrSecondVInt = hasDecimalOrSecondVInt(bytes[offset]); long seconds = (long) TimestampWritable.getSeconds(bytes, offset); int nanos = 0; if (hasDecimalOrSecondVInt) { nanos = TimestampWritable.getNanos(bytes, offset + 4); if (hasSecondVInt(bytes[offset + 4])) { seconds += LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])); } } t.setTime(seconds * 1000); if (nanos != 0) { t.setNanos(nanos); } }
public static void setTimestamp(Timestamp t, byte[] bytes, int offset) { boolean hasDecimalOrSecondVInt = hasDecimalOrSecondVInt(bytes[offset]); long seconds = (long) TimestampWritable.getSeconds(bytes, offset); int nanos = 0; if (hasDecimalOrSecondVInt) { nanos = TimestampWritable.getNanos(bytes, offset + 4); if (hasSecondVInt(bytes[offset + 4])) { seconds += LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])); } } t.setTime(seconds * 1000); if (nanos != 0) { t.setNanos(nanos); } }
public static void setTimestamp(Timestamp t, byte[] bytes, int offset) { boolean hasDecimalOrSecondVInt = hasDecimalOrSecondVInt(bytes[offset]); long seconds = (long) TimestampWritable.getSeconds(bytes, offset); int nanos = 0; if (hasDecimalOrSecondVInt) { nanos = TimestampWritable.getNanos(bytes, offset + 4); if (hasSecondVInt(bytes[offset + 4])) { seconds += LazyBinaryUtils.readVLongFromByteArray(bytes, offset + 4 + WritableUtils.decodeVIntSize(bytes[offset + 4])); } } t.setTime(seconds * 1000); if (nanos != 0) { t.setNanos(nanos); } }