commit efa92243f12934e70f0e2a928fbb1fb22066a9a5 Author: Alix von Schirp Date: Wed May 8 22:44:23 2024 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a879ee7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/ +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ade214c --- /dev/null +++ b/pom.xml @@ -0,0 +1,166 @@ + + + 4.0.0 + + space.b00tload.services + SnowflakeService + 1.0-SNAPSHOT + + SnowflakeService + A tool/microservice to centrally generate snowflake IDs. Can be run distributed. + 2024 + https://github.com/B00tLoad/SnowflakeService + + + + B00tLoad_ + Alix von Schirp + alix.von-schirp@bootmedia.de + + developer + + Europe/Berlin + + @b00tload_ + @b00tload_ + order-of-gathering.de + @b00tload.space + she/they + + + + + + GitHub + https://github.com/B00tLoad/SnowflakeService/issues + + + + + GNU GPL v3.0 + https://github.com/B00tLoad/SnowflakeService/blob/master/LICENSE + + + + + scm:git:git://github.com/B00tLoad/SnowflakeService.git + scm:git:ssh://github.com:B00tLoad/SnowflakeService.git + https://github.com/B00tLoad/SnowflakeService + + + + 21 + 21 + UTF-8 + + + + + space.b00tload.utils + ConfigurationUtilities + 1.0.0 + + + org.slf4j + slf4j-api + 2.0.13 + + + + + + + org.sonatype.central + central-publishing-maven-plugin + 0.4.0 + true + + central + true + + + + org.apache.maven.plugins + maven-source-plugin + 3.3.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.6.3 + + + + example + a + Usage example: + + + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 3.2.4 + + + sign-artifacts + verify + + sign + + + ${gpg.keyname} + ${gpg.keyname} + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.7.1 + + + package + + single + + + + + WatchlistHelper + + jar-with-dependencies + + + + space.b00tload.services.snowflake.SnowflakeService + true + true + + + + + + + + \ No newline at end of file diff --git a/src/main/java/space/b00tload/services/snowflake/SnowflakeIDGenerator.java b/src/main/java/space/b00tload/services/snowflake/SnowflakeIDGenerator.java new file mode 100644 index 0000000..ef784d6 --- /dev/null +++ b/src/main/java/space/b00tload/services/snowflake/SnowflakeIDGenerator.java @@ -0,0 +1,73 @@ +package space.b00tload.services.snowflake; + +import org.slf4j.LoggerFactory; + +public class SnowflakeIDGenerator { + + // Constants + private static final long EPOCH = 1704067200000L; // January 1, 2024, 00:00:00 UTC + private static final long MACHINE_ID_BITS = 10L; + private static final long SEQUENCE_BITS = 12L; + + private static final long MAX_MACHINE_ID = (1L << MACHINE_ID_BITS) - 1; + private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1; + + private static final long MACHINE_ID_SHIFT = SEQUENCE_BITS; + private static final long TIMESTAMP_SHIFT = SEQUENCE_BITS + MACHINE_ID_BITS; + + // State + private final long machineId; + private long sequence = 0L; + private long lastTimestamp = -1L; + + // Constructor + public SnowflakeIDGenerator(long machineId) { + if (machineId < 0 || machineId > MAX_MACHINE_ID) { + throw new IllegalArgumentException("Machine ID must be between 0 and " + MAX_MACHINE_ID); + } + this.machineId = machineId; + } + + // Method to generate a new ID + public synchronized long generateID() { + long currentTimestamp = currentTimeMillis(); + + if (currentTimestamp < lastTimestamp) { + LoggerFactory.getLogger(SnowflakeIDGenerator.class).warn("Clock is moving backwards. Waiting to generate ID."); + while (currentTimestamp < lastTimestamp) { + currentTimestamp = currentTimeMillis(); + } + } + + if (currentTimestamp == lastTimestamp) { + sequence = (sequence + 1) & MAX_SEQUENCE; + if (sequence == 0) { + currentTimestamp = waitForNextTimestamp(currentTimestamp); + } + } else { + sequence = 0; + } + + lastTimestamp = currentTimestamp; + + return ((currentTimestamp - EPOCH) << TIMESTAMP_SHIFT) + | (machineId << MACHINE_ID_SHIFT) + | sequence; + } + + // Get the current timestamp in milliseconds + private long currentTimeMillis() { + return System.currentTimeMillis(); + } + + // Wait for the next timestamp + private long waitForNextTimestamp(long currentTimestamp) { + long nextTimestamp = currentTimeMillis(); + while (nextTimestamp <= currentTimestamp) { + nextTimestamp = currentTimeMillis(); + } + return nextTimestamp; + } +} + + diff --git a/src/main/java/space/b00tload/services/snowflake/SnowflakeService.java b/src/main/java/space/b00tload/services/snowflake/SnowflakeService.java new file mode 100644 index 0000000..10cd196 --- /dev/null +++ b/src/main/java/space/b00tload/services/snowflake/SnowflakeService.java @@ -0,0 +1,9 @@ +package space.b00tload.services.snowflake; + +public class SnowflakeService { + + public static void main(String[] args) { + + } + +}