ring

package
v0.0.0-...-3f1de41 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: ISC Imports: 1 Imported by: 0

Documentation

Overview

Package ring is a specialized adaption of `container/ring` for use in LIRS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metadata

type Metadata[Key comparable] struct {
	// Name is the identifier of the data this metadata is bound to.
	Name Key
	// LIR (Low Inter-Reference Recency) is true
	// if the page is frequently accessed relative to other pages,
	// to be spared from eviction because of its short "reuse distance".
	// See LIRS algorithm for more detail.
	LIR bool
	// Resident is true if the data this metadata
	// is associated with, is to be considered valid.
	// I.e. true if the data is still stored in memory
	// and has not been nullified via eviction.
	Resident bool
	// Demoted is true if the page has been moved
	// to the test/ghost list (HIR non-resident).
	Demoted bool
	// Referenced is true if the page was
	// accessed since the last sweep.
	Referenced bool
	// Stacked is true if the page is currently in the LRU/LIRS stack.
	Stacked bool
}

Metadata stores LIRS (Low Inter‑Reference Recency Set) state of a cache page. It is used by CLOCK‑Pro and related eviction algorithms.

type Ring

type Ring[Key comparable, Value any] struct {
	Value Value
	Metadata[Key]
	// contains filtered or unexported fields
}

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.

func New

func New[Key comparable, Value any](n int) *Ring[Key, Value]

New creates a ring of n elements.

func (*Ring[Key, Value]) Do

func (r *Ring[Key, Value]) Do(yield func(Value) bool)

Do calls function f on each element of the ring, in forward order, stopping early if yield returns false. The behavior of Do is undefined if f changes *r.

func (*Ring[Key, Value]) Iter

func (r *Ring[Key, Value]) Iter() iter.Seq[*Ring[Key, Value]]

func (*Ring[Key, Value]) Len

func (r *Ring[Key, Value]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[Key, Value]) Link(s *Ring[Key, Value]) *Ring[Key, Value]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[Key, Value]) Move

func (r *Ring[Key, Value]) Move(n int) *Ring[Key, Value]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

func (*Ring[Key, Value]) Next

func (r *Ring[Key, Value]) Next() *Ring[Key, Value]

Next returns the next ring element. r must not be empty.

func (*Ring[Key, Value]) Prev

func (r *Ring[Key, Value]) Prev() *Ring[Key, Value]

Prev returns the previous ring element. r must not be empty.

func (r *Ring[Key, Value]) Unlink(n int) *Ring[Key, Value]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

Jump to

Keyboard shortcuts

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