cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eev...@apache.org
Subject [1/2] git commit: eliminate dependency on FBUtilities
Date Fri, 27 Jan 2012 19:53:56 GMT
Updated Branches:
  refs/heads/cassandra-1.0 2a1ec27e2 -> 89ed0b6ec


eliminate dependency on FBUtilities

Patch by eevans; reviewed by Dave Brosius for CASSANDRA-3665


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/89ed0b6e
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/89ed0b6e
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/89ed0b6e

Branch: refs/heads/cassandra-1.0
Commit: 89ed0b6ecdde8a7fd64f2139c02223ae4c040e8c
Parents: 32075c4
Author: Eric Evans <eevans@apache.org>
Authored: Thu Jan 26 16:04:11 2012 -0600
Committer: Eric Evans <eevans@apache.org>
Committed: Fri Jan 27 13:53:07 2012 -0600

----------------------------------------------------------------------
 src/java/org/apache/cassandra/utils/UUIDGen.java |   39 ++++++++++++++++-
 1 files changed, 37 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/89ed0b6e/src/java/org/apache/cassandra/utils/UUIDGen.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/UUIDGen.java b/src/java/org/apache/cassandra/utils/UUIDGen.java
index f1969fe..75828f0 100644
--- a/src/java/org/apache/cassandra/utils/UUIDGen.java
+++ b/src/java/org/apache/cassandra/utils/UUIDGen.java
@@ -23,6 +23,8 @@ package org.apache.cassandra.utils;
 import java.io.*;
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Random;
@@ -42,7 +44,31 @@ public class UUIDGen
     
     private long lastNanos;
     private final Map<InetAddress, Long> nodeCache = new HashMap<InetAddress, Long>();
-    
+
+    private static final ThreadLocal<MessageDigest> localMD5Digest = new ThreadLocal<MessageDigest>()
+    {
+        @Override
+        protected MessageDigest initialValue()
+        {
+            try
+            {
+                return MessageDigest.getInstance("MD5");
+            }
+            catch (NoSuchAlgorithmException nsae)
+            {
+                throw new RuntimeException("MD5 digest algorithm is not available", nsae);
+            }
+        }
+
+        @Override
+        public MessageDigest get()
+        {
+            MessageDigest digest = super.get();
+            digest.reset();
+            return digest;
+        }
+    };
+
     private UUIDGen()
     {
         // make sure someone didn't whack the clock by changing the order of instantiation.
@@ -195,7 +221,7 @@ public class UUIDGen
             return nodeCache.get(addr);
         
         // ideally, we'd use the MAC address, but java doesn't expose that.
-        byte[] hash = FBUtilities.hash(ByteBuffer.wrap(addr.toString().getBytes()));
+        byte[] hash = hash(addr.toString());
         long node = 0;
         for (int i = 0; i < Math.min(6, hash.length); i++)
             node |= (0x00000000000000ff & (long)hash[i]) << (5-i)*8;
@@ -205,6 +231,15 @@ public class UUIDGen
         
         return node;
     }
+
+    private static byte[] hash(String... data)
+    {
+        MessageDigest messageDigest = localMD5Digest.get();
+        for(String block : data)
+            messageDigest.update(block.getBytes());
+
+        return messageDigest.digest();
+    }
 }
 
 // for the curious, here is how I generated START_EPOCH


Mime
View raw message