From 35a7f55e907ceeff298d3c8d6d637842d573c622 Mon Sep 17 00:00:00 2001 From: Christopher Tubbs Date: Wed, 18 Mar 2015 17:23:24 -0400 Subject: [PATCH] ACCUMULO-2945 Add mutation buffer size hinting --- .../apache/accumulo/core/data/Mutation.java | 60 ++++++++++++++++++- .../accumulo/core/data/MutationTest.java | 42 +++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/accumulo/core/data/Mutation.java b/core/src/main/java/org/apache/accumulo/core/data/Mutation.java index 4b1a812ab16..ed51204279d 100644 --- a/core/src/main/java/org/apache/accumulo/core/data/Mutation.java +++ b/core/src/main/java/org/apache/accumulo/core/data/Mutation.java @@ -108,6 +108,19 @@ public Mutation(byte[] row) { this(row, 0, row.length); } + /** + * Creates a new mutation. A defensive copy is made. + * + * @param row + * row ID + * @param initialBufferSize + * the initial size, in bytes, of the internal buffer for serializing + * @since 1.7.0 + */ + public Mutation(byte[] row, int initialBufferSize) { + this(row, 0, row.length, initialBufferSize); + } + /** * Creates a new mutation. A defensive copy is made. * @@ -122,9 +135,28 @@ public Mutation(byte[] row) { * @since 1.5.0 */ public Mutation(byte[] row, int start, int length) { + this(row, start, length, 64); + } + + /** + * Creates a new mutation. A defensive copy is made. + * + * @param row + * byte array containing row ID + * @param start + * starting index of row ID in byte array + * @param length + * length of row ID in byte array + * @param initialBufferSize + * the initial size, in bytes, of the internal buffer for serializing + * @throws IndexOutOfBoundsException + * if start or length is invalid + * @since 1.7.0 + */ + public Mutation(byte[] row, int start, int length, int initialBufferSize) { this.row = new byte[length]; System.arraycopy(row, start, this.row, 0, length); - buffer = new UnsynchronizedBuffer.Writer(); + buffer = new UnsynchronizedBuffer.Writer(initialBufferSize); } /** @@ -137,6 +169,19 @@ public Mutation(Text row) { this(row.getBytes(), 0, row.getLength()); } + /** + * Creates a new mutation. A defensive copy is made. + * + * @param row + * row ID + * @param initialBufferSize + * the initial size, in bytes, of the internal buffer for serializing + * @since 1.7.0 + */ + public Mutation(Text row, int initialBufferSize) { + this(row.getBytes(), 0, row.getLength(), initialBufferSize); + } + /** * Creates a new mutation. * @@ -147,6 +192,19 @@ public Mutation(CharSequence row) { this(new Text(row.toString())); } + /** + * Creates a new mutation. + * + * @param row + * row ID + * @param initialBufferSize + * the initial size, in bytes, of the internal buffer for serializing + * @since 1.7.0 + */ + public Mutation(CharSequence row, int initialBufferSize) { + this(new Text(row.toString()), initialBufferSize); + } + /** * Creates a new mutation. */ diff --git a/core/src/test/java/org/apache/accumulo/core/data/MutationTest.java b/core/src/test/java/org/apache/accumulo/core/data/MutationTest.java index 8b507885421..6607c6e841b 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/MutationTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/MutationTest.java @@ -16,6 +16,7 @@ */ package org.apache.accumulo.core.data; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -520,6 +521,47 @@ public void testReserialize() throws Exception { verifyColumnUpdate(m2.getUpdates().get(3), "cf2", "big", "", 0l, false, false, bigVal.toString()); } + // populate for testInitialBufferSizesEquals method + private static void populate(Mutation... muts) { + for (Mutation m : muts) { + m.put("cf1", "cq1", "v1"); + m.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); + m.put("cf1", "cq1", 3, "v3"); + m.put("cf1", "cq1", new ColumnVisibility("A&B&C"), 4, "v4"); + m.putDelete("cf2", "cf3"); + m.putDelete("cf2", "cf4", 3); + m.putDelete("cf2", "cf4", new ColumnVisibility("A&B&C"), 3); + } + } + + @Test + public void testInitialBufferSizesEquals() { + // m1 uses CharSequence constructor + Mutation m1 = new Mutation("r1"); + // m2 uses a different buffer size + Mutation m2 = new Mutation("r1", 4242); + // m3 uses Text constructor + Mutation m3 = new Mutation(new Text("r1")); + // m4 uses a different buffer size + Mutation m4 = new Mutation(new Text("r1"), 4242); + // m5 uses bytes constructor with offset/length + byte[] r1Bytes = "r1".getBytes(UTF_8); + Mutation m5 = new Mutation(r1Bytes); + // m6 uses a different buffer size + Mutation m6 = new Mutation(r1Bytes, 4242); + // m7 uses bytes constructor with offset/length + Mutation m7 = new Mutation(r1Bytes, 0, r1Bytes.length); + // m8 uses a different buffer size + Mutation m8 = new Mutation(r1Bytes, 0, r1Bytes.length, 4242); + + Mutation[] muts = new Mutation[] {m1, m2, m3, m4, m5, m6, m7, m8}; + populate(muts); + + for (Mutation m : muts) { + assertEquals(m1, m); + } + } + @Test public void testEquals() { Mutation m1 = new Mutation("r1");