Search in sources :

Example 6 with ToBytesSerialiser

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

the class AbstractCoreKeyAccumuloElementConverter method serialiseSizeAndPropertyValue.

protected void serialiseSizeAndPropertyValue(final String propertyName, final SchemaElementDefinition elementDefinition, final Properties properties, final ByteArrayOutputStream stream) {
    try {
        final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
        final ToBytesSerialiser serialiser = (null == typeDefinition) ? null : (ToBytesSerialiser) typeDefinition.getSerialiser();
        byte[] bytes;
        if (null == serialiser) {
            bytes = AccumuloStoreConstants.EMPTY_BYTES;
        } else {
            Object value = properties.get(propertyName);
            // serialiseNull could be different to AccumuloStoreConstants.EMPTY_BYTES
            bytes = (null == value) ? serialiser.serialiseNull() : serialiser.serialise(value);
        }
        writeBytes(bytes, stream);
    } catch (final IOException e) {
        throw new AccumuloElementConversionException("Failed to write serialised property to ByteArrayOutputStream" + propertyName, e);
    }
}
Also used : ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) IOException(java.io.IOException) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 7 with ToBytesSerialiser

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

the class ElementSerialisation method getValue.

public byte[] getValue(final String group, final Properties properties) throws SerialisationException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new SerialisationException("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 ToBytesSerialiser serialiser = (null != typeDefinition) ? (ToBytesSerialiser) 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(HBaseStoreConstants.EMPTY_BYTES, out);
                }
            } catch (final IOException e) {
                throw new SerialisationException("Failed to write serialise property to ByteArrayOutputStream" + propertyName, e);
            }
        }
    }
    return out.toByteArray();
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) IOException(java.io.IOException) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Example 8 with ToBytesSerialiser

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

the class ElementSerialisation method getPropertiesFromValue.

public Properties getPropertiesFromValue(final String group, final byte[] value) throws SerialisationException {
    final Properties properties = new Properties();
    if (null == value || value.length == 0) {
        return properties;
    }
    int lastDelimiter = 0;
    final int arrayLength = value.length;
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new SerialisationException("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();
    while (propertyNames.hasNext() && lastDelimiter < arrayLength) {
        final String propertyName = propertyNames.next();
        if (isStoredInValue(propertyName, elementDefinition)) {
            final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
            final ToBytesSerialiser serialiser = (null != typeDefinition) ? (ToBytesSerialiser) typeDefinition.getSerialiser() : null;
            if (null != serialiser) {
                final int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(value[lastDelimiter]);
                int currentPropLength;
                try {
                    // value is never larger than int.
                    currentPropLength = (int) CompactRawSerialisationUtils.readLong(value, lastDelimiter);
                } catch (final SerialisationException e) {
                    throw new SerialisationException("Exception reading length of property");
                }
                lastDelimiter += numBytesForLength;
                if (currentPropLength > 0) {
                    try {
                        properties.put(propertyName, serialiser.deserialise(value, lastDelimiter, currentPropLength));
                        lastDelimiter += currentPropLength;
                    } catch (final SerialisationException e) {
                        throw new SerialisationException("Failed to deserialise property " + propertyName, e);
                    }
                } else {
                    try {
                        properties.put(propertyName, serialiser.deserialiseEmpty());
                    } catch (final SerialisationException e) {
                        throw new SerialisationException("Failed to deserialise property " + propertyName, e);
                    }
                }
            } else {
                LOGGER.warn("No serialiser found in schema for property {} in group {}", propertyName, group);
            }
        }
    }
    return properties;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Properties(uk.gov.gchq.gaffer.data.element.Properties) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Example 9 with ToBytesSerialiser

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

the class ElementSerialisation method getColumnQualifier.

public byte[] getColumnQualifier(final String group, final Properties properties) throws SerialisationException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new SerialisationException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    try {
        final byte[] groupBytes = Bytes.toBytes(group);
        writeBytes(groupBytes, out);
    } catch (final IOException e) {
        throw new SerialisationException("Failed to serialise group to ByteArrayOutputStream", e);
    }
    for (final String propertyName : elementDefinition.getGroupBy()) {
        final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
        final ToBytesSerialiser serialiser = (null != typeDefinition) ? (ToBytesSerialiser) 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(HBaseStoreConstants.EMPTY_BYTES, out);
            }
        } catch (final IOException e) {
            throw new SerialisationException("Failed to write serialise property to ByteArrayOutputStream" + propertyName, e);
        }
    }
    return out.toByteArray();
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Example 10 with ToBytesSerialiser

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

the class ElementSerialisation method getColumnVisibility.

public byte[] getColumnVisibility(final String group, final Properties properties) throws SerialisationException {
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new SerialisationException("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 Object property = properties.get(schema.getVisibilityProperty());
            final ToBytesSerialiser serialiser = (ToBytesSerialiser) propertyDef.getSerialiser();
            if (null != property) {
                try {
                    return serialiser.serialise(property);
                } catch (final SerialisationException e) {
                    throw new SerialisationException(e.getMessage(), e);
                }
            } else {
                return serialiser.serialiseNull();
            }
        }
    }
    return HBaseStoreConstants.EMPTY_BYTES;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Aggregations

ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)23 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)14 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)10 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)7 IOException (java.io.IOException)4 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)4 Test (org.junit.jupiter.api.Test)3 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)3 Properties (uk.gov.gchq.gaffer.data.element.Properties)3 Serialiser (uk.gov.gchq.gaffer.serialisation.Serialiser)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Key (org.apache.accumulo.core.data.Key)2 SeedMatching (uk.gov.gchq.gaffer.operation.SeedMatching)2 LengthValueBytesSerialiserUtil (uk.gov.gchq.gaffer.serialisation.util.LengthValueBytesSerialiserUtil)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 Range (org.apache.accumulo.core.data.Range)1