@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }
@Override public byte[] serialise(final TypeValue typeValue) throws SerialisationException { String type = typeValue.getType(); String value = typeValue.getValue(); if ((null == type || type.isEmpty()) && (null == value || value.isEmpty())) { throw new SerialisationException("TypeValue passed to serialiser is blank"); } ByteArrayOutputStream out = new ByteArrayOutputStream(); if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeValue Object", e); } } out.write(ByteArrayEscapeUtils.DELIMITER); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeValue Object", e); } } return out.toByteArray(); }
if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeSubTypeValue Object", e); if (null != subType) { try { out.write(ByteArrayEscapeUtils.escape(subType.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the SubType from TypeSubTypeValue Object", e); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeSubTypeValue Object", e);
@Test public void testCanSerialiseDeserialiseCorrectlyAndBeEscaped() throws SerialisationException { TypeValue typeValue = new TypeValue("testType", "testValue"); byte[] bytes = ByteArrayEscapeUtils.escape(serialiser.serialise(typeValue)); String serialisedForm = new String(bytes); assertEquals("testType\1\1testValue", serialisedForm); TypeValue deSerialisedTypeValue = serialiser.deserialise(ByteArrayEscapeUtils .unEscape(bytes)); assertEquals(typeValue.getType(), deSerialisedTypeValue.getType()); assertEquals(typeValue.getValue(), deSerialisedTypeValue.getValue()); assertEquals(typeValue, deSerialisedTypeValue); }
@Test public void testCanSerialiseDeserialiseCorrectlyAndBeEscaped() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue("testType", "testSubType", "testValue"); byte[] bytes = ByteArrayEscapeUtils.escape(serialiser.serialise(typeSubTypeValue)); String serialisedForm = new String(bytes); assertEquals("testType\1\1testSubType\1\1testValue", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(ByteArrayEscapeUtils.unEscape(bytes)); assertEquals(typeSubTypeValue.getType(), deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertEquals(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
/** * Escapes the provided byte[] so that it no longer contains the * Constants.DELIMITER character. * After escaping the byte[] the appendAfterEscaping[] is appended unescaped. * * @param bytes the byte array to escape * @param appendAfterEscaping the bytes to append to the array after being escaped. * @return the escaped byte array */ public static byte[] escape(final byte[] bytes, final byte... appendAfterEscaping) { final byte[] temp = new byte[(2 * bytes.length) + ((null == appendAfterEscaping) ? 0 : appendAfterEscaping.length)]; int currentPosition = escape(bytes, temp, 0); if (null != appendAfterEscaping) { for (final byte b : appendAfterEscaping) { temp[currentPosition++] = b; } } return Arrays.copyOfRange(temp, 0, currentPosition); }
@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }
@Override public byte[] serialise(final TypeValue typeValue) throws SerialisationException { String type = typeValue.getType(); String value = typeValue.getValue(); if ((null == type || type.isEmpty()) && (null == value || value.isEmpty())) { throw new SerialisationException("TypeValue passed to serialiser is blank"); } ByteArrayOutputStream out = new ByteArrayOutputStream(); if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeValue Object", e); } } out.write(ByteArrayEscapeUtils.DELIMITER); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeValue Object", e); } } return out.toByteArray(); }
if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeSubTypeValue Object", e); if (null != subType) { try { out.write(ByteArrayEscapeUtils.escape(subType.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the SubType from TypeSubTypeValue Object", e); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeSubTypeValue Object", e);
@Override public byte[] serialiseVertex(final Object vertex) { try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex)); } catch (final SerialisationException e) { throw new AccumuloElementConversionException( "Failed to serialise given identifier object for use in the bloom filter", e); } }
protected byte[] getSerialisedDestination(final Edge edge) { try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(edge.getDestination())); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to serialise Edge Destination", e); } }
protected byte[] getSerialisedSource(final Edge edge) { try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(edge.getSource())); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to serialise Edge Source", e); } }
@Override protected byte[] getRowKeyFromEntity(final Entity entity) { // No Delimiters but need to escape bytes // because later we check how many delimiter characters there are try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(entity.getVertex())); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to serialise Entity Identifier", e); } }
@Override protected byte[] getRowKeyFromEntity(final Entity entity) { try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(entity.getVertex()), ByteArrayEscapeUtils.DELIMITER, ByteEntityPositions.ENTITY); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to serialise Entity Identifier", e); } }
protected Key getKeyFromEdgeId(final Object source, final Object destination, final boolean directed, final boolean endKey) throws RangeFactoryException { final ToBytesSerialiser vertexSerialiser = (ToBytesSerialiser) schema.getVertexSerialiser(); final byte directionFlag = directed ? ByteEntityPositions.CORRECT_WAY_DIRECTED_EDGE : ByteEntityPositions.UNDIRECTED_EDGE; byte[] sourceValue; try { sourceValue = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(source)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise Edge Source", e); } byte[] destinationValue; try { destinationValue = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(destination)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise Edge Destination", e); } byte[] key = getKey(endKey, directionFlag, sourceValue, destinationValue); return new Key(key, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, Long.MAX_VALUE); }
serialisedVertex = ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise identifier", e);
source = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(sourceVal)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise Edge Source", e); destination = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(destVal)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise Edge Destination", e);
serialisedVertex = ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex)); } catch (final SerialisationException e) { throw new RangeFactoryException("Failed to serialise identifier", e);