There is a 1-second latency between ivy.repo.save(object) and ivy.repo.search(Object.class)
Hi Axon.ivy Team,
Given the snippet of code:
Person p = new Person();
p.id = UUID.randomUUID().toString();
p.name = "John Doe";
p.birthdate = LocalDate.now();
ivy.repo.save(p);
Object result = ivy.repo.search(Person.class)
.textField("id").containsPhrase(p.id)
.execute().getFirst();
ivy.log.error("HHH - it is null " + #result);
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
result = ivy.repo.search(Person.class)
.textField("id").containsPhrase(p.id)
.execute().getFirst();
ivy.log.error("HHH - it is null " + #result);
I found that there was a *1-second* latency between the call to `ivy.repo.save()` and `ivy.repo.search()`. This is super annoying because we had to wait for *1-second* every time until the saved object can be searched via the API. Blocking the execution for 1-second to wait until it is indexed to ES _every_ _time_ we save the object into the `BusinessDataRepository` is a terrible hack.
Is it by design? Can this be fixed so that the two calls work?
> P.S: Please don't advise me to use the returned BusinessData id because it doesn't address my issue. I want to *search* back the data I save based on the object's field, not fetching it again.
> P.P.S: Tested with Axon.ivy Designer 7.2.1 on my local workstation.
**UPDATE**
_25.2.2019_:
In our project, we were able to develop RESTful API for other parties. As we already have persisted our objects (for example `Dossier`), in `BusinessDataRespository`, we would want to preserve that behavior.
An excerpt from of our OpenAPI spec:
paths:
/dossiers/{dossier_id}/persons/:
post:
operationId: addPersonToDossier
description: add a single person into an existing Dossier
requestbody:
'application/json':
schema:
$ref: '#/components/schemas/Person'
responses:
'201':
get:
operationId: getAllPersonsInDossier
description: get *all* existing persons of an existing Dossier
responses:
'200':
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/schemas/Person'
From our client, after they have already invoked several calls to `addPersonToDossier`, they may want to invoke `getAllPersonsInDossier` to fetch all `Person`s of a `Dossier`.
We persists `Person` separately from our `Dossier` to avoid `ConcurrentModificationException`, each `Person` holds a reference to its `Dossier`. Now in order to implement `getAllPersonsInDossier`, we have to use `Ivy.repo().search(..)`.
Unfortunately, due to the 1-second latency, calling `getAllPersonsInDossier` too soon will probably return an empty result. This forces us to either:
- Require our clients to WAIT on their side for *1-second* until they can call `getAllPersonsInDossier`.
- On the server side, we somehow have to implement a `while` loop to check until the `Person` could be found via search API, then we consider the `addPersonToDossier` finishes (or worse, a `Thread.sleep(1000)`).
Jack