Skip to content

Commit

Permalink
add distributed id generator
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamv108 committed Jan 21, 2024
1 parent caad54d commit a92e67a
Show file tree
Hide file tree
Showing 59 changed files with 534 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package code.shubham;

import code.shubham.commons.annotations.SpringBootApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("code.shubham")
@SpringBootApplication
@SpringBootApp
public class TemplateServiceJavaSpringBootApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootApplication
@ComponentScan("code.shubham")
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public @interface SpringBootJpaApplication {
@SpringBootApplication
public @interface SpringBootApp {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public class UserIDContextHolder {

private static final ThreadLocal<String> CONTEXT = new ThreadLocal<>();
private static final ThreadLocal<Long> CONTEXT = new ThreadLocal<>();

public static void set(final String id) {
public static void set(final Long id) {
CONTEXT.set(id);
}

public static String get() {
public static Long get() {
return CONTEXT.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package code.shubham.commons.dao.entities;
package code.shubham.commons.dao;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@Configuration
public class PersistenceConfig {

@Bean
public AuditorAware<String> auditorProvider() {
public AuditorAware<Long> auditorProvider() {
return new UserIdAuditorAware();
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/code/shubham/commons/dao/UserIdAuditorAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package code.shubham.commons.dao;

import code.shubham.commons.contexts.UserIDContextHolder;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

public class UserIdAuditorAware implements AuditorAware<Long> {

@Override
public Optional<Long> getCurrentAuditor() {
final Optional<Long> userId = Optional.ofNullable(UserIDContextHolder.get());
if (userId.isPresent())
return userId;

return Optional.ofNullable(-1L);
}

}
29 changes: 29 additions & 0 deletions src/main/java/code/shubham/commons/dao/address/entities/City.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package code.shubham.commons.dao.address.entities;

import code.shubham.commons.dao.base.entities.BaseAbstractDistributedIdEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "cities")
public class City extends BaseAbstractDistributedIdEntity {

@Column(nullable = false)
private String name;

@Column
private Long stateId;

@Column(nullable = false, updatable = false)
private Long countryId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package code.shubham.commons.dao.address.entities;

import code.shubham.commons.dao.base.entities.BaseAbstractDistributedIdEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "countries")
public class Country extends BaseAbstractDistributedIdEntity {

@Column(nullable = false, unique = true)
private String name;

}
26 changes: 26 additions & 0 deletions src/main/java/code/shubham/commons/dao/address/entities/State.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package code.shubham.commons.dao.address.entities;

import code.shubham.commons.dao.base.entities.BaseAbstractDistributedIdEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "cities")
public class State extends BaseAbstractDistributedIdEntity {

@Column(nullable = false, unique = true)
private String name;

@Column(nullable = false, updatable = false)
private Long countryId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package code.shubham.commons.dao.address.repositories;

import code.shubham.commons.dao.address.entities.City;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package code.shubham.commons.dao.address.repositories;

import code.shubham.commons.dao.address.entities.Country;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package code.shubham.commons.dao.entities.base;
package code.shubham.commons.dao.base.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand All @@ -21,7 +21,7 @@
@EqualsAndHashCode(callSuper = true)
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)
public abstract class BaseAbstractAuditableEntity extends BaseIdEntity {
public abstract class BaseAbstractAuditableEntity extends BaseAbstractDistributedIdEntity {

private static final long serialVersionUID = 8953324502234883513L;

Expand All @@ -43,13 +43,11 @@ public abstract class BaseAbstractAuditableEntity extends BaseIdEntity {
private Integer version = 0;

@JsonIgnore
@Column(name = "created_by", updatable = false, columnDefinition = "VARCHAR(36)", length = 36)
@CreatedBy
private String createdBy;
private Long createdBy;

@JsonIgnore
@Column(name = "updated_by", columnDefinition = "VARCHAR(36)", length = 36)
@LastModifiedBy
private String updatedBy;
private Long updatedBy;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package code.shubham.commons.dao.base.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.GenericGenerator;

import java.io.Serializable;
import java.util.Objects;

@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseAbstractDistributedIdEntity implements Serializable {

private static final long serialVersionUID = 8953224502234813513L;

@JsonIgnore
@Id
@GeneratedValue(generator = "distributed-id")
@GenericGenerator(name = "distributed-id",
strategy = "code.shubham.commons.generators.id.hibernate.DistributedIDGenerator")
private Long id;

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final BaseAbstractDistributedIdEntity that = (BaseAbstractDistributedIdEntity) o;
return this.id.equals(that.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package code.shubham.commons.dao.entities.base;
package code.shubham.commons.dao.base.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
Expand All @@ -19,7 +19,7 @@
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseIdEntity implements Serializable {
public abstract class BaseAbstractIdEntity implements Serializable {

private static final long serialVersionUID = 8953224502234883513L;

Expand All @@ -36,7 +36,7 @@ public boolean equals(Object o) {
return true;
if (o == null || getClass() != o.getClass())
return false;
BaseIdEntity that = (BaseIdEntity) o;
BaseAbstractIdEntity that = (BaseAbstractIdEntity) o;
return this.id.equals(that.id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package code.shubham.commons.dao.entities.base;
package code.shubham.commons.dao.base.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.GeneratedValue;
Expand All @@ -16,7 +16,7 @@
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseIntegerIdEntity implements Serializable {
public abstract class BaseAbstractIntegerIdEntity implements Serializable {

private static final long serialVersionUID = 8953224502234883513L;

Expand All @@ -31,7 +31,7 @@ public boolean equals(Object o) {
return true;
if (o == null || getClass() != o.getClass())
return false;
final BaseIntegerIdEntity that = (BaseIntegerIdEntity) o;
final BaseAbstractIntegerIdEntity that = (BaseAbstractIntegerIdEntity) o;
return this.id.equals(that.id);
}

Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/code/shubham/commons/filters/ContextFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse
final String userId = Optional.ofNullable(request.getHeader("userId"))
.orElse((String) request.getAttribute("userId"));
if (userId != null)
UserIDContextHolder.set(userId);
UserIDContextHolder.set(Long.valueOf(userId));

final String tenantId = request.getHeader("tenantId");
if (tenantId != null)
Expand All @@ -43,7 +43,7 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse
final String userEmail = Optional.ofNullable(request.getHeader("userEmail"))
.orElse((String) request.getAttribute("userEmail"));
if (userEmail != null)
UserContextHolder.set(new UserDTO(userId, userEmail));
UserContextHolder.set(new UserDTO(Long.valueOf(userId), userEmail));

chain.doFilter(servletRequest, servletResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package code.shubham.commons.generators.id;

public interface IDGenerator<Type> {

Type generate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package code.shubham.commons.generators.id.constants;

public class Constants {

public static final long SIGN_BIT_LENGTH = 1l;

public static final long EPOCH_BIT_LENGTH = 41l;

public static final long NODE_ID_BIT_LENGTH = 10l;

public static final long SEQUENCE_BIT_LENGTH = 12l;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package code.shubham.commons.generators.id.hibernate;

import code.shubham.commons.generators.id.implementations.SnowflakeSequenceIdGenerator;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

public class DistributedIDGenerator implements IdentifierGenerator {

@Override
public Object generate(final SharedSessionContractImplementor sharedSessionContractImplementor, final Object o) {
return SnowflakeSequenceIdGenerator.getInstance().generate();
}

}
Loading

0 comments on commit a92e67a

Please sign in to comment.