Skip to content

Commit

Permalink
Store compressed bytes to save mem
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Apr 4, 2022
1 parent 96c4f7c commit 2703d96
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/seqera/storage/DigestByteArray.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.seqera.storage;

import io.seqera.util.ZipUtils;

/**
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
Expand All @@ -9,13 +11,13 @@ public class DigestByteArray {
private String digest;

public DigestByteArray(byte[] bytes, String mediaType, String digest) {
this.bytes = bytes;
this.bytes = ZipUtils.compress(bytes);
this.mediaType = mediaType;
this.digest = digest;
}

public byte[] getBytes() {
return bytes;
return ZipUtils.decompressAsBytes(bytes);
}

public String getMediaType() {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/io/seqera/util/ZipUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.seqera.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterInputStream;

/**
* Helper class to basic compress/decompress funcionality
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
public class ZipUtils {

public static byte[] compress(InputStream stream) {
try (stream) {
return new DeflaterInputStream(stream).readAllBytes();
}
catch (IOException e) {
throw new RuntimeException("Unable to compress provider stream", e);
}
}

public static byte[] compress(byte[] bytes) {
return compress(new ByteArrayInputStream(bytes));
}

public static byte[] compress(String text) throws IOException {
return compress(new ByteArrayInputStream(text.getBytes()));
}

public static InputStream decompress(byte[] buffer) {
return new InflaterInputStream(new ByteArrayInputStream(buffer));
}

public static String decompressAsString(byte[] buffer) throws IOException {
return new String(decompress(buffer).readAllBytes());
}

public static byte[] decompressAsBytes(byte[] buffer) {
try {
return decompress(buffer).readAllBytes();
}
catch (IOException e) {
throw new RuntimeException("Unable to decompress provider buffer", e);
}
}
}
31 changes: 31 additions & 0 deletions src/test/groovy/io/seqera/util/ZipUtilsTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.seqera.util

import spock.lang.Specification

/**
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
class ZipUtilsTest extends Specification {

def 'should compress/decompress a text'() {
given:
def TEXT = 'Hello world\n' * 10

when:
def buffer = ZipUtils.compress(TEXT)
then:
buffer.size() > 0 && buffer.size() < TEXT.bytes.size()

when:
def copy = ZipUtils.decompressAsString(buffer)
then:
copy == TEXT

when:
def bytes = ZipUtils.decompressAsBytes(buffer)
then:
bytes == TEXT.bytes
}

}

0 comments on commit 2703d96

Please sign in to comment.