/** * Creates new pseudo-random ID. * * @return Newly created pseudo-random ID. */ public static GridUuid randomUuid() { return new GridUuid(VM_ID, cntGen.incrementAndGet()); }
/** * @return Version represented as {@code GridUuid} */ public GridUuid asGridUuid() { return new GridUuid(new UUID(((long)topVer << 32) | nodeOrderDrId, globalTime), order); }
/** * Converts string into {@code GridUuid}. The String must be in the format generated * by {@link #toString() GridUuid.toString()} method. * * @param s String to convert to {@code GridUuid}. * @return {@code GridUuid} instance representing given string. */ public static GridUuid fromString(String s) { int firstDash = s.indexOf('-'); return new GridUuid( UUID.fromString(s.substring(firstDash + 1)), Long.valueOf(new StringBuilder(s.substring(0, firstDash)).reverse().toString(), 16) ); }
/** * Constructs new {@code GridUuid} based on global and local ID portions. * * @param id UUID instance. * @return Newly created pseudo-random ID. */ public static GridUuid fromUuid(UUID id) { A.notNull(id, "id"); return new GridUuid(id, cntGen.getAndIncrement()); }
/** * @param ptr Offheap address. * @return UUID. */ @Nullable public static GridUuid readGridUuid(long ptr) { if (UNSAFE.getBoolean(null, ptr++)) { long most = UNSAFE.getLong(ptr); ptr += 8; long least = UNSAFE.getLong(ptr); ptr += 8; UUID globalId = new UUID(most, least); long locId = UNSAFE.getLong(ptr); return new GridUuid(globalId, locId); } return null; }
/** * @param arr Array. * @param off Offset. * @return UUID. */ @Nullable public static GridUuid readGridUuid(byte[] arr, long off) { if (UNSAFE.getBoolean(arr, off++)) { long most = UNSAFE.getLong(arr, off); off += 8; long least = UNSAFE.getLong(arr, off); off += 8; UUID globalId = new UUID(most, least); long locId = UNSAFE.getLong(arr, off); return new GridUuid(globalId, locId); } return null; }
/** * @return {@link GridUuid} or special * {@link GridTcpCommunicationMessageAdapter#GRID_UUID_NOT_READ} * value if it was not fully read. */ public final GridUuid getGridUuid() { byte[] arr = getByteArray(); if (arr == BYTE_ARR_NOT_READ) return GRID_UUID_NOT_READ; else if (arr == null) return null; else { long most = UNSAFE.getLong(arr, BYTE_ARR_OFF); long least = UNSAFE.getLong(arr, BYTE_ARR_OFF + 8); long loc = UNSAFE.getLong(arr, BYTE_ARR_OFF + 16); return new GridUuid(new UUID(most, least), loc); } }
/** * Reads {@link GridUuid} from input stream. This method is meant to be used by * implementations of {@link Externalizable} interface. * * @param in Input stream. * @return Read UUID. * @throws IOException If read failed. */ @Nullable public static GridUuid readGridUuid(DataInput in) throws IOException { // If UUID is not null. if (!in.readBoolean()) { long most = in.readLong(); long least = in.readLong(); UUID globalId = GridUuidCache.onGridUuidRead(new UUID(most, least)); long locId = in.readLong(); return new GridUuid(globalId, locId); } return null; }
/** * Converts bytes to GridUuid. * * @param in Input byte array. * @param off Offset from which start reading. * @return GridUuid instance. */ public static GridUuid bytesToGridUuid(byte[] in, int off) { long most = U.bytesToLong(in, off); long least = U.bytesToLong(in, off + 8); long locId = U.bytesToLong(in, off + 16); return new GridUuid(GridUuidCache.onGridUuidRead(new UUID(most, least)), locId); }
public static final GridUuid GRID_UUID_NOT_READ = new GridUuid(new UUID(0, 0), 0);
/** * Generates next affinity key for local node based on current topology. If previous affinity key maps * on local node, return previous affinity key to prevent unnecessary file map growth. * * @param prevAffKey Affinity key of previous block. * @return Affinity key. */ public GridUuid nextAffinityKey(@Nullable GridUuid prevAffKey) { // Do not generate affinity key for non-affinity nodes. if (!isAffinityNode(dataCache.configuration())) return null; UUID nodeId = ggfsCtx.kernalContext().localNodeId(); if (prevAffKey != null && dataCache.affinity().mapKeyToNode(prevAffKey).isLocal()) return prevAffKey; while (true) { GridUuid key = new GridUuid(nodeId, affKeyGen.getAndIncrement()); if (dataCache.affinity().mapKeyToNode(key).isLocal()) return key; } }
/** * Generates next affinity key for local node based on current topology. If previous affinity key maps * on local node, return previous affinity key to prevent unnecessary file map growth. * * @param prevAffKey Affinity key of previous block. * @return Affinity key. */ public GridUuid nextAffinityKey(@Nullable GridUuid prevAffKey) { // Do not generate affinity key for non-affinity nodes. if (!isAffinityNode(dataCache.configuration())) return null; UUID nodeId = ggfsCtx.kernalContext().localNodeId(); if (prevAffKey != null && dataCache.affinity().mapKeyToNode(prevAffKey).isLocal()) return prevAffKey; while (true) { GridUuid key = new GridUuid(nodeId, affKeyGen.getAndIncrement()); if (dataCache.affinity().mapKeyToNode(key).isLocal()) return key; } }