Stable bounded-memory external sorting and merge planning for MoonBit
Dependencies
///|
test {
let config = SortConfig::new(memory_budget_bytes=1024, max_record_bytes=256)
let builder = RunBuilder::new(config)
ignore(builder.push("second", "b"))
ignore(builder.push("first", "a"))
guard builder.finish() is Some(run) else { fail("expected run") }
assert_eq(run.map(record => record.payload), ["first", "second"])
}pub(all) struct GroupStatistics {
records : Int64
distinct_keys : Int64
singleton_groups : Int64
repeated_groups : Int64
largest_group_records : Int64
first_key : SortValue?
last_key : SortValue?
verification : VerificationReport
} derive(Eq, Debug)pub(all) struct JobEstimate {
input_records : Int64
input_payload_bytes : Int64
average_payload_bytes : Int64
records_per_run : Int64
initial_runs : Int64
merge_passes : Int
minimum_payload_io_bytes : Int64
peak_payload_generation_bytes : Int64
schedule : Array[MergePassEstimate]
saturated : Bool
} derive(Eq, Debug)pub(all) struct JobManifest {
phase : JobPhase
input_path : String
output_path : String
work_directory : String
config : SortConfig
selector : KeySelector
input_records : Int64
initial_run_count : Int
merge_pass : Int
runs : Array[RunDescriptor]
} derive(Eq, Debug)pub struct RunBuilder {
config : SortConfig
records : Array[SortRecord]
retained_bytes : Int
next_position : Int64
}fn RunBuilder::push(self : RunBuilder, payload : String, key_text : String) -> Array[SortRecord]? raise SortErrorfn SortConfig::new(order? : SortOrder, key_kind? : KeyKind, memory_budget_bytes? : Int, max_open_runs? : Int, max_record_bytes? : Int) -> SortConfig raise SortErrorpub(all) enum SortValue {
TextValue(String)
IntegerValue(Int64)
DecimalValue(DecimalKeyValue)
} derive(Eq, Debug)pub struct SortedGroupCounter {
config : SortConfig
verifier : SortednessVerifier
previous_key : SortValue?
first_key : SortValue?
last_key : SortValue?
records : Int64
distinct_keys : Int64
singleton_groups : Int64
repeated_groups : Int64
current_group_records : Int64
largest_group_records : Int64
next_position : Int64
finished : Bool
}fn SortedGroupCounter::new(config : SortConfig, start_position? : Int64) -> SortedGroupCounter raise SortErrorfn SortedGroupCounter::push(self : SortedGroupCounter, payload : String, key_text : String) -> Unit raise SortErrorfn SortedGroupCounter::push_record(self : SortedGroupCounter, record : SortRecord) -> Unit raise SortErrorpub struct SortednessVerifier {
config : SortConfig
previous : SortRecord?
records : Int64
equal_adjacent_keys : Int64
violation : OrderViolation?
}fn SortednessVerifier::push(self : SortednessVerifier, payload : String, key_text : String) -> Unit raise SortErrorpub(all) struct TopKResult {
records : Array[SortRecord]
input_records : Int64
discarded_records : Int64
retained_bytes : Int
} derive(Eq, Debug)pub struct TopKSelector {
config : SortConfig
limit : Int
heap : Array[SortRecord]
retained_bytes : Int
input_records : Int64
discarded_records : Int64
next_position : Int64
finished : Bool
}fn TopKSelector::new(config : SortConfig, limit : Int, start_position? : Int64) -> TopKSelector raise SortErrorfn TopKSelector::push(self : TopKSelector, payload : String, key_text : String) -> Unit raise SortErrorpub(all) struct VerificationReport {
records : Int64
equal_adjacent_keys : Int64
violation : OrderViolation?
} derive(Eq, Debug)fn build_merge_schedule(initial_runs : Int64, max_open_runs : Int) -> Array[MergePassEstimate] raise SortErrorfn estimate_job(input_records : Int64, input_payload_bytes : Int64, config : SortConfig) -> JobEstimate raise SortErrorInstall
Download zipStable bounded-memory external sorting and merge planning for MoonBit
Dependencies