Skip to content

Commit

Permalink
narrow the semantics of func resetItems
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Mar 27, 2024
1 parent 3286767 commit ace6df5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
21 changes: 15 additions & 6 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (f *Freezer) TruncateHead(items uint64) (uint64, error) {
if kind != ChainFreezerBlobSidecarTable {
return 0, err
}
nt, err := table.resetItems(items-f.offset, items-f.offset)
nt, err := table.resetItems(items - f.offset)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -489,7 +489,7 @@ func (f *Freezer) repair() error {
if kind != ChainFreezerBlobSidecarTable {
return err
}
nt, err := table.resetItems(head, head)
nt, err := table.resetItems(head)
if err != nil {
return err
}
Expand Down Expand Up @@ -720,11 +720,20 @@ func (f *Freezer) ResetTable(kind string, tail, head uint64, onlyEmpty bool) err
return nil
}

nt, err := f.tables[kind].resetItems(tail-f.offset, head-f.offset)
if err != nil {
return err
if tail != head {
if err := f.tables[kind].truncateHead(head); err != nil {
return err
}
if err := f.tables[kind].truncateTail(tail); err != nil {
return err
}
} else {
nt, err := f.tables[kind].resetItems(head - f.offset)
if err != nil {
return err
}
f.tables[kind] = nt
}
f.tables[kind] = nt

if err := f.repair(); err != nil {
for _, table := range f.tables {
Expand Down
20 changes: 4 additions & 16 deletions core/rawdb/freezer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,34 +1029,22 @@ func (t *freezerTable) ResetItemsOffset(virtualTail uint64) error {
return nil
}

// resetItems reset freezer table head & tail
// resetItems reset freezer table to 0 items with head `headAndTail`
// only used for ChainFreezerBlobSidecarTable now
func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) {
func (t *freezerTable) resetItems(headAndTail uint64) (*freezerTable, error) {
if t.readonly {
return nil, errors.New("resetItems in readonly mode")
}

itemHidden := t.itemHidden.Load()
items := t.items.Load()
if tail > head || (tail < head && (itemHidden > tail || items < head)) {
if itemHidden > headAndTail || items < headAndTail {
return nil, errors.New("cannot reset to non-exist range")
}

var err error
if tail < head {
if err = t.truncateHead(head); err != nil {
return nil, err
}
if err = t.truncateTail(tail); err != nil {
return nil, err
}
return t, nil
}

t.lock.Lock()
defer t.lock.Unlock()

// if tail == head, it means table reset to 0 item
// remove all data files
t.head.Close()
t.releaseFilesAfter(0, true)
Expand Down Expand Up @@ -1086,7 +1074,7 @@ func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) {
}
tailIndex := indexEntry{
filenum: 0,
offset: uint32(tail),
offset: uint32(headAndTail),
}
if _, err = index.Write(tailIndex.append(nil)); err != nil {
return nil, err
Expand Down

0 comments on commit ace6df5

Please sign in to comment.