private String generatePoolId() throws UnsupportedEncodingException, NoSuchAlgorithmException { synchronized (lock) { String newId = String.valueOf(startingPoint++) + "-" + serverId; MessageDigest md = MessageDigest.getInstance("SHA-1"); return StringUtils.encode(md.digest(newId.getBytes("UTF-8")), ENCODING); } }
@Test public void testId() { for (int i = 0; i < 10; i++) { Thread t = new Thread( new Runnable() { public void run() { synchronized (lockObject) { nrunning++; } try { for (int j = 0; j < 100; j++) { BigInteger id = getId(); assertFalse("Failed for " + id + " after " + j, hash.containsKey(id)); hash.put(id, id); } } finally { synchronized (lockObject) { nrunning--; } } } }); t.start(); } do { LOGGER.info("Running " + nrunning + " Hash Size is " + hash.size()); try { Thread.sleep(1000); } catch (InterruptedException e) { LOGGER.info(e.getMessage(), e); } } while (nrunning > 0); for (Entry<BigInteger, BigInteger> e : hash.entrySet()) { LOGGER.info( " Entry is " + e.getValue() + " " + StringUtils.encode(e.getValue().toByteArray(), StringUtils.URL_SAFE_ENCODING).trim() + " Epoch " + String.valueOf(epoch) + " Size " + String.valueOf(System.currentTimeMillis() - epoch)); } }
private String hash(String poolId) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance("SHA-1"); String encodedId = StringUtils.encode(md.digest(poolId.getBytes("UTF-8")), HASHENCODING); LOGGER.info("Hashing [{}] gave [{}] ", poolId, encodedId); return "/_p/" + encodedId.charAt(0) + "/" + encodedId.substring(1, 3) + "/" + encodedId.substring(3, 5) + "/" + encodedId.substring(5, 7) + "/" + poolId; }