Hi Priya,
thank you for the explanation and the warning not to use it. My original problem was to update a document via SG Admin REST API and I needed to get the latest revision of an existing document. [1]
I ran the original code twice and further down are the logs. As far as I can tell the document ist created as it returns an id, status ok=true and a revision. The first time I ran the code updating worked fine. Second time there was a conflict. I started the code manually by clicking a button. The doc name is the current time in millis. So it’s not a for-loop which might create a race condition.
docName = 1560799655926
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 92 100 75 100 17 1960 444 --:--:-- --:--:-- --:--:-- 1973
{"id":"1560799655926","ok":true,"rev":"1-34b681f7e2d8fa8e4f4e75e95c14ee88"}
queryString = SELECT meta().xattrs._sync.rev FROM my_bucket USE KEYS '1560799655926';
rev = 1-34b681f7e2d8fa8e4f4e75e95c14ee88
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 90 100 75 100 15 13758 2751 --:--:-- --:--:-- --:--:-- 15000
{"id":"1560799655926","ok":true,"rev":"2-a000951bd8edf462fd063d47af1caa5b"}
docName = 1560799702281
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 92 100 75 100 17 10799 2447 --:--:-- --:--:-- --:--:-- 12500
{"id":"1560799702281","ok":true,"rev":"1-34b681f7e2d8fa8e4f4e75e95c14ee88"}
queryString = SELECT meta().xattrs._sync.rev FROM my_bucket USE KEYS '1560799702281';
rev = 1-34b681f7e2d8fa8e4f4e75e95c14ee88
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 73 100 58 100 15 9566 2474 --:--:-- --:--:-- --:--:-- 11600
{"error":"conflict","reason":"Document revision conflict"}
The new code following @graham.pople’s advice uses the Java SDK to get the latest revision:
/* init cluster*/
CouchbaseCluster cluster = CouchbaseCluster.create("localhost");
cluster.authenticate("sync_gateway", "my_pass");
Bucket bucket = cluster.openBucket("my_bucket");
/* Create document */
long docName = System.currentTimeMillis();
log.info("docName = " + docName);
String urlEncodedDocName = URLEncoder.encode(String.valueOf(docName), "UTF-8");
JsonObject content = JsonObject.empty().put("color", "green");
ProcessBuilder pb = new ProcessBuilder("curl", "-X", "PUT", "http://localhost:4985/my_bucket/" + urlEncodedDocName + "?new_edits=true",
"-H", "accept: application/json", "-H", "Content-Type: application/json", "-d", content.toString());
pb.start();
pb.redirectErrorStream(true);
Process process = pb.start();
String s = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8.name());
log.info("process = " + s);
/* Get revision */
DocumentFragment<Lookup> result = bucket.lookupIn(String.valueOf(docName)).get("_sync.rev", SubdocOptionsBuilder.builder().xattr(true)).execute();
String rev = (String) result.content(0);
log.info("rev = " + rev);
/* Update document */
content = JsonObject.empty().put("color", "red");
pb = new ProcessBuilder("curl", "-X", "PUT", "http://localhost:4985/my_bucket/" + urlEncodedDocName + "?new_edits=true&rev=" + rev,
"-H", "accept: application/json", "-H", "Content-Type: application/json", "-d", content.toString());
pb.start();
pb.redirectErrorStream(true);
process = pb.start();
s = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8.name());
log.info("process = " + s);
I tried 3 times and the third time a conflict occured. Here are the logs:
docName = 1560800656201
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 64 100 47 100 17 4034 1459 --:--:-- --:--:-- --:--:-- 4272
{"error":"conflict","reason":"Document exists"}
rev = 1-34b681f7e2d8fa8e4f4e75e95c14ee88
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 90 100 75 100 15 56137 11227 --:--:-- --:--:-- --:--:-- 75000
{"id":"1560800656201","ok":true,"rev":"2-a000951bd8edf462fd063d47af1caa5b"}
docName = 1560800686403
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 92 100 75 100 17 8679 1967 --:--:-- --:--:-- --:--:-- 9375
{"id":"1560800686403","ok":true,"rev":"1-34b681f7e2d8fa8e4f4e75e95c14ee88"}
rev = 1-34b681f7e2d8fa8e4f4e75e95c14ee88
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 90 100 75 100 15 13794 2758 --:--:-- --:--:-- --:--:-- 15000
{"id":"1560800686403","ok":true,"rev":"2-a000951bd8edf462fd063d47af1caa5b"}
docName = 1560800710049
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 64 100 47 100 17 3549 1283 --:--:-- --:--:-- --:--:-- 3615
{"error":"conflict","reason":"Document exists"}
rev = 1-34b681f7e2d8fa8e4f4e75e95c14ee88
process = % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 73 100 58 100 15 9689 2505 --:--:-- --:--:-- --:--:-- 9666100 73 100 58 100 15 9460 2446 --:--:-- --:--:-- --:--:-- 8285
{"error":"conflict","reason":"Document revision conflict"}
Because of the above I believe that there is a bug when updating a document via SG Admin REST API. When you run the code do you also see this conflict? Or how would one update a document correctly with the SG please? Where did I go wrong in the code?
Thanks!
[1] https://www.couchbase.com/forums/t/retrieve-xattrs-sync-rev-via-n1ql-java-sdk-or-admin-rest-api/21759