1

My question must be so basic but any help is appreciated

I have a object like below

{
    "type":"type_value",
    "fruits" :[{"amt": 5},{"amt": 10}],
    "shops" : [{"location": "X"},{"location": "Y"}]
}

I am using Spring WebFlux and couchbase. Couchbase supports collections and I am looking to store above object in 3 collections. the array of fruits/shops can contain any number of elements.

Couchbase has 'insert' function which returns Mono on one insertion. e.g.

Step 1:

Mono<TransactionGetResult> insertResult = ctx.insert(reactiveCollectio, id, objectToPersist)

I can only insert next element on 'insertResult' object. like below Step 2:

 Mono<TransactionGetResult> insertResult = insertResult.then(ctx.insert(reactiveCollectio, id, objectToPersist));

again now if I want to insert next element I have to follow same as step 2 and keep doing until I am done then only I can commit the transaction.

using reactor api's how can I iterate and and within same how to use previous result ( Mono<TransactionGetResult> insertResult ) to perform my insertion?

Satya
  • 39
  • 5
  • 1
    Use `ctx.insert(...).map(result -> ...)` – Felipe Apr 25 '21 at 07:47
  • can you explain with a sample code snippet, how I will get next element in my stream and how this will work when I have unknown number of elements in list? – Satya Apr 25 '21 at 10:14
  • Which list are you talking? Your return is an object wrapped in a Mono: `Mono ` – Felipe Apr 25 '21 at 10:38
  • I have list of fruits which I am trying to insert, he list can contain any number of elements. I can only insert second element after I get result of Mono – Satya Apr 25 '21 at 10:51
  • Is the list of fruits `reactiveCollectio`? If so, you have to iterate `reactiveCollectio` outside of the `ctx.insert(` with a `foreach` – Felipe Apr 25 '21 at 11:01
  • ReactiveCollection is an instance pointing to couchbase collection ( which is equivalent to table in relational database). fruits is an array which I want to persist but the only way is I need to take first entry then call insert on it, take the response from last insert and call insert on next element. In a usual for loop it can be written as follows – Satya Apr 25 '21 at 14:21
  • ```` Mono transactionResult = null; for (int i = 0; i < fruitsList.size(); i++) { Fruit fruit1 = fruitsList.get(i); String fruitId = UUID.randomUUID().toString(); System.out.println("Fruit Id: " + fruitId); fruit1.setIdIfNotPresent(fruitId); if (transactionResult == null) { transactionResult = ctx.insert(collection, fruit1.getId(), fruit1); } else { transactionResult = transactionResult.then(ctx.insert(collection, fruit1.getId(), fruit1)); } } ```` – Satya Apr 25 '21 at 14:22
  • @Toerktumlare : are you part of stack overflow team? – Satya Apr 26 '21 at 13:08
  • Just to clarify I am not here to buy any code either if people are selling code. Thanks for your inputs anyway. – Satya Apr 26 '21 at 13:16
  • `can you explain with a sample code snippet, how I will get next element in my stream and how this will work when I have unknown number of elements in list` please post what you have tried first, and we will help you work through your problems. – Toerktumlare Apr 26 '21 at 15:56
  • @Satya have you looked through the examples on the docs (https://docs.couchbase.com/java-sdk/current/howtos/distributed-acid-transactions-from-the-sdk.html)? Generally with Reactive Mono you will use .flatMap() or .then() to combine ops. E.g. `return ctx.insert(...).flatMap(ignored -> ctx.insert(...)).flatMap(ignored -> ctx.insert(...))`. Or `return ctx.insert(...).then(ctx.insert(...)).then(ctx.insert(...))` – Graham Pople Apr 28 '21 at 16:49

0 Answers0