protected void setUp() { lc = new LexicographicalComparator(); } }
@Override public int compare(byte[] b1, int o1, int l1, byte[] b2, int o2, int l2) { byte type1 = b1[o1]; byte type2 = b2[o2]; // optimized comparisons if (isString(type1) && isString(type2)) { return compareAsString(type1, b1, o1, type2, b2, o2); } else if (isNumeric(type1) && isNumeric(type2)) { return compareAsNumeric(type1, b1, o1, type2, b2, o2); } else if (type1 == DSCODE.BOOLEAN && type2 == DSCODE.BOOLEAN) { return compareAsBoolean(getBoolean(b1, o1), getBoolean(b2, o2)); } else if (type1 == DSCODE.CHARACTER && type2 == DSCODE.CHARACTER) { return compareAsChar(getChar(b1, o1), getChar(b2, o2)); } else if (type1 == DSCODE.NULL || type2 == DSCODE.NULL) { // null check, assumes NULLs sort last return type1 == type2 ? 0 : type1 == DSCODE.NULL ? 1 : -1; } // fallback, will deserialize to Comparable return compareAsObject(b1, o1, l1, b2, o2, l2); }
private static int compareAsNumeric(byte type1, byte[] b1, int o1, byte type2, byte[] b2, int o2) { switch (type1 << 8 | type2) { case BYTE_TO_BYTE: return compareAsShort (getByte (b1, o1), getByte (b2, o2)); case BYTE_TO_SHORT: return compareAsShort (getByte (b1, o1), getShort (b2, o2)); case BYTE_TO_INT: return compareAsInt (getByte (b1, o1), getInt (b2, o2)); case BYTE_TO_LONG: return compareAsLong (getByte (b1, o1), getLong (b2, o2)); case BYTE_TO_FLOAT: return compareAsFloat (getByte (b1, o1), getFloat (b2, o2)); case BYTE_TO_DOUBLE: return compareAsDouble(getByte (b1, o1), getDouble(b2, o2)); case SHORT_TO_BYTE: return compareAsShort (getShort (b1, o1), getByte (b2, o2)); case SHORT_TO_SHORT: return compareAsShort (getShort (b1, o1), getShort (b2, o2)); case SHORT_TO_INT: return compareAsInt (getShort (b1, o1), getInt (b2, o2)); case SHORT_TO_LONG: return compareAsLong (getShort (b1, o1), getLong (b2, o2)); case SHORT_TO_FLOAT: return compareAsFloat (getShort (b1, o1), getFloat (b2, o2)); case SHORT_TO_DOUBLE: return compareAsDouble(getShort (b1, o1), getDouble(b2, o2)); case INT_TO_BYTE: return compareAsInt (getInt (b1, o1), getByte (b2, o2)); case INT_TO_SHORT: return compareAsInt (getInt (b1, o1), getShort (b2, o2)); case INT_TO_INT: return compareAsInt (getInt (b1, o1), getInt (b2, o2)); case INT_TO_LONG: return compareAsLong (getInt (b1, o1), getLong (b2, o2)); case INT_TO_FLOAT: return compareAsFloat (getInt (b1, o1), getFloat (b2, o2)); case INT_TO_DOUBLE: return compareAsDouble(getInt (b1, o1), getDouble(b2, o2)); case LONG_TO_BYTE: return compareAsLong (getLong (b1, o1), getByte (b2, o2)); case LONG_TO_SHORT: return compareAsLong (getLong (b1, o1), getShort (b2, o2)); case LONG_TO_INT: return compareAsLong (getLong (b1, o1), getInt (b2, o2)); case LONG_TO_LONG: return compareAsLong (getLong (b1, o1), getLong (b2, o2)); case LONG_TO_FLOAT: return compareAsDouble(getLong (b1, o1), getFloat (b2, o2)); case LONG_TO_DOUBLE: return compareAsDouble(getLong (b1, o1), getDouble(b2, o2));
private static int compareAsStringOfUtf(byte[] b1, int o1, byte[] b2, int o2) { int offset = 3; int l1 = Bytes.toUnsignedShort(b1[o1 + 1], b1[o1 + 2]); int l2 = Bytes.toUnsignedShort(b2[o1 + 1], b2[o1 + 2]); assert b1.length >= o1 + offset + l1; assert b2.length >= o2 + offset + l2; int i = 0; int j = 0; while (i < l1 && j < l2) { final int idx = o1 + offset + i; final int ilen = getUtfLength(b1[idx]); final char c1 = getUtfChar(b1, idx, ilen); i += ilen; final int jdx = o2 + offset + j; final int jlen = getUtfLength(b2[jdx]); char c2 = getUtfChar(b2, jdx, jlen); j += jlen; int diff = compareAsChar(c1, c2); if (diff != 0) { return diff; } } return (l1 - i) - (l2 - j); }
public void testIntPerformance() throws Exception { ByteBuffer b1 = ByteBuffer.allocate(5).put(0, DSCODE.INTEGER); ByteBuffer b2 = ByteBuffer.allocate(5).put(0, DSCODE.INTEGER); for (int n = 0; n < 5; n++) { long diff = 0; int count = 10000000; long start = System.nanoTime(); for (int i = 0; i < count; i++) { b1.putInt(1, i); b2.putInt(1, i + 1); diff += lc.compare(b1.array(), b1.arrayOffset(), b1.capacity(), b2.array(), b2.arrayOffset(), b2.capacity()); } long nanos = System.nanoTime() - start; System.out.printf("(%d) %f int comparisons / ms\n", diff, 1000000.0 * count / nanos); diff = 0; start = System.nanoTime(); for (int i = 0; i < count; i++) { b1.putInt(1, i); b2.putInt(1, i + 1); diff += Bytes.compareTo(b1.array(), b1.arrayOffset(), b1.capacity(), b2.array(), b2.arrayOffset(), b2.capacity()); } nanos = System.nanoTime() - start; System.out.printf("(%d) %f byte comparisons / ms\n\n", diff, 1000000.0 * count / nanos); } }
private static int compareAsStringOfByteToUtf(byte[] b1, int o1, byte[] b2, int o2) { int offset = 3; int l1 = Bytes.toUnsignedShort(b1[o1 + 1], b1[o1 + 2]); int l2 = Bytes.toUnsignedShort(b2[o1 + 1], b2[o1 + 2]); assert b1.length >= o1 + offset + l1; assert b2.length >= o2 + offset + l2; int i = 0; int j = 0; while (i < l1 && j < l2) { final int idx = o1 + offset + i; final char c1 = (char) b1[idx]; i++; final int jdx = o2 + offset + j; final int jlen = getUtfLength(b2[jdx]); char c2 = getUtfChar(b2, jdx, jlen); j += jlen; int diff = compareAsChar(c1, c2); if (diff != 0) { return diff; } } return (l1 - i) - (l2 - j); }
public void setUp() { comp = new ArraySerializedComparator(); comp.setComparators(new SerializedComparator[] { new LexicographicalComparator(), // string new LexicographicalComparator(), // int new LexicographicalComparator() // boolean }); } }
public IndexSerializedComparator() { primary = new LexicographicalComparator(); secondary = new ByteComparator(); }
public void testReadAndWrite() throws Exception { ArraySerializedComparator ac = new ArraySerializedComparator(); ac.setComparators(new SerializedComparator[] { new LexicographicalComparator(), new LexicographicalComparator(), new LexicographicalComparator() });
public void testReadAndWrite() throws Exception { SortedOplogConfiguration config = new SortedOplogConfiguration("lex") .setComparator(new LexicographicalComparator()); hfile = new HFileSortedOplog(new File("lexi.soplog"), config); SortedOplogWriter wtr = hfile.createWriter(); for (int i = -100; i < 100; i++) { wtr.append(ComparisonTestCase.convert(i), ComparisonTestCase.convert(i)); } wtr.close(null); SortedReader<ByteBuffer> rdr = hfile.createReader(); for (int i = -100; i < 100; i++) { byte[] key = ComparisonTestCase.convert(i); assertTrue(rdr.mightContain(key)); ByteBuffer val = rdr.read(key); assertEquals(i, ComparisonTestCase.recover(val.array(), val.arrayOffset(), val.remaining())); } rdr.close(); }