FEATURE: Add pluggable compression codec support with Snappy algorithm#1074
FEATURE: Add pluggable compression codec support with Snappy algorithm#1074jhpark816 merged 1 commit intonaver:developfrom
Conversation
b356b4a to
c33eb69
Compare
| byte[] result = bos.toByteArray(); | ||
| getLogger().debug("Compressed %d bytes to %d", in.length, result.length); | ||
| return result; |
There was a problem hiding this comment.
여기를 try 문 안으로 이동할 수 있나요?
There was a problem hiding this comment.
저도 말씀해주신대로 try-catch 블록 안에 bos.toByteArray() 를 옮기고싶었는데, 그렇게 하게 되었을 때 정상적인 결과를 얻을 수 없습니다.
원인을 찾아보니 GZIPOutputStream(gz) 에서 close가 정상적으로 되어야만 ByteArrayOutputStream(bos) 에서 bos.toByteArray() 가 호출이 가능해집니다.
따라서, try-catch 블록 안으로 옮기기 위해서는 아래와 같은 코드로 변경해야하는데 어떤 방식이 더 적합할까요?
@Override
public byte[] compress(byte[] in) {
if (in == null) {
throw new NullPointerException("Can't compress null");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GZIPOutputStream gz = new GZIPOutputStream(bos)) {
gz.write(in);
gz.finish(); // or gz.close();
byte[] result = bos.toByteArray();
getLogger().debug("Compressed %d bytes to %d", in.length, result.length);
return result;
} catch (IOException e) {
throw new RuntimeException("IO exception compressing data", e);
}
}저는 try-with-resources를 통해서 자원을 자동으로 반납한 후에 데이터를 가져오는 방향으로 구현했던 것이었습니다.
There was a problem hiding this comment.
snappy codec과 일관성이 맞지 않아 코멘트한 것이었으나, 로직이 부가적으로 필요하다면 지금대로 두는게 낫습니다.
971c452 to
f83da33
Compare
f83da33 to
b984695
Compare
| @@ -0,0 +1,12 @@ | |||
| package net.spy.memcached.transcoders.compression; | |||
|
|
|||
| public interface CompressionCodecIF { | |||
There was a problem hiding this comment.
막상 코드를 보니, "IF" 문자를 제거하는 것이 자연스러울 것 같습니다.
예전에 넣자고 했는 데, 지금은 다시 제외하는 것이 나을 것 같습니다.
어떤가요?
There was a problem hiding this comment.
구현체(GZIPCompressionCodec, SnappyCompressionCodec)가 명확하게 분리되어 있어서 IF 문자를 없애는 방향도 좋아보입니다.
There was a problem hiding this comment.
@oliviarla @f1v3-dev
여기서는 IF 문자를 제거하는 것이 좋겠습니다.
b984695 to
acf59cf
Compare
🔗 Related Issue
⌨️ What I did
압축 인터페이스 도입 및 Snappy 알고리즘을 추가합니다.
CompressionUtils.java에 하드코딩된 GZIP 압축 방식을CompressionCodecIF인터페이스 기반으로 변경합니다.BaseSerializingTranscoder,GenericJsonSerializingTranscoder,JsonSerializingTranscoder에서 생성자(Builder)를 통해 압축 코덱을 주입받도록 변경합니다.