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:19:36 UTC
svn commit: r752282 - in
/hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase:
client/HTable.java regionserver/HRegion.java
Author: stack
Date: Tue Mar 10 22:19:36 2009
New Revision: 752282
URL: http://svn.apache.org/viewvc?rev=752282&view=rev
Log:
HBASE-1252 Make atomic increment perform a binary increment (v2 from jgray)
Modified:
hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/client/HTable.java
hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java
Modified: hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/client/HTable.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/client/HTable.java?rev=752282&r1=752281&r2=752282&view=diff
==============================================================================
--- hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/client/HTable.java (original)
+++ hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/client/HTable.java Tue Mar 10 22:19:36 2009
@@ -1482,7 +1482,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/branches/0.19/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java?rev=752282&r1=752281&r2=752282&view=diff
==============================================================================
--- hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java (original)
+++ hadoop/hbase/branches/0.19/src/java/org/apache/hadoop/hbase/regionserver/HRegion.java Tue Mar 10 22:19:36 2009
@@ -2626,7 +2626,6 @@
HStoreKey hsk = new HStoreKey(row, column);
long ts = System.currentTimeMillis();
byte [] value = null;
- long newval; // the new value.
HStore store = getStore(column);
@@ -2661,22 +2660,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];
@@ -2685,8 +2684,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;
}
}