Search in sources :

Example 1 with Element

use of uk.gov.gchq.gaffer.data.element.Element in project Gaffer by gchq.

the class ElementFilterTest method shouldWrapElementInElementTupleAndCallSuper.

@Test
public void shouldWrapElementInElementTupleAndCallSuper() {
    // Given
    final String reference = "reference1";
    final String value = "value";
    final ElementFilter filter = new ElementFilter();
    final ConsumerFunctionContext<String, FilterFunction> functionContext1 = mock(ConsumerFunctionContext.class);
    final FilterFunction function = mock(FilterFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    filter.addFunction(functionContext1);
    final Element element = mock(Element.class);
    given(element.getProperty(reference)).willReturn(value);
    final ArgumentCaptor<ElementTuple> elementTupleCaptor = ArgumentCaptor.forClass(ElementTuple.class);
    given(functionContext1.select(elementTupleCaptor.capture())).willReturn(new Object[] { value });
    // When
    filter.filter(element);
    // Then
    assertSame(element, elementTupleCaptor.getValue().getElement());
    verify(functionContext1).getFunction();
    final ArgumentCaptor<Object[]> argumentCaptor = ArgumentCaptor.forClass(Object[].class);
    verify(function).isValid(argumentCaptor.capture());
    assertEquals(value, argumentCaptor.getValue()[0]);
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) Element(uk.gov.gchq.gaffer.data.element.Element) ElementTuple(uk.gov.gchq.gaffer.data.element.ElementTuple) Test(org.junit.Test)

Example 2 with Element

use of uk.gov.gchq.gaffer.data.element.Element in project Gaffer by gchq.

the class AccumuloIDWithinSetRetrieverTest method returnElementsFromOperation.

private Set<Element> returnElementsFromOperation(final AccumuloStore store, final GetElements operation, final User user, final boolean loadIntoMemory) throws StoreException {
    final AccumuloRetriever<?> retriever = new AccumuloIDWithinSetRetriever(store, operation, user, loadIntoMemory);
    final Set<Element> results = new HashSet<>();
    for (final Element elm : retriever) {
        results.add(elm);
    }
    retriever.close();
    return results;
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) HashSet(java.util.HashSet)

Example 3 with Element

use of uk.gov.gchq.gaffer.data.element.Element in project Gaffer by gchq.

the class GetJavaRDDOfElementsExample method getJavaRddOfElements.

public void getJavaRddOfElements(final JavaSparkContext sc, final Graph graph) throws OperationException {
    ROOT_LOGGER.setLevel(Level.INFO);
    // Avoid using getMethodNameAsSentence as it messes up the formatting of the "RDD" part
    log("#### get Java RDD of elements\n");
    printGraph();
    ROOT_LOGGER.setLevel(Level.OFF);
    final GetJavaRDDOfElements<ElementSeed> operation = new GetJavaRDDOfElements.Builder<>().addSeed(new EdgeSeed(1, 2, true)).addSeed(new EdgeSeed(2, 3, true)).javaSparkContext(sc).build();
    final JavaRDD<Element> rdd = graph.execute(operation, new User("user01"));
    final List<Element> elements = rdd.collect();
    ROOT_LOGGER.setLevel(Level.INFO);
    printJava("GetJavaRDDOfElements<ElementSeed> operation = new GetJavaRDDOfElements.Builder<>()\n" + "                .addSeed(new EdgeSeed(1, 2, true))\n" + "                .addSeed(new EdgeSeed(2, 3, true))\n" + "                .javaSparkContext(sc)\n" + "                .build();\n" + "JavaRDD<Element> rdd = graph.execute(operation, new User(\"user01\"));\n" + "List<Element> elements = rdd.collect();");
    log("The results are:");
    log("```");
    for (final Element e : elements) {
        log(e.toString());
    }
    log("```");
    ROOT_LOGGER.setLevel(Level.OFF);
}
Also used : User(uk.gov.gchq.gaffer.user.User) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) Element(uk.gov.gchq.gaffer.data.element.Element) ElementSeed(uk.gov.gchq.gaffer.operation.data.ElementSeed)

Example 4 with Element

use of uk.gov.gchq.gaffer.data.element.Element 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);
    }
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) Value(org.apache.accumulo.core.data.Value) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Key(org.apache.accumulo.core.data.Key) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) GafferRuntimeException(uk.gov.gchq.gaffer.core.exception.GafferRuntimeException)

Example 5 with Element

use of uk.gov.gchq.gaffer.data.element.Element in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getFullElement.

@Override
public Element getFullElement(final Key key, final Value value, final Map<String, String> options) throws AccumuloElementConversionException {
    final Element element = getElementFromKey(key, options);
    element.copyProperties(getPropertiesFromValue(element.getGroup(), value));
    return element;
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element)

Aggregations

Element (uk.gov.gchq.gaffer.data.element.Element)581 Test (org.junit.jupiter.api.Test)285 Entity (uk.gov.gchq.gaffer.data.element.Entity)202 Edge (uk.gov.gchq.gaffer.data.element.Edge)199 ArrayList (java.util.ArrayList)149 User (uk.gov.gchq.gaffer.user.User)144 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)137 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)120 Graph (uk.gov.gchq.gaffer.graph.Graph)117 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)117 HashSet (java.util.HashSet)110 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)101 Test (org.junit.Test)77 Schema (uk.gov.gchq.gaffer.store.schema.Schema)74 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)68 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)67 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)66 List (java.util.List)64 OperationException (uk.gov.gchq.gaffer.operation.OperationException)55 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)53