Search in sources :

Example 1 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateEdge method execute.

/**
   * Execute the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (clazz == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    return OGraphCommandExecutorSQLFactory.runInConfiguredTxMode(new OGraphCommandExecutorSQLFactory.GraphCallBack<List<Object>>() {

        @Override
        public List<Object> call(OrientBaseGraph graph) {
            final Set<OIdentifiable> fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), from, context, iArgs);
            final Set<OIdentifiable> toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), to, context, iArgs);
            // CREATE EDGES
            final List<Object> edges = new ArrayList<Object>();
            for (OIdentifiable from : fromIds) {
                final OrientVertex fromVertex = graph.getVertex(from);
                if (fromVertex == null)
                    throw new OCommandExecutionException("Source vertex '" + from + "' not exists");
                for (OIdentifiable to : toIds) {
                    final OrientVertex toVertex;
                    if (from.equals(to)) {
                        toVertex = fromVertex;
                    } else {
                        toVertex = graph.getVertex(to);
                    }
                    if (fields != null)
                        // EVALUATE FIELDS
                        for (final OPair<String, Object> f : fields) {
                            if (f.getValue() instanceof OSQLFunctionRuntime) {
                                f.setValue(((OSQLFunctionRuntime) f.getValue()).getValue(to, null, context));
                            } else if (f.getValue() instanceof OSQLFilterItem) {
                                f.setValue(((OSQLFilterItem) f.getValue()).getValue(to, null, context));
                            }
                        }
                    OrientEdge edge = null;
                    if (content != null) {
                        if (fields != null)
                            // MERGE CONTENT WITH FIELDS
                            fields.addAll(OPair.convertFromMap(content.toMap()));
                        else
                            fields = OPair.convertFromMap(content.toMap());
                    }
                    edge = fromVertex.addEdge(null, toVertex, edgeLabel, clusterName, fields);
                    if (fields != null && !fields.isEmpty()) {
                        if (edge.isLightweight())
                            edge.convertToDocument();
                        OSQLHelper.bindParameters(edge.getRecord(), fields, new OCommandParameters(iArgs), context);
                    }
                    edge.save(clusterName);
                    edges.add(edge);
                    if (batch > 0 && edges.size() % batch == 0) {
                        graph.commit();
                        graph.begin();
                    }
                }
            }
            if (edges.isEmpty()) {
                if (fromIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no source vertices");
                else if (toIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no target vertices");
                throw new OCommandExecutionException("No edge has been created between " + fromIds + " and " + toIds);
            }
            return edges;
        }
    });
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 2 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OHttpGraphResponse method writeRecords.

public void writeRecords(final Object iRecords, final String iFetchPlan, String iFormat, final String accept, final Map<String, Object> iAdditionalProperties, final String mode) throws IOException {
    if (iRecords == null) {
        send(OHttpUtils.STATUS_OK_NOCONTENT_CODE, "", OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
        return;
    }
    if (!mode.equalsIgnoreCase("graph")) {
        super.writeRecords(iRecords, iFetchPlan, iFormat, accept, iAdditionalProperties, mode);
        return;
    }
    if (accept != null && accept.contains("text/csv"))
        throw new IllegalArgumentException("Graph mode cannot accept '" + accept + "'");
    final OrientGraphNoTx graph = (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) ODatabaseRecordThreadLocal.INSTANCE.get());
    try {
        // DIVIDE VERTICES FROM EDGES
        final Set<OrientVertex> vertices = new HashSet<OrientVertex>();
        final Set<OrientEdge> edges = new HashSet<OrientEdge>();
        final Iterator<Object> iIterator = OMultiValue.getMultiValueIterator(iRecords);
        while (iIterator.hasNext()) {
            Object entry = iIterator.next();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            entry = ((OIdentifiable) entry).getRecord();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            if (entry instanceof ODocument) {
                OClass schemaClass = ((ODocument) entry).getSchemaClass();
                if (schemaClass != null && schemaClass.isVertexType())
                    vertices.add(graph.getVertex(entry));
                else if (schemaClass != null && schemaClass.isEdgeType()) {
                    OrientEdge edge = graph.getEdge(entry);
                    vertices.add(graph.getVertex(edge.getVertex(Direction.IN)));
                    vertices.add(graph.getVertex(edge.getVertex(Direction.OUT)));
                    edges.add(edge);
                } else
                    // IGNORE IT
                    continue;
            }
        }
        final StringWriter buffer = new StringWriter();
        final OJSONWriter json = new OJSONWriter(buffer, "");
        json.beginObject();
        json.beginObject("graph");
        // WRITE VERTICES
        json.beginCollection("vertices");
        for (OrientVertex vertex : vertices) {
            json.beginObject();
            json.writeAttribute("@rid", vertex.getIdentity());
            json.writeAttribute("@class", vertex.getRecord().getClassName());
            // ADD ALL THE VERTEX'S EDGES
            for (Edge e : vertex.getEdges(Direction.BOTH)) edges.add((OrientEdge) e);
            // ADD ALL THE PROPERTIES
            for (String field : vertex.getPropertyKeys()) {
                final Object v = vertex.getProperty(field);
                if (v != null)
                    json.writeAttribute(field, v);
            }
            json.endObject();
        }
        json.endCollection();
        // WRITE EDGES
        json.beginCollection("edges");
        for (OrientEdge edge : edges) {
            if (!vertices.contains(edge.getVertex(Direction.OUT)) || !vertices.contains(edge.getVertex(Direction.IN)))
                // ONE OF THE 2 VERTICES ARE NOT PART OF THE RESULT SET: DISCARD IT
                continue;
            json.beginObject();
            json.writeAttribute("@rid", edge.getIdentity());
            json.writeAttribute("@class", edge.getRecord().getClassName());
            json.writeAttribute("out", edge.getVertex(Direction.OUT).getId());
            json.writeAttribute("in", edge.getVertex(Direction.IN).getId());
            for (String field : edge.getPropertyKeys()) {
                final Object v = edge.getProperty(field);
                if (v != null)
                    json.writeAttribute(field, v);
            }
            json.endObject();
        }
        json.endCollection();
        if (iAdditionalProperties != null) {
            for (Map.Entry<String, Object> entry : iAdditionalProperties.entrySet()) {
                final Object v = entry.getValue();
                if (OMultiValue.isMultiValue(v)) {
                    json.beginCollection(-1, true, entry.getKey());
                    formatMultiValue(OMultiValue.getMultiValueIterator(v), buffer, null);
                    json.endCollection(-1, true);
                } else
                    json.writeAttribute(entry.getKey(), v);
                if (Thread.currentThread().isInterrupted())
                    break;
            }
        }
        json.endObject();
        json.endObject();
        send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } finally {
        graph.shutdown();
    }
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Map(java.util.Map) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionLabel method getLabel.

private Object getLabel(final OrientBaseGraph graph, final OIdentifiable iCurrentRecord) {
    final ODocument rec = iCurrentRecord.getRecord();
    OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(rec);
    if (immutableClass.isVertexType()) {
        // VERTEX
        final OrientVertex vertex = graph.getVertex(iCurrentRecord);
        return vertex.getLabel();
    } else if (immutableClass.isEdgeType()) {
        // EDGE
        final OrientEdge edge = graph.getEdge(iCurrentRecord);
        return edge.getLabel();
    } else
        throw new OCommandExecutionException("Invalid record: is neither a vertex nor an edge. Found class: " + immutableClass);
}
Also used : OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionMove method e2v.

protected Object e2v(final OrientBaseGraph graph, final OIdentifiable iRecord, final Direction iDirection, final String[] iLabels) {
    final ODocument rec = iRecord.getRecord();
    OImmutableClass clazz = ODocumentInternal.getImmutableSchemaClass(rec);
    if (clazz != null && clazz.isEdgeType()) {
        // EDGE
        final OrientEdge edge = graph.getEdge(rec);
        if (edge != null) {
            if (Direction.BOTH.equals(iDirection)) {
                Set<Vertex> result = new HashSet<Vertex>();
                result.add(edge.getVertex(Direction.OUT));
                result.add(edge.getVertex(Direction.IN));
                return result;
            } else {
                return edge.getVertex(iDirection);
            }
        }
    }
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet)

Example 5 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionGremlin method execute.

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams, final OCommandContext iContext) {
    final ODatabaseDocumentTx db = OGremlinHelper.getGraphDatabase(ODatabaseRecordThreadLocal.INSTANCE.get());
    result = new ArrayList<Object>();
    OGremlinHelper.execute(db, (String) iParams[0], null, (Map) iContext.getVariables(), result, new OGremlinHelper.OGremlinCallback() {

        @Override
        public boolean call(final ScriptEngine iEngine, final OrientBaseGraph iGraph) {
            if (iCurrentRecord == null)
                // IGNORE PRE-PROCESSING
                return true;
            final ODocument document = (ODocument) iCurrentRecord;
            OClass clazz = ODocumentInternal.getImmutableSchemaClass(document);
            if (clazz != null && clazz.isSubClassOf(OrientEdgeType.CLASS_NAME)) {
                // EDGE TYPE, CREATE THE BLUEPRINTS'S WRAPPER
                OrientEdge graphElement = (OrientEdge) new OrientElementIterable<OrientEdge>(iGraph, Arrays.asList(new ODocument[] { document })).iterator().next();
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("current", graphElement);
                // FRAMES LIKE SYNTAX
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("it", graphElement);
            } else {
                // VERTEX TYPE, CREATE THE BLUEPRINTS'S WRAPPER
                OrientVertex graphElement = (OrientVertex) new OrientElementIterable<OrientVertex>(iGraph, Arrays.asList(new ODocument[] { document })).iterator().next();
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("current", graphElement);
                // FRAMES LIKE SYNTAX
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("it", graphElement);
            }
            return true;
        }
    }, null);
    return result;
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ScriptEngine(javax.script.ScriptEngine) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OGremlinHelper(com.orientechnologies.orient.graph.gremlin.OGremlinHelper) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OrientElementIterable(com.tinkerpop.blueprints.impls.orient.OrientElementIterable)

Aggregations

OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)32 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 Test (org.junit.Test)17 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)6 Edge (com.tinkerpop.blueprints.Edge)5 HashSet (java.util.HashSet)5 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)4 HashMap (java.util.HashMap)4 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)3 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)2 OIndex (com.orientechnologies.orient.core.index.OIndex)2 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)2 ODirtyManager (com.orientechnologies.orient.core.record.impl.ODirtyManager)2