buffer

package
v1.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	io.Writer
	io.ReaderAt
	io.Closer
}

Backend is a backing store of bytes for a Backend.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer lets one write to some Backend and lazily create readers from it.

func New

func New(backend Backend, writeAhead int64) *Buffer

New constructs a Buffer using the underlying Backend and allowing the writer to write writeAhead extra bytes in front of the most advanced reader.

func (*Buffer) DoneReading

func (w *Buffer) DoneReading(err error)

DoneReading signals to the Buffer that no more Read calls are coming and that Write calls should return the provided error. The first call that causes both DoneReading and DoneWriting to have been called closes the underlying backend.

func (*Buffer) DoneWriting

func (w *Buffer) DoneWriting(err error)

DoneWriting signals to the Buffer that no more Write calls are coming and that Read calls should return the provided error. The first call that causes both DoneReading and DoneWriting to have been called closes the underlying backend.

func (*Buffer) Reader

func (w *Buffer) Reader() io.Reader

Reader returns a fresh io.Reader that can be used to read all of the previously and future written bytes to the Buffer.

func (*Buffer) Write

func (w *Buffer) Write(p []byte) (n int, err error)

Write appends the bytes in p to the Buffer. It blocks until the furthest advanced reader is less than the write ahead amount.

type ChunkBackend added in v1.13.0

type ChunkBackend struct {
	// contains filtered or unexported fields
}

ChunkBackend implements the Backend interface backed by a chained series of memory-pooled slices.

func NewChunkBackend added in v1.13.0

func NewChunkBackend(cap int64) (rv *ChunkBackend)

NewChunkBackend returns a ChunkBackend with the provided initial capacity. Internally it stitchers writes together into small chunks to reduce the size of allocations needed for small objects. It implements the Backend interface. TODO: evaluate the usefulness of `cap` for the chunk backend.

func (*ChunkBackend) Close added in v1.13.0

func (u *ChunkBackend) Close() error

Close releases memory and causes future calls to ReadAt and Write to fail.

func (*ChunkBackend) ReadAt added in v1.13.0

func (u *ChunkBackend) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads into the provided buffer p starting at off.

func (*ChunkBackend) Write added in v1.13.0

func (u *ChunkBackend) Write(p []byte) (n int, err error)

Write appends the data to the buffer.

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor keeps track of how many bytes have been written and the furthest advanced reader, letting one wait until space or bytes are available.

func NewCursor

func NewCursor(writeAhead int64) *Cursor

NewCursor constructs a new cursor that keeps track of reads and writes into some buffer, allowing one to wait until enough data has been read or written.

func (*Cursor) DoneReading

func (c *Cursor) DoneReading(err error) bool

DoneReading signals that no more Read calls will happen. It returns true the first time DoneWriting and DoneReading have both been called.

func (*Cursor) DoneWriting

func (c *Cursor) DoneWriting(err error) bool

DoneWriting signals that no more Write calls will happen. It returns true the first time DoneWriting and DoneReading have both been called.

func (*Cursor) ReadTo

func (c *Cursor) ReadTo(n int64)

ReadTo reports to the cursor that some reader read up to byte offset n.

func (*Cursor) WaitRead

func (c *Cursor) WaitRead(n int64) (m int64, ok bool, err error)

WaitRead blocks until the writer is done or until at least n bytes have been written. It returns min(n, w.written) letting the caller know the largest offset that can be read. The ok boolean is true if there are more bytes to be read. If writing is done with an error, then 0 and that error are returned. If writing is done with no error and the requested amount is at least the amount written, it returns the written amount, false, and nil.

func (*Cursor) WaitWrite

func (c *Cursor) WaitWrite(n int64) (m int64, ok bool, err error)

WaitWrite blocks until the readers are done or until the furthest advanced reader is within the writeAhead of the writer. It returns the largest offset that can be written. The ok boolean is true if there are readers waiting for more bytes. If reading is done with an error, then 0 and that error are returned. If reading is done with no error, then it returns the amount written, false, and nil.

func (*Cursor) WroteTo

func (c *Cursor) WroteTo(n int64)

WroteTo reports to the cursor that the writer wrote up to byte offset n.

type MemoryBackend

type MemoryBackend struct {
	// contains filtered or unexported fields
}

MemoryBackend implements the Backend interface backed by a slice.

func NewMemoryBackend

func NewMemoryBackend(cap int64) (rv *MemoryBackend)

NewMemoryBackend returns a MemoryBackend with the provided initial capacity. It implements the Backend interface.

func (*MemoryBackend) Close

func (u *MemoryBackend) Close() error

Close releases memory and causes future calls to ReadAt and Write to fail.

func (*MemoryBackend) ReadAt

func (u *MemoryBackend) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads into the provided buffer p starting at off.

func (*MemoryBackend) Write

func (u *MemoryBackend) Write(p []byte) (n int, err error)

Write appends the data to the buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL