Hello all,

This concerns the current SVN trunk.

The javadoc in IDMigrator:

  /**
   * @return the top 8 bytes of the MD5 hash of the bytes of the given
{...@link String}'s UTF-8 encoding as a long.
   *  The reverse mapping is also stored.
   * @throws TasteException if an error occurs while storing the mapping
   */
  long toLongID(String stringID);

However in AbstractIDMigrator, the method is implemented without a call to
storeMapping().

  @Override
  public long toLongID(String stringID) {
    return hash(stringID);
  }

  /**
   * @return most significant 8 bytes of the MD5 hash of the string, as a
long
   */
  protected final long hash(String value) {
    byte[] md5hash;
    synchronized (md5Digest) {
      md5hash = md5Digest.digest(value.getBytes(UTF8_CHARSET));
      md5Digest.reset();
    }
    long hash = 0L;
    for (int i = 0; i < 8; i++) {
      hash = (hash << 8) | (md5hash[i] & 0x00000000000000FFL);
    }
    return hash;
  }


So, it is possible to migrate a String ID to a long but not possible to do a
reverse lookup.  This affects MemoryIDMigrator and would affect other
classes that extend AbstractIDMigrator but do not override toLongID().

For example:

        IDMigrator idMigratorTest = new MemoryIDMigrator();
        long migratedId = idMigratorTest.toLongID("someStringId");
        String stringId = idMigratorTest.toStringID(migratedId);
        System.out.println("migratedId=" + migratedId + "; stringId="
                + stringId);

Will output:

migratedId=___some_long_value___; stringId=null

Is this a bug or am I just not correctly using the class?

- Y

Reply via email to