Search in sources :

Example 6 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SerialisationFactoryTest method shouldReturnSerialiserForAnInteger.

@Test
public void shouldReturnSerialiserForAnInteger() throws SerialisationException {
    // Given
    final SerialisationFactory factory = new SerialisationFactory();
    final Class<?> clazz = Integer.class;
    // When
    final Serialisation serialiser = factory.getSerialiser(clazz);
    // Then
    assertTrue(serialiser.canHandle(clazz));
    assertEquals(CompactRawIntegerSerialiser.class, serialiser.getClass());
}
Also used : Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Test(org.junit.Test)

Example 7 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SerialisationFactoryTest method shouldReturnSerialiserForAString.

@Test
public void shouldReturnSerialiserForAString() throws SerialisationException {
    // Given
    final SerialisationFactory factory = new SerialisationFactory();
    final Class<?> clazz = String.class;
    // When
    final Serialisation serialiser = factory.getSerialiser(clazz);
    // Then
    assertTrue(serialiser.canHandle(clazz));
    assertEquals(StringSerialiser.class, serialiser.getClass());
}
Also used : Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Test(org.junit.Test)

Example 8 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getPropertiesFromColumnVisibility.

@Override
public Properties getPropertiesFromColumnVisibility(final String group, final byte[] columnVisibility) throws AccumuloElementConversionException {
    final Properties properties = new Properties();
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new AccumuloElementConversionException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    if (null != schema.getVisibilityProperty()) {
        final TypeDefinition propertyDef = elementDefinition.getPropertyTypeDef(schema.getVisibilityProperty());
        if (null != propertyDef) {
            final Serialisation serialiser = propertyDef.getSerialiser();
            try {
                if (columnVisibility == null || columnVisibility.length == 0) {
                    final Object value = serialiser.deserialiseEmptyBytes();
                    if (value != null) {
                        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;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Properties(uk.gov.gchq.gaffer.data.element.Properties) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Example 9 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class ClassicRangeFactory method getKeyFromEdgeSeed.

@Override
protected <T extends GetElementsOperation<?, ?>> Key getKeyFromEdgeSeed(final EdgeSeed seed, final T operation, final boolean endKey) throws RangeFactoryException {
    final byte directionFlag1 = seed.isDirected() ? (operation.getIncludeIncomingOutGoing() == IncludeIncomingOutgoingType.INCOMING ? ClassicBytePositions.INCORRECT_WAY_DIRECTED_EDGE : ClassicBytePositions.CORRECT_WAY_DIRECTED_EDGE) : ClassicBytePositions.UNDIRECTED_EDGE;
    final Serialisation vertexSerialiser = schema.getVertexSerialiser();
    // Serialise source and destination to byte arrays, escaping if
    // necessary
    byte[] source;
    try {
        source = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(seed.getSource()));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Source", e);
    }
    byte[] destination;
    try {
        destination = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(seed.getDestination()));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Destination", e);
    }
    // Length of row key is the length of the source
    // plus the length of the destination
    // plus one for the delimiter in between the source and destination
    // plus one for the delimiter in between the destination and the direction flag
    // plus one for the direction flag at the end.
    byte[] key;
    if (endKey) {
        key = new byte[source.length + destination.length + 4];
        key[key.length - 3] = ByteArrayEscapeUtils.DELIMITER;
        key[key.length - 2] = directionFlag1;
        key[key.length - 1] = ByteArrayEscapeUtils.DELIMITER_PLUS_ONE;
    } else {
        key = new byte[source.length + destination.length + 3];
        key[key.length - 2] = ByteArrayEscapeUtils.DELIMITER;
        key[key.length - 1] = directionFlag1;
    }
    // Create first key: source DELIMITER destination DELIMITER (CORRECT_WAY_DIRECTED_EDGE or UNDIRECTED_EDGE)
    // Here if we desire an EdgeID we and the user has asked for incoming
    // edges only for related items, we put the destination first so we find the flipped edge's key,
    // this key will pass the filter iterators check for incoming edges, but
    // the result will be flipped back to the correct edge on the client end conversion
    // Simply put when looking up an EDGE ID that ID counts as both incoming
    // and outgoing so we use the reversed key when looking up incoming.
    byte[] firstValue;
    byte[] secondValue;
    if (operation.getIncludeIncomingOutGoing() == IncludeIncomingOutgoingType.INCOMING) {
        firstValue = destination;
        secondValue = source;
    } else {
        firstValue = source;
        secondValue = destination;
    }
    System.arraycopy(firstValue, 0, key, 0, firstValue.length);
    key[firstValue.length] = ByteArrayEscapeUtils.DELIMITER;
    System.arraycopy(secondValue, 0, key, firstValue.length + 1, secondValue.length);
    return new Key(key, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, Long.MAX_VALUE);
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Key(org.apache.accumulo.core.data.Key) RangeFactoryException(uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)

Example 10 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getValueFromProperties.

@Override
public Value getValueFromProperties(final String group, final Properties properties) throws AccumuloElementConversionException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new AccumuloElementConversionException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    final Iterator<String> propertyNames = elementDefinition.getProperties().iterator();
    String propertyName;
    while (propertyNames.hasNext()) {
        propertyName = propertyNames.next();
        final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
        if (isStoredInValue(propertyName, elementDefinition)) {
            final Serialisation serialiser = (typeDefinition != null) ? typeDefinition.getSerialiser() : null;
            try {
                if (null != serialiser) {
                    Object value = properties.get(propertyName);
                    if (null != value) {
                        final byte[] bytes = serialiser.serialise(value);
                        writeBytes(bytes, out);
                    } else {
                        final byte[] bytes = serialiser.serialiseNull();
                        writeBytes(bytes, out);
                    }
                } else {
                    writeBytes(AccumuloStoreConstants.EMPTY_BYTES, out);
                }
            } catch (final IOException e) {
                throw new AccumuloElementConversionException("Failed to write serialise property to ByteArrayOutputStream" + propertyName, e);
            }
        }
    }
    return new Value(out.toByteArray());
}
Also used : Value(org.apache.accumulo.core.data.Value) Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Aggregations

Serialisation (uk.gov.gchq.gaffer.serialisation.Serialisation)15 Test (org.junit.Test)8 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)4 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)3 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Key (org.apache.accumulo.core.data.Key)2 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)2 SchemaException (uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Value (org.apache.accumulo.core.data.Value)1 Properties (uk.gov.gchq.gaffer.data.element.Properties)1 JavaSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser)1