Skip to content

Commit

Permalink
Generalize iterator exhausted error (#13060)
Browse files Browse the repository at this point in the history
As part of #12907 I'll have
other iterator-like sequence implementations.

It makes sense to generalize the ErrEliasFanoIterExhausted and move it
to a common package and reuse it, rather than making a bunch of
IteratorExhausted-like for each implementation.
  • Loading branch information
wmitsuda authored Dec 10, 2024
1 parent dce89b3 commit c4bd853
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 6 additions & 0 deletions erigon-lib/kv/stream/stream_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package stream

import "errors"

// Streams - it's iterator-like composable high-level abstraction - which designed for inter-process communication and between processes:
// - cancelable
// - return errors
Expand All @@ -38,6 +40,10 @@ package stream
// check in loops on stream. Duo has very limited API - user has no way to
// terminate it - but user can specify more strict conditions when creating stream (then server knows better when to stop)

// Indicates the iterator has no more elements. Meant to be returned by implementations of Next()
// when there are no more elements.
var ErrIteratorExhausted = errors.New("iterator exhausted")

// Uno - return 1 item. Example:
//
// for s.HasNext() {
Expand Down
6 changes: 2 additions & 4 deletions erigon-lib/recsplit/eliasfano32/elias_fano.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package eliasfano32

import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
Expand All @@ -27,6 +26,7 @@ import (
"unsafe"

"github.com/erigontech/erigon-lib/common/bitutil"
"github.com/erigontech/erigon-lib/kv/stream"
)

// EliasFano algo overview https://www.antoniomallia.it/sorted-integers-compression-with-elias-fano-encoding.html
Expand All @@ -44,8 +44,6 @@ const (
superQSize uint64 = 1 + qPerSuperQ/2 // 1 + 64/2 = 33
)

var ErrEliasFanoIterExhausted = errors.New("elias fano iterator exhausted")

// EliasFano can be used to encode one monotone sequence
type EliasFano struct {
data []uint64
Expand Down Expand Up @@ -486,7 +484,7 @@ func (efi *EliasFanoIter) decrement() {

func (efi *EliasFanoIter) Next() (uint64, error) {
if !efi.HasNext() {
return 0, ErrEliasFanoIterExhausted
return 0, stream.ErrIteratorExhausted
}
idx64, shift := efi.lowerIdx/64, efi.lowerIdx%64
lower := efi.lowerBits[idx64] >> shift
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/recsplit/eliasfano32/elias_fano_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,15 @@ func TestIterator(t *testing.T) {
require.NoError(t, err)
}
_, err := it.Next()
require.ErrorIs(t, err, ErrEliasFanoIterExhausted)
require.ErrorIs(t, err, stream.ErrIteratorExhausted)

it = ef.ReverseIterator()
for range offsets {
_, err := it.Next()
require.NoError(t, err)
}
_, err = it.Next()
require.ErrorIs(t, err, ErrEliasFanoIterExhausted)
require.ErrorIs(t, err, stream.ErrIteratorExhausted)
})

t.Run("article-example1", func(t *testing.T) {
Expand Down

0 comments on commit c4bd853

Please sign in to comment.