WriteReferencePersister { public WriteReference unpersist(StoredWriteReference writeReference) { return writeReference.accept(new StoredWriteReference.Visitor<WriteReference>() { @Override public WriteReference visitJson(byte[] ref) { try { return OBJECT_MAPPER.readValue(ref, WriteReference.class); } catch (IOException e) { throw new SafeRuntimeException("Exception hydrating object."); } } @Override public WriteReference visitTableNameAsStringBinary(byte[] ref) { int offset = 1; String tableReferenceString = EncodingUtils.decodeVarString(ref, offset); TableReference tableReference = TableReference.fromString(tableReferenceString); offset += EncodingUtils.sizeOfVarString(tableReferenceString); byte[] row = EncodingUtils.decodeSizedBytes(ref, offset); offset += EncodingUtils.sizeOfSizedBytes(row); byte[] column = EncodingUtils.decodeSizedBytes(ref, offset); offset += EncodingUtils.sizeOfSizedBytes(column); long isTombstone = EncodingUtils.decodeUnsignedVarLong(ref, offset); return ImmutableWriteReference.builder() .tableRef(tableReference) .cell(Cell.create(row, column)) .isTombstone(isTombstone == 1) .build(); } @Override public WriteReference visitTableIdBinary(byte[] ref) { int offset = 1; int tableId = Ints.checkedCast(EncodingUtils.decodeUnsignedVarLong(ref, offset)); TableReference tableReference = tableIndices.getTableReference(tableId); offset += EncodingUtils.sizeOfUnsignedVarLong(tableId); byte[] row = EncodingUtils.decodeSizedBytes(ref, offset); offset += EncodingUtils.sizeOfSizedBytes(row); byte[] column = EncodingUtils.decodeSizedBytes(ref, offset); offset += EncodingUtils.sizeOfSizedBytes(column); long isTombstone = EncodingUtils.decodeUnsignedVarLong(ref, offset); return ImmutableWriteReference.builder() .tableRef(tableReference) .cell(Cell.create(row, column)) .isTombstone(isTombstone == 1) .build(); } }); } WriteReferencePersister(SweepTableIndices tableIndices); WriteReference unpersist(StoredWriteReference writeReference); StoredWriteReference persist(WriteReference writeReference);  }
@Test public void testCanUnpersistJsonValues() { String original = "{\"t\":{\"namespace\":{\"name\":\"test_ctx\"},\"tablename\":\"test__table_name\"},\"c\":{\"" + "rowName\":\"P7du\",\"columnName\":\"dg==\"},\"d\":true}"; StoredWriteReference stored = StoredWriteReference.BYTES_HYDRATOR.hydrateFromBytes( original.getBytes(StandardCharsets.UTF_8)); assertThat(persister.unpersist(stored)).isEqualTo(WRITE_REFERENCE); }
@Test public void testCanUnpersistBinary_tableNameAsString() { byte[] data = EncodingUtils.add( new byte[1], EncodingUtils.encodeVarString(TABLE.getQualifiedName()), EncodingUtils.encodeSizedBytes(row), EncodingUtils.encodeSizedBytes(column), EncodingUtils.encodeVarLong(1)); StoredWriteReference stored = StoredWriteReference.BYTES_HYDRATOR.hydrateFromBytes(data); assertThat(persister.unpersist(stored)).isEqualTo(WRITE_REFERENCE); }