Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of default properties of blosc compressor. #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/main/java/com/bc/zarr/CompressorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void uncompress(InputStream is, OutputStream os) throws IOException {
}
}

static class BloscCompressor extends Compressor {
public static class BloscCompressor extends Compressor {

final static int AUTOSHUFFLE = -1;
final static int NOSHUFFLE = 0;
Expand All @@ -223,21 +223,24 @@ static class BloscCompressor extends Compressor {
public final static int defaultShuffle = BYTESHUFFLE;
public final static String keyBlocksize = "blocksize";
public final static int defaultBlocksize = 0;
public final static String keyNumThreads = "nthreads";
public final static int defaultNumThreads = 1;
public final static int[] supportedShuffle = new int[]{/*AUTOSHUFFLE, */NOSHUFFLE, BYTESHUFFLE, BITSHUFFLE};
public final static String[] supportedCnames = new String[]{"zstd", "blosclz", defaultCname, "lz4hc", "zlib"/*, "snappy"*/};

public final static Map<String, Object> defaultProperties = Collections
.unmodifiableMap(new HashMap<String, Object>() {{
public final static Map<String, Object> defaultProperties = new HashMap<String, Object>() {{
put(keyCname, defaultCname);
put(keyClevel, defaultCLevel);
put(keyShuffle, defaultShuffle);
put(keyBlocksize, defaultBlocksize);
}});
put(keyNumThreads, defaultNumThreads);
}};

private final int clevel;
private final int blocksize;
private final int shuffle;
private final String cname;
private final int nthreads;

private BloscCompressor(Map<String, Object> map) {
final Object cnameObj = map.get(keyCname);
Expand Down Expand Up @@ -285,6 +288,16 @@ private BloscCompressor(Map<String, Object> map) {
} else {
this.blocksize = ((Number) blocksizeObj).intValue();
}

Object nthreadsObj = map.get(keyNumThreads);
if (nthreadsObj == null) {
nthreadsObj = defaultProperties.get(keyNumThreads);
}
if (nthreadsObj instanceof String) {
this.nthreads = Integer.parseInt((String) nthreadsObj);
} else {
this.nthreads = ((Number) nthreadsObj).intValue();
}
}

@Override
Expand All @@ -308,6 +321,10 @@ public String getCname() {
return cname;
}

public int getNumThreads() {
return nthreads;
}

@Override
public String toString() {
return "compressor=" + getId()
Expand All @@ -324,7 +341,7 @@ public void compress(InputStream is, OutputStream os) throws IOException {
final int outputSize = inputSize + JBlosc.OVERHEAD;
final ByteBuffer inputBuffer = ByteBuffer.wrap(inputBytes);
final ByteBuffer outBuffer = ByteBuffer.allocate(outputSize);
final int i = JBlosc.compressCtx(clevel, shuffle, 1, inputBuffer, inputSize, outBuffer, outputSize, cname, blocksize, 1);
final int i = JBlosc.compressCtx(clevel, shuffle, 1, inputBuffer, inputSize, outBuffer, outputSize, cname, blocksize, nthreads);
final BufferSizes bs = cbufferSizes(outBuffer);
byte[] compressedChunk = Arrays.copyOfRange(outBuffer.array(), 0, (int) bs.getCbytes());
os.write(compressedChunk);
Expand All @@ -341,7 +358,7 @@ public void uncompress(InputStream is, OutputStream os) throws IOException {
byte[] inBytes = Arrays.copyOf(header, compressedSize);
di.readFully(inBytes, header.length, compressedSize - header.length);
ByteBuffer outBuffer = ByteBuffer.allocate(uncompressedSize);
JBlosc.decompressCtx(ByteBuffer.wrap(inBytes), outBuffer, outBuffer.limit(), 1);
JBlosc.decompressCtx(ByteBuffer.wrap(inBytes), outBuffer, outBuffer.limit(), nthreads);
os.write(outBuffer.array());
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/bc/zarr/CompressorFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ public class CompressorFactoryTest {
public void getDefaultCompressorProperties() {
final Map<String, Object> map = CompressorFactory.getDefaultCompressorProperties();
assertNotNull(map);
assertEquals(5, map.size());
assertEquals(6, map.size());
assertThat(map.containsKey("id"), is(true));
assertThat(map.containsKey("cname"), is(true));
assertThat(map.containsKey("clevel"), is(true));
assertThat(map.containsKey("blocksize"), is(true));
assertThat(map.containsKey("shuffle"), is(true));
assertThat(map.containsKey("nthreads"), is(true));

assertThat(map.get("id"), is("blosc"));
assertThat(map.get("cname"), is("lz4"));
assertThat(map.get("clevel"), is(5));
assertThat(map.get("blocksize"), is(0));
assertThat(map.get("shuffle"), is(1));
assertThat(map.get("nthreads"), is(1));
}

@Test
Expand Down