bayofmany / peapod

A new object-graph-wrapper for the Tinkerpop 3 graph stack.
Apache License 2.0
40 stars 8 forks source link

Inserting to titan with peapod: unique entities #12

Open joeyfezster opened 8 years ago

joeyfezster commented 8 years ago

Hi, I'm attempting to use peapod to write a lightweight data-stream adapter + data framer for the graph.

My problem:

  1. i can't first instantiate a framed class from the stream (lets say json), and after that push to the graph: the framed class:
@Vertex
public interface Person {
    public String getName();
    public void setName(String name);

    @Edge("worksAt")
    public List<Location> geLocations();
    public void addLocation(Location loc);
    public void removeLocation(Location loc);
}

what i'd like to do:

Person p1 = readNextObject(stream);
framedGraph.addVertex(p1);

the above would also take care of my next (and more interesting problem): unique entities

  1. let's say p1 has a location:
@Vertex
public interface Location{
    public String getName();//Would use a @UniqueID kind of annotation for this property
    public void setName(String name);
}

that is already represented in the graph (a vertex labeld "location", name: "NYC"). in this case, framedGraph.addVertex(p1) would note that p1 has the location "NYC", which is already in the graph. framedGraph would then make sure to create the "worksAt" edge between the new p1 node, and the existing location.

I have solved this issue by "wrapping" the framed class (interface, actually):

public class WLocation {
    private String name;

    public WLocation(String name){
        this.name = name;
    }

    public Location writeToGraph(FramedGraph fg) throws SchemaViolationException{
        //location is unique
        Location loc = null;
        List<Location> locs = fg.V(Location.class).has("name", name).toList();
        if(locs.size() == 0){
            loc = fg.addVertex(Location.class); 
            loc.setName(name);
        }
        else if(locs.size() == 1){            
            loc = locs.get(0);            
        }
        else{
            throw new SchemaViolationException("more than location with name " + name);
        }
        return loc;
    }
}

and something very similar for Person (only relevant code in WPerson class):

    public Person writeToGraph(FramedGraph fg) throws SchemaViolationException{
        //person is unique(by email)
        List<Person> pers = fg.V(Person.class).has("email", email).toList();

        if(pers.size() == 0){
            Person per = fg.addVertex(Person.class);
            locations.forEach( (loc) -> per.addLocation(loc.writeToGraph(fg)) );

            per.setEmail(email);
            per.setName(name);
            return per;
        }
        else if(pers.size() == 1){
            return pers.get(0);
        }
        else{
            throw new SchemaViolationException("multiple ppl with same email: " + email);
        }
    }

i can now call:

WPerson p1 = readNextObject(stream);
p1.writeToGraph(framedGraph);

now i have successfully enforced uniqueness for locations using my own wrappers.

The Downside: for every framed class, i now have to write a wrapping class, and enforce schema in quite a complex, messy all-over-the-place way.

I'd like to know if i missed something (read most your tests) that does this, and if not, if you're planning on this for the next round, and if not, if you would mind i (attempted to) add this and create a pull request.