Check an Existence of Data

To check already existing data (for example, an entity exists according to some condition). Dynamic Logic can have a condition depending on the presence of data.

Following is an example to check if Registrations with status N exist for the policy; some action needs to happen.

Make sure you setPageSize to (1) to limit the number of rows.

List<Registration> registrations = new SearchBuilder(Registration.class)
        .by("correlationId").eq(policy.gid).and()
        .by("status").eq("N")
        .setPageSize(1) // only check for existence
        .execute()

if (registrations) {
    log.debug("found registration {0} in status N for policy GID {1} ", registrations[0].id, policy.gid)
    // Some action
}

You can also query to check for an entity’s existence without setting the page size setPageSize to (1) using the search builder in the following way:

List<Registration> registrations = new SearchBuilder(Registration.class)
        .by("correlationId").eq(policy.gid).and()
        .by("status").eq("N")
        .execute()

if (registrations) {
    log.debug("found registration {0} in status N for policy GID {1} ", registrations[0].id, policy.gid)
    // Some action
}
if (registrations) is the Groovy way to ask if the list registrations is not empty.

This logic is functionally correct, but this approach may impact the performance and stability of the system. All the registrations are loaded into memory (but we only need to know if there is at least one). There is also no limitation on the number of rows retrieved, so the amount of memory required has no limitation. In the most extreme case, the system can run out of memory, causing the unavailability of the system.

It is advisable to check for existing data only by setting the number of rows to retrieve to 1, which speeds up the data retrieval and reduces memory consumption.