Skip to content

Commit b0c0f8e

Browse files
committed
Version 5.0.0 #68
1 parent eaa9e89 commit b0c0f8e

File tree

12 files changed

+146
-114
lines changed

12 files changed

+146
-114
lines changed

CHANGELOG.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ All notable changes to this project will be documented in this file.
66

77
Nothing unreleased.
88

9-
## [5.0.0] - 2022-??-??
9+
## [5.0.0] - 2022-07-02
1010

11-
To be done.
11+
This version contains has breaking changes.
12+
13+
- Added Max UUID. #67
14+
- Added implementations for UUID v7. #67
15+
- Added minimum support for UUID v8. #67
16+
- Added `UriCodec.isUuidUri()`. #66
17+
- Added `UrnCodec` with `UrnCodec.isUuidUrn()`. #66
18+
- Changed `UuidCodec` implementations to explicitly and only throw `InvalidUuidException`. #65
19+
- Fixed out-of-order when trying to generate a huge batch of UUID v6 in a loop. #69
1220

1321
## [4.6.1] - 2022-03-19
1422

@@ -32,7 +40,7 @@ Add `Clock` parameter for COMB factories and time functions for tests. #60
3240

3341
## [4.4.0] - 2022-01-28
3442

35-
This version contains has breaking changes.
43+
This version contains breaking changes.
3644

3745
Add credits file for contributors. #58
3846

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Add these lines to your `pom.xml`:
3535
<dependency>
3636
<groupId>com.github.f4b6a3</groupId>
3737
<artifactId>uuid-creator</artifactId>
38-
<version>4.6.1</version>
38+
<version>5.0.0</version>
3939
</dependency>
4040
```
4141
See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/uuid-creator).

benchmark/src/main/java/benchmark/Throughput.java

+60-76
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
package benchmark;
33

4-
import java.security.SecureRandom;
5-
import java.util.Random;
6-
import java.util.SplittableRandom;
74
import java.util.UUID;
85
import java.util.concurrent.TimeUnit;
96

@@ -19,24 +16,11 @@
1916
import org.openjdk.jmh.annotations.Warmup;
2017

2118
import com.github.f4b6a3.uuid.UuidCreator;
22-
import com.github.f4b6a3.uuid.codec.base.Base16Codec;
23-
import com.github.f4b6a3.uuid.codec.base.Base32Codec;
24-
import com.github.f4b6a3.uuid.codec.base.Base58BtcCodec;
25-
import com.github.f4b6a3.uuid.codec.base.Base62Codec;
26-
import com.github.f4b6a3.uuid.codec.base.Base64UrlCodec;
27-
import com.github.f4b6a3.uuid.factory.UuidFactory;
28-
import com.github.f4b6a3.uuid.factory.function.ClockSeqFunction;
29-
import com.github.f4b6a3.uuid.factory.function.NodeIdFunction;
30-
import com.github.f4b6a3.uuid.factory.function.RandomFunction;
31-
import com.github.f4b6a3.uuid.factory.function.TimeFunction;
32-
import com.github.f4b6a3.uuid.factory.function.impl.DefaultClockSeqFunction;
33-
import com.github.f4b6a3.uuid.factory.function.impl.DefaultRandomFunction;
34-
import com.github.f4b6a3.uuid.factory.function.impl.DefaultTimeFunction;
35-
import com.github.f4b6a3.uuid.factory.function.impl.HashNodeIdFunction;
36-
import com.github.f4b6a3.uuid.factory.function.impl.MacNodeIdFunction;
37-
import com.github.f4b6a3.uuid.factory.rfc4122.RandomBasedFactory;
38-
import com.github.f4b6a3.uuid.factory.rfc4122.TimeBasedFactory;
39-
import com.github.f4b6a3.uuid.factory.rfc4122.TimeOrderedEpochFactory;
19+
//import com.github.f4b6a3.uuid.codec.base.Base16Codec;
20+
//import com.github.f4b6a3.uuid.codec.base.Base32Codec;
21+
//import com.github.f4b6a3.uuid.codec.base.Base58BtcCodec;
22+
//import com.github.f4b6a3.uuid.codec.base.Base62Codec;
23+
//import com.github.f4b6a3.uuid.codec.base.Base64UrlCodec;
4024

4125
@Fork(1)
4226
@Threads(1)
@@ -137,59 +121,59 @@ public UUID uuid_creator_12_time_ordered_epoch_plusn() {
137121

138122
/*********** UUID Codecs ************/
139123

140-
String base16 = Base16Codec.INSTANCE.encode(uuid);
141-
String base32 = Base32Codec.INSTANCE.encode(uuid);
142-
String base58 = Base58BtcCodec.INSTANCE.encode(uuid);
143-
String base62 = Base62Codec.INSTANCE.encode(uuid);
144-
String base64 = Base64UrlCodec.INSTANCE.encode(uuid);
145-
146-
@Benchmark
147-
public UUID uuid_codec_base_16_decode() {
148-
return Base16Codec.INSTANCE.decode(base16);
149-
}
150-
151-
@Benchmark
152-
public UUID uuid_codec_base_32_decode() {
153-
return Base32Codec.INSTANCE.decode(base32);
154-
}
155-
156-
@Benchmark
157-
public UUID uuid_codec_base_58_decode() {
158-
return Base58BtcCodec.INSTANCE.decode(base58);
159-
}
160-
161-
@Benchmark
162-
public UUID uuid_codec_base_62_decode() {
163-
return Base62Codec.INSTANCE.decode(base62);
164-
}
165-
166-
@Benchmark
167-
public UUID uuid_codec_base_64_decode() {
168-
return Base64UrlCodec.INSTANCE.decode(base64);
169-
}
170-
171-
@Benchmark
172-
public String uuid_codec_base_16_encode() {
173-
return Base16Codec.INSTANCE.encode(uuid);
174-
}
175-
176-
@Benchmark
177-
public String uuid_codec_base_32_encode() {
178-
return Base32Codec.INSTANCE.encode(uuid);
179-
}
180-
181-
@Benchmark
182-
public String uuid_codec_base_58_encode() {
183-
return Base58BtcCodec.INSTANCE.encode(uuid);
184-
}
185-
186-
@Benchmark
187-
public String uuid_codec_base_62_encode() {
188-
return Base62Codec.INSTANCE.encode(uuid);
189-
}
190-
191-
@Benchmark
192-
public String uuid_codec_base_64_encode() {
193-
return Base64UrlCodec.INSTANCE.encode(uuid);
194-
}
124+
// String base16 = Base16Codec.INSTANCE.encode(uuid);
125+
// String base32 = Base32Codec.INSTANCE.encode(uuid);
126+
// String base58 = Base58BtcCodec.INSTANCE.encode(uuid);
127+
// String base62 = Base62Codec.INSTANCE.encode(uuid);
128+
// String base64 = Base64UrlCodec.INSTANCE.encode(uuid);
129+
//
130+
// @Benchmark
131+
// public UUID uuid_codec_base_16_decode() {
132+
// return Base16Codec.INSTANCE.decode(base16);
133+
// }
134+
//
135+
// @Benchmark
136+
// public UUID uuid_codec_base_32_decode() {
137+
// return Base32Codec.INSTANCE.decode(base32);
138+
// }
139+
//
140+
// @Benchmark
141+
// public UUID uuid_codec_base_58_decode() {
142+
// return Base58BtcCodec.INSTANCE.decode(base58);
143+
// }
144+
//
145+
// @Benchmark
146+
// public UUID uuid_codec_base_62_decode() {
147+
// return Base62Codec.INSTANCE.decode(base62);
148+
// }
149+
//
150+
// @Benchmark
151+
// public UUID uuid_codec_base_64_decode() {
152+
// return Base64UrlCodec.INSTANCE.decode(base64);
153+
// }
154+
//
155+
// @Benchmark
156+
// public String uuid_codec_base_16_encode() {
157+
// return Base16Codec.INSTANCE.encode(uuid);
158+
// }
159+
//
160+
// @Benchmark
161+
// public String uuid_codec_base_32_encode() {
162+
// return Base32Codec.INSTANCE.encode(uuid);
163+
// }
164+
//
165+
// @Benchmark
166+
// public String uuid_codec_base_58_encode() {
167+
// return Base58BtcCodec.INSTANCE.encode(uuid);
168+
// }
169+
//
170+
// @Benchmark
171+
// public String uuid_codec_base_62_encode() {
172+
// return Base62Codec.INSTANCE.encode(uuid);
173+
// }
174+
//
175+
// @Benchmark
176+
// public String uuid_codec_base_64_encode() {
177+
// return Base64UrlCodec.INSTANCE.encode(uuid);
178+
// }
195179
}

src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.github.f4b6a3.uuid.factory;
2626

27+
import java.security.SecureRandom;
2728
import java.util.Random;
2829

2930
import com.github.f4b6a3.uuid.enums.UuidVersion;
@@ -62,15 +63,25 @@ protected static RandomFunction newRandomFunction(Random random) {
6263
return new DefaultRandomFunction();
6364
}
6465

65-
return new RandomFunction() {
66-
private final Random entropy = random;
67-
68-
@Override
69-
public synchronized byte[] apply(final int length) {
66+
if (random instanceof SecureRandom) {
67+
final Random entropy = random;
68+
return (final int length) -> {
7069
final byte[] bytes = new byte[length];
7170
entropy.nextBytes(bytes);
7271
return bytes;
73-
}
74-
};
72+
};
73+
} else {
74+
// a synchronized function
75+
return new RandomFunction() {
76+
private final Random entropy = random;
77+
78+
@Override
79+
public synchronized byte[] apply(final int length) {
80+
final byte[] bytes = new byte[length];
81+
entropy.nextBytes(bytes);
82+
return bytes;
83+
}
84+
};
85+
}
7586
}
7687
}

src/main/java/com/github/f4b6a3/uuid/factory/AbstTimeBasedFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public abstract class AbstTimeBasedFactory extends UuidFactory implements NoArgs
5252
private static final String NODE_HASH = "hash";
5353
private static final String NODE_RANDOM = "random";
5454

55-
private static final long EPOCH_TIMESTAMP = TimeFunction.toTimestamp(UuidTime.EPOCH_GREG);
55+
private static final long EPOCH_TIMESTAMP = TimeFunction.toUnixTimestamp(UuidTime.EPOCH_GREG);
5656

5757
protected AbstTimeBasedFactory(UuidVersion version, Builder<?> builder) {
5858
super(version);
@@ -155,13 +155,13 @@ protected AbstTimeBasedFactory(UuidVersion version, Builder<?> builder) {
155155
public synchronized UUID create() {
156156

157157
// (3a) get the timestamp
158-
final long timestamp = this.timeFunction.getAsLong() - EPOCH_TIMESTAMP;
158+
final long timestamp = TimeFunction.toExpectedRange(this.timeFunction.getAsLong() - EPOCH_TIMESTAMP);
159159

160160
// (4a)(5a) get the node identifier
161-
final long nodeIdentifier = this.nodeidFunction.getAsLong();
161+
final long nodeIdentifier = NodeIdFunction.toExpectedRange(this.nodeidFunction.getAsLong());
162162

163163
// (5a)(6a) get the sequence value
164-
final long clockSequence = this.clockseqFunction.applyAsLong(timestamp);
164+
final long clockSequence = ClockSeqFunction.toExpectedRange(this.clockseqFunction.applyAsLong(timestamp));
165165

166166
// (9a) format the most significant bits
167167
final long msb = this.formatMostSignificantBits(timestamp);
@@ -347,7 +347,7 @@ public Builder<T> withClockSeqFunction(ClockSeqFunction clockseqFunction) {
347347
}
348348

349349
public Builder<T> withInstant(Instant instant) {
350-
final long timestamp = TimeFunction.toTimestamp(instant);
350+
final long timestamp = TimeFunction.toUnixTimestamp(instant);
351351
this.timeFunction = () -> timestamp;
352352
return this;
353353
}

src/main/java/com/github/f4b6a3/uuid/factory/function/ClockSeqFunction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static long getRandom() {
7272
* @param clockseq the clock sequence
7373
* @return a clock sequence in the range 0 to 16383 (2^14-1).
7474
*/
75-
public static long toExpectedRange(long clockseq) {
75+
public static long toExpectedRange(final long clockseq) {
7676
return clockseq & 0x0000000000003fffL;
7777
}
7878

src/main/java/com/github/f4b6a3/uuid/factory/function/NodeIdFunction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static long getMulticastRandom() {
8181
* @param nodeid the node identifier
8282
* @return a node identifier in the range 0 to 2^48-1.
8383
*/
84-
public static long toExpectedRange(long nodeid) {
84+
public static long toExpectedRange(final long nodeid) {
8585
return nodeid & 0x0000_ffffffffffffL;
8686
}
8787

src/main/java/com/github/f4b6a3/uuid/factory/function/TimeFunction.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@
3333
/**
3434
* It must return a number of 100-nanoseconds since 1970-01-01 (Unix epoch).
3535
*
36-
* Use {@link TimeFunction#toTimestamp(Instant)} to convert the output to
37-
* 100-nanoseconds since 1970-01-01 (Unix epoch). It also sets the output within
38-
* the range 0 to 2^60-1.
39-
*
4036
* The {@link AbstTimeBasedFactory} will convert the output time to Gregorian
4137
* epoch (1582-10-15) for you.
4238
*
4339
* Example:
4440
*
4541
* <pre>
4642
* // A `TimeFunction` that returns the `Instant.now()` as a number of 100-nanoseconds
47-
* TimeFunction f = () -> TimeFunction.toTimestamp(Instant.now()); // for JDK 9+
43+
* TimeFunction f = () -> TimeFunction.toUnixTimestamp(Instant.now()); // for JDK 9+
4844
* </pre>
4945
*
5046
* In JDK 8, {@link Instant#now()} has millisecond precision, in spite of
@@ -65,7 +61,20 @@ public interface TimeFunction extends LongSupplier {
6561
* @param instant an instant
6662
* @return a number of 100-nanoseconds since 1970-01-01 (Unix epoch)
6763
*/
68-
public static long toTimestamp(final Instant instant) {
64+
public static long toUnixTimestamp(final Instant instant) {
6965
return UuidTime.toUnixTimestamp(instant);
7066
}
67+
68+
/**
69+
* This method clears the unnecessary leading bits so that the resulting number
70+
* is in the range 0 to 2^60-1.
71+
*
72+
* The result is equivalent to {@code n % 2^60}.
73+
*
74+
* @param timestamp the node identifier
75+
* @return a timestamp in the range 0 to 2^60-1.
76+
*/
77+
public static long toExpectedRange(final long timestamp) {
78+
return timestamp & 0x0_fffffffffffffffL;
79+
}
7180
}

0 commit comments

Comments
 (0)