1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>file_sorter</title>
27
<prepared>Hans Bolinder</prepared>
28
<responsible>nobody</responsible>
30
<approved>nobody</approved>
32
<date>2001-03-13</date>
34
<file>file_sorter.sgml</file>
36
<module>file_sorter</module>
37
<modulesummary>File Sorter</modulesummary>
39
<p>The functions of this module sort terms on files, merge already
40
sorted files, and check files for sortedness. Chunks containing
41
binary terms are read from a sequence of files, sorted
42
internally in memory and written on temporary files, which are
43
merged producing one sorted file as output. Merging is provided
44
as an optimization; it is faster when the files are already
45
sorted, but it always works to sort instead of merge.
47
<p>On a file, a term is represented by a header and a binary. Two
48
options define the format of terms on files:
50
<list type="bulleted">
51
<item><c>{header, HeaderLength}</c>. HeaderLength determines the
52
number of bytes preceding each binary and containing the
53
length of the binary in bytes. Default is 4. The order of the
54
header bytes is defined as follows: if <c>B</c> is a binary
55
containing a header only, the size <c>Size</c> of the binary
57
<c><![CDATA[<<Size:HeaderLength/unit:8>> = B]]></c>.
59
<item><c>{format, Format}</c>. The format determines the
60
function that is applied to binaries in order to create the
61
terms that will be sorted. The default value is
62
<c>binary_term</c>, which is equivalent to
63
<c>fun binary_to_term/1</c>. The value <c>binary</c> is
64
equivalent to <c>fun(X) -> X end</c>, which means that the
65
binaries will be sorted as they are. This is the fastest
66
format. If <c>Format</c> is <c>term</c>, <c>io:read/2</c> is
67
called to read terms. In that case only the default value of
68
the <c>header</c> option is allowed. The <c>format</c> option
69
also determines what is written to the sorted output file: if
70
<c>Format</c> is <c>term</c> then <c>io:format/3</c> is called
71
to write each term, otherwise the binary prefixed by a header
72
is written. Note that the binary written is the same binary
73
that was read; the results of applying the <c>Format</c>
74
function are thrown away as soon as the terms have been
75
sorted. Reading and writing terms using the <c>io</c> module
76
is very much slower than reading and writing binaries.
81
<list type="bulleted">
82
<item><c>{order, Order}</c>. The default is to sort terms in
83
ascending order, but that can be changed by the value
84
<c>descending</c> or by giving an ordering function <c>Fun</c>.
85
An ordering function is antisymmetric, transitive and total.
86
<c>Fun(A, B)</c> should return <c>true</c> if <c>A</c>
87
comes before <c>B</c> in the ordering, <c>false</c> otherwise.
88
Using an ordering function will slow down the sort
89
considerably. The <c>keysort</c>, <c>keymerge</c> and
90
<c>keycheck</c> functions do not accept ordering functions.
92
<item><c>{unique, bool()}</c>. When sorting or merging files,
93
only the first of a sequence of terms that compare equal is
94
output if this option is set to <c>true</c>. The default
95
value is <c>false</c> which implies that all terms that
96
compare equal are output. When checking files for
97
sortedness, a check that no pair of consecutive terms
98
compares equal is done if this option is set to <c>true</c>.
100
<item><c>{tmpdir, TempDirectory}</c>. The directory where
101
temporary files are put can be chosen explicitly. The
102
default, implied by the value <c>""</c>, is to put temporary
103
files on the same directory as the sorted output file. If
104
output is a function (see below), the directory returned by
105
<c>file:get_cwd()</c> is used instead. The names of
106
temporary files are derived from the Erlang nodename
107
(<c>node()</c>), the process identifier of the current Erlang
108
emulator (<c>os:getpid()</c>), and a timestamp
109
(<c>erlang:now()</c>); a typical name would be
110
<c>fs_mynode@myhost_1763_1043_337000_266005.17</c>, where
111
<c>17</c> is a sequence number. Existing files will be
112
overwritten. Temporary files are deleted unless some
113
uncaught EXIT signal occurs.
115
<item><c>{compressed, bool()}</c>. Temporary files and the
116
output file may be compressed. The default value
117
<c>false</c> implies that written files are not
118
compressed. Regardless of the value of the <c>compressed</c>
119
option, compressed files can always be read. Note that
120
reading and writing compressed files is significantly slower
121
than reading and writing uncompressed files.
123
<item><c>{size, Size}</c>. By default approximately 512*1024
124
bytes read from files are sorted internally. This option
125
should rarely be needed.
127
<item><c>{no_files, NoFiles}</c>. By default 16 files are
128
merged at a time. This option should rarely be needed.
131
<p>To summarize, here is the syntax of the options:</p>
132
<list type="bulleted">
134
<p><c>Options = [Option] | Option</c></p>
137
<p><c>Option = {header, HeaderLength} | {format, Format} | {order, Order} | {unique, bool()} | {tmpdir, TempDirectory} | {compressed, bool()} | {size, Size} | {no_files, NoFiles}</c></p>
140
<p><c>HeaderLength = int() > 0</c></p>
143
<p><c>Format = binary_term | term | binary | FormatFun</c></p>
146
<p><c>FormatFun = fun(Binary) -> Term</c></p>
149
<p><c>Order = ascending | descending | OrderFun</c></p>
152
<p><c>OrderFun = fun(Term, Term) -> bool()</c></p>
155
<p><c>TempDirectory = "" | file_name()</c></p>
158
<p><c>Size = int() >= 0</c></p>
161
<p><c>NoFiles = int() > 1</c></p>
164
<p>As an alternative to sorting files, a function of one argument
165
can be given as input. When called with the argument <c>read</c>
166
the function is assumed to return <c>end_of_input</c> or
167
<c>{end_of_input, Value}}</c> when there is no more input
168
(<c>Value</c> is explained below), or <c>{Objects, Fun}</c>,
169
where <c>Objects</c> is a list of binaries or terms depending on
170
the format and <c>Fun</c> is a new input function. Any other
171
value is immediately returned as value of the current call to
172
<c>sort</c> or <c>keysort</c>. Each input function will be
173
called exactly once, and should an error occur, the last
174
function is called with the argument <c>close</c>, the reply of
177
<p>A function of one argument can be given as output. The results
178
of sorting or merging the input is collected in a non-empty
179
sequence of variable length lists of binaries or terms depending
180
on the format. The output function is called with one list at a
181
time, and is assumed to return a new output function. Any other
182
return value is immediately returned as value of the current
183
call to the sort or merge function. Each output function is
184
called exactly once. When some output function has been applied
185
to all of the results or an error occurs, the last function is
186
called with the argument <c>close</c>, and the reply is returned
187
as value of the current call to the sort or merge function. If a
188
function is given as input and the last input function returns
189
<c>{end_of_input, Value}</c>, the function given as output will
190
be called with the argument <c>{value, Value}</c>. This makes it
191
easy to initiate the sequence of output functions with a value
192
calculated by the input functions.
194
<p>As an example, consider sorting the terms on a disk log file.
195
A function that reads chunks from the disk log and returns a
196
list of binaries is used as input. The results are collected in
200
{ok, _} = disk_log:open([{name,Log}, {mode,read_only}]),
201
Input = input(Log, start),
203
Reply = file_sorter:sort(Input, Output, {format,term}),
204
ok = disk_log:close(Log),
211
\011 case disk_log:chunk(Log, Cont) of
212
\011\011{error, Reason} ->
213
\011\011 {error, Reason};
214
\011\011{Cont2, Terms} ->
215
\011\011 {Terms, input(Log, Cont2)};
216
\011\011{Cont2, Terms, _Badbytes} ->
217
\011\011 {Terms, input(Log, Cont2)};
219
\011\011 end_of_input
225
\011 lists:append(lists:reverse(L));
227
\011 output([Terms | L])
229
<p>Further examples of functions as input and output can be found
230
at the end of the <c>file_sorter</c> module; the <c>term</c>
231
format is implemented with functions.
233
<p>The possible values of <c>Reason</c> returned when an error
235
<list type="bulleted">
237
<p><c>bad_object</c>, <c>{bad_object, FileName}</c>.
238
Applying the format function failed for some binary,
239
or the key(s) could not be extracted from some term.</p>
242
<p><c>{bad_term, FileName}</c>. <c>io:read/2</c> failed
243
to read some term.\011 </p>
246
<p><c>{file_error, FileName, Reason2}</c>. See
247
<c>file(3)</c> for an explanation of <c>Reason2</c>.</p>
250
<p><c>{premature_eof, FileName}</c>. End-of-file was
251
encountered inside some binary term.</p>
254
<p><em>Types</em></p>
257
FileName = file_name()
258
FileNames = [FileName]
259
ICommand = read | close
260
IReply = end_of_input | {end_of_input, Value} | {[Object], Infun} | InputReply
261
Infun = fun(ICommand) -> IReply
262
Input = FileNames | Infun
264
KeyPos = int() > 0 | [int() > 0]
265
OCommand = {value, Value} | [Object] | close
266
OReply = Outfun | OutputReply
267
Object = Term | Binary
268
Outfun = fun(OCommand) -> OReply
269
Output = FileName | Outfun
276
<name>sort(FileName) -> Reply</name>
277
<name>sort(Input, Output) -> Reply</name>
278
<name>sort(Input, Output, Options) -> Reply</name>
279
<fsummary>Sort terms on files.</fsummary>
281
<v>Reply = ok | {error, Reason} | InputReply | OutputReply</v>
284
<p>Sorts terms on files.
286
<p><c>sort(FileName)</c> is equivalent to
287
<c>sort([FileName], FileName)</c>.
289
<p><c>sort(Input, Output)</c> is equivalent to
290
<c>sort(Input, Output, [])</c>.
296
<name>keysort(KeyPos, FileName) -> Reply</name>
297
<name>keysort(KeyPos, Input, Output) -> Reply</name>
298
<name>keysort(KeyPos, Input, Output, Options) -> Reply</name>
299
<fsummary>Sort terms on files by key.</fsummary>
301
<v>Reply = ok | {error, Reason} | InputReply | OutputReply</v>
304
<p>Sorts tuples on files. The sort is performed on the
305
element(s) mentioned in <c>KeyPos</c>. If two tuples
306
compare equal on one element, next element according to
307
<c>KeyPos</c> is compared. The sort is stable.
309
<p><c>keysort(N, FileName)</c> is equivalent to
310
<c>keysort(N, [FileName], FileName)</c>.
312
<p><c>keysort(N, Input, Output)</c> is equivalent to
313
<c>keysort(N, Input, Output, [])</c>.
319
<name>merge(FileNames, Output) -> Reply</name>
320
<name>merge(FileNames, Output, Options) -> Reply</name>
321
<fsummary>Merge terms on files.</fsummary>
323
<v>Reply = ok | {error, Reason} | OutputReply</v>
326
<p>Merges terms on files. Each input file is assumed to be
329
<p><c>merge(FileNames, Output)</c> is equivalent to
330
<c>merge(FileNames, Output, [])</c>.
335
<name>keymerge(KeyPos, FileNames, Output) -> Reply</name>
336
<name>keymerge(KeyPos, FileNames, Output, Options) -> Reply</name>
337
<fsummary>Merge terms on files by key.</fsummary>
339
<v>Reply = ok | {error, Reason} | OutputReply</v>
342
<p>Merges tuples on files. Each input file is assumed to be
345
<p><c>keymerge(KeyPos, FileNames, Output)</c> is equivalent
346
to <c>keymerge(KeyPos, FileNames, Output, [])</c>.
352
<name>check(FileName) -> Reply</name>
353
<name>check(FileNames, Options) -> Reply</name>
354
<fsummary>Check whether terms on files are sorted.</fsummary>
356
<v>Reply = {ok, [Result]} | {error, Reason}</v>
357
<v>Result = {FileName, TermPosition, Term}</v>
358
<v>TermPosition = int() > 1</v>
361
<p>Checks files for sortedness. If a file is not sorted, the
362
first out-of-order element is returned. The first term on a
365
<p><c>check(FileName)</c> is equivalent to
366
<c>check([FileName], [])</c>.
371
<name>keycheck(KeyPos, FileName) -> CheckReply</name>
372
<name>keycheck(KeyPos, FileNames, Options) -> Reply</name>
373
<fsummary>Check whether terms on files are sorted by key.</fsummary>
375
<v>Reply = {ok, [Result]} | {error, Reason}</v>
376
<v>Result = {FileName, TermPosition, Term}</v>
377
<v>TermPosition = int() > 1</v>
380
<p>Checks files for sortedness. If a file is not sorted, the
381
first out-of-order element is returned. The first term on a
384
<p><c>keycheck(KeyPos, FileName)</c> is equivalent
385
to <c>keycheck(KeyPos, [FileName], [])</c>.