public static int getResultsSize(List<Versioned<byte[]>> values) { int size = 4; for(Versioned<byte[]> v: values) { size += 4; size += ((VectorClock) v.getVersion()).sizeInBytes(); size += v.getValue().length; } return size; }
public int compare(Versioned<S> v1, Versioned<S> v2) { Occurred occurred = v1.getVersion().compare(v2.getVersion()); if(occurred == Occurred.BEFORE) return -1; else if(occurred == Occurred.AFTER) return 1; else return 0; } }
private Versioned<byte[]> incremented(Versioned<byte[]> versioned, int nodeId) { return new Versioned<byte[]>(versioned.getValue(), ((VectorClock) versioned.getVersion()).incremented(nodeId, time.getMilliseconds())); }
public UpdateElementById(VListKey<K> key, Versioned<E> element) { _key = key; _element = element.getValue(); _version = element.getVersion(); }
public void testClone() { Versioned<Integer> v1 = getVersioned(2, 1, 2, 3); Versioned<Integer> v2 = v1.cloneVersioned(); assertEquals(v1, v2); assertTrue(v1 != v2); assertTrue(v1.getVersion() != v2.getVersion()); ((VectorClock) v2.getVersion()).incrementVersion(1, System.currentTimeMillis()); assertTrue(!v1.equals(v2)); }
public static <S> Versioned<S> value(S s) { return new Versioned<S>(s, new VectorClock()); }
public void testIncrement() { ClockEntry v = new ClockEntry((short) 0, 1); assertEquals(v.getNodeId(), 0); assertEquals(v.getVersion(), 1); ClockEntry v2 = v.incremented(); assertEquals(v.getVersion(), 1); assertEquals(v2.getVersion(), 2); }
/** * Get new vector clock based on this clock but incremented on index nodeId * * @param nodeId The id of the node to increment * @return A vector clock equal on each element execept that indexed by * nodeId */ public VectorClock incremented(int nodeId, long time) { VectorClock copyClock = this.clone(); copyClock.incrementVersion(nodeId, time); return copyClock; }
private boolean clockContainsNonReplicas(VectorClock clock, List<Integer> replicas) { for(ClockEntry cEntry: clock.getEntries()) { if(!replicas.contains((int) cEntry.getNodeId())) { return true; } } return false; }
public byte[] toBytes() { byte[] serialized = new byte[sizeInBytes()]; toBytes(serialized, 0); return serialized; }
private VectorClock getVectorClock(byte[] bytes) { if(bytes[0] >= 0) return new VectorClock(bytes); return null; }
public void testEquality() { ClockEntry v1 = new ClockEntry((short) 0, 1); ClockEntry v2 = new ClockEntry((short) 0, 1); assertTrue(v1.equals(v1)); assertTrue(!v1.equals(null)); assertEquals(v1, v2); v1 = new ClockEntry((short) 0, 1); v2 = new ClockEntry((short) 0, 2); assertTrue(!v1.equals(v2)); v1 = new ClockEntry(Short.MAX_VALUE, 256); v2 = new ClockEntry(Short.MAX_VALUE, 256); assertEquals(v1, v2); }
/** * Record events for the given sequence of nodes * * @param clock The VectorClock to record the events on * @param nodes The sequences of node events */ public static void increment(VectorClock clock, int... nodes) { for(int n: nodes) clock.incrementVersion((short) n, clock.getTimestamp()); }
public void testGetVersions() { List<?> versions = ImmutableList.of("version1", "version2"); InconsistentDataException exception = new InconsistentDataException("foo", versions); assertEquals(versions, exception.getVersions()); } }
@Override public void setUp() { resolver = new VectorClockInconsistencyResolver<String>(); current = getVersioned(1, 1, 2, 3); prior = getVersioned(1, 2, 3); concurrent = getVersioned(1, 2, 3, 3); concurrent2 = getVersioned(1, 2, 3, 4); later = getVersioned(1, 1, 2, 2, 3); }
@Override public int getResponseSize() { int size = 2 + 4; for(Version v: results) { size += 4 + ((VectorClock) v).sizeInBytes(); } return size; }
public void validate() { if(nodeId < 0) throw new InvalidClockEntryException("Node id " + nodeId + " is not in the range (0, " + Short.MAX_VALUE + ")."); if(version < 1) throw new InvalidClockEntryException("Version " + version + " is not in the range (1, " + Short.MAX_VALUE + ")."); }
static int valueSize(Versioned<byte[]> value) { return value.getValue().length + ((VectorClock) value.getVersion()).sizeInBytes() + 1; }