Search in sources :

Example 16 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class SqlgWhereTraversalStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        while (this.starts.hasNext()) {
            Traverser.Admin start = this.starts.next();
            // However the traverser might have been created from a step and strategy that does not care for TraverserRequirement.ONE_BULK so we force it here.
            if (start instanceof SqlgTraverser) {
                ((SqlgTraverser) start).setRequiresOneBulk(true);
            }
            List<Object> startObjects = start.path().objects();
            StringBuilder recordIdConcatenated = new StringBuilder();
            for (Object startObject : startObjects) {
                if (startObject instanceof Element) {
                    Element e = (Element) startObject;
                    recordIdConcatenated.append(e.id().toString());
                } else {
                    recordIdConcatenated.append(startObject.toString());
                }
            }
            this.startRecordIds.put(recordIdConcatenated.toString(), start);
            this.whereTraversal.addStart(start);
        }
        while (this.whereTraversal.hasNext()) {
            Traverser.Admin<?> filterTraverser = this.whereTraversal.nextTraverser();
            List<Object> filterTraverserObjects = filterTraverser.path().objects();
            String startId = "";
            for (Object filteredTraverserObject : filterTraverserObjects) {
                if (filteredTraverserObject instanceof Element) {
                    Element e = (Element) filteredTraverserObject;
                    startId += e.id().toString();
                } else {
                    startId += filteredTraverserObject.toString();
                }
                if (startRecordIds.containsKey(startId)) {
                    this.results.addAll(startRecordIds.get(startId));
                    startRecordIds.removeAll(startId);
                }
                if (startRecordIds.isEmpty()) {
                    break;
                }
            }
        }
        this.results.sort((o1, o2) -> {
            SqlgTraverser x = (SqlgTraverser) o1;
            SqlgTraverser y = (SqlgTraverser) o2;
            return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
        });
        this.resultIterator = this.results.iterator();
    }
    if (this.resultIterator.hasNext()) {
        Traverser.Admin<S> traverser = this.resultIterator.next();
        return PathProcessor.processTraverserPathLabels(traverser, this.keepLabels);
    } else {
        // The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
        reset();
        throw FastNoSuchElementException.instance();
    }
}
Also used : SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) Element(org.apache.tinkerpop.gremlin.structure.Element)

Example 17 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class SqlgOptionalStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
    if (this.first) {
        Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
        this.first = false;
        long startCount = 1;
        while (this.starts.hasNext()) {
            Traverser.Admin<S> start = this.starts.next();
            this.optionalTraversal.addStart(start);
            ((SqlgElement) start.get()).setInternalStartTraverserIndex(startCount++);
            List<Object> startObjects = start.path().objects();
            StringBuilder recordIdConcatenated = new StringBuilder();
            for (Object startObject : startObjects) {
                Element e = (Element) startObject;
                recordIdConcatenated.append(e.id().toString());
            }
            startRecordIds.put(recordIdConcatenated.toString(), start);
        }
        while (true) {
            if (this.optionalTraversal.hasNext()) {
                Traverser.Admin<S> optionalTraverser = this.optionalTraversal.nextTraverser();
                this.results.add(optionalTraverser);
                List<Object> optionObjects = optionalTraverser.path().objects();
                String startId = "";
                for (Object optionObject : optionObjects) {
                    Element e = (Element) optionObject;
                    startId += e.id().toString();
                    if (startRecordIds.removeAll(startId).isEmpty()) {
                        break;
                    }
                }
            } else {
                break;
            }
        }
        for (Traverser.Admin<S> start : startRecordIds.values()) {
            this.results.add(start);
            // Bulking logic interferes here, addStart calls DefaultTraversal.merge which has bulking logic
            start.setBulk(1L);
        }
        // Now travers the options. The starts have been set.
        while (true) {
            if (this.optionalTraversal.hasNext()) {
                this.results.add(optionalTraversal.nextTraverser());
            } else {
                break;
            }
        }
        // Sort the results, this is to ensure the the incoming start order is not lost.
        this.results.sort((o1, o2) -> {
            SqlgTraverser x = (SqlgTraverser) o1;
            SqlgTraverser y = (SqlgTraverser) o2;
            return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
        });
        this.resultIterator = this.results.iterator();
    }
    while (this.resultIterator.hasNext()) {
        return this.resultIterator.next();
    }
    throw FastNoSuchElementException.instance();
}
Also used : SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) SqlgElement(org.umlg.sqlg.structure.SqlgElement) SqlgElement(org.umlg.sqlg.structure.SqlgElement) Element(org.apache.tinkerpop.gremlin.structure.Element) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser)

Example 18 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class TestTopologyUpgrade method testModernGraph.

@Test
public void testModernGraph() throws Exception {
    loadModern();
    Traversal<Vertex, Long> traversal = this.sqlgGraph.traversal().V().both().both().count();
    Assert.assertEquals(new Long(30), traversal.next());
    Assert.assertFalse(traversal.hasNext());
    this.sqlgGraph.traversal().V().forEachRemaining(Element::remove);
    this.sqlgGraph.tx().commit();
    // Delete the topology
    dropSqlgSchema(this.sqlgGraph);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        loadModern(sqlgGraph1);
        traversal = sqlgGraph1.traversal().V().both().both().count();
        Assert.assertEquals(new Long(30), traversal.next());
        Assert.assertFalse(traversal.hasNext());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Element(org.apache.tinkerpop.gremlin.structure.Element) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 19 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class TestColumnNamePropertyNameMapScope method testColumnNamePropertyNameMap.

/**
 * This tests a bug with the columnName/propertyName maps having the wrong scope.
 * @throws Exception
 */
@Test
public void testColumnNamePropertyNameMap() throws Exception {
    Vertex group = this.sqlgGraph.addVertex(T.label, "Group", "name", "group1", "className", "this.that.Group", "uid", UUID.randomUUID().toString());
    Vertex network = this.sqlgGraph.addVertex(T.label, "Network", "name", "network1", "className", "this.that.Network", "uid", UUID.randomUUID().toString());
    group.addEdge("group_network", network);
    for (int i = 0; i < 10; i++) {
        Vertex nsv = this.sqlgGraph.addVertex(T.label, "NetworkSoftwareVersion", "name", "R15_HUAWEI_GSM" + i, "className", "this.that.NetworkSoftwareVersion", "firstLoad", false, "softwareVersion", "R15_HUAWEI_GSM", "uid", UUID.randomUUID().toString());
        network.addEdge("network_networkSoftwareVersion", nsv);
    }
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.traversal().V(network.id()).out("network_networkNodeGroup").<Vertex>forEachRemaining(Element::remove);
    this.sqlgGraph.tx().commit();
    List<Vertex> vertexList = sqlgGraph.traversal().V().hasLabel("Group").emit().repeat(__.out("group_network", "network_networkSoftwareVersion")).times(5).toList();
    System.out.println(vertexList);
    assertEquals("this.that.Network", vertexList.get(1).value("className"));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Element(org.apache.tinkerpop.gremlin.structure.Element) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 20 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project timbuctoo by HuygensING.

the class DatabaseLog method generate.

public void generate() {
    long prevTimestamp = 0L;
    // union warns about unchecked.
    @SuppressWarnings("unchecked") GraphTraversal<Vertex, ? extends Element> results = graphWrapper.getGraph().traversal().V().has("modified").has("rev").has("tim_id").not(// ignore search results
    __.has(T.label, LabelP.of("searchresult"))).not(// ignore database log items
    __.bothE("NEXT_ITEM")).union(__.identity(), __.outE().has("modified")).dedup().by(__.valueMap("rev", "tim_id")).order().by("modified", new ChangeStringComparator());
    List<Edge> edges = Lists.newArrayList();
    List<Vertex> vertices = Lists.newArrayList();
    logOutput.prepareToWrite();
    for (; results.hasNext(); ) {
        Element element = results.next();
        String modifiedString = element.value("modified");
        try {
            long curTimestamp = getTimestampFromChangeString(modifiedString);
            /* To make sure the EdgeLogEntries are added right behind the VertexLogEntries with the same timestamp, add
         * the LogEntries to the database before the timestamp changes.
         */
            if (prevTimestamp != curTimestamp) {
                vertices.forEach(vertex -> logEntryFactory.createForVertex(vertex).appendToLog(logOutput));
                vertices.clear();
                edges.forEach(edge -> logEntryFactory.createForEdge(edge).appendToLog(logOutput));
                edges.clear();
                prevTimestamp = curTimestamp;
            }
            if (element instanceof Vertex) {
                vertices.add((Vertex) element);
            } else if (element instanceof Edge) {
                edges.add((Edge) element);
            } else {
                LOG.error("Element type {} not expected.", element.getClass());
            }
        } catch (IOException e) {
            LOG.error("Change '{}' could nog be parsed.", modifiedString);
        }
    }
    // add remaining
    vertices.forEach(vertex -> logEntryFactory.createForVertex(vertex).appendToLog(logOutput));
    vertices.clear();
    edges.forEach(edge -> logEntryFactory.createForEdge(edge).appendToLog(logOutput));
    edges.clear();
    logOutput.finishWriting();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Element(org.apache.tinkerpop.gremlin.structure.Element) IOException(java.io.IOException) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Aggregations

Element (org.apache.tinkerpop.gremlin.structure.Element)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 Traverser (org.apache.tinkerpop.gremlin.process.traversal.Traverser)8 SqlgTraverser (org.umlg.sqlg.structure.SqlgTraverser)6 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 Edge (org.apache.tinkerpop.gremlin.structure.Edge)4 Test (org.junit.Test)4 BaseTest (org.umlg.sqlg.test.BaseTest)4 Var (ai.grakn.graql.Var)2 IOException (java.io.IOException)2 java.util (java.util)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)2 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)2 ConfigElement (org.janusgraph.diskstorage.configuration.ConfigElement)2 Concept (ai.grakn.concept.Concept)1 ConceptId (ai.grakn.concept.ConceptId)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Match (ai.grakn.graql.Match)1