You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by ct...@apache.org on 2020/08/06 06:55:53 UTC

[accumulo] branch main updated: Fix #1104 Update javadocs and unit test for Key constructors (#1105)

This is an automated email from the ASF dual-hosted git repository.

ctubbsii pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 1f5ffd2  Fix #1104 Update javadocs and unit test for Key constructors (#1105)
1f5ffd2 is described below

commit 1f5ffd2e99704ddba81eae7c7558f15139bb3b85
Author: Don Resnik <do...@vistronix.com>
AuthorDate: Thu Aug 6 02:55:43 2020 -0400

    Fix #1104 Update javadocs and unit test for Key constructors (#1105)
    
    * Update javadoc comments to document copy behavior
    * Update javadoc comments with <p> per code review recommendation
    
    * Add missing `@Test` annotation to KeyTest
    * Add method to consolidate unit tests for byte[] and Text constructors
    * Clean up tests and add place holder byte[] for tests that do not populate all values
---
 .../java/org/apache/accumulo/core/data/Key.java    | 103 ++++++++++++++-----
 .../org/apache/accumulo/core/data/KeyTest.java     | 113 +++++++++++++++++++++
 2 files changed, 189 insertions(+), 27 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/data/Key.java b/core/src/main/java/org/apache/accumulo/core/data/Key.java
index ee07f53..c0246d7 100644
--- a/core/src/main/java/org/apache/accumulo/core/data/Key.java
+++ b/core/src/main/java/org/apache/accumulo/core/data/Key.java
@@ -39,7 +39,7 @@ import org.apache.hadoop.io.WritableUtils;
 /**
  * This is the Key used to store and access individual values in Accumulo. A Key is a tuple composed
  * of a row, column family, column qualifier, column visibility, timestamp, and delete marker.
- *
+ * <p>
  * Keys are comparable and therefore have a sorted order defined by {@link #compareTo(Key)}.
  */
 public class Key implements WritableComparable<Key>, Cloneable {
@@ -121,7 +121,12 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key with the specified row, empty column family, empty column qualifier, empty column
-   * visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false.
+   * visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false. This constructor creates
+   * a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @param row
    *          row ID
@@ -135,7 +140,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, empty column family, empty column qualifier, empty column
    * visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false. This constructor creates
-   * a copy of row. If you don't want to create a copy of row, you should call
+   * a copy of row.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -151,7 +158,12 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key with the specified row, empty column family, empty column qualifier, empty column
-   * visibility, the specified timestamp, and delete marker false.
+   * visibility, the specified timestamp, and delete marker false. This constructor creates a copy
+   * of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @param row
    *          row ID
@@ -167,7 +179,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, empty column family, empty column qualifier, empty column
    * visibility, the specified timestamp, and delete marker false. This constructor creates a copy
-   * of row. If you don't want to create a copy, you should call
+   * of row.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -185,7 +199,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key. The delete marker defaults to false. This constructor creates a copy of each
-   * specified array. If you don't want to create a copy of the arrays, you should call
+   * specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -223,10 +239,7 @@ public class Key implements WritableComparable<Key>, Cloneable {
   }
 
   /**
-   * Creates a key. The delete marker defaults to false. This constructor creates a copy of each
-   * specified array. If you don't want to create a copy of the arrays, you should call
-   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
-   * instead.
+   * Creates a key.
    *
    * @param row
    *          bytes containing row ID
@@ -267,7 +280,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key. The delete marker defaults to false. This constructor creates a copy of each
-   * specified array. If you don't want to create a copy of the arrays, you should call
+   * specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -289,8 +304,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   }
 
   /**
-   * Creates a key. This constructor creates a copy of each specified arrays. If you don't want to
-   * create a copy, you should call
+   * Creates a key. This constructor creates a copy of each specified arrays.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -338,7 +354,12 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key with the specified row, the specified column family, empty column qualifier,
-   * empty column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false.
+   * empty column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false. This
+   * constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -350,8 +371,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, empty column qualifier,
    * empty column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false. This
-   * constructor creates a copy of each specified array. If you don't want to create a copy of the
-   * arrays, you should call
+   * constructor creates a copy of each specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -366,6 +388,11 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, empty column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false.
+   * This constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -377,8 +404,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, empty column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker false.
-   * This constructor creates a copy of each specified array. If you don't want to create a copy of
-   * the arrays, you should call
+   * This constructor creates a copy of each specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -393,7 +421,11 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, the specified column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker
-   * false.
+   * false. This constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -405,8 +437,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, the specified column visibility, timestamp {@link Long#MAX_VALUE}, and delete marker
-   * false. This constructor creates a copy of each specified array. If you don't want to create a
-   * copy of the arrays, you should call
+   * false. This constructor creates a copy of each specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -420,7 +453,12 @@ public class Key implements WritableComparable<Key>, Cloneable {
 
   /**
    * Creates a key with the specified row, the specified column family, the specified column
-   * qualifier, empty column visibility, the specified timestamp, and delete marker false.
+   * qualifier, empty column visibility, the specified timestamp, and delete marker false. This
+   * constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -432,8 +470,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, empty column visibility, the specified timestamp, and delete marker false. This
-   * constructor creates a copy of each specified array. If you don't want to create a copy of the
-   * arrays, you should call
+   * constructor creates a copy of each specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -448,6 +487,11 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, the specified column visibility, the specified timestamp, and delete marker false.
+   * This constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -459,6 +503,11 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, the specified column visibility, the specified timestamp, and delete marker false.
+   * This constructor creates a copy of row.
+   * <p>
+   * To avoid copying, use
+   * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
+   * instead.
    *
    * @see #builder()
    */
@@ -471,8 +520,9 @@ public class Key implements WritableComparable<Key>, Cloneable {
   /**
    * Creates a key with the specified row, the specified column family, the specified column
    * qualifier, the specified column visibility, the specified timestamp, and delete marker false.
-   * This constructor creates a copy of each specified array. If you don't want to create a copy of
-   * the arrays, you should call
+   * This constructor creates a copy of each specified array.
+   * <p>
+   * To avoid copying, use
    * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)}
    * instead.
    *
@@ -865,7 +915,6 @@ public class Key implements WritableComparable<Key>, Cloneable {
     colVisibility = k.colVisibility;
     timestamp = k.timestamp;
     deleted = k.deleted;
-
   }
 
   @Override
diff --git a/core/src/test/java/org/apache/accumulo/core/data/KeyTest.java b/core/src/test/java/org/apache/accumulo/core/data/KeyTest.java
index 5bf6b4b..ccb7789 100644
--- a/core/src/test/java/org/apache/accumulo/core/data/KeyTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/data/KeyTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
@@ -83,6 +84,117 @@ public class KeyTest {
   }
 
   @Test
+  public void testCopyDataWithByteArrayConstructors() {
+    byte[] row = "r".getBytes();
+    byte[] cf = "cf".getBytes();
+    byte[] cq = "cq".getBytes();
+    byte[] cv = "cv".getBytes();
+    byte[] empty = "".getBytes();
+
+    Key kRow = new Key(row);
+    Key kRowcolFam = new Key(row, cf);
+    Key kRowcolFamColQual = new Key(row, cf, cq);
+    Key kRowcolFamColQualColVis = new Key(row, cf, cq, cv);
+    Key kRowcolFamColQualColVisTimeStamp = new Key(row, cf, cq, cv, 5L);
+
+    // test row constructor
+    assertNotSameByteArray(kRow, row, empty, empty, empty);
+
+    // test row, column family constructor
+    assertNotSameByteArray(kRowcolFam, row, cf, empty, empty);
+
+    // test row, column family, column qualifier constructor
+    assertNotSameByteArray(kRowcolFamColQual, row, cf, cq, empty);
+
+    // test row, column family, column qualifier, column visibility constructor
+    assertNotSameByteArray(kRowcolFamColQualColVis, row, cf, cq, cv);
+
+    // test row, column family, column qualifier, column visibility, timestamp constructor
+    assertNotSameByteArray(kRowcolFamColQualColVisTimeStamp, row, cf, cq, cv);
+  }
+
+  private void assertNotSameByteArray(Key key, byte[] row, byte[] cf, byte[] cq, byte[] cv) {
+    if (key.getRowBytes().length != 0) {
+      assertNotSame(row, key.getRowBytes());
+      assertNotSame(row, key.getRowData().getBackingArray());
+      assertTrue(Arrays.equals(row, key.getRowBytes()));
+
+    }
+    if (key.getColFamily().length != 0) {
+      assertNotSame(cf, key.getColFamily());
+      assertNotSame(cf, key.getColumnFamilyData().getBackingArray());
+      assertTrue(Arrays.equals(cf, key.getColFamily()));
+
+    }
+    if (key.getColQualifier().length != 0) {
+      assertNotSame(cq, key.getColQualifier());
+      assertNotSame(cq, key.getColumnQualifierData().getBackingArray());
+      assertTrue(Arrays.equals(cq, key.getColQualifier()));
+
+    }
+    if (key.getColVisibility().length != 0) {
+      assertNotSame(cv, key.getColVisibility());
+      assertNotSame(cv, key.getColumnVisibilityData().getBackingArray());
+      assertTrue(Arrays.equals(cv, key.getColVisibility()));
+    }
+  }
+
+  @Test
+  public void testTextConstructorByteArrayConversion() {
+    Text rowText = new Text("r");
+    Text cfText = new Text("cf");
+    Text cqText = new Text("cq");
+    Text cvText = new Text("cv");
+
+    // make Keys from Text parameters
+    Key kRow = new Key(rowText);
+    Key kRowColFam = new Key(rowText, cfText);
+    Key kRowColFamColQual = new Key(rowText, cfText, cqText);
+    Key kRowColFamColQualColVis = new Key(rowText, cfText, cqText, cvText);
+    Key kRowColFamColQualColVisTimeStamp = new Key(rowText, cfText, cqText, cvText, 5L);
+
+    // test row constructor
+    assertTextValueConversionToByteArray(kRow);
+
+    // test row, column family constructor
+    assertTextValueConversionToByteArray(kRowColFam);
+
+    // test row, column family, column qualifier constructor
+    assertTextValueConversionToByteArray(kRowColFamColQual);
+
+    // test row, column family, column qualifier, column visibility constructor
+    assertTextValueConversionToByteArray(kRowColFamColQualColVis);
+
+    // test row, column family, column qualifier, column visibility, timestamp constructor
+    assertTextValueConversionToByteArray(kRowColFamColQualColVisTimeStamp);
+  }
+
+  private void assertTextValueConversionToByteArray(Key key) {
+    byte[] row = "r".getBytes();
+    byte[] cf = "cf".getBytes();
+    byte[] cq = "cq".getBytes();
+    byte[] cv = "cv".getBytes();
+    // show Text values submitted in constructor
+    // are converted to byte array containing
+    // the same value
+    if (key.getRowBytes().length != 0) {
+      assertTrue(Arrays.equals(row, key.getRowBytes()));
+
+    }
+    if (key.getColFamily().length != 0) {
+      assertTrue(Arrays.equals(cf, key.getColFamily()));
+
+    }
+    if (key.getColQualifier().length != 0) {
+      assertTrue(Arrays.equals(cq, key.getColQualifier()));
+
+    }
+    if (key.getColVisibility().length != 0) {
+      assertTrue(Arrays.equals(cv, key.getColVisibility()));
+    }
+  }
+
+  @Test
   public void testString() {
     Key k1 = new Key("r1");
     Key k2 = new Key(new Text("r1"));
@@ -117,6 +229,7 @@ public class KeyTest {
         "r f:q [v%00;] " + Long.MAX_VALUE + " false");
   }
 
+  @Test
   public void testVisibilityGetters() {
     Key k = new Key("r", "f", "q", "v1|(v2&v3)");