package org.argeo.api.uuid;
-import static java.lang.System.Logger.Level.DEBUG;
-import static java.lang.System.Logger.Level.WARNING;
-
-import java.lang.System.Logger;
import java.security.SecureRandom;
import java.time.Clock;
import java.time.Duration;
* and that such reallocation won't have to happen too often.
*/
public class ConcurrentTimeUuidState implements UuidFactory.TimeUuidState {
- private final static Logger logger = System.getLogger(ConcurrentTimeUuidState.class.getName());
+// private final static Logger logger = System.getLogger(ConcurrentTimeUuidState.class.getName());
/** The maximum possible value of the clocksequence. */
private final static int MAX_CLOCKSEQUENCE = 0x3F00;
}
assert holderToRemove != null;
- long oldClockSequence = holderToRemove.clockSequence;
+// long oldClockSequence = holderToRemove.clockSequence;
holderToRemove.clockSequence = -1;
activeHolders.remove(holderToRemove);
- if (logger.isLoggable(WARNING))
- logger.log(WARNING, "Removed " + holderToRemove + ", oldClockSequence=" + oldClockSequence);
+// if (logger.isLoggable(WARNING))
+// logger.log(WARNING, "Removed " + holderToRemove + ", oldClockSequence=" + oldClockSequence);
}
long newClockSequence = -1;
// TODO use an iterator to check the values
holder.setClockSequence(newClockSequence);
activeHolders.put(holder, newClockSequence);
- if (logger.isLoggable(DEBUG)) {
- String clockDesc = range >= 0 ? Long.toHexString(newClockSequence & 0x00FF)
- : Long.toHexString(newClockSequence | 0x8000);
- String rangeDesc = Long.toHexString(min | 0x8000) + "-" + Long.toHexString(max | 0x8000);
- logger.log(DEBUG, "New clocksequence " + clockDesc + " for thread " + Thread.currentThread().getId()
- + " (in range " + rangeDesc + ")");
- }
+// if (logger.isLoggable(DEBUG)) {
+// String clockDesc = range >= 0 ? Long.toHexString(newClockSequence & 0x00FF)
+// : Long.toHexString(newClockSequence | 0x8000);
+// String rangeDesc = Long.toHexString(min | 0x8000) + "-" + Long.toHexString(max | 0x8000);
+// logger.log(DEBUG, "New clocksequence " + clockDesc + " for thread " + Thread.currentThread().getId()
+// + " (in range " + rangeDesc + ")");
+// }
}
private synchronized int getRangeSize() {
package org.argeo.api.uuid;
-import static java.lang.System.Logger.Level.DEBUG;
-import static java.lang.System.Logger.Level.WARNING;
-
-import java.lang.System.Logger;
import java.security.DrbgParameters;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
* @see https://datatracker.ietf.org/doc/html/rfc4122
*/
public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory implements TypedUuidFactory {
- private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName());
+// private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName());
public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId) {
this(initialClockRange, nodeId, 0);
}
+ /** With a random node id. */
+ public ConcurrentUuidFactory(long initialClockRange) {
+ this(initialClockRange, NodeIdSupplier.randomNodeId());
+ }
+
public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId, int offset) {
Objects.requireNonNull(nodeId);
if (offset + 6 > nodeId.length)
DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes()));
} catch (NoSuchAlgorithmException e) {
try {
- logger.log(DEBUG, "DRBG secure random not found, using strong");
+// logger.log(DEBUG, "DRBG secure random not found, using strong");
secureRandom = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e1) {
- logger.log(WARNING, "No strong secure random was found, using default");
+// logger.log(WARNING, "No strong secure random was found, using default");
secureRandom = new SecureRandom();
}
+ } catch (java.lang.NoClassDefFoundError e) {// Android
+ secureRandom = new SecureRandom();
}
return secureRandom;
}
package org.argeo.api.uuid;
+import java.security.SecureRandom;
import java.util.function.Supplier;
/** A factory for node id base */
public interface NodeIdSupplier extends Supplier<Long> {
static long toNodeIdBase(byte[] node) {
assert node.length == 6;
- return UuidFactory.LEAST_SIG_RFC4122_VARIANT
- | (node[0] & 0xFFL) //
+ return UuidFactory.LEAST_SIG_RFC4122_VARIANT | (node[0] & 0xFFL) //
| ((node[1] & 0xFFL) << 8) //
| ((node[2] & 0xFFL) << 16) //
| ((node[3] & 0xFFL) << 24) //
return (nodeId[0] & 1) != 0;
}
+ static byte[] randomNodeId() {
+ SecureRandom random = new SecureRandom();
+ byte[] nodeId = new byte[6];
+ random.nextBytes(nodeId);
+ return nodeId;
+ }
}