Example #1
0
  /**
   * Generate a UID, globally unique. This method relies on the configured serverId for network
   * uniqueness.
   *
   * @return the generated UID.
   */
  public static Uid generateUid() {
    byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis());
    byte[] sequence = Encoder.intToBytes(getNextSequenceNumber());
    byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray();

    int uidLength = serverId.length + timestamp.length + sequence.length;
    byte[] uidArray = new byte[uidLength];

    // TODO: the server ID is encoded first but its size is variable and can change between runs.
    // It should be encoded last but that would make TX logs incompatible with older versions !
    System.arraycopy(serverId, 0, uidArray, 0, serverId.length);
    System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length);
    System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length);

    return new Uid(uidArray);
  }