Git Product home page Git Product logo

uniquegen's Introduction

uniquegen

Java based unique ID Generator

How to install with Gradle

Using jitpack

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.mrppa:uniquegen:<Version>'
}

Maven and more

Version - Refer to releases for the latest version. https://github.com/mrppa/uniquegen/releases

Usage

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

ID provider type

DATE_SEQUENCE_BASED

Generate distributed id based on the instance id, date and a running sequence. Length is 29 digits

Format

[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

Example usage

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(); 

Context variables

Variable Desc Data type Mandatory
INSTANCE_ID Instance id of the service String No. Default set to 000000

JDBC_SEQUENCE_BASED

Generate sequence backed by JDBC sequences . Length is 24 digits

Format

[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

Example usage

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(); 

Context variables

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

Supported databases

  • PostgreSQL
  • MariaDB
  • H2

JDBC_TABLE_SEQUENCE_BASED

Generate ids backed by JDBC table . Length is 20 digits

Format

[Sequence]

Sequence - sequence number . 20 digits left padded.

sample id

00000000000000000001

Example usage

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(); 

Context variables

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

Supported databases

  • PostgreSQL
  • MariaDB
  • H2
  • MySql

Development/Extention Guideline

  • Checkout the repository and open the project with the ide
  • gradle build for build the project

Classes

IDGenerator.java

The main interface for any id generation logic.

GenerateType.java

Enum to hold the generated types

IDGenProvider

Factory to return the IDGenerator implementation by GenerateType

BaseIDGeneratorTest

Base unit test to for each IDGenerator implementation

IDGeneratorContext

Hold the context for each generators

uniquegen's People

Contributors

mrppa avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.