Skip to content

Commit

Permalink
bugfix: sequence delta of stream item may be negative(#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDT3213 committed Jan 4, 2024
1 parent 4c2b9a3 commit 0532e58
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cases/issue27.json

Large diffs are not rendered by default.

Binary file added cases/issue27.rdb
Binary file not shown.
4 changes: 2 additions & 2 deletions core/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ func (dec *Decoder) readListPackEntry(buf []byte, cursor *int) ([]byte, uint32,
return nil, 0, fmt.Errorf("unknown entry header")
}

func (dec *Decoder) readListPackEntryAsUint(buf []byte, cursor *int) (uint64, error) {
func (dec *Decoder) readListPackEntryAsInt(buf []byte, cursor *int) (int64, error) {
bin, _, err := dec.readListPackEntry(buf, cursor)
if err != nil {
return 0, fmt.Errorf("read from failed: %v", err)
}
i, err := strconv.ParseUint(string(bin), 10, 64)
i, err := strconv.ParseInt(string(bin), 10, 64)
if err != nil {
return 0, fmt.Errorf("%s is not a uint", string(bin))
}
Expand Down
22 changes: 12 additions & 10 deletions core/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"encoding/binary"
"fmt"

"github.com/hdt3213/rdb/model"
)

Expand Down Expand Up @@ -123,17 +124,17 @@ func (dec *Decoder) readStreamEntries() ([]*model.StreamEntry, error) {
// readStreamEntryContent read messages in a stream entry
func (dec *Decoder) readStreamEntryContent(buf []byte, cursor *int, firstId *model.StreamId) (*model.StreamEntry, error) {
// read count
count, err := dec.readListPackEntryAsUint(buf, cursor)
count, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream entry count failed: %v", err)
}
deleted, err := dec.readListPackEntryAsUint(buf, cursor)
deleted, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream entry deleted count failed: %v", err)
}

// read field names of master entry
fieldNum0, err := dec.readListPackEntryAsUint(buf, cursor)
fieldNum0, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream field number failed: %v", err)
}
Expand All @@ -153,26 +154,27 @@ func (dec *Decoder) readStreamEntryContent(buf []byte, cursor *int, firstId *mod

total := count + deleted
msgs := make([]*model.StreamMessage, 0, total)
for i := uint64(0); i < total; i++ {
flag, err := dec.readListPackEntryAsUint(buf, cursor)
for i := int64(0); i < total; i++ {
flag, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream item flag failed: %v", err)
}
ms, err := dec.readListPackEntryAsUint(buf, cursor)
ms, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream item id ms failed: %v", err)
}
seq, err := dec.readListPackEntryAsUint(buf, cursor)
seq, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream item id seq failed: %v", err)
}
// ms and seq may be negative
msgId := &model.StreamId{
Ms: ms + firstId.Ms,
Sequence: seq + firstId.Sequence,
Ms: uint64(ms + int64(firstId.Ms)),
Sequence: uint64(seq + int64(firstId.Sequence)),
}
fieldNum := masterFieldNum
if flag&StreamItemFlagSameFields == 0 {
fieldNum0, err := dec.readListPackEntryAsUint(buf, cursor)
fieldNum0, err := dec.readListPackEntryAsInt(buf, cursor)
if err != nil {
return nil, fmt.Errorf("read stream item field number failed: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions helper/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestToJson(t *testing.T) {
}
}()
testCases := []string{
"issue27",
"set_listpack",
"stream_listpacks_2",
"stream_listpacks_1",
Expand Down

0 comments on commit 0532e58

Please sign in to comment.