/** Compare binary encoded data. If equal, return zero. If greater-than, * return 1, if less than return -1. Order is consistent with that of {@link * org.apache.avro.generic.GenericData#compare(Object, Object, Schema)}.*/ public static int compare(byte[] b1, int s1, byte[] b2, int s2, Schema schema) { return compare(b1, s1, b1.length - s1, b2, s2, b2.length - s2, schema); }
/** Hash binary encoded data. Consistent with {@link * org.apache.avro.generic.GenericData#hashCode(Object, Schema)}.*/ public static int hashCode(byte[] bytes, int start, int length, Schema schema) { HashData data = HASH_DATA.get(); data.set(bytes, start, length); try { return hashCode(data, schema); } catch (IOException e) { throw new AvroRuntimeException(e); } }
@Override public int compareTo(Utf8 that) { return BinaryData.compareBytes(this.bytes, 0, this.length, that.bytes, 0, that.length); }
continue; int c = compare(d, field.schema()); if (c != 0) return (field.order() != Field.Order.DESCENDING) ? c : -c; long l = Math.min(l1, l2); while (i < l) { // compare to end of block int c = compare(d, schema.getElementType()); if (c != 0) return c; i++; r1--; r2--; int i2 = d2.readInt(); if (i1 == i2) { return compare(d, schema.getTypes().get(i1)); } else { return i1 - i2; int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size); d.d1.skipFixed(size); int l1 = d1.readInt(); int l2 = d2.readInt(); int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2); d.d1.skipFixed(l1);
assertEquals(-1, BinaryData.compare(b1, 0, b2, 0, schema)); assertEquals(1, BinaryData.compare(b2, 0, b1, 0, schema)); assertEquals(0, BinaryData.compare(b1, 0, b1, 0, schema)); assertEquals(0, BinaryData.compare(b2, 0, b2, 0, schema)); BinaryData.hashCode(b1, 0, b1.length, schema)); assertEquals(o2.hashCode(), BinaryData.hashCode(b2, 0, b2.length, schema)); BinaryData.hashCode(b1, 0, b1.length, schema)); assertEquals(comparator.hashCode(o2, schema), BinaryData.hashCode(b2, 0, b2.length, schema));
continue; hashCode = hashCode*31 + hashCode(data, field.schema()); for (long l = decoder.readArrayStart(); l != 0; l = decoder.arrayNext()) for (long i = 0; i < l; i++) hashCode = hashCode*31 + hashCode(data, elementType); return hashCode; throw new AvroRuntimeException("Can't hashCode maps!"); case UNION: return hashCode(data, schema.getTypes().get(decoder.readInt())); case FIXED: return hashBytes(1, data, schema.getFixedSize(), false); case STRING: return hashBytes(0, data, decoder.readInt(), false); case BYTES: return hashBytes(1, data, decoder.readInt(), true); case BOOLEAN: return decoder.readBoolean() ? 1231 : 1237;
@Override public void writeLong(long n) throws IOException { long val = (n << 1) ^ (n >> 63); // move sign to low-order bit if ((val & ~0x7FFFFFFFL) == 0) { int i = (int) val; while ((i & ~0x7F) != 0) { out.write((byte)((0x80 | i) & 0xFF)); i >>>= 7; } out.write((byte)i); return; } int len = BinaryData.encodeLong(n, buf, 0); out.write(buf, 0, len); }
@Override public void writeDouble(double d) throws IOException { byte[] buf = new byte[8]; int len = BinaryData.encodeDouble(d, buf, 0); out.write(buf, 0, len); }
@Override public void writeBoolean(boolean b) throws IOException { ensureBounds(1); pos += BinaryData.encodeBoolean(b, buf, pos); }
continue; int c = compare(d, field.schema()); if (c != 0) return (field.order() != Field.Order.DESCENDING) ? c : -c; long l = Math.min(l1, l2); while (i < l) { // compare to end of block int c = compare(d, schema.getElementType()); if (c != 0) return c; i++; r1--; r2--; int i2 = d2.readInt(); if (i1 == i2) { return compare(d, schema.getTypes().get(i1)); } else { return i1 - i2; int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size); d.d1.skipFixed(size); int l1 = d1.readInt(); int l2 = d2.readInt(); int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2); d.d1.skipFixed(l1);
continue; hashCode = hashCode*31 + hashCode(data, field.schema()); for (long l = decoder.readArrayStart(); l != 0; l = decoder.arrayNext()) for (long i = 0; i < l; i++) hashCode = hashCode*31 + hashCode(data, elementType); return hashCode; throw new AvroRuntimeException("Can't hashCode maps!"); case UNION: return hashCode(data, schema.getTypes().get(decoder.readInt())); case FIXED: return hashBytes(1, data, schema.getFixedSize(), false); case STRING: return hashBytes(0, data, decoder.readInt(), false); case BYTES: return hashBytes(1, data, decoder.readInt(), true); case BOOLEAN: return decoder.readBoolean() ? 1231 : 1237;
@Override public void writeLong(long n) throws IOException { long val = (n << 1) ^ (n >> 63); // move sign to low-order bit if ((val & ~0x7FFFFFFFL) == 0) { int i = (int) val; while ((i & ~0x7F) != 0) { out.write((byte)((0x80 | i) & 0xFF)); i >>>= 7; } out.write((byte)i); return; } int len = BinaryData.encodeLong(n, buf, 0); out.write(buf, 0, len); }
@Override public void writeDouble(double d) throws IOException { int len = BinaryData.encodeDouble(d, buf, 0); out.write(buf, 0, len); }
@Override public void writeBoolean(boolean b) throws IOException { // inlined, shorter version of ensureBounds if (buf.length == pos) { flushBuffer(); } pos += BinaryData.encodeBoolean(b, buf, pos); }
/** {@inheritDoc} */ @Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { return BinaryData.compare(b1, s1, b2, s2, mSchema); }
continue; int c = compare(d, field.schema()); if (c != 0) return (field.order() != Field.Order.DESCENDING) ? c : -c; long l = Math.min(l1, l2); while (i < l) { // compare to end of block int c = compare(d, schema.getElementType()); if (c != 0) return c; i++; r1--; r2--; int i2 = d2.readInt(); if (i1 == i2) { return compare(d, schema.getTypes().get(i1)); } else { return i1 - i2; int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size); d.d1.skipFixed(size); int l1 = d1.readInt(); int l2 = d2.readInt(); int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2); d.d1.skipFixed(l1);
continue; hashCode = hashCode*31 + hashCode(data, field.schema()); for (long l = decoder.readArrayStart(); l != 0; l = decoder.arrayNext()) for (long i = 0; i < l; i++) hashCode = hashCode*31 + hashCode(data, elementType); return hashCode; throw new AvroRuntimeException("Can't hashCode maps!"); case UNION: return hashCode(data, schema.getTypes().get(decoder.readInt())); case FIXED: return hashBytes(1, data, schema.getFixedSize(), false); case STRING: return hashBytes(0, data, decoder.readInt(), false); case BYTES: return hashBytes(1, data, decoder.readInt(), true); case BOOLEAN: return decoder.readBoolean() ? 1231 : 1237;