2

I am using Spring data couchbase with reactive repository. Calling findById method from reactive repository. Once while updating checking if record exists and another one while fetching by document id. Both cases passing the unique value which creates document id. Fetching document by value successfully retrieves data.While updating data using the same fetchMyValue method to check if document exists which always returns empty result. Both cases id string generated is same.

Below is the code snippet for reference.

public Mono<MyValue> fetchMyValue(final String id) {
    String id = CommonUtil.buildDocumentKey(id); //same id
    return repo.findById(id).flatMap(mo -> {

        return Mono.just(mo);
    }).switchIfEmpty(//error Handling);

}

Above method returns value from couchbase. But below one fetchMyValue always returns empty mono object and update logic is not executed.Always switchIfEmpty is executed.

public Mono<ResponseStatus> saveMyObject(final Mono<MyObject> value) {
    return value.flatMap(myValue -> {
        try {
            if (null != myValue.getId()) {
                 fetchMyValue(myValue.getId())
               //calling above method 
               //passing value which generates same id
                .flatMap(mv -> {
                            //update logic
                        })
                .switchIfEmpty(saveMyValue(myValue));
            }
        } catch (Exception e) {
            //exception handling
        }

Below is buildDocumentKey method

public static String buildDocumentKey(String type) {
     return new StringBuilder()
             .append(Constants.PREFIX).append(Constants.DELIMITER)
             .append(type).toString();      
}

Even copy pasting document key value from fetchMyValue and executong in couchbase query returns \n

  1. empty if its from fetchMyValue->Save/Update method

  2. returns a value if its directly from fetchMyValue

Both strings are same.Then why is couchbase not returning result while saving/update.Is my reactive coding not in place. Please help me on this have been struggling for a long time.

Sagarmatha
  • 65
  • 7
  • Are you sure you use the same bucket for fetch() and save()? – Bor Laze Mar 11 '18 at 17:35
  • Yes, I am using the same bucket. – Sagarmatha Mar 12 '18 at 02:59
  • Could you share a bit more in your samples? Could your provide the missing code snippets in `saveMyObject` - there is no return statement within the `value.flatMap` call so it should not compile – Brian Clozel Mar 12 '18 at 11:58
  • I'm having a bit trouble understanding, does fetchMyValue return value and saveMyValue (which uses save) returns empty? – subhashni Mar 12 '18 at 21:06
  • fetchMyValue is used in saveMyObject which always return empty .But If I directly call fetchMyValue it returns correct values. – Sagarmatha Mar 13 '18 at 04:50
  • So bascially `fetchMyValue` within `saveMyObject` returns empty.But same call `fetchMyValue` if I call directly it returns correct values.`fetchMyValue` uses `repo.findById` – Sagarmatha Mar 13 '18 at 04:58

0 Answers0