Skip to content

Commit

Permalink
feat: optimize KeyCodec (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 3, 2025
1 parent 82b3db5 commit 14ace96
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.constants.EmbeddedStorageConstants.*;

/**
* Created by caojiajun on 2025/1/3
*/
Expand Down Expand Up @@ -80,19 +82,26 @@ public static byte[] encodeBucket(Map<BytesKey, KeyInfo> keys) {
pack.getBuffer().capacity(pack.getBuffer().readableBytes());
byte[] array = pack.getBuffer().array();
short decompressLen = (short) array.length;
CompressType compressType = CompressType.zstd;
ICompressor compressor = CompressUtils.get(compressType);
byte[] compressed = compressor.compress(array, 0, array.length);
if (compressed.length > array.length) {
CompressType compressType;
if (decompressLen + 9 > _4k) {//4k装不下,才压缩
compressType = CompressType.zstd;
} else {
compressType = CompressType.none;
compressed = array;
}
if (compressed.length + 9 > EmbeddedStorageConstants._4k) {
return null;
byte[] compressed;
short compressLen;
if (compressType == CompressType.none) {
compressed = array;
compressLen = (short) compressed.length;
} else {
ICompressor compressor = CompressUtils.get(compressType);
compressed = compressor.compress(array, 0, array.length);
compressLen = (short) compressed.length;
if (compressed.length + 9 > _4k) {
return null;
}
}
short compressLen = (short) compressed.length;

ByteBuffer buffer = ByteBuffer.allocate(EmbeddedStorageConstants._4k);
ByteBuffer buffer = ByteBuffer.allocate(_4k);
buffer.putInt(0);//0,1,2,3
buffer.putShort(decompressLen);//4,5
buffer.putShort(compressLen);//6,7
Expand Down

0 comments on commit 14ace96

Please sign in to comment.