Skip to content

Commit

Permalink
[Improvement] Make codec to be a singleton. (#840)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
Codec will be created for many times. We should make codec to be a singleton.  I make codec to be a singleton.

### Why are the changes needed?
Fix: #389

### Does this PR introduce _any_ user-facing change?
No.

### How was this patch tested?
  UT

Co-authored-by: tobehardest <cvchen587@163.com>
  • Loading branch information
tobehardest and tobehardest committed Apr 25, 2023
1 parent 4a17a58 commit c58110f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public static Codec newInstance(RssConf rssConf) {
Type type = rssConf.get(COMPRESSION_TYPE);
switch (type) {
case ZSTD:
return new ZstdCodec(rssConf.get(ZSTD_COMPRESSION_LEVEL));
return ZstdCodec.getInstance(rssConf.get(ZSTD_COMPRESSION_LEVEL));
case SNAPPY:
return new SnappyCodec();
return SnappyCodec.getInstance();
case NOOP:
return new NoOpCodec();
return NoOpCodec.getInstance();
case LZ4:
default:
return new Lz4Codec();
return Lz4Codec.getInstance();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public class Lz4Codec extends Codec {

private LZ4Factory lz4Factory;

private static class LazyHolder {
static final Lz4Codec INSTANCE = new Lz4Codec();
}

public static Lz4Codec getInstance() {
return LazyHolder.INSTANCE;
}

public Lz4Codec() {
this.lz4Factory = LZ4Factory.fastestInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

public class NoOpCodec extends Codec {

private static class LazyHolder {
static final NoOpCodec INSTANCE = new NoOpCodec();
}

public static NoOpCodec getInstance() {
return LazyHolder.INSTANCE;
}

@Override
public void decompress(ByteBuffer src, int uncompressedLen, ByteBuffer dest, int destOffset) {
dest.put(src);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
import org.apache.uniffle.common.exception.RssException;

public class SnappyCodec extends Codec {

private static class LazyHolder {
static final SnappyCodec INSTANCE = new SnappyCodec();
}

public static SnappyCodec getInstance() {
return LazyHolder.INSTANCE;
}

@Override
public void decompress(ByteBuffer src, int uncompressedLen, ByteBuffer dest, int destOffset) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
public class ZstdCodec extends Codec {
private static final Logger LOGGER = LoggerFactory.getLogger(ZstdCodec.class);

private final int compressionLevel;
private int compressionLevel;

public ZstdCodec(int level) {
this.compressionLevel = level;
private static class LazyHolder {
static final ZstdCodec INSTANCE = new ZstdCodec();
}

public static ZstdCodec getInstance(int level) {
LazyHolder.INSTANCE.compressionLevel = level;
return LazyHolder.INSTANCE;
}

@Override
Expand Down

0 comments on commit c58110f

Please sign in to comment.