
org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.tinkergraph.structure;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import org.apache.tinkerpop.gremlin.util.CollectionUtil;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class TinkerVertex extends TinkerElement implements Vertex {
protected Map> properties;
// Edges should be used by non-transaction Graph due to performance
protected Map> outEdges;
protected Map> inEdges;
// Edge ids are for transactional Graph
protected Map> outEdgesId;
protected Map> inEdgesId;
protected final AbstractTinkerGraph graph;
private boolean allowNullPropertyValues;
private final boolean isTxMode;
protected TinkerVertex(final Object id, final String label, final AbstractTinkerGraph graph) {
super(id, label);
this.graph = graph;
this.isTxMode = graph instanceof TinkerTransactionGraph;
this.allowNullPropertyValues = graph.features().vertex().supportsNullPropertyValues();
}
protected TinkerVertex(final Object id, final String label, final AbstractTinkerGraph graph, final long currentVersion) {
super(id, label, currentVersion);
this.graph = graph;
this.isTxMode = graph instanceof TinkerTransactionGraph;
this.allowNullPropertyValues = graph.features().vertex().supportsNullPropertyValues();
}
@Override
public Object clone() {
if (!isTxMode) {
final TinkerVertex vertex = new TinkerVertex(id, label, graph, currentVersion);
vertex.inEdgesId = inEdgesId;
vertex.outEdgesId = outEdgesId;
vertex.properties = properties;
return vertex;
}
final TinkerVertex vertex = new TinkerVertex(id, label, graph, currentVersion);
if (inEdgesId != null)
vertex.inEdgesId = CollectionUtil.clone((ConcurrentHashMap>) inEdgesId);
if (outEdgesId != null)
vertex.outEdgesId = CollectionUtil.clone((ConcurrentHashMap>) outEdgesId);
if (properties != null) {
final ConcurrentHashMap> result = new ConcurrentHashMap<>(properties.size());
// clone will not work because TinkerVertexProperty contains link to Vertex
for (Map.Entry> entry : properties.entrySet()) {
final List clonedValue = entry.getValue().stream()
.map(vp ->((TinkerVertexProperty) vp).copy(vertex))
.collect(Collectors.toList());
result.put(entry.getKey(), clonedValue);
}
vertex.properties = result;
}
return vertex;
}
@Override
public Graph graph() {
return this.graph;
}
@Override
public VertexProperty property(final String key) {
if (this.removed) return VertexProperty.empty();
if (TinkerHelper.inComputerMode(this.graph)) {
final List list = (List) this.graph.graphComputerView.getProperty(this, key);
if (list.size() == 0)
return VertexProperty.empty();
else if (list.size() == 1)
return list.get(0);
else
throw Vertex.Exceptions.multiplePropertiesExistForProvidedKey(key);
} else {
if (this.properties != null && this.properties.containsKey(key)) {
final List list = this.properties.get(key);
if (list.size() > 1)
throw Vertex.Exceptions.multiplePropertiesExistForProvidedKey(key);
else
return list.get(0);
} else
return VertexProperty.empty();
}
}
@Override
public VertexProperty property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
graph.touch(this);
if (this.removed) throw elementAlreadyRemoved(Vertex.class, id);
ElementHelper.legalPropertyKeyValueArray(keyValues);
ElementHelper.validateProperty(key, value);
// if we don't allow null property values and the value is null then the key can be removed but only if the
// cardinality is single. if it is list/set then we can just ignore the null.
if (!allowNullPropertyValues && null == value) {
final VertexProperty.Cardinality card = null == cardinality ? graph.features().vertex().getCardinality(key) : cardinality;
if (VertexProperty.Cardinality.single == card)
properties(key).forEachRemaining(VertexProperty::remove);
return VertexProperty.empty();
}
final Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy