gizfs:ordered
An ordered map from byte strings to integers: a B+tree whose inner nodes keep each child's largest key and entry count, so seek, insert and delete cost O(log n) and rank queries too. Keys compare as bytes; callers encode what they order so that byte order is their order (the caller's value encoding). One structure serves retained roots, database ordered indexes and key-value sorted sets.
Nodes live in one pool and name each other by position. The pool is kept in pages of NP nodes: when a tree is shared with other threads' readers (an engine or storage snapshot), a write copies the pages it changes, not every node. Leaves are linked both ways for scans. A child's recorded largest key may be stale after a delete (larger than its real largest): it still bounds the child, so routing stays correct, and it is never smaller.
import * as Ordered from "gizfs:ordered";Types
Ordered
type OrderedAn ordered map from byte keys to integers in memory (a B+ tree with counts, so rank and position are fast), or a view of a paged tree in a file.
pages:M39.DirectoryPages<Vec<M39.Node>>used:i64free:M39.DirectoryPages<i64>root:i64size:i64weighted:boolpopulation:i64directoryNodes:i64directorySlots:i64leafKeyCredit:i64innerKeyCredit:i64structuralCredit:i64cold:Vec<M39.ColdPages>
ColdPages
type ColdPages = { disk: Disk; path: string; end: Task.Atomic; cursor: i64; }Where a tree's evicted leaf pages are (Ordered.evictLeaves): an append-only spill file, never rewritten, so every copy of the tree reads back the page version its directory names. The file is scratch: the WAL and segment files rebuild the tree at open, which removes it.
disk:M37.Diskpath:stringend:gizlang_task.Atomiccursor:i64
Pos
type Pos = { node: i64; at: i64; }A position: an entry of a leaf, or past the end when node is -1.
node:i64at:i64
OrderedImage
type OrderedImage = { pages: PageDirectory<Vec<Node>>; used: i64; free: PageDirectory<i64>; root: i64; size: i64; weighted: bool; population: i64; directoryNodes: i64; directorySlots: i64; leafKeyCredit: i64; innerKeyCredit: i64; structuralCredit: i64; cold: Vec<ColdPages>; }An Ordered with its pages in owned directories and no native owner, as a value with no Ref: it crosses threads (Task.cell), as a database's read view does.
pages:_gizlang_sdk_collections.PageDirectory<Vec<M39.Node>>used:i64free:_gizlang_sdk_collections.PageDirectory<i64>root:i64size:i64weighted:boolpopulation:i64directoryNodes:i64directorySlots:i64leafKeyCredit:i64innerKeyCredit:i64structuralCredit:i64cold:Vec<M39.ColdPages>
OrderedEntries
type OrderedEntries = { keys: Vec<bytes>; vals: Vec<i64>; }Entries as two parallel lists: the keys and their values.
keys:Vec<bytes>vals:Vec<i64>
OrderedRun
type OrderedRun = { keys: Vec<bytes>; vals: Vec<i64>; past: Pos; }Up to N entries from P on, toward the end or (DESC) the start, and the position past them.
keys:Vec<bytes>vals:Vec<i64>past:M39.Pos
OrderedEntry
type OrderedEntry = { key: bytes; value: i64; }A key and its value.
key:bytesvalue:i64
PagedOrdered
type PagedOrdered = { root: StoredPage; count: i64; height: i64; maxPageBytes: i64; }A paged ordered tree in a file: its root page, its number of entries, its height and its greatest page size.
root:M37.StoredPagecount:i64height:i64maxPageBytes:i64
OrderedPosition
type OrderedPosition = { root: PagePosition; count: i64; height: i64; maxPageBytes: i64; }Where a paged tree is, as it is stored: the position of its root page and its size.
root:M37.PagePositioncount:i64height:i64maxPageBytes:i64
OrderedPages
type OrderedPages = { tree: PagedOrdered; end: i64; }A paged tree that a build made, and the end of its pages in the file.
tree:M39.PagedOrderedend:i64
OrderedCursor
type OrderedCursor = { tree: PagedOrdered; frames: Vec<OrderedFrame>; leaf: Option<OrderedNodeLease>; at: i64; descending: bool; lease: ResourceLease; closed: bool; }A cursor over the entries of a paged tree, in key order or in reverse.
tree:M39.PagedOrderedframes:Vec<M39.OrderedFrame>leaf:_gizlang_sdk_core.Option<M39.OrderedNodeLease>at:i64descending:boollease:M37.ResourceLeaseclosed:bool
OrderedUpdate
type OrderedUpdate = { tree: PagedOrdered; end: i64; changed: bool; fresh: bool; }A paged tree after a change: its new tree, the end of its pages, and whether the change changed it and wrote new pages.
tree:M39.PagedOrderedend:i64changed:boolfresh:bool
OrderedEdit
type OrderedEdit = { key: bytes; value: Option<i64>; }A change of one key: a new value, or none to remove it.
key:bytesvalue:_gizlang_sdk_core.Option<i64>
Functions
newOrdered
function newOrdered(): OrderedAn empty ordered map.
Effects: fail, read, write.
newManagedOrdered
function newManagedOrdered(): OrderedRef-held metadata keeps node-page roots as identities; ordinary value trees retain their buffers under the ordinary allocation budget.
Effects: fail, read, write.
orderedOfImage
function orderedOfImage(image: OrderedImage): OrderedThe ordered map of IMAGE.
Effects: fail.
nativeOrdered
function nativeOrdered(owner: Ref<NativePages>): Result<Ordered, IoError>Same ordered-map contract, with roots in the caller's common page owner.
Effects: fail, read, write, console, io.
adoptOrdered
function adoptOrdered(owner: Ref<NativePages>, tree: PagedOrdered): OrderedThe ordered map that reads the paged tree TREE of OWNER.
Effects: fail, read, write.
treeRoot
function treeRoot(tree: Ordered, written: bool): PagedOrderedThe root of the native TREE for a snapshot (WRITTEN: nativeRoot) or for its size (storedRoot).
Effects: fail, read, write, console, io.
newWeightedOrdered
function newWeightedOrdered(): OrderedOptional population/peak summaries share the same tree, page pool and mutations.
Effects: fail, read, write.
orderedSorted
function orderedSorted(keys: Vec<bytes>, vals: Vec<i64>): OrderedA tree of KEYS, strictly ascending, and their VALS, built bottom up: the leaves take the entries in order and each inner level their maxima, where putting them one by one descended the tree and split it for each key (a million-key rebuild was most of ADD PRIMARY KEY and of a large memtable flush).
Effects: fail, read, write, console, io.
prefixEnd
function prefixEnd(prefix: bytes): Option<bytes>The smallest key above every key that starts with prefix, or none when no key is (the
prefix is all 0xFF): with seek, the end of a prefix range.
Effects: fail.
formats
function formats(): Vec<Format>The on-disk formats of this module. A test opens a committed store sample of every version.
Effects: fail.
orderedPosition
function orderedPosition(tree: PagedOrdered): OrderedPositionThe stored position of TREE.
bindOrdered
function bindOrdered(position: OrderedPosition, object: bytes, path: string): PagedOrderedThe paged tree at POSITION in OBJECT of the file at PATH.
writeOrderedPosition
function writeOrderedPosition(out: mut bytes, position: OrderedPosition): voidAppends POSITION.
Effects: fail.
buildOrderedPagesContext
function buildOrderedPagesContext(file: File, path: string, object: bytes, credits: mut ResourceContext, maxPageBytes: i64, pull: () => Result<Option<OrderedEntry>, IoError>): Result<OrderedPages, IoError>Bounded bulk build from strictly ascending keys. The file must already be caller-owned staging storage; errors roll back its appended tail. Success does not sync or publish a root: the existing Engine checkpoint/WAL owner must do that before making it visible.
Effects: fail, read, write, console, io.
buildOrderedPages
function buildOrderedPages(file: File, path: string, object: bytes, credits: mut Resources, maxPageBytes: i64, pull: () => Result<Option<OrderedEntry>, IoError>): Result<OrderedPages, IoError>A paged tree of the entries that PULL gives in key order, written to FILE at PATH from the end of OBJECT. Pages are at most MAXPAGEBYTES.
Effects: fail, read, write, console, io.
Methods
Ordered.evictLeaves
function Ordered.evictLeaves(self: mut Ordered, disk: Disk, path: string, bytes: i64): Result<i64, IoError>Moves about bytes of leaf pages to the spill file, on from where the last eviction stopped, as
LeanStore picks pages to cool without tracking each access; its bytes in memory left. A page with an
inner node, the root's included, stays: a lookup then reads at most one page back.
Effects: fail, read, write, console, io.
Ordered.residentBytes
function Ordered.residentBytes(self: Ordered): i64The bytes of the tree's nodes held in memory (their reservations).
Effects: fail.
Ordered.image
function Ordered.image(self: Ordered): Option<OrderedImage>The image of the tree, or none for a managed or native one. It shares the tree's pages.
Effects: fail.
Ordered.nativeRoot
function Ordered.nativeRoot(self: Ordered): Option<PagedOrdered>The paged tree under the map, when the map reads one.
Effects: fail, read, write, console, io.
Ordered.storedRoot
function Ordered.storedRoot(self: Ordered): Option<PagedOrdered>The root of the pages of a native tree, without its pending edits. It encodes in as many bytes as the root that writes them (nativeRoot), so a size needs no write.
Effects: fail, read.
Ordered.count
function Ordered.count(self: Ordered): i64The number of entries.
Ordered.recycledNodes
function Ordered.recycledNodes(self: Ordered): i64Retained pool nodes available for reuse, independent of their page carrier.
Effects: fail.
Ordered.weightCount
function Ordered.weightCount(self: Ordered): Option<i64>The sum of the weights, when the map is weighted.
Ordered.putWeight
function Ordered.putWeight(self: mut Ordered, key: bytes, weight: i64): Result<bool, string>Sets the weight of KEY in a weighted map; true when KEY is new. It fails when the map is not weighted or WEIGHT is negative.
Effects: fail, read, write, console, io.
Ordered.keyReservation
function Ordered.keyReservation(self: Ordered, includeLeaf: bool): i64Reserved key-buffer credit, maintained only on touched nodes. Separator references always count.
Effects: fail.
Ordered.reservation
function Ordered.reservation(self: Ordered, includeLeaf: bool): i64Conservative single-version reservation: bounded node vectors, directories and key buffers. includeLeaf=false excludes only leaf keys already charged by a caller's canonical bag wire owner. It never excludes separator keys. Shared historical versions and allocator overhead are not measured.
Effects: fail.
Ordered.entries
function Ordered.entries(self: Ordered): OrderedEntriesEvery entry in key order, read a leaf at a time (first and next read a node for each entry).
Effects: fail, read, write, console, io.
Ordered.putFresh
function Ordered.putFresh(self: mut Ordered, key: bytes, val: i64): boolKEY, which the caller knows is not in the tree, put with VAL: a native tree reads no page to learn it.
Effects: fail, read, write, console, io.
Ordered.removePresent
function Ordered.removePresent(self: mut Ordered, key: bytes): boolKEY, which the caller knows is in the tree, removed: a native tree reads no page to learn it.
Effects: fail, read, write, console, io.
Ordered.put
function Ordered.put(self: mut Ordered, key: bytes, val: i64): boolSets KEY to VAL; true when KEY is new.
Effects: fail, read, write, console, io.
Ordered.swap
function Ordered.swap(self: mut Ordered, key: bytes, val: i64): Option<i64>Puts KEY and gives the value it replaced, in one descent (a get, then a put, read the path twice).
Effects: fail, read, write, console, io.
Ordered.remove
function Ordered.remove(self: mut Ordered, key: bytes): boolRemoves key; whether it was there. An emptied node leaves its parent; a root with one
child gives way to it.
Effects: fail, read, write, console, io.
Ordered.rekey
function Ordered.rekey(self: mut Ordered, oldKey: bytes, newKey: bytes, val: i64): Result<bool, string>A key transfer uses one pool-page candidate, including both search paths. Collision checks precede all edits and leave diagnostics unchanged.
Effects: fail, read, write, console, io.
Ordered.get
function Ordered.get(self: Ordered, key: bytes): Option<i64>The value of key, or none.
Effects: fail, read, write, console, io.
Ordered.seek
function Ordered.seek(self: Ordered, key: bytes): PosThe first entry not below key.
Effects: fail, read, write, console, io.
Ordered.first
function Ordered.first(self: Ordered): PosThe position of the first entry.
Effects: fail, read, write, console, io.
Ordered.last
function Ordered.last(self: Ordered): PosThe position of the last entry.
Effects: fail, read, write, console, io.
Ordered.next
function Ordered.next(self: Ordered, p: Pos): PosThe position after P.
Effects: fail, read, write, console, io.
Ordered.prev
function Ordered.prev(self: Ordered, p: Pos): PosThe position before P.
Effects: fail, read, write, console, io.
Ordered.keyAt
function Ordered.keyAt(self: Ordered, p: Pos): bytesThe key at P.
Effects: fail, read, write, console, io.
Ordered.valAt
function Ordered.valAt(self: Ordered, p: Pos): i64The value at P.
Effects: fail, read, write, console, io.
Ordered.run
function Ordered.run(self: Ordered, p: Pos, desc: bool, n: i64): OrderedRunOrderedRun read a leaf at a time: keyAt, valAt and next or prev read the node for each entry, and were half of a walk back through a district's orders.
Effects: fail, read, write, console, io.
Ordered.before
function Ordered.before(self: Ordered, key: bytes): PosThe last entry below key.
Effects: fail, read, write, console, io.
Ordered.rank
function Ordered.rank(self: Ordered, key: bytes): i64How many entries are below key.
Effects: fail, read, write, console, io.
Ordered.atRank
function Ordered.atRank(self: Ordered, r0: i64): PosThe entry of rank r (0-based), or past the end.
Effects: fail, read, write, console, io.
Ordered.weightRank
function Ordered.weightRank(self: Ordered, key: bytes): Option<i64>Payload population below key; ordinary maps have no weight summaries.
Effects: fail, read, write, console, io.
Ordered.atWeight
function Ordered.atWeight(self: Ordered, rank: i64): PosEntry containing zero-based population rank; zero-weight entries contribute no positions.
Effects: fail, read, write, console, io.
Ordered.peakWeight
function Ordered.peakWeight(self: Ordered): Option<i64>Largest payload population; zero for an empty/zero-population weighted map.
Effects: fail, read, write, console, io.
Ordered.atPeak
function Ordered.atPeak(self: Ordered, descending: bool): PosFirst/last key at the largest positive population, in bounded tree-height work.
Effects: fail, read, write, console, io.
Ordered.firstEntry
function Ordered.firstEntry(self: Ordered): Option<OrderedEntry>The first entry. A native tree reads it from its pages and pending edits, with no write of them, when its pending removals of stored keys are its first stored keys (a free list that hands out its first slot); else it settles.
Effects: fail, read, write, console, io.
Ordered.prefixCount
function Ordered.prefixCount(self: Ordered, prefix: bytes): i64The number of keys that start with PREFIX.
Effects: fail, read, write, console, io.
Ordered.prefixValues
function Ordered.prefixValues(self: Ordered, prefix: bytes): Vec<i64>Materialize only the requested prefix, never unrelated entries.
Effects: fail, read, write, console, io.
Reader.orderedPosition
function Reader.orderedPosition(self: mut Reader): OrderedPositionThe next position of a paged tree.
Effects: fail, read, write, console, io.
PagedOrdered.seekManyContext
function PagedOrdered.seekManyContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, queries: Vec<bytes>): Result<Vec<Option<OrderedEntry>>, IoError>For each of the ascending queries, the first entry at or above it, or none: one descent for
the whole batch that loads each page it touches once, instead of one root-to-leaf path per
query. A query above every key finds none (deep review §8: a commit's keys probe the root
per page, not per row).
Effects: fail, read, write, console, io.
PagedOrdered.rankContext
function PagedOrdered.rankContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, key: bytes): Result<i64, IoError>Exact lookup/rank touches one page per level and drops decoded parents immediately.
Effects: fail, read, write, console, io.
PagedOrdered.getContext
function PagedOrdered.getContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, key: bytes): Result<Option<i64>, IoError>The value of KEY, or none; reads are charged to CREDITS.
Effects: fail, read, write, console, io.
PagedOrdered.atRankContext
function PagedOrdered.atRankContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, rank: i64): Result<Option<OrderedEntry>, IoError>Select by the same checked subtree counts used by rank; no leaf directory is loaded or reconstructed in RAM.
Effects: fail, read, write, console, io.
PagedOrdered.residentEntries
function PagedOrdered.residentEntries(self: PagedOrdered, cache: mut PageCache, key: bytes, count: i64): Option<Vec<OrderedEntry>>Up to count entries from the first key at or above key, read from resident decoded pages alone (PageCache.resident):
no pin, no grant and no cursor. None when a page on the path is not resident and decoded, or when the entries run
past their leaf; the caller then seeks with a cursor. Each child agrees with its parent's separator, and each node with
its link's level and count, as a cursor checks. An empty result means no key is at or above key.
Effects: fail, write.
PagedOrdered.residentAround
function PagedOrdered.residentAround(self: PagedOrdered, cache: mut PageCache, key: bytes, end: bytes): Option<Vec<OrderedEntry>>The last entry below KEY, then the entries at or above KEY and below END, from resident decoded pages alone (no pin, grant or cursor). None when a page on the path is not resident and decoded, or when an entry may be in another leaf; the caller then uses cursors. A write reads the spans it meets this way (Store.Spans.edit).
Effects: fail, write.
PagedOrdered.seekContext
function PagedOrdered.seekContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, key: bytes): Result<OrderedCursor, IoError>Cursor memory is one leaf plus a bounded path of physical ranges, never all leaf links.
Effects: fail, read, write, console, io.
PagedOrdered.beforeContext
function PagedOrdered.beforeContext(self: PagedOrdered, cache: mut PageCache, credits: mut ResourceContext, key: Option<bytes>): Result<OrderedCursor, IoError>The same checked traversal walks backwards, strictly below the optional upper cut. No upper cut starts at the real final leaf; an empty tree still returns EOF.
Effects: fail, read, write, console, io.
OrderedCursor.nextContext
function OrderedCursor.nextContext(self: mut OrderedCursor, cache: mut PageCache, credits: mut ResourceContext): Result<Option<OrderedEntry>, IoError>The next entry, or none at the end; reads are charged to CREDITS.
Effects: fail, read, write, console, io.
OrderedCursor.nextManyContext
function OrderedCursor.nextManyContext(self: mut OrderedCursor, cache: mut PageCache, credits: mut ResourceContext, limit: i64): Result<Vec<OrderedEntry>, IoError>Up to limit next entries, in one call.
Effects: fail, read, write, console, io.
OrderedCursor.closeContext
function OrderedCursor.closeContext(self: mut OrderedCursor, credits: mut ResourceContext): Result<bool, IoError>Closes the cursor and gives back its charges.
Effects: fail, read, write, console, io.
PagedOrdered.putContext
function PagedOrdered.putContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, key: bytes, value: i64): Result<OrderedUpdate, IoError>Path-copy mutation appends only affected pages. Prior roots stay valid for MVCC readers. File/sync/WAL/root publication remain the caller's existing Store transaction contract.
Effects: fail, read, write, console, io.
PagedOrdered.removeContext
function PagedOrdered.removeContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, key: bytes): Result<OrderedUpdate, IoError>The tree without KEY; reads are charged to CREDITS.
Effects: fail, read, write, console, io.
PagedOrdered.putSortedContext
function PagedOrdered.putSortedContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, entries: Vec<OrderedEntry>): Result<OrderedUpdate, IoError>Puts strictly ascending entries in one merge: each touched page is copied once, not once per key, and an existing key takes the new value. The caller bounds the batch (a commit chunk).
Effects: fail, read, write, console, io.
PagedOrdered.editContext
function PagedOrdered.editContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, edits: Vec<OrderedEdit>): Result<OrderedUpdate, IoError>Puts and removes strictly ascending keys in one merge (a value puts its key, none removes it): each touched page is copied once. StoredCatalog.editContext changes many rows this way.
Effects: fail, read, write, console, io.
PagedOrdered.replaceContext
function PagedOrdered.replaceContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, prior: bytes, target: bytes, value: i64): Result<OrderedUpdate, IoError>Moves VALUE from key PRIOR to key TARGET (PRIOR !== TARGET) in one path copy when both are in one leaf. The update is fresh when TARGET was absent, and keeps the tree's count when PRIOR was present. A catalog row whose value changes does this.
Effects: fail, read, write, console, io.
PagedOrdered.rank
function PagedOrdered.rank(self: PagedOrdered, cache: mut PageCache, credits: mut Resources, key: bytes): Result<i64, IoError>The number of keys less than KEY.
Effects: fail, read, write, console, io.
PagedOrdered.get
function PagedOrdered.get(self: PagedOrdered, cache: mut PageCache, credits: mut Resources, key: bytes): Result<Option<i64>, IoError>The value of KEY, or none.
Effects: fail, read, write, console, io.
PagedOrdered.seek
function PagedOrdered.seek(self: PagedOrdered, cache: mut PageCache, credits: mut Resources, key: bytes): Result<OrderedCursor, IoError>A cursor from the first key not less than KEY.
Effects: fail, read, write, console, io.
PagedOrdered.before
function PagedOrdered.before(self: PagedOrdered, cache: mut PageCache, credits: mut Resources, key: Option<bytes>): Result<OrderedCursor, IoError>A cursor in reverse from the last key less than KEY, or from the last key when KEY is none.
Effects: fail, read, write, console, io.
OrderedCursor.next
function OrderedCursor.next(self: mut OrderedCursor, cache: mut PageCache, credits: mut Resources): Result<Option<OrderedEntry>, IoError>The next entry, or none at the end.
Effects: fail, read, write, console, io.
OrderedCursor.close
function OrderedCursor.close(self: mut OrderedCursor, credits: mut Resources): Result<bool, IoError>Closes the cursor and gives back its charges.
Effects: fail, read, write, console, io.
PagedOrdered.put
function PagedOrdered.put(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut Resources, key: bytes, value: i64): Result<OrderedUpdate, IoError>The tree with KEY set to VALUE; new pages go to FILE.
Effects: fail, read, write, console, io.
PagedOrdered.remove
function PagedOrdered.remove(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut Resources, key: bytes): Result<OrderedUpdate, IoError>The tree without KEY; new pages go to FILE.
Effects: fail, read, write, console, io.
PagedOrdered.removeRangeContext
function PagedOrdered.removeRangeContext(self: PagedOrdered, file: File, cache: mut PageCache, credits: mut ResourceContext, start: bytes, end: bytes): Result<OrderedUpdate, IoError>Delete [start,end) by copying boundary paths, with the same tail rollback, cache invalidation and root contraction as point mutations.
Effects: fail, read, write, console, io.
Constants
MIN_ORDERED_PAGE_BYTES
const MIN_ORDERED_PAGE_BYTES = 128The least page size of a paged ordered tree.
MAX_ORDERED_PAGE_BYTES
const MAX_ORDERED_PAGE_BYTES = 1048576The greatest page size of a paged ordered tree.
ORDERED_HEIGHT
const ORDERED_HEIGHT = 64The most levels of a paged tree.
ORDERED_HEAD
const ORDERED_HEAD = 24The bytes of the head of a page of a paged tree.
ORDERED_BRANCH_BYTES
const ORDERED_BRANCH_BYTES = 32The bytes of one branch of an inner page.