How to Find Vertices of Specific class with Tinker

2019-07-24 19:36发布

I have a FramedGraph object with Vertices of different classes, for instance Person and Location. I would like to retrieve all vertices of the "Person" class. Here is my current method.

    public List<Person> listPeople() {
      List<Person> people = new ArrayList<Person>();
      Iterator iterator =  g.getVertices().iterator();
      while (iterator.hasNext()) {
        Vertex v = (Vertex) iterator.next();
        Person p = (Person) g.getVertex(v.getId(), Person.class);
        people.add(p);
      }
      return people;
   }

This feels terrifically inefficient since I am looping over all vertices then dipping back in for one at a time. I looked into using the Gremlin syntax, but I don't see how to restrict by a Frames class. Is there a more efficient retrieval method? Thanks..

1条回答
闹够了就滚
2楼-- · 2019-07-24 19:41

As far as I understand, the Tinkerpop Frame framework acts as a wrapper class around a vertex. The vertex isn't actually stored as the interface class. As such, we need a way to identify the vertex as being of a particular type.

My solution, I added @TypeField and @TypeValue annotations to my Frame classes. Then I use these values to query my FramedGraph.

The documentation for these annotations can be found here: https://github.com/tinkerpop/frames/wiki/Typed-Graph

Example Code

@TypeField("type")
@TypeValue("person")
interface Person extends VertexFrame { /* ... */ }

Then define the FramedGraphFactory by adding TypedGraphModuleBuilder like this.

static final FramedGraphFactory FACTORY = new FramedGraphFactory(
    new TypedGraphModuleBuilder()
        .withClass(Person.class)
        //add any more classes that use the above annotations. 
        .build()
);

Then to retrieve vertices of type Person

Iterable<Person> people = framedGraph.getVertices('type', 'person', Person.class);

I'm not sure this is the most efficient/succinct solution (I'd like to see what @stephen mallette suggests). It's not currently available, but it'd be logical to be able to do something like:

// framedGraph.getVertices(Person.class)

This question looks likes it's the same as this question (looks like you were first) - Tinkerpop Frames: Query vertices based on interface type.

查看更多
登录 后发表回答