Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserting to titan with peapod: unique entities #12

Open
joeyfezster opened this issue Jul 7, 2016 · 0 comments
Open

Inserting to titan with peapod: unique entities #12

joeyfezster opened this issue Jul 7, 2016 · 0 comments

Comments

@joeyfezster
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant