Segments

June 12, 2026 ยท View on GitHub

A segment is an ordered record set.

ZoneTree uses segments as the movement units of the storage engine. Writes start in a writable memory segment, frozen memory segments are merged into disk segments, and large disk segments can move into the bottom segment queue.

writes
  |
  v
mutable segment
  |
  | move forward
  v
read-only segment queue
  |
  | merge
  v
DiskSegment
  |
  | exceeds DiskSegmentMaxItemCount
  v
bottom segment queue

Storage Layers

LayerCode shapeMeaning
Mutable segmentIMutableSegmentWritable in-memory segment protected by a WAL.
Read-only segmentsIReadOnlySegment queueFrozen in-memory segments waiting for merge.
DiskSegmentIDiskSegment slotPersistent disk segment used by normal merge.
Bottom segmentsIDiskSegment queuePersistent disk segments below the DiskSegment slot.

Disk Segment Shapes

In code, IDiskSegment<TKey, TValue> is the important abstraction. It means "a persistent segment ZoneTree can read, merge, back up, and drop."

IDiskSegment
  |
  +-- single-part disk segment
  |
  +-- multipart disk segment
TermMeaning
Disk segmentPersistent ordered segment.
Single-part disk segmentOne physical disk segment layout.
Multipart disk segmentOne logical disk segment made from ordered parts.
PartReusable disk segment inside a multipart disk segment.

Single-part disk segments have four physical variations. ZoneTree chooses the variation from the key and value shape:

VariationUsed when
Fixed-size key + fixed-size valueboth key and value have fixed unmanaged size
Fixed-size key + variable-size valuekey has fixed unmanaged size, value is variable-size
Variable-size key + fixed-size valuekey is variable-size, value has fixed unmanaged size
Variable-size key + variable-size valueboth key and value are variable-size

These variations let compact unmanaged keys or values use simpler disk layouts, while strings, arrays, reference types, and other variable-size serialized values use header/offset layouts.

Single-Part And Multipart

A single-part disk segment owns its physical files directly.

single-part disk segment
  segment id: 10
  files:
    10.data.z
    10.data.h.z
    10.sparse.z

A multipart disk segment owns a small descriptor and points to ordered disk-segment parts.

multipart disk segment
  segment id: 20
  descriptor:
    20.multi
  parts:
    segment id: 11
    segment id: 12
    segment id: 13

From the outside, both are one IDiskSegment. They expose one logical record count, one ordered keyspace, and the same read/merge/drop surface.

Why Multipart Exists

Multipart disk segments let ZoneTree carry unchanged parts forward during merge.

old disk segment:
[ A ][ B ][ C ][ D ]

new records overlap B

new disk segment:
[ carry A ][ write B2 ][ carry C ][ carry D ]

The old part files are immutable. ZoneTree does not patch them in place. A clean part can be reused by the next logical disk segment, while changed ranges are written as new parts.

Null Disk Segment

NullDiskSegment<TKey, TValue> is an empty placeholder for the DiskSegment slot.

It appears when there is no disk segment in that slot, for example after a large disk segment moves into the bottom segment queue.

DiskSegment slot:
  NullDiskSegment

bottom segment queue:
  disk segment 101
  disk segment 102

Common Names

NameRefers to
MutableSegmentMaxItemCountrecord count limit before the mutable segment moves forward
DiskSegmentMaxItemCountrecord count limit before DiskSegment moves to bottom segments
ReadOnlySegmentsCountfrozen in-memory segment count
MutableSegmentRecordCountrecords in the writable memory segment
ReadOnlySegmentsRecordCountrecords across frozen memory segments
TotalRecordCountphysical records across mutable, read-only, disk, and bottom segment layers

For the full storage flow, see LSM Tree. For multipart merge behavior, see write amplification.