private static <T> T getValue(final ToBytesSerialiser<T> serialiser, final byte[] valueBytes) throws SerialisationException { if (0 == valueBytes.length) { return serialiser.deserialiseEmpty(); } return serialiser.deserialise(valueBytes); }
/** * @param allBytes The bytes to be decoded into characters * @param offset The index of the first byte to decode * @param length The number of bytes to decode * @return T the deserialised object * @throws SerialisationException issues during deserialisation */ default T deserialise(final byte[] allBytes, final int offset, final int length) throws SerialisationException { final byte[] selection = new byte[length]; try { System.arraycopy(allBytes, offset, selection, 0, length); } catch (final NullPointerException e) { throw new SerialisationException(String.format("Deserialising with giving range caused ArrayIndexOutOfBoundsException. byte[].size:%d startPos:%d length:%d", allBytes.length, 0, length), e); } return deserialise(selection); }
public static <T> ObjectCarriage<T> deserialiseNextObject(final ToBytesSerialiser<T> serialiser, final int currentCarriage, final byte[] bytes) throws SerialisationException { int rtn = currentCarriage; int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[rtn]); int currentPropLength = getCurrentPropLength(bytes, rtn, numBytesForLength); int from = rtn += numBytesForLength; int to = rtn += currentPropLength; T object = serialiser.deserialise(Arrays.copyOfRange(bytes, from, to)); return new ObjectCarriage<T>(object, rtn); }
private Object getDeserialisedObject(final ToBytesSerialiser serialiser, final byte[] bytes, final int from, final int length) throws SerialisationException { //Don't initialise with #deserialiseEmpty() as this might initialise an complex empty structure to be immediately overwritten e.g. TreeSet<String> Object deserialisedObject; if (length > 0) { deserialisedObject = serialiser.deserialise(bytes, from, length); } else { deserialisedObject = serialiser.deserialiseEmpty(); } return deserialisedObject; }
@Override public Object deserialise(final byte[] bytes) throws SerialisationException { try { byte keyByte = bytes[0]; ToBytesSerialiser serialiser = nullCheck(supportedSerialisers.getSerialiserFromKey(keyByte)); return serialiser.deserialise(bytes, 1, bytes.length - 1); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }
private static <T> T getValue(final ToBytesSerialiser<T> serialiser, final byte[] valueBytes) throws SerialisationException { if (0 == valueBytes.length) { return serialiser.deserialiseEmpty(); } return serialiser.deserialise(valueBytes); }
private void test(final long value) throws SerialisationException { final byte[] b = serialiser.serialise(value); final Object o = ((ToBytesSerialiser) serialiser).deserialise(b, 0, b.length); assertEquals(Long.class, o.getClass()); assertEquals(value, o); final ByteArrayOutputStream stream = new ByteArrayOutputStream(); CompactRawSerialisationUtils.write(value, new DataOutputStream(stream)); final long result = CompactRawSerialisationUtils.read(new DataInputStream(new ByteArrayInputStream(stream.toByteArray()))); assertEquals(result, value); }
@Override public Properties getPropertiesFromColumnVisibility(final String group, final byte[] columnVisibility) { final Properties properties = new Properties(); final SchemaElementDefinition elementDefinition = getSchemaElementDefinition(group); if (null != schema.getVisibilityProperty()) { final TypeDefinition propertyDef = elementDefinition.getPropertyTypeDef(schema.getVisibilityProperty()); if (null != propertyDef) { final ToBytesSerialiser serialiser = (ToBytesSerialiser) propertyDef.getSerialiser(); try { if (null == columnVisibility || columnVisibility.length == 0) { final Object value = serialiser.deserialiseEmpty(); if (null != value) { properties.put(schema.getVisibilityProperty(), value); } } else { properties.put(schema.getVisibilityProperty(), serialiser.deserialise(columnVisibility)); } } catch (final SerialisationException e) { throw new AccumuloElementConversionException(e.getMessage(), e); } } } return properties; }
/** * @param allBytes The bytes to be decoded into characters * @param offset The index of the first byte to decode * @param length The number of bytes to decode * @return T the deserialised object * @throws SerialisationException issues during deserialisation */ default T deserialise(final byte[] allBytes, final int offset, final int length) throws SerialisationException { final byte[] selection = new byte[length]; try { System.arraycopy(allBytes, offset, selection, 0, length); } catch (final NullPointerException e) { throw new SerialisationException(String.format("Deserialising with giving range caused ArrayIndexOutOfBoundsException. byte[].size:%d startPos:%d length:%d", allBytes.length, 0, length), e); } return deserialise(selection); }
public static <T> ObjectCarriage<T> deserialiseNextObject(final ToBytesSerialiser<T> serialiser, final int currentCarriage, final byte[] bytes) throws SerialisationException { int rtn = currentCarriage; int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[rtn]); int currentPropLength = getCurrentPropLength(bytes, rtn, numBytesForLength); int from = rtn += numBytesForLength; int to = rtn += currentPropLength; T object = serialiser.deserialise(Arrays.copyOfRange(bytes, from, to)); return new ObjectCarriage<T>(object, rtn); }
@Override public T read(final Kryo kryo, final Input input, final Class<T> type) { final int serialisedLength = input.readInt(); final byte[] serialised = input.readBytes(serialisedLength); try { return serialiser.deserialise(serialised); } catch (final SerialisationException e) { throw new GafferRuntimeException("Exception deserialising " + type.getSimpleName() + " to a byte array", e); } } }
@Override public Object deserialise(final byte[] bytes) throws SerialisationException { try { byte keyByte = bytes[0]; ToBytesSerialiser serialiser = nullCheck(supportedSerialisers.getSerialiserFromKey(keyByte)); return serialiser.deserialise(bytes, 1, bytes.length - 1); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }
@SuppressWarnings("unchecked") @Override protected EntityId getEntityId(final byte[] row) { try { return new EntitySeed(((ToBytesSerialiser) schema.getVertexSerialiser()) .deserialise(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(row, 0, row.length - 2)))); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to create EntityId from Accumulo row key", e); } }
protected EdgeId getEdgeId(final byte[] row, final boolean includeMatchedVertex) { final byte[][] result = new byte[2][]; final EdgeDirection direction = getSourceAndDestinationFromRowKey(row, result); final EdgeId.MatchedVertex matchedVertex; if (!includeMatchedVertex) { matchedVertex = null; } else if (EdgeDirection.DIRECTED_REVERSED == direction) { matchedVertex = EdgeId.MatchedVertex.DESTINATION; } else { matchedVertex = EdgeId.MatchedVertex.SOURCE; } try { return new EdgeSeed(((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[0]), ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[1]), direction.isDirected(), matchedVertex); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to create EdgeId from Accumulo row key", e); } }
@Override protected EntityId getEntityId(final byte[] row) { try { return new EntitySeed(((ToBytesSerialiser) schema.getVertexSerialiser()) .deserialise(ByteArrayEscapeUtils.unEscape(row))); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to create EntityId from Accumulo row key", e); } }
@Override protected Entity getEntityFromKey(final Key key, final byte[] row) { try { final Entity entity = new Entity(getGroupFromKey(key), ((ToBytesSerialiser) schema.getVertexSerialiser()) .deserialise(ByteArrayEscapeUtils.unEscape(row))); addPropertiesToElement(entity, key); return entity; } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to re-create Entity from key", e); } }
@Override protected Entity getEntityFromKey(final Key key, final byte[] row) { try { final Entity entity = new Entity(getGroupFromKey(key), ((ToBytesSerialiser) schema.getVertexSerialiser()) .deserialise(ByteArrayEscapeUtils.unEscape(row, 0, row.length - 2))); addPropertiesToElement(entity, key); return entity; } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to re-create Entity from key", e); } }
@SuppressWarnings("WeakerAccess") protected Edge getEdgeFromKey(final Key key, final byte[] row, final boolean includeMatchedVertex) { final byte[][] result = new byte[2][]; final EdgeDirection direction = getSourceAndDestinationFromRowKey(row, result); final EdgeId.MatchedVertex matchedVertex; if (!includeMatchedVertex) { matchedVertex = null; } else if (EdgeDirection.DIRECTED_REVERSED == direction) { matchedVertex = EdgeId.MatchedVertex.DESTINATION; } else { matchedVertex = EdgeId.MatchedVertex.SOURCE; } final String group = getGroupFromColumnFamily(key.getColumnFamilyData().getBackingArray()); try { final Edge edge = new Edge(group, ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[0]), ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[1]), direction.isDirected(), matchedVertex, null); addPropertiesToElement(edge, key); return edge; } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to re-create Edge from key", e); } }