hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurt...@apache.org
Subject svn commit: r1128978 - in /hbase/trunk: CHANGES.txt src/main/java/org/apache/hadoop/hbase/client/Get.java src/test/java/org/apache/hadoop/hbase/client/TestGet.java
Date Mon, 30 May 2011 01:10:02 GMT
Author: apurtell
Date: Mon May 30 01:10:01 2011
New Revision: 1128978

URL: http://svn.apache.org/viewvc?rev=1128978&view=rev
Log:
HBASE-3931 Allow adding attributes to Get

Added:
    hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestGet.java
Modified:
    hbase/trunk/CHANGES.txt
    hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Get.java

Modified: hbase/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hbase/trunk/CHANGES.txt?rev=1128978&r1=1128977&r2=1128978&view=diff
==============================================================================
--- hbase/trunk/CHANGES.txt (original)
+++ hbase/trunk/CHANGES.txt Mon May 30 01:10:01 2011
@@ -233,6 +233,7 @@ Release 0.91.0 - Unreleased
                one another (Harsh J Chouraria)
    HBASE-2937  Facilitate Timeouts In HBase Client (Karthick Sankarachary)
    HBASE-3921  Allow adding arbitrary blobs to Put (dhruba borthakur)
+   HBASE-3931  Allow adding attributes to Get
 
   TASKS
    HBASE-3559  Move report of split to master OFF the heartbeat channel

Modified: hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Get.java
URL: http://svn.apache.org/viewvc/hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Get.java?rev=1128978&r1=1128977&r2=1128978&view=diff
==============================================================================
--- hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Get.java (original)
+++ hbase/trunk/src/main/java/org/apache/hadoop/hbase/client/Get.java Mon May 30 01:10:01
2011
@@ -26,10 +26,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.Set;
@@ -61,7 +64,7 @@ import java.util.TreeSet;
  * To add a filter, execute {@link #setFilter(Filter) setFilter}.
  */
 public class Get implements Writable, Row, Comparable<Row> {
-  private static final byte GET_VERSION = (byte)1;
+  private static final byte GET_VERSION = (byte)2;
 
   private byte [] row = null;
   private long lockId = -1L;
@@ -71,6 +74,7 @@ public class Get implements Writable, Ro
   private TimeRange tr = new TimeRange();
   private Map<byte [], NavigableSet<byte []>> familyMap =
     new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
+  private Map<String, byte[]> attributes;
 
   /** Constructor for Writable.  DO NOT USE */
   public Get() {}
@@ -300,6 +304,54 @@ public class Get implements Writable, Ro
   }
 
   /**
+   * Sets arbitrary get'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 get'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
@@ -391,6 +443,15 @@ public class Get implements Writable, Ro
       }
       this.familyMap.put(family, set);
     }
+    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)
@@ -423,6 +484,15 @@ public class Get implements Writable, Ro
         }
       }
     }
+    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());
+      }
+    }    
   }
 
   @SuppressWarnings("unchecked")

Added: hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestGet.java
URL: http://svn.apache.org/viewvc/hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestGet.java?rev=1128978&view=auto
==============================================================================
--- hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestGet.java (added)
+++ hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestGet.java Mon May 30 01:10:01
2011
@@ -0,0 +1,106 @@
+/**
+ * Copyright 2011 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.client;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.junit.Assert;
+import org.junit.Test;
+
+// TODO: cover more test cases
+public class TestGet {
+  @Test
+  public void testAttributesSerialization() throws IOException {
+    Get get = new Get();
+    get.setAttribute("attribute1", Bytes.toBytes("value1"));
+    get.setAttribute("attribute2", Bytes.toBytes("value2"));
+    get.setAttribute("attribute3", Bytes.toBytes("value3"));
+
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutput out = new DataOutputStream(byteArrayOutputStream);
+    get.write(out);
+
+    Get get2 = new Get();
+    Assert.assertTrue(get2.getAttributesMap().isEmpty());
+
+    get2.readFields(new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
+
+    Assert.assertNull(get2.getAttribute("absent"));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
+    Assert.assertEquals(3, get2.getAttributesMap().size());
+  }
+
+  @Test
+  public void testGetAttributes() {
+    Get get = new Get();
+    Assert.assertTrue(get.getAttributesMap().isEmpty());
+    Assert.assertNull(get.getAttribute("absent"));
+
+    get.setAttribute("absent", null);
+    Assert.assertTrue(get.getAttributesMap().isEmpty());
+    Assert.assertNull(get.getAttribute("absent"));
+
+    // adding attribute
+    get.setAttribute("attribute1", Bytes.toBytes("value1"));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
+    Assert.assertEquals(1, get.getAttributesMap().size());
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
+
+    // overriding attribute value
+    get.setAttribute("attribute1", Bytes.toBytes("value12"));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
+    Assert.assertEquals(1, get.getAttributesMap().size());
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
+
+    // adding another attribute
+    get.setAttribute("attribute2", Bytes.toBytes("value2"));
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
+    Assert.assertEquals(2, get.getAttributesMap().size());
+    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
+
+    // removing attribute
+    get.setAttribute("attribute2", null);
+    Assert.assertNull(get.getAttribute("attribute2"));
+    Assert.assertEquals(1, get.getAttributesMap().size());
+    Assert.assertNull(get.getAttributesMap().get("attribute2"));
+
+    // removing non-existed attribute
+    get.setAttribute("attribute2", null);
+    Assert.assertNull(get.getAttribute("attribute2"));
+    Assert.assertEquals(1, get.getAttributesMap().size());
+    Assert.assertNull(get.getAttributesMap().get("attribute2"));
+
+    // removing another attribute
+    get.setAttribute("attribute1", null);
+    Assert.assertNull(get.getAttribute("attribute1"));
+    Assert.assertTrue(get.getAttributesMap().isEmpty());
+    Assert.assertNull(get.getAttributesMap().get("attribute1"));
+  }
+}



Mime
View raw message