You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2009/03/10 23:22:06 UTC

svn commit: r752284 - in /hadoop/hbase/trunk: CHANGES.txt src/java/org/apache/hadoop/hbase/client/HTable.java src/java/org/apache/hadoop/hbase/regionserver/HRegion.java

Author: stack
Date: Tue Mar 10 22:22:06 2009
New Revision: 752284

URL: http://svn.apache.org/viewvc?rev=752284&view=rev
Log:
HBASE-1252 Make atomic increment perform a binary increment

Modified:
    hadoop/hbase/trunk/CHANGES.txt
    hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/client/HTable.java
    hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java

Modified: hadoop/hbase/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/CHANGES.txt?rev=752284&r1=752283&r2=752284&view=diff
==============================================================================
--- hadoop/hbase/trunk/CHANGES.txt (original)
+++ hadoop/hbase/trunk/CHANGES.txt Tue Mar 10 22:22:06 2009
@@ -87,6 +87,8 @@
    HBASE-1240  Would be nice if RowResult could be comparable
                (Erik Holstad via Stack)
    HBASE-803   Atomic increment operations (Ryan Rawson and Jon Gray via Stack)
+   HBASE-1252  Make atomic increment perform a binary increment
+               (Jonathan Gray via Stack)
 
 Release 0.19.0 - 01/21/2009
   INCOMPATIBLE CHANGES

Modified: hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/client/HTable.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/client/HTable.java?rev=752284&r1=752283&r2=752284&view=diff
==============================================================================
--- hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/client/HTable.java (original)
+++ hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/client/HTable.java Tue Mar 10 22:22:06 2009
@@ -1481,7 +1481,7 @@
   }
   
   public long incrementColumnValue(final byte [] row, final byte [] column,
-      final int amount) throws IOException {
+      final long amount) throws IOException {
     return connection.getRegionServerWithRetries(
         new ServerCallable<Long>(connection, tableName, row) {
           public Long call() throws IOException {

Modified: hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java?rev=752284&r1=752283&r2=752284&view=diff
==============================================================================
--- hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java (original)
+++ hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java Tue Mar 10 22:22:06 2009
@@ -2598,7 +2598,6 @@
       HStoreKey hsk = new HStoreKey(row, column);
       long ts = System.currentTimeMillis();
       byte [] value = null;
-      long newval; // the new value.
 
       Store store = getStore(column);
 
@@ -2633,22 +2632,22 @@
       if (value == null) {
         // Doesn't exist
         LOG.debug("Creating new counter value for " + Bytes.toString(row) + "/"+ Bytes.toString(column));
-        newval = amount;
+        value = Bytes.toBytes(amount);
       } else {
-        newval = incrementBytes(value, amount);
+        value = incrementBytes(value, amount);
       }
 
       BatchUpdate b = new BatchUpdate(row, ts);
-      b.put(column, Bytes.toBytes(newval));
+      b.put(column, value);
       batchUpdate(b, lid, true);
-      return newval;
+      return Bytes.toLong(value);
     } finally {
       splitsAndClosesLock.readLock().unlock();
       releaseRowLock(lid);
     }
   }
 
-  private long incrementBytes(byte[] value, long amount) throws IOException {
+  private byte [] incrementBytes(byte[] value, long amount) throws IOException {
     // Hopefully this doesn't happen too often.
     if (value.length < Bytes.SIZEOF_LONG) {
       byte [] newvalue = new byte[Bytes.SIZEOF_LONG];
@@ -2657,8 +2656,22 @@
     } else if (value.length > Bytes.SIZEOF_LONG) {
       throw new DoNotRetryIOException("Increment Bytes - value too big: " + value.length);
     }
-    long v = Bytes.toLong(value);
-    v += amount;
-    return v;
+    return binaryIncrement(value, amount);
+  }
+  
+  private byte [] binaryIncrement(byte [] value, long amount) {
+    for(int i=0;i<value.length;i++) {
+      int cur = (int)(amount >> (8 * i)) % 256;
+      int val = (int)(value[value.length-i-1] & 0xff);
+      int total = cur + val;
+      if(total > 255) {
+        amount += ((long)256 << (8 * i));
+        total %= 256;
+      }
+      value[value.length-i-1] = (byte)total;
+      amount = (amount >> (8 * (i + 1))) << (8 * (i + 1));
+      if(amount == 0) return value;
+    }
+    return value;
   }
 }