private NavigableMap<Long, Set<ChangeId>> decodeChangeSets(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); NavigableMap<Long, Set<ChangeId>> changeSets = new TreeMap<Long, Set<ChangeId>>(); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { changeSets.put(decoder.readLong(), decodeChanges(decoder)); } size = decoder.readInt(); } return changeSets; }
private void encodeChangeSets(BinaryEncoder encoder, Map<Long, Set<ChangeId>> changes) throws IOException { if (!changes.isEmpty()) { encoder.writeInt(changes.size()); for (Map.Entry<Long, Set<ChangeId>> entry : changes.entrySet()) { encoder.writeLong(entry.getKey()); encodeChanges(encoder, entry.getValue()); } } encoder.writeInt(0); // zero denotes end of list as per AVRO spec }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
@Override public TransactionSnapshot decode(InputStream in) { BinaryDecoder decoder = new BinaryDecoder(in); try { TransactionVisibilityState minTxSnapshot = decodeTransactionVisibilityState(in); NavigableMap<Long, Set<ChangeId>> committing = decodeChangeSets(decoder); NavigableMap<Long, Set<ChangeId>> committed = decodeChangeSets(decoder); return new TransactionSnapshot(minTxSnapshot.getTimestamp(), minTxSnapshot.getReadPointer(), minTxSnapshot.getWritePointer(), minTxSnapshot.getInvalid(), minTxSnapshot.getInProgress(), committing, committed); } catch (IOException e) { LOG.error("Unable to deserialize transaction state: ", e); throw Throwables.propagate(e); } }
@Override public TransactionVisibilityState decodeTransactionVisibilityState(InputStream in) { BinaryDecoder decoder = new BinaryDecoder(in); try { long timestamp = decoder.readLong(); long readPointer = decoder.readLong(); long writePointer = decoder.readLong(); Collection<Long> invalid = decodeInvalid(decoder); NavigableMap<Long, TransactionManager.InProgressTx> inProgress = decodeInProgress(decoder); return new TransactionSnapshot(timestamp, readPointer, writePointer, invalid, inProgress); } catch (IOException e) { LOG.error("Unable to deserialize transaction state: ", e); throw Throwables.propagate(e); } }