Updated Branches: refs/heads/1.6.0-SNAPSHOT 73cbdc046 -> f06b06706
ACCUMULO-1959 Deprecate Value(ByteBuffer,boolean) The Value constructor that takes a ByteBuffer and a copy flag does an unnecessary double copy when the flag is true. This change deprecates the constructor in favor of the one without the flag (which always performs a copy). Signed-off-by: Josh Elser <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/f06b0670 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/f06b0670 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/f06b0670 Branch: refs/heads/1.6.0-SNAPSHOT Commit: f06b067063c92c1ae46f0c02dc24ff40564741b6 Parents: 73cbdc0 Author: Bill Havanki <[email protected]> Authored: Thu Dec 5 14:37:51 2013 -0500 Committer: Josh Elser <[email protected]> Committed: Mon Dec 16 13:39:02 2013 -0500 ---------------------------------------------------------------------- .../org/apache/accumulo/core/data/Value.java | 6 +- .../apache/accumulo/core/data/ValueTest.java | 186 +++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/f06b0670/core/src/main/java/org/apache/accumulo/core/data/Value.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/data/Value.java b/core/src/main/java/org/apache/accumulo/core/data/Value.java index 7d3cf8f..39ebbd0 100644 --- a/core/src/main/java/org/apache/accumulo/core/data/Value.java +++ b/core/src/main/java/org/apache/accumulo/core/data/Value.java @@ -60,8 +60,12 @@ public class Value implements WritableComparable<Object> { this(toBytes(bytes), false); } + /** + * @deprecated A copy of the bytes in the buffer is always made. Use {@link #Value(ByteBuffer)} instead. + */ + @Deprecated public Value(ByteBuffer bytes, boolean copy) { - this(toBytes(bytes), copy); + this(toBytes(bytes), false); } public Value(byte[] bytes, boolean copy) { http://git-wip-us.apache.org/repos/asf/accumulo/blob/f06b0670/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java b/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java new file mode 100644 index 0000000..37529aa --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java @@ -0,0 +1,186 @@ +/* + * 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.accumulo.core.data; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.*; + +public class ValueTest { + private static final byte[] toBytes(String s) { + return s.getBytes(Charset.forName("UTF-8")); + } + + private static final byte[] DATA = toBytes("data"); + private static final ByteBuffer DATABUFF = ByteBuffer.allocate(DATA.length); + static { + DATABUFF.put(DATA); + } + + @Before + public void setUp() throws Exception { + DATABUFF.rewind(); + } + + @Test(expected = IllegalStateException.class) + public void testNull() { + Value v = new Value(); + v.get(); + } + + @Test + public void testByteArray() { + Value v = new Value(DATA); + assertArrayEquals(DATA, v.get()); + assertSame(DATA, v.get()); + } + + @Test + public void testByteArrayCopy() { + Value v = new Value(DATA, true); + assertArrayEquals(DATA, v.get()); + assertNotSame(DATA, v.get()); + } + + @Test + public void testByteBuffer() { + Value v = new Value(DATABUFF); + assertArrayEquals(DATA, v.get()); + } + + @Test + public void testByteBufferCopy() { + Value v = new Value(DATABUFF, true); + assertArrayEquals(DATA, v.get()); + } + + @Test + public void testValueCopy() { + Value ov = createMock(Value.class); + expect(ov.get()).andReturn(DATA); + expect(ov.getSize()).andReturn(4); + replay(ov); + Value v = new Value(ov); + assertArrayEquals(DATA, v.get()); + } + + @Test + public void testByteArrayOffsetLength() { + Value v = new Value(DATA, 0, 4); + assertArrayEquals(DATA, v.get()); + } + + @Test + public void testSet() { + Value v = new Value(); + v.set(DATA); + assertArrayEquals(DATA, v.get()); + assertSame(DATA, v.get()); + } + + @Test + public void testCopy() { + Value v = new Value(); + v.copy(DATA); + assertArrayEquals(DATA, v.get()); + assertNotSame(DATA, v.get()); + } + + @Test + public void testGetSize() { + Value v = new Value(DATA); + assertEquals(DATA.length, v.getSize()); + } + + @Test(expected = IllegalStateException.class) + public void testGetSize_Null() { + Value v = new Value(); + v.getSize(); + } + + @Test + public void testWriteRead() throws Exception { + Value v = new Value(DATA); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + v.write(dos); + dos.close(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + DataInputStream dis = new DataInputStream(bais); + Value v2 = new Value(); + v2.readFields(dis); + dis.close(); + assertArrayEquals(DATA, v2.get()); + } + + @Test + public void testHashCode() { + Value v1 = new Value(DATA); + Value v2 = new Value(DATA); + assertEquals(v1.hashCode(), v2.hashCode()); + } + + @Test + public void testCompareTo() { + Value v1 = new Value(DATA); + Value v2 = new Value(toBytes("datb")); + assertTrue(v1.compareTo(v2) < 0); + assertTrue(v2.compareTo(v1) > 0); + Value v1a = new Value(DATA); + assertTrue(v1.compareTo(v1a) == 0); + Value v3 = new Value(toBytes("datc")); + assertTrue(v2.compareTo(v3) < 0); + assertTrue(v1.compareTo(v3) < 0); + } + + @Test + public void testEquals() { + Value v1 = new Value(DATA); + assertTrue(v1.equals(v1)); + Value v2 = new Value(DATA); + assertTrue(v1.equals(v2)); + assertTrue(v2.equals(v1)); + Value v3 = new Value(toBytes("datb")); + assertFalse(v1.equals(v3)); + } + + @Test + public void testToArray() { + List<byte[]> l = new java.util.ArrayList<byte[]>(); + byte[] one = toBytes("one"); + byte[] two = toBytes("two"); + byte[] three = toBytes("three"); + l.add(one); + l.add(two); + l.add(three); + + byte[][] a = Value.toArray(l); + assertEquals(3, a.length); + assertArrayEquals(one, a[0]); + assertArrayEquals(two, a[1]); + assertArrayEquals(three, a[2]); + } +}
