use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.
the class ByteEntityAccumuloElementConverter method getSourceAndDestinationFromRowKey.
@Override
protected boolean getSourceAndDestinationFromRowKey(final byte[] rowKey, final byte[][] sourceDestValues, final Map<String, String> options) throws AccumuloElementConversionException {
// Get element class, sourceValue, destinationValue and directed flag from row key
// Expect to find 3 delimiters (4 fields)
final int[] positionsOfDelimiters = new int[3];
short numDelims = 0;
// Last byte will be directional flag so don't count it
for (int i = 0; i < rowKey.length - 1; ++i) {
if (rowKey[i] == ByteArrayEscapeUtils.DELIMITER) {
if (numDelims >= 3) {
throw new AccumuloElementConversionException("Too many delimiters found in row key - found more than the expected 3.");
}
positionsOfDelimiters[numDelims++] = i;
}
}
if (numDelims != 3) {
throw new AccumuloElementConversionException("Wrong number of delimiters found in row key - found " + numDelims + ", expected 3.");
}
// If edge is undirected then create edge
// (no need to worry about which direction the vertices should go in).
// If the edge is directed then need to decide which way round the vertices should go.
byte directionFlag;
try {
directionFlag = rowKey[rowKey.length - 1];
} catch (final NumberFormatException e) {
throw new AccumuloElementConversionException("Error parsing direction flag from row key - " + e);
}
if (directionFlag == ByteEntityPositions.UNDIRECTED_EDGE) {
// Edge is undirected
sourceDestValues[0] = getSourceBytes(rowKey, positionsOfDelimiters);
sourceDestValues[1] = getDestBytes(rowKey, positionsOfDelimiters);
return false;
} else if (directionFlag == ByteEntityPositions.CORRECT_WAY_DIRECTED_EDGE) {
// Edge is directed and the first identifier is the source of the edge
sourceDestValues[0] = getSourceBytes(rowKey, positionsOfDelimiters);
sourceDestValues[1] = getDestBytes(rowKey, positionsOfDelimiters);
return true;
} else if (directionFlag == ByteEntityPositions.INCORRECT_WAY_DIRECTED_EDGE) {
// Edge is directed and the second identifier is the source of the edge
int src = 1;
int dst = 0;
if (matchEdgeSource(options)) {
src = 0;
dst = 1;
}
sourceDestValues[src] = getSourceBytes(rowKey, positionsOfDelimiters);
sourceDestValues[dst] = getDestBytes(rowKey, positionsOfDelimiters);
return true;
} else {
throw new AccumuloElementConversionException("Invalid direction flag in row key - flag was " + directionFlag);
}
}
use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.
the class AccumuloStore method insertGraphElements.
protected void insertGraphElements(final Iterable<Element> elements) throws StoreException {
// Create BatchWriter
final BatchWriter writer = TableUtils.createBatchWriter(this);
// too high a latency, etc.
if (elements != null) {
for (final Element element : elements) {
final Pair<Key> keys;
try {
keys = keyPackage.getKeyConverter().getKeysFromElement(element);
} catch (final AccumuloElementConversionException e) {
LOGGER.error("Failed to create an accumulo key from element of type " + element.getGroup() + " when trying to insert elements");
continue;
}
final Value value;
try {
value = keyPackage.getKeyConverter().getValueFromElement(element);
} catch (final AccumuloElementConversionException e) {
LOGGER.error("Failed to create an accumulo value from element of type " + element.getGroup() + " when trying to insert elements");
continue;
}
final Mutation m = new Mutation(keys.getFirst().getRow());
m.put(keys.getFirst().getColumnFamily(), keys.getFirst().getColumnQualifier(), new ColumnVisibility(keys.getFirst().getColumnVisibility()), keys.getFirst().getTimestamp(), value);
try {
writer.addMutation(m);
} catch (final MutationsRejectedException e) {
LOGGER.error("Failed to create an accumulo key mutation");
continue;
}
// If the GraphElement is an Edge then there will be 2 keys.
if (keys.getSecond() != null) {
final Mutation m2 = new Mutation(keys.getSecond().getRow());
m2.put(keys.getSecond().getColumnFamily(), keys.getSecond().getColumnQualifier(), new ColumnVisibility(keys.getSecond().getColumnVisibility()), keys.getSecond().getTimestamp(), value);
try {
writer.addMutation(m2);
} catch (final MutationsRejectedException e) {
LOGGER.error("Failed to create an accumulo key mutation");
}
}
}
} else {
throw new GafferRuntimeException("Could not find any elements to add to graph.", Status.BAD_REQUEST);
}
try {
writer.close();
} catch (final MutationsRejectedException e) {
LOGGER.warn("Accumulo batch writer failed to close", e);
}
}
use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.
the class ByteEntityAccumuloElementConverter method getRowKeyFromEntity.
@Override
protected byte[] getRowKeyFromEntity(final Entity entity) throws AccumuloElementConversionException {
byte[] value;
try {
value = ByteArrayEscapeUtils.escape(getVertexSerialiser().serialise(entity.getVertex()));
final byte[] returnVal = Arrays.copyOf(value, value.length + 2);
returnVal[returnVal.length - 2] = ByteArrayEscapeUtils.DELIMITER;
returnVal[returnVal.length - 1] = ByteEntityPositions.ENTITY;
return returnVal;
} catch (final SerialisationException e) {
throw new AccumuloElementConversionException("Failed to serialise Entity Identifier", e);
}
}
use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.
the class ByteEntityAccumuloElementConverter method getEntityFromKey.
@Override
protected Entity getEntityFromKey(final Key key) throws AccumuloElementConversionException {
try {
final Entity entity = new Entity(getGroupFromKey(key), getVertexSerialiser().deserialise(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(key.getRowData().getBackingArray(), 0, (key.getRowData().getBackingArray().length) - 2))));
addPropertiesToElement(entity, key);
return entity;
} catch (final SerialisationException e) {
throw new AccumuloElementConversionException("Failed to re-create Entity from key", e);
}
}
use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.
the class ClassicAccumuloElementConverter method getEntityFromKey.
@Override
protected Entity getEntityFromKey(final Key key) throws AccumuloElementConversionException {
try {
final Entity entity = new Entity(getGroupFromKey((key)), getVertexSerialiser().deserialise(ByteArrayEscapeUtils.unEscape(key.getRowData().getBackingArray())));
addPropertiesToElement(entity, key);
return entity;
} catch (final SerialisationException e) {
throw new AccumuloElementConversionException("Failed to re-create Entity from key", e);
}
}
Aggregations