1

I am trying to implement a highly concurrent access pattern where every request should get a unique document when they do a get. I can't use N1QL and I dont have key to do KV fetch. I implemented an array of documents and as Arrays.asList(remove(0)) is a thread safe call, every parallel thread should be able to remove the rolling 0th element of the array, making sure, no 2 threads remove the same element. This is working fine with concurrent thread. However, now problem is, that as every thread also wants to use the document content retrieved, I am not seeing any method to deserialize the removed element and read the content. Remove call doesn't return the element as such.

Any guidance/pointers will be appreciated.

Here is my code snippet:

MutateInResult resultDet = collection.mutateIn("TestDoc", Arrays.asList(remove("[0]")));

Thanks Naved

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121

1 Answers1

2

This feature (return the removed content) has been requested before, and is tracked as MB-31401. The comments on that issue explain why it wasn't part of the original design.

Until that issue is resolved, you'd need to get the value using lookupIn before removing it (using the CAS value from the lookupIn result, and retrying on CAS mismatch). There's an example of this technique in CouchbaseQueue.poll(), although that particular code runs the risk of removing the item from the database before it has actually been processed by your application.

I'm not aware of a simple way to ensure each array element is handled by exactly one thread. If you ensure the element is processed before it is deleted, it might be processed by multiple threads. On the other hand, if you delete the element before processing it, you can ensure it's handled by at most one thread, but you risk it being handled by zero threads if there's an application failure after the element is deleted and before it's processed.

dnault
  • 8,340
  • 1
  • 34
  • 53