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);
}
}
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();
}
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;
}
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();
}
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;
}
Aggregations