timebucket

package
v0.49.3 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 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

Overview

Package timebucket shares the aligned merge ladder and bounded selection between both engines.

Index

Constants

View Source
const MaxOpenWriters = 32

MaxOpenWriters bounds the day-keyed writers one merge holds open: a month of days, so a batch of straddlers spanning weeks is still written in one pass without finishing a day early.

Variables

View Source
var Ladder = []int64{
	int64(time.Hour),
	int64(6 * time.Hour),
	int64(24 * time.Hour),
}

Ladder is the merge bucket ladder, ascending. Each level divides the next, so a bucket nests exactly inside its parent and a part promoted upward never straddles. The top level is the widest part a merge builds, and so the coarsest time locality a query can rely on.

Functions

func CanCompact added in v0.49.3

func CanCompact(lo, hi, level, newest int64) bool

CanCompact reports whether a part may join size compaction at level. The newest larger bucket admits only parts already too wide for its preceding level.

func End

func End(ts, level int64) int64

End returns the last timestamp of the level-aligned bucket holding ts, saturating at the int64 maximum instead of overflowing past it.

func Finest

func Finest(lo, hi int64) (int64, bool)

Finest returns the narrowest level whose bucket contains [lo, hi], reporting false for a straddler: a span crossing a top-level boundary.

func Fits

func Fits(lo, hi, level int64) bool

Fits reports whether [lo, hi] lies within one level-aligned bucket.

func Forced

func Forced[P any](
	src []P,
	span func(P) (lo, hi int64),
	size func(P) int64,
	forced, sealed func(P) bool,
	capBytes int64,
	maxParts int,
) []P

Forced returns the forced rewrite for one merge, in src order. The oldest forced part picks the bucket; that bucket's forced parts are taken oldest first up to capBytes of their size (≤ 0 ⇒ unbounded) and maxParts of them (≤ 0 ⇒ unbounded), at least one, and its parts neither forced nor sealed then fill what remains, smallest first. A forced straddler belongs to no bucket and goes alone.

func Of

func Of(ts, level int64) int64

Of returns the start of the level-aligned bucket holding ts. It rounds toward negative infinity, not toward zero: Go's % keeps the dividend's sign, which would make the bucket at ts = -1 start at 0 and overlap its successor.

func Runs

func Runs(ts []int64, fn func(lo, hi int) error) error

Runs calls fn once per run of the ascending ts inside one top-level bucket, with the run's [lo, hi) index range.

func SplitsFrom

func SplitsFrom[P any](parts []P, span func(P) (lo, hi int64), start int64) bool

SplitsFrom reports whether merging parts from start on writes more than one top-level bucket.

func Straddlers

func Straddlers[P any](src []P, span func(P) (lo, hi int64), size func(P) int64, capBytes int64, maxParts int) []P

Straddlers returns the straddlers to split in one merge: oldest first, up to capBytes of their size (≤ 0 ⇒ unbounded) and maxParts of them (≤ 0 ⇒ unbounded), at least one, in src order.

Merging straddlers together rather than one at a time is what makes a backlog converge: their rows land in the same few buckets, so one merge writes a part per bucket touched, not per straddler.

func Top

func Top() int64

Top is the widest ladder level, and so the boundary every merge output is split on.

func Union

func Union[P any](parts []P, span func(P) (lo, hi int64)) (lo, hi int64)

Union returns the union of the parts' spans.

Types

type Router

type Router[W any] struct {
	Open          func(bucket int64) (W, error)
	Finish        func(W) error
	Resident      func(W) int64
	Held          func() int64
	MaxOpen       int
	ResidentLimit int64
	ReserveRun    int64
	// contains filtered or unexported fields
}

Router hands a merge's output rows to one writer per top-level bucket, so a merge whose rows span several days writes a part per day in a single pass over its inputs.

At most MaxOpen writers are open, and together with Held — what the caller holds beside them — they hold less than ResidentLimit (≤ 0 ⇒ unbounded) after every run Router.Append routes: past either bound the writer holding the most is finished early. A writer's size is whatever Resident reports, so it must count everything the writer holds. A day can therefore end up in several parts, each of which still fits the day, and the ladder merges them later. The limit is checked per run rather than per series, so one series spanning every open day overshoots it by one run, not by one writer's worth per day — unless ReserveRun keeps room for the next run: the writers are then shed once they leave less than ReserveRun or the largest run so far, whichever is more, and exceed the limit only by a run that outgrows both, by the difference.

func (*Router[W]) Append

func (r *Router[W]) Append(ts int64, add func(W) (full bool, err error)) error

Append routes one run of rows, all in the top-level bucket holding ts, to that day's writer: add appends it and reports whether the writer is full, which finishes it. The open writers are then shed back under ResidentLimit.

func (*Router[W]) Close

func (r *Router[W]) Close() error

Close finishes every open writer, oldest bucket first.

func (*Router[W]) Drop

func (r *Router[W]) Drop(release func(W))

Drop hands every open writer to release unfinished, for a merge that failed.

func (*Router[W]) Peak

func (r *Router[W]) Peak() (total, run int64)

Peak returns the most the open writers held together as a run landed, and the most one run added to a writer: the first exceeds ResidentLimit by at most the second.

func (*Router[W]) Seal

func (r *Router[W]) Seal(ts int64) error

Seal finishes the writer for the bucket holding ts, if one is open.

func (*Router[W]) Shed

func (r *Router[W]) Shed() error

Shed finishes the largest writers while the open ones together hold ResidentLimit or more, less the room kept for the next run. The peak is tracked whether or not a limit is set.

func (*Router[W]) Writer

func (r *Router[W]) Writer(ts int64) (W, error)

Writer returns the writer for the top-level bucket holding ts, opening it if needed.

Jump to

Keyboard shortcuts

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