ptree

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: GPL-3.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	MaxTreeDepth = 255
)

Variables

View Source
var ErrOutOfRoom = errors.New("out of room")

ErrOutOfRoom is returned when an encoder does not have enough room to write an entry.

Functions

func Copy

func Copy[T, Ref any](ctx context.Context, b *Builder[T, Ref], it *Iterator[T, Ref]) error

Copy copies all the entries from it to b.

func DebugTree

func DebugTree[T, Ref any](ctx context.Context, params ReadParams[T, Ref], x Root[T, Ref], w io.Writer) error

func ListEntries

func ListEntries[T, Ref any](ctx context.Context, params ReadParams[T, Ref], idx Index[T, Ref]) ([]T, error)

ListEntries returns a slice of all the entries pointed to by idx, directly. If idx points to other indexes directly, then ListEntries returns the entries for those indexes.

func MaxEntry

func MaxEntry[T, Ref any](ctx context.Context, params ReadParams[T, Ref], x Root[T, Ref], lt maybe.Maybe[T], dst *T) error

MaxEntry returns the entry in span with the greatest (ordered last) key.

func NextIndexFromSlice

func NextIndexFromSlice[T, Ref any](idxs []Index[T, Ref]) func(ctx context.Context, dst *Index[T, Ref]) error

NextIndexFromSlice returns a NextIndex function for StreamReaders which will iterate over the provided slice.

func PointsToEntries

func PointsToEntries[T, Ref any](root Root[T, Ref]) bool

PointsToEntries returns true if root points to non-index Entries

func PointsToIndexes

func PointsToIndexes[T, Ref any](root Root[T, Ref]) bool

PointsToIndexes returns true if root points to indexes.

Types

type Builder

type Builder[T, Ref any] struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder[T, Ref any](params BuilderParams[T, Ref]) *Builder[T, Ref]

func (*Builder[T, Ref]) Finish

func (b *Builder[T, Ref]) Finish(ctx context.Context) (*Root[T, Ref], error)

func (*Builder[T, Ref]) Put

func (b *Builder[T, Ref]) Put(ctx context.Context, x T) error

type BuilderParams

type BuilderParams[T, Ref any] struct {
	Store           Poster[Ref]
	MeanSize        int
	MaxSize         int
	Seed            *[16]byte
	Compare         CompareFunc[T]
	NewEncoder      func() Encoder[T]
	NewIndexEncoder func() IndexEncoder[T, Ref]
	Copy            func(dst *T, src T)
}

type CompareFunc

type CompareFunc[T any] func(a, b T) int

CompareFunc compares 2 keys

type Decoder

type Decoder[T, Ref any] interface {
	// ReadEntry parses an entry from src into ent.  It returns the number of bytes read or an error.
	Read(src []byte, ent *T) (int, error)
	// PeekEntry is like ReadEntry except it should not affect the state of the Decoder
	Peek(src []byte, ent *T) error
	// Reset returns the decoder to the original state for the star of a node.
	Reset(parent Index[T, Ref])
}

type Encoder

type Encoder[T any] interface {
	// Write encodes ent to dst and returns the number of bytes written or an error.
	// ErrOutOfRoom should be returned to indicate that the entry will not fit in the buffer.
	Write(dst []byte, ent T) (int, error)
	// EncodednLen returns the number of bytes that it would take to encode ent.
	// Calling EncodedLen should not affect the state of the Encoder.
	EncodedLen(ent T) int
	// Reset returns the encoder to it's orginally contructed state.
	Reset()
}

type Getter

type Getter[Ref any] interface {
	// Get fills buf with data at ref, or returns an error.
	// Get will always return an n <= MaxSize()
	Get(ctx context.Context, ref Ref, buf []byte) (n int, err error)
	// MaxSize returns the maximum amount of bytes that could be stored at a ref.
	MaxSize() int
}

Getter is used to retrieve nodes from storage by Ref

type Index

type Index[T, Ref any] struct {
	Ref Ref
	// IsNatural is true if this index is a natural boundary
	// A natural boundary.
	IsNatural bool

	Span  state.Span[T]
	Count uint
}

func ListIndexes

func ListIndexes[T, Ref any](ctx context.Context, params ReadParams[T, Ref], root Root[T, Ref]) ([]Index[T, Ref], error)

ListIndexes returns the immediate children of root if any.

func (Index[T, Ref]) Clone

func (idx Index[T, Ref]) Clone(cp func(dst *T, src T)) Index[T, Ref]

func (Index[T, Ref]) String

func (idx Index[T, Ref]) String() string

type IndexDecoder

type IndexDecoder[T, Ref any] interface {
	Read(src []byte, ent *Index[T, Ref]) (int, error)
	// PeekEntry is like ReadEntry except it should not affect the state of the Decoder
	Peek(src []byte, ent *Index[T, Ref]) error
	// Reset returns the decoder to the original state for the star of a node.
	Reset(parent Index[T, Ref])
}

type IndexEncoder

type IndexEncoder[T, Ref any] Encoder[Index[T, Ref]]

type IndexHandler

type IndexHandler[T, Ref any] func(Index[T, Ref]) error

type Iterator

type Iterator[T, Ref any] struct {
	// contains filtered or unexported fields
}

func NewIterator

func NewIterator[T, Ref any](params IteratorParams[T, Ref]) *Iterator[T, Ref]

func (*Iterator[T, Ref]) Next

func (it *Iterator[T, Ref]) Next(ctx context.Context, dst []T) (int, error)

func (*Iterator[T, Ref]) Peek

func (it *Iterator[T, Ref]) Peek(ctx context.Context, dst *T) error

func (*Iterator[T, Ref]) Seek

func (it *Iterator[T, Ref]) Seek(ctx context.Context, gteq T) error

type IteratorParams

type IteratorParams[T, Ref any] struct {
	Store           Getter[Ref]
	NewDecoder      func() Decoder[T, Ref]
	NewIndexDecoder func() IndexDecoder[T, Ref]
	Compare         CompareFunc[T]
	Copy            func(dst *T, src T)

	Root Root[T, Ref]
	Span state.Span[T]
}

type Poster

type Poster[Ref any] interface {
	// Posts stores the data and returns a Ref for retrieving it.
	Post(ctx context.Context, data []byte) (Ref, error)
	// MaxSize is the maximum amount of data that can be Posted in bytes
	MaxSize() int
}

Poster is used to store nodes in storage and retrieve a Ref

type ReadParams

type ReadParams[T, Ref any] struct {
	Store           Getter[Ref]
	NewDecoder      func() Decoder[T, Ref]
	NewIndexDecoder func() IndexDecoder[T, Ref]
	Compare         CompareFunc[T]
}

ReadParams are parameters needed to read from a tree

type Root

type Root[T, Ref any] struct {
	Index[T, Ref]
	Depth uint8
}

Root is the root of the tree It contains the same information as an Index, plus the depth of the tree

func (*Root[T, Ref]) String

func (r *Root[T, Ref]) String() string

type Store

type Store[Ref any] interface {
	Getter[Ref]
	Poster[Ref]
}

type StreamReader

type StreamReader[T, Ref any] struct {
	// contains filtered or unexported fields
}

func NewStreamReader

func NewStreamReader[T, Ref any](params StreamReaderParams[T, Ref]) *StreamReader[T, Ref]

func (*StreamReader[T, Ref]) Buffered

func (r *StreamReader[T, Ref]) Buffered() int

Buffered returns the number of bytes in the StreamReader which have not been read.

func (*StreamReader[T, Ref]) Next

func (r *StreamReader[T, Ref]) Next(ctx context.Context, dst *T) error

func (*StreamReader[T, Ref]) Peek

func (r *StreamReader[T, Ref]) Peek(ctx context.Context, dst *T) error

func (*StreamReader[T, Ref]) PeekNoLoad

func (r *StreamReader[T, Ref]) PeekNoLoad(dst *T) error

func (*StreamReader[T, Ref]) Seek

func (r *StreamReader[T, Ref]) Seek(ctx context.Context, gteq T) error

type StreamReaderParams

type StreamReaderParams[T, Ref any] struct {
	Store     Getter[Ref]
	Decoder   Decoder[T, Ref]
	Compare   CompareFunc[T]
	NextIndex func(ctx context.Context, dst *Index[T, Ref]) error
}

type StreamWriter

type StreamWriter[T, Ref any] struct {
	// contains filtered or unexported fields
}

func NewStreamWriter

func NewStreamWriter[T, Ref any](params StreamWriterParams[T, Ref]) *StreamWriter[T, Ref]

func (*StreamWriter[T, Ref]) Append

func (w *StreamWriter[T, Ref]) Append(ctx context.Context, ent T) error

func (*StreamWriter[T, Ref]) Buffered

func (w *StreamWriter[T, Ref]) Buffered() int

func (*StreamWriter[T, Ref]) Flush

func (w *StreamWriter[T, Ref]) Flush(ctx context.Context) error

type StreamWriterParams

type StreamWriterParams[T, Ref any] struct {
	Store    Poster[Ref]
	Seed     *[16]byte
	MeanSize int
	MaxSize  int
	Compare  func(a, b T) int
	Encoder  Encoder[T]
	// OnIndex must not retain the index after the call has ended.
	OnIndex IndexHandler[T, Ref]
	Copy    func(dst *T, src T)
}

Jump to

Keyboard shortcuts

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