Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add three level hashpath to reduce path depth
- Loading branch information
jhpoelen
committed
Jan 31, 2019
1 parent
a5b0ad3
commit 713a426
Showing
5 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
19
src/main/java/bio/guoda/preston/store/KeyTo3LevelPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)), "/"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/bio/guoda/preston/store/KeyTo3LevelPathTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |