Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public enum RedisOpType {

// Bit single
SETBIT(false, 4),
BITCOUNT(false, 4),


// String multi
DEL(true, -2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ private RdbConstant() {
public static final short REDIS_RDB_TYPE_HASH_ZIPLIST = 13;
public static final short REDIS_RDB_TYPE_LIST_QUICKLIST = 14;
public static final short REDIS_RDB_TYPE_STREAM_LISTPACKS = 15;
// public static final short REDIS_RDB_TYPE_BITMAP = 16;
public static final short REDIS_RDB_TYPE_BITMAP = 192;
public static final short REDIS_RDB_TYPE_BITMAP_9 = 16;
public static final short REDIS_RDB_TYPE_CRDT = 200;

// Redis 8.x 新增操作码 (rdb.h)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ enum RdbType {
HASH_ZIPLIST(RdbConstant.REDIS_RDB_TYPE_HASH_ZIPLIST, false, RdbHashZipListParser::new),
LIST_QUICKLIST(RdbConstant.REDIS_RDB_TYPE_LIST_QUICKLIST, false, RdbQuickListParser::new),
STREAM_LISTPACKS(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS, false, RdbStreamListpacksParser::new),
// BITMAP(RdbConstant.REDIS_RDB_TYPE_BITMAP, false, RdbBitmapParser::new),
BITMAP(RdbConstant.REDIS_RDB_TYPE_BITMAP, false, RdbBitmapParser::new),
BITMAP_9(9,RdbConstant.REDIS_RDB_TYPE_BITMAP_9, false, RdbBitmapParser::new),
CRDT(RdbConstant.REDIS_RDB_TYPE_CRDT, false, DefaultRdbCrdtParser::new),
// MODULE_AUX(RdbConstant.REDIS_RDB_OP_CODE_MODULE_AUX),
IDLE(RdbConstant.REDIS_RDB_OP_CODE_IDLE, true, RdbIdleParser::new),
Expand Down Expand Up @@ -132,15 +133,25 @@ enum RdbType {
STREAM_LISTPACKS_3(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS_3, false, RdbStreamListpacks3Parser::new),
STREAM_LISTPACKS_4(RdbConstant.REDIS_RDB_TYPE_STREAM_LISTPACKS_4, false, RdbStreamListpacks4Parser::new);

private int version;
private short code;

private boolean rdbOp;

private Function<RdbParseContext, RdbParser> parserConstructor;

private static final Map<Short, RdbType> types = new HashMap<>();
private static final Map<Integer,Map<Short, RdbType>> versionTypes = new HashMap<>();

RdbType(short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor) {
this.version = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复用RdbType(int version,short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor)?

this.code = code;
this.rdbOp = rdbOp;
this.parserConstructor = parserConstructor;
}

RdbType(int version,short code, boolean rdbOp, Function<RdbParseContext, RdbParser> parserConstructor) {
this.version = version;
this.code = code;
this.rdbOp = rdbOp;
this.parserConstructor = parserConstructor;
Expand All @@ -163,14 +174,28 @@ public RdbParser<?> makeParser(RdbParseContext parserManager) {

static {
for (RdbType rdbType : values()) {
types.put(rdbType.code, rdbType);
if(rdbType.version != 0) {
Map<Short, RdbType> versionMap = versionTypes.computeIfAbsent(rdbType.version, (version) -> new HashMap<>());
versionMap.put(rdbType.code, rdbType);
}else {
types.put(rdbType.code, rdbType);
}
}
}

public static RdbType findByCode(short code) {
return types.get(code);
}

public static RdbType findByCode(int version,short code) {
Map<Short,RdbType> versionMap = versionTypes.getOrDefault(version,types);
RdbType rdbType = versionMap.get(code);
if(rdbType == null){
return findByCode(code);
}
return rdbType;
}

}

String cSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Void read(ByteBuf byteBuf) {

case READ_TYPE:
short type = byteBuf.readUnsignedByte();
RdbParseContext.RdbType newType = RdbParseContext.RdbType.findByCode(type);
RdbParseContext.RdbType newType = RdbParseContext.RdbType.findByCode(rdbVersion,type);
if (currentType == RdbParseContext.RdbType.AUX && newType != RdbParseContext.RdbType.AUX) {
auxFinished.set(true);
notifyAuxEnd(rdbParseContext.getAllAux());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ private void propagateCmdIfNeed(byte[] value) {
RedisOpType.SET,
new byte[][]{RedisOpType.SET.name().getBytes(), context.getKey().get(), value},
context.getKey(), value));

notifyRedisOp(new RedisOpSingleKey(
RedisOpType.BITCOUNT,
new byte[][]{RedisOpType.BITCOUNT.name().getBytes(), context.getKey().get()},
context.getKey(), value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public void testParseCrdtSortedSet() {
Assert.assertEquals("ZADD testSS329 0.2700072245105105 4key89", redisOps.get(19).toString());
}

/*
@Test
public void testParseBitmap() {
ByteBuf byteBuf = Unpooled.wrappedBuffer(rorBitmap);
Expand All @@ -337,17 +336,43 @@ public void testParseBitmap() {
Assert.assertEquals("SELECT 0", redisOps.get(0).toString());
Assert.assertEquals("SET mykey ", redisOps.get(1).toString());

byte[][] bitmapKey2 = redisOps.get(2).buildRawOpArgs();
byte[][] bitmapKey2 = redisOps.get(3).buildRawOpArgs();
Assert.assertEquals("bitmap_key2", new String(bitmapKey2[1]));
Assert.assertEquals(700 / 8 + (700 % 8 == 0 ? 0 : 1),bitmapKey2[2].length);
byte[][] bitmapKey = redisOps.get(3).buildRawOpArgs();
byte[][] bitmapKey = redisOps.get(5).buildRawOpArgs();
Assert.assertEquals("bitmap_key", new String(bitmapKey[1]));
Assert.assertEquals(700 / 8 + (700 % 8 == 0 ? 0 : 1),bitmapKey[2].length);
Assert.assertNotEquals(0, bitmapKey[2][87] & (1 << 3)); // getbit bitmap_key 700 -> 1
Assert.assertNotEquals(0, bitmapKey[2][8] & (1 << 1)); // getbit bitmap_key 70 -> 1
Assert.assertEquals("SET common_key value", redisOps.get(4).toString());
Assert.assertEquals("SET common_key value", redisOps.get(7).toString());
}

@Test
public void testParseRor8Bitmap() {
ByteBuf byteBuf = Unpooled.wrappedBuffer(ror8Bitmap);
while (!parser.isFinish()) {
parser.read(byteBuf);
}

Assert.assertEquals("SELECT 0", redisOps.get(0).toString());

byte[][] bitmapKey2 = redisOps.get(1).buildRawOpArgs();
Assert.assertEquals("bitmap_key2", new String(bitmapKey2[1]));
Assert.assertEquals(700 / 8 + (700 % 8 == 0 ? 0 : 1),bitmapKey2[2].length);
Assert.assertEquals("BITCOUNT bitmap_key2", redisOps.get(2).toString());

Assert.assertEquals("SET common_key value", redisOps.get(3).toString());

Assert.assertEquals("SET mykey aa", redisOps.get(5).toString());

byte[][] bitmapKey = redisOps.get(7).buildRawOpArgs();
Assert.assertEquals("bitmap_key", new String(bitmapKey[1]));
Assert.assertEquals(700 / 8 + (700 % 8 == 0 ? 0 : 1),bitmapKey[2].length);
Assert.assertNotEquals(0, bitmapKey[2][87] & (1 << 3)); // getbit bitmap_key 700 -> 1
Assert.assertNotEquals(0, bitmapKey[2][8] & (1 << 1)); // getbit bitmap_key 70 -> 1

Assert.assertEquals(9,redisOps.size());
}
*/

@Test
public void testParseListpackStream3() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ public class RdbDataBytes {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x08, (byte) 0xf9, 0x05, 0x10, (byte) 0x0a, 0x63, (byte) 0x6f, (byte) 0x6d, (byte) 0x6d, (byte) 0x6f, (byte) 0x6e, (byte) 0x5f, (byte) 0x6b, 0x65, 0x79, 0x05, 0x50, 0x00, 0x05, 0x76, 0x61, (byte) 0x6c,
0x75, 0x65, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

public static final byte[] ror8Bitmap = new byte[]{0x52, 0x45, 0x44, 0x49, 0x53, 0x30, 0x30, 0x31, 0x32, (byte) 0xFA, 0x09, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x76, 0x65, 0x72, 0x05, 0x38, 0x2E, 0x32, 0x2E, 0x30, (byte) 0xFA, 0x0A, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x62, 0x69, 0x74, 0x73, (byte) 0xC0, 0x40, (byte) 0xFA, 0x05, 0x63, 0x74, 0x69, 0x6D, 0x65, (byte) 0xC2, 0x44, (byte) 0x81, 0x58, 0x6A, (byte) 0xFA, 0x08, 0x75, 0x73, 0x65, 0x64, 0x2D, 0x6D, 0x65, 0x6D, (byte) 0xC2, 0x78, (byte) 0x9E, (byte) 0x97, 0x02, (byte) 0xFA, 0x0E, 0x72, 0x65, 0x70, 0x6C, 0x2D, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, 0x2D, 0x64, 0x62, (byte) 0xC0, 0x00, (byte) 0xFA, 0x07, 0x72, 0x65, 0x70, 0x6C, 0x2D, 0x69, 0x64, 0x28, 0x30, 0x32, 0x32, 0x32, 0x36, 0x31, 0x66, 0x37, 0x31, 0x65, 0x36, 0x66, 0x35, 0x65, 0x33, 0x65, 0x61, 0x62, 0x32, 0x64, 0x62, 0x33, 0x33, 0x63, 0x37, 0x32, 0x39, 0x34, 0x39, 0x30, 0x30, 0x39, 0x34, 0x63, 0x30, 0x37, 0x36, 0x33, 0x37, 0x34, (byte) 0xFA, 0x0B, 0x72, 0x65, 0x70, 0x6C, 0x2D, 0x6F, 0x66, 0x66, 0x73, 0x65, 0x74, (byte) 0xC1, (byte) 0xA9, 0x00, (byte) 0xFA, 0x0E, 0x67, 0x74, 0x69, 0x64, 0x2D, 0x72, 0x65, 0x70, 0x6C, 0x2D, 0x6D, 0x6F, 0x64, 0x65, 0x05, 0x70, 0x73, 0x79, 0x6E, 0x63, (byte) 0xFA, 0x0D, 0x67, 0x74, 0x69, 0x64, 0x2D, 0x65, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x64, 0x00, (byte) 0xFA, 0x09, 0x67, 0x74, 0x69, 0x64, 0x2D, 0x6C, 0x6F, 0x73, 0x74, 0x00, (byte) 0xFA, 0x08, 0x61, 0x6F, 0x66, 0x2D, 0x62, 0x61, 0x73, 0x65, (byte) 0xC0, 0x00, (byte) 0xFE, 0x00, (byte) 0xFB, 0x04, 0x00, (byte) 0xF9, 0x00, (byte) 0xC0, 0x0B, 0x62, 0x69, 0x74, 0x6D, 0x61, 0x70, 0x5F, 0x6B, 0x65, 0x79, 0x32, 0x40, 0x58, 0x50, 0x00, 0x40, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, (byte) 0xF9, 0x00, (byte) 0xC0, 0x0A, 0x63, 0x6F, 0x6D, 0x6D, 0x6F, 0x6E, 0x5F, 0x6B, 0x65, 0x79, 0x05, 0x50, 0x00, 0x05, 0x76, 0x61, 0x6C, 0x75, 0x65, (byte) 0xF9, 0x00, (byte) 0xC0, 0x05, 0x6D, 0x79, 0x6B, 0x65, 0x79, 0x02, 0x50, 0x00, 0x02, 0x61, 0x61, (byte) 0xF9, 0x00, (byte) 0xC0, 0x0A, 0x62, 0x69, 0x74, 0x6D, 0x61, 0x70, 0x5F, 0x6B, 0x65, 0x79, 0x40, 0x58, 0x50, 0x00, 0x40, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, (byte) 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};


public static final byte[] listpackStream3 = new byte[]{0x52, 0x45, 0x44, 0x49, 0x53, 0x30, 0x30, 0x31, 0x32, (byte) 0xFA, 0x09, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x76, 0x65, 0x72, 0x05, 0x38, 0x2E, 0x32, 0x2E, 0x30, (byte) 0xFA, 0x0A, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x62, 0x69, 0x74, 0x73, (byte) 0xC0, 0x40, (byte) 0xFA, 0x05, 0x63, 0x74, 0x69, 0x6D, 0x65, (byte) 0xC2, (byte) 0xEC, (byte) 0xFB, (byte) 0xC0, 0x69, (byte) 0xFA, 0x08, 0x75, 0x73, 0x65, 0x64, 0x2D, 0x6D, 0x65, 0x6D, (byte) 0xC2, (byte) 0xD0, 0x18, 0x13, 0x00, (byte) 0xFA, 0x08, 0x61, 0x6F, 0x66, 0x2D, 0x62, 0x61, 0x73, 0x65, (byte) 0xC0, 0x00, (byte) 0xFE, 0x00, (byte) 0xFB, 0x01, 0x00, 0x15, 0x08, 0x6D, 0x79, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, 0x01, 0x10, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCE, (byte) 0xED, (byte) 0xD2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xC3, 0x40, 0x61, 0x40, 0x71, 0x06, 0x71, 0x00, 0x00, 0x00, 0x17, 0x00, 0x01, 0x20, 0x00, 0x0B, 0x02, 0x01, (byte) 0x84, 0x6E, 0x61, 0x6D, 0x65, 0x05, (byte) 0x87, 0x73, 0x75, 0x72, 0x40, 0x08, 0x05, 0x08, 0x00, 0x01, 0x03, 0x01, 0x00, 0x20, 0x01, 0x0F, (byte) 0x84, 0x53, 0x61, 0x72, 0x61, 0x05, (byte) 0x87, 0x4F, 0x43, 0x6F, 0x6E, 0x6E, 0x6F, 0x72, 0x08, 0x05, 0x20, 0x12, 0x03, (byte) 0xF1, (byte) 0xD9, 0x3F, 0x03, 0x40, 0x1E, 0x0D, (byte) 0x86, 0x66, 0x69, 0x65, 0x6C, 0x64, 0x31, 0x07, (byte) 0x86, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x20, 0x07, 0x60, 0x0F, 0x00, 0x32, (byte) 0xA0, 0x0F, 0x20, 0x07, 0x60, 0x0F, 0x00, 0x33, (byte) 0xA0, 0x0F, 0x04, 0x33, 0x07, 0x0A, 0x01, (byte) 0xFF, 0x01, (byte) 0x81, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCF, 0x2D, (byte) 0xAB, 0x00, (byte) 0x81, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCF, 0x2D, (byte) 0xAB, 0x00, (byte) 0x81, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCE, (byte) 0xED, (byte) 0xD2, 0x00, 0x02, 0x01, 0x07, 0x6D, 0x79, 0x67, 0x72, 0x6F, 0x75, 0x70, (byte) 0x81, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCF, 0x2D, (byte) 0xAB, 0x00, 0x02, 0x01, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCF, 0x2D, (byte) 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x7C, (byte) 0xD3, 0x19, (byte) 0x9D, 0x01, 0x00, 0x00, 0x01, 0x01, 0x0A, 0x6D, 0x79, 0x63, 0x6F, 0x6E, 0x73, 0x75, 0x6D, 0x65, 0x72, 0x21, 0x7C, (byte) 0xD3, 0x19, (byte) 0x9D, 0x01, 0x00, 0x00, 0x21, 0x7C, (byte) 0xD3, 0x19, (byte) 0x9D, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, (byte) 0x9D, 0x19, (byte) 0xCF, 0x2D, (byte) 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xAE, (byte) 0xBE, 0x77, 0x6F, 0x5D, (byte) 0xBC, (byte) 0x9C, (byte) 0x8C};

public static final byte[] listpackHashEx = new byte[]{0x52, 0x45, 0x44, 0x49, 0x53, 0x30, 0x30, 0x31, 0x32, (byte) 0xFA, 0x09, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x76, 0x65, 0x72, 0x05, 0x38, 0x2E, 0x32, 0x2E, 0x30, (byte) 0xFA, 0x0A, 0x72, 0x65, 0x64, 0x69, 0x73, 0x2D, 0x62, 0x69, 0x74, 0x73, (byte) 0xC0, 0x40, (byte) 0xFA, 0x05, 0x63, 0x74, 0x69, 0x6D, 0x65, (byte) 0xC2, (byte) 0x94, 0x25, (byte) 0xC1, 0x69, (byte) 0xFA, 0x08, 0x75, 0x73, 0x65, 0x64, 0x2D, 0x6D, 0x65, 0x6D, (byte) 0xC2, 0x30, (byte) 0xE5, 0x14, 0x00, (byte) 0xFA, 0x08, 0x61, 0x6F, 0x66, 0x2D, 0x62, 0x61, 0x73, 0x65, (byte) 0xC0, 0x00, (byte) 0xFE, 0x00, (byte) 0xFB, 0x01, 0x00, 0x19, 0x07, 0x6D, 0x79, 0x65, 0x78, 0x6B, 0x65, 0x79, (byte) 0xA2, (byte) 0xA4, (byte) 0xEE, 0x2F, (byte) 0x9D, 0x01, 0x00, 0x00, (byte) 0xC3, 0x2F, 0x3D, 0x14, 0x3D, 0x00, 0x00, 0x00, 0x09, 0x00, (byte) 0x82, 0x66, 0x33, 0x03, (byte) 0x82, 0x76, 0x33, 0x03, (byte) 0xF4, (byte) 0xA2, (byte) 0xA4, (byte) 0xEE, 0x2F, (byte) 0x9D, 0x01, 0x20, 0x12, 0x02, (byte) 0x82, 0x66, 0x32, 0x20, 0x11, 0x00, 0x32, (byte) 0xE0, 0x04, 0x11, 0x00, 0x31, 0x20, 0x11, 0x00, 0x31, (byte) 0xE0, 0x01, 0x11, 0x01, 0x09, (byte) 0xFF, (byte) 0xFF, 0x5D, (byte) 0x89, (byte) 0xB4, 0x4E, (byte) 0xBA, (byte) 0xC3, (byte) 0xDF, (byte) 0xBA};
Expand Down
Loading