private SnapshotCodec getCodec(InputStream in) { BinaryDecoder decoder = new BinaryDecoder(in); int persistedVersion; try { persistedVersion = decoder.readInt(); } catch (IOException e) { LOG.error("Unable to read transaction state version: ", e); throw Throwables.propagate(e); } return getCodecForVersion(persistedVersion); }
/** * Read one int from the input. * @return the read number * @throws java.io.IOException If there is IO error. * @throws java.io.EOFException If end of file reached. */ public int readInt() throws IOException { int val = 0; int shift = 0; int b = readByte(); while (b > 0x7f) { val ^= (b & 0x7f) << shift; shift += 7; b = readByte(); } val ^= b << shift; return (val >>> 1) ^ -(val & 1); }
private Set<ChangeId> decodeChanges(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); HashSet<ChangeId> changes = Sets.newHashSetWithExpectedSize(size); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { changes.add(new ChangeId(decoder.readBytes())); } size = decoder.readInt(); } // todo is there an immutable hash set? return changes; } }
private Collection<Long> decodeInvalid(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); Collection<Long> invalid = Lists.newArrayListWithCapacity(size); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { invalid.add(decoder.readLong()); } size = decoder.readInt(); } return invalid; }
@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); } }
/** * Read a byte sequence. First read an int to indicate how many bytes to read, then that many bytes. * @return the read bytes as a byte array * @throws java.io.IOException If there is IO error. * @throws java.io.EOFException If end of file reached. */ public byte[] readBytes() throws IOException { int toRead = readInt(); byte[] bytes = new byte[toRead]; while (toRead > 0) { int byteRead = input.read(bytes, bytes.length - toRead, toRead); if (byteRead == -1) { throw new EOFException(); } toRead -= byteRead; } return bytes; }
@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); } }
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; }
protected NavigableMap<Long, TransactionManager.InProgressTx> decodeInProgress(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); NavigableMap<Long, TransactionManager.InProgressTx> inProgress = Maps.newTreeMap(); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { long txId = decoder.readLong(); long expiration = decoder.readLong(); long visibilityUpperBound = decoder.readLong(); inProgress.put(txId, new TransactionManager.InProgressTx(visibilityUpperBound, expiration)); } size = decoder.readInt(); } return inProgress; }
/** * Read one long int from the input. * @return the read number * @throws java.io.IOException If there is IO error. * @throws java.io.EOFException If end of file reached. */ public long readLong() throws IOException { long val = 0; int shift = 0; int b = readByte(); while (b > 0x7f) { val ^= (long) (b & 0x7f) << shift; shift += 7; b = readByte(); } val ^= (long) b << shift; return (val >>> 1) ^ -(val & 1); }
throws IOException { int size = decoder.readInt(); NavigableMap<Long, TransactionManager.InProgressTx> inProgress = Maps.newTreeMap(); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { long txId = decoder.readLong(); long expiration = decoder.readLong(); long visibilityUpperBound = decoder.readLong(); int txTypeIdx = decoder.readInt(); TransactionManager.InProgressType txType; try { int checkpointPointerSize = decoder.readInt(); LongArrayList checkpointPointers = new LongArrayList(checkpointPointerSize); while (checkpointPointerSize != 0) { for (int checkpointRemaining = checkpointPointerSize; checkpointRemaining > 0; --checkpointRemaining) { checkpointPointers.add(decoder.readLong()); checkpointPointerSize = decoder.readInt(); size = decoder.readInt();
@Override protected NavigableMap<Long, TransactionManager.InProgressTx> decodeInProgress(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); NavigableMap<Long, TransactionManager.InProgressTx> inProgress = Maps.newTreeMap(); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { long txId = decoder.readLong(); long expiration = decoder.readLong(); long visibilityUpperBound = decoder.readLong(); int txTypeIdx = decoder.readInt(); TransactionManager.InProgressType txType; try { txType = TransactionManager.InProgressType.values()[txTypeIdx]; } catch (ArrayIndexOutOfBoundsException e) { throw new IOException("Type enum ordinal value is out of range: " + txTypeIdx); } inProgress.put(txId, new TransactionManager.InProgressTx(visibilityUpperBound, expiration, txType, new LongArrayList())); } size = decoder.readInt(); } return inProgress; } }