HBASE-13119 FileLink should implement equals

This commit is contained in:
Enis Soztutar 2015-02-27 17:19:56 -08:00
parent 21f12ce8e5
commit bec2b0d320
3 changed files with 81 additions and 3 deletions

View File

@ -19,8 +19,8 @@
package org.apache.hadoop.hbase.io;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
@ -336,6 +336,7 @@ public class FileLink {
return locations;
}
@Override
public String toString() {
StringBuilder str = new StringBuilder(getClass().getName());
str.append(" locations=[");
@ -472,5 +473,22 @@ public class FileLink {
if (dirPath == null) return false;
return dirPath.getName().startsWith(BACK_REFERENCES_DIRECTORY_PREFIX);
}
@Override
public boolean equals(Object obj) {
// Assumes that the ordering of locations between objects are the same. This is true for the
// current subclasses already (HFileLink, WALLink). Otherwise, we may have to sort the locations
// or keep them presorted
if (this.getClass().equals(obj.getClass())) {
return Arrays.equals(this.locations, ((FileLink) obj).locations);
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(locations);
}
}

View File

@ -20,6 +20,7 @@ package org.apache.hadoop.hbase.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.io.FileNotFoundException;
@ -46,6 +47,41 @@ import org.junit.experimental.categories.Category;
*/
@Category({IOTests.class, MediumTests.class})
public class TestFileLink {
@Test
public void testEquals() {
Path p1 = new Path("/p1");
Path p2 = new Path("/p2");
Path p3 = new Path("/p3");
assertEquals(new FileLink(), new FileLink());
assertEquals(new FileLink(p1), new FileLink(p1));
assertEquals(new FileLink(p1, p2), new FileLink(p1, p2));
assertEquals(new FileLink(p1, p2, p3), new FileLink(p1, p2, p3));
assertNotEquals(new FileLink(p1), new FileLink(p3));
assertNotEquals(new FileLink(p1, p2), new FileLink(p1));
assertNotEquals(new FileLink(p1, p2), new FileLink(p2));
assertNotEquals(new FileLink(p1, p2), new FileLink(p2, p1)); // ordering important!
}
@Test
public void testHashCode() {
Path p1 = new Path("/p1");
Path p2 = new Path("/p2");
Path p3 = new Path("/p3");
assertEquals(new FileLink().hashCode(), new FileLink().hashCode());
assertEquals(new FileLink(p1).hashCode(), new FileLink(p1).hashCode());
assertEquals(new FileLink(p1, p2).hashCode(), new FileLink(p1, p2).hashCode());
assertEquals(new FileLink(p1, p2, p3).hashCode(), new FileLink(p1, p2, p3).hashCode());
assertNotEquals(new FileLink(p1).hashCode(), new FileLink(p3).hashCode());
assertNotEquals(new FileLink(p1, p2).hashCode(), new FileLink(p1).hashCode());
assertNotEquals(new FileLink(p1, p2).hashCode(), new FileLink(p2).hashCode());
assertNotEquals(new FileLink(p1, p2).hashCode(), new FileLink(p2, p1).hashCode()); // ordering
}
/**
* Test, on HDFS, that the FileLink is still readable
* even when the current file gets renamed.

View File

@ -18,7 +18,11 @@
*/
package org.apache.hadoop.hbase.regionserver;
import org.apache.hadoop.hbase.HBaseTestCase;
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
@ -30,7 +34,7 @@ import org.junit.experimental.categories.Category;
* Test HStoreFile
*/
@Category({RegionServerTests.class, SmallTests.class})
public class TestStoreFileInfo extends HBaseTestCase {
public class TestStoreFileInfo {
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
/**
@ -59,5 +63,25 @@ public class TestStoreFileInfo extends HBaseTestCase {
assertFalse("should not be a valid link: " + name, HFileLink.isHFileLink(name));
}
}
@Test
public void testEqualsWithLink() throws IOException {
Path origin = new Path("/origin");
Path tmp = new Path("/tmp");
Path archive = new Path("/archive");
HFileLink link1 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"),
new Path(archive, "f1"));
HFileLink link2 = new HFileLink(new Path(origin, "f1"), new Path(tmp, "f1"),
new Path(archive, "f1"));
StoreFileInfo info1 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
TEST_UTIL.getTestFileSystem(), null, link1);
StoreFileInfo info2 = new StoreFileInfo(TEST_UTIL.getConfiguration(),
TEST_UTIL.getTestFileSystem(), null, link2);
assertEquals(info1, info2);
assertEquals(info1.hashCode(), info2.hashCode());
}
}