I want to retrieve the specific range of users(required for pagination) and want to retrieve the total count as well, I'm executing the below query which is retrieving the list of user vertices as expected but the total count is returned as BulkSet
Map<String, Object> result = gt.V().hasLabel("user").sideEffect(__.count().store("total"))
.order().by("name", Order.desc)
.range(0, 10).fold().as("list")
.select("list","total").next();
The output is as below
How do I get the correct count as a Long
value instead of the BulkSet
?
Paging with Gremlin is discussed here and references this blog post which provides additional information on the topic. Those resources should help you with your paging strategy.
You framed this question in terms of inquiring about BulkSet
so it isn't quite a duplicate of the answer I referenced, so I will try to answer that much for you. BulkSet
allows for an important traversal optimization in TinkerPop which helps reduce object propagation, thus reducing memory requirements for a particular query. It does this by holding the traverser object and its count where the count is the number of times that object has been added to the BulkSet
. Calling size()
or longSize()
(where the latter returns a long
and the former returns int
) will return the summation of the counts and therefore the "correct" or actual count of the objects. A call to uniqueSize()
will return actual size of the set which will be the unique objects within it.
If you want the size of the BulkSet
you just need to count()
it:
gt.V().hasLabel("user").sideEffect(__.count().store("total"))
.order().by("name", Order.desc)
.range(0, 10).fold().as("list")
.select("list","total")
.by().
.by(count(local))
That said, I don't think your traversal isn't really doing what you want . The sideEffect()
is just counting the current traverser which will simply return "1" and then you store that "1" in the list "total". At least that's what I see with TinkerGraph:
gremlin> g.V().hasLabel("person").sideEffect(count().store("total")).range(0,1).fold().as('list').select('list','total').by().by(count(local))
==>[list:[v[1]],total:1]
gremlin> g.V().hasLabel("person").sideEffect(count().store("total")).range(0,10).fold().as('list').select('list','total').by().by(count(local))
==>[list:[v[1],v[2],v[4],v[6]],total:4]
Interesting that JanusGraph somehow gives you 114 rather than 10 for the "total". I'd not expect that. I'd consider avoiding reliance on that "feature" in the case it is a "bug" that is later "fixed". Instead, please consider the posts I'd provided and look at them for inspiration.