/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { if (b1[s1] == 0 || b2[s2] == 0) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } return ByteArrayUtil.compare( ByteArrayUtil.readShort(b1, s1 + 1), ByteArrayUtil.readShort(b2, s2 + 1)); } }
private static boolean equalsTexts(Text a, Text b) { return ByteArrayUtil.equals( a.getBytes(), 0, a.getLength(), b.getBytes(), 0, b.getLength()); }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { if (b1[s1] == 0 || b2[s2] == 0) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } return ByteArrayUtil.compare( ByteArrayUtil.readInt(b1, s1 + 1), ByteArrayUtil.readInt(b2, s2 + 1)); } }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { if (b1[s1] == 0 || b2[s2] == 0) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } return ByteArrayUtil.compare( ByteArrayUtil.readLong(b1, s1 + 1), ByteArrayUtil.readLong(b2, s2 + 1)); } }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { if (b1[s1] == 0 || b2[s2] == 0) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } int n1 = WritableUtils.decodeVIntSize(b1[s1 + 1]); int n2 = WritableUtils.decodeVIntSize(b2[s2 + 1]); int len1 = (int) ByteArrayUtil.readVLong(b1, s1 + 1); int len2 = (int) ByteArrayUtil.readVLong(b2, s2 + 1); return ByteArrayUtil.compare( b1, s1 + 1 + n1, len1, b2, s2 + 1 + n2, len2); }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a Date field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 4 + 1) { modify(ByteArrayUtil.readInt(bytes, offset + 1)); return 5; } else { throw new IOException(MessageFormat.format( "Cannot restore a Date field ({0})", "invalid length")); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a long field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 1 + 1) { modify(ByteArrayUtil.readLong(bytes, offset + 1) + Long.MIN_VALUE); return 8 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore a long field ({0})", "invalid length")); } }
/** * Returns the actual number of bytes from the serialized byte array. * @param bytes the target byte array * @param offset the beginning index in the byte array (inclusive) * @param length the limit length of the byte array * @return the comparison result */ public static int getBytesLength(byte[] bytes, int offset, int length) { if (bytes[offset] == 0) { return 1; } int size = WritableUtils.decodeVIntSize(bytes[offset + 1]); int textLength = (int) ByteArrayUtil.readVLong(bytes, offset + 1); return 1 + size + textLength; }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a short field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 1 + 1) { modify(ByteArrayUtil.readShort(bytes, offset + 1)); return 2 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore a short field ({0})", "invalid length")); } }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { if (b1[s1] == 0 || b2[s2] == 0) { return ByteArrayUtil.compare(b1[s1], b2[s2]); } return ByteArrayUtil.compare(b1[s1 + 1], b2[s2 + 1]); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore int field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 1 + 1) { modify(ByteArrayUtil.readInt(bytes, offset + 1) + Integer.MIN_VALUE); return 4 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore int field ({0})", "invalid length")); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a DateTime field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 8 + 1) { modify(ByteArrayUtil.readLong(bytes, offset + 1)); return 8 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore a DateTime field ({0})", "invalid length")); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a String field ({0})", "invalid length")); } if (bytes[offset] == 0) { setNull(); return 1; } int size = WritableUtils.decodeVIntSize(bytes[offset + 1]); if (limit - offset < size + 1) { throw new IOException(MessageFormat.format( "Cannot restore a String field ({0})", "invalid length")); } int length = (int) ByteArrayUtil.readVLong(bytes, offset + 1); if (limit - offset >= size + 1 + length) { nullValue = false; entity.set(bytes, offset + size + 1, length); return size + 1 + length; } else { throw new IOException(MessageFormat.format( "Cannot restore a String field ({0})", "invalid length")); } }
private static int compareTexts(Text a, Text b) { return ByteArrayUtil.compare( a.getBytes(), 0, a.getLength(), b.getBytes(), 0, b.getLength()); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a float field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 1 + 1) { modify(decode(ByteArrayUtil.readInt(bytes, offset + 1))); return 4 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore a float field ({0})", "invalid length")); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { if (limit - offset == 0) { throw new IOException(MessageFormat.format( "Cannot restore a double field ({0})", "invalid length")); } if (bytes[offset + 0] == 0) { setNull(); return 1; } else if (limit - offset >= 1 + 1) { modify(decode(ByteArrayUtil.readLong(bytes, offset + 1))); return 8 + 1; } else { throw new IOException(MessageFormat.format( "Cannot restore a double field ({0})", "invalid length")); } }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { int len1 = getBytesLength(b1, s1, l1); int len2 = getBytesLength(b2, s2, l2); return ByteArrayUtil.compare(b1, s1, len1, b2, s2, len2); }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { int len1 = getBytesLength(b1, s1, l1); int len2 = getBytesLength(b2, s2, l2); return ByteArrayUtil.compare(b1, s1, len1, b2, s2, len2); }
/** * Compares between the two objects in serialized form. * @param b1 the first byte array to be compared * @param s1 the beginning index in {@code b1} * @param l1 the limit byte size in {@code b1} * @param b2 the second byte array to be compared * @param s2 the beginning index in {@code b2} * @param l2 the limit byte size in {@code b2} * @return the comparison result */ public static int compareBytes( byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { int len1 = getBytesLength(b1, s1, l1); int len2 = getBytesLength(b2, s2, l2); return ByteArrayUtil.compare(b1, s1, len1, b2, s2, len2); } }