Skip to content

Commit

Permalink
Domain specific collision checker for id generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sarb1208 authored and santanusinha committed May 21, 2024
1 parent 8727e11 commit daaec42
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 51 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<guava.version>31.0.1-jre</guava.version>
<guava.version>33.2.0-jre</guava.version>
<curator.version>5.5.0</curator.version>
<slf4j.version>1.7.32</slf4j.version>

Expand All @@ -92,7 +92,7 @@
<lombok.version>1.18.22</lombok.version>
<annotations.version>3.0.1u2</annotations.version>

<junit.jupiter.version>5.8.2</junit.jupiter.version>
<junit.version>5.8.2</junit.version>
<awaitility.version>4.1.1</awaitility.version>
<guava-retrying.version>2.0.0</guava-retrying.version>

Expand All @@ -108,7 +108,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.jupiter.version}</version>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"iterations" : 4,
"threads" : 1,
"forks" : 3,
"mean_ops" : 644166.1778513143
"mean_ops" : 738950.7655028169
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"iterations" : 4,
"threads" : 1,
"forks" : 3,
"mean_ops" : 502644.4941310657
"mean_ops" : 573183.379799968
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package io.appform.ranger.discovery.bundle.id;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.util.BitSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -33,15 +35,22 @@ public class CollisionChecker {

private final Lock dataLock = new ReentrantLock();

private final TimeUnit resolution;

public CollisionChecker() {
//Nothing to do here
this(TimeUnit.MILLISECONDS);
}

public CollisionChecker(@NonNull TimeUnit resolution) {
this.resolution = resolution;
}

public boolean check(long time, int location) {
public boolean check(long timeInMillis, int location) {
dataLock.lock();
try {
if (currentInstant != time) {
currentInstant = time;
long resolvedTime = resolution.convert(timeInMillis, TimeUnit.MILLISECONDS);;
if (currentInstant != resolvedTime) {
currentInstant = resolvedTime;
bitSet.clear();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.appform.ranger.discovery.bundle.id;


import io.appform.ranger.discovery.bundle.id.constraints.IdValidationConstraint;
import io.appform.ranger.discovery.bundle.id.formatter.DefaultIdFormatter;
import io.appform.ranger.discovery.bundle.id.formatter.IdFormatter;
import io.appform.ranger.discovery.bundle.id.formatter.IdFormatters;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Getter
public class Domain {
public static final String DEFAULT_DOMAIN_NAME = "__DEFAULT_DOMAIN__";
public static final Domain DEFAULT = new Domain(DEFAULT_DOMAIN_NAME,
List.of(),
new DefaultIdFormatter(),
TimeUnit.MILLISECONDS);

private final String domain;
private final List<IdValidationConstraint> constraints;
private final IdFormatter idFormatter;
private final CollisionChecker collisionChecker;


@Builder
public Domain(@NonNull String domain,
@NonNull List<IdValidationConstraint> constraints,
IdFormatter idFormatter,
TimeUnit resolution) {
this.domain = domain;
this.constraints = constraints;
this.idFormatter = Objects.requireNonNullElse(idFormatter, IdFormatters.original());
this.collisionChecker = new CollisionChecker(Objects.requireNonNullElse(resolution, TimeUnit.MILLISECONDS));
}

}
Loading

0 comments on commit daaec42

Please sign in to comment.