Created
July 13, 2012 12:17
-
-
Save spmallette/3104606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinkerpop.blueprints.util.wrappers.batch.cache; | |
import com.tinkerpop.blueprints.Graph; | |
import com.tinkerpop.blueprints.Vertex; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* (c) Matthias Broecheler ([email protected]) | |
*/ | |
public class ObjectIDVertexCache implements VertexCache { | |
private static final int INITIAL_CAPACITY = 1000; | |
private final Graph graph; | |
private final Map<Object, Object> map; | |
private final List<Object> newlyAddedKeys; | |
public ObjectIDVertexCache(final Graph graph) { | |
if (graph == null) throw new IllegalArgumentException("Graph expected."); | |
this.graph = graph; | |
map = new HashMap<Object, Object>(INITIAL_CAPACITY); | |
newlyAddedKeys = new List<Object>(); | |
} | |
@Override | |
public Vertex getVertex(Object externalID) { | |
Object o = map.get(externalID); | |
if (o instanceof Vertex) { | |
return (Vertex) o; | |
} else if (o != null) { //o is the internal id | |
Vertex v = graph.getVertex(o); | |
map.put(externalID, o); | |
return v; | |
} else return null; | |
} | |
@Override | |
public void add(Vertex vertex, Object externalID) { | |
assert !map.containsKey(externalID); | |
map.put(externalID, vertex); | |
newlyAddedKeys.add(externalID); | |
} | |
@Override | |
public void newTransaction() { | |
for (Object newKey : newlyAddedKeys) { | |
Vertex v = (Vertex) map.get(newKey); | |
entry.setValue(v.getId()); | |
} | |
newlyAddedKeys.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment