diff --git a/src/main/java/bio/guoda/preston/cmd/CmdCopyTo.java b/src/main/java/bio/guoda/preston/cmd/CmdCopyTo.java index 94a9ebe1..faa1a631 100644 --- a/src/main/java/bio/guoda/preston/cmd/CmdCopyTo.java +++ b/src/main/java/bio/guoda/preston/cmd/CmdCopyTo.java @@ -83,8 +83,8 @@ public void on(Triple statement) { } } stopWatch.stop(); - System.out.println("\tcopying [100%]."); - System.out.println("copying [" + IRIStrings.size() + "] datasets from [" + source.getAbsolutePath() + "] to [" + targetDir + "] completed in [" + stopWatch.getTime(TimeUnit.MINUTES) + "] min."); + System.out.println("\tcopying done."); + System.out.println("Copied [" + IRIStrings.size() + "] datasets from [" + source.getAbsolutePath() + "] to [" + targetDir + "] in [" + stopWatch.getTime(TimeUnit.MINUTES) + "] minutes."); } static String formatProgress(long i, long total) { diff --git a/src/main/java/bio/guoda/preston/store/HashKeyUtil.java b/src/main/java/bio/guoda/preston/store/HashKeyUtil.java new file mode 100644 index 00000000..377257f0 --- /dev/null +++ b/src/main/java/bio/guoda/preston/store/HashKeyUtil.java @@ -0,0 +1,14 @@ +package bio.guoda.preston.store; + +import bio.guoda.preston.Hasher; +import org.apache.commons.lang3.StringUtils; + +public class HashKeyUtil { + public static void validateHashKey(String hashKey) { + int offset = Hasher.getHashPrefix().length(); + int expectedLength = 8 + offset; + if (StringUtils.length(hashKey) < expectedLength) { + throw new IllegalArgumentException("expected id [" + hashKey + "] of at least [" + expectedLength + "] characters, instead got [" + hashKey.length() + "] characters."); + } + } +} diff --git a/src/main/java/bio/guoda/preston/store/KeyTo3LevelPath.java b/src/main/java/bio/guoda/preston/store/KeyTo3LevelPath.java new file mode 100644 index 00000000..1a77a661 --- /dev/null +++ b/src/main/java/bio/guoda/preston/store/KeyTo3LevelPath.java @@ -0,0 +1,19 @@ +package bio.guoda.preston.store; + +import bio.guoda.preston.Hasher; +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; + +public class KeyTo3LevelPath implements KeyToPath { + @Override + public String toPath(String key) { + HashKeyUtil.validateHashKey(key); + + int offset = Hasher.getHashPrefix().length(); + String u0 = key.substring(offset + 0, offset + 2); + String u1 = key.substring(offset + 2, offset + 4); + return StringUtils.join(Arrays.asList(u0, u1, key.substring(offset)), "/"); + } + +} diff --git a/src/main/java/bio/guoda/preston/store/KeyTo5LevelPath.java b/src/main/java/bio/guoda/preston/store/KeyTo5LevelPath.java index 9d96cc98..0b6a2cee 100644 --- a/src/main/java/bio/guoda/preston/store/KeyTo5LevelPath.java +++ b/src/main/java/bio/guoda/preston/store/KeyTo5LevelPath.java @@ -8,11 +8,9 @@ public class KeyTo5LevelPath implements KeyToPath { @Override public String toPath(String key) { + HashKeyUtil.validateHashKey(key); + int offset = Hasher.getHashPrefix().length(); - int expectedLength = 8 + offset; - if (StringUtils.length(key) < expectedLength) { - throw new IllegalArgumentException("expected id [" + key + "] of at least [" + expectedLength + "] characters"); - } String u0 = key.substring(offset + 0, offset + 2); String u1 = key.substring(offset + 2, offset + 4); String u2 = key.substring(offset + 4, offset + 6); diff --git a/src/test/java/bio/guoda/preston/store/KeyTo3LevelPathTest.java b/src/test/java/bio/guoda/preston/store/KeyTo3LevelPathTest.java new file mode 100644 index 00000000..8275ce42 --- /dev/null +++ b/src/test/java/bio/guoda/preston/store/KeyTo3LevelPathTest.java @@ -0,0 +1,27 @@ +package bio.guoda.preston.store; + +import bio.guoda.preston.Hasher; +import org.apache.commons.rdf.api.IRI; +import org.hamcrest.core.Is; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class KeyTo3LevelPathTest { + + @Test + public void toPath() { + IRI hash = Hasher.calcSHA256("bla"); + assertThat(hash.getIRIString(), is("hash://sha256/4df3c3f68fcc83b27e9d42c90431a72499f17875c81a599b566c9889b9696703")); + + String actualPath = new KeyTo3LevelPath().toPath(hash.getIRIString()); + assertThat(actualPath, Is.is("4d/f3/4df3c3f68fcc83b27e9d42c90431a72499f17875c81a599b566c9889b9696703")); + } + + @Test(expected = IllegalArgumentException.class) + public void toPathTooShort() { + new KeyTo3LevelPath().toPath("too:short"); + } + +} \ No newline at end of file