Java Code Examples for org.apache.tinkerpop.gremlin.structure.util.ElementHelper#attachProperties()
The following examples show how to use
org.apache.tinkerpop.gremlin.structure.util.ElementHelper#attachProperties() .
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: TinkerGraph.java From tinkergraph-gremlin with Apache License 2.0 | 6 votes |
private Vertex createVertex(final long idValue, final String label, final Object... keyValues) { final Vertex vertex; if (specializedVertexFactoryByLabel.containsKey(label)) { final SpecializedElementFactory.ForVertex factory = specializedVertexFactoryByLabel.get(label); final SpecializedTinkerVertex underlying = factory.createVertex(idValue, this); vertex = factory.createVertexRef(underlying); } else { // vertex label not registered for a specialized factory, treating as generic vertex if (this.usesSpecializedElements) { throw new IllegalArgumentException( "this instance of TinkerGraph uses specialized elements, but doesn't have a factory for label " + label + ". Mixing specialized and generic elements is not (yet) supported"); } vertex = new TinkerVertex(idValue, label, this); } ElementHelper.attachProperties(vertex, VertexProperty.Cardinality.list, keyValues); return vertex; }
Example 2
Source File: TinkerHelper.java From tinkergraph-gremlin with Apache License 2.0 | 6 votes |
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 3
Source File: TinkerHelper.java From tinkerpop with Apache License 2.0 | 6 votes |
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 4
Source File: TinkerGraph.java From tinkerpop with Apache License 2.0 | 6 votes |
@Override public Vertex addVertex(final Object... keyValues) { ElementHelper.legalPropertyKeyValueArray(keyValues); Object idValue = vertexIdManager.convert(ElementHelper.getIdValue(keyValues).orElse(null)); final String label = ElementHelper.getLabelValue(keyValues).orElse(Vertex.DEFAULT_LABEL); if (null != idValue) { if (this.vertices.containsKey(idValue)) throw Exceptions.vertexWithIdAlreadyExists(idValue); } else { idValue = vertexIdManager.getNextId(this); } final Vertex vertex = new TinkerVertex(idValue, label, this); this.vertices.put(vertex.id(), vertex); ElementHelper.attachProperties(vertex, VertexProperty.Cardinality.list, keyValues); return vertex; }
Example 5
Source File: Neo4JSession.java From neo4j-gremlin-bolt with Apache License 2.0 | 6 votes |
Neo4JVertex addVertex(Object... keyValues) { Objects.requireNonNull(keyValues, "keyValues cannot be null"); // verify parameters are key/value pairs ElementHelper.legalPropertyKeyValueArray(keyValues); // id cannot be present if (ElementHelper.getIdValue(keyValues).isPresent()) throw Vertex.Exceptions.userSuppliedIdsNotSupported(); // create vertex Neo4JVertex vertex = new Neo4JVertex(graph, this, vertexIdProvider, edgeIdProvider, Arrays.asList(ElementHelper.getLabelValue(keyValues).orElse(Vertex.DEFAULT_LABEL).split(Neo4JVertex.LabelDelimiter))); // add vertex to transient set (before processing properties to avoid having a transient vertex in update queue) transientVertices.add(vertex); // attach properties ElementHelper.attachProperties(vertex, keyValues); // check vertex has id Object id = vertex.id(); if (id != null) transientVertexIndex.put(id, vertex); // return vertex return vertex; }
Example 6
Source File: StarGraph.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public Vertex addVertex(final Object... keyValues) { if (null == this.starVertex) { ElementHelper.legalPropertyKeyValueArray(keyValues); this.starVertex = new StarVertex(ElementHelper.getIdValue(keyValues).orElse(this.nextId()), ElementHelper.getLabelValue(keyValues).orElse(Vertex.DEFAULT_LABEL)); ElementHelper.attachProperties(this.starVertex, VertexProperty.Cardinality.list, keyValues); // TODO: is this smart? I say no... cause vertex property ids are not preserved. return this.starVertex; } else return new StarAdjacentVertex(ElementHelper.getIdValue(keyValues).orElse(this.nextId())); }
Example 7
Source File: Neo4jVertex.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 8
Source File: Neo4jGraph.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public Vertex addVertex(final Object... keyValues) { ElementHelper.legalPropertyKeyValueArray(keyValues); if (ElementHelper.getIdValue(keyValues).isPresent()) throw Vertex.Exceptions.userSuppliedIdsNotSupported(); this.tx().readWrite(); final Neo4jVertex vertex = new Neo4jVertex(this.baseGraph.createNode(ElementHelper.getLabelValue(keyValues).orElse(Vertex.DEFAULT_LABEL).split(Neo4jVertex.LABEL_DELIMINATOR)), this); ElementHelper.attachProperties(vertex, keyValues); return vertex; }
Example 9
Source File: TinkerVertexProperty.java From tinkerpop with Apache License 2.0 | 5 votes |
/** * Use this constructor to construct {@link VertexProperty} instances for {@link TinkerGraph} where the {@code id} * can be explicitly set and validated against the expected data type. */ public TinkerVertexProperty(final Object id, final TinkerVertex vertex, final String key, final V value, final Object... propertyKeyValues) { super(id, key); this.allowNullPropertyValues = vertex.graph().features().vertex().properties().supportsNullPropertyValues(); if (!allowNullPropertyValues && null == value) throw new IllegalArgumentException("value cannot be null as feature supportsNullPropertyValues is false"); this.vertex = vertex; this.key = key; this.value = value; ElementHelper.legalPropertyKeyValueArray(propertyKeyValues); ElementHelper.attachProperties(this, propertyKeyValues); }
Example 10
Source File: StarGraph.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, V value, final Object... keyValues) { ElementHelper.legalPropertyKeyValueArray(keyValues); if (null == this.vertexProperties) this.vertexProperties = new HashMap<>(); final List<VertexProperty> list = cardinality.equals(VertexProperty.Cardinality.single) ? new ArrayList<>(1) : this.vertexProperties.getOrDefault(key, new ArrayList<>()); final VertexProperty<V> vertexProperty = new StarVertexProperty<>(ElementHelper.getIdValue(keyValues).orElse(nextId()), key, value); ElementHelper.attachProperties(vertexProperty, keyValues); list.add(vertexProperty); this.vertexProperties.put(key, list); return vertexProperty; }
Example 11
Source File: StarGraph.java From tinkerpop with Apache License 2.0 | 5 votes |
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 12
Source File: GraphTransaction.java From hugegraph with Apache License 2.0 | 5 votes |
@Watched(prefix = "graph") public HugeVertex constructVertex(boolean verifyVL, Object... keyValues) { HugeElement.ElementKeys elemKeys = HugeElement.classifyKeys(keyValues); VertexLabel vertexLabel = this.checkVertexLabel(elemKeys.label(), verifyVL); Id id = HugeVertex.getIdValue(elemKeys.id()); List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys()); // Check whether id match with id strategy this.checkId(id, keys, vertexLabel); // Create HugeVertex HugeVertex vertex = new HugeVertex(this, null, vertexLabel); // Set properties ElementHelper.attachProperties(vertex, keyValues); // Assign vertex id if (this.params().mode().maintaining() && vertexLabel.idStrategy() == IdStrategy.AUTOMATIC) { // Resume id for AUTOMATIC id strategy in restoring mode vertex.assignId(id, true); } else { vertex.assignId(id); } vertex.setExpiredTime(); return vertex; }
Example 13
Source File: Neo4JSession.java From neo4j-gremlin-bolt with Apache License 2.0 | 5 votes |
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 14
Source File: EdgeDeserializer.java From tinkergraph-gremlin with Apache License 2.0 | 5 votes |
@Override protected Edge createElement(long id, String label, Map<String, Object> properties, Map<String, long[]> inVertexIdsByLabel, Map<String, long[]> outVertexIdsByLabel) { VertexRef outVertexRef = getVertexRef(outVertexIdsByLabel, Direction.OUT); VertexRef inVertexRef = getVertexRef(inVertexIdsByLabel, Direction.IN); SpecializedTinkerEdge edge = edgeFactoryByLabel.get(label).createEdge(id, graph, outVertexRef, inVertexRef); ElementHelper.attachProperties(edge, toTinkerpopKeyValues(properties)); edge.setModifiedSinceLastSerialization(false); return edge; }
Example 15
Source File: TinkerVertexProperty.java From tinkergraph-gremlin with Apache License 2.0 | 5 votes |
/** * Use this constructor to construct {@link VertexProperty} instances for {@link TinkerGraph} where the {@code id} * can be explicitly set and validated against the expected data type. */ public TinkerVertexProperty(final Object id, final TinkerVertex vertex, final String key, final V value, final Object... propertyKeyValues) { super(id, key, vertex.tinkerGraph()); this.vertex = vertex; this.key = key; this.value = value; ElementHelper.legalPropertyKeyValueArray(propertyKeyValues); ElementHelper.attachProperties(this, propertyKeyValues); }
Example 16
Source File: TinkerVertexProperty.java From tinkergraph-gremlin with Apache License 2.0 | 5 votes |
/** * This constructor will not validate the ID type against the {@link Graph}. It will always just use a * {@code Long} for its identifier. This is useful for constructing a {@link VertexProperty} for usage * with {@link TinkerGraphComputerView}. */ public TinkerVertexProperty(final TinkerVertex vertex, final String key, final V value, final Object... propertyKeyValues) { super(vertex.tinkerGraph().vertexPropertyIdManager.getNextId(vertex.tinkerGraph()), key, vertex.tinkerGraph()); this.vertex = vertex; this.key = key; this.value = value; ElementHelper.legalPropertyKeyValueArray(propertyKeyValues); ElementHelper.attachProperties(this, propertyKeyValues); }
Example 17
Source File: ArangoDBGraph.java From arangodb-tinkerpop-provider with Apache License 2.0 | 4 votes |
@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; }
Example 18
Source File: BlazeGraph.java From tinkerpop3 with GNU General Public License v2.0 | 4 votes |
/** * Helper for {@link BlazeVertex#property(Cardinality, String, Object, Object...)}. * * @param vertex the BlazeVertex * @param cardinality the property cardinality * @param key the property key * @param val the property value * @param kvs the properties to attach to the BlazeVertexProperty * @return a BlazeVertexProperty */ <V> BlazeVertexProperty<V> vertexProperty(final BlazeVertex vertex, final Cardinality cardinality, final String key, final V val, final Object... kvs) { final BigdataValueFactory rdfvf = rdfValueFactory(); final URI s = vertex.rdfId(); final URI p = rdfvf.asValue(vf.propertyURI(key)); final Literal lit = rdfvf.asValue(vf.toLiteral(val)); final BigdataStatement stmt; if (cardinality == Cardinality.list) { final Literal timestamp = rdfvf.createLiteral( String.valueOf(vpIdFactory.getAndIncrement()), LI_DATATYPE); stmt = rdfvf.createStatement(s, p, timestamp); } else { stmt = rdfvf.createStatement(s, p, lit); } final BigdataBNode sid = rdfvf.createBNode(stmt); final String vpId = vertexPropertyId(stmt); final RepositoryConnection cxn = cxn(); Code.wrapThrow(() -> { if (cardinality == Cardinality.list) { // <s> <key> timestamp . cxn.add(stmt); // <<<s> <key> timestamp>> rdf:value "val" . cxn.add(sid, VALUE, lit); } else { if (cardinality == Cardinality.single && !bulkLoad) { final String queryStr = sparql.cleanVertexProps(s, p, lit); update(queryStr); } // << stmt >> <key> "val" . cxn.add(stmt); } }); final BlazeProperty<V> prop = new BlazeProperty<>(this, vertex, p, lit); final BlazeVertexProperty<V> bvp = new BlazeVertexProperty<>(prop, vpId, sid); ElementHelper.attachProperties(bvp, kvs); return bvp; }
Example 19
Source File: SpecializedTinkerVertex.java From tinkergraph-gremlin with Apache License 2.0 | 4 votes |
@Override public Edge addEdge(final String label, Vertex inVertex, final Object... keyValues) { if (graph.isClosed()) { throw new IllegalStateException("cannot add more elements, graph is closed"); } if (null == inVertex) { throw Graph.Exceptions.argumentCanNotBeNull("inVertex"); } VertexRef<TinkerVertex> inVertexRef = null; if (inVertex instanceof VertexRef) { inVertexRef = (VertexRef) inVertex; inVertex = inVertexRef.get(); } if (this.removed) { throw elementAlreadyRemoved(Vertex.class, this.id); } if (!allowedOutEdgeLabels().contains(label)) { throw new IllegalArgumentException(getClass().getName() + " doesn't allow outgoing edges with label=" + label); } if (!((SpecializedTinkerVertex) inVertex).allowedInEdgeLabels().contains(label)) { throw new IllegalArgumentException(inVertex.getClass().getName() + " doesn't allow incoming edges with label=" + label); } ElementHelper.legalPropertyKeyValueArray(keyValues); if (graph.specializedEdgeFactoryByLabel.containsKey(label)) { SpecializedElementFactory.ForEdge factory = graph.specializedEdgeFactoryByLabel.get(label); Long idValue = (Long) graph.edgeIdManager.convert(ElementHelper.getIdValue(keyValues).orElse(null)); if (null != idValue) { if (graph.edges.containsKey(idValue)) throw Graph.Exceptions.edgeWithIdAlreadyExists(idValue); } else { idValue = (Long) graph.edgeIdManager.getNextId(graph); } graph.currentId.set(Long.max(idValue, graph.currentId.get())); // TODO hold link to vertexRef locally so we don't need the following lookup VertexRef<TinkerVertex> outVertexRef = (VertexRef<TinkerVertex>) graph.vertices.get(id); final SpecializedTinkerEdge underlying = factory.createEdge(idValue, graph, outVertexRef, inVertexRef); final Edge edge; if (graph.ondiskOverflowEnabled) { edge = factory.createEdgeRef(underlying); } else { edge = factory.createEdge(idValue, graph, outVertexRef, inVertexRef); } ElementHelper.attachProperties(edge, keyValues); graph.edges.put((long)edge.id(), edge); graph.getElementsByLabel(graph.edgesByLabel, label).add(edge); // acquireModificationLock(); storeOutEdge(edge); ((SpecializedTinkerVertex) inVertex).storeInEdge(edge); // releaseModificationLock(); modifiedSinceLastSerialization = true; return edge; } else { // edge label not registered for a specialized factory, treating as generic edge throw new IllegalArgumentException( "this instance of TinkerGraph uses specialized elements, but doesn't have a factory for label " + label + ". Mixing specialized and generic elements is not (yet) supported"); } }
Example 20
Source File: ArangoDBVertex.java From arangodb-tinkerpop-provider with Apache License 2.0 | 4 votes |
@Override public <V> VertexProperty<V> property( Cardinality cardinality, String key, V value, Object... keyValues) { logger.debug("setting vertex property {} = {} ({})", key, value, keyValues); ElementHelper.validateProperty(key, value); ElementHelper.legalPropertyKeyValueArray(keyValues); Optional<Object> idValue = ElementHelper.getIdValue(keyValues); String id = null; if (idValue.isPresent()) { if (graph.features().vertex().willAllowId(idValue.get())) { id = idValue.get().toString(); 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(ArangoDBGraph.ELEMENT_PROPERTIES_COLLECTION)) { id = parts[1]; } } Matcher m = ArangoDBUtil.DOCUMENT_KEY.matcher((String)id); if (!m.matches()) { throw new ArangoDBGraphException(String.format("Given id (%s) has unsupported characters.", id)); } } else { throw VertexProperty.Exceptions.userSuppliedIdsOfThisTypeNotSupported(); } int idIndex = 0; for (int i = 0; i < keyValues.length; i+=2) { if (keyValues[i] == T.id) { idIndex = i; break; } } keyValues = ArrayUtils.remove(keyValues, idIndex); keyValues = ArrayUtils.remove(keyValues, idIndex); } final Optional<VertexProperty<V>> optionalVertexProperty = ElementHelper.stageVertexProperty(this, cardinality, key, value, keyValues); if (optionalVertexProperty.isPresent()) return optionalVertexProperty.get(); ArangoDBVertexProperty<V> p = ArangoDBUtil.createArangoDBVertexProperty(id, key, value, this); ElementHelper.attachProperties(p, keyValues); return p; }