Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesname committed Jul 3, 2013
0 parents commit 37f7aa3
Show file tree
Hide file tree
Showing 14 changed files with 1,521 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .gitignore
@@ -0,0 +1,64 @@
nb-configuration.xml
nbactions.xml
*.class
*.java.orig
*.xml.orig
*.~lock.*#

# Package Files #
*.jar
*.war
*.ear

*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
target

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# Ignore target
**/target/*

Intellij
*iml
.idea


# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db


neo4j-community-1.9*

# Ignore test data folder
testdata
69 changes: 69 additions & 0 deletions pom.xml
@@ -0,0 +1,69 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-sql-graph</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>blueprints-sql-graph</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<blueprints.version>2.4.0-SNAPSHOT</blueprints.version>
</properties>

<dependencies>

<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>${blueprints.version}</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>

<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-test</artifactId>
<version>${blueprints.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.172</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>Sonatype-public</id>
<name>SnakeYAML repository</name>
<url>http://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
</project>
95 changes: 95 additions & 0 deletions src/main/java/com/tinkerpop/blueprints/impls/sql/SqlEdge.java
@@ -0,0 +1,95 @@
package com.tinkerpop.blueprints.impls.sql;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.StringFactory;

import java.sql.Connection;

/**
* @author Mike Bryant (http://github.com/mikesname)
*/
public class SqlEdge extends SqlElement implements Edge {

public static final String TABLE_NAME = "edges";
public static final String PROPERTY_TABLE_NAME = "edge_properties";
public static final String PROPERTY_TABLE_COL = "edge_id";

private final Object vertexOut;
private final Object vertexIn;
private final String label;

SqlEdge(ThreadLocal<Connection> conn, SqlGraph graph, Object id, Object vertexOut, Object vertexIn, String label) {
super(conn, graph, id);
this.vertexIn = vertexIn;
this.vertexOut = vertexOut;
this.label = label;
}

@Override
public Vertex getVertex(Direction direction) throws IllegalArgumentException {
switch (direction) {
case IN: return new SqlVertex(conn, graph, vertexIn);
case OUT: return new SqlVertex(conn, graph, vertexOut);
default: throw new IllegalArgumentException("Direction must be either IN or OUT, not BOTH");
}
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + id + " (" + label + ")]";
}

@Override
public void setProperty(String key, Object value) {
if (key != null && key.equals(StringFactory.LABEL)) {
throw new IllegalArgumentException("Property key '" + StringFactory.LABEL + "' is reserved.");
}
super.setProperty(key, value);
}

@Override
public String getLabel() {
return label;
}

@Override
String getTbl() {
return TABLE_NAME;
}

@Override
String getPropTbl() {
return PROPERTY_TABLE_NAME;
}

@Override
String getFk() {
return PROPERTY_TABLE_COL;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

SqlEdge pgEdge = (SqlEdge) o;

if (!label.equals(pgEdge.label)) return false;
if (!vertexIn.equals(pgEdge.vertexIn)) return false;
if (!vertexOut.equals(pgEdge.vertexOut)) return false;

return true;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + vertexOut.hashCode();
result = 31 * result + vertexIn.hashCode();
result = 31 * result + label.hashCode();
return result;
}
}
100 changes: 100 additions & 0 deletions src/main/java/com/tinkerpop/blueprints/impls/sql/SqlEdgeIterable.java
@@ -0,0 +1,100 @@
package com.tinkerpop.blueprints.impls.sql;

import com.tinkerpop.blueprints.CloseableIterable;
import com.tinkerpop.blueprints.Edge;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* @author Mike Bryant (http://github.com/mikesname)
*/
public class SqlEdgeIterable<T extends Edge> implements CloseableIterable<Edge> {
private final ResultSet rs;
private final ThreadLocal<Connection> conn;
private final SqlGraph graph;

SqlEdgeIterable(final ThreadLocal<Connection> conn, SqlGraph graph, final ResultSet rs) {
this.conn = conn;
this.rs = rs;
this.graph = graph;
}

@Override
public void close() {
try {
rs.close();
} catch (SQLException e) {
throw new SqlGraphException(e);
}
}

@Override
public Iterator<Edge> iterator() {
try {
// Reset the ResultSet each time we ask for
// an iterable.
rs.beforeFirst();
} catch (SQLException e) {
throw new SqlGraphException(e);
}
return new Iterator<Edge>() {
private Edge nextVal = null;

@Override
public boolean hasNext() {
if (nextVal != null) {
// Already been cached.
return true;
}
try {
if (rs.next()) {
nextVal = new SqlEdge(conn, graph,
rs.getLong(1),
rs.getLong(2),
rs.getLong(3),
rs.getString(4));
return true;
} else {
nextVal = null;
return false;
}
} catch (SQLException e) {
throw new SqlGraphException(e);
}
}

@Override
public Edge next() {
if (nextVal != null) {
Edge next = nextVal;
nextVal = null;
return next;
} else {
try {
if (rs.next()) {
return new SqlEdge(conn, graph,
rs.getLong(1),
rs.getLong(2),
rs.getLong(3),
rs.getString(4));
} else {
throw new NoSuchElementException();
}
} catch (SQLException e) {
throw new SqlGraphException(e);
}
}
}

@Override
public void remove() {
throw new NotImplementedException();
}
};
}
}

0 comments on commit 37f7aa3

Please sign in to comment.