TabletRowAdapter { public static OptionalLong decodeRowOffset(Text row) { OptionalLong offset = OptionalLong.empty(); byte[] bytes = row.getBytes(); int delimidx = findRowDelimiterIndex(bytes); if (delimidx > 0) { try { byte[] buffer = Arrays.copyOfRange(bytes, delimidx + 1, bytes.length); buffer = ByteUtils.unescape(buffer); int extendBy = (MIN_OFFSET_LEN - (buffer.length)); if (extendBy > 0) { buffer = ArrayUtils.addAll(buffer, new byte[extendBy]); } long decodedOffset = OFFSET_DECODER.decode(buffer); if (decodedOffset > 0) { offset = OptionalLong.of(decodedOffset); } else if (LOG.isTraceEnabled()) { Optional<String> prefix = decodeRowPrefix(row); LOG.trace( "Delimiter identified but offset could not parse { prefix: {}, byte-len: {}, " + "offset-len: {}, offset-bytes: {}, row-bytes: {} }", prefix, bytes.length, buffer.length, Hex.encodeHex(buffer), Hex.encodeHex(bytes)); } } catch (IllegalArgumentException e) { if (LOG.isTraceEnabled()) { LOG.trace("Unable to parse offset: " + e.getMessage(), e); } } } return offset; }  static OptionalLong decodeRowOffset(Text row); static Optional<String> decodeRowPrefix(Text row); static String toDebugOutput(TabletId tid);  }
@Test public void decodePrefix() { ComparablePair<String, Long> p = new ComparablePair<>("test", 1L); byte[] bytes = pairCoder.encode(p); OptionalLong decoded = TabletRowAdapter.decodeRowOffset(new Text(bytes)); assertTrue(decoded.isPresent()); }
@Test public void decodeOffsetOnly() { ComparablePair<String, Long> p = new ComparablePair<>("test", 1L); byte[] bytes = pairCoder.encode(p); OptionalLong decoded = TabletRowAdapter.decodeRowOffset(new Text(bytes)); assertTrue(decoded.isPresent()); assertEquals(1L, decoded.getAsLong()); }
@Test public void decodeOffsetWithTruncatedOffset() { long truncateOffset = 1554962382848L; ComparablePair<String, Long> p = new ComparablePair<>("sys.disk.disk_octets", truncateOffset); byte[] bytes = pairCoder.encode(p); bytes = Arrays.copyOf(bytes, bytes.length - 4); OptionalLong decoded = TabletRowAdapter.decodeRowOffset(new Text(bytes)); assertTrue(decoded.isPresent()); assertEquals(truncateOffset, decoded.getAsLong()); }
@Test public void decodeOffsetWithBadDataWillSet() { ComparablePair<String, String> p = new ComparablePair<>("test", "<"); byte[] bytes = new PairLexicoder<>(prefixCoder, prefixCoder).encode(p); OptionalLong decoded = TabletRowAdapter.decodeRowOffset(new Text(bytes)); assertFalse(decoded.isPresent()); }