2024-09-18 22:28:53 +00:00
|
|
|
// SPDX-License-Identifier: Unlicense
|
|
|
|
|
2024-09-02 17:04:00 +00:00
|
|
|
package cursor
|
|
|
|
|
|
|
|
import (
|
2024-09-25 22:34:31 +00:00
|
|
|
"errors"
|
2024-09-02 17:04:00 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cursor reads data from a specific spot in a data source.
|
|
|
|
type Cursor[Datum any] interface {
|
|
|
|
// I almost parameterized Cursor by its implementation (i.e. the Curiously
|
|
|
|
// Recurring Template Pattern), but then each parser would need that parameter.
|
|
|
|
// That might work well in a language with much stronger type inference, but
|
|
|
|
// not in Go. The upside would have been that for each implementation Impl,
|
|
|
|
// Impl.Read could have returned an unboxed Impl, which would have slightly
|
|
|
|
// simplified testing and maybe slightly reduced allocs.
|
|
|
|
|
|
|
|
// Read fill dst with data from this Cursor's position in the underlying
|
|
|
|
// source. It returns the number of data it read and a new Cursor for
|
|
|
|
// the position at which the read ended, or an error if the read failed.
|
|
|
|
// All calls to a given Cursor will return data from the same position.
|
2024-09-03 19:55:32 +00:00
|
|
|
// If n < len(dst) or if the cursor's position is at the end of the data source,
|
|
|
|
// Read will return an error explaining why it read fewer bytes than requested.
|
|
|
|
// If the error was due to the cursor reaching the end of the data source,
|
2024-09-02 17:04:00 +00:00
|
|
|
// err will be io.EOF.
|
|
|
|
Read(dst []Datum) (n uint64, next Cursor[Datum], err error)
|
|
|
|
|
|
|
|
// Pos returns the Cursor's position within the source.
|
|
|
|
Pos() uint64
|
2024-09-03 22:10:27 +00:00
|
|
|
|
|
|
|
// At returns a new cursor at the position pos.
|
|
|
|
At(pos uint64) Cursor[Datum]
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
type ReaderAt[T any] interface {
|
|
|
|
ReadAt(p []T, off int64) (n int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SliceReaderAt[T any] []T
|
|
|
|
|
|
|
|
func (s SliceReaderAt[T]) ReadAt(dst []T, off int64) (n int, err error) {
|
|
|
|
if off < 0 {
|
|
|
|
return 0, errors.New("SliceReaderAt.ReadAt: negative offset")
|
|
|
|
}
|
|
|
|
if off >= int64(len(s)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n = copy(dst, s[off:])
|
|
|
|
if n < len(dst) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:04:00 +00:00
|
|
|
type SliceCursor[Datum any] struct {
|
|
|
|
data []Datum
|
|
|
|
offset uint64
|
|
|
|
}
|
|
|
|
|
2024-09-03 19:55:32 +00:00
|
|
|
func NewSlice[Datum any](data []Datum) SliceCursor[Datum] {
|
|
|
|
return SliceCursor[Datum]{
|
|
|
|
data: data,
|
|
|
|
offset: 0,
|
|
|
|
}
|
|
|
|
}
|
2024-09-02 17:04:00 +00:00
|
|
|
|
|
|
|
func (sc SliceCursor[Datum]) Read(dst []Datum) (n uint64, next Cursor[Datum], err error) {
|
2024-09-03 19:55:32 +00:00
|
|
|
if sc.offset == uint64(len(sc.data)) {
|
|
|
|
return 0, sc, io.EOF
|
|
|
|
}
|
2024-09-02 17:04:00 +00:00
|
|
|
copied := copy(dst, sc.data[sc.offset:])
|
|
|
|
if copied < len(dst) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
n = uint64(copied)
|
|
|
|
sc.offset += n
|
|
|
|
return n, sc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc SliceCursor[Datum]) Pos() uint64 {
|
|
|
|
return sc.offset
|
|
|
|
}
|
|
|
|
|
2024-09-03 22:10:27 +00:00
|
|
|
func (sc SliceCursor[Datum]) At(pos uint64) Cursor[Datum] {
|
|
|
|
sc.offset = pos
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
type ReaderAtCursor[T any] struct {
|
|
|
|
r ReaderAt[T]
|
2024-09-02 17:04:00 +00:00
|
|
|
pos uint64
|
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
func NewReaderAt[T any](r ReaderAt[T]) ReaderAtCursor[T] {
|
|
|
|
return ReaderAtCursor[T]{r: r}
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
func (rac ReaderAtCursor[T]) Read(dst []T) (uint64, Cursor[T], error) {
|
2024-09-02 17:04:00 +00:00
|
|
|
n, err := rac.r.ReadAt(dst, int64(rac.pos))
|
|
|
|
if n > 0 {
|
|
|
|
rac.pos += uint64(n)
|
|
|
|
}
|
|
|
|
return uint64(n), rac, err
|
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
func (rac ReaderAtCursor[T]) Pos() uint64 {
|
2024-09-02 17:04:00 +00:00
|
|
|
return rac.pos
|
|
|
|
}
|
|
|
|
|
2024-09-25 22:34:31 +00:00
|
|
|
func (rac ReaderAtCursor[T]) At(pos uint64) Cursor[T] {
|
2024-09-03 22:10:27 +00:00
|
|
|
rac.pos = pos
|
|
|
|
return rac
|
|
|
|
}
|