@Override public RVVExceptionB clone() { RVVExceptionB clone = new RVVExceptionB(previousVersion, nextVersion); if (this.received != null) { clone.received = (BitSet) this.received.clone(); clone.receivedBaseVersion = this.receivedBaseVersion; } return clone; }
/** * add a received version */ @Override public void add(long receivedVersion) { // String me = this.toString(); // long oldv = this.nextVersion; if (receivedVersion == this.previousVersion + 1) { this.previousVersion = receivedVersion; if (this.received != null) { addReceived(receivedVersion); consumeReceivedVersions(); } } else if (receivedVersion == this.nextVersion - 1) { this.nextVersion = receivedVersion; if (this.received != null) { addReceived(receivedVersion); consumeReceivedVersions(); } } else if (this.previousVersion < receivedVersion && receivedVersion < this.nextVersion) { addReceived(receivedVersion); } // if (this.nextVersion == 29 && oldv != 29) { // System.out.println("before=" + me + "\nafter=" + this + "\nadded "+receivedVersion); // } }
@Override public long getHighestReceivedVersion() { if (isEmpty()) { return this.previousVersion; } else { // Note, the "length" of the bitset is the highest set bit + 1, // see the javadocs. That's why this works to return the highest // received version return receivedBaseVersion + received.length() - 1; } }
@Test public void testRVVExceptionB() { RVVExceptionB ex = new RVVExceptionB(5, 10); ex.add(8); ex.add(6); assertEquals(8, ex.getHighestReceivedVersion()); ex.add(5); assertEquals(8, ex.getHighestReceivedVersion()); }
@Override public RVVException changeForm() { // Convert the exception to a bitset exception RVVExceptionB ex = new RVVExceptionB(previousVersion, nextVersion); for (ReceivedVersionsReverseIterator it = this.receivedVersionsReverseIterator(); it .hasNext();) { long next = it.next(); ex.add(next); } return ex; }
protected boolean sameAs(RVVExceptionB ex) { if (!super.sameAs(ex)) { return false; } for (ReceivedVersionsReverseIterator it = receivedVersionsReverseIterator(); it.hasNext();) { if (!ex.contains(it.next())) { return false; } } for (ReceivedVersionsReverseIterator it = ex.receivedVersionsReverseIterator(); it.hasNext();) { if (!contains(it.next())) { return false; } } return true; }
@Override public void toData(DataOutput out) throws IOException { InternalDataSerializer.writeUnsignedVL(this.previousVersion, out); writeReceived(out); }
@Override protected void writeReceived(DataOutput out) throws IOException { LinkedList<Long> deltas = new LinkedList<>(); long last = this.nextVersion; // TODO - it would be better just to serialize the longs[] in the BitSet // as is, rather than go through this delta encoding. for (ReceivedVersionsReverseIterator it = receivedVersionsReverseIterator(); it.hasNext();) { long version = it.next(); deltas.addFirst(last - version); last = version; } InternalDataSerializer.writeUnsignedVL(deltas.size(), out); // Number of received versions // Last version is the oldest received version, still need the delta from there to previous deltas.addFirst(last - previousVersion); for (long value : deltas) { InternalDataSerializer.writeUnsignedVL(value, out); } }
/** Use this method to create a new RVVException */ static RVVException createException(long previousVersion, long nextVersion, long initialExceptionCount) { // arbitrary cutoff of 100 bytes to use a treeSet instead of bitSet // But if we are deserializing an exception too many received versions use a // bitset anyway. long delta = nextVersion - previousVersion; if (UseTreeSetsForTesting || (delta > RVV_MAX_BITSET_SPAN && initialExceptionCount * 512 < delta)) { return new RVVExceptionT(previousVersion, nextVersion); } return new RVVExceptionB(previousVersion, nextVersion); }
@Test public void testRVVExceptionBOutput() throws Exception { testExceptionOutput(new RVVExceptionB(50, 100)); }