tape

package module
v0.0.0-...-c9879b4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

tape

Documentation

Overview

Package tape implements a file-based FIFO queue.

The typical use case is to use an os.File as backing storage and has special handling for this use case. However, any type that implements io.ReaderAt and io.WriterAt can be used. The documentation is written with the assumption that the backing storage is an os.File.

Operations

The queue does not support the "pop" operation. This prevents callers from accidentally removing data from the queue before the operation that uses the data is successful.

The queue does not shrink as elements are removed. To shrink the queue, wait until its size drops to zero call Reset once or just it and create a new one.

Durability

The queue is designed to be resilient against corruption from power loss and program crashes. For example, if an addition to the queue fails partway through, the queue will remain uncorrupted so long as the 32-byte queue header is updated atomically (or not at all). Queue operations are flushed to disk after each operation.

On macOS and iOS, operations are flushed to disk using fcntl(F_BARRIERFSYNC) instead of calling Sync. This is because Sync uses fcntl(F_FULLFSYNC) which (objectively) has horrendous performance and, according to Apple, usually isn't necessary. For more information, see Apple's documentation1.

Example usage (error handing is omitted for brevity):

q, _:= New(f)
q.Add([]byte("hello!"))
got, _ := q.Peek()
if string(got) == "hello!" {
    // The data is correct, so pop it from the queue.
    q.Remove()
}

It is heavily based off Square's Tape library0.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmpty = errors.New("tape: queue is empty")

ErrEmpty is returned when the queue is empty.

Functions

This section is empty.

Types

type CorruptedError

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

CorruptedError is returned when the queue has been corrupted.

func (*CorruptedError) Error

func (e *CorruptedError) Error() string

func (*CorruptedError) Unwrap

func (e *CorruptedError) Unwrap() error

type File

type File interface {
	io.ReaderAt
	io.WriterAt
}

File is the underlying file.

type Option

type Option func(*qopts)

Option configures a queue.

func WithNoSync

func WithNoSync() Option

WithNoSync disables syncing the queue to permanent storge.

This should only be set in tests to make the disk-based tests quicker.

func WithSize

func WithSize(n int64) Option

WithSize configures the default allocation size for new files.

type Queue

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

Queue is a file-based FIFO queue.

func New

func New(f File, opts ...Option) (*Queue, error)

New creates a queue from the File.

func (*Queue) Add

func (q *Queue) Add(data []byte) error

Add appends an element to the end of the queue.

func (*Queue) Available

func (q *Queue) Available() int64

Available returns the number of bytes available for new queue elements before growing.

func (*Queue) Cap

func (q *Queue) Cap() int64

Cap returns the total space allocated by the queue.

func (*Queue) Len

func (q *Queue) Len() int

Len returns the number of entries in the queue.

func (*Queue) Peek

func (q *Queue) Peek() ([]byte, error)

Peek returns the first entry from the queue, but does not remove it.

func (*Queue) Range

func (q *Queue) Range(fn func(p []byte) bool) error

Range calls fn for each element element in the queue, or until fn returns false.

The slice passed to the function is only valid until the function returns.

It is undefined behavior to call Add or Remove while iterating over the elements.

func (*Queue) Remove

func (q *Queue) Remove(n int) error

Remove deletes the first n entries from the queue.

If the queue is empty or n <= 0, RemoveN returns nil.

func (*Queue) Reset

func (q *Queue) Reset() error

Reset clears the queue to its original size.

func (*Queue) Size

func (q *Queue) Size() int64

Size reports the number of bytes being used by queue elements and metadata.

type Syncer

type Syncer interface {
	Sync() error
}

Syncer is an optional interface used to flush the File to permanent storage.

type Truncater

type Truncater interface {
	Truncate(int64) error
}

Truncater is an optional interface used to grow or shrink the size of the File as necessary.

Jump to

Keyboard shortcuts

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