What is the best way to check if the document exists?


I am trying to make an application (in PHP) using the Couchbase DB, but I encounter a problem which I dont’t know how to handle.

If I check if a document exists with something similar to this:

 $result = CouchbaseClient::getCollection($bucketName)

I get a false response (document doesn’t exists)

(I want to mention that for some keys the above code return the correct response)

after this if I try to insert a document with the same key, I get an error saying that the document exists:

Couchbase\KeyExistsException: LCB_ERR_DOCUMENT_EXISTS (305)

sometime I also get this error

Couchbase\DurabilityException: LCB_ERR_DURABLE_WRITE_IN_PROGRESS (310)

From what I understand it seems that the document exists() method doesn’t really return the correct result if the document was just written, or something similar.

Can somebody please help me fix this?

I use couchbase 6.6 and PHP sdk 3.0

This does appear to be correct. Could you supply the specific version of the PHP client and libcouchbase you’re using along with the platform? Or, you may want to file an issue. It might be useful to turn up the log level (TL;DR, just set the LCB_LOGLEVEL envvar to 5) to see if there’s some more detail there.

That definitely shouldn’t be the case. Immediately after creation, it should always return exists.

I may had another problem

two threads running the same code.

both were checking for a document to be present (almost in the same time),

the IF was returning false and the code was continuing up to one of the threads doing the insert, the second one failing.

I have added a file lock and seems that the error dissapeared, so probably this is the explanation.

Not sure if this is the best approach, as it it limited for the code to work on the same server, but if there are multiple servers runnig same code, the filelock would not work…