Skip to content

Commit

Permalink
Merge pull request #4 from karthicks/ytd-changes
Browse files Browse the repository at this point in the history
A few year-to-date fixes and improvements.
  • Loading branch information
Karthick Sankarachary authored Dec 27, 2017
2 parents 6902290 + 8a5588a commit 778fe51
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 90 deletions.
13 changes: 12 additions & 1 deletion gremlin-objects/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
<parent>
<groupId>com.github.karthicks</groupId>
<artifactId>gremlin-ogm</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.1-SNAPSHOT</version>
</parent>
<artifactId>gremlin-objects</artifactId>
<name>Gremlin OGM :: Gremlin Objects</name>
Expand All @@ -39,6 +39,17 @@ limitations under the License.
<artifactId>gremlin-core</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>${tinkerpop.version}</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public final Graph graph() {
}
break;
case NOTHING:
break;
return makeGraph();
default:
break;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public final Query query() {
}
break;
case NOTHING:
break;
return makeQuery();
default:
break;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public void clear() {
*/
public enum ShouldCache {
/**
* Cache the instances returned by {@link #graph()}, {@link #query()}, and {@link #system()}.
* Cache the instances returned by {@link #graph()}, and {@link #query()}.
*
* <p> This mode may be used in single-threaded applications, if you want to avoid the overhead
* of creating the state of the {@link #graph()} and {@link #query()} instances. The caller must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@
*/
package org.apache.tinkerpop.gremlin.object.reflect;

import org.apache.tinkerpop.gremlin.object.model.PropertyValue;
import org.apache.tinkerpop.gremlin.object.structure.Element;
import org.apache.tinkerpop.gremlin.object.traversal.Query;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import lombok.SneakyThrows;

import static org.apache.tinkerpop.gremlin.object.reflect.Classes.is;
import static org.apache.tinkerpop.gremlin.object.reflect.Classes.isCollection;
import static org.apache.tinkerpop.gremlin.object.reflect.Classes.newCollection;
Expand All @@ -44,6 +28,21 @@
import static org.apache.tinkerpop.gremlin.object.reflect.Fields.propertyKey;
import static org.apache.tinkerpop.gremlin.object.reflect.Primitives.isPrimitive;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import lombok.SneakyThrows;
import org.apache.tinkerpop.gremlin.object.model.PropertyValue;
import org.apache.tinkerpop.gremlin.object.structure.Element;
import org.apache.tinkerpop.gremlin.object.traversal.Query;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty;

/**
* The {@link Parser}'s job is to take an element returned by a traversal and convert it into its
* corresponding class' object, through the {@link #as(Object, Class)} method.
Expand Down Expand Up @@ -149,6 +148,13 @@ public <T> T as(org.apache.tinkerpop.gremlin.structure.Element element, Class<T>
Object elementId = element.id();
instance.setUserSuppliedId(elementId);
instance.setDelegate(element);
if (element instanceof Edge &&
instance instanceof org.apache.tinkerpop.gremlin.object.structure.Edge) {
org.apache.tinkerpop.gremlin.object.structure.Edge edge =
(org.apache.tinkerpop.gremlin.object.structure.Edge) instance;
edge.fromId(((Edge) element).outVertex().id());
edge.toId(((Edge) element).inVertex().id());
}
for (Field field : fields(instance)) {
String propertyName = propertyKey(field);
Class propertyType = field.getType();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 org.apache.tinkerpop.gremlin.object.reflect;

import org.apache.tinkerpop.gremlin.groovy.jsr223.GroovyTranslator;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;

/**
* @author Karthick Sankarachary
*/
public class Traversal {

private static final GroovyTranslator GROOVY_TRANSLATOR = GroovyTranslator.of("g");

public static String show(GraphTraversal traversal) {
return (traversal instanceof DefaultGraphTraversal) ? GROOVY_TRANSLATOR
.translate(((DefaultGraphTraversal) traversal).getBytecode()) : traversal.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
*/
package org.apache.tinkerpop.gremlin.object.structure;

import org.apache.tinkerpop.gremlin.object.model.Hidden;
import org.apache.tinkerpop.gremlin.object.reflect.Parser;
import org.apache.tinkerpop.gremlin.object.traversal.AnyTraversal;
import org.apache.tinkerpop.gremlin.object.traversal.SubTraversal;
import org.apache.tinkerpop.gremlin.structure.Direction;

import java.util.Arrays;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.tinkerpop.gremlin.object.model.Hidden;
import org.apache.tinkerpop.gremlin.object.reflect.Parser;
import org.apache.tinkerpop.gremlin.object.traversal.AnyTraversal;
import org.apache.tinkerpop.gremlin.object.traversal.SubTraversal;
import org.apache.tinkerpop.gremlin.structure.Direction;

/**
* An {@link Edge} is an {@link Element} that is an object-oriented manifestation of an underlying
Expand Down Expand Up @@ -80,6 +78,15 @@ public abstract class Edge extends Element {

protected abstract List<Connection> connections();


/**
* Return the underlying gremlin edge.
*/
public org.apache.tinkerpop.gremlin.structure.Edge delegate() {
return (org.apache.tinkerpop.gremlin.structure.Edge) delegate;
}


/**
* Return the "from" vertex as the given type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.object.structure;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.tinkerpop.gremlin.object.reflect.Parser;
import org.apache.tinkerpop.gremlin.object.reflect.Properties;
import org.apache.tinkerpop.gremlin.object.traversal.AnyTraversal;
Expand All @@ -28,12 +32,6 @@
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
* The {@link EdgeGraph} performs add and remove edge operations on the underlying {@link
* GraphTraversal} or {@link org.apache.tinkerpop.gremlin.structure.Graph}.
Expand All @@ -56,7 +54,8 @@ public EdgeGraph(Graph graph, Query query, GraphTraversalSource g) {
}

public org.apache.tinkerpop.gremlin.structure.Edge delegate(Edge edge) {
return edge.id() != null ? this.delegates.get(edge) : null;
return edge.delegate != null ? edge.delegate() :
edge.id() != null ? this.delegates.get(edge) : null;
}

/**
Expand Down Expand Up @@ -218,8 +217,11 @@ protected org.apache.tinkerpop.gremlin.structure.Edge insert(
}

<E extends Edge> GraphTraversal find(E edge, Object fromId, Object toId) {
return g.V().hasId(fromId).as("from").out(edge.label()).hasId(toId).as("to")
.inE(edge.label()).as("edge");
return g.V()
.hasId(fromId).as("from")
.outE(edge.label()).as("edge")
.inV().hasId(toId).as("to")
.select("edge");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ public boolean equals(Object other) {
return false;
}
Element that = (Element) other;
if (this.id() != null && that.id() != null && !this.id().equals(that.id())) {
if (this.id() != null &&
that.id() != null &&
this.id().getClass().equals(that.id().getClass()) &&
!this.id().equals(that.id())) {
return false;
}
return compareTo(that) == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,15 @@ protected void append(List<Update> updates, Cardinality cardinality,
case 0:
break;
case 1:
// this is a single valued property.
updates.add(Update.of(key, values.get(0)));
if (cardinality == null || cardinality == Cardinality.single) {
// this is a single valued property.
updates.add(Update.of(key, values.get(0)));
break;
} else {
// this is either a multi- or meta-property
updates.add(Update.of(cardinality, key, values.remove(0),
values.toArray(new Object[] {})));
}
break;
default:
// this is either a multi- or meta-property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
*/
package org.apache.tinkerpop.gremlin.object.structure;

import java.util.function.Consumer;
import org.apache.tinkerpop.gremlin.object.traversal.AnyTraversal;
import org.apache.tinkerpop.gremlin.object.traversal.Query;
import org.apache.tinkerpop.gremlin.object.traversal.SubTraversal;

import java.util.function.Consumer;

/**
* In order to write objects to the property graph, you'll need an instance of the {@link Graph}.
*
Expand All @@ -44,7 +43,7 @@
*
* @author Karthick Sankarachary (http://github.com/karthicks)
*/
public interface Graph {
public interface Graph {

/**
* Add an given {@link Vertex} to the graph.
Expand Down Expand Up @@ -99,6 +98,13 @@ public interface Graph {
<E extends Edge,
V extends Vertex> Graph addEdge(E edge, Vertex fromVertex, AnyTraversal toAnyTraversal);

/**
* Add an {@link Edge} from the given vertex to the vertices returned by the {@link
* SubTraversal}s.
*/
<E extends Edge,
V extends Vertex> Graph addEdge(E edge, Vertex fromVertex, SubTraversal... toVertexQueries);

/**
* Add an {@link Edge} to the vertices returned by the given {@link AnyTraversal}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
*/
package org.apache.tinkerpop.gremlin.object.structure;

import static org.apache.tinkerpop.gremlin.object.structure.Graph.Should.CREATE;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.tinkerpop.gremlin.object.model.Object;
import org.apache.tinkerpop.gremlin.object.reflect.Primitives;
import org.apache.tinkerpop.gremlin.object.traversal.AnyTraversal;
import org.apache.tinkerpop.gremlin.object.traversal.Query;
import org.apache.tinkerpop.gremlin.object.traversal.SubTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;

import static org.apache.tinkerpop.gremlin.object.structure.Graph.Should.CREATE;

/**
* The {@link ObjectGraph} implements the {@link Graph} interface using the provided {@link
* GraphTraversalSource} and {@link Query} instances.
Expand Down Expand Up @@ -153,6 +151,14 @@ public <E extends Edge, V extends Vertex> Graph addEdge(E edge, Vertex fromVerte
return this;
}

@Override
public <E extends Edge, V extends Vertex> Graph addEdge(E edge, Vertex fromVertex,
SubTraversal... toVertexQueries) {
edge.from(fromVertex);
this.object = edgeGraph.addEdge(edge, vertexGraph.delegate(fromVertex), toVertexQueries);
return this;
}

@Override
public <E extends Edge> Graph addEdge(E edge, AnyTraversal toAnyTraversal) {
return addEdge(edge, vertexGraph.getVertex(), toAnyTraversal);
Expand Down
Loading

0 comments on commit 778fe51

Please sign in to comment.