Skip to content

Commit 70ae50a

Browse files
bpintHDT3213
authored andcommitted
rdb 12 (redis 7.4+): add support for hash with field expiration
1 parent dbcc2d4 commit 70ae50a

12 files changed

+476
-33
lines changed

build.sh

100644100755
File mode changed.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
*8
2+
$5
3+
HMSET
4+
$12
5+
listpack-hfe
6+
$2
7+
F1
8+
$2
9+
V1
10+
$2
11+
F3
12+
$2
13+
V3
14+
$2
15+
F2
16+
$2
17+
V2
18+
*6
19+
$10
20+
HPEXPIREAT
21+
$12
22+
listpack-hfe
23+
$13
24+
2755484483878
25+
$6
26+
FIELDS
27+
$1
28+
1
29+
$2
30+
F3
31+
*5
32+
$8
33+
HPERSIST
34+
$12
35+
listpack-hfe
36+
$6
37+
FIELDS
38+
$1
39+
1
40+
$2
41+
F2
42+
*6
43+
$10
44+
HPEXPIREAT
45+
$12
46+
listpack-hfe
47+
$13
48+
2755482478325
49+
$6
50+
FIELDS
51+
$1
52+
1
53+
$2
54+
F1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{"db":0,"key":"listpack-hfe","size":316,"type":"hash","encoding":"listpackex","hash":{"F1":"V1","F2":"V2","F3":"V3"},"expire":{"F1":2755482478325,"F2":0,"F3":2755484483878}}
3+
]
169 Bytes
Binary file not shown.

cases/hash_with_hfe.aof

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
*18
2+
$5
3+
HMSET
4+
$8
5+
hash-hfe
6+
$2
7+
F4
8+
$2
9+
V4
10+
$2
11+
F7
12+
$2
13+
V7
14+
$2
15+
F8
16+
$2
17+
V8
18+
$2
19+
F2
20+
$2
21+
V2
22+
$2
23+
F5
24+
$2
25+
V5
26+
$2
27+
F3
28+
$2
29+
V3
30+
$2
31+
F1
32+
$2
33+
V1
34+
$2
35+
F6
36+
$2
37+
V6
38+
*5
39+
$8
40+
HPERSIST
41+
$8
42+
hash-hfe
43+
$6
44+
FIELDS
45+
$1
46+
1
47+
$2
48+
F8
49+
*6
50+
$10
51+
HPEXPIREAT
52+
$8
53+
hash-hfe
54+
$13
55+
2755483429282
56+
$6
57+
FIELDS
58+
$1
59+
1
60+
$2
61+
F2
62+
*5
63+
$8
64+
HPERSIST
65+
$8
66+
hash-hfe
67+
$6
68+
FIELDS
69+
$1
70+
1
71+
$2
72+
F5
73+
*6
74+
$10
75+
HPEXPIREAT
76+
$8
77+
hash-hfe
78+
$13
79+
2755484433842
80+
$6
81+
FIELDS
82+
$1
83+
1
84+
$2
85+
F3
86+
*6
87+
$10
88+
HPEXPIREAT
89+
$8
90+
hash-hfe
91+
$13
92+
2755482424661
93+
$6
94+
FIELDS
95+
$1
96+
1
97+
$2
98+
F1
99+
*5
100+
$8
101+
HPERSIST
102+
$8
103+
hash-hfe
104+
$6
105+
FIELDS
106+
$1
107+
1
108+
$2
109+
F6
110+
*5
111+
$8
112+
HPERSIST
113+
$8
114+
hash-hfe
115+
$6
116+
FIELDS
117+
$1
118+
1
119+
$2
120+
F4
121+
*5
122+
$8
123+
HPERSIST
124+
$8
125+
hash-hfe
126+
$6
127+
FIELDS
128+
$1
129+
1
130+
$2
131+
F7

cases/hash_with_hfe.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{"db":0,"key":"hash-hfe","size":660,"type":"hash","encoding":"hashex","hash":{"F1":"V1","F2":"V2","F3":"V3","F4":"V4","F5":"V5","F6":"V6","F7":"V7","F8":"V8"},"expire":{"F1":2755482424661,"F2":2755483429282,"F3":2755484433842,"F4":0,"F5":0,"F6":0,"F7":0,"F8":0}}
3+
]

cases/hash_with_hfe.rdb

176 Bytes
Binary file not shown.

core/decoder.go

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"encoding/binary"
88
"errors"
99
"fmt"
10-
"github.com/hdt3213/rdb/memprofiler"
11-
"github.com/hdt3213/rdb/model"
1210
"io"
1311
"strconv"
1412
"time"
13+
14+
"github.com/hdt3213/rdb/memprofiler"
15+
"github.com/hdt3213/rdb/model"
1516
)
1617

1718
// Decoder is an instance of rdb parsing process
@@ -87,27 +88,41 @@ const (
8788
typeStreamListPacks2
8889
typeSetListPack
8990
typeStreamListPacks3
91+
typeHashWithHfeRc // rdb 12 (only redis 7.4 rc)
92+
typeHashListPackWithHfeRc // rdb 12 (only redis 7.4 rc)
93+
typeHashWithHfe // since rdb 12 (redis 7.4)
94+
typeHashListPackWithHfe // since rdb 12 (redis 7.4)
95+
)
96+
97+
const (
98+
EB_EXPIRE_TIME_MAX int64 = 0x0000FFFFFFFFFFFF
99+
EB_EXPIRE_TIME_INVALID int64 = EB_EXPIRE_TIME_MAX + 1
100+
HFE_MAX_ABS_TIME_MSEC int64 = EB_EXPIRE_TIME_MAX >> 2
90101
)
91102

92103
var encodingMap = map[int]string{
93-
typeString: model.StringEncoding,
94-
typeList: model.ListEncoding,
95-
typeSet: model.SetEncoding,
96-
typeZset: model.ZSetEncoding,
97-
typeHash: model.HashEncoding,
98-
typeZset2: model.ZSet2Encoding,
99-
typeHashZipMap: model.ZipMapEncoding,
100-
typeListZipList: model.ZipListEncoding,
101-
typeSetIntSet: model.IntSetEncoding,
102-
typeZsetZipList: model.ZipListEncoding,
103-
typeHashZipList: model.ZipListEncoding,
104-
typeListQuickList: model.QuickListEncoding,
105-
typeStreamListPacks: model.ListPackEncoding,
106-
typeStreamListPacks2: model.ListPackEncoding,
107-
typeHashListPack: model.ListPackEncoding,
108-
typeZsetListPack: model.ListPackEncoding,
109-
typeListQuickList2: model.QuickList2Encoding,
110-
typeSetListPack: model.ListPackEncoding,
104+
typeString: model.StringEncoding,
105+
typeList: model.ListEncoding,
106+
typeSet: model.SetEncoding,
107+
typeZset: model.ZSetEncoding,
108+
typeHash: model.HashEncoding,
109+
typeZset2: model.ZSet2Encoding,
110+
typeHashZipMap: model.ZipMapEncoding,
111+
typeListZipList: model.ZipListEncoding,
112+
typeSetIntSet: model.IntSetEncoding,
113+
typeZsetZipList: model.ZipListEncoding,
114+
typeHashZipList: model.ZipListEncoding,
115+
typeListQuickList: model.QuickListEncoding,
116+
typeStreamListPacks: model.ListPackEncoding,
117+
typeStreamListPacks2: model.ListPackEncoding,
118+
typeHashListPack: model.ListPackEncoding,
119+
typeZsetListPack: model.ListPackEncoding,
120+
typeListQuickList2: model.QuickList2Encoding,
121+
typeSetListPack: model.ListPackEncoding,
122+
typeHashWithHfeRc: model.HashExEncoding,
123+
typeHashListPackWithHfeRc: model.ListPackExEncoding,
124+
typeHashWithHfe: model.HashExEncoding,
125+
typeHashListPackWithHfe: model.ListPackExEncoding,
111126
}
112127

113128
// checkHeader checks whether input has valid RDB file header
@@ -311,6 +326,27 @@ func (dec *Decoder) readObject(flag byte, base *model.BaseObject) (model.RedisOb
311326
BaseObject: base,
312327
Members: set,
313328
}, nil
329+
case typeHashWithHfe, typeHashWithHfeRc:
330+
hash, expire, err := dec.readHashMapEx(func() bool { return flag == typeHashWithHfeRc }())
331+
if err != nil {
332+
return nil, err
333+
}
334+
return &model.HashObject{
335+
BaseObject: base,
336+
Hash: hash,
337+
FieldExpirations: expire,
338+
}, nil
339+
case typeHashListPackWithHfe, typeHashListPackWithHfeRc:
340+
m, e, extra, err := dec.readListPackHashEx(func() bool { return flag == typeHashListPackWithHfeRc }())
341+
if err != nil {
342+
return nil, err
343+
}
344+
base.Extra = extra
345+
return &model.HashObject{
346+
BaseObject: base,
347+
Hash: m,
348+
FieldExpirations: e,
349+
}, nil
314350
}
315351
return nil, fmt.Errorf("unknown type flag: %b", flag)
316352
}

0 commit comments

Comments
 (0)