From cb53484d15e1565af5fe644addab98bf87e25b64 Mon Sep 17 00:00:00 2001 From: Mario Pastorelli Date: Sun, 17 Jul 2016 01:01:31 +0200 Subject: [PATCH] ACCUMULO-4375 Add missing byte[]-based Key constructors --- .../org/apache/accumulo/core/data/Key.java | 85 ++++++++++++++++++- .../apache/accumulo/core/data/KeyTest.java | 41 +++++++++ 2 files changed, 122 insertions(+), 4 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 758436de45e..678da0c5704 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 @@ -106,6 +106,18 @@ public Key(Text row) { init(row.getBytes(), 0, row.getLength(), EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); } + /** + * 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 + * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + * + * @param row + * row ID + */ + public Key(byte[] row) { + init(row, 0, row.length, EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); + } + /** * Creates a key with the specified row, empty column family, empty column qualifier, empty column visibility, the specified timestamp, and delete marker * false. @@ -121,7 +133,23 @@ public Key(Text row, long ts) { } /** - * Creates a key. The delete marker defaults to false. + * 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 + * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + * + * @param row + * row ID + * @param ts + * timestamp + */ + public Key(byte[] row, long ts) { + this(row); + timestamp = ts; + } + + /** + * 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. * * @param row * bytes containing row ID @@ -155,7 +183,8 @@ public Key(byte row[], int rOff, int rLen, byte cf[], int cfOff, int cfLen, byte } /** - * Creates a key. The delete marker defaults to false. + * 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. * * @param row * row ID @@ -173,7 +202,8 @@ public Key(byte[] row, byte[] colFamily, byte[] colQualifier, byte[] colVisibili } /** - * Creates a key. + * Creates a key. This constructor creates a copy of each specified arrays. If you don't want to create a copy, you should call + * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. * * @param row * row ID @@ -222,12 +252,31 @@ public Key(Text row, Text cf) { init(row.getBytes(), 0, row.getLength(), cf.getBytes(), 0, cf.getLength(), EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); } + /** + * 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 + * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + */ + public Key(byte[] row, byte[] cf) { + init(row, 0, row.length, cf, 0, cf.length, EMPTY_BYTES, 0, 0, EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); + } + /** * 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. */ public Key(Text row, Text cf, Text cq) { - init(row.getBytes(), 0, row.getLength(), cf.getBytes(), 0, cf.getLength(), cq.getBytes(), 0, cq.getLength(), EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); + init(row.getBytes(), 0, row.getLength(), cf.getBytes(), 0, cf.getLength(), cq.getBytes(), 0, cq.getLength(), EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, + true); + } + + /** + * 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 {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + */ + public Key(byte[] row, byte[] cf, byte[] cq) { + init(row, 0, row.length, cf, 0, cf.length, cq, 0, cq.length, EMPTY_BYTES, 0, 0, Long.MAX_VALUE, false, true); } /** @@ -239,6 +288,15 @@ public Key(Text row, Text cf, Text cq, Text cv) { Long.MAX_VALUE, false, true); } + /** + * 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 {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + */ + public Key(byte[] row, byte[] cf, byte[] cq, byte[] cv) { + init(row, 0, row.length, cf, 0, cf.length, cq, 0, cq.length, cv, 0, cv.length, Long.MAX_VALUE, false, true); + } + /** * 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. @@ -247,6 +305,15 @@ public Key(Text row, Text cf, Text cq, long ts) { init(row.getBytes(), 0, row.getLength(), cf.getBytes(), 0, cf.getLength(), cq.getBytes(), 0, cq.getLength(), EMPTY_BYTES, 0, 0, ts, false, true); } + /** + * 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 + * {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + */ + public Key(byte[] row, byte[] cf, byte[] cq, long ts) { + init(row, 0, row.length, cf, 0, cf.length, cq, 0, cq.length, EMPTY_BYTES, 0, 0, ts, false, true); + } + /** * 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. @@ -265,6 +332,16 @@ public Key(Text row, Text cf, Text cq, ColumnVisibility cv, long ts) { init(row.getBytes(), 0, row.getLength(), cf.getBytes(), 0, cf.getLength(), cq.getBytes(), 0, cq.getLength(), expr, 0, expr.length, ts, false, true); } + /** + * 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 {@link Key#Key(byte[] row, byte[] cf, byte[] cq, byte[] cv, long ts, boolean deleted, boolean copy)} instead. + */ + public Key(byte[] row, byte[] cf, byte[] cq, ColumnVisibility cv, long ts) { + byte[] expr = cv.getExpression(); + init(row, 0, row.length, cf, 0, cf.length, cq, 0, cq.length, expr, 0, expr.length, ts, false, true); + } + /** * Converts CharSequence to Text and creates a Key using {@link #Key(Text)}. */ 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 c94c6b42b4f..f14786fd392 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 @@ -166,4 +166,45 @@ public void testCompressDecompress() { assertEquals(kv.getKey(), new Key(tkv.getKey())); } } + + @Test + public void testBytesText() { + byte[] row = new byte[] {1}; + Key bytesRowKey = new Key(row); + Key textRowKey = new Key(new Text(row)); + assertEquals(bytesRowKey, textRowKey); + + byte[] colFamily = new byte[] {0, 1}; + Key bytesColFamilyKey = new Key(row, colFamily); + Key textColFamilyKey = new Key(new Text(row), new Text(colFamily)); + assertEquals(bytesColFamilyKey, textColFamilyKey); + + byte[] colQualifier = new byte[] {0, 0, 1}; + Key bytesColQualifierKey = new Key(row, colFamily, colQualifier); + Key textColQualifierKey = new Key(new Text(row), new Text(colFamily), new Text(colQualifier)); + assertEquals(bytesColQualifierKey, textColQualifierKey); + + byte[] colVisibility = new byte[] {0, 0, 0, 1}; + Key bytesColVisibilityKey = new Key(row, colFamily, colQualifier, colVisibility); + Key textColVisibilityKey = new Key(new Text(row), new Text(colFamily), new Text(colQualifier), new Text(colVisibility)); + assertEquals(bytesColVisibilityKey, textColVisibilityKey); + + long ts = 0L; + Key bytesTSKey = new Key(row, colFamily, colQualifier, colVisibility, ts); + Key textTSKey = new Key(new Text(row), new Text(colFamily), new Text(colQualifier), new Text(colVisibility), ts); + assertEquals(bytesTSKey, textTSKey); + + Key bytesTSKey2 = new Key(row, ts); + Key textTSKey2 = new Key(new Text(row), ts); + assertEquals(bytesTSKey2, textTSKey2); + + Key bytesTSKey3 = new Key(row, colFamily, colQualifier, ts); + Key testTSKey3 = new Key(new Text(row), new Text(colFamily), new Text(colQualifier), ts); + assertEquals(bytesTSKey3, testTSKey3); + + ColumnVisibility colVisibility2 = new ColumnVisibility("v1"); + Key bytesColVisibilityKey2 = new Key(row, colFamily, colQualifier, colVisibility2, ts); + Key textColVisibilityKey2 = new Key(new Text(row), new Text(colFamily), new Text(colQualifier), colVisibility2, ts); + assertEquals(bytesColVisibilityKey2, textColVisibilityKey2); + } }