Skip to content

Commit

Permalink
fix: KeyCodec (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 3, 2025
1 parent 0c72246 commit 95f2bb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
public class KeyCodec {

/**
* 解码一整个slot
* @param all data
* @return 解码结果
*/
public static Map<BytesKey, KeyInfo> decodeSlot(byte[] all) {
ByteBuffer buffer = ByteBuffer.wrap(all);
int bucketSize = all.length / EmbeddedStorageConstants._4k;
Expand All @@ -40,14 +45,15 @@ public static Map<BytesKey, KeyInfo> decodeSlot(byte[] all) {
*/
public static Map<BytesKey, KeyInfo> decodeBucket(byte[] bytes) {
int crc1 = BytesUtils.toInt(bytes, 0);//0,1,2,3
int crc2 = RedisClusterCRC16Utils.getCRC16(bytes, 5, bytes.length);
int crc2 = RedisClusterCRC16Utils.getCRC16(bytes, 9, bytes.length);
if (crc1 != crc2) {
return new HashMap<>();
}
int decompressLen = BytesUtils.toShort(bytes, 4);//4,5
byte compressType = bytes[6];//6
int compressLen = BytesUtils.toShort(bytes, 6);//6,7
byte compressType = bytes[8];//8
ICompressor compressor = CompressUtils.get(CompressType.getByValue(compressType));
byte[] decompressData = compressor.decompress(bytes, 7, bytes.length - 7, decompressLen);
byte[] decompressData = compressor.decompress(bytes, 9, compressLen, decompressLen);
Unpack unpack = new Unpack(decompressData);
int size = unpack.popVarUint();
Map<BytesKey, KeyInfo> map = new HashMap<>();
Expand Down Expand Up @@ -81,15 +87,20 @@ public static byte[] encodeBucket(Map<BytesKey, KeyInfo> keys) {
compressType = CompressType.none;
compressed = array;
}
if (compressed.length + 5 > EmbeddedStorageConstants._4k) {
if (compressed.length + 9 > EmbeddedStorageConstants._4k) {
return null;
}
int crc = RedisClusterCRC16Utils.getCRC16(compressed, 0, compressed.length);
short compressLen = (short) compressed.length;

ByteBuffer buffer = ByteBuffer.allocate(EmbeddedStorageConstants._4k);
buffer.putInt(crc);
buffer.putShort(decompressLen);
buffer.put(compressType.getType());
buffer.putInt(0);//0,1,2,3
buffer.putShort(decompressLen);//4,5
buffer.putShort(compressLen);//6,7
buffer.put(compressType.getType());//8
buffer.put(compressed);
byte[] bytes = buffer.array();
int crc = RedisClusterCRC16Utils.getCRC16(bytes, 9, bytes.length);
buffer.putInt(0, crc);
return buffer.array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ZstdCompressor implements ICompressor {
private final int compressionLevel;

public ZstdCompressor() {
compressionLevel = Zstd.defaultCompressionLevel();
compressionLevel = Zstd.maxCompressionLevel();
}

@Override
Expand Down

0 comments on commit 95f2bb5

Please sign in to comment.