http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/ConditionTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/ConditionTest.java b/core/src/test/java/org/apache/accumulo/core/data/ConditionTest.java index c76275b..8de9668 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/ConditionTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/ConditionTest.java @@ -195,34 +195,34 @@ public class ConditionTest { c2.setIterators(ITERATORS); assertTrue(c.equals(c2)); assertTrue(c2.equals(c)); - + // set everything but vis, so its null Condition c4 = new Condition(FAMILY, QUALIFIER); c4.setValue(VALUE); c4.setTimestamp(1234L); c4.setIterators(ITERATORS); - + assertFalse(c.equals(c4)); assertFalse(c4.equals(c)); - + // set everything but timestamp, so its null Condition c5 = new Condition(FAMILY, QUALIFIER); c5.setVisibility(cvis); c5.setValue(VALUE); c5.setIterators(ITERATORS); - + assertFalse(c.equals(c5)); assertFalse(c5.equals(c)); - + // set everything but value Condition c6 = new Condition(FAMILY, QUALIFIER); c6.setVisibility(cvis); c6.setTimestamp(1234L); c6.setIterators(ITERATORS); - + assertFalse(c.equals(c6)); assertFalse(c6.equals(c)); - + // test w/ no optional fields set Condition c7 = new Condition(FAMILY, QUALIFIER); Condition c8 = new Condition(FAMILY, QUALIFIER);
http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/ConstraintViolationSummaryTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/ConstraintViolationSummaryTest.java b/core/src/test/java/org/apache/accumulo/core/data/ConstraintViolationSummaryTest.java index ec2110e..00e7c56 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/ConstraintViolationSummaryTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/ConstraintViolationSummaryTest.java @@ -16,7 +16,7 @@ */ package org.apache.accumulo.core.data; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -24,14 +24,11 @@ public class ConstraintViolationSummaryTest { @Test public void testToString() { - ConstraintViolationSummary cvs = new ConstraintViolationSummary( - "fooClass", (short) 1, "fooDescription", 100L); - assertEquals("ConstraintViolationSummary(constrainClass:fooClass, violationCode:1, violationDescription:fooDescription, numberOfViolatingMutations:100)", - cvs.toString()); - - cvs = new ConstraintViolationSummary( - null, (short) 2, null, 101L); - assertEquals("ConstraintViolationSummary(constrainClass:null, violationCode:2, violationDescription:null, numberOfViolatingMutations:101)", + ConstraintViolationSummary cvs = new ConstraintViolationSummary("fooClass", (short) 1, "fooDescription", 100L); + assertEquals("ConstraintViolationSummary(constrainClass:fooClass, violationCode:1, violationDescription:fooDescription, numberOfViolatingMutations:100)", cvs.toString()); + + cvs = new ConstraintViolationSummary(null, (short) 2, null, 101L); + assertEquals("ConstraintViolationSummary(constrainClass:null, violationCode:2, violationDescription:null, numberOfViolatingMutations:101)", cvs.toString()); } } http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java b/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java index b32dd26..749f14c 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java @@ -45,7 +45,7 @@ public class KeyExtentTest { KeyExtent nke(String t, String er, String per) { return new KeyExtent(new Text(t), er == null ? null : new Text(er), per == null ? null : new Text(per)); } - + KeyExtent ke; TreeSet<KeyExtent> set0; @@ -57,153 +57,153 @@ public class KeyExtentTest { @Test public void testDecodingMetadataRow() { Text flattenedExtent = new Text("foo;bar"); - + ke = new KeyExtent(flattenedExtent, (Text) null); - + assertEquals(new Text("bar"), ke.getEndRow()); assertEquals(new Text("foo"), ke.getTableId()); assertNull(ke.getPrevEndRow()); - + flattenedExtent = new Text("foo<"); - + ke = new KeyExtent(flattenedExtent, (Text) null); - + assertNull(ke.getEndRow()); assertEquals(new Text("foo"), ke.getTableId()); assertNull(ke.getPrevEndRow()); - + flattenedExtent = new Text("foo;bar;"); - + ke = new KeyExtent(flattenedExtent, (Text) null); - + assertEquals(new Text("bar;"), ke.getEndRow()); assertEquals(new Text("foo"), ke.getTableId()); assertNull(ke.getPrevEndRow()); - + } - + @Test public void testFindContainingExtents() { assertNull(KeyExtent.findContainingExtent(nke("t", null, null), set0)); assertNull(KeyExtent.findContainingExtent(nke("t", "1", "0"), set0)); assertNull(KeyExtent.findContainingExtent(nke("t", "1", null), set0)); assertNull(KeyExtent.findContainingExtent(nke("t", null, "0"), set0)); - + TreeSet<KeyExtent> set1 = new TreeSet<KeyExtent>(); - + set1.add(nke("t", null, null)); - + assertEquals(nke("t", null, null), KeyExtent.findContainingExtent(nke("t", null, null), set1)); assertEquals(nke("t", null, null), KeyExtent.findContainingExtent(nke("t", "1", "0"), set1)); assertEquals(nke("t", null, null), KeyExtent.findContainingExtent(nke("t", "1", null), set1)); assertEquals(nke("t", null, null), KeyExtent.findContainingExtent(nke("t", null, "0"), set1)); - + TreeSet<KeyExtent> set2 = new TreeSet<KeyExtent>(); - + set2.add(nke("t", "g", null)); set2.add(nke("t", null, "g")); - + assertNull(KeyExtent.findContainingExtent(nke("t", null, null), set2)); assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "c", "a"), set2)); assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "c", null), set2)); - + assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "g", "a"), set2)); assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "g", null), set2)); - + assertNull(KeyExtent.findContainingExtent(nke("t", "h", "a"), set2)); assertNull(KeyExtent.findContainingExtent(nke("t", "h", null), set2)); - + assertNull(KeyExtent.findContainingExtent(nke("t", "z", "f"), set2)); assertNull(KeyExtent.findContainingExtent(nke("t", null, "f"), set2)); - + assertEquals(nke("t", null, "g"), KeyExtent.findContainingExtent(nke("t", "z", "g"), set2)); assertEquals(nke("t", null, "g"), KeyExtent.findContainingExtent(nke("t", null, "g"), set2)); - + assertEquals(nke("t", null, "g"), KeyExtent.findContainingExtent(nke("t", "z", "h"), set2)); assertEquals(nke("t", null, "g"), KeyExtent.findContainingExtent(nke("t", null, "h"), set2)); - + TreeSet<KeyExtent> set3 = new TreeSet<KeyExtent>(); - + set3.add(nke("t", "g", null)); set3.add(nke("t", "s", "g")); set3.add(nke("t", null, "s")); - + assertNull(KeyExtent.findContainingExtent(nke("t", null, null), set3)); - + assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "g", null), set3)); assertEquals(nke("t", "s", "g"), KeyExtent.findContainingExtent(nke("t", "s", "g"), set3)); assertEquals(nke("t", null, "s"), KeyExtent.findContainingExtent(nke("t", null, "s"), set3)); - + assertNull(KeyExtent.findContainingExtent(nke("t", "t", "g"), set3)); assertNull(KeyExtent.findContainingExtent(nke("t", "t", "f"), set3)); assertNull(KeyExtent.findContainingExtent(nke("t", "s", "f"), set3)); - + assertEquals(nke("t", "s", "g"), KeyExtent.findContainingExtent(nke("t", "r", "h"), set3)); assertEquals(nke("t", "s", "g"), KeyExtent.findContainingExtent(nke("t", "s", "h"), set3)); assertEquals(nke("t", "s", "g"), KeyExtent.findContainingExtent(nke("t", "r", "g"), set3)); - + assertEquals(nke("t", null, "s"), KeyExtent.findContainingExtent(nke("t", null, "t"), set3)); assertNull(KeyExtent.findContainingExtent(nke("t", null, "r"), set3)); - + assertEquals(nke("t", "g", null), KeyExtent.findContainingExtent(nke("t", "f", null), set3)); assertNull(KeyExtent.findContainingExtent(nke("t", "h", null), set3)); - + TreeSet<KeyExtent> set4 = new TreeSet<KeyExtent>(); - + set4.add(nke("t1", "d", null)); set4.add(nke("t1", "q", "d")); set4.add(nke("t1", null, "q")); set4.add(nke("t2", "g", null)); set4.add(nke("t2", "s", "g")); set4.add(nke("t2", null, "s")); - + assertNull(KeyExtent.findContainingExtent(nke("t", null, null), set4)); assertNull(KeyExtent.findContainingExtent(nke("z", null, null), set4)); assertNull(KeyExtent.findContainingExtent(nke("t11", null, null), set4)); assertNull(KeyExtent.findContainingExtent(nke("t1", null, null), set4)); assertNull(KeyExtent.findContainingExtent(nke("t2", null, null), set4)); - + assertNull(KeyExtent.findContainingExtent(nke("t", "g", null), set4)); assertNull(KeyExtent.findContainingExtent(nke("z", "g", null), set4)); assertNull(KeyExtent.findContainingExtent(nke("t11", "g", null), set4)); assertNull(KeyExtent.findContainingExtent(nke("t1", "g", null), set4)); - + assertEquals(nke("t2", "g", null), KeyExtent.findContainingExtent(nke("t2", "g", null), set4)); assertEquals(nke("t2", "s", "g"), KeyExtent.findContainingExtent(nke("t2", "s", "g"), set4)); assertEquals(nke("t2", null, "s"), KeyExtent.findContainingExtent(nke("t2", null, "s"), set4)); - + assertEquals(nke("t1", "d", null), KeyExtent.findContainingExtent(nke("t1", "d", null), set4)); assertEquals(nke("t1", "q", "d"), KeyExtent.findContainingExtent(nke("t1", "q", "d"), set4)); assertEquals(nke("t1", null, "q"), KeyExtent.findContainingExtent(nke("t1", null, "q"), set4)); - + } - + private static boolean overlaps(KeyExtent extent, SortedMap<KeyExtent,Object> extents) { return !KeyExtent.findOverlapping(extent, extents).isEmpty(); } - + @Test public void testOverlaps() { SortedMap<KeyExtent,Object> set0 = new TreeMap<KeyExtent,Object>(); set0.put(nke("a", null, null), null); - + // Nothing overlaps with the empty set assertFalse(overlaps(nke("t", null, null), null)); assertFalse(overlaps(nke("t", null, null), set0)); - + SortedMap<KeyExtent,Object> set1 = new TreeMap<KeyExtent,Object>(); - + // Everything overlaps with the infinite range set1.put(nke("t", null, null), null); assertTrue(overlaps(nke("t", null, null), set1)); assertTrue(overlaps(nke("t", "b", "a"), set1)); assertTrue(overlaps(nke("t", null, "a"), set1)); - + set1.put(nke("t", "b", "a"), null); assertTrue(overlaps(nke("t", null, null), set1)); assertTrue(overlaps(nke("t", "b", "a"), set1)); assertTrue(overlaps(nke("t", null, "a"), set1)); - + // simple overlaps SortedMap<KeyExtent,Object> set2 = new TreeMap<KeyExtent,Object>(); set2.put(nke("a", null, null), null); @@ -214,7 +214,7 @@ public class KeyExtentTest { assertTrue(overlaps(nke("t", "z", "a"), set2)); assertFalse(overlaps(nke("t", "j", "a"), set2)); assertFalse(overlaps(nke("t", "z", "m"), set2)); - + // non-overlaps assertFalse(overlaps(nke("t", "b", "a"), set2)); assertFalse(overlaps(nke("t", "z", "y"), set2)); @@ -222,7 +222,7 @@ public class KeyExtentTest { assertFalse(overlaps(nke("t", null, "y"), set2)); assertFalse(overlaps(nke("t", "j", null), set2)); assertFalse(overlaps(nke("t", null, "m"), set2)); - + // infinite overlaps SortedMap<KeyExtent,Object> set3 = new TreeMap<KeyExtent,Object>(); set3.put(nke("t", "j", null), null); @@ -232,10 +232,10 @@ public class KeyExtentTest { assertTrue(overlaps(nke("t", "z", "k"), set3)); assertTrue(overlaps(nke("t", null, "k"), set3)); assertTrue(overlaps(nke("t", null, null), set3)); - + // falls between assertFalse(overlaps(nke("t", "l", "k"), set3)); - + SortedMap<KeyExtent,Object> set4 = new TreeMap<KeyExtent,Object>(); set4.put(nke("t", null, null), null); assertTrue(overlaps(nke("t", "k", "a"), set4)); @@ -244,7 +244,7 @@ public class KeyExtentTest { assertTrue(overlaps(nke("t", null, "k"), set4)); assertTrue(overlaps(nke("t", null, null), set4)); assertTrue(overlaps(nke("t", null, null), set4)); - + for (String er : new String[] {"z", "y", "r", null}) { for (String per : new String[] {"a", "b", "d", null}) { assertTrue(nke("t", "y", "b").overlaps(nke("t", er, per))); @@ -253,7 +253,7 @@ public class KeyExtentTest { assertTrue(nke("t", null, null).overlaps(nke("t", er, per))); } } - + assertFalse(nke("t", "y", "b").overlaps(nke("t", "z", "y"))); assertFalse(nke("t", "y", "b").overlaps(nke("t", null, "y"))); assertFalse(nke("t", "y", null).overlaps(nke("t", "z", "y"))); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/KeyTest.java ---------------------------------------------------------------------- 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 cc737db..9cee691 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 @@ -39,87 +39,87 @@ public class KeyTest { Key k2 = new Key("r1".getBytes(), "cf".getBytes(), "cq".getBytes(), new byte[0], 0, false); Key k3 = new Key("r1".getBytes(), "cf".getBytes(), "cq".getBytes(), new byte[0], 0, true); Key k4 = new Key("r1".getBytes(), "cf".getBytes(), "cq".getBytes(), new byte[0], 0, true); - + assertTrue(k1.equals(k2)); assertTrue(k3.equals(k4)); assertTrue(k1.compareTo(k3) > 0); assertTrue(k3.compareTo(k1) < 0); } - + @Test public void testCopyData() { byte row[] = "r".getBytes(); byte cf[] = "cf".getBytes(); byte cq[] = "cq".getBytes(); byte cv[] = "cv".getBytes(); - + Key k1 = new Key(row, cf, cq, cv, 5l, false, false); Key k2 = new Key(row, cf, cq, cv, 5l, false, true); - + assertSame(row, k1.getRowBytes()); assertSame(cf, k1.getColFamily()); assertSame(cq, k1.getColQualifier()); assertSame(cv, k1.getColVisibility()); - + assertSame(row, k1.getRowData().getBackingArray()); assertSame(cf, k1.getColumnFamilyData().getBackingArray()); assertSame(cq, k1.getColumnQualifierData().getBackingArray()); assertSame(cv, k1.getColumnVisibilityData().getBackingArray()); - + assertNotSame(row, k2.getRowBytes()); assertNotSame(cf, k2.getColFamily()); assertNotSame(cq, k2.getColQualifier()); assertNotSame(cv, k2.getColVisibility()); - + assertNotSame(row, k2.getRowData().getBackingArray()); assertNotSame(cf, k2.getColumnFamilyData().getBackingArray()); assertNotSame(cq, k2.getColumnQualifierData().getBackingArray()); assertNotSame(cv, k2.getColumnVisibilityData().getBackingArray()); - + assertEquals(k1, k2); - + } - + @Test public void testString() { Key k1 = new Key("r1"); Key k2 = new Key(new Text("r1")); assertEquals(k2, k1); - + k1 = new Key("r1", "cf1"); k2 = new Key(new Text("r1"), new Text("cf1")); assertEquals(k2, k1); - + k1 = new Key("r1", "cf2", "cq2"); k2 = new Key(new Text("r1"), new Text("cf2"), new Text("cq2")); assertEquals(k2, k1); - + k1 = new Key("r1", "cf2", "cq2", "cv"); k2 = new Key(new Text("r1"), new Text("cf2"), new Text("cq2"), new Text("cv")); assertEquals(k2, k1); - + k1 = new Key("r1", "cf2", "cq2", "cv", 89); k2 = new Key(new Text("r1"), new Text("cf2"), new Text("cq2"), new Text("cv"), 89); assertEquals(k2, k1); - + k1 = new Key("r1", "cf2", "cq2", 89); k2 = new Key(new Text("r1"), new Text("cf2"), new Text("cq2"), 89); assertEquals(k2, k1); - + } - + @Test public void testVisibilityFollowingKey() { Key k = new Key("r", "f", "q", "v"); assertEquals(k.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS).toString(), "r f:q [v%00;] " + Long.MAX_VALUE + " false"); } - + public void testVisibilityGetters() { Key k = new Key("r", "f", "q", "v1|(v2&v3)"); - + Text expression = k.getColumnVisibility(); ColumnVisibility parsed = k.getColumnVisibilityParsed(); - + assertEquals(expression, new Text(parsed.getExpression())); } @@ -131,7 +131,7 @@ public class KeyTest { assertEquals(k, k2); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void testThrift_Invalid() { Key k = new Key("r1", "cf2", "cq2", "cv"); TKey tk = k.toThrift(); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/MutationTest.java ---------------------------------------------------------------------- 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 33b060e..8b50788 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,10 @@ */ package org.apache.accumulo.core.data; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; @@ -24,17 +28,13 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; -import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import org.apache.accumulo.core.data.thrift.TMutation; import org.apache.accumulo.core.security.ColumnVisibility; import org.apache.hadoop.io.Text; +import org.junit.Test; + +public class MutationTest { -public class MutationTest { - private static String toHexString(byte[] ba) { StringBuilder str = new StringBuilder(); for (int i = 0; i < ba.length; i++) { @@ -43,102 +43,101 @@ public class MutationTest { return str.toString(); } - /* Test constructing a Mutation using a byte buffer. The byte array - * returned as the row is converted to a hexadecimal string for easy - * comparision. + /* + * Test constructing a Mutation using a byte buffer. The byte array returned as the row is converted to a hexadecimal string for easy comparision. */ public void testByteConstructor() { Mutation m = new Mutation("0123456789".getBytes()); assertEquals("30313233343536373839", toHexString(m.getRow())); } - + public void testLimitedByteConstructor() { Mutation m = new Mutation("0123456789".getBytes(), 2, 5); assertEquals("3233343536", toHexString(m.getRow())); } - + @Test public void test1() { Mutation m = new Mutation(new Text("r1")); m.put(new Text("cf1"), new Text("cq1"), new Value("v1".getBytes())); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(1, updates.size()); - + ColumnUpdate cu = updates.get(0); - + assertEquals("cf1", new String(cu.getColumnFamily())); assertEquals("cq1", new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertFalse(cu.hasTimestamp()); - + } - + @Test public void test2() throws IOException { Mutation m = new Mutation(new Text("r1")); m.put(new Text("cf1"), new Text("cq1"), new Value("v1".getBytes())); m.put(new Text("cf2"), new Text("cq2"), 56, new Value("v2".getBytes())); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(2, updates.size()); - + assertEquals("r1", new String(m.getRow())); ColumnUpdate cu = updates.get(0); - + assertEquals("cf1", new String(cu.getColumnFamily())); assertEquals("cq1", new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertFalse(cu.hasTimestamp()); - + cu = updates.get(1); - + assertEquals("cf2", new String(cu.getColumnFamily())); assertEquals("cq2", new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertTrue(cu.hasTimestamp()); assertEquals(56, cu.getTimestamp()); - + m = cloneMutation(m); - + assertEquals("r1", new String(m.getRow())); updates = m.getUpdates(); - + assertEquals(2, updates.size()); - + cu = updates.get(0); - + assertEquals("cf1", new String(cu.getColumnFamily())); assertEquals("cq1", new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertFalse(cu.hasTimestamp()); - + cu = updates.get(1); - + assertEquals("cf2", new String(cu.getColumnFamily())); assertEquals("cq2", new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertTrue(cu.hasTimestamp()); assertEquals(56, cu.getTimestamp()); - + } - + private Mutation cloneMutation(Mutation m) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); m.write(dos); dos.close(); - + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DataInputStream dis = new DataInputStream(bais); - + m = new Mutation(); m.readFields(dis); return m; } - + @Test public void test3() throws IOException { Mutation m = new Mutation(new Text("r1")); @@ -147,11 +146,11 @@ public class MutationTest { byte val[] = new byte[len]; for (int j = 0; j < len; j++) val[j] = (byte) i; - + m.put(new Text("cf" + i), new Text("cq" + i), new Value(val)); - + } - + for (int r = 0; r < 3; r++) { assertEquals("r1", new String(m.getRow())); List<ColumnUpdate> updates = m.getUpdates(); @@ -162,149 +161,149 @@ public class MutationTest { assertEquals("cq" + i, new String(cu.getColumnQualifier())); assertEquals("", new String(cu.getColumnVisibility())); assertFalse(cu.hasTimestamp()); - + byte[] val = cu.getValue(); int len = Mutation.VALUE_SIZE_COPY_CUTOFF - 2 + i; assertEquals(len, val.length); for (int j = 0; j < len; j++) assertEquals(i, val[j]); } - + m = cloneMutation(m); } } - + private Text nt(String s) { return new Text(s); } - + private Value nv(String s) { return new Value(s.getBytes()); } - + @Test public void testPuts() { Mutation m = new Mutation(new Text("r1")); - + m.put(nt("cf1"), nt("cq1"), nv("v1")); m.put(nt("cf2"), nt("cq2"), new ColumnVisibility("cv2"), nv("v2")); m.put(nt("cf3"), nt("cq3"), 3l, nv("v3")); m.put(nt("cf4"), nt("cq4"), new ColumnVisibility("cv4"), 4l, nv("v4")); - + m.putDelete(nt("cf5"), nt("cq5")); m.putDelete(nt("cf6"), nt("cq6"), new ColumnVisibility("cv6")); m.putDelete(nt("cf7"), nt("cq7"), 7l); m.putDelete(nt("cf8"), nt("cq8"), new ColumnVisibility("cv8"), 8l); - + assertEquals(8, m.size()); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(8, m.size()); assertEquals(8, updates.size()); - + verifyColumnUpdate(updates.get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(updates.get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); verifyColumnUpdate(updates.get(2), "cf3", "cq3", "", 3l, true, false, "v3"); verifyColumnUpdate(updates.get(3), "cf4", "cq4", "cv4", 4l, true, false, "v4"); - + verifyColumnUpdate(updates.get(4), "cf5", "cq5", "", 0l, false, true, ""); verifyColumnUpdate(updates.get(5), "cf6", "cq6", "cv6", 0l, false, true, ""); verifyColumnUpdate(updates.get(6), "cf7", "cq7", "", 7l, true, true, ""); verifyColumnUpdate(updates.get(7), "cf8", "cq8", "cv8", 8l, true, true, ""); - + } - + @Test public void testPutsString() { Mutation m = new Mutation("r1"); - + m.put("cf1", "cq1", nv("v1")); m.put("cf2", "cq2", new ColumnVisibility("cv2"), nv("v2")); m.put("cf3", "cq3", 3l, nv("v3")); m.put("cf4", "cq4", new ColumnVisibility("cv4"), 4l, nv("v4")); - + m.putDelete("cf5", "cq5"); m.putDelete("cf6", "cq6", new ColumnVisibility("cv6")); m.putDelete("cf7", "cq7", 7l); m.putDelete("cf8", "cq8", new ColumnVisibility("cv8"), 8l); - + assertEquals(8, m.size()); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(8, m.size()); assertEquals(8, updates.size()); - + verifyColumnUpdate(updates.get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(updates.get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); verifyColumnUpdate(updates.get(2), "cf3", "cq3", "", 3l, true, false, "v3"); verifyColumnUpdate(updates.get(3), "cf4", "cq4", "cv4", 4l, true, false, "v4"); - + verifyColumnUpdate(updates.get(4), "cf5", "cq5", "", 0l, false, true, ""); verifyColumnUpdate(updates.get(5), "cf6", "cq6", "cv6", 0l, false, true, ""); verifyColumnUpdate(updates.get(6), "cf7", "cq7", "", 7l, true, true, ""); verifyColumnUpdate(updates.get(7), "cf8", "cq8", "cv8", 8l, true, true, ""); } - + @Test public void testPutsStringString() { Mutation m = new Mutation("r1"); - + m.put("cf1", "cq1", "v1"); m.put("cf2", "cq2", new ColumnVisibility("cv2"), "v2"); m.put("cf3", "cq3", 3l, "v3"); m.put("cf4", "cq4", new ColumnVisibility("cv4"), 4l, "v4"); - + m.putDelete("cf5", "cq5"); m.putDelete("cf6", "cq6", new ColumnVisibility("cv6")); m.putDelete("cf7", "cq7", 7l); m.putDelete("cf8", "cq8", new ColumnVisibility("cv8"), 8l); - + assertEquals(8, m.size()); assertEquals("r1", new String(m.getRow())); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(8, m.size()); assertEquals(8, updates.size()); - + verifyColumnUpdate(updates.get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(updates.get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); verifyColumnUpdate(updates.get(2), "cf3", "cq3", "", 3l, true, false, "v3"); verifyColumnUpdate(updates.get(3), "cf4", "cq4", "cv4", 4l, true, false, "v4"); - + verifyColumnUpdate(updates.get(4), "cf5", "cq5", "", 0l, false, true, ""); verifyColumnUpdate(updates.get(5), "cf6", "cq6", "cv6", 0l, false, true, ""); verifyColumnUpdate(updates.get(6), "cf7", "cq7", "", 7l, true, true, ""); verifyColumnUpdate(updates.get(7), "cf8", "cq8", "cv8", 8l, true, true, ""); } - + public void testByteArrays() { Mutation m = new Mutation("r1".getBytes()); - + m.put("cf1".getBytes(), "cq1".getBytes(), "v1".getBytes()); m.put("cf2".getBytes(), "cq2".getBytes(), new ColumnVisibility("cv2"), "v2".getBytes()); m.put("cf3".getBytes(), "cq3".getBytes(), 3l, "v3".getBytes()); m.put("cf4".getBytes(), "cq4".getBytes(), new ColumnVisibility("cv4"), 4l, "v4".getBytes()); - + m.putDelete("cf5".getBytes(), "cq5".getBytes()); m.putDelete("cf6".getBytes(), "cq6".getBytes(), new ColumnVisibility("cv6")); m.putDelete("cf7".getBytes(), "cq7".getBytes(), 7l); m.putDelete("cf8".getBytes(), "cq8".getBytes(), new ColumnVisibility("cv8"), 8l); - + assertEquals(8, m.size()); - + List<ColumnUpdate> updates = m.getUpdates(); - + assertEquals(8, m.size()); assertEquals(8, updates.size()); - + verifyColumnUpdate(updates.get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(updates.get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); verifyColumnUpdate(updates.get(2), "cf3", "cq3", "", 3l, true, false, "v3"); verifyColumnUpdate(updates.get(3), "cf4", "cq4", "cv4", 4l, true, false, "v4"); - + verifyColumnUpdate(updates.get(4), "cf5", "cq5", "", 0l, false, true, ""); verifyColumnUpdate(updates.get(5), "cf6", "cq6", "cv6", 0l, false, true, ""); verifyColumnUpdate(updates.get(6), "cf7", "cq7", "", 7l, true, true, ""); @@ -327,28 +326,28 @@ public class MutationTest { m1.put("cf1.3", "cq1.3", new ColumnVisibility("E|F"), new String(val1_3)); int size1 = m1.size(); long nb1 = m1.numBytes(); - + Mutation m2 = new Mutation("row2"); byte[] val2 = new byte[Mutation.VALUE_SIZE_COPY_CUTOFF + 2]; Arrays.fill(val2, (byte) 2); m2.put("cf2", "cq2", new ColumnVisibility("G|H"), 1234, new String(val2)); int size2 = m2.size(); long nb2 = m2.numBytes(); - + ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); m1.write(dos); m2.write(dos); dos.close(); - + // Now read the mutations back in from the byte array, making sure to // reuse the same mutation object, and make sure everything is correct. ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); - + Mutation m = new Mutation(); m.readFields(dis); - + assertEquals("row1", new String(m.getRow())); assertEquals(size1, m.size()); assertEquals(nb1, m.numBytes()); @@ -356,20 +355,20 @@ public class MutationTest { verifyColumnUpdate(m.getUpdates().get(0), "cf1.1", "cq1.1", "A|B", 0L, false, false, "val1.1"); verifyColumnUpdate(m.getUpdates().get(1), "cf1.2", "cq1.2", "C|D", 0L, false, false, "val1.2"); verifyColumnUpdate(m.getUpdates().get(2), "cf1.3", "cq1.3", "E|F", 0L, false, false, new String(val1_3)); - + // Reuse the same mutation object (which is what happens in the hadoop framework // when objects are read by an input format) m.readFields(dis); - + assertEquals("row2", new String(m.getRow())); assertEquals(size2, m.size()); assertEquals(nb2, m.numBytes()); assertEquals(1, m.getUpdates().size()); verifyColumnUpdate(m.getUpdates().get(0), "cf2", "cq2", "G|H", 1234L, true, false, new String(val2)); } - + private void verifyColumnUpdate(ColumnUpdate cu, String cf, String cq, String cv, long ts, boolean timeSet, boolean deleted, String val) { - + assertEquals(cf, new String(cu.getColumnFamily())); assertEquals(cq, new String(cu.getColumnQualifier())); assertEquals(cv, new String(cu.getColumnVisibility())); @@ -379,38 +378,38 @@ public class MutationTest { assertEquals(deleted, cu.isDeleted()); assertEquals(val, new String(cu.getValue())); } - + @Test public void test4() throws Exception { Mutation m1 = new Mutation(new Text("r1")); - + m1.put(nt("cf1"), nt("cq1"), nv("v1")); m1.put(nt("cf2"), nt("cq2"), new ColumnVisibility("cv2"), nv("v2")); - + ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); m1.write(dos); dos.close(); - + Mutation m2 = new Mutation(new Text("r2")); - + m2.put(nt("cf3"), nt("cq3"), nv("v3")); m2.put(nt("cf4"), nt("cq4"), new ColumnVisibility("cv2"), nv("v4")); - + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); - + // used to be a bug where puts done before readFields would be seen // after readFields m2.readFields(dis); - + assertEquals("r1", new String(m2.getRow())); assertEquals(2, m2.getUpdates().size()); assertEquals(2, m2.size()); verifyColumnUpdate(m2.getUpdates().get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(m2.getUpdates().get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); } - + Mutation convert(OldMutation old) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); @@ -423,7 +422,7 @@ public class MutationTest { dis.close(); return m; } - + @Test public void testNewSerialization() throws Exception { // write an old mutation @@ -435,12 +434,12 @@ public class MutationTest { DataOutputStream dos = new DataOutputStream(bos); m2.write(dos); dos.close(); - long oldSize = dos.size(); + long oldSize = dos.size(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); m2.readFields(dis); dis.close(); - + // check it assertEquals("r1", new String(m2.getRow())); assertEquals(3, m2.getUpdates().size()); @@ -450,14 +449,14 @@ public class MutationTest { verifyColumnUpdate(m2.getUpdates().get(2), "cf3", "cq3", "", 0l, false, true, ""); Mutation m1 = convert(m2); - + assertEquals("r1", new String(m1.getRow())); assertEquals(3, m1.getUpdates().size()); assertEquals(3, m1.size()); verifyColumnUpdate(m1.getUpdates().get(0), "cf1", "cq1", "", 0l, false, false, "v1"); verifyColumnUpdate(m1.getUpdates().get(1), "cf2", "cq2", "cv2", 0l, false, false, "v2"); verifyColumnUpdate(m1.getUpdates().get(2), "cf3", "cq3", "", 0l, false, true, ""); - + Text exampleRow = new Text(" 123456789 123456789 123456789 123456789 123456789"); int exampleLen = exampleRow.getLength(); m1 = new Mutation(exampleRow); @@ -472,7 +471,7 @@ public class MutationTest { assertEquals(10, newSize - exampleLen); assertEquals(68, oldSize - exampleLen); // I am converting to integer to avoid comparing floats which are inaccurate - assertEquals(14705, (int)(((newSize-exampleLen) * 100. / (oldSize - exampleLen)) * 1000)); + assertEquals(14705, (int) (((newSize - exampleLen) * 100. / (oldSize - exampleLen)) * 1000)); StringBuilder sb = new StringBuilder(); byte[] ba = bos.toByteArray(); for (int i = 0; i < bos.size(); i += 4) { @@ -481,10 +480,11 @@ public class MutationTest { } sb.append(" "); } - assertEquals("80322031 32333435 36373839 20313233 34353637 38392031 32333435 36373839 20313233 34353637 38392031 32333435 36373839 06000000 00000001 ", sb.toString()); + assertEquals("80322031 32333435 36373839 20313233 34353637 38392031 32333435 36373839 20313233 34353637 38392031 32333435 36373839 06000000 00000001 ", + sb.toString()); } - + @Test public void testReserialize() throws Exception { // test reading in a new mutation from an old mutation and reserializing the new mutation... this was failing @@ -498,20 +498,19 @@ public class MutationTest { } om.put("cf2", "big", bigVal); - Mutation m1 = convert(om); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); m1.write(dos); dos.close(); - + Mutation m2 = new Mutation(); - + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); m2.readFields(dis); - + assertEquals("r1", new String(m1.getRow())); assertEquals(4, m2.getUpdates().size()); assertEquals(4, m2.size()); @@ -520,11 +519,11 @@ public class MutationTest { verifyColumnUpdate(m2.getUpdates().get(2), "cf3", "cq3", "", 0l, false, true, ""); verifyColumnUpdate(m2.getUpdates().get(3), "cf2", "big", "", 0l, false, false, bigVal.toString()); } - + @Test public void testEquals() { Mutation m1 = new Mutation("r1"); - + m1.put("cf1", "cq1", "v1"); m1.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); m1.put("cf1", "cq1", 3, "v3"); @@ -532,10 +531,10 @@ public class MutationTest { m1.putDelete("cf2", "cf3"); m1.putDelete("cf2", "cf4", 3); m1.putDelete("cf2", "cf4", new ColumnVisibility("A&B&C"), 3); - + // m2 has same data as m1 Mutation m2 = new Mutation("r1"); - + m2.put("cf1", "cq1", "v1"); m2.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); m2.put("cf1", "cq1", 3, "v3"); @@ -543,10 +542,10 @@ public class MutationTest { m2.putDelete("cf2", "cf3"); m2.putDelete("cf2", "cf4", 3); m2.putDelete("cf2", "cf4", new ColumnVisibility("A&B&C"), 3); - + // m3 has different row than m1 Mutation m3 = new Mutation("r2"); - + m3.put("cf1", "cq1", "v1"); m3.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); m3.put("cf1", "cq1", 3, "v3"); @@ -557,7 +556,7 @@ public class MutationTest { // m4 has a different column than m1 Mutation m4 = new Mutation("r1"); - + m4.put("cf2", "cq1", "v1"); m4.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); m4.put("cf1", "cq1", 3, "v3"); @@ -565,10 +564,10 @@ public class MutationTest { m4.putDelete("cf2", "cf3"); m4.putDelete("cf2", "cf4", 3); m4.putDelete("cf2", "cf4", new ColumnVisibility("A&B&C"), 3); - + // m5 has a different value than m1 Mutation m5 = new Mutation("r1"); - + m5.put("cf1", "cq1", "v1"); m5.put("cf1", "cq1", new ColumnVisibility("A&B"), "v2"); m5.put("cf1", "cq1", 3, "v4"); @@ -599,8 +598,8 @@ public class MutationTest { Mutation m2 = new Mutation(tm1); assertEquals(m1, m2); } - - @Test(expected=IllegalArgumentException.class) + + @Test(expected = IllegalArgumentException.class) public void testThrift_Invalid() { Mutation m1 = new Mutation("r1"); m1.put("cf1", "cq1", "v1"); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/data/OldMutation.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/OldMutation.java b/core/src/test/java/org/apache/accumulo/core/data/OldMutation.java index 5f6f993..a40f4e0 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/OldMutation.java +++ b/core/src/test/java/org/apache/accumulo/core/data/OldMutation.java @@ -35,46 +35,46 @@ import org.apache.hadoop.io.Writable; * Will read/write old mutations. */ public class OldMutation implements Writable { - + static final int VALUE_SIZE_COPY_CUTOFF = 1 << 15; - + private byte[] row; private byte[] data; private int entries; private List<byte[]> values; - + // created this little class instead of using ByteArrayOutput stream and DataOutputStream // because both are synchronized... lots of small syncs slow things down private static class ByteBuffer { - + int offset; byte data[] = new byte[64]; - + private void reserve(int l) { if (offset + l > data.length) { int newSize = data.length * 2; while (newSize <= offset + l) newSize = newSize * 2; - + byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, offset); data = newData; } - + } - + void add(byte[] b) { reserve(b.length); System.arraycopy(b, 0, data, offset, b.length); offset += b.length; } - + public void add(byte[] bytes, int off, int length) { reserve(length); System.arraycopy(bytes, off, data, offset, length); offset += length; } - + void add(boolean b) { reserve(1); if (b) @@ -82,7 +82,7 @@ public class OldMutation implements Writable { else data[offset++] = 0; } - + void add(long v) { reserve(8); data[offset++] = (byte) (v >>> 56); @@ -94,7 +94,7 @@ public class OldMutation implements Writable { data[offset++] = (byte) (v >>> 8); data[offset++] = (byte) (v >>> 0); } - + void add(int i) { reserve(4); data[offset++] = (byte) (i >>> 24); @@ -102,69 +102,69 @@ public class OldMutation implements Writable { data[offset++] = (byte) (i >>> 8); data[offset++] = (byte) (i >>> 0); } - + public byte[] toArray() { byte ret[] = new byte[offset]; System.arraycopy(data, 0, ret, 0, offset); return ret; } - + } - + private static class SimpleReader { int offset; byte data[]; - + SimpleReader(byte b[]) { this.data = b; } - + int readInt() { return (data[offset++] << 24) + ((data[offset++] & 255) << 16) + ((data[offset++] & 255) << 8) + ((data[offset++] & 255) << 0); - + } - + long readLong() { return (((long) data[offset++] << 56) + ((long) (data[offset++] & 255) << 48) + ((long) (data[offset++] & 255) << 40) + ((long) (data[offset++] & 255) << 32) + ((long) (data[offset++] & 255) << 24) + ((data[offset++] & 255) << 16) + ((data[offset++] & 255) << 8) + ((data[offset++] & 255) << 0)); } - + void readBytes(byte b[]) { System.arraycopy(data, offset, b, 0, b.length); offset += b.length; } - + boolean readBoolean() { return (data[offset++] == 1); } - + } - + private ByteBuffer buffer; - + private List<ColumnUpdate> updates; - + private static final byte[] EMPTY_BYTES = new byte[0]; - + private void serialize() { if (buffer != null) { data = buffer.toArray(); buffer = null; } } - + public OldMutation(Text row) { this.row = new byte[row.getLength()]; System.arraycopy(row.getBytes(), 0, this.row, 0, row.getLength()); buffer = new ByteBuffer(); } - + public OldMutation(CharSequence row) { this(new Text(row.toString())); } - + public OldMutation() {} - + public OldMutation(TMutation tmutation) { this.row = ByteBufferUtil.toBytes(tmutation.row); this.data = ByteBufferUtil.toBytes(tmutation.data); @@ -178,45 +178,45 @@ public class OldMutation implements Writable { throw new IllegalArgumentException("null serialized data"); } } - + public byte[] getRow() { return row; } - + private void put(byte b[]) { buffer.add(b.length); buffer.add(b); } - + private void put(Text t) { buffer.add(t.getLength()); buffer.add(t.getBytes(), 0, t.getLength()); } - + private void put(boolean b) { buffer.add(b); } - + private void put(int i) { buffer.add(i); } - + private void put(long l) { buffer.add(l); } - + private void put(Text cf, Text cq, byte[] cv, boolean hasts, long ts, boolean deleted, byte[] val) { - + if (buffer == null) throw new IllegalStateException("Can not add to mutation after serializing it"); - + put(cf); put(cq); put(cv); put(hasts); put(ts); put(deleted); - + if (val.length < VALUE_SIZE_COPY_CUTOFF) { put(val); } else { @@ -227,129 +227,129 @@ public class OldMutation implements Writable { values.add(copy); put(-1 * values.size()); } - + entries++; } - + private void put(CharSequence cf, CharSequence cq, byte[] cv, boolean hasts, long ts, boolean deleted, byte[] val) { put(new Text(cf.toString()), new Text(cq.toString()), cv, hasts, ts, deleted, val); } - + private void put(CharSequence cf, CharSequence cq, byte[] cv, boolean hasts, long ts, boolean deleted, CharSequence val) { put(cf, cq, cv, hasts, ts, deleted, TextUtil.getBytes(new Text(val.toString()))); } - + public void put(Text columnFamily, Text columnQualifier, Value value) { put(columnFamily, columnQualifier, EMPTY_BYTES, false, 0l, false, value.get()); } - + public void put(Text columnFamily, Text columnQualifier, ColumnVisibility columnVisibility, Value value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), false, 0l, false, value.get()); } - + public void put(Text columnFamily, Text columnQualifier, long timestamp, Value value) { put(columnFamily, columnQualifier, EMPTY_BYTES, true, timestamp, false, value.get()); } - + public void put(Text columnFamily, Text columnQualifier, ColumnVisibility columnVisibility, long timestamp, Value value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), true, timestamp, false, value.get()); } - + public void putDelete(Text columnFamily, Text columnQualifier) { put(columnFamily, columnQualifier, EMPTY_BYTES, false, 0l, true, EMPTY_BYTES); } - + public void putDelete(Text columnFamily, Text columnQualifier, ColumnVisibility columnVisibility) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), false, 0l, true, EMPTY_BYTES); } - + public void putDelete(Text columnFamily, Text columnQualifier, long timestamp) { put(columnFamily, columnQualifier, EMPTY_BYTES, true, timestamp, true, EMPTY_BYTES); } - + public void putDelete(Text columnFamily, Text columnQualifier, ColumnVisibility columnVisibility, long timestamp) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), true, timestamp, true, EMPTY_BYTES); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, Value value) { put(columnFamily, columnQualifier, EMPTY_BYTES, false, 0l, false, value.get()); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility, Value value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), false, 0l, false, value.get()); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, long timestamp, Value value) { put(columnFamily, columnQualifier, EMPTY_BYTES, true, timestamp, false, value.get()); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility, long timestamp, Value value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), true, timestamp, false, value.get()); } - + public void putDelete(CharSequence columnFamily, CharSequence columnQualifier) { put(columnFamily, columnQualifier, EMPTY_BYTES, false, 0l, true, EMPTY_BYTES); } - + public void putDelete(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), false, 0l, true, EMPTY_BYTES); } - + public void putDelete(CharSequence columnFamily, CharSequence columnQualifier, long timestamp) { put(columnFamily, columnQualifier, EMPTY_BYTES, true, timestamp, true, EMPTY_BYTES); } - + public void putDelete(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility, long timestamp) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), true, timestamp, true, EMPTY_BYTES); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, CharSequence value) { put(columnFamily, columnQualifier, EMPTY_BYTES, false, 0l, false, value); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility, CharSequence value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), false, 0l, false, value); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, long timestamp, CharSequence value) { put(columnFamily, columnQualifier, EMPTY_BYTES, true, timestamp, false, value); } - + public void put(CharSequence columnFamily, CharSequence columnQualifier, ColumnVisibility columnVisibility, long timestamp, CharSequence value) { put(columnFamily, columnQualifier, columnVisibility.getExpression(), true, timestamp, false, value); } - + private byte[] readBytes(SimpleReader in) { int len = in.readInt(); if (len == 0) return EMPTY_BYTES; - + byte bytes[] = new byte[len]; in.readBytes(bytes); return bytes; } - + public List<ColumnUpdate> getUpdates() { serialize(); - + SimpleReader in = new SimpleReader(data); - + if (updates == null) { if (entries == 1) { updates = Collections.singletonList(deserializeColumnUpdate(in)); } else { ColumnUpdate[] tmpUpdates = new ColumnUpdate[entries]; - + for (int i = 0; i < entries; i++) tmpUpdates[i] = deserializeColumnUpdate(in); - + updates = Arrays.asList(tmpUpdates); } } - + return updates; } - + private ColumnUpdate deserializeColumnUpdate(SimpleReader in) { byte[] cf = readBytes(in); byte[] cq = readBytes(in); @@ -357,10 +357,10 @@ public class OldMutation implements Writable { boolean hasts = in.readBoolean(); long ts = in.readLong(); boolean deleted = in.readBoolean(); - + byte[] val; int valLen = in.readInt(); - + if (valLen < 0) { val = values.get((-1 * valLen) - 1); } else if (valLen == 0) { @@ -369,44 +369,44 @@ public class OldMutation implements Writable { val = new byte[valLen]; in.readBytes(val); } - + return new ColumnUpdate(cf, cq, cv, hasts, ts, deleted, val); } - + private int cachedValLens = -1; - + long getValueLengths() { if (values == null) return 0; - + if (cachedValLens == -1) { int tmpCVL = 0; for (byte[] val : values) tmpCVL += val.length; - + cachedValLens = tmpCVL; } - + return cachedValLens; - + } - + public long numBytes() { serialize(); return row.length + data.length + getValueLengths(); } - + public long estimatedMemoryUsed() { return numBytes() + 230; } - + /** * @return the number of column value pairs added to the mutation */ public int size() { return entries; } - + @Override public void readFields(DataInput in) throws IOException { // Clear out cached column updates and value lengths so @@ -415,7 +415,7 @@ public class OldMutation implements Writable { updates = null; cachedValLens = -1; buffer = null; - + int len = in.readInt(); row = new byte[len]; in.readFully(row); @@ -423,7 +423,7 @@ public class OldMutation implements Writable { data = new byte[len]; in.readFully(data); entries = in.readInt(); - + boolean valuesPresent = in.readBoolean(); if (!valuesPresent) { values = null; @@ -438,7 +438,7 @@ public class OldMutation implements Writable { } } } - + @Override public void write(DataOutput out) throws IOException { serialize(); @@ -447,7 +447,7 @@ public class OldMutation implements Writable { out.writeInt(data.length); out.write(data); out.writeInt(entries); - + if (values == null) out.writeBoolean(false); else { @@ -459,21 +459,21 @@ public class OldMutation implements Writable { out.write(val); } } - + } - + @Override public boolean equals(Object o) { if (o instanceof OldMutation) return equals((OldMutation) o); return false; } - + @Override public int hashCode() { return toThrift().hashCode(); } - + public boolean equals(OldMutation m) { serialize(); if (!Arrays.equals(row, m.getRow())) @@ -490,10 +490,10 @@ public class OldMutation implements Writable { } return false; } - + public TMutation toThrift() { serialize(); return new TMutation(java.nio.ByteBuffer.wrap(row), java.nio.ByteBuffer.wrap(data), ByteBufferUtil.toByteBuffers(values), entries); } - + } http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/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 index 0aa7649..9a9e21d 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java @@ -18,6 +18,15 @@ package org.apache.accumulo.core.data; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -29,9 +38,6 @@ 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(UTF_8); @@ -54,22 +60,22 @@ public class ValueTest { assertEquals(0, v.get().length); } - @Test(expected=NullPointerException.class) + @Test(expected = NullPointerException.class) public void testNullBytesConstructor() { new Value((byte[]) null); } - @Test(expected=NullPointerException.class) + @Test(expected = NullPointerException.class) public void testNullCopyConstructor() { new Value((Value) null); } - @Test(expected=NullPointerException.class) + @Test(expected = NullPointerException.class) public void testNullByteBufferConstructor() { - new Value((ByteBuffer)null); + new Value((ByteBuffer) null); } - @Test(expected=NullPointerException.class) + @Test(expected = NullPointerException.class) public void testNullSet() { Value v = new Value(); v.set(null); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/file/FileOperationsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/file/FileOperationsTest.java b/core/src/test/java/org/apache/accumulo/core/file/FileOperationsTest.java index 30c667d..3fdeb8a 100644 --- a/core/src/test/java/org/apache/accumulo/core/file/FileOperationsTest.java +++ b/core/src/test/java/org/apache/accumulo/core/file/FileOperationsTest.java @@ -30,15 +30,15 @@ import org.apache.hadoop.fs.FileSystem; import org.junit.Test; public class FileOperationsTest { - + public FileOperationsTest() {} - + /** * Test for filenames with +1 dot */ @Test public void handlesFilenamesWithMoreThanOneDot() throws IOException { - + Boolean caughtException = false; FileSKVWriter writer = null; String filename = "target/test.file." + RFile.EXTENSION; @@ -61,7 +61,7 @@ public class FileOperationsTest { } FileUtils.forceDelete(testFile); } - + assertFalse("Should not throw with more than 1 dot in filename.", caughtException); } } http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestCachedBlockQueue.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestCachedBlockQueue.java b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestCachedBlockQueue.java index 19f279b..940fcf8 100644 --- a/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestCachedBlockQueue.java +++ b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestCachedBlockQueue.java @@ -24,9 +24,9 @@ import java.util.LinkedList; import junit.framework.TestCase; public class TestCachedBlockQueue extends TestCase { - + public void testQueue() throws Exception { - + CachedBlock cb1 = new CachedBlock(1000, "cb1", 1); CachedBlock cb2 = new CachedBlock(1500, "cb2", 2); CachedBlock cb3 = new CachedBlock(1000, "cb3", 3); @@ -37,9 +37,9 @@ public class TestCachedBlockQueue extends TestCase { CachedBlock cb8 = new CachedBlock(1500, "cb8", 8); CachedBlock cb9 = new CachedBlock(1000, "cb9", 9); CachedBlock cb10 = new CachedBlock(1500, "cb10", 10); - + CachedBlockQueue queue = new CachedBlockQueue(10000, 1000); - + queue.add(cb1); queue.add(cb2); queue.add(cb3); @@ -50,12 +50,12 @@ public class TestCachedBlockQueue extends TestCase { queue.add(cb8); queue.add(cb9); queue.add(cb10); - + // We expect cb1 through cb8 to be in the queue long expectedSize = cb1.heapSize() + cb2.heapSize() + cb3.heapSize() + cb4.heapSize() + cb5.heapSize() + cb6.heapSize() + cb7.heapSize() + cb8.heapSize(); - + assertEquals(queue.heapSize(), expectedSize); - + LinkedList<org.apache.accumulo.core.file.blockfile.cache.CachedBlock> blocks = queue.getList(); assertEquals(blocks.poll().getName(), "cb1"); assertEquals(blocks.poll().getName(), "cb2"); @@ -65,11 +65,11 @@ public class TestCachedBlockQueue extends TestCase { assertEquals(blocks.poll().getName(), "cb6"); assertEquals(blocks.poll().getName(), "cb7"); assertEquals(blocks.poll().getName(), "cb8"); - + } - + public void testQueueSmallBlockEdgeCase() throws Exception { - + CachedBlock cb1 = new CachedBlock(1000, "cb1", 1); CachedBlock cb2 = new CachedBlock(1500, "cb2", 2); CachedBlock cb3 = new CachedBlock(1000, "cb3", 3); @@ -80,9 +80,9 @@ public class TestCachedBlockQueue extends TestCase { CachedBlock cb8 = new CachedBlock(1500, "cb8", 8); CachedBlock cb9 = new CachedBlock(1000, "cb9", 9); CachedBlock cb10 = new CachedBlock(1500, "cb10", 10); - + CachedBlockQueue queue = new CachedBlockQueue(10000, 1000); - + queue.add(cb1); queue.add(cb2); queue.add(cb3); @@ -93,20 +93,20 @@ public class TestCachedBlockQueue extends TestCase { queue.add(cb8); queue.add(cb9); queue.add(cb10); - + CachedBlock cb0 = new CachedBlock(10 + CachedBlock.PER_BLOCK_OVERHEAD, "cb0", 0); queue.add(cb0); - + // This is older so we must include it, but it will not end up kicking // anything out because (heapSize - cb8.heapSize + cb0.heapSize < maxSize) // and we must always maintain heapSize >= maxSize once we achieve it. - + // We expect cb0 through cb8 to be in the queue long expectedSize = cb1.heapSize() + cb2.heapSize() + cb3.heapSize() + cb4.heapSize() + cb5.heapSize() + cb6.heapSize() + cb7.heapSize() + cb8.heapSize() + cb0.heapSize(); - + assertEquals(queue.heapSize(), expectedSize); - + LinkedList<org.apache.accumulo.core.file.blockfile.cache.CachedBlock> blocks = queue.getList(); assertEquals(blocks.poll().getName(), "cb0"); assertEquals(blocks.poll().getName(), "cb1"); @@ -117,9 +117,9 @@ public class TestCachedBlockQueue extends TestCase { assertEquals(blocks.poll().getName(), "cb6"); assertEquals(blocks.poll().getName(), "cb7"); assertEquals(blocks.poll().getName(), "cb8"); - + } - + private static class CachedBlock extends org.apache.accumulo.core.file.blockfile.cache.CachedBlock { public CachedBlock(long heapSize, String name, long accessTime) { super(name, new byte[(int) (heapSize - CachedBlock.PER_BLOCK_OVERHEAD)], accessTime, false); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestLruBlockCache.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestLruBlockCache.java b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestLruBlockCache.java index 8d6a61a..88d61ac 100644 --- a/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestLruBlockCache.java +++ b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestLruBlockCache.java @@ -27,26 +27,26 @@ import junit.framework.TestCase; /** * Tests the concurrent LruBlockCache. * <p> - * + * * Tests will ensure it grows and shrinks in size properly, evictions run when they're supposed to and do what they should, and that cached blocks are * accessible when expected to be. */ public class TestLruBlockCache extends TestCase { - + public void testBackgroundEvictionThread() throws Exception { - + long maxSize = 100000; long blockSize = calculateBlockSizeDefault(maxSize, 9); // room for 9, will evict - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize); - + Block[] blocks = generateFixedBlocks(10, blockSize, "block"); - + // Add all the blocks for (Block block : blocks) { cache.cacheBlock(block.blockName, block.buf); } - + // Let the eviction run int n = 0; while (cache.getEvictionCount() == 0) { @@ -56,85 +56,85 @@ public class TestLruBlockCache extends TestCase { // A single eviction run should have occurred assertEquals(cache.getEvictionCount(), 1); } - + public void testCacheSimple() throws Exception { - + long maxSize = 1000000; long blockSize = calculateBlockSizeDefault(maxSize, 101); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize); - + Block[] blocks = generateRandomBlocks(100, blockSize); - + long expectedCacheSize = cache.heapSize(); - + // Confirm empty for (Block block : blocks) { assertTrue(cache.getBlock(block.blockName) == null); } - + // Add blocks for (Block block : blocks) { cache.cacheBlock(block.blockName, block.buf); expectedCacheSize += block.heapSize(); } - + // Verify correctly calculated cache heap size assertEquals(expectedCacheSize, cache.heapSize()); - + // Check if all blocks are properly cached and retrieved for (Block block : blocks) { CacheEntry ce = cache.getBlock(block.blockName); assertTrue(ce != null); assertEquals(ce.getBuffer().length, block.buf.length); } - + // Verify correctly calculated cache heap size assertEquals(expectedCacheSize, cache.heapSize()); - + // Check if all blocks are properly cached and retrieved for (Block block : blocks) { CacheEntry ce = cache.getBlock(block.blockName); assertTrue(ce != null); assertEquals(ce.getBuffer().length, block.buf.length); } - + // Expect no evictions assertEquals(0, cache.getEvictionCount()); // Thread t = new LruBlockCache.StatisticsThread(cache); // t.start(); // t.join(); } - + public void testCacheEvictionSimple() throws Exception { - + long maxSize = 100000; long blockSize = calculateBlockSizeDefault(maxSize, 10); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false); - + Block[] blocks = generateFixedBlocks(10, blockSize, "block"); - + long expectedCacheSize = cache.heapSize(); - + // Add all the blocks for (Block block : blocks) { cache.cacheBlock(block.blockName, block.buf); expectedCacheSize += block.heapSize(); } - + // A single eviction run should have occurred assertEquals(1, cache.getEvictionCount()); - + // Our expected size overruns acceptable limit assertTrue(expectedCacheSize > (maxSize * LruBlockCache.DEFAULT_ACCEPTABLE_FACTOR)); - + // But the cache did not grow beyond max assertTrue(cache.heapSize() < maxSize); - + // And is still below the acceptable limit assertTrue(cache.heapSize() < (maxSize * LruBlockCache.DEFAULT_ACCEPTABLE_FACTOR)); - + // All blocks except block 0 and 1 should be in the cache assertTrue(cache.getBlock(blocks[0].blockName) == null); assertTrue(cache.getBlock(blocks[1].blockName) == null); @@ -142,292 +142,292 @@ public class TestLruBlockCache extends TestCase { assertTrue(Arrays.equals(cache.getBlock(blocks[i].blockName).getBuffer(), blocks[i].buf)); } } - + public void testCacheEvictionTwoPriorities() throws Exception { - + long maxSize = 100000; long blockSize = calculateBlockSizeDefault(maxSize, 10); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false, (int) Math.ceil(1.2 * maxSize / blockSize), LruBlockCache.DEFAULT_LOAD_FACTOR, LruBlockCache.DEFAULT_CONCURRENCY_LEVEL, 0.98f, // min 0.99f, // acceptable 0.25f, // single 0.50f, // multi 0.25f);// memory - + Block[] singleBlocks = generateFixedBlocks(5, 10000, "single"); Block[] multiBlocks = generateFixedBlocks(5, 10000, "multi"); - + long expectedCacheSize = cache.heapSize(); - + // Add and get the multi blocks for (Block block : multiBlocks) { cache.cacheBlock(block.blockName, block.buf); expectedCacheSize += block.heapSize(); assertTrue(Arrays.equals(cache.getBlock(block.blockName).getBuffer(), block.buf)); } - + // Add the single blocks (no get) for (Block block : singleBlocks) { cache.cacheBlock(block.blockName, block.buf); expectedCacheSize += block.heapSize(); } - + // A single eviction run should have occurred assertEquals(cache.getEvictionCount(), 1); - + // We expect two entries evicted assertEquals(cache.getEvictedCount(), 2); - + // Our expected size overruns acceptable limit assertTrue(expectedCacheSize > (maxSize * LruBlockCache.DEFAULT_ACCEPTABLE_FACTOR)); - + // But the cache did not grow beyond max assertTrue(cache.heapSize() <= maxSize); - + // And is now below the acceptable limit assertTrue(cache.heapSize() <= (maxSize * LruBlockCache.DEFAULT_ACCEPTABLE_FACTOR)); - + // We expect fairness across the two priorities. // This test makes multi go barely over its limit, in-memory // empty, and the rest in single. Two single evictions and // one multi eviction expected. assertTrue(cache.getBlock(singleBlocks[0].blockName) == null); assertTrue(cache.getBlock(multiBlocks[0].blockName) == null); - + // And all others to be cached for (int i = 1; i < 4; i++) { assertTrue(Arrays.equals(cache.getBlock(singleBlocks[i].blockName).getBuffer(), singleBlocks[i].buf)); assertTrue(Arrays.equals(cache.getBlock(multiBlocks[i].blockName).getBuffer(), multiBlocks[i].buf)); } } - + public void testCacheEvictionThreePriorities() throws Exception { - + long maxSize = 100000; long blockSize = calculateBlockSize(maxSize, 10); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false, (int) Math.ceil(1.2 * maxSize / blockSize), LruBlockCache.DEFAULT_LOAD_FACTOR, LruBlockCache.DEFAULT_CONCURRENCY_LEVEL, 0.98f, // min 0.99f, // acceptable 0.33f, // single 0.33f, // multi 0.34f);// memory - + Block[] singleBlocks = generateFixedBlocks(5, blockSize, "single"); Block[] multiBlocks = generateFixedBlocks(5, blockSize, "multi"); Block[] memoryBlocks = generateFixedBlocks(5, blockSize, "memory"); - + long expectedCacheSize = cache.heapSize(); - + // Add 3 blocks from each priority for (int i = 0; i < 3; i++) { - + // Just add single blocks cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf); expectedCacheSize += singleBlocks[i].heapSize(); - + // Add and get multi blocks cache.cacheBlock(multiBlocks[i].blockName, multiBlocks[i].buf); expectedCacheSize += multiBlocks[i].heapSize(); cache.getBlock(multiBlocks[i].blockName); - + // Add memory blocks as such cache.cacheBlock(memoryBlocks[i].blockName, memoryBlocks[i].buf, true); expectedCacheSize += memoryBlocks[i].heapSize(); - + } - + // Do not expect any evictions yet assertEquals(0, cache.getEvictionCount()); - + // Verify cache size assertEquals(expectedCacheSize, cache.heapSize()); - + // Insert a single block, oldest single should be evicted cache.cacheBlock(singleBlocks[3].blockName, singleBlocks[3].buf); - + // Single eviction, one thing evicted assertEquals(1, cache.getEvictionCount()); assertEquals(1, cache.getEvictedCount()); - + // Verify oldest single block is the one evicted assertEquals(null, cache.getBlock(singleBlocks[0].blockName)); - + // Change the oldest remaining single block to a multi cache.getBlock(singleBlocks[1].blockName); - + // Insert another single block cache.cacheBlock(singleBlocks[4].blockName, singleBlocks[4].buf); - + // Two evictions, two evicted. assertEquals(2, cache.getEvictionCount()); assertEquals(2, cache.getEvictedCount()); - + // Oldest multi block should be evicted now assertEquals(null, cache.getBlock(multiBlocks[0].blockName)); - + // Insert another memory block cache.cacheBlock(memoryBlocks[3].blockName, memoryBlocks[3].buf, true); - + // Three evictions, three evicted. assertEquals(3, cache.getEvictionCount()); assertEquals(3, cache.getEvictedCount()); - + // Oldest memory block should be evicted now assertEquals(null, cache.getBlock(memoryBlocks[0].blockName)); - + // Add a block that is twice as big (should force two evictions) Block[] bigBlocks = generateFixedBlocks(3, blockSize * 3, "big"); cache.cacheBlock(bigBlocks[0].blockName, bigBlocks[0].buf); - + // Four evictions, six evicted (inserted block 3X size, expect +3 evicted) assertEquals(4, cache.getEvictionCount()); assertEquals(6, cache.getEvictedCount()); - + // Expect three remaining singles to be evicted assertEquals(null, cache.getBlock(singleBlocks[2].blockName)); assertEquals(null, cache.getBlock(singleBlocks[3].blockName)); assertEquals(null, cache.getBlock(singleBlocks[4].blockName)); - + // Make the big block a multi block cache.getBlock(bigBlocks[0].blockName); - + // Cache another single big block cache.cacheBlock(bigBlocks[1].blockName, bigBlocks[1].buf); - + // Five evictions, nine evicted (3 new) assertEquals(5, cache.getEvictionCount()); assertEquals(9, cache.getEvictedCount()); - + // Expect three remaining multis to be evicted assertEquals(null, cache.getBlock(singleBlocks[1].blockName)); assertEquals(null, cache.getBlock(multiBlocks[1].blockName)); assertEquals(null, cache.getBlock(multiBlocks[2].blockName)); - + // Cache a big memory block cache.cacheBlock(bigBlocks[2].blockName, bigBlocks[2].buf, true); - + // Six evictions, twelve evicted (3 new) assertEquals(6, cache.getEvictionCount()); assertEquals(12, cache.getEvictedCount()); - + // Expect three remaining in-memory to be evicted assertEquals(null, cache.getBlock(memoryBlocks[1].blockName)); assertEquals(null, cache.getBlock(memoryBlocks[2].blockName)); assertEquals(null, cache.getBlock(memoryBlocks[3].blockName)); - + } - + // test scan resistance public void testScanResistance() throws Exception { - + long maxSize = 100000; long blockSize = calculateBlockSize(maxSize, 10); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false, (int) Math.ceil(1.2 * maxSize / blockSize), LruBlockCache.DEFAULT_LOAD_FACTOR, LruBlockCache.DEFAULT_CONCURRENCY_LEVEL, 0.66f, // min 0.99f, // acceptable 0.33f, // single 0.33f, // multi 0.34f);// memory - + Block[] singleBlocks = generateFixedBlocks(20, blockSize, "single"); Block[] multiBlocks = generateFixedBlocks(5, blockSize, "multi"); - + // Add 5 multi blocks for (Block block : multiBlocks) { cache.cacheBlock(block.blockName, block.buf); cache.getBlock(block.blockName); } - + // Add 5 single blocks for (int i = 0; i < 5; i++) { cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf); } - + // An eviction ran assertEquals(1, cache.getEvictionCount()); - + // To drop down to 2/3 capacity, we'll need to evict 4 blocks assertEquals(4, cache.getEvictedCount()); - + // Should have been taken off equally from single and multi assertEquals(null, cache.getBlock(singleBlocks[0].blockName)); assertEquals(null, cache.getBlock(singleBlocks[1].blockName)); assertEquals(null, cache.getBlock(multiBlocks[0].blockName)); assertEquals(null, cache.getBlock(multiBlocks[1].blockName)); - + // Let's keep "scanning" by adding single blocks. From here on we only // expect evictions from the single bucket. - + // Every time we reach 10 total blocks (every 4 inserts) we get 4 single // blocks evicted. Inserting 13 blocks should yield 3 more evictions and // 12 more evicted. - + for (int i = 5; i < 18; i++) { cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf); } - + // 4 total evictions, 16 total evicted assertEquals(4, cache.getEvictionCount()); assertEquals(16, cache.getEvictedCount()); - + // Should now have 7 total blocks assertEquals(7, cache.size()); - + } - + // test setMaxSize public void testResizeBlockCache() throws Exception { - + long maxSize = 300000; long blockSize = calculateBlockSize(maxSize, 31); - + LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false, (int) Math.ceil(1.2 * maxSize / blockSize), LruBlockCache.DEFAULT_LOAD_FACTOR, LruBlockCache.DEFAULT_CONCURRENCY_LEVEL, 0.98f, // min 0.99f, // acceptable 0.33f, // single 0.33f, // multi 0.34f);// memory - + Block[] singleBlocks = generateFixedBlocks(10, blockSize, "single"); Block[] multiBlocks = generateFixedBlocks(10, blockSize, "multi"); Block[] memoryBlocks = generateFixedBlocks(10, blockSize, "memory"); - + // Add all blocks from all priorities for (int i = 0; i < 10; i++) { - + // Just add single blocks cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf); - + // Add and get multi blocks cache.cacheBlock(multiBlocks[i].blockName, multiBlocks[i].buf); cache.getBlock(multiBlocks[i].blockName); - + // Add memory blocks as such cache.cacheBlock(memoryBlocks[i].blockName, memoryBlocks[i].buf, true); } - + // Do not expect any evictions yet assertEquals(0, cache.getEvictionCount()); - + // Resize to half capacity plus an extra block (otherwise we evict an extra) cache.setMaxSize((long) (maxSize * 0.5f)); - + // Should have run a single eviction assertEquals(1, cache.getEvictionCount()); - + // And we expect 1/2 of the blocks to be evicted assertEquals(15, cache.getEvictedCount()); - + // And the oldest 5 blocks from each category should be gone for (int i = 0; i < 5; i++) { assertEquals(null, cache.getBlock(singleBlocks[i].blockName)); assertEquals(null, cache.getBlock(multiBlocks[i].blockName)); assertEquals(null, cache.getBlock(memoryBlocks[i].blockName)); } - + // And the newest 5 blocks should still be accessible for (int i = 5; i < 10; i++) { assertTrue(Arrays.equals(singleBlocks[i].buf, cache.getBlock(singleBlocks[i].blockName).getBuffer())); @@ -435,7 +435,7 @@ public class TestLruBlockCache extends TestCase { assertTrue(Arrays.equals(memoryBlocks[i].buf, cache.getBlock(memoryBlocks[i].blockName).getBuffer())); } } - + private Block[] generateFixedBlocks(int numBlocks, int size, String pfx) { Block[] blocks = new Block[numBlocks]; for (int i = 0; i < numBlocks; i++) { @@ -443,11 +443,11 @@ public class TestLruBlockCache extends TestCase { } return blocks; } - + private Block[] generateFixedBlocks(int numBlocks, long size, String pfx) { return generateFixedBlocks(numBlocks, (int) size, pfx); } - + private Block[] generateRandomBlocks(int numBlocks, long maxSize) { Block[] blocks = new Block[numBlocks]; Random r = new Random(); @@ -456,7 +456,7 @@ public class TestLruBlockCache extends TestCase { } return blocks; } - + private long calculateBlockSize(long maxSize, int numBlocks) { long roughBlockSize = maxSize / numBlocks; int numEntries = (int) Math.ceil((1.2) * maxSize / roughBlockSize); @@ -466,7 +466,7 @@ public class TestLruBlockCache extends TestCase { negateBlockSize += CachedBlock.PER_BLOCK_OVERHEAD; return ClassSize.align((long) Math.floor((roughBlockSize - negateBlockSize) * 0.99f)); } - + private long calculateBlockSizeDefault(long maxSize, int numBlocks) { long roughBlockSize = maxSize / numBlocks; int numEntries = (int) Math.ceil((1.2) * maxSize / roughBlockSize); @@ -476,16 +476,16 @@ public class TestLruBlockCache extends TestCase { negateBlockSize += CachedBlock.PER_BLOCK_OVERHEAD; return ClassSize.align((long) Math.floor((roughBlockSize - negateBlockSize) * LruBlockCache.DEFAULT_ACCEPTABLE_FACTOR)); } - + private static class Block implements HeapSize { String blockName; byte buf[]; - + Block(String blockName, int size) { this.blockName = blockName; this.buf = new byte[size]; } - + public long heapSize() { return CachedBlock.PER_BLOCK_OVERHEAD + ClassSize.align(blockName.length()) + ClassSize.align(buf.length); } http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/file/rfile/BlockIndexTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/file/rfile/BlockIndexTest.java b/core/src/test/java/org/apache/accumulo/core/file/rfile/BlockIndexTest.java index f293718..1b2b2a6 100644 --- a/core/src/test/java/org/apache/accumulo/core/file/rfile/BlockIndexTest.java +++ b/core/src/test/java/org/apache/accumulo/core/file/rfile/BlockIndexTest.java @@ -31,28 +31,28 @@ import org.junit.Assert; import org.junit.Test; /** - * + * */ public class BlockIndexTest { - + private static class MyCacheEntry implements CacheEntry { Object idx; byte[] data; - + MyCacheEntry(byte[] d) { this.data = d; } - + @Override public void setIndex(Object idx) { this.idx = idx; } - + @Override public Object getIndex() { return idx; } - + @Override public byte[] getBuffer() { return data; @@ -63,43 +63,42 @@ public class BlockIndexTest { public void test1() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos); - + Key prevKey = null; - + int num = 1000; - + for (int i = 0; i < num; i++) { Key key = new Key(RFileTest.nf("", i), "cf1", "cq1"); new RelativeKey(prevKey, key).write(out); new Value(new byte[0]).write(out); prevKey = key; } - + out.close(); final byte[] data = baos.toByteArray(); - + CacheEntry ce = new MyCacheEntry(data); ABlockReader cacheBlock = new CachableBlockFile.CachedBlockRead(ce, data); BlockIndex blockIndex = null; - + for (int i = 0; i < 129; i++) blockIndex = BlockIndex.getIndex(cacheBlock, new IndexEntry(prevKey, num, 0, 0, 0)); - + BlockIndexEntry[] indexEntries = blockIndex.getIndexEntries(); - + for (int i = 0; i < indexEntries.length; i++) { int row = Integer.parseInt(indexEntries[i].getPrevKey().getRowData().toString()); - + BlockIndexEntry bie; - bie = blockIndex.seekBlock(new Key(RFileTest.nf("", row), "cf1", "cq1"), cacheBlock); if (i == 0) Assert.assertSame(null, bie); else Assert.assertSame(indexEntries[i - 1], bie); - + Assert.assertSame(bie, blockIndex.seekBlock(new Key(RFileTest.nf("", row - 1), "cf1", "cq1"), cacheBlock)); bie = blockIndex.seekBlock(new Key(RFileTest.nf("", row + 1), "cf1", "cq1"), cacheBlock); @@ -108,7 +107,7 @@ public class BlockIndexTest { RelativeKey rk = new RelativeKey(); rk.setPrevKey(bie.getPrevKey()); rk.readFields(cacheBlock); - + Assert.assertEquals(rk.getKey(), new Key(RFileTest.nf("", row + 1), "cf1", "cq1")); } @@ -119,56 +118,56 @@ public class BlockIndexTest { public void testSame() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos); - + Key prevKey = null; - + int num = 1000; - + for (int i = 0; i < num; i++) { Key key = new Key(RFileTest.nf("", 1), "cf1", "cq1"); new RelativeKey(prevKey, key).write(out); new Value(new byte[0]).write(out); prevKey = key; } - + for (int i = 0; i < num; i++) { Key key = new Key(RFileTest.nf("", 3), "cf1", "cq1"); new RelativeKey(prevKey, key).write(out); new Value(new byte[0]).write(out); prevKey = key; } - + for (int i = 0; i < num; i++) { Key key = new Key(RFileTest.nf("", 5), "cf1", "cq1"); new RelativeKey(prevKey, key).write(out); new Value(new byte[0]).write(out); prevKey = key; } - + out.close(); final byte[] data = baos.toByteArray(); - + CacheEntry ce = new MyCacheEntry(data); ABlockReader cacheBlock = new CachableBlockFile.CachedBlockRead(ce, data); BlockIndex blockIndex = null; - + for (int i = 0; i < 257; i++) blockIndex = BlockIndex.getIndex(cacheBlock, new IndexEntry(prevKey, num, 0, 0, 0)); - + Assert.assertSame(null, blockIndex.seekBlock(new Key(RFileTest.nf("", 0), "cf1", "cq1"), cacheBlock)); Assert.assertSame(null, blockIndex.seekBlock(new Key(RFileTest.nf("", 1), "cf1", "cq1"), cacheBlock)); - + for (int i = 2; i < 6; i++) { Key seekKey = new Key(RFileTest.nf("", i), "cf1", "cq1"); BlockIndexEntry bie = blockIndex.seekBlock(seekKey, cacheBlock); - + Assert.assertTrue(bie.getPrevKey().compareTo(seekKey) < 0); RelativeKey rk = new RelativeKey(); rk.setPrevKey(bie.getPrevKey()); rk.readFields(cacheBlock); - + Assert.assertTrue(rk.getKey().compareTo(seekKey) <= 0); } cacheBlock.close(); http://git-wip-us.apache.org/repos/asf/accumulo/blob/6bc67602/core/src/test/java/org/apache/accumulo/core/file/rfile/CreateCompatTestFile.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/file/rfile/CreateCompatTestFile.java b/core/src/test/java/org/apache/accumulo/core/file/rfile/CreateCompatTestFile.java index 46c2f0e..3eadc06 100644 --- a/core/src/test/java/org/apache/accumulo/core/file/rfile/CreateCompatTestFile.java +++ b/core/src/test/java/org/apache/accumulo/core/file/rfile/CreateCompatTestFile.java @@ -30,54 +30,54 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CreateCompatTestFile { - + public static Set<ByteSequence> ncfs(String... colFams) { HashSet<ByteSequence> cfs = new HashSet<ByteSequence>(); - + for (String cf : colFams) { cfs.add(new ArrayByteSequence(cf)); } - + return cfs; } - + private static Key nk(String row, String cf, String cq, String cv, long ts) { return new Key(row.getBytes(), cf.getBytes(), cq.getBytes(), cv.getBytes(), ts); } - + private static Value nv(String val) { return new Value(val.getBytes()); } - + private static String nf(String prefix, int i) { return String.format(prefix + "%06d", i); } - + public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); CachableBlockFile.Writer _cbw = new CachableBlockFile.Writer(fs, new Path(args[0]), "gz", conf, AccumuloConfiguration.getDefaultConfiguration()); RFile.Writer writer = new RFile.Writer(_cbw, 1000); - + writer.startNewLocalityGroup("lg1", ncfs(nf("cf_", 1), nf("cf_", 2))); - + for (int i = 0; i < 1000; i++) { writer.append(nk(nf("r_", i), nf("cf_", 1), nf("cq_", 0), "", 1000 - i), nv(i + "")); writer.append(nk(nf("r_", i), nf("cf_", 2), nf("cq_", 0), "", 1000 - i), nv(i + "")); } - + writer.startNewLocalityGroup("lg2", ncfs(nf("cf_", 3))); - + for (int i = 0; i < 1000; i++) { writer.append(nk(nf("r_", i), nf("cf_", 3), nf("cq_", 0), "", 1000 - i), nv(i + "")); } - + writer.startDefaultLocalityGroup(); - + for (int i = 0; i < 1000; i++) { writer.append(nk(nf("r_", i), nf("cf_", 4), nf("cq_", 0), "", 1000 - i), nv(i + "")); } - + writer.close(); _cbw.close(); }