Skip to content

Commit

Permalink
fix: fix panic when writing the same key multiple times in the same t…
Browse files Browse the repository at this point in the history
…ransaction (nutsdb#581)
  • Loading branch information
mrene committed Feb 25, 2024
1 parent a4ca1b9 commit b346859
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
16 changes: 8 additions & 8 deletions pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ func (pending *pendingEntryList) submitEntry(ds Ds, bucket string, e *Entry) {
if _, exist := pending.entriesInBTree[bucket]; !exist {
pending.entriesInBTree[bucket] = map[string]*Entry{}
}
pending.entriesInBTree[bucket][string(e.Key)] = e
if _, exist := pending.entriesInBTree[bucket][string(e.Key)]; !exist {
pending.entriesInBTree[bucket][string(e.Key)] = e
pending.size++
}
default:
if _, exist := pending.entries[ds]; !exist {
pending.entries[ds] = map[BucketName][]*Entry{}
}
entries := pending.entries[ds][bucket]
entries = append(entries, e)
pending.entries[ds][bucket] = entries
pending.size++
}
pending.size++
}

// rangeBucket input a range handler function f and call it with every bucket in pendingBucketList
Expand All @@ -85,19 +88,16 @@ func (p pendingBucketList) rangeBucket(f func(bucket *Bucket) error) error {

// toList collect all the entries in pendingEntryList to a list.
func (pending *pendingEntryList) toList() []*Entry {
list := make([]*Entry, pending.size)
var i int
list := make([]*Entry, 0, pending.size)
for _, entriesInBucket := range pending.entriesInBTree {
for _, entry := range entriesInBucket {
list[i] = entry
i++
list = append(list, entry)
}
}
for _, entriesInDS := range pending.entries {
for _, entries := range entriesInDS {
for _, entry := range entries {
list[i] = entry
i++
list = append(list, entry)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ func (tx *Tx) Commit() (err error) {
var records []*Record

pendingWriteList := tx.pendingWrites.toList()
writesLen := tx.pendingWrites.size
lastIndex := writesLen - 1
for i := 0; i < writesLen; i++ {
lastIndex := len(pendingWriteList) - 1
for i := 0; i < len(pendingWriteList); i++ {
entry := pendingWriteList[i]
entrySize := entry.Size()
if entrySize > tx.db.opt.SegmentSize {
Expand Down

0 comments on commit b346859

Please sign in to comment.