cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r833577 - /incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java
Date Fri, 06 Nov 2009 22:17:14 GMT
Author: jbellis
Date: Fri Nov  6 22:17:14 2009
New Revision: 833577

URL: http://svn.apache.org/viewvc?rev=833577&view=rev
Log:
make sortedtokens passably efficient
patch by jbellis; reviewed by Jaakko Laine for CASSANDRA-525

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java?rev=833577&r1=833576&r2=833577&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/TokenMetadata.java Fri
Nov  6 22:17:14 2009
@@ -46,6 +46,7 @@
     
     /* Use this lock for manipulating the token map */
     private final ReadWriteLock lock = new ReentrantReadWriteLock(true);
+    private List<Token> sortedTokens;
 
     public TokenMetadata()
     {
@@ -61,6 +62,14 @@
             bootstrapTokenMap = HashBiMap.create();
         this.tokenToEndPointMap = tokenToEndPointMap;
         this.bootstrapTokenMap = bootstrapTokenMap;
+        sortedTokens = sortTokens();
+    }
+
+    private List<Token> sortTokens()
+    {
+        List<Token> tokens = new ArrayList<Token>(tokenToEndPointMap.keySet());
+        Collections.sort(tokens);
+        return Collections.unmodifiableList(tokens);
     }
 
     public TokenMetadata(BiMap<Token, InetAddress> tokenEndpointMap)
@@ -84,6 +93,7 @@
             {
                 Map<Token, InetAddress> map = bootstrapping.contains(endpoint) ? bootstrapTokenMap
: tokenToEndPointMap;
                 map.put(t, endpoint);
+                sortedTokens = sortTokens();
             }
         }
         finally
@@ -122,6 +132,7 @@
             Map<Token, InetAddress> otherMap = bootstrapping.contains(endpoint) ? tokenToEndPointMap
: bootstrapTokenMap;
             map.put(token, endpoint);
             otherMap.remove(token);
+            sortedTokens = sortTokens();
         }
         finally
         {
@@ -240,18 +251,15 @@
 
     public List<Token> sortedTokens()
     {
-        List<Token> tokens;
         lock.readLock().lock();
         try
         {
-            tokens = new ArrayList<Token>(tokenToEndPointMap.keySet());
+            return sortedTokens;
         }
         finally
         {
             lock.readLock().unlock();
         }
-        Collections.sort(tokens);
-        return tokens;
     }
 
     public Token getPredecessor(Token token)



Mime
View raw message