@Test public void testSamePrefixAnd00() { Key prev = new Key("r3boot4", "f89222", "q90232e"); assertEquals(prev, KeyShortener.shorten(prev, newKey(append00("r3boot4"), "f89222", "q90232e", 8))); prev = new Key("r3boot4", "f892", "q90232e"); assertEquals(prev, KeyShortener.shorten(prev, newKey("r3boot4", append00("f892"), "q90232e", 8))); prev = new Key("r3boot4", "f89222", "q902"); assertEquals(prev, KeyShortener.shorten(prev, newKey("r3boot4", "f89222", append00("q902"), 8))); }
public static ByteSequence shorten(ByteSequence prev, ByteSequence current) { int minLen = Math.min(prev.length(), current.length()); for (int i = 0; i < minLen; i++) { int pb = 0xff & prev.byteAt(i); int cb = 0xff & current.byteAt(i); int diff = cb - pb; if (diff == 1) { int newLen = findNonFF(prev, i + 1); byte[] successor; if (newLen < prev.length()) { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF); } else { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00); } return new ArrayByteSequence(successor); } else if (diff > 1) { byte[] copy = new byte[i + 1]; System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1); copy[i] = (byte) ((0xff & copy[i]) + 1); return new ArrayByteSequence(copy); } } ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(), B00)); if (successor.equals(current)) { return null; } return successor; }
KeyShortener { public static ByteSequence shorten(ByteSequence prev, ByteSequence current) { int minLen = Math.min(prev.length(), current.length()); for (int i = 0; i < minLen; i++) { int pb = 0xff & prev.byteAt(i); int cb = 0xff & current.byteAt(i); int diff = cb - pb; if (diff == 1) { int newLen = findNonFF(prev, i + 1); byte[] successor; if (newLen < prev.length()) { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF); } else { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00); } return new ArrayByteSequence(successor); } else if (diff > 1) { byte[] copy = new byte[i + 1]; System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1); copy[i] = (byte) ((0xff & copy[i]) + 1); return new ArrayByteSequence(copy); } } ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(), B00)); if (successor.equals(current)) { return null; } return successor; } }
KeyShortener { public static ByteSequence shorten(ByteSequence prev, ByteSequence current) { int minLen = Math.min(prev.length(), current.length()); for (int i = 0; i < minLen; i++) { int pb = 0xff & prev.byteAt(i); int cb = 0xff & current.byteAt(i); int diff = cb - pb; if (diff == 1) { int newLen = findNonFF(prev, i + 1); byte[] successor; if (newLen < prev.length()) { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF); } else { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00); } return new ArrayByteSequence(successor); } else if (diff > 1) { byte[] copy = new byte[i + 1]; System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1); copy[i] = (byte) ((0xff & copy[i]) + 1); return new ArrayByteSequence(copy); } } ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(), B00)); if (successor.equals(current)) { return null; } return successor; } private  KeyShortener(); }
KeyShortener { public static ByteSequence shorten(ByteSequence prev, ByteSequence current) { int minLen = Math.min(prev.length(), current.length()); for (int i = 0; i < minLen; i++) { int pb = 0xff & prev.byteAt(i); int cb = 0xff & current.byteAt(i); int diff = cb - pb; if (diff == 1) { int newLen = findNonFF(prev, i + 1); byte[] successor; if (newLen < prev.length()) { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF); } else { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00); } return new ArrayByteSequence(successor); } else if (diff > 1) { byte[] copy = new byte[i + 1]; System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1); copy[i] = (byte) ((0xff & copy[i]) + 1); return new ArrayByteSequence(copy); } } ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(), B00)); if (successor.equals(current)) { return null; } return successor; } private  KeyShortener(); static ByteSequence shorten(ByteSequence prev, ByteSequence current); static Key shorten(Key prev, Key current); }
KeyShortener { public static ByteSequence shorten(ByteSequence prev, ByteSequence current) { int minLen = Math.min(prev.length(), current.length()); for (int i = 0; i < minLen; i++) { int pb = 0xff & prev.byteAt(i); int cb = 0xff & current.byteAt(i); int diff = cb - pb; if (diff == 1) { int newLen = findNonFF(prev, i + 1); byte[] successor; if (newLen < prev.length()) { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF); } else { successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00); } return new ArrayByteSequence(successor); } else if (diff > 1) { byte[] copy = new byte[i + 1]; System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1); copy[i] = (byte) ((0xff & copy[i]) + 1); return new ArrayByteSequence(copy); } } ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(), B00)); if (successor.equals(current)) { return null; } return successor; } private  KeyShortener(); static ByteSequence shorten(ByteSequence prev, ByteSequence current); static Key shorten(Key prev, Key current);  }