Permalink
Browse files

Removed IdIndexGraph, which has been mainlined into TinkerPop 2 as Id…

…Graph
  • Loading branch information...
1 parent ef9f510 commit ca6c85d2afcfaf950523100fd0115728fd72b120 @joshsh joshsh committed Jul 9, 2012
View
7 id-index-graph/.gitignore
@@ -1,7 +0,0 @@
-/target
-.classpath
-.project
-.settings
-*.iml
-.idea
-*~
View
110 id-index-graph/pom.xml
@@ -1,110 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.tinkerpop.tinkubator</groupId>
- <artifactId>id-index-graph</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>IdIndexGraph</name>
- <description>Persistent identifiers in Blueprints graphs</description>
-
- <repositories>
- <repository>
- <id>tinkerpop-repository</id>
- <name>TinkerPop Maven2 Repository</name>
- <url>http://tinkerpop.com/maven2</url>
- <snapshots>
- <enabled>true</enabled>
- <updatePolicy>always</updatePolicy>
- </snapshots>
- </repository>
- </repositories>
-
- <properties>
- <blueprints.version>1.2</blueprints.version>
- <junit.version>4.5</junit.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.tinkerpop.blueprints</groupId>
- <artifactId>blueprints-core</artifactId>
- <version>${blueprints.version}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.tinkerpop.blueprints</groupId>
- <artifactId>blueprints-test</artifactId>
- <version>${blueprints.version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <sourceDirectory>src/main/java</sourceDirectory>
- <testSourceDirectory>src/test/java</testSourceDirectory>
- <directory>target</directory>
- <finalName>${project.artifactId}-${project.version}</finalName>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- </resource>
- </resources>
- <testResources>
- <testResource>
- <directory>src/test/resources</directory>
- </testResource>
- </testResources>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- <!-- Note: plugin versions are the most recent (in repo1.maven.org) as of 2011-04-05 -->
- <reporting>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-javadoc-plugin</artifactId>
- <version>2.7</version>
- <configuration>
- <aggregate>true</aggregate>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>cobertura-maven-plugin</artifactId>
- <version>2.4</version>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jxr-plugin</artifactId>
- <version>2.2</version>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <configLocation>${basedir}/ripple-demo/src/site/checkstyle.xml</configLocation>
- </configuration>
- </plugin>
- </plugins>
- </reporting>
-
-</project>
View
51 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexEdge.java
@@ -1,51 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Vertex;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-class IdIndexEdge extends IdIndexElement implements Edge {
- private IdIndexVertex inVertex;
- private IdIndexVertex outVertex;
-
- public IdIndexEdge(Edge base) {
- super(base);
- }
-
- public Edge getBase() {
- return (Edge) base;
- }
-
- public Vertex getOutVertex() {
- if (null == outVertex) {
- outVertex = new IdIndexVertex(((Edge) base).getOutVertex());
- }
-
- return outVertex;
- }
-
- public Vertex getInVertex() {
- if (null == inVertex) {
- inVertex = new IdIndexVertex(((Edge) base).getInVertex());
- }
-
- return inVertex;
- }
-
- public String getLabel() {
- return ((Edge) base).getLabel();
- }
-
- @Override
- public boolean equals(Object other) {
- return other instanceof IdIndexEdge
- && ((Edge) other).getId().equals(getId());
- }
-
- @Override
- public int hashCode() {
- return 808068 + getId().hashCode();
- }
-}
View
34 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexEdgeIterable.java
@@ -1,34 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-
-import java.util.Iterator;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-class IdIndexEdgeIterable implements Iterable<Edge> {
- private final Iterable<Edge> base;
-
- public IdIndexEdgeIterable(Iterable<Edge> base) {
- this.base = base;
- }
-
- public Iterator<Edge> iterator() {
- final Iterator<Edge> baseIter = base.iterator();
-
- return new Iterator<Edge>() {
- public boolean hasNext() {
- return baseIter.hasNext();
- }
-
- public Edge next() {
- return new IdIndexEdge(baseIter.next());
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
-}
View
53 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexElement.java
@@ -1,53 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.Element;
-
-import java.util.Set;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-abstract class IdIndexElement implements Element {
- protected final Element base;
-
- public IdIndexElement(Element base) {
- this.base = base;
- }
-
- public Object getProperty(String s) {
- if (s.equals(IdIndexGraph.ID)) {
- return null;
- }
-
- return base.getProperty(s);
- }
-
- public Set<String> getPropertyKeys() {
- Set<String> keys = base.getPropertyKeys();
-
- // TODO: this will fail if the returned collection is immutable
- keys.remove(IdIndexGraph.ID);
-
- return keys;
- }
-
- public void setProperty(String s, Object o) {
- if (s.equals(IdIndexGraph.ID)) {
- throw new IllegalArgumentException("can't set value for reserved property '" + IdIndexGraph.ID + "'");
- }
-
- base.setProperty(s, o);
- }
-
- public Object removeProperty(String s) {
- if (s.equals(IdIndexGraph.ID)) {
- throw new IllegalArgumentException("can't remove value for reserved property '" + IdIndexGraph.ID + "'");
- }
-
- return base.removeProperty(s);
- }
-
- public Object getId() {
- return base.getProperty(IdIndexGraph.ID);
- }
-}
View
459 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexGraph.java
@@ -1,459 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.AutomaticIndex;
-import com.tinkerpop.blueprints.pgm.CloseableSequence;
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Element;
-import com.tinkerpop.blueprints.pgm.Index;
-import com.tinkerpop.blueprints.pgm.IndexableGraph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Set;
-import java.util.UUID;
-import java.util.logging.Logger;
-
-/**
- * An IndexableGraph implementation which wraps another IndexableGraph implementation,
- * enabling custom element IDs even for those graphs which don't otherwise support them.
- *
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-public class IdIndexGraph implements IndexableGraph {
- private static final Logger LOGGER = Logger.getLogger(IdIndexGraph.class.getName());
-
- // Note: using "__id" instead of "_id" avoids collision with Rexster's "_id"
- public static final String ID = "__id";
-
- public static final String
- VERTEX_IDS = "__vertex-ids",
- EDGE_IDS = "__edge-ids";
-
- private final IndexableGraph base;
- private final Index<Vertex> vertexIds;
- private final Index<Edge> edgeIds;
- private final IdFactory idFactory;
-
- /**
- * Adds custom ID functionality to the given graph.
- *
- * @param base the base graph which does not permit custom element IDs
- */
- public IdIndexGraph(final IndexableGraph base) {
- this(base, null);
- }
-
- /**
- * Adds custom ID functionality to the given graph, also specifying a factory for new element IDs.
- *
- * @param base the base graph which may or may not permit custom element IDs
- * @param idFactory a factory for new element IDs.
- * When vertices or edges are created using null IDs, the actual IDs are chosen based on this factory.
- */
- public IdIndexGraph(final IndexableGraph base,
- final IdFactory idFactory) {
- this.base = base;
-
- this.idFactory = null == idFactory
- ? new IdFactory() {
- public Object createId() {
- return UUID.randomUUID().toString();
- }
- } : idFactory;
-
- Set<String> ids = new HashSet<String>();
- ids.add(ID);
-
- vertexIds = null == base.getIndex(VERTEX_IDS, Vertex.class)
- ? base.createAutomaticIndex(VERTEX_IDS, Vertex.class, ids)
- : base.getIndex(VERTEX_IDS, Vertex.class);
-
- edgeIds = null == base.getIndex(EDGE_IDS, Edge.class)
- ? base.createAutomaticIndex(EDGE_IDS, Edge.class, ids)
- : base.getIndex(EDGE_IDS, Edge.class);
- }
-
- public <T extends Element> Index<T> createManualIndex(final String s,
- final Class<T> tClass,
- final Parameter... params) {
- if (0 < params.length) {
- LOGGER.warning("index parameters will be ignored");
- }
-
- if (s.equals(VERTEX_IDS) || s.equals(EDGE_IDS)) {
- throw new IllegalArgumentException("can't create index with reserved name '" + s + "'");
- }
-
- Index<T> index = base.createManualIndex(s, tClass);
-
- if (Vertex.class.isAssignableFrom(tClass)) {
- return (Index<T>) new VertexIndexWrapper((Index<Vertex>) index);
- } else if (Edge.class.isAssignableFrom(tClass)) {
- return (Index<T>) new EdgeIndexWrapper((Index<Edge>) index);
- } else {
- throw new IllegalStateException("unexpected index class: " + tClass);
- }
- }
-
- public <T extends Element> AutomaticIndex<T> createAutomaticIndex(final String s,
- final Class<T> tClass,
- final Set<String> strings,
- final Parameter... params) {
- if (0 < params.length) {
- LOGGER.warning("index parameters will be ignored");
- }
-
- if (s.equals(VERTEX_IDS) || s.equals(EDGE_IDS)) {
- throw new IllegalArgumentException("can't create index with reserved name '" + s + "'");
- }
-
- if (strings.contains(ID)) {
- throw new IllegalArgumentException("can't index on reserved property '" + ID + "'");
- }
-
- AutomaticIndex<T> index = base.createAutomaticIndex(s, tClass, strings);
-
- if (Vertex.class.isAssignableFrom(tClass)) {
- return (AutomaticIndex<T>) new AutoVertexIndexWrapper((Index<Vertex>) index);
- } else if (Edge.class.isAssignableFrom(tClass)) {
- return (AutomaticIndex<T>) new AutoEdgeIndexWrapper((Index<Edge>) index);
- } else {
- throw new IllegalStateException("unexpected index class: " + tClass);
- }
- }
-
- public <T extends Element> Index<T> getIndex(String s, Class<T> tClass) {
- if (s.equals(VERTEX_IDS) || s.equals(EDGE_IDS)) {
- return null;
- } else {
- Index<T> index = base.getIndex(s, tClass);
-
- if (null == index) {
- return null;
- }
-
- if (Vertex.class.isAssignableFrom(tClass)) {
- if (Index.Type.AUTOMATIC.equals(index.getIndexType())) {
- return (Index<T>) new AutoVertexIndexWrapper((Index<Vertex>) index);
- } else {
- return (Index<T>) new VertexIndexWrapper((Index<Vertex>) index);
- }
- } else if (Edge.class.isAssignableFrom(tClass)) {
- if (Index.Type.AUTOMATIC.equals(index.getIndexType())) {
- return (Index<T>) new AutoEdgeIndexWrapper((Index<Edge>) index);
- } else {
- return (Index<T>) new EdgeIndexWrapper((Index<Edge>) index);
- }
- } else {
- throw new IllegalStateException("unexpected index class: " + tClass);
- }
- }
- }
-
- public Iterable<Index<? extends Element>> getIndices() {
- Collection<Index<? extends Element>> indices = new LinkedList<Index<? extends Element>>();
-
- for (Index<? extends Element> i : base.getIndices()) {
- if (!(i.getIndexName().equals(VERTEX_IDS) || i.getIndexName().equals(EDGE_IDS))) {
-
- if (Vertex.class.isAssignableFrom(i.getIndexClass())) {
- if (Index.Type.AUTOMATIC.equals(i.getIndexType())) {
- indices.add(new AutoVertexIndexWrapper((Index<Vertex>) i));
- } else {
- indices.add(new VertexIndexWrapper((Index<Vertex>) i));
- }
- } else if (Edge.class.isAssignableFrom(i.getIndexClass())) {
- if (Index.Type.AUTOMATIC.equals(i.getIndexType())) {
- indices.add(new AutoEdgeIndexWrapper((Index<Edge>) i));
- } else {
- indices.add(new EdgeIndexWrapper((Index<Edge>) i));
- }
- } else {
- throw new IllegalStateException("unexpected index class: " + i.getIndexClass());
- }
- }
- }
-
- return indices;
- }
-
- public void dropIndex(String s) {
- if (!(s.equals(VERTEX_IDS) || s.equals(EDGE_IDS))) {
- base.dropIndex(s);
- }
- }
-
- public Vertex addVertex(Object id) {
- if (null != id && null != getVertex(id)) {
- throw new IllegalArgumentException("vertex with given id already exists: '" + id + "'");
- }
-
- Vertex v = base.addVertex(null);
-
- v.setProperty(ID, null == id ? idFactory.createId() : id);
-
- return new IdIndexVertex(v);
- }
-
- public Vertex getVertex(Object id) {
- if (null == id) {
- throw new IllegalArgumentException("null id");
- }
-
- CloseableSequence<Vertex> i = vertexIds.get(ID, id);
- try {
- if (!i.hasNext()) {
- return null;
- } else {
- Vertex e = i.next();
-
- if (i.hasNext()) {
- throw new IllegalStateException("multiple vertices exist with id '" + id + "'");
- }
-
- return new IdIndexVertex(e);
- }
- } finally {
- i.close();
- }
- }
-
- public void removeVertex(Vertex vertex) {
- verifyNativeElement(vertex);
-
- base.removeVertex(((IdIndexVertex) vertex).getBase());
- }
-
- public Iterable<Vertex> getVertices() {
- return new IdIndexVertexIterable(base.getVertices());
- }
-
- public Edge addEdge(Object id, Vertex v1, Vertex v2, String s) {
- if (null != id && null != getEdge(id)) {
- throw new IllegalArgumentException("edge with given id already exists: '" + id + "'");
- }
-
- verifyNativeElement(v1);
- verifyNativeElement(v2);
-
- Edge e = base.addEdge(null, ((IdIndexVertex) v1).getBase(), ((IdIndexVertex) v2).getBase(), s);
-
- e.setProperty(ID, null == id ? idFactory.createId() : id);
-
- return new IdIndexEdge(e);
- }
-
- public Edge getEdge(Object id) {
- CloseableSequence<Edge> i = edgeIds.get(ID, id);
- try {
- if (!i.hasNext()) {
- return null;
- } else {
- Edge e = i.next();
-
- if (i.hasNext()) {
- throw new IllegalStateException("multiple edges exist with id '" + id + "'");
- }
-
- return new IdIndexEdge(e);
- }
- } finally {
- i.close();
- }
- }
-
- public void removeEdge(Edge edge) {
- verifyNativeElement(edge);
-
- base.removeEdge(((IdIndexEdge) edge).getBase());
- }
-
- public Iterable<Edge> getEdges() {
- return new IdIndexEdgeIterable(base.getEdges());
- }
-
- public void clear() {
- base.clear();
- }
-
- public void shutdown() {
- base.shutdown();
- }
-
- private static void verifyNativeElement(final Element e) {
- if (!(e instanceof IdIndexElement)) {
- throw new IllegalArgumentException("given element was not created in this graph");
- }
- }
-
- /**
- * A factory for IDs of newly-created vertices and edges (where an ID is not otherwise specified).
- */
- public static interface IdFactory {
- Object createId();
- }
-
- private static class VertexIndexWrapper implements Index<Vertex> {
- protected final Index<Vertex> base;
-
- public VertexIndexWrapper(Index<Vertex> base) {
- this.base = base;
- }
-
- public String getIndexName() {
- return base.getIndexName();
- }
-
- public Class<Vertex> getIndexClass() {
- return base.getIndexClass();
- }
-
- public Type getIndexType() {
- return base.getIndexType();
- }
-
- public void put(String s, Object o, Vertex vertex) {
- verifyNativeElement(vertex);
-
- base.put(s, o, ((IdIndexVertex) vertex).getBase());
- }
-
- public CloseableSequence<Vertex> get(String s, Object o) {
- return new CloseableVertexSequence(base.get(s, o));
- }
-
- public long count(String s, Object o) {
- return base.count(s, o);
- }
-
- public void remove(String s, Object o, Vertex vertex) {
- verifyNativeElement(vertex);
-
- base.remove(s, o, ((IdIndexVertex) vertex).getBase());
- }
- }
-
- private static class AutoVertexIndexWrapper extends VertexIndexWrapper implements AutomaticIndex<Vertex> {
- public AutoVertexIndexWrapper(Index<Vertex> base) {
- super(base);
- }
-
- public Set<String> getAutoIndexKeys() {
- return ((AutomaticIndex) base).getAutoIndexKeys();
- }
- }
-
- private class EdgeIndexWrapper implements Index<Edge> {
- protected final Index<Edge> base;
-
- public EdgeIndexWrapper(Index<Edge> base) {
- this.base = base;
- }
-
- public String getIndexName() {
- return base.getIndexName();
- }
-
- public Class<Edge> getIndexClass() {
- return base.getIndexClass();
- }
-
- public Type getIndexType() {
- return base.getIndexType();
- }
-
- public void put(String s, Object o, Edge edge) {
- verifyNativeElement(edge);
-
- base.put(s, o, ((IdIndexEdge) edge).getBase());
- }
-
- public CloseableSequence<Edge> get(String s, Object o) {
- return new CloseableEdgeSequence(base.get(s, o));
- }
-
- public long count(String s, Object o) {
- return base.count(s, o);
- }
-
- public void remove(String s, Object o, Edge edge) {
- verifyNativeElement(edge);
-
- base.remove(s, o, ((IdIndexEdge) edge).getBase());
- }
- }
-
- private class AutoEdgeIndexWrapper extends EdgeIndexWrapper implements AutomaticIndex<Edge> {
- public AutoEdgeIndexWrapper(Index<Edge> base) {
- super(base);
- }
-
- public Set<String> getAutoIndexKeys() {
- return ((AutomaticIndex) base).getAutoIndexKeys();
- }
- }
-
- private static class CloseableVertexSequence implements CloseableSequence<Vertex> {
- private final CloseableSequence<Vertex> base;
-
- public CloseableVertexSequence(CloseableSequence<Vertex> base) {
- this.base = base;
- }
-
- public void close() {
- base.close();
- }
-
- public Iterator<Vertex> iterator() {
- return this;
- }
-
- public boolean hasNext() {
- return base.hasNext();
- }
-
- public Vertex next() {
- return new IdIndexVertex(base.next());
- }
-
- public void remove() {
- base.remove();
- }
- }
-
- private static class CloseableEdgeSequence implements CloseableSequence<Edge> {
- private final CloseableSequence<Edge> base;
-
- public CloseableEdgeSequence(CloseableSequence<Edge> base) {
- this.base = base;
- }
-
- public void close() {
- base.close();
- }
-
- public Iterator<Edge> iterator() {
- return this;
- }
-
- public boolean hasNext() {
- return base.hasNext();
- }
-
- public Edge next() {
- return new IdIndexEdge(base.next());
- }
-
- public void remove() {
- base.remove();
- }
- }
-
- public static void main(final String[] args) throws Exception {
- System.out.println(UUID.randomUUID().toString());
- }
-}
View
36 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexVertex.java
@@ -1,36 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Vertex;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-class IdIndexVertex extends IdIndexElement implements Vertex {
- public IdIndexVertex(Vertex base) {
- super(base);
- }
-
- public Vertex getBase() {
- return (Vertex) base;
- }
-
- public Iterable<Edge> getOutEdges(String... strings) {
- return new IdIndexEdgeIterable(((Vertex) base).getOutEdges(strings));
- }
-
- public Iterable<Edge> getInEdges(String... strings) {
- return new IdIndexEdgeIterable(((Vertex) base).getInEdges(strings));
- }
-
- @Override
- public boolean equals(Object other) {
- return other instanceof IdIndexVertex
- && ((Vertex) other).getId().equals(getId());
- }
-
- @Override
- public int hashCode() {
- return 274703 + getId().hashCode();
- }
-}
View
34 id-index-graph/src/main/java/com/tinkerpop/tinkubator/idindex/IdIndexVertexIterable.java
@@ -1,34 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.Vertex;
-
-import java.util.Iterator;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-class IdIndexVertexIterable implements Iterable<Vertex> {
- private final Iterable<Vertex> base;
-
- public IdIndexVertexIterable(Iterable<Vertex> base) {
- this.base = base;
- }
-
- public Iterator<Vertex> iterator() {
- final Iterator<Vertex> baseIter = base.iterator();
-
- return new Iterator<Vertex>() {
- public boolean hasNext() {
- return baseIter.hasNext();
- }
-
- public Vertex next() {
- return new IdIndexVertex(baseIter.next());
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
-}
View
195 id-index-graph/src/test/java/com.tinkerpop.tinkubator.idindex/IdIndexGraphTest.java
@@ -1,195 +0,0 @@
-package com.tinkerpop.tinkubator.idindex;
-
-import com.tinkerpop.blueprints.pgm.AutomaticIndex;
-import com.tinkerpop.blueprints.pgm.CloseableSequence;
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Element;
-import com.tinkerpop.blueprints.pgm.Index;
-import com.tinkerpop.blueprints.pgm.IndexableGraph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-/**
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-public class IdIndexGraphTest {
- private IndexableGraph base;
- IdIndexGraph graph;
-
- @Before
- public void setUp() throws Exception {
- base = new TinkerGraph();
- graph = new IdIndexGraph(base);
- }
-
- @After
- public void tearDown() throws Exception {
- base.shutdown();
- }
-
- @Test
- public void testElementClasses() throws Exception {
- Vertex v1 = graph.addVertex(null);
- Vertex v2 = graph.addVertex(null);
- Edge e = graph.addEdge(null, v1, v2, "knows");
-
- assertTrue(v1 instanceof IdIndexVertex);
- assertTrue(e instanceof IdIndexEdge);
-
- Iterator<Edge> outE = v1.getOutEdges().iterator();
- assertTrue(outE.hasNext());
- e = outE.next();
- assertTrue(e instanceof IdIndexEdge);
- assertTrue(e.getInVertex() instanceof IdIndexVertex);
- assertTrue(e.getOutVertex() instanceof IdIndexVertex);
-
- Iterator<Vertex> vertices = graph.getVertices().iterator();
- assertTrue(vertices.hasNext());
- while (vertices.hasNext()) {
- assertTrue(vertices.next() instanceof IdIndexVertex);
- }
-
- Iterator<Edge> edges = graph.getEdges().iterator();
- assertTrue(edges.hasNext());
- while (edges.hasNext()) {
- assertTrue(edges.next() instanceof IdIndexEdge);
- }
- }
-
- @Test
- public void testIdIndicesExist() throws Exception {
- Index<Vertex> vertexIds = base.getIndex(IdIndexGraph.VERTEX_IDS, Vertex.class);
- Index<Edge> edgeIds = base.getIndex(IdIndexGraph.EDGE_IDS, Edge.class);
-
- assertNotNull(vertexIds);
- assertNotNull(edgeIds);
-
- assertNull(graph.getIndex(IdIndexGraph.VERTEX_IDS, Vertex.class));
- assertNull(graph.getIndex(IdIndexGraph.EDGE_IDS, Edge.class));
- }
-
- @Test
- public void testDefaultIdFactory() throws Exception {
- Vertex v = graph.addVertex(null);
- String id = (String) v.getId();
-
- assertEquals(36, id.length());
- assertEquals(5, id.split("-").length);
-
- Vertex v2 = graph.addVertex(null);
- Edge e = graph.addEdge(null, v, v2, "knows");
-
- id = (String) e.getId();
- assertEquals(36, id.length());
- assertEquals(5, id.split("-").length);
- }
-
- @Test
- public void testAddVertexWithSpecifiedId() throws Exception {
- Vertex v = graph.addVertex("forty-two");
-
- assertEquals("forty-two", v.getId());
- }
-
- @Test
- public void testIndices() throws Exception {
- Set<String> nameKeys = new HashSet<String>();
- nameKeys.add("name");
-
- graph.createAutomaticIndex("names", Vertex.class, nameKeys);
- graph.createManualIndex("weights", Edge.class);
-
- Iterable<Index<? extends Element>> indices = graph.getIndices();
- int count = 0;
- for (Index<? extends Element> i : indices) {
- String name = i.getIndexName();
- Class c = i.getIndexClass();
- Index.Type t = i.getIndexType();
-
- if (name.equals("names")) {
- assertEquals(Index.Type.AUTOMATIC, t);
- assertEquals(Vertex.class, c);
- Set<String> keys = ((AutomaticIndex) i).getAutoIndexKeys();
- assertEquals(1, keys.size());
- assertTrue(keys.contains("name"));
- } else if (name.equals("weights")) {
- assertEquals(Index.Type.MANUAL, t);
- assertEquals(Edge.class, c);
- } else if (!name.equals("edges") && !name.equals("vertices")) {
- fail("unexpected index: " + name);
- }
-
- count++;
- }
- assertEquals(4, count);
-
- AutomaticIndex<Vertex> names = (AutomaticIndex<Vertex>) graph.getIndex("names", Vertex.class);
- Index<Edge> weights = graph.getIndex("weights", Edge.class);
-
- Vertex v1 = graph.addVertex(null);
- v1.setProperty("name", "Arthur");
-
- Vertex v2 = graph.addVertex(null);
- v2.setProperty("name", "Ford");
-
- Edge e = graph.addEdge(null, v1, v2, "knows");
- e.setProperty("weight", 0.8);
-
- Collection<Vertex> vertices;
- vertices = toCollection(names.get("name", "Arthur"));
- assertEquals(1, vertices.size());
- assertEquals(v1.getId(), vertices.iterator().next().getId());
- vertices = toCollection(names.get("name", "Ford"));
- assertEquals(1, vertices.size());
- assertEquals(v2.getId(), vertices.iterator().next().getId());
-
- weights.put("weight", 0.4, e);
- Collection<Edge> edges;
- edges = toCollection(weights.get("weight", 0.8));
- assertEquals(0, edges.size());
- edges = toCollection(weights.get("weight", 0.4));
- assertEquals(1, edges.size());
- assertEquals(e.getId(), edges.iterator().next().getId());
- }
-
- @Test
- public void testProperties() throws Exception {
- Vertex v = graph.addVertex(null);
- v.setProperty("name", "Zaphod");
- v.setProperty("profession", "ex-president of the Galaxy");
-
- Set<String> keys = v.getPropertyKeys();
- assertEquals(2, keys.size());
- assertTrue(keys.contains("name"));
- assertTrue(keys.contains("profession"));
- assertEquals("Zaphod", v.getProperty("name"));
- }
-
- private <T> Collection<T> toCollection(final CloseableSequence<T> s) {
- Collection<T> c = new LinkedList<T>();
- try {
- while (s.hasNext()) {
- c.add(s.next());
- }
- } finally {
- s.close();
- }
-
- return c;
- }
-}

0 comments on commit ca6c85d

Please sign in to comment.