gremlintinkerpop

What's a hasContainer() in TinkerPop Gremlin?


I recently stumbled upon the concept of hasContainers, however I don't find much about what it is in the documentation and the Practical Gremlin book.

So what's a hasContainer and how and when is it used?


Solution

  • If you are the average user of Gremlin the HasContainer really shouldn't be of interest to you in building graph applications. The documentation you linked is meant for those types of users so that's why you won't find much about that in there. In fact, outside of the javadoc and the code itself I doubt you will really find it mentioned.

    In answer your question though a HasContainer is a concept that only has relevance to those building TinkerPop graph implementations, typically in relation to query optimization when developing TraversalStrategy implementations. It is thus an internal concept to TinkerPop. The following won't make a lot of sense unless you're really tuned into this aspect of working with TinkerPop.

    A HasContainer is constructed internally within certain Gremlin Step implementations like HasStep to hold the key and the Predicate from calls to has(key, predicate). Rather than construct multiple HasStep objects for each call to has(), multiple HasContainer objects are constructed and are folded back into the same HasStep. For example:

    gremlin> g.V().hasLabel('person').has('name','bob').toString()
    ==>[GraphStep(vertex,[]), HasStep([~label.eq(person), name.eq(bob)])]
    

    Note how the toString() representation of Gremlin above does not have two HasStep objects. It has the "GraphStep(vertex,[])" for V() and "HasStep([~label.eq(person), name.eq(bob)])" for hasLabel('person').has('name','bob'). The hasLabel('person') constructs the HasStep with a HasContainer for "~label.eq(person)" and then the next call to has('name','bob') doesn't bother with a new HasStep. It detects the previous one and folds it's HasContainer into that for "name.eq(bob)".

    The HasContainer itself has some helper methods for working with the the key and the predicate as single object for convenience. Again, these are extreme internals for TinkerPop and you would likely not encounter them with normal usage.