Is there a way to get the number of items in a bucket?

For some test automation, I’m trying to use gocb to flush a bucket. However, since (as I understand it) flushing a bucket is an async operation, I need to do a polling loop to block until there are no longer any items left in the bucket.

I see in the Getting single bucket information docs, there is a REST api which includes the itemCount field, which looks like what I need to determine when the bucket flush has finished.

    "basicStats": {
        "dataUsed": 16824320, 
        "diskFetches": 0, 
        "diskUsed": 18068198, 
        "itemCount": 0,  <---------- 
        "memUsed": 33948168, 
        "opsPerSec": 0, 
        "quotaPercentUsed": 12.64667809009552

Is there a way to call this API from gocb or otherwise find out the bucket item count?

1 Like

Hey @traun,

There is not currently a method allowing you to fetch the total number of items within a bucket. The ideal method for detecting when the flush has completed is to insert a document prior to performing the flush, and then watch for the document being no longer found. We are however looking into provide a method to enable cluster-topology aware HTTP requests, which would allow you to fetch the statistics you mentioned.

Cheers, Brett

Ok thanks for the heads up.

As a workaround I’m directly hitting the GET /pools/default/buckets/[bucket-name] endpoint and checking the itemCount – so far it seems reliable, but if you know of any reason why this would fail, then I can switch over to the insert/poll workaround.

FWIW… I’ve done this with a N1QL count query but maybe hitting REST is more reliable at some level I’m unaware of. In my (very tiny) test environment a flush of travel-sample resulted in 0 documents in <500ms.