@Override public void writeResponse(DataOutputStream outputStream) throws IOException { outputStream.writeShort(0); outputStream.writeInt(results.size()); for(Version v: results) { byte[] clock = ((VectorClock) v).toBytes(); outputStream.writeInt(clock.length); outputStream.write(clock); } }
public byte[] toBytes() { byte[] serialized = new byte[sizeInBytes()]; toBytes(serialized, 0); return serialized; }
public static void writeResults(DataOutputStream outputStream, List<Versioned<byte[]>> values) throws IOException { outputStream.writeInt(values.size()); for(Versioned<byte[]> v: values) { byte[] clock = ((VectorClock) v.getVersion()).toBytes(); byte[] value = v.getValue(); outputStream.writeInt(clock.length + value.length); outputStream.write(clock); outputStream.write(value); } }
private void writeVersion(String key, VectorClock version) { try { File versionFile = new File(getVersionDirectory(), key); if(!versionFile.exists() || versionFile.delete()) { // write the version file. String hexCode = new String(Hex.encodeHex(version.toBytes())); FileUtils.writeStringToFile(versionFile, hexCode, "UTF-8"); } } catch(Exception e) { throw new VoldemortException("Failed to write Version for Key:" + key, e); } }
public byte[] getBytes() { try { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(byteOutput); output.writeByte(opCode); if(opCode != VoldemortOpCode.GET_OP_CODE) output.write(version.toBytes()); output.writeUTF(key); if(opCode == VoldemortOpCode.PUT_OP_CODE) { output.writeInt(value.length); output.write(value); } return byteOutput.toByteArray(); } catch(IOException e) { throw new SerializationException(e); } }
private void writeVersion(VectorClock newClock) { File versionFile = getVersionFile(); try { if(!versionFile.exists() || versionFile.delete()) { String hexCode = new String(Hex.encodeHex(newClock.toBytes())); FileUtils.writeStringToFile(versionFile, hexCode, "UTF-8"); this.cachedVersion = newClock; } } catch(Exception e) { throw new VoldemortException("Failed to write Version for the current file :" + getName(), e); } }
/** * Store the versioned values * * @param values list of versioned bytes * @return the list of versioned values rolled into an array of bytes */ private byte[] assembleValues(List<Versioned<byte[]>> values) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(stream); for(Versioned<byte[]> value: values) { byte[] object = value.getValue(); dataStream.writeInt(object.length); dataStream.write(object); VectorClock clock = (VectorClock) value.getVersion(); dataStream.writeInt(clock.sizeInBytes()); dataStream.write(clock.toBytes()); } return stream.toByteArray(); }
@Override public void printTo(DataOutputStream out) throws IOException { while(entryIterator.hasNext()) { Pair<ByteArray, Versioned<byte[]>> kvPair = entryIterator.next(); byte[] keyBytes = kvPair.getFirst().get(); byte[] versionBytes = ((VectorClock) kvPair.getSecond().getVersion()).toBytes(); byte[] valueBytes = kvPair.getSecond().getValue(); out.writeInt(keyBytes.length); out.write(keyBytes); out.writeInt(versionBytes.length); out.write(versionBytes); out.writeInt(valueBytes.length); out.write(valueBytes); } } });
public void writeDeleteRequest(DataOutputStream outputStream, String storeName, ByteArray key, VectorClock version, RequestRoutingType routingType) throws IOException { StoreUtils.assertValidKey(key); outputStream.writeByte(VoldemortOpCode.DELETE_OP_CODE); outputStream.writeUTF(storeName); outputStream.writeBoolean(routingType.equals(RequestRoutingType.ROUTED)); if(protocolVersion > 1) { outputStream.writeByte(routingType.getRoutingTypeCode()); } outputStream.writeInt(key.length()); outputStream.write(key.get()); VectorClock clock = version; outputStream.writeShort(clock.sizeInBytes()); outputStream.write(clock.toBytes()); }
public void writePutRequest(DataOutputStream outputStream, String storeName, ByteArray key, byte[] value, byte[] transforms, VectorClock version, RequestRoutingType routingType) throws IOException { StoreUtils.assertValidKey(key); outputStream.writeByte(VoldemortOpCode.PUT_OP_CODE); outputStream.writeUTF(storeName); outputStream.writeBoolean(routingType.equals(RequestRoutingType.ROUTED)); if(protocolVersion > 1) { outputStream.writeByte(routingType.getRoutingTypeCode()); } outputStream.writeInt(key.length()); outputStream.write(key.get()); outputStream.writeInt(value.length + version.sizeInBytes()); outputStream.write(version.toBytes()); outputStream.write(value); if(protocolVersion > 2) { if(transforms != null) { outputStream.writeBoolean(true); outputStream.writeInt(transforms.length); outputStream.write(transforms); } else outputStream.writeBoolean(false); } }
public byte[] toBytes(Versioned<T> versioned) { byte[] versionBytes = null; if(versioned.getVersion() == null) versionBytes = new byte[] { -1 }; else versionBytes = ((VectorClock) versioned.getVersion()).toBytes(); byte[] objectBytes = innerSerializer.toBytes(versioned.getValue()); return ByteUtils.cat(versionBytes, objectBytes); }
+ ByteUtils.toHexString(version.toBytes()) + "\n"); out.write("VECTOR_CLOCK_TEXT: " + version.toString() + '[' + new Date(version.getTimestamp()).toString() + "]\n");
public static String makeLine(Pair<ByteArray, Versioned<byte[]>> pair) { Versioned<byte[]> versioned = pair.getSecond(); byte[] keyBytes = pair.getFirst().get(); byte[] versionBytes = ((VectorClock)versioned.getVersion()).toBytes(); byte[] valueBytes = pair.getSecond().getValue(); return String.format("%s %s %s\n", ByteUtils.toHexString(keyBytes),ByteUtils.toHexString(versionBytes),ByteUtils.toHexString(valueBytes)); }
public static byte[] toByteArray(List<Versioned<byte[]>> values) { int size = 1; for(Versioned<byte[]> v: values) { size += ((VectorClock) v.getVersion()).sizeInBytes(); size += ByteUtils.SIZE_OF_INT; size += v.getValue().length; } byte[] bytes = new byte[size]; int pos = 1; bytes[0] = VERSION; for(Versioned<byte[]> v: values) { pos += ((VectorClock) v.getVersion()).toBytes(bytes, pos); int len = v.getValue().length; ByteUtils.writeInt(bytes, len, pos); pos += ByteUtils.SIZE_OF_INT; System.arraycopy(v.getValue(), 0, bytes, pos, len); pos += len; } if(pos != bytes.length) throw new VoldemortException((bytes.length - pos) + " straggling bytes found in value (this should not be possible)!"); return bytes; }
@Test public void testSerializationWraps() { VectorClock clock = getClock(1, 1, 2, 3, 3, 6); for(int i = 0; i < 300; i++) clock.incrementVersion(2, System.currentTimeMillis()); assertEquals("Clock does not serialize to itself.", clock, new VectorClock(clock.toBytes())); }
@Test public void testSerializationBackwardCompatibility() { assertEquals("The empty clock serializes incorrectly.", getClock(), new VectorClock(getClock().toBytes())); VectorClock clock = getClock(1, 1, 2, 3, 4, 4, 6); // Old Vector Clock would serialize to this: // 0 5 1 0 1 2 0 2 1 0 3 1 0 4 2 0 6 1 [timestamp] byte[] knownSerializedHead = { 0, 5, 1, 0, 1, 2, 0, 2, 1, 0, 3, 1, 0, 4, 2, 0, 6, 1 }; byte[] serialized = clock.toBytes(); for(int index = 0; index < knownSerializedHead.length; index++) { assertEquals("byte at index " + index + " is not equal", knownSerializedHead[index], serialized[index]); } }
/** * Pre-condition: timestamp is ignored in determine vector clock equality */ @Test public void testDeserializationBackwardCompatibility() { assertEquals("The empty clock serializes incorrectly.", getClock(), new VectorClock(getClock().toBytes())); VectorClock clock = getClock(1, 1, 2, 3, 4, 4, 6); // Old Vector Clock would serialize to this: // 0 5; 1; 0 1, 2; 0 2, 1; 0 3, 1; 0 4, 2; 0 6, 1; [timestamp=random] byte[] knownSerialized = { 0, 5, 1, 0, 1, 2, 0, 2, 1, 0, 3, 1, 0, 4, 2, 0, 6, 1, 0, 0, 1, 0x3e, 0x7b, (byte) 0x8c, (byte) 0x9d, 0x19 }; assertEquals("vector clock does not deserialize correctly on given byte array", clock, new VectorClock(knownSerialized)); DataInputStream ds = new DataInputStream(new ByteArrayInputStream(knownSerialized)); VectorClock clock2 = VectorClock.createVectorClock(ds); assertEquals("vector clock does not deserialize correctly on given input stream", clock, clock2); }
@Test public void testIncrementAndSerialize() { int node = 1; VectorClock vc = getClock(node); assertEquals(node, vc.getMaxVersion()); int increments = 3000; for(int i = 0; i < increments; i++) { vc.incrementVersion(node, 45); // serialize vc = new VectorClock(vc.toBytes()); } assertEquals(increments + 1, vc.getMaxVersion()); }