Search in sources :

Example 6 with Pipe

use of com.tinkerpop.pipes.Pipe in project incubator-atlas by apache.

the class BaseQuery method executeQuery.

private List<Vertex> executeQuery() {
    GremlinPipeline pipeline = buildPipeline().as("root");
    Pipe expressionPipe = queryExpression.asPipe();
    // AlwaysQuery returns null for pipe
    return expressionPipe == null ? pipeline.toList() : pipeline.add(expressionPipe).back("root").toList();
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) Pipe(com.tinkerpop.pipes.Pipe) PropertyFilterPipe(com.tinkerpop.pipes.filter.PropertyFilterPipe)

Example 7 with Pipe

use of com.tinkerpop.pipes.Pipe in project incubator-atlas by apache.

the class AtlasEntityQueryTest method testExecute_Collection_update.

@Test
public void testExecute_Collection_update() throws Exception {
    AtlasGraph graph = createStrictMock(AtlasGraph.class);
    QueryExpression expression = createStrictMock(QueryExpression.class);
    ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
    Request request = createStrictMock(Request.class);
    GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
    Pipe queryPipe = createStrictMock(Pipe.class);
    Pipe expressionPipe = createStrictMock(Pipe.class);
    Pipe notDeletedPipe = createStrictMock(Pipe.class);
    GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
    Vertex vertex1 = createStrictMock(Vertex.class);
    VertexWrapper vertex1Wrapper = createStrictMock(VertexWrapper.class);
    Capture<Long> modifiedTimestampCapture = newCapture();
    List<Vertex> results = new ArrayList<>();
    results.add(vertex1);
    Map<String, Object> vertex1PropertyMap = new HashMap<>();
    vertex1PropertyMap.put("prop1", "prop1.value1");
    vertex1PropertyMap.put("prop2", "prop2.value1");
    Map<String, Object> filteredVertex1PropertyMap = new HashMap<>();
    filteredVertex1PropertyMap.put("prop1", "prop1.value1");
    Map<String, Object> updateProperties = new HashMap<>();
    updateProperties.put("prop3", "newValue");
    // mock expectations
    expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
    expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
    expect(initialPipeline.as("root")).andReturn(rootPipeline);
    expect(expression.asPipe()).andReturn(expressionPipe);
    expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
    expect(expressionPipeline.back("root")).andReturn(rootPipeline);
    expect(rootPipeline.toList()).andReturn(results);
    graph.commit();
    vertex1Wrapper.setProperty("prop3", "newValue");
    vertex1Wrapper.setProperty(eq(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY), capture(modifiedTimestampCapture));
    expect(vertex1Wrapper.getPropertyMap()).andReturn(vertex1PropertyMap);
    expect(resourceDefinition.filterProperties(request, vertex1PropertyMap)).andReturn(filteredVertex1PropertyMap);
    expect(resourceDefinition.resolveHref(filteredVertex1PropertyMap)).andReturn("/foo/bar");
    expect(request.getCardinality()).andReturn(Request.Cardinality.COLLECTION);
    replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
    // end mock expectations
    AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, vertex1Wrapper);
    long startTime = System.currentTimeMillis();
    // invoke method being tested
    Collection<Map<String, Object>> queryResults = query.execute(updateProperties);
    long endTime = System.currentTimeMillis();
    assertEquals(queryResults.size(), 1);
    Map<String, Object> queryResultMap = queryResults.iterator().next();
    assertEquals(queryResultMap.size(), 2);
    assertEquals(queryResultMap.get("prop1"), "prop1.value1");
    assertEquals(queryResultMap.get("href"), "/foo/bar");
    long modifiedTimestamp = modifiedTimestampCapture.getValue();
    assertTrue(modifiedTimestamp >= startTime && modifiedTimestamp <= endTime);
    verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) Request(org.apache.atlas.catalog.Request) Pipe(com.tinkerpop.pipes.Pipe) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) Test(org.testng.annotations.Test)

Example 8 with Pipe

use of com.tinkerpop.pipes.Pipe in project incubator-atlas by apache.

the class AtlasEntityQueryTest method testExecute_Collection_rollbackOnException.

@Test
public void testExecute_Collection_rollbackOnException() throws Exception {
    AtlasGraph graph = createStrictMock(AtlasGraph.class);
    QueryExpression expression = createStrictMock(QueryExpression.class);
    ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
    Request request = createStrictMock(Request.class);
    GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
    Pipe queryPipe = createStrictMock(Pipe.class);
    Pipe expressionPipe = createStrictMock(Pipe.class);
    Pipe notDeletedPipe = createStrictMock(Pipe.class);
    GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
    // mock expectations
    expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
    expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
    expect(initialPipeline.as("root")).andReturn(rootPipeline);
    expect(expression.asPipe()).andReturn(expressionPipe);
    expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
    expect(expressionPipeline.back("root")).andReturn(rootPipeline);
    expect(rootPipeline.toList()).andThrow(new RuntimeException("something bad happened"));
    graph.rollback();
    replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline);
    // end mock expectations
    AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, null);
    try {
        // invoke method being tested
        query.execute();
        fail("expected exception");
    } catch (RuntimeException e) {
        assertEquals(e.getMessage(), "something bad happened");
    }
    verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline);
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) Request(org.apache.atlas.catalog.Request) Pipe(com.tinkerpop.pipes.Pipe) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) Test(org.testng.annotations.Test)

Example 9 with Pipe

use of com.tinkerpop.pipes.Pipe in project frames by tinkerpop.

the class GremlinGroovyAnnotationHandler method processVertex.

public Object processVertex(final GremlinGroovy annotation, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex vertex) {
    try {
        final CompiledScript script = this.engine.compile(annotation.value());
        final Bindings bindings = getBindings(method, arguments);
        bindings.put(IT, vertex);
        bindings.put(G, framedGraph);
        final Object result = script.eval(bindings);
        // TODO: Deprecate the use of _() and replace with it
        if (result instanceof Pipe & annotation.value().startsWith(PIPE)) {
            LOGGER.warning("_() is deprecated in favor of using 'it' to represent the framed vertex");
            ((Pipe) result).setStarts(new SingleIterator<Element>(vertex));
        }
        if (annotation.frame()) {
            if (result instanceof Iterable) {
                final FramedVertexIterable r = new FramedVertexIterable(framedGraph, (Iterable) result, ClassUtilities.getGenericClass(method));
                return (ClassUtilities.returnsIterable(method)) ? r : r.iterator().hasNext() ? r.iterator().next() : null;
            } else if (ClassUtilities.returnsMap(method)) {
                return new FramedVertexMap(framedGraph, (Map) result, ClassUtilities.getGenericClass(method));
            } else if (result instanceof Vertex) {
                return framedGraph.frame((Vertex) result, ClassUtilities.getGenericClass(method));
            } else {
                throw new IllegalStateException("The returned object can not be framed: " + result.getClass());
            }
        } else {
            return result;
        }
    } catch (ScriptException e) {
        //Preserve original exception functionality.
        ExceptionUtils.sneakyThrow(e);
        return null;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) Vertex(com.tinkerpop.blueprints.Vertex) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Element(com.tinkerpop.blueprints.Element) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap) Pipe(com.tinkerpop.pipes.Pipe) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Map(java.util.Map) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap)

Example 10 with Pipe

use of com.tinkerpop.pipes.Pipe in project gremlin by tinkerpop.

the class GremlinFluentUtility method optimizePipelineForGraphQuery.

private static boolean optimizePipelineForGraphQuery(final GremlinPipeline pipeline, final Pipe pipe) {
    GraphQueryPipe queryPipe = null;
    for (int i = pipeline.size() - 1; i > 0; i--) {
        final Pipe temp = pipeline.get(i);
        if (temp instanceof GraphQueryPipe) {
            queryPipe = (GraphQueryPipe) temp;
            break;
        } else if (!(temp instanceof IdentityPipe))
            break;
    }
    if (null != queryPipe) {
        if (pipe instanceof PropertyFilterPipe) {
            final PropertyFilterPipe temp = (PropertyFilterPipe) pipe;
            queryPipe.addHasContainer(new QueryPipe.HasContainer(temp.getKey(), temp.getPredicate(), temp.getValue()));
        } else if (pipe instanceof IntervalFilterPipe) {
            final IntervalFilterPipe temp = (IntervalFilterPipe) pipe;
            queryPipe.addIntervalContainer(new QueryPipe.IntervalContainer(temp.getKey(), temp.getStartValue(), temp.getEndValue()));
        } else if (pipe instanceof RangeFilterPipe) {
            queryPipe.setLowRange(((RangeFilterPipe) pipe).getLowRange());
            queryPipe.setHighRange(((RangeFilterPipe) pipe).getHighRange());
        }
        pipeline.addPipe(new IdentityPipe());
        return true;
    } else {
        return false;
    }
}
Also used : PropertyFilterPipe(com.tinkerpop.pipes.filter.PropertyFilterPipe) RangeFilterPipe(com.tinkerpop.pipes.filter.RangeFilterPipe) GraphQueryPipe(com.tinkerpop.pipes.transform.GraphQueryPipe) IntervalFilterPipe(com.tinkerpop.pipes.filter.IntervalFilterPipe) RangeFilterPipe(com.tinkerpop.pipes.filter.RangeFilterPipe) IdentityPipe(com.tinkerpop.pipes.IdentityPipe) VerticesVerticesPipe(com.tinkerpop.pipes.transform.VerticesVerticesPipe) VertexQueryPipe(com.tinkerpop.pipes.transform.VertexQueryPipe) Pipe(com.tinkerpop.pipes.Pipe) PropertyFilterPipe(com.tinkerpop.pipes.filter.PropertyFilterPipe) GraphQueryPipe(com.tinkerpop.pipes.transform.GraphQueryPipe) IntervalFilterPipe(com.tinkerpop.pipes.filter.IntervalFilterPipe) EdgesVerticesPipe(com.tinkerpop.pipes.transform.EdgesVerticesPipe) QueryPipe(com.tinkerpop.pipes.transform.QueryPipe) VerticesEdgesPipe(com.tinkerpop.pipes.transform.VerticesEdgesPipe) IdentityPipe(com.tinkerpop.pipes.IdentityPipe) VertexQueryPipe(com.tinkerpop.pipes.transform.VertexQueryPipe) GraphQueryPipe(com.tinkerpop.pipes.transform.GraphQueryPipe) QueryPipe(com.tinkerpop.pipes.transform.QueryPipe)

Aggregations

Pipe (com.tinkerpop.pipes.Pipe)14 GremlinPipeline (com.tinkerpop.gremlin.java.GremlinPipeline)6 Vertex (com.tinkerpop.blueprints.Vertex)4 AndFilterPipe (com.tinkerpop.pipes.filter.AndFilterPipe)4 OrFilterPipe (com.tinkerpop.pipes.filter.OrFilterPipe)4 PropertyFilterPipe (com.tinkerpop.pipes.filter.PropertyFilterPipe)4 Request (org.apache.atlas.catalog.Request)3 ResourceDefinition (org.apache.atlas.catalog.definition.ResourceDefinition)3 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)3 BooleanClause (org.apache.lucene.search.BooleanClause)3 Test (org.testng.annotations.Test)3 IdentityPipe (com.tinkerpop.pipes.IdentityPipe)2 IntervalFilterPipe (com.tinkerpop.pipes.filter.IntervalFilterPipe)2 RangeFilterPipe (com.tinkerpop.pipes.filter.RangeFilterPipe)2 EdgesVerticesPipe (com.tinkerpop.pipes.transform.EdgesVerticesPipe)2 GraphQueryPipe (com.tinkerpop.pipes.transform.GraphQueryPipe)2 QueryPipe (com.tinkerpop.pipes.transform.QueryPipe)2 VertexQueryPipe (com.tinkerpop.pipes.transform.VertexQueryPipe)2 VerticesEdgesPipe (com.tinkerpop.pipes.transform.VerticesEdgesPipe)2 VerticesVerticesPipe (com.tinkerpop.pipes.transform.VerticesVerticesPipe)2