You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/02/12 14:01:31 UTC
[05/77] [partial] incubator-tinkerpop git commit: moved com/tinkerpop
directories to org/apache/tinkerpop
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchGraph.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchGraph.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchGraph.java
new file mode 100644
index 0000000..4820501
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchGraph.java
@@ -0,0 +1,656 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch;
+
+import com.tinkerpop.gremlin.process.T;
+import com.tinkerpop.gremlin.process.Traversal;
+import com.tinkerpop.gremlin.process.computer.GraphComputer;
+import com.tinkerpop.gremlin.process.graph.traversal.GraphTraversal;
+import com.tinkerpop.gremlin.process.graph.traversal.VertexTraversal;
+import com.tinkerpop.gremlin.structure.Direction;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Transaction;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.VertexProperty;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+import com.tinkerpop.gremlin.structure.util.batch.cache.VertexCache;
+import com.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+/**
+ * {@code BatchGraph} is a wrapper that enables batch loading of a large number of edges and vertices by chunking the entire
+ * load into smaller batches and maintaining a sideEffects-efficient vertex cache so that the entire transactional state can
+ * be flushed after each chunk is loaded.
+ * <br />
+ * {@code BatchGraph} is ONLY meant for loading data and does not support any retrieval or removal operations.
+ * That is, BatchGraph only supports the following methods:
+ * - {@link #addVertex(Object...)} for adding vertices
+ * - {@link Vertex#addEdge(String, com.tinkerpop.gremlin.structure.Vertex, Object...)} for adding edges
+ * - {@link #V(Object...)} to be used when adding edges
+ * - Property getter, setter and removal methods for vertices and edges.
+ * <br />
+ * An important limitation of BatchGraph is that edge properties can only be set immediately after the edge has been added.
+ * If other vertices or edges have been created in the meantime, setting, getting or removing properties will throw
+ * exceptions. This is done to avoid caching of edges which would require a great amount of sideEffects.
+ * <br />
+ * {@code BatchGraph} can also automatically set the provided element ids as properties on the respective element. Use
+ * {@link Builder#vertexIdKey(String)} and {@link Builder#edgeIdKey(String)} to set the keys
+ * for the vertex and edge properties respectively. This allows to make the loaded baseGraph compatible for later
+ * operation with {@link com.tinkerpop.gremlin.structure.strategy.IdStrategy}.
+ * <br/>
+ * Note that {@code BatchGraph} itself is not a {@link com.tinkerpop.gremlin.structure.strategy.GraphStrategy} because
+ * it requires that the {@link Vertex} implementation not hold on to the underlying {@link Vertex} reference and
+ * {@link com.tinkerpop.gremlin.structure.strategy.StrategyVertex} does that by it's very nature. While it might
+ * be possible to work around this issue, it is likely better for performance to simply leave this as a "half-wrapper"
+ * implementation, instead of forcing it into a {@link com.tinkerpop.gremlin.structure.strategy.GraphStrategy}.
+ *
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class BatchGraph<G extends Graph> implements Graph, Graph.Iterators {
+ /**
+ * Default buffer size is 10000.
+ */
+ public static final long DEFAULT_BUFFER_SIZE = 10000;
+
+ private final G baseGraph;
+
+ private final String vertexIdKey;
+ private final String edgeIdKey;
+ private final boolean incrementalLoading;
+ private final boolean baseSupportsSuppliedVertexId;
+ private final boolean baseSupportsSuppliedEdgeId;
+ private final boolean baseSupportsTransactions;
+ private final BiConsumer<Element, Object[]> existingVertexStrategy;
+ private final BiConsumer<Element, Object[]> existingEdgeStrategy;
+
+ private final VertexCache cache;
+
+ private final long bufferSize;
+ private long remainingBufferSize;
+
+ private BatchEdge currentEdge = null;
+ private Edge currentEdgeCached = null;
+
+ private Object previousOutVertexId = null;
+
+ private final BatchFeatures batchFeatures;
+
+ private final Transaction batchTransaction;
+
+ /**
+ * Constructs a BatchGraph wrapping the provided baseGraph, using the specified buffer size and expecting vertex
+ * ids of the specified IdType. Supplying vertex ids which do not match this type will throw exceptions.
+ *
+ * @param graph Graph to be wrapped
+ * @param type Type of vertex id expected. This information is used to apply the vertex cache
+ * sideEffects footprint.
+ * @param bufferSize Defines the number of vertices and edges loaded before starting a new transaction. The
+ * larger this value, the more sideEffects is required but the faster the loading process.
+ */
+ private BatchGraph(final G graph, final VertexIdType type, final long bufferSize, final String vertexIdKey,
+ final String edgeIdKey, final boolean incrementalLoading,
+ final BiConsumer<Element, Object[]> existingVertexStrategy,
+ final BiConsumer<Element, Object[]> existingEdgeStrategy) {
+ this.baseGraph = graph;
+ this.batchTransaction = new BatchTransaction();
+ this.batchFeatures = new BatchFeatures(graph.features());
+ this.bufferSize = bufferSize;
+ this.cache = type.getVertexCache();
+ this.remainingBufferSize = this.bufferSize;
+ this.vertexIdKey = vertexIdKey;
+ this.edgeIdKey = edgeIdKey;
+ this.incrementalLoading = incrementalLoading;
+ this.baseSupportsSuppliedEdgeId = this.baseGraph.features().edge().supportsUserSuppliedIds();
+ this.baseSupportsSuppliedVertexId = this.baseGraph.features().vertex().supportsUserSuppliedIds();
+ this.baseSupportsTransactions = this.baseGraph.features().graph().supportsTransactions();
+ this.existingEdgeStrategy = existingEdgeStrategy;
+ this.existingVertexStrategy = existingVertexStrategy;
+ }
+
+ private void nextElement() {
+ currentEdge = null;
+ currentEdgeCached = null;
+ if (remainingBufferSize <= 0) {
+ if (this.baseSupportsTransactions) baseGraph.tx().commit();
+ cache.newTransaction();
+ remainingBufferSize = bufferSize;
+ }
+ remainingBufferSize--;
+ }
+
+ private Vertex retrieveFromCache(final Object externalID) {
+ final Object internal = cache.getEntry(externalID);
+ if (internal instanceof Vertex) {
+ return (Vertex) internal;
+ } else if (internal != null) { //its an internal id
+ final Vertex v = baseGraph.V(internal).next();
+ cache.set(v, externalID);
+ return v;
+ } else return null;
+ }
+
+ private Vertex getCachedVertex(final Object externalID) {
+ final Vertex v = retrieveFromCache(externalID);
+ if (v == null) throw new IllegalArgumentException("Vertex for given ID cannot be found: " + externalID);
+ return v;
+ }
+
+ @Override
+ public Vertex addVertex(final Object... keyValues) {
+ final Object id = ElementHelper.getIdValue(keyValues).orElseThrow(() -> new IllegalArgumentException("Vertex id value cannot be null"));
+ if (!incrementalLoading && retrieveFromCache(id) != null)
+ throw new IllegalArgumentException("Vertex id already exists");
+ nextElement();
+
+ // if the vertexIdKey is not the T.id then append it as a name/value pair. this will overwrite what
+ // is present in that field already
+ final Object[] keysVals = T.id.getAccessor().equals(vertexIdKey) ? keyValues : ElementHelper.upsert(keyValues, vertexIdKey, id);
+
+ // if the graph doesn't support vertex ids or the vertex id is not the T.id then remove that key
+ // value pair as it will foul up insertion (i.e. an exception for graphs that don't support it and the
+ // id will become the value of the vertex id which might not be expected.
+ final Optional<Object[]> kvs = this.baseSupportsSuppliedVertexId && T.id.getAccessor().equals(vertexIdKey) ?
+ Optional.ofNullable(keyValues) : ElementHelper.remove(T.id, keysVals);
+
+ Vertex currentVertex;
+ if (!incrementalLoading)
+ currentVertex = kvs.isPresent() ? baseGraph.addVertex(kvs.get()) : baseGraph.addVertex();
+ else {
+ final Traversal<Vertex, Vertex> traversal = baseGraph.V().has(vertexIdKey, id);
+ if (traversal.hasNext()) {
+ final Vertex v = traversal.next();
+ if (traversal.hasNext())
+ throw new IllegalStateException(String.format("There is more than one vertex identified by %s=%s", vertexIdKey, id));
+
+ // let the caller decide how to handle conflict
+ kvs.ifPresent(keyvals -> existingVertexStrategy.accept(v, keyvals));
+ currentVertex = v;
+ } else
+ currentVertex = kvs.isPresent() ? baseGraph.addVertex(kvs.get()) : baseGraph.addVertex();
+ }
+
+ cache.set(currentVertex, id);
+
+ return new BatchVertex(id);
+ }
+
+ @Override
+ public GraphTraversal<Edge, Edge> E(final Object... edgeIds) {
+ throw retrievalNotSupported();
+ }
+
+ @Override
+ public Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public Iterator<Vertex> vertexIterator(final Object... vertexIds) {
+ if (vertexIds.length > 1)
+ throw new IllegalArgumentException("BatchGraph only allows a single vertex id at one time");
+ if ((this.previousOutVertexId != null) && (this.previousOutVertexId.equals(vertexIds[0]))) {
+ return IteratorUtils.of(new BatchVertex(this.previousOutVertexId));
+ } else {
+ Vertex vertex = retrieveFromCache(vertexIds[0]);
+ if (null == vertex) {
+ if (!this.incrementalLoading) return Collections.emptyIterator();
+ else {
+ final Iterator<Vertex> iterator = this.baseGraph.V().has(this.vertexIdKey, vertexIds[0]);
+ if (!iterator.hasNext()) return Collections.emptyIterator();
+ vertex = iterator.next();
+ if (iterator.hasNext())
+ throw new IllegalStateException("There are multiple vertices with the provided id in the graph: " + vertexIds[0]);
+ this.cache.set(vertex, vertexIds[0]);
+ }
+ }
+ return IteratorUtils.of(new BatchVertex(vertexIds[0]));
+ }
+ }
+
+ @Override
+ public Iterator<Edge> edgeIterator(final Object... edgeIds) {
+ throw retrievalNotSupported();
+ }
+
+ @Override
+ public <T extends Traversal<S, S>, S> T of(final Class<T> traversalClass) {
+ throw retrievalNotSupported();
+ }
+
+ @Override
+ public GraphComputer compute(final Class... graphComputerClass) {
+ throw Exceptions.graphComputerNotSupported();
+ }
+
+ @Override
+ public Transaction tx() {
+ return this.batchTransaction;
+ }
+
+ @Override
+ public Variables variables() {
+ throw Exceptions.variablesNotSupported();
+ }
+
+ @Override
+ public Configuration configuration() {
+ return new BaseConfiguration();
+ }
+
+ @Override
+ public Features features() {
+ return this.batchFeatures;
+ }
+
+ @Override
+ public void close() throws Exception {
+ baseGraph.close();
+
+ // call reset after the close in case the close behavior fails
+ reset();
+ }
+
+ private void reset() {
+ currentEdge = null;
+ currentEdgeCached = null;
+ remainingBufferSize = 0;
+ }
+
+ public static <T extends Graph> Builder build(final T g) {
+ return new Builder<>(g);
+ }
+
+ private class BatchTransaction implements Transaction {
+ private final boolean supportsTx;
+
+ public BatchTransaction() {
+ supportsTx = baseGraph.features().graph().supportsTransactions();
+ }
+
+ @Override
+ public Transaction onClose(final Consumer<Transaction> consumer) {
+ throw new UnsupportedOperationException("Transaction behavior cannot be altered in batch mode - set the behavior on the base graph");
+ }
+
+ @Override
+ public Transaction onReadWrite(final Consumer<Transaction> consumer) {
+ throw new UnsupportedOperationException("Transaction behavior cannot be altered in batch mode - set the behavior on the base graph");
+ }
+
+ @Override
+ public void close() {
+ if (supportsTx) baseGraph.tx().close();
+
+ // call reset after the close in case the close behavior fails
+ reset();
+ }
+
+ @Override
+ public void readWrite() {
+ if (supportsTx) baseGraph.tx().readWrite();
+ }
+
+ @Override
+ public boolean isOpen() {
+ return !supportsTx || baseGraph.tx().isOpen();
+ }
+
+ @Override
+ public <G extends Graph> G create() {
+ throw new UnsupportedOperationException("Cannot start threaded transaction during batch loading");
+ }
+
+ @Override
+ public <R> Workload<R> submit(final Function<Graph, R> work) {
+ throw new UnsupportedOperationException("Cannot submit a workload during batch loading");
+ }
+
+ @Override
+ public void rollback() {
+ throw new UnsupportedOperationException("Cannot issue a rollback during batch loading");
+ }
+
+ @Override
+ public void commit() {
+ if (supportsTx) baseGraph.tx().commit();
+
+ // call reset after the close in case the close behavior fails
+ reset();
+ }
+
+ @Override
+ public void open() {
+ if (supportsTx) baseGraph.tx().open();
+ }
+ }
+
+ private class BatchVertex implements Vertex, Vertex.Iterators, VertexTraversal {
+
+ private final Object externalID;
+
+ BatchVertex(final Object id) {
+ if (id == null) throw new IllegalArgumentException("External id may not be null");
+ externalID = id;
+ }
+
+ @Override
+ public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
+ if (!BatchVertex.class.isInstance(inVertex))
+ throw new IllegalArgumentException("Given element was not created in this baseGraph");
+ nextElement();
+
+ final Vertex ov = getCachedVertex(externalID);
+ final Vertex iv = getCachedVertex(inVertex.id());
+
+ previousOutVertexId = externalID; //keep track of the previous out vertex id
+
+ if (!incrementalLoading) {
+ final Optional<Object[]> kvs = baseSupportsSuppliedEdgeId && T.id.getAccessor().equals(edgeIdKey) ?
+ Optional.ofNullable(keyValues) : ElementHelper.remove(T.id, keyValues);
+ currentEdgeCached = kvs.isPresent() ? ov.addEdge(label, iv, kvs.get()) : ov.addEdge(label, iv);
+ } else {
+ final Optional<Object> id = ElementHelper.getIdValue(keyValues);
+ // if the edgeIdKey is not the Element.ID then append it as a name/value pair. this will overwrite what
+ // is present in that field already
+ final Object[] keysVals = id.isPresent() && T.id.getAccessor().equals(edgeIdKey) ? keyValues :
+ id.isPresent() ? ElementHelper.upsert(keyValues, edgeIdKey, id.get()) : keyValues;
+
+ // if the graph doesn't support edge ids or the edge id is not the Element.ID then remove that key
+ // value pair as it will foul up insertion (i.e. an exception for graphs that don't support it and the
+ // id will become the value of the edge id which might not be expected.
+ final Optional<Object[]> kvs = baseSupportsSuppliedEdgeId && T.id.getAccessor().equals(edgeIdKey) ?
+ Optional.ofNullable(keyValues) : ElementHelper.remove(T.id, keysVals);
+
+ if (id.isPresent()) {
+ final Traversal<Edge, Edge> traversal = baseGraph.E().has(edgeIdKey, id.get());
+ if (traversal.hasNext()) {
+ final Edge e = traversal.next();
+ // let the user decide how to handle conflict
+ kvs.ifPresent(keyvals -> existingEdgeStrategy.accept(e, keyvals));
+ currentEdgeCached = e;
+ } else
+ currentEdgeCached = kvs.isPresent() ? ov.addEdge(label, iv, kvs.get()) : ov.addEdge(label, iv);
+ } else {
+ currentEdgeCached = kvs.isPresent() ? ov.addEdge(label, iv, kvs.get()) : ov.addEdge(label, iv);
+ }
+ }
+
+ currentEdge = new BatchEdge();
+
+ return currentEdge;
+ }
+
+ @Override
+ public Object id() {
+ return this.externalID;
+ }
+
+ @Override
+ public Graph graph() {
+ return getCachedVertex(externalID).graph();
+ }
+
+ @Override
+ public String label() {
+ return getCachedVertex(externalID).label();
+ }
+
+ @Override
+ public void remove() {
+ throw removalNotSupported();
+ }
+
+ @Override
+ public Set<String> keys() {
+ return getCachedVertex(externalID).keys();
+ }
+
+ @Override
+ public <V> VertexProperty<V> property(final String key, final V value, final Object... keyValues) {
+ return getCachedVertex(externalID).property(key, value, keyValues);
+ }
+
+ @Override
+ public <V> VertexProperty<V> property(final String key) {
+ return getCachedVertex(externalID).property(key);
+ }
+
+ @Override
+ public <V> VertexProperty<V> property(final String key, final V value) {
+ return getCachedVertex(externalID).property(key, value);
+ }
+
+ @Override
+ public <V> V value(final String key) throws NoSuchElementException {
+ return getCachedVertex(externalID).value(key);
+ }
+
+ @Override
+ public Vertex.Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public Iterator<Edge> edgeIterator(final Direction direction, final String... edgeLabels) {
+ throw retrievalNotSupported();
+ }
+
+ @Override
+ public Iterator<Vertex> vertexIterator(final Direction direction, final String... labels) {
+ throw retrievalNotSupported();
+ }
+
+ @Override
+ public <V> Iterator<VertexProperty<V>> propertyIterator(final String... propertyKeys) {
+ return getCachedVertex(externalID).iterators().propertyIterator(propertyKeys);
+ }
+ }
+
+ private class BatchEdge implements Edge, Edge.Iterators {
+
+
+ @Override
+ public Graph graph() {
+ return getWrappedEdge().graph();
+ }
+
+ @Override
+ public Object id() {
+ return getWrappedEdge().label();
+ }
+
+ @Override
+ public String label() {
+ return getWrappedEdge().label();
+ }
+
+ @Override
+ public void remove() {
+ throw removalNotSupported();
+ }
+
+ @Override
+ public <V> Property<V> property(final String key) {
+ return getWrappedEdge().property(key);
+ }
+
+ @Override
+ public <V> Property<V> property(final String key, final V value) {
+ return getWrappedEdge().property(key, value);
+ }
+
+ @Override
+ public Set<String> keys() {
+ return getWrappedEdge().keys();
+ }
+
+ @Override
+ public <V> V value(final String key) throws NoSuchElementException {
+ return getWrappedEdge().value(key);
+ }
+
+ private Edge getWrappedEdge() {
+ if (this != currentEdge) {
+ throw new UnsupportedOperationException("This edge is no longer in scope");
+ }
+ return currentEdgeCached;
+ }
+
+ @Override
+ public Edge.Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public <V> Iterator<Property<V>> propertyIterator(final String... propertyKeys) {
+ return getWrappedEdge().iterators().propertyIterator(propertyKeys);
+ }
+
+ @Override
+ public Iterator<Vertex> vertexIterator(final Direction direction) {
+ return getWrappedEdge().iterators().vertexIterator(direction);
+ }
+ }
+
+ private static UnsupportedOperationException retrievalNotSupported() {
+ return new UnsupportedOperationException("Retrieval operations are not supported during batch loading");
+ }
+
+ private static UnsupportedOperationException removalNotSupported() {
+ return new UnsupportedOperationException("Removal operations are not supported during batch loading");
+ }
+
+ public static class Builder<G extends Graph> {
+ private final G graphToLoad;
+ private boolean incrementalLoading = false;
+ private String vertexIdKey = T.id.getAccessor();
+ private String edgeIdKey = T.id.getAccessor();
+ private long bufferSize = DEFAULT_BUFFER_SIZE;
+ private VertexIdType vertexIdType = VertexIdType.OBJECT;
+ private BiConsumer<Element, Object[]> existingVertexStrategy = Exists.IGNORE;
+ private BiConsumer<Element, Object[]> existingEdgeStrategy = Exists.IGNORE;
+
+ private Builder(final G g) {
+ if (null == g) throw new IllegalArgumentException("Graph may not be null");
+ if (g instanceof BatchGraph)
+ throw new IllegalArgumentException("BatchGraph cannot wrap another BatchGraph instance");
+ this.graphToLoad = g;
+ }
+
+ /**
+ * Sets the key to be used when setting the vertex id as a property on the respective vertex. If this
+ * value is not set it defaults to {@link T#id}.
+ *
+ * @param key Key to be used.
+ */
+ public Builder vertexIdKey(final String key) {
+ if (null == key) throw new IllegalArgumentException("Key cannot be null");
+ this.vertexIdKey = key;
+ return this;
+ }
+
+ /**
+ * Sets the key to be used when setting the edge id as a property on the respective edge.
+ * If the key is null, then no property will be set.
+ *
+ * @param key Key to be used.
+ */
+ public Builder edgeIdKey(final String key) {
+ if (null == key) throw new IllegalArgumentException("Optional value for key cannot be null");
+ this.edgeIdKey = key;
+ return this;
+ }
+
+ /**
+ * Number of mutations to perform between calls to {@link com.tinkerpop.gremlin.structure.Transaction#commit}.
+ */
+ public Builder bufferSize(long bufferSize) {
+ if (bufferSize <= 0) throw new IllegalArgumentException("BufferSize must be positive");
+ this.bufferSize = bufferSize;
+ return this;
+ }
+
+ /**
+ * Sets the type of the id used for the vertex which in turn determines the cache type that is used.
+ */
+ public Builder vertexIdType(final VertexIdType type) {
+ if (null == type) throw new IllegalArgumentException("Type may not be null");
+ this.vertexIdType = type;
+ return this;
+ }
+
+ /**
+ * Sets whether the graph loaded through this instance of {@link BatchGraph} is loaded from scratch
+ * (i.e. the wrapped graph is initially empty) or whether graph is loaded incrementally into an
+ * existing graph.
+ * <p/>
+ * In the former case, BatchGraph does not need to check for the existence of vertices with the wrapped
+ * graph but only needs to consult its own cache which can be significantly faster. In the latter case,
+ * the cache is checked first but an additional check against the wrapped graph may be necessary if
+ * the vertex does not exist.
+ * <p/>
+ * By default, BatchGraph assumes that the data is loaded from scratch.
+ */
+ public Builder incrementalLoading(final boolean incrementalLoading) {
+ this.incrementalLoading = incrementalLoading;
+ return this;
+ }
+
+ /**
+ * Sets whether the graph loaded through this instance of {@link BatchGraph} is loaded from scratch
+ * (i.e. the wrapped graph is initially empty) or whether graph is loaded incrementally into an
+ * existing graph.
+ * <p/>
+ * In the former case, BatchGraph does not need to check for the existence of vertices with the wrapped
+ * graph but only needs to consult its own cache which can be significantly faster. In the latter case,
+ * the cache is checked first but an additional check against the wrapped graph may be necessary if
+ * the vertex does not exist.
+ * <p/>
+ * By default, BatchGraph assumes that the data is loaded from scratch.
+ */
+ public Builder incrementalLoading(final boolean incrementalLoading,
+ final BiConsumer<Element, Object[]> existingVertexStrategy,
+ final BiConsumer<Element, Object[]> existingEdgeStrategy) {
+ this.incrementalLoading = incrementalLoading;
+ this.existingVertexStrategy = existingVertexStrategy;
+ this.existingEdgeStrategy = existingEdgeStrategy;
+ return this;
+ }
+
+ public BatchGraph<G> create() {
+ return new BatchGraph<>(graphToLoad, vertexIdType, bufferSize, vertexIdKey, edgeIdKey,
+ incrementalLoading, this.existingVertexStrategy, this.existingEdgeStrategy);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/Exists.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/Exists.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/Exists.java
new file mode 100644
index 0000000..08d5ee1
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/Exists.java
@@ -0,0 +1,60 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch;
+
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+
+import java.util.function.BiConsumer;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public enum Exists implements BiConsumer<Element, Object[]> {
+ IGNORE {
+ @Override
+ public void accept(final Element element, final Object[] objects) {
+ // do nothing
+ }
+ },
+ THROW {
+ @Override
+ public void accept(final Element element, final Object[] objects) {
+ throw new IllegalStateException(String.format(
+ "Element of type %s with id of [%s] was not expected to exist in target graph",
+ element.getClass().getSimpleName(), element.id()));
+ }
+ },
+ OVERWRITE {
+ @Override
+ public void accept(final Element element, final Object[] keyValues) {
+ ElementHelper.attachProperties(element, keyValues);
+ }
+ },
+ OVERWRITE_SINGLE {
+ @Override
+ public void accept(final Element element, final Object[] keyValues) {
+ if (element instanceof Vertex)
+ ElementHelper.attachSingleProperties((Vertex) element, keyValues);
+ else
+ ElementHelper.attachProperties(element, keyValues);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/VertexIdType.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/VertexIdType.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/VertexIdType.java
new file mode 100644
index 0000000..30eb2f8
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/VertexIdType.java
@@ -0,0 +1,65 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch;
+
+import com.tinkerpop.gremlin.structure.util.batch.cache.LongIDVertexCache;
+import com.tinkerpop.gremlin.structure.util.batch.cache.ObjectIDVertexCache;
+import com.tinkerpop.gremlin.structure.util.batch.cache.StringIDVertexCache;
+import com.tinkerpop.gremlin.structure.util.batch.cache.URLCompression;
+import com.tinkerpop.gremlin.structure.util.batch.cache.VertexCache;
+
+/**
+ * Type of vertex ids expected by BatchGraph. The default is IdType.OBJECT.
+ * Use the IdType that best matches the used vertex id types in order to save sideEffects.
+ *
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ */
+public enum VertexIdType {
+
+ OBJECT {
+ @Override
+ public VertexCache getVertexCache() {
+ return new ObjectIDVertexCache();
+ }
+ },
+
+ NUMBER {
+ @Override
+ public VertexCache getVertexCache() {
+ return new LongIDVertexCache();
+ }
+ },
+
+ STRING {
+ @Override
+ public VertexCache getVertexCache() {
+ return new StringIDVertexCache();
+ }
+ },
+
+ URL {
+ @Override
+ public VertexCache getVertexCache() {
+ return new StringIDVertexCache(new URLCompression());
+
+ }
+ };
+
+ public abstract VertexCache getVertexCache();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/AbstractIDVertexCache.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/AbstractIDVertexCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/AbstractIDVertexCache.java
new file mode 100644
index 0000000..4dbb618
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/AbstractIDVertexCache.java
@@ -0,0 +1,78 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+import com.tinkerpop.gremlin.structure.Vertex;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+abstract class AbstractIDVertexCache implements VertexCache {
+
+ static final int INITIAL_CAPACITY = 1000;
+ static final int INITIAL_TX_CAPACITY = 100;
+
+ private final Map<Object, Object> map;
+ private final Set<Object> mapKeysInCurrentTx;
+
+ AbstractIDVertexCache() {
+ map = new HashMap<>(INITIAL_CAPACITY);
+ mapKeysInCurrentTx = new HashSet<>(INITIAL_TX_CAPACITY);
+ }
+
+ @Override
+ public Object getEntry(final Object externalId) {
+ return map.get(externalId);
+ }
+
+ @Override
+ public void set(final Vertex vertex, final Object externalId) {
+ setId(vertex, externalId);
+ }
+
+ @Override
+ public void setId(final Object vertexId, final Object externalId) {
+ map.put(externalId, vertexId);
+ mapKeysInCurrentTx.add(externalId);
+ }
+
+ @Override
+ public boolean contains(final Object externalId) {
+ return map.containsKey(externalId);
+ }
+
+ @Override
+ public void newTransaction() {
+ for (Object id : mapKeysInCurrentTx) {
+ Object o = map.get(id);
+ assert null != o;
+ if (o instanceof Vertex) {
+ Vertex v = (Vertex) o;
+ map.put(id, v.id());
+ }
+ }
+ mapKeysInCurrentTx.clear();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/LongIDVertexCache.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/LongIDVertexCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/LongIDVertexCache.java
new file mode 100644
index 0000000..885eb9b
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/LongIDVertexCache.java
@@ -0,0 +1,94 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+import com.carrotsearch.hppc.LongArrayList;
+import com.carrotsearch.hppc.LongObjectMap;
+import com.carrotsearch.hppc.LongObjectOpenHashMap;
+import com.carrotsearch.hppc.procedures.LongProcedure;
+import com.tinkerpop.gremlin.structure.Vertex;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class LongIDVertexCache implements VertexCache {
+
+ private final LongObjectMap<Object> map;
+ private final LongArrayList mapKeysInCurrentTx;
+ private final LongProcedure newTransactionProcedure;
+
+ public LongIDVertexCache() {
+ map = new LongObjectOpenHashMap<>(AbstractIDVertexCache.INITIAL_CAPACITY);
+ mapKeysInCurrentTx = new LongArrayList(AbstractIDVertexCache.INITIAL_TX_CAPACITY);
+ newTransactionProcedure = new VertexConverterLP();
+ }
+
+ private static long getID(final Object externalID) {
+ if (!(externalID instanceof Number)) throw new IllegalArgumentException("Number expected.");
+ return ((Number) externalID).longValue();
+ }
+
+ @Override
+ public Object getEntry(final Object externalId) {
+ return map.get(getID(externalId));
+ }
+
+ @Override
+ public void set(final Vertex vertex, final Object externalId) {
+ setId(vertex, externalId);
+ }
+
+ @Override
+ public void setId(final Object vertexId, final Object externalId) {
+ final long id = getID(externalId);
+ map.put(id, vertexId);
+ mapKeysInCurrentTx.add(id);
+ }
+
+ @Override
+ public boolean contains(final Object externalId) {
+ return map.containsKey(getID(externalId));
+ }
+
+ @Override
+ public void newTransaction() {
+ mapKeysInCurrentTx.forEach(newTransactionProcedure);
+ mapKeysInCurrentTx.clear();
+ }
+
+ /**
+ * See {@link LongIDVertexCache#newTransaction()}
+ */
+ private class VertexConverterLP implements LongProcedure {
+ /**
+ * Retrieve the Object associated with each long from {@code map}. If it
+ * is an {@code instanceof Vertex}, then replace it in the map with
+ * {@link Vertex#id()}. Otherwise, do nothing.
+ */
+ @Override
+ public void apply(final long l) {
+ final Object o = map.get(l);
+ assert null != o;
+ if (o instanceof Vertex) {
+ map.put(l, ((Vertex) o).id());
+ }
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/ObjectIDVertexCache.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/ObjectIDVertexCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/ObjectIDVertexCache.java
new file mode 100644
index 0000000..95e2150
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/ObjectIDVertexCache.java
@@ -0,0 +1,25 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ */
+public class ObjectIDVertexCache extends AbstractIDVertexCache {
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringCompression.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringCompression.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringCompression.java
new file mode 100644
index 0000000..ee04a59
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringCompression.java
@@ -0,0 +1,29 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public interface StringCompression {
+ public static final StringCompression NO_COMPRESSION = input -> input;
+
+ public String compress(final String input);
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringIDVertexCache.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringIDVertexCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringIDVertexCache.java
new file mode 100644
index 0000000..b69c958
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/StringIDVertexCache.java
@@ -0,0 +1,87 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+import com.tinkerpop.gremlin.structure.Vertex;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class StringIDVertexCache implements VertexCache {
+
+ private static final int INITIAL_CAPACITY = 1000;
+ private static final int INITIAL_TX_CAPACITY = 100;
+
+ private final Map<String, Object> map;
+ private final Set<String> mapKeysInCurrentTx;
+ private final StringCompression compression;
+
+ public StringIDVertexCache(final StringCompression compression) {
+ if (compression == null) throw new IllegalArgumentException("Compression expected.");
+ this.compression = compression;
+ map = new HashMap<>(INITIAL_CAPACITY);
+ mapKeysInCurrentTx = new HashSet<>(INITIAL_TX_CAPACITY);
+ }
+
+ public StringIDVertexCache() {
+ this(StringCompression.NO_COMPRESSION);
+ }
+
+ @Override
+ public Object getEntry(final Object externalId) {
+ final String id = compression.compress(externalId.toString());
+ return map.get(id);
+ }
+
+ @Override
+ public void set(final Vertex vertex, final Object externalId) {
+ setId(vertex, externalId);
+ }
+
+ @Override
+ public void setId(final Object vertexId, final Object externalId) {
+ final String id = compression.compress(externalId.toString());
+ map.put(id, vertexId);
+ mapKeysInCurrentTx.add(id);
+ }
+
+ @Override
+ public boolean contains(final Object externalId) {
+ return map.containsKey(compression.compress(externalId.toString()));
+ }
+
+ @Override
+ public void newTransaction() {
+ for (String id : mapKeysInCurrentTx) {
+ final Object o = map.get(id);
+ assert null != o;
+ if (o instanceof Vertex) {
+ Vertex v = (Vertex) o;
+ map.put(id, v.id());
+ }
+ }
+ mapKeysInCurrentTx.clear();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/URLCompression.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/URLCompression.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/URLCompression.java
new file mode 100644
index 0000000..f06ef73
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/URLCompression.java
@@ -0,0 +1,67 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class URLCompression implements StringCompression {
+
+ private static final String DELIMITER = "$";
+
+ private int prefixCounter = 0;
+
+ private final Map<String, String> urlPrefix = new HashMap<>();
+
+ @Override
+ public String compress(final String input) {
+ final String[] url = splitURL(input);
+ String prefix = urlPrefix.get(url[0]);
+ if (prefix == null) {
+ //New Prefix
+ prefix = Long.toString(prefixCounter, Character.MAX_RADIX) + DELIMITER;
+ prefixCounter++;
+ urlPrefix.put(url[0], prefix);
+ }
+ return prefix + url[1];
+ }
+
+ private final static char[] urlDelimiters = new char[]{'/', '#', ':'};
+
+ private static String[] splitURL(final String url) {
+ final String[] res = new String[2];
+ int pos = -1;
+ for (char delimiter : urlDelimiters) {
+ int currentpos = url.lastIndexOf(delimiter);
+ if (currentpos > pos) pos = currentpos;
+ }
+ if (pos < 0) {
+ res[0] = "";
+ res[1] = url;
+ } else {
+ res[0] = url.substring(0, pos + 1);
+ res[1] = url.substring(pos + 1);
+ }
+ return res;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/VertexCache.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/VertexCache.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/VertexCache.java
new file mode 100644
index 0000000..32b5bc5
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/cache/VertexCache.java
@@ -0,0 +1,36 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.batch.cache;
+
+import com.tinkerpop.gremlin.structure.Vertex;
+
+/**
+ * @author Matthias Broecheler (http://www.matthiasb.com)
+ */
+public interface VertexCache {
+ public Object getEntry(final Object externalId);
+
+ public void set(final Vertex vertex, final Object externalId);
+
+ public void setId(final Object vertexId, final Object externalId);
+
+ public boolean contains(final Object externalId);
+
+ public void newTransaction();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/Attachable.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/Attachable.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/Attachable.java
new file mode 100644
index 0000000..fe62ffb
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/Attachable.java
@@ -0,0 +1,33 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Vertex;
+
+/**
+ * An interface that provides methods for detached properties and elements to be re-attached to the {@link Graph}.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public interface Attachable<T> {
+ public abstract T attach(final Vertex hostVertex) throws IllegalStateException;
+
+ public abstract T attach(final Graph hostGraph) throws IllegalStateException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
new file mode 100644
index 0000000..be20e6d
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
@@ -0,0 +1,166 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.process.T;
+import com.tinkerpop.gremlin.structure.Direction;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+import com.tinkerpop.gremlin.structure.util.StringFactory;
+import com.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.javatuples.Pair;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+/**
+ * Represents an {@link Edge} that is disconnected from a {@link Graph}. "Disconnection" can mean detachment from
+ * a {@link Graph} in the sense that the {@link Edge} was constructed from a {@link Graph} instance and this reference
+ * was removed or it can mean that the {@code DetachedEdge} could have been constructed independently of a
+ * {@link Graph} instance in the first place.
+ * <br/>
+ * A {@code DetachedEdge} only has reference to the properties and in/out vertices that are associated with it at the
+ * time of detachment (or construction) and is not traversable or mutable. Note that the references to the in/out
+ * vertices are {@link DetachedVertex} instances that only have reference to the
+ * {@link com.tinkerpop.gremlin.structure.Vertex#id()} and {@link com.tinkerpop.gremlin.structure.Vertex#label()}.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class DetachedEdge extends DetachedElement<Edge> implements Edge, Edge.Iterators {
+
+ private DetachedVertex outVertex;
+ private DetachedVertex inVertex;
+
+ private DetachedEdge() {
+
+ }
+
+ protected DetachedEdge(final Edge edge, final boolean withProperties) {
+ super(edge);
+ this.outVertex = DetachedFactory.detach(edge.iterators().vertexIterator(Direction.OUT).next(), false);
+ this.inVertex = DetachedFactory.detach(edge.iterators().vertexIterator(Direction.IN).next(), false);
+
+ // only serialize properties if requested, the graph supports it and there are meta properties present.
+ // this prevents unnecessary object creation of a new HashMap of a new HashMap which will just be empty.
+ // it will use Collections.emptyMap() by default
+ if (withProperties) {
+ final Iterator<Property<Object>> propertyIterator = edge.iterators().propertyIterator();
+ if (propertyIterator.hasNext()) {
+ this.properties = new HashMap<>();
+ propertyIterator.forEachRemaining(property -> this.properties.put(property.key(), Collections.singletonList(DetachedFactory.detach(property))));
+ }
+ }
+ }
+
+ public DetachedEdge(final Object id, final String label,
+ final Map<String, Object> properties,
+ final Pair<Object, String> outV,
+ final Pair<Object, String> inV) {
+ super(id, label);
+ this.outVertex = new DetachedVertex(outV.getValue0(), outV.getValue1(), Collections.emptyMap());
+ this.inVertex = new DetachedVertex(inV.getValue0(), inV.getValue1(), Collections.emptyMap());
+ if (!properties.isEmpty()) {
+ this.properties = new HashMap<>();
+ properties.entrySet().stream().forEach(entry -> this.properties.put(entry.getKey(), Collections.singletonList(new DetachedProperty<>(entry.getKey(), entry.getValue(), this))));
+ }
+ }
+
+
+ @Override
+ public String toString() {
+ return StringFactory.edgeString(this);
+ }
+
+ @Override
+ public Edge attach(final Vertex hostVertex) {
+ final Iterator<Edge> edges = IteratorUtils.filter(hostVertex.iterators().edgeIterator(Direction.OUT, this.label), edge -> edge.equals(this));
+ if (!edges.hasNext())
+ throw new IllegalStateException("The detached edge could not be be found incident to the provided vertex: " + this);
+ return edges.next();
+ }
+
+ @Override
+ public Edge attach(final Graph hostGraph) {
+ return hostGraph.iterators().edgeIterator(this.id).next();
+ }
+
+ public static Edge addTo(final Graph graph, final DetachedEdge detachedEdge) {
+ Vertex outV;
+ try {
+ outV = graph.iterators().vertexIterator(detachedEdge.outVertex.id()).next();
+ } catch (final NoSuchElementException e) {
+ outV = null;
+ }
+ if (null == outV) {
+ outV = graph.addVertex(T.id, detachedEdge.outVertex.id());
+ }
+
+ Vertex inV;
+ try {
+ inV = graph.iterators().vertexIterator(detachedEdge.inVertex.id()).next();
+ } catch (final NoSuchElementException e) {
+ inV = null;
+ }
+ if (null == inV) {
+ inV = graph.addVertex(T.id, detachedEdge.inVertex.id());
+ }
+
+ if (ElementHelper.areEqual(outV, inV)) {
+ final Iterator<Edge> itty = outV.iterators().edgeIterator(Direction.OUT, detachedEdge.label());
+ while (itty.hasNext()) {
+ final Edge e = itty.next();
+ if (ElementHelper.areEqual(detachedEdge, e))
+ return e;
+ }
+ }
+
+ final Edge e = outV.addEdge(detachedEdge.label(), inV, T.id, detachedEdge.id());
+ detachedEdge.properties.entrySet().forEach(kv -> kv.getValue().forEach(p -> e.<Object>property(kv.getKey(), p.value())));
+ return e;
+ }
+
+ @Override
+ public Edge.Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public Iterator<Vertex> vertexIterator(final Direction direction) {
+ switch (direction) {
+ case OUT:
+ return IteratorUtils.of(this.outVertex);
+ case IN:
+ return IteratorUtils.of(this.inVertex);
+ default:
+ return IteratorUtils.of(this.outVertex, this.inVertex);
+ }
+ }
+
+ @Override
+ public <V> Iterator<Property<V>> propertyIterator(final String... propertyKeys) {
+ return (Iterator) super.propertyIterator(propertyKeys);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
new file mode 100644
index 0000000..4acdcc9
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
@@ -0,0 +1,105 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public abstract class DetachedElement<E> implements Element, Element.Iterators, Serializable, Attachable<E> {
+
+ protected Object id;
+ protected String label;
+ protected Map<String, List<? extends Property>> properties = Collections.emptyMap();
+
+ protected DetachedElement() {
+
+ }
+
+ protected DetachedElement(final Element element) {
+ this(element.id(), element.label());
+ }
+
+ protected DetachedElement(final Object id, final String label) {
+ this.id = id;
+ this.label = label;
+ }
+
+ @Override
+ public Graph graph() {
+ throw new UnsupportedOperationException("The detached element is no longer attached to a graph");
+ }
+
+ @Override
+ public Object id() {
+ return this.id;
+ }
+
+ @Override
+ public String label() {
+ return this.label;
+ }
+
+ @Override
+ public <V> Property<V> property(final String key, final V value) {
+ throw new UnsupportedOperationException("Detached elements are readonly: " + this);
+ }
+
+ @Override
+ public <V> Property<V> property(final String key) {
+ return this.properties.containsKey(key) ? this.properties.get(key).get(0) : Property.empty();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Detached elements are readonly: " + this);
+ }
+
+ @Override
+ public int hashCode() {
+ return ElementHelper.hashCode(this);
+ }
+
+ @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
+ @Override
+ public boolean equals(final Object object) {
+ return ElementHelper.areEqual(this, object);
+ }
+
+ @Override
+ public Element.Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public <V> Iterator<? extends Property<V>> propertyIterator(final String... propertyKeys) {
+ return (Iterator) this.properties.entrySet().stream().filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)).flatMap(entry -> entry.getValue().stream()).iterator();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedFactory.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedFactory.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedFactory.java
new file mode 100644
index 0000000..09c0654
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedFactory.java
@@ -0,0 +1,74 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.process.Path;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.VertexProperty;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class DetachedFactory {
+ public static DetachedVertex detach(final Vertex vertex, final boolean withProperties) {
+ return vertex instanceof DetachedVertex ? (DetachedVertex) vertex : new DetachedVertex(vertex, withProperties);
+ }
+
+ public static DetachedEdge detach(final Edge edge, final boolean withProperties) {
+ return edge instanceof DetachedEdge ? (DetachedEdge) edge : new DetachedEdge(edge, withProperties);
+ }
+
+ public static <V> DetachedVertexProperty detach(final VertexProperty<V> vertexProperty, final boolean withProperties) {
+ return vertexProperty instanceof DetachedVertexProperty ? (DetachedVertexProperty) vertexProperty : new DetachedVertexProperty<>(vertexProperty, withProperties);
+ }
+
+ public static <V> DetachedProperty<V> detach(final Property<V> property) {
+ return property instanceof DetachedProperty ? (DetachedProperty<V>) property : new DetachedProperty<>(property);
+ }
+
+ public static DetachedPath detach(final Path path, final boolean withProperties) {
+ return path instanceof DetachedPath ? (DetachedPath) path : new DetachedPath(path, withProperties);
+ }
+
+ public static DetachedElement detach(final Element element, final boolean withProperties) {
+ if (element instanceof Vertex)
+ return detach((Vertex) element, withProperties);
+ else if (element instanceof Edge)
+ return detach((Edge) element, withProperties);
+ else if (element instanceof VertexProperty)
+ return detach((VertexProperty) element, withProperties);
+ else
+ throw new IllegalArgumentException("The provided argument is an unknown element: " + element + ':' + element.getClass());
+ }
+
+ public static <D> D detach(final Object object, final boolean withProperties) {
+ if (object instanceof Element) {
+ return (D) DetachedFactory.detach((Element) object, withProperties);
+ } else if (object instanceof Property) {
+ return (D) DetachedFactory.detach((Property) object);
+ } else if (object instanceof Path) {
+ return (D) DetachedFactory.detach((Path) object, withProperties);
+ } else {
+ return (D) object;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedPath.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedPath.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedPath.java
new file mode 100644
index 0000000..dd8cf4a
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedPath.java
@@ -0,0 +1,75 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.process.Path;
+import com.tinkerpop.gremlin.process.util.path.MutablePath;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class DetachedPath extends MutablePath implements Attachable<Path> {
+
+ private DetachedPath() {
+
+ }
+
+ protected DetachedPath(final Path path, final boolean withProperties) {
+ path.forEach((object, labels) -> {
+ if (object instanceof DetachedElement || object instanceof DetachedProperty || object instanceof DetachedPath) {
+ this.objects.add(object);
+ this.labels.add(labels);
+ } else if (object instanceof Element) {
+ this.objects.add(DetachedFactory.detach((Element) object, withProperties));
+ this.labels.add(labels);
+ } else if (object instanceof Property) {
+ this.objects.add(DetachedFactory.detach((Property) object));
+ this.labels.add(labels);
+ } else if (object instanceof Path) {
+ this.objects.add(DetachedFactory.detach((Path) object, withProperties));
+ this.labels.add(labels);
+ } else {
+ this.objects.add(object);
+ this.labels.add(labels);
+ }
+ });
+ }
+
+ @Override
+ public Path attach(final Graph hostGraph) {
+ final Path path = MutablePath.make(); // TODO: Use ImmutablePath?
+ this.forEach((object, labels) -> path.extend(object instanceof Attachable ? ((Attachable) object).attach(hostGraph) : object, labels.toArray(new String[labels.size()])));
+ return path;
+ }
+
+ @Override
+ public Path attach(final Vertex hostVertex) {
+ final Path path = MutablePath.make(); // TODO: Use ImmutablePath?
+ this.forEach((object, labels) -> path.extend(object instanceof Attachable ? ((Attachable) object).attach(hostVertex) : object, labels.toArray(new String[labels.size()])));
+ return path;
+ }
+
+ public String toString() {
+ return this.objects.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedProperty.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedProperty.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedProperty.java
new file mode 100644
index 0000000..21a5b1b
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedProperty.java
@@ -0,0 +1,116 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+import com.tinkerpop.gremlin.structure.util.StringFactory;
+
+import java.io.Serializable;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class DetachedProperty<V> implements Property, Serializable, Attachable<Property<V>> {
+
+ private String key;
+ private V value;
+ private transient DetachedElement element;
+
+ private DetachedProperty() {
+ }
+
+ protected DetachedProperty(final Property<V> property) {
+ this.key = property.key();
+ this.value = property.value();
+ this.element = DetachedFactory.detach(property.element(), false);
+ }
+
+ public DetachedProperty(final String key, final V value, final Element element) {
+ this.key = key;
+ this.value = value;
+ this.element = DetachedFactory.detach(element, false);
+ }
+
+
+ @Override
+ public boolean isPresent() {
+ return true;
+ }
+
+ @Override
+ public String key() {
+ return this.key;
+ }
+
+ @Override
+ public V value() {
+ return this.value;
+ }
+
+ @Override
+ public Element element() {
+ return this.element;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Detached properties are readonly: " + this.toString());
+ }
+
+ @Override
+ public String toString() {
+ return StringFactory.propertyString(this);
+ }
+
+ @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
+ @Override
+ public boolean equals(final Object object) {
+ return ElementHelper.areEqual(this, object);
+ }
+
+ @Override
+ public int hashCode() {
+ return ElementHelper.hashCode(this);
+ }
+
+ @Override
+ public Property<V> attach(final Vertex hostVertex) {
+ final Element element = (Element) this.element.attach(hostVertex);
+ final Property<V> property = element.property(this.key);
+ if (property.isPresent())
+ return property;
+ else
+ throw new IllegalStateException("The detached property could not be be found at the provided vertex: " + this);
+ }
+
+ @Override
+ public Property<V> attach(final Graph hostGraph) {
+ final Element hostElement = (Element) this.element.attach(hostGraph);
+ final Property<V> property = hostElement.property(this.key);
+ if (property.isPresent())
+ return property;
+ else
+ throw new IllegalStateException("The detached property could not be be found at the provided vertex: " + this);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
new file mode 100644
index 0000000..d7f450b
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
@@ -0,0 +1,166 @@
+/*
+ * 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 com.tinkerpop.gremlin.structure.util.detached;
+
+import com.tinkerpop.gremlin.process.T;
+import com.tinkerpop.gremlin.process.graph.traversal.GraphTraversal;
+import com.tinkerpop.gremlin.structure.Direction;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.VertexProperty;
+import com.tinkerpop.gremlin.structure.util.StringFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Represents a {@link Vertex} that is disconnected from a {@link Graph}. "Disconnection" can mean detachment from
+ * a {@link Graph} in the sense that a {@link Vertex} was constructed from a {@link Graph} instance and this reference
+ * was removed or it can mean that the {@code DetachedVertex} could have been constructed independently of a
+ * {@link Graph} instance in the first place.
+ * <br/>
+ * A {@code DetachedVertex} only has reference to the properties that are associated with it at the time of detachment
+ * (or construction) and is not traversable or mutable.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class DetachedVertex extends DetachedElement<Vertex> implements Vertex, Vertex.Iterators {
+
+ private static final String ID = "id";
+ private static final String VALUE = "value";
+ private static final String PROPERTIES = "properties";
+
+ private DetachedVertex() {
+ }
+
+ protected DetachedVertex(final Vertex vertex, final boolean withProperties) {
+ super(vertex);
+
+ // only serialize properties if requested, and there are meta properties present. this prevents unnecessary
+ // object creation of a new HashMap of a new HashMap which will just be empty. it will use
+ // Collections.emptyMap() by default
+ if (withProperties) {
+ final Iterator<VertexProperty<Object>> propertyIterator = vertex.iterators().propertyIterator();
+ if (propertyIterator.hasNext()) {
+ this.properties = new HashMap<>();
+ propertyIterator.forEachRemaining(property -> {
+ final List<VertexProperty<?>> list = (List<VertexProperty<?>>) this.properties.getOrDefault(property.key(), new ArrayList<>());
+ list.add(DetachedFactory.detach(property, true));
+ this.properties.put(property.key(), list);
+ });
+ }
+ }
+ }
+
+ public DetachedVertex(final Object id, final String label, final Map<String, Object> properties) {
+ super(id, label);
+ if (!properties.isEmpty()) {
+ this.properties = new HashMap<>();
+ properties.entrySet().stream().forEach(
+ entry -> this.properties.put(entry.getKey(), ((List<Map<String, Object>>) entry.getValue()).stream()
+ .map(m -> (Property) new DetachedVertexProperty<>(m.get(ID), entry.getKey(), m.get(VALUE), (Map<String, Object>) m.getOrDefault(PROPERTIES, new HashMap<>()), this))
+ .collect(Collectors.toList())));
+ }
+ }
+
+ @Override
+ public <V> VertexProperty<V> property(final String key, final V value) {
+ throw new UnsupportedOperationException("Detached vertices are readonly: " + this);
+ }
+
+ @Override
+ public <V> VertexProperty<V> property(final String key) {
+ if (this.properties.containsKey(key)) {
+ final List<VertexProperty> list = (List) this.properties.get(key);
+ if (list.size() > 1)
+ throw Vertex.Exceptions.multiplePropertiesExistForProvidedKey(key);
+ else
+ return list.get(0);
+ } else
+ return VertexProperty.<V>empty();
+ }
+
+ @Override
+ public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
+ throw new UnsupportedOperationException("Detached vertices do not store edges: " + this);
+ }
+
+ @Override
+ public String toString() {
+ return StringFactory.vertexString(this);
+ }
+
+ @Override
+ public Vertex attach(final Vertex hostVertex) {
+ if (hostVertex.equals(this))
+ return hostVertex;
+ else
+ throw new IllegalStateException("The host vertex must be the detached vertex to attach: " + this);
+ }
+
+ @Override
+ public Vertex attach(final Graph hostGraph) {
+ return hostGraph.iterators().vertexIterator(this.id).next();
+ }
+
+ public static Vertex addTo(final Graph graph, final DetachedVertex detachedVertex) {
+ final Vertex vertex = graph.addVertex(T.id, detachedVertex.id(), T.label, detachedVertex.label());
+ detachedVertex.properties.entrySet().forEach(kv ->
+ kv.getValue().forEach(property -> {
+ final VertexProperty vertexProperty = (VertexProperty) property;
+ final List<Object> propsOnProps = new ArrayList<>();
+ vertexProperty.iterators().propertyIterator().forEachRemaining(h -> {
+ propsOnProps.add(h.key());
+ propsOnProps.add(h.value());
+ });
+ propsOnProps.add(T.id);
+ propsOnProps.add(vertexProperty.id());
+ vertex.property(kv.getKey(), property.value(), propsOnProps.toArray());
+ })
+ );
+ return vertex;
+ }
+
+ @Override
+ public Vertex.Iterators iterators() {
+ return this;
+ }
+
+ @Override
+ public <V> Iterator<VertexProperty<V>> propertyIterator(final String... propertyKeys) {
+ return (Iterator) super.propertyIterator(propertyKeys);
+ }
+
+ @Override
+ public GraphTraversal<Vertex, Edge> edgeIterator(final Direction direction, final String... edgeLabels) {
+ throw new UnsupportedOperationException("Detached vertices do not have edges");
+ }
+
+ @Override
+ public GraphTraversal<Vertex, Vertex> vertexIterator(final Direction direction, final String... labels) {
+ throw new UnsupportedOperationException("Detached vertices do not have edges and thus, adjacent vertices can not be accessed");
+ }
+}