internal/compaction/compaction.go
internal/compaction · 273 lines · 15 declarations · source
This file carries the package documentation, rendered on the package page.
Declarations
const Branch, Verbatim, MaxLevel
const (
// Branch is how many consecutive units, turns or segments of one level, one segment covers.
Branch = 8
// Verbatim is how many of the newest turns a context keeps as they were said.
Verbatim = 8
// MaxLevel bounds the hierarchy; 8^6 turns is more history than any subject holds.
MaxLevel = 6
)
type Turn
type Turn struct {
Offset int64
Size int
}
Turn is one formed turn as the store reports it: its offset in the log and its size in characters, which is what the assembly budgets.
type Segment
type Segment struct {
ID string
Level int
From int64
To int64
// Covered is how many turns the range held when the segment was written.
Covered int
Size int
}
Segment is one written summary: the level it sits at and the contiguous offset range it covers, inclusive at both ends.
type Plan
type Plan struct {
Level int
From int64
To int64
// Units are the segments the new one summarises (level 2 and up); empty at level 1, where the
// material is the turns themselves.
Units []Segment
// Turns is how many turns the range covers, taken from the units or from the turns.
Turns int
}
Plan is the next segment to write, or nothing.
var ErrDisorder
var ErrDisorder = errors.New("turns and segments must be in offset order")
func Next
func Next(turns []Turn, segments []Segment) (Plan, bool, error)
Next says what to write next for one subject, given every formed turn and every segment that exists. It returns false when the history is fully rolled up.
The newest Verbatim turns are never covered: a segment written over turns that are still shown verbatim would be paid for and never read. Level 1 is offered first, oldest run first, because a level cannot be built over a lower one that has gaps.
const MinGapRun
const MinGapRun = 2
MinGapRun is the smallest run written when it is bounded by an existing segment rather than by reaching Branch: a single turn summarised is a summary of nothing.
func plan
func plan(level int, units []Segment) Plan
type Context
type Context struct {
Segments []Segment
Turns []Turn
Characters int
Truncated bool
}
Context is what a caller is handed: the verbatim turns, oldest first, and the segments covering what is older, oldest first, plus how much was cut.
func Assemble
func Assemble(turns []Turn, segments []Segment, budget int) (Context, error)
Assemble picks the newest Verbatim turns and, for everything older, the highest-level segment covering each offset, then cuts from the oldest end to fit the budget.
type item
type item struct {
segment *Segment
turn *Turn
size int
from int64
}
func covering
func covering(sorted []Segment, offset int64) (Segment, bool)
func coveredAt
func coveredAt(segments []Segment, offset int64) bool
func adjacent
func adjacent(a, b Segment, turns []Turn) bool
adjacent says whether two segments of one level are consecutive in the log: no compactable turn lies between them.
func sorted
func sorted(turns []Turn) bool