# ID Generators

Id generator class is defined with "id.generator" setting and can be one of the following classes.

All id generators can use the following parameters:

| Parameter | Definition                                                                   | Example | Default         |
| --------- | ---------------------------------------------------------------------------- | ------- | --------------- |
| id.prefix | Prefix to add to generated ids                                               | ID-     | -               |
| id.suffix | Suffix to add to generated ids                                               | -val    | <p></p><p>-</p> |
| id.digits | Minimum number of digits for an id                                           | 10      | -               |
| id.filler | Character to repeat as prefix if id is shorter than minimum number of digits | X       | 0               |

## RandomIDGenerator

Generates a random globally unique ID each time. This generator[^1] has no settings.

## JmesPathIDGenerator

Generates an ID using JmesPath expression with event payload used as the input. Uses the following state parameters:

| Parameter  | Definition                | Example                    | Default |
| ---------- | ------------------------- | -------------------------- | ------- |
| id.pattern | JmesPath pattern to apply | join('' \['id-', data.id]) | -       |

## NumberIDGenerator

Uses instance of a number generator[^2] to generate unique numerical ids. Uses the following state parameters:

| Parameter              | Definition                                                                                       | Example                                                    | Default         |
| ---------------------- | ------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- | --------------- |
| id.numberGenerator     | Class name of number generator to use                                                            | com.rierino.handler.util.generator.ConstantNumberGenerator | -               |
| id.label               | Label to use for numbers generated if number generator is shared between different types of data | product                                                    | <p></p><p>-</p> |
| id.checksum            | Whether to calculate a checksum digit for generated number                                       | true                                                       | false           |
| id.checkReverse        | Whether the checksum digit should be subtracted from 10                                          | true                                                       | false           |
| id.checkOddMultiplier  | Number to multiply each odd digit with for checksum calculation                                  | 3                                                          | 1               |
| id.checkEvenMultiplier | Number to multiply each even digit with for checksum calculation                                 | 1                                                          | 1               |

### ConstantNumberGenerator

Produces a constant number only, using "id.value" setting, assigning same id to all records, only used for testing purposes.

### EpochNumberGenerator

Produces number based on epoch time in milliseconds, which does not guarantee uniqueness especially for a distributed deployment.

### MongoNumberGenerator

Through "id.system" and "id.collection" settings, uses a MongoDB collection for creating and tracking globally unique ids.

{% hint style="info" %}
MongoNumberGenerator is a buffered number generator, where "id.buffer" can be set to define maximum increments allowed for "reserving" ids on each request.

Buffering happens in exponential iterations (e.g. reserving 1, 2, 4, 8, ... until max is reached) so that continuous system restarts do not result in unnecessary reservations.
{% endhint %}

## OffsetPartitionedIDGenerator

Uses source partition, key and offset of the event for generating unique ids. This [generator ](#user-content-fn-3)[^3]has no settings.

## PartitionedIDGenerator

[Uses ](#user-content-fn-4)[^4]instances of another id generator class, with each instance dedicated to a [specific partition](#user-content-fn-5)[^5]. Partition is suffixed to the id generated by used instances, ensuring global uniqueness across partitions. Uses the following state parameters:

| Parameter            | Definition                           | Example                                              | Default                                              |
| -------------------- | ------------------------------------ | ---------------------------------------------------- | ---------------------------------------------------- |
| id.partitioned.class | Class name of inner generator to use | com.rierino.handler.util.generator.NumberIDGenerator | com.rierino.handler.util.generator.RandomIDGenerator |

## Dependent ID Generators

These id generators generate ids for children of an existing entity (e.g. variants of a product), in a \[prefix]\[delimiter]\[newid] type format. The following settings are shared among these generators:

| Parameter       | Definition                                                                   | Example     | Default       |
| --------------- | ---------------------------------------------------------------------------- | ----------- | ------------- |
| id.digits       | Number of digits to generate children id with (prefixing shorted ids with 0) | 4           | -             |
| id.suffixParent | Whether parent id should be a prefix or a suffix                             | true        | false         |
| id.delimiter    | Delimiter between parent and child ids                                       | -           |               |
| id.parentPath   | Json path of the data element keeping parent id for a child record           | data.parent | data.parentId |

### DependentEventIDGenerator

Uses and increments an ID provided in event payload to generate a new ID. Typically used within a saga where a previous step calculates max child id so far.

| Parameter       | Definition                                      | Example | Default |
| --------------- | ----------------------------------------------- | ------- | ------- |
| id.maxChildPath | Json path of the max ID so far in event payload | prevId  | maxId   |

### DependentQueryIDGenerator

Uses and increments the max ID stored in a database to generate a new ID. Typically used with a query getting maximum child ID directly from a database table / collection.

| Parameter        | Definition                                                          | Example         | Default             |
| ---------------- | ------------------------------------------------------------------- | --------------- | ------------------- |
| id.eventParent   | ...                                                                 | true            | false               |
| id.query.state   | State manager keeping query details                                 | query\_local    | query               |
| id.query.id      | Id of the query for selecting max child id for a given parent       | product\_maxid  | \[state][^6]\_maxid |
| id.query.manager | Query manager which should run the query for selecting max child id | product\_master | master              |

This generator injects "%%parentid%%" variable in executed query and expects the query to return maximum child id so far as "maxId" field.&#x20;

[^1]: com.rierino.handler.util.generator.RandomIDGenerator

[^2]: com.rierino.handler.util.generator.NumberIDGenerator

[^3]: com.rierino.handler.util.generator.OffsetPartitionedIDGenerator

[^4]: com.rierino.handler.util.generator.PartitionedIDGenerator

[^5]: Which is the event source partition

[^6]: State for which the id is generated    &#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rierino.com/devops/microservices/building-blocks/data-sources/id-generators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
