Java Code Examples for org.apache.tinkerpop.gremlin.structure.util.ElementHelper#validateLabel()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.ElementHelper#validateLabel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HugeElement.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Watched(prefix = "element")
public static final Object getLabelValue(Object... keyValues) {
    Object labelValue = null;
    for (int i = 0; i < keyValues.length; i = i + 2) {
        if (keyValues[i].equals(T.label)) {
            labelValue = keyValues[i + 1];
            E.checkArgument(labelValue instanceof String ||
                            labelValue instanceof VertexLabel,
                            "Expect a string or a VertexLabel object " +
                            "as the vertex label argument, but got: '%s'",
                            labelValue);
            if (labelValue instanceof String) {
                ElementHelper.validateLabel((String) labelValue);
            }
            break;
        }
    }
    return labelValue;
}
 
Example 2
Source File: GraphTransaction.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
private VertexLabel checkVertexLabel(Object label, boolean verifyLabel) {
    HugeVertexFeatures features = graph().features().vertex();

    // Check Vertex label
    if (label == null && features.supportsDefaultLabel()) {
        label = features.defaultLabel();
    }

    if (label == null) {
        throw Element.Exceptions.labelCanNotBeNull();
    }

    E.checkArgument(label instanceof String || label instanceof VertexLabel,
                    "Expect a string or a VertexLabel object " +
                    "as the vertex label argument, but got: '%s'", label);
    // The label must be an instance of String or VertexLabel
    if (label instanceof String) {
        if (verifyLabel) {
            ElementHelper.validateLabel((String) label);
        }
        label = graph().vertexLabel((String) label);
    }

    assert (label instanceof VertexLabel);
    return (VertexLabel) label;
}
 
Example 3
Source File: TinkerHelper.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
protected static Edge addEdge(final TinkerGraph graph, final TinkerVertex outVertex, final TinkerVertex inVertex, final String label, final Object... keyValues) {
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);

    Object idValue = graph.edgeIdManager.convert(ElementHelper.getIdValue(keyValues).orElse(null));

    final Edge edge;
    if (null != idValue) {
        if (graph.edges.containsKey((long)idValue))
            throw Graph.Exceptions.edgeWithIdAlreadyExists(idValue);
    } else {
        idValue = graph.edgeIdManager.getNextId(graph);
    }

    edge = new TinkerEdge(graph, idValue, outVertex, label, inVertex);
    ElementHelper.attachProperties(edge, keyValues);
    graph.edges.put((long)edge.id(), edge);
    TinkerHelper.addOutEdge(outVertex, label, edge);
    TinkerHelper.addInEdge(inVertex, label, edge);
    return edge;

}
 
Example 4
Source File: TinkerHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
protected static Edge addEdge(final TinkerGraph graph, final TinkerVertex outVertex, final TinkerVertex inVertex, final String label, final Object... keyValues) {
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);

    Object idValue = graph.edgeIdManager.convert(ElementHelper.getIdValue(keyValues).orElse(null));

    final Edge edge;
    if (null != idValue) {
        if (graph.edges.containsKey(idValue))
            throw Graph.Exceptions.edgeWithIdAlreadyExists(idValue);
    } else {
        idValue = graph.edgeIdManager.getNextId(graph);
    }

    edge = new TinkerEdge(idValue, outVertex, label, inVertex);
    ElementHelper.attachProperties(edge, keyValues);
    graph.edges.put(edge.id(), edge);
    TinkerHelper.addOutEdge(outVertex, label, edge);
    TinkerHelper.addInEdge(inVertex, label, edge);
    return edge;

}
 
Example 5
Source File: HBaseBulkLoader.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public Edge addEdge(Vertex outVertex, Vertex inVertex, String label, Object... keyValues) {
    try {
        if (null == inVertex) throw Graph.Exceptions.argumentCanNotBeNull("inVertex");
        ElementHelper.validateLabel(label);
        ElementHelper.legalPropertyKeyValueArray(keyValues);
        Object idValue = ElementHelper.getIdValue(keyValues).orElse(null);

        idValue = HBaseGraphUtils.generateIdIfNeeded(idValue);
        long now = System.currentTimeMillis();
        HBaseEdge edge = new HBaseEdge(graph, idValue, label, now, now,
                HBaseGraphUtils.propertiesToMap(keyValues), inVertex, outVertex);
        edge.validate();

        Iterator<IndexMetadata> indices = edge.getIndices(OperationType.WRITE);
        indexEdge(edge, indices);

        EdgeIndexWriter writer = new EdgeIndexWriter(graph, edge, Constants.CREATED_AT);
        if (edgeIndicesMutator != null) edgeIndicesMutator.mutate(getMutationList(writer.constructInsertions()));

        Creator creator = new EdgeWriter(graph, edge);
        if (edgesMutator != null) edgesMutator.mutate(getMutationList(creator.constructInsertions()));

        return edge;
    } catch (IOException e) {
        throw new HBaseGraphException(e);
    }
}
 
Example 6
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    if (null == inVertex) throw Graph.Exceptions.argumentCanNotBeNull("inVertex");
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    Object idValue = ElementHelper.getIdValue(keyValues).orElse(null);

    idValue = HBaseGraphUtils.generateIdIfNeeded(idValue);
    long now = System.currentTimeMillis();
    HBaseEdge newEdge = new HBaseEdge(graph, idValue, label, now, now, HBaseGraphUtils.propertiesToMap(keyValues), inVertex, this);
    newEdge.validate();
    newEdge.writeEdgeEndpoints();
    newEdge.writeToModel();

    invalidateEdgeCache();
    if (!isCached()) {
        HBaseVertex cachedVertex = (HBaseVertex) graph.findVertex(id, false);
        if (cachedVertex != null) cachedVertex.invalidateEdgeCache();
    }
    ((HBaseVertex) inVertex).invalidateEdgeCache();
    if (!((HBaseVertex) inVertex).isCached()) {
        HBaseVertex cachedInVertex = (HBaseVertex) graph.findVertex(inVertex.id(), false);
        if (cachedInVertex != null) cachedInVertex.invalidateEdgeCache();
    }

    Edge edge = graph.findOrCreateEdge(idValue);
    ((HBaseEdge) edge).copyFrom(newEdge);
    return edge;
}
 
Example 7
Source File: Neo4JVertex.java    From neo4j-gremlin-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Edge addEdge(String label, Vertex vertex, Object... keyValues) {
    // validate label
    ElementHelper.validateLabel(label);
    // vertex must exist
    if (vertex == null)
        throw Graph.Exceptions.argumentCanNotBeNull("vertex");
    // validate properties
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    // transaction should be ready for io operations
    graph.tx().readWrite();
    // add edge
    return session.addEdge(label, this, (Neo4JVertex)vertex, keyValues);
}
 
Example 8
Source File: Neo4JSession.java    From neo4j-gremlin-bolt with Apache License 2.0 5 votes vote down vote up
Neo4JEdge addEdge(String label, Neo4JVertex out, Neo4JVertex in, Object... keyValues) {
    Objects.requireNonNull(label, "label cannot be null");
    Objects.requireNonNull(out, "out cannot be null");
    Objects.requireNonNull(in, "in cannot be null");
    Objects.requireNonNull(keyValues, "keyValues cannot be null");
    // validate label
    ElementHelper.validateLabel(label);
    // verify parameters are key/value pairs
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    // id cannot be present
    if (ElementHelper.getIdValue(keyValues).isPresent())
        throw Vertex.Exceptions.userSuppliedIdsNotSupported();
    // create edge
    Neo4JEdge edge = new Neo4JEdge(graph, this, edgeIdProvider, label, out, in);
    // register transient edge (before processing properties to avoid having a transient edge in update queue)
    transientEdges.add(edge);
    // attach properties
    ElementHelper.attachProperties(edge, keyValues);
    // register transient edge with adjacent vertices
    out.addOutEdge(edge);
    in.addInEdge(edge);
    // check edge has id
    Object id = edge.id();
    if (id != null)
        transientEdgeIndex.put(id, edge);
    // return edge
    return edge;
}
 
Example 9
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    if (null == this.outEdges)
        this.outEdges = new HashMap<>();
    List<Edge> outE = this.outEdges.get(label);
    if (null == outE) {
        outE = new ArrayList<>();
        this.outEdges.put(label, outE);
    }
    final StarEdge outEdge = new StarOutEdge(ElementHelper.getIdValue(keyValues).orElse(nextId()), label, inVertex.id());
    ElementHelper.attachProperties(outEdge, keyValues);
    outE.add(outEdge);
    return outEdge;
}
 
Example 10
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
Edge addInEdge(final String label, final Vertex outVertex, final Object... keyValues) {
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    if (null == this.inEdges)
        this.inEdges = new HashMap<>();
    List<Edge> inE = this.inEdges.get(label);
    if (null == inE) {
        inE = new ArrayList<>();
        this.inEdges.put(label, inE);
    }
    final StarEdge inEdge = new StarInEdge(ElementHelper.getIdValue(keyValues).orElse(nextId()), label, outVertex.id());
    ElementHelper.attachProperties(inEdge, keyValues);
    inE.add(inEdge);
    return inEdge;
}
 
Example 11
Source File: Neo4jVertex.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    if (null == inVertex) throw Graph.Exceptions.argumentCanNotBeNull("inVertex");
    ElementHelper.validateLabel(label);
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    if (ElementHelper.getIdValue(keyValues).isPresent())
        throw Edge.Exceptions.userSuppliedIdsNotSupported();

    this.graph.tx().readWrite();
    final Neo4jNode node = (Neo4jNode) this.baseElement;
    final Neo4jEdge edge = new Neo4jEdge(node.connectTo(((Neo4jVertex) inVertex).getBaseVertex(), label), this.graph);
    ElementHelper.attachProperties(edge, keyValues);
    return edge;
}
 
Example 12
Source File: SqlgVertex.java    From sqlg with MIT License 5 votes vote down vote up
private Edge addEdgeInternal(boolean complete, String label, Vertex inVertex, Object... keyValues) {
    if (null == inVertex) throw Graph.Exceptions.argumentCanNotBeNull("vertex");
    if (this.removed) {
        throw new IllegalStateException(String.format("Vertex with id %s was removed.", id().toString()));
    }

    ElementHelper.validateLabel(label);

    Preconditions.checkArgument(!label.contains("."), String.format("Edge label may not contain a '.' , the edge will be stored in the schema of the owning vertex. label = %s", label));

    ElementHelper.legalPropertyKeyValueArray(keyValues);
    if (ElementHelper.getIdValue(keyValues).isPresent())
        throw Edge.Exceptions.userSuppliedIdsNotSupported();

    List<String> previousBatchModeKeys;
    if (complete) {
        previousBatchModeKeys = this.sqlgGraph.tx().getBatchManager().getStreamingBatchModeEdgeKeys();
    } else {
        previousBatchModeKeys = Collections.emptyList();
    }
    Triple<Map<String, PropertyType>, Map<String, Object>, Map<String, Object>> keyValueMapTriple = SqlgUtil.validateVertexKeysValues(this.sqlgGraph.getSqlDialect(), keyValues, previousBatchModeKeys);
    if (!complete && keyValueMapTriple.getRight().size() != keyValueMapTriple.getMiddle().size()) {
        throw Property.Exceptions.propertyValueCanNotBeNull();
    }
    final Pair<Map<String, Object>, Map<String, Object>> keyValueMapPair = Pair.of(keyValueMapTriple.getMiddle(), keyValueMapTriple.getRight());
    final Map<String, PropertyType> columns = keyValueMapTriple.getLeft();
    Optional<VertexLabel> outVertexLabelOptional = this.sqlgGraph.getTopology().getVertexLabel(this.schema, this.table);
    Optional<VertexLabel> inVertexLabelOptional = this.sqlgGraph.getTopology().getVertexLabel(((SqlgVertex) inVertex).schema, ((SqlgVertex) inVertex).table);
    Preconditions.checkState(outVertexLabelOptional.isPresent(), "Out VertexLabel must be present. Not found for %s", this.schema + "." + this.table);
    Preconditions.checkState(inVertexLabelOptional.isPresent(), "In VertexLabel must be present. Not found for %s", ((SqlgVertex) inVertex).schema + "." + ((SqlgVertex) inVertex).table);

    this.sqlgGraph.getTopology().threadWriteLock();

    //noinspection OptionalGetWithoutIsPresent
    EdgeLabel edgeLabel = this.sqlgGraph.getTopology().ensureEdgeLabelExist(label, outVertexLabelOptional.get(), inVertexLabelOptional.get(), columns);
    if (!edgeLabel.hasIDPrimaryKey()) {
        Preconditions.checkArgument(columns.keySet().containsAll(edgeLabel.getIdentifiers()), "identifiers must be present %s", edgeLabel.getIdentifiers());
    }
    return new SqlgEdge(this.sqlgGraph, complete, this.schema, label, (SqlgVertex) inVertex, this, keyValueMapPair);
}
 
Example 13
Source File: ArangoDBGraph.java    From arangodb-tinkerpop-provider with Apache License 2.0 4 votes vote down vote up
@Override
public Vertex addVertex(Object... keyValues) {
       ElementHelper.legalPropertyKeyValueArray(keyValues);
       Object id;
       String collection;
       if (!schemaless) {
       	collection = ElementHelper.getLabelValue(keyValues).orElse(null);
       	ElementHelper.validateLabel(collection);
       }
       else {
       	collection = DEFAULT_VERTEX_COLLECTION;
       }
       if (!vertexCollections().contains(collection)) {
		throw new IllegalArgumentException(String.format("Vertex label (%s) not in graph (%s) vertex collections.", collection, name));
	}
       ArangoDBVertex vertex = null;
       if (ElementHelper.getIdValue(keyValues).isPresent()) {
       	id = ElementHelper.getIdValue(keyValues).get();
       	if (this.features().vertex().willAllowId(id)) {
        	if (id.toString().contains("/")) {
        		String fullId = id.toString();
        		String[] parts = fullId.split("/");
        		// The collection name is the last part of the full name
        		String[] collectionParts = parts[0].split("_");
				String collectionName = collectionParts[collectionParts.length-1];
				if (collectionName.contains(collection)) {
        			id = parts[1];
        			
        		}
        	}
       		Matcher m = ArangoDBUtil.DOCUMENT_KEY.matcher((String)id);
       		if (m.matches()) {
       			vertex = new ArangoDBVertex(id.toString(), collection, this);
       		}
       		else {
           		throw new ArangoDBGraphException(String.format("Given id (%s) has unsupported characters.", id));
           	}
       	}
       	else {
       		throw Vertex.Exceptions.userSuppliedIdsOfThisTypeNotSupported();
       	}

       }
       else {
       	vertex = new ArangoDBVertex(this, collection);
       }
       // The vertex needs to exist before we can attach properties
       client.insertDocument(vertex);
       ElementHelper.attachProperties(vertex, keyValues);
       return vertex;
}