This module contains functions for sorting terms on files, merging already sorted files, and checking files for sortedness. Chunks containing binary terms are read from a sequence of files, sorted internally in memory and written on temporary files, which are merged producing one sorted file as output. Merging is provided as an optimization; it is faster when the files are already sorted, but it always works to sort instead of merge.
On a file, a term is represented by a header and a binary. Two options define the format of terms on files:
Option
Option
Other options are:
The default is to sort terms in
ascending order, but that can be changed by value
When sorting or merging files,
only the first of a sequence of terms that compare equal (
The directory where
temporary files are put can be chosen explicitly. The
default, implied by value
Temporary files and the output file can be compressed. Defaults
By default about 512*1024 bytes read from files are sorted internally. This option is rarely needed.
By default 16 files are merged at a time. This option is rarely needed.
As an alternative to sorting files, a function of one argument
can be specified as input. When called with argument
Any other value is immediately returned as value of the current call
to
A function of one argument can be specified as output. The results
of sorting or merging the input is collected in a non-empty
sequence of variable length lists of binaries or terms depending
on the format. The output function is called with one list at a
time, and is assumed to return a new output function. Any other
return value is immediately returned as value of the current
call to the sort or merge function. Each output function is
called exactly once. When some output function has been applied
to all of the results or an error occurs, the last function is
called with argument
If a function is specified as input and the last input function
returns
As an example, consider sorting the terms on a disk log file. A function that reads chunks from the disk log and returns a list of binaries is used as input. The results are collected in a list of terms.
sort(Log) -> {ok, _} = disk_log:open([{name,Log}, {mode,read_only}]), Input = input(Log, start), Output = output([]), Reply = file_sorter:sort(Input, Output, {format,term}), ok = disk_log:close(Log), Reply. input(Log, Cont) -> fun(close) -> ok; (read) -> case disk_log:chunk(Log, Cont) of {error, Reason} -> {error, Reason}; {Cont2, Terms} -> {Terms, input(Log, Cont2)}; {Cont2, Terms, _Badbytes} -> {Terms, input(Log, Cont2)}; eof -> end_of_input end end. output(L) -> fun(close) -> lists:append(lists:reverse(L)); (Terms) -> output([Terms | L]) end.
For more examples of functions as input and output, see
the end of the
The possible values of
Checks files for sortedness. If a file is not sorted, the first out-of-order element is returned. The first term on a file has position 1.
Checks files for sortedness. If a file is not sorted, the first out-of-order element is returned. The first term on a file has position 1.
Merges tuples on files. Each input file is assumed to be sorted on key(s).
Sorts tuples on files.
Sorts tuples on files. The sort is performed on the
element(s) mentioned in
Merges terms on files. Each input file is assumed to be sorted.
Sorts terms on files.
Sorts terms on files.