ringbuffer

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package ringbuffer provides a ring buffer for storing blocks of bytes. Bytes are written and read in discrete blocks. If the buffer becomes full, then writing to it will evict the oldest blocks until there is space for a new one.

Index

Constants

View Source
const BlockHeaderSize = 5

BlockHeaderSize is the size of the block header, in bytes.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockHeader

type BlockHeader struct {
	// Tag is the block's tag.
	Tag BlockTag

	// Size is the size of the block data, in bytes.
	Size uint32
}

BlockHeader holds a fixed-size block header.

type BlockTag

type BlockTag uint8

BlockTag is a block tag, which can be used for classification.

type Buffer

type Buffer struct {

	// Evicted will be called when an old block is evicted to make place for a new one.
	Evicted func(BlockHeader)
	// contains filtered or unexported fields
}

Buffer is a ring buffer of byte blocks.

func New

func New(size int) *Buffer

New returns a new Buffer with the given size in bytes.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the buffer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the number of bytes currently in the buffer, including block-accounting bytes.

func (*Buffer) WriteBlock

func (b *Buffer) WriteBlock(p []byte, tag BlockTag) (int, error)

WriteBlock writes p as a block to b, with tag t.

If len(p)+BlockHeaderSize > b.Cap(), bytes.ErrTooLarge will be returned. If the buffer does not currently have room for the block, then the oldest blocks will be evicted until enough room is available.

func (*Buffer) WriteBlockTo

func (b *Buffer) WriteBlockTo(w io.Writer) (header BlockHeader, written int64, err error)

WriteBlockTo writes the oldest block in b to w, returning the block header and the number of bytes written to w.

Jump to

Keyboard shortcuts

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