@Override public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) { if (this.removed) throw elementAlreadyRemoved(Vertex.class, id); ElementHelper.legalPropertyKeyValueArray(keyValues); ElementHelper.validateProperty(key, value); final Optional<Object> optionalId = ElementHelper.getIdValue(keyValues); final Optional<VertexProperty<V>> optionalVertexProperty = ElementHelper.stageVertexProperty(this, cardinality, key, value, keyValues); if (optionalVertexProperty.isPresent()) return optionalVertexProperty.get(); if (TinkerHelper.inComputerMode(this.graph)) { final VertexProperty<V> vertexProperty = (VertexProperty<V>) this.graph.graphComputerView.addProperty(this, key, value); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } else { final Object idValue = optionalId.isPresent() ? graph.vertexPropertyIdManager.convert(optionalId.get()) : graph.vertexPropertyIdManager.getNextId(graph); final VertexProperty<V> vertexProperty = new TinkerVertexProperty<V>(idValue, this, key, value); if (null == this.properties) this.properties = new HashMap<>(); final List<VertexProperty> list = this.properties.getOrDefault(key, new ArrayList<>()); list.add(vertexProperty); this.properties.put(key, list); TinkerHelper.autoUpdateIndex(this, key, value, null); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } }
@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); } if (specializedVertexFactoryByLabel.containsKey(label)) { SpecializedElementFactory.ForVertex factory = specializedVertexFactoryByLabel.get(label); SpecializedTinkerVertex vertex = factory.createVertex(idValue, this); this.vertices.put(idValue, vertex); ElementHelper.attachProperties(vertex, VertexProperty.Cardinality.list, keyValues); return vertex; } 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"); } final Vertex vertex = new TinkerVertex(idValue, label, this); this.vertices.put(vertex.id(), vertex); ElementHelper.attachProperties(vertex, VertexProperty.Cardinality.list, keyValues); return vertex; } }
@Override public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) { if (this.removed) throw elementAlreadyRemoved(Vertex.class, id); ElementHelper.legalPropertyKeyValueArray(keyValues); ElementHelper.validateProperty(key, value); final Optional<Object> optionalId = ElementHelper.getIdValue(keyValues); final Optional<VertexProperty<V>> optionalVertexProperty = ElementHelper.stageVertexProperty(this, cardinality, key, value, keyValues); if (optionalVertexProperty.isPresent()) return optionalVertexProperty.get(); if (TinkerHelper.inComputerMode(this.graph)) { final VertexProperty<V> vertexProperty = (VertexProperty<V>) this.graph.graphComputerView.addProperty(this, key, value); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } else { final Object idValue = optionalId.isPresent() ? graph.vertexPropertyIdManager.convert(optionalId.get()) : graph.vertexPropertyIdManager.getNextId(graph); final VertexProperty<V> vertexProperty = new TinkerVertexProperty<V>(idValue, this, key, value); if (null == this.properties) this.properties = new HashMap<>(); final List<VertexProperty> list = this.properties.getOrDefault(key, new ArrayList<>()); list.add(vertexProperty); this.properties.put(key, list); TinkerHelper.autoUpdateIndex(this, key, value, null); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } }
@Override public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) { if (this.removed) throw elementAlreadyRemoved(Vertex.class, id); ElementHelper.legalPropertyKeyValueArray(keyValues); ElementHelper.validateProperty(key, value); final Optional<Object> optionalId = ElementHelper.getIdValue(keyValues); final Optional<VertexProperty<V>> optionalVertexProperty = ElementHelper.stageVertexProperty(this, cardinality, key, value, keyValues); if (optionalVertexProperty.isPresent()) return optionalVertexProperty.get(); if (TinkerHelper.inComputerMode(this.graph)) { final VertexProperty<V> vertexProperty = (VertexProperty<V>) this.graph.graphComputerView.addProperty(this, key, value); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } else { final Object idValue = optionalId.isPresent() ? graph.vertexPropertyIdManager.convert(optionalId.get()) : graph.vertexPropertyIdManager.getNextId(graph); final VertexProperty<V> vertexProperty = new TinkerVertexProperty<V>(idValue, this, key, value); if (null == this.properties) this.properties = new HashMap<>(); final List<VertexProperty> list = this.properties.getOrDefault(key, new ArrayList<>()); list.add(vertexProperty); this.properties.put(key, list); TinkerHelper.autoUpdateIndex(this, key, value, null); ElementHelper.attachProperties(vertexProperty, keyValues); return vertexProperty; } }
private <T extends Element> Iterator<T> createElementIterator(final Class<T> clazz, final Map<Object, T> elements, final IdManager idManager, final Object... ids) { final Iterator<T> iterator; if (0 == ids.length) { iterator = elements.values().iterator(); } else { final List<Object> idList = Arrays.asList(ids); validateHomogenousIds(idList); // if the type is of Element - have to look each up because it might be an Attachable instance or // other implementation. the assumption is that id conversion is not required for detached // stuff - doesn't seem likely someone would detach a Titan vertex then try to expect that // vertex to be findable in OrientDB return clazz.isAssignableFrom(ids[0].getClass()) ? IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(clazz.cast(id).id())).iterator(), Objects::nonNull) : IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(idManager.convert(id))).iterator(), Objects::nonNull); } return TinkerHelper.inComputerMode(this) ? (Iterator<T>) (clazz.equals(Vertex.class) ? IteratorUtils.filter((Iterator<Vertex>) iterator, t -> this.graphComputerView.legalVertex(t)) : IteratorUtils.filter((Iterator<Edge>) iterator, t -> this.graphComputerView.legalEdge(t.outVertex(), t))) : iterator; }
private <T extends Element> Iterator<T> createElementIterator(final Class<T> clazz, final Map<Object, T> elements, final IdManager idManager, final Object... ids) { final Iterator<T> iterator; if (0 == ids.length) { iterator = elements.values().iterator(); } else { final List<Object> idList = Arrays.asList(ids); validateHomogenousIds(idList); // if the type is of Element - have to look each up because it might be an Attachable instance or // other implementation. the assumption is that id conversion is not required for detached // stuff - doesn't seem likely someone would detach a Titan vertex then try to expect that // vertex to be findable in OrientDB return clazz.isAssignableFrom(ids[0].getClass()) ? IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(clazz.cast(id).id())).iterator(), Objects::nonNull) : IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(idManager.convert(id))).iterator(), Objects::nonNull); } return TinkerHelper.inComputerMode(this) ? (Iterator<T>) (clazz.equals(Vertex.class) ? IteratorUtils.filter((Iterator<Vertex>) iterator, t -> this.graphComputerView.legalVertex(t)) : IteratorUtils.filter((Iterator<Edge>) iterator, t -> this.graphComputerView.legalEdge(t.outVertex(), t))) : iterator; }
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; }
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; }
@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; }
@Override public boolean willAllowId(final Object id) { return vertexIdManager.allow(id); }
/** * 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(((TinkerGraph) vertex.graph()).vertexPropertyIdManager.getNextId((TinkerGraph) vertex.graph()), key); this.vertex = vertex; this.key = key; this.value = value; ElementHelper.legalPropertyKeyValueArray(propertyKeyValues); ElementHelper.attachProperties(this, propertyKeyValues); }
@Override public boolean willAllowId(final Object id) { return vertexIdManager.allow(id); }
@Override public boolean willAllowId(final Object id) { return vertexIdManager.allow(id); } }
/** * 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(((TinkerGraph) vertex.graph()).vertexPropertyIdManager.getNextId((TinkerGraph) vertex.graph()), key); this.vertex = vertex; this.key = key; this.value = value; ElementHelper.legalPropertyKeyValueArray(propertyKeyValues); ElementHelper.attachProperties(this, propertyKeyValues); }
@Override public boolean willAllowId(final Object id) { return edgeIdManager.allow(id); } }
@Override public boolean willAllowId(final Object id) { return vertexIdManager.allow(id); } }
@Override public boolean willAllowId(final Object id) { return edgeIdManager.allow(id); } }
private <T extends Element> Iterator<T> createElementIterator(final Class<T> clazz, final Map<Object, T> elements, final IdManager idManager, final Object... ids) { final Iterator<T> iterator; if (0 == ids.length) { iterator = elements.values().iterator(); } else { final List<Object> idList = Arrays.asList(ids); validateHomogenousIds(idList); // if the type is of Element - have to look each up because it might be an Attachable instance or // other implementation. the assumption is that id conversion is not required for detached // stuff - doesn't seem likely someone would detach a Titan vertex then try to expect that // vertex to be findable in OrientDB return clazz.isAssignableFrom(ids[0].getClass()) ? IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(clazz.cast(id).id())).iterator(), Objects::nonNull) : IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(idManager.convert(id))).iterator(), Objects::nonNull); } return TinkerHelper.inComputerMode(this) ? (Iterator<T>) (clazz.equals(Vertex.class) ? IteratorUtils.filter((Iterator<Vertex>) iterator, t -> this.graphComputerView.legalVertex(t)) : IteratorUtils.filter((Iterator<Edge>) iterator, t -> this.graphComputerView.legalEdge(t.outVertex(), t))) : iterator; }
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; }