Which version of Java SDK has the counter support?

#1

I am trying out the below code samples available at http://docs.couchbase.com/developer/java-2.1/documents-atomic.html , but i don’t see the code available in 2.1.3. I have added the gradle dependency for couchbase as

compile(‘com.couchbase.client:java-client:2.1.3’)

It doesn’t seem to recognize Observable , LongDocument or there is no map method available in bucket. What is the correct java sdk version of couchbase for this example?

Observable doc = bucket.counter(“id”, 5);

bucket
.counter(“user::id”, 1, 1)
.map(new Func1<LongDocument, String>() {
@Override
public String call(LongDocument counter) {
return “user::” + counter.content();
}
})
.flatMap(new Func1<String, Observable>() {
@Override
public Observable call(String id) {
return bucket.insert(JsonDocument.create(id, JsonObject.empty()));
}
}).subscribe();

#2

hi @madhu_garimilla1
It may be misleading, the bucket variable in this example is actually supposed to be an AsyncBucket instance.
If you use the blocking api you have a Bucket instance. You can easily access the async API from there by just chaining in the async() method.
So your example would probably work with the following addition (row 2):

bucket
.async()
.counter("user::id", 1, 1)
.map(new Func1() {
@Override
public String call(LongDocument counter) {
return "user::" + counter.content();
}
})
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.insert(JsonDocument.create(id, JsonObject.empty()));
}
}).subscribe();
#3

Thanks @simonbasle. i got it working this way

private static void initCouchbaseBucket(JsonObject object)
{
List list = new ArrayList<>();
list.add(“localhost”);

	Cluster cluster = CouchbaseCluster.create(list);
	Bucket bucket = cluster.openBucket("samplebucket", "");
	
	bucket.async()
    .counter("ADJ_TXN_ID:COUNTER", 1, 1).map(new Func1<JsonLongDocument, String>() {

		@Override
		public String call(JsonLongDocument counter) {
			return "ADJ_TXN_ID:" + counter.content();
		}		
	}).flatMap(new Func1<String, rx.Observable<JsonDocument>>() {

		@Override
		public Observable<JsonDocument> call(String id) {
			return bucket.async().insert(JsonDocument.create(id, object));
		}
	}).subscribe();

}

Now here in my second call() , i am trying to insert a new json document by sending it as argument to this method and trying to use the id ,generated using the counter , as my document id. however, when i run this method and check in the couch base server I could see counter value getting incremented but only one of my document(first) is added. after that only counter is getting incremented but new documents are not added. What am i missing here ? Is this the right way of inserting a document?

#4

yeah it should work… you’re running the whole method several times and only the first time generates a document, correct?

#5

Yes. you are correct. Then if i delete the document then i can insert a new document and that’s just once. same problem repeats again. How do i solve this ?

#6

can you check that the generated keys (in your map) are congruent with the state of the counter you see in the webconsole? (eg. by logging it directly in the map function or in a separate doOnNext)

#7

Counter value is being incremented properly which i could see in web console but the control is not coming to the flatMap

flatMap(new Func1<String, rx.Observable>() {

	@Override
	public Observable<JsonDocument> call(String id) {
		return bucket.async().insert(JsonDocument.create(id, object));
	}
}
#8

to debug further, you should probably add actions in your subscribe:

...
.subscribe(new Action1<JsonDocument>() {
  public void call(JsonDocument doc) {
    //log the document / document's id, anything. this means that the doc was stored
  },
  //this is the error handler
  new Action1<Throwable>() {
    public void call(Throwable error) {
      //log the error, you probably got one
    }
);
#9

Added these actions to subscribe() , i don’t see any errors logged. This works fine when i do a step by step debug but it doesn’t work if i do a fast debug jumping from one method to another or if i run the test without debug. Looks like there is some timing issue. Any clues?

#10

are you by any chance running that code in a main method? if so it could be that the app exits before the SDK even had the chance to do the inserts… try replacing the subscribe(...) with a toBlocking().single().

I’ve quickly tested to reproduce your code inside a 10 iterations for-loop and that’s the first thing that came up, with the blocking it works fine…

1 Like
#11

Thanks @simonbasle this worked.