Author: stack
Date: Wed May 25 20:41:20 2011
New Revision: 1127676

URL: http://svn.apache.org/viewvc?rev=1127676&view=rev
Log:
HBASE-3811 Allow adding attributes to Scan

Modified:
    hbase/trunk/CHANGES.txt
    hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Scan.java

Modified: hbase/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hbase/trunk/CHANGES.txt?rev=1127676&r1=1127675&r2=1127676&view=diff
==============================================================================
--- hbase/trunk/CHANGES.txt (original)
+++ hbase/trunk/CHANGES.txt Wed May 25 20:41:20 2011
@@ -228,6 +228,7 @@ Release 0.91.0 - Unreleased
    HBASE-3880  Make mapper function in ImportTSV plug-able (Bill Graham)
    HBASE-2938  HBASE-2938 Add Thread-Local Behavior To HTable Pool
                (Karthick Sankarachary)
+   HBASE-3811  Allow adding attributes to Scan (Alex Baranau)
 
   TASKS
    HBASE-3559  Move report of split to master OFF the heartbeat channel

Modified: hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Scan.java
URL: 
http://svn.apache.org/viewvc/hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Scan.java?rev=1127676&r1=1127675&r2=1127676&view=diff
==============================================================================
--- hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Scan.java 
(original)
+++ hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Scan.java Wed May 
25 20:41:20 2011
@@ -29,10 +29,13 @@ import org.apache.hadoop.hbase.io.TimeRa
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.io.Writable;
 import org.apache.hadoop.io.WritableFactories;
+import org.apache.hadoop.io.WritableUtils;
 
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.NavigableSet;
 import java.util.TreeMap;
@@ -79,11 +82,13 @@ import java.util.TreeSet;
  * execute {@link #setCacheBlocks(boolean)}.
  */
 public class Scan implements Writable {
-  private static final byte SCAN_VERSION = (byte)1;
+  private static final byte SCAN_VERSION = (byte)2;
   private byte [] startRow = HConstants.EMPTY_START_ROW;
   private byte [] stopRow  = HConstants.EMPTY_END_ROW;
   private int maxVersions = 1;
   private int batch = -1;
+  private Map<String, byte[]> attributes;
+
   /*
    * -1 means no caching
    */
@@ -440,6 +445,55 @@ public class Scan implements Writable {
   }
 
   /**
+   * Sets arbitrary scan's attribute.
+   * In case value = null attribute is removed from the attributes map.
+   * @param name attribute name
+   * @param value attribute value
+   */
+  public void setAttribute(String name, byte[] value) {
+    if (attributes == null && value == null) {
+      return;
+    }
+
+    if (attributes == null) {
+      attributes = new HashMap<String, byte[]>();
+    }
+
+    if (value == null) {
+      attributes.remove(name);
+      if (attributes.isEmpty()) {
+        this.attributes = null;
+      }
+    } else {
+      attributes.put(name, value);
+    }
+  }
+
+  /**
+   * Gets scan's attribute
+   * @param name attribute name
+   * @return attribute value if attribute is set, <tt>null</tt> otherwise
+   */
+  public byte[] getAttribute(String name) {
+    if (attributes == null) {
+      return null;
+    }
+
+    return attributes.get(name);
+  }
+
+  /**
+   * Gets all scan's attributes
+   * @return unmodifiable map of all attributes
+   */
+  public Map<String, byte[]> getAttributesMap() {
+    if (attributes == null) {
+      return Collections.emptyMap();
+    }
+    return Collections.unmodifiableMap(attributes);
+  }
+
+  /**
    * @return String
    */
   @Override
@@ -539,6 +593,18 @@ public class Scan implements Writable {
       }
       this.familyMap.put(family, set);
     }
+
+    if (version > 1) {
+      int numAttributes = in.readInt();
+      if (numAttributes > 0) {
+        this.attributes = new HashMap<String, byte[]>();
+        for(int i=0; i<numAttributes; i++) {
+          String name = WritableUtils.readString(in);
+          byte[] value = Bytes.readByteArray(in);
+          this.attributes.put(name, value);
+        }
+      }
+    }
   }
 
   public void write(final DataOutput out)
@@ -571,6 +637,16 @@ public class Scan implements Writable {
         out.writeInt(0);
       }
     }
+
+    if (this.attributes == null) {
+      out.writeInt(0);
+    } else {
+      out.writeInt(this.attributes.size());
+      for (Map.Entry<String, byte[]> attr : this.attributes.entrySet()) {
+        WritableUtils.writeString(out, attr.getKey());
+        Bytes.writeByteArray(out, attr.getValue());
+      }
+    }
   }
 
    /**


Reply via email to