-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Java based unique ID Generator
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mrppa:uniquegen:<Version>'
}
Version
- Refer to releases for the latest version. https://github.com/mrppa/uniquegen/releases
Syntex
final IDGeneratorContext idGeneratorContext = new ContextBuilder()
...
.build();
final IDGenerator idGenerator = IDGenProvider.getGenerator(<IDProviderType>, idGeneratorContext);
String generatedId = idGenerator.generateId();
example
final IDGeneratorContext idGeneratorContext = new ContextBuilder()
.add(DateSequenceIDGenerator.CONTEXT_INSTANCE_ID, "inst1")
.build();
final IDGenerator idGenerator = IDGenProvider.getGenerator(GenerateType.DATE_SEQUENCE_BASED, idGeneratorContext);
String generatedId = idGenerator.generateId();
IDProviderType
- Type of the id provider
idGeneratorContext
- Context with required variables for each type
Refer to each ID Provider types for required context variables
Generate distributed id based on the instance id, date and a running sequence. Length is 29 digits
[Date][Sequence][Instance id]
Date
- Date formatted in yyyyMMddHHmmssSSS . 17 digits
Sequence
- A running sequence number . 6 digits left padded. Recycled after every 999,999 records
Instance id
- Instance Id left padded . 6 digits
sample id
202212280002261240000010inst1
final IDGeneratorContext idGeneratorContext = new ContextBuilder()
.add(DateSequenceIDGenerator.CONTEXT_INSTANCE_ID, "inst1")
.build();
final IDGenerator idGenerator = IDGenProvider.getGenerator(GenerateType.DATE_SEQUENCE_BASED, idGeneratorContext);
String generatedId = idGenerator.generateId();
Variable | Desc | Data type | Mandatory |
---|---|---|---|
INSTANCE_ID | Instance id of the service | String | No. Default set to 000000 |
Generate sequence backed by JDBC sequences . Length is 24 digits
[Date][Sequence]
Date
- Date formatted in yyyyMMddHHmmss . 14 digits
Sequence
- database sequence number . 10 digits left padded. Recycled based on the database
sample id
202212291707590000000001
final IDGeneratorContext idGeneratorContext = new ContextBuilder()
.add(DateSequenceIDGenerator.JDBC_DATASOURCE, datasource)
.add(DateSequenceIDGenerator.SEQUENCE_NAME, "test_sequence")
.build();
final IDGenerator idGenerator = IDGenProvider.getGenerator(GenerateType.JDBC_SEQUENCE_BASED, idGeneratorContext);
String generatedId = idGenerator.generateId();
Variable | Desc | Data Type | Mandatory |
---|---|---|---|
JDBC_DATASOURCE | JDBC datasource object | javax.sql.DataSource | Yes |
SEQUENCE_NAME | DB sequence name compatible with JDBC naming conventions | String | No. Value default to uniquegen_jdbc |
- PostgreSQL
- MariaDB
- H2
Generate ids backed by JDBC table . Length is 20 digits
[Sequence]
Sequence
- sequence number . 20 digits left padded.
sample id
00000000000000000001
final IDGeneratorContext idGeneratorContext = new ContextBuilder()
.add(JDBCTableSequenceIDGenerator.JDBC_DATASOURCE, dataSource)
.add(JDBCTableSequenceIDGenerator.SEQUENCE_NAME, "test_Sequence")
.add(JDBCTableSequenceIDGenerator.CACHE_SIZE, 1000)
.build();
final IDGenerator idGenerator = IDGenProvider.getGenerator(GenerateType.JDBC_TABLE_SEQUENCE_BASED, idGeneratorContext);
String generatedId = idGenerator.generateId();
Variable | Desc | Data Type | Mandatory |
---|---|---|---|
JDBC_DATASOURCE | JDBC datasource object | javax.sql.DataSource | Yes |
SEQUENCE_NAME | Sequence name. Maximum 30 characters allowed | String | No. Value default to uniquegen_seq |
CACHE_SIZE | Amount of records to be fetched in one time from DB | Integer | No. Value default to 100 |
- PostgreSQL
- MariaDB
- H2
- MySql
- Checkout the repository and open the project with the ide
-
gradle build
for build the project
The main interface for any id generation logic.
Enum to hold the generated types
Factory to return the IDGenerator implementation by GenerateType
Base unit test to for each IDGenerator implementation
Hold the context for each generators