Skip to content

Commit

Permalink
rename modules
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jan 30, 2025
1 parent 4ed349d commit bf16f91
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
19 changes: 9 additions & 10 deletions module/block_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ type IterateRange struct {
End uint64 // the end of the range
}

// IterateProgress is an interface for reading and writing the progress of the iterator
type IterateProgress interface {
IterateProgressReader
IterateProgressWriter
// IteratorState is an interface for reading and writing the progress of the iterator
type IteratorState interface {
IteratorStateReader
IteratorStateWriter
}

// IterateProgressReader reads the progress of the iterator, useful for resuming the iteration
// IteratorStateReader reads the progress of the iterator, useful for resuming the iteration
// after restart
type IterateProgressReader interface {
type IteratorStateReader interface {
// LoadState reads the next block to iterate
// caller must ensure the reader is created by the IterateProgressInitializer,
// otherwise LoadState would return exception.
// caller must ensure the state is initialized, otherwise LoadState would return exception.
LoadState() (progress uint64, exception error)
}

// IterateProgressWriter saves the progress of the iterator
type IterateProgressWriter interface {
// IteratorStateWriter saves the progress of the iterator
type IteratorStateWriter interface {
// SaveState persists the next block to be iterated
SaveState(uint64) (exception error)
}
Expand Down
4 changes: 2 additions & 2 deletions module/block_iterator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// a new block iterator can be created to iterate through the next range.
type Creator struct {
getBlockIDByIndex func(uint64) (flow.Identifier, bool, error)
progress *NextProgress
progress *PersistentIteratorState
}

var _ module.IteratorCreator = (*Creator)(nil)
Expand All @@ -27,7 +27,7 @@ func NewCreator(
latest func() (uint64, error),
) (*Creator, error) {
// initialize the progress in storage, saving the root block index in storage
progress, err := NewNextProgress(progressStorage, root, latest)
progress, err := NewPersistentIteratorState(progressStorage, root, latest)
if err != nil {
return nil, fmt.Errorf("failed to initialize progress: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions module/block_iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
type IndexedBlockIterator struct {
// dependencies
getBlockIDByIndex func(uint64) (blockID flow.Identifier, indexed bool, exception error)
progress module.IterateProgressWriter // for saving the next index to be iterated for resuming the iteration
endIndex uint64 // the end index to iterate, this never change
nextIndex uint64 // the start index to iterate, this will be updated after each iteration
progress module.IteratorStateWriter // for saving the next index to be iterated for resuming the iteration
endIndex uint64 // the end index to iterate, this never change
nextIndex uint64 // the start index to iterate, this will be updated after each iteration
}

var _ module.BlockIterator = (*IndexedBlockIterator)(nil)

// caller must ensure that both iterRange.Start and iterRange.End are finalized
func NewIndexedBlockIterator(
getBlockIDByIndex func(uint64) (blockID flow.Identifier, indexed bool, exception error),
progress module.IterateProgressWriter,
progress module.IteratorStateWriter,
iterRange module.IterateRange,
) module.BlockIterator {
return &IndexedBlockIterator{
Expand Down
4 changes: 2 additions & 2 deletions module/block_iterator/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ type saveNextHeight struct {
savedNextHeight uint64
}

var _ module.IterateProgressWriter = (*saveNextHeight)(nil)
var _ module.IterateProgressReader = (*saveNextHeight)(nil)
var _ module.IteratorStateWriter = (*saveNextHeight)(nil)
var _ module.IteratorStateReader = (*saveNextHeight)(nil)

func (s *saveNextHeight) SaveState(height uint64) error {
s.savedNextHeight = height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/onflow/flow-go/storage"
)

type NextProgress struct {
// PersistentIteratorState stores the state of the iterator in a persistent storage
type PersistentIteratorState struct {
store storage.ConsumerProgress
latest func() (uint64, error)
}

var _ module.IterateProgress = (*NextProgress)(nil)
var _ module.IteratorState = (*PersistentIteratorState)(nil)

func NewNextProgress(store storage.ConsumerProgress, root uint64, latest func() (uint64, error)) (*NextProgress, error) {
func NewPersistentIteratorState(store storage.ConsumerProgress, root uint64, latest func() (uint64, error)) (*PersistentIteratorState, error) {
_, err := store.ProcessedIndex()
if errors.Is(err, storage.ErrNotFound) {
next := root + 1
Expand All @@ -25,22 +26,22 @@ func NewNextProgress(store storage.ConsumerProgress, root uint64, latest func()
}
}

return &NextProgress{
return &PersistentIteratorState{
store: store,
latest: latest,
}, nil
}

func (n *NextProgress) LoadState() (uint64, error) {
func (n *PersistentIteratorState) LoadState() (uint64, error) {
return n.store.ProcessedIndex()
}

func (n *NextProgress) SaveState(next uint64) error {
func (n *PersistentIteratorState) SaveState(next uint64) error {
return n.store.SetProcessedIndex(next)
}

// NextRange returns the next range of blocks to iterate over
func (n *NextProgress) NextRange() (module.IterateRange, error) {
func (n *PersistentIteratorState) NextRange() (module.IterateRange, error) {
next, err := n.LoadState()
if err != nil {
return module.IterateRange{}, fmt.Errorf("failed to read next height: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestProgress(t *testing.T) {

store := storagepebble.NewConsumerProgress(db, "test")

progress, err := NewNextProgress(store, root, getLatest)
progress, err := NewPersistentIteratorState(store, root, getLatest)
require.NoError(t, err)

// initial state should be the next of root
Expand Down

0 comments on commit bf16f91

Please sign in to comment.