Skip to content

Commit

Permalink
add three level hashpath to reduce path depth
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpoelen committed Jan 31, 2019
1 parent a5b0ad3 commit 713a426
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/bio/guoda/preston/cmd/CmdCopyTo.java
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions 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.");
}
}
}
19 changes: 19 additions & 0 deletions 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)), "/");
}

}
6 changes: 2 additions & 4 deletions src/main/java/bio/guoda/preston/store/KeyTo5LevelPath.java
Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions 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");
}

}

0 comments on commit 713a426

Please sign in to comment.