File: file_sorter.html

package info (click to toggle)
erlang-doc-html 1%3A8.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 18,028 kB
  • ctags: 7,419
  • sloc: perl: 1,841; ansic: 323; erlang: 155
file content (380 lines) | stat: -rw-r--r-- 14,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>file_sorter</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>


<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>file_sorter</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
file_sorter</UL>
<H3>MODULE SUMMARY</H3>
<UL>
File Sorter</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>The functions of this module sort terms on files, merge already
sorted files, and check 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.

<P>On a file, a term is represented by a header and a binary. Two
options define the format of terms on files:

<P><UL>
<LI><CODE>{header, HeaderLength}</CODE>. HeaderLength determines the
         number of bytes preceding each binary and containing the
         length of the binary in bytes. Default is 4. The order of the
         header bytes is defined as follows: if <CODE>B</CODE> is a binary
         containing a header only, the size <CODE>Size</CODE> of the binary
         is calculated as
         <CODE>&#60;&#60;Size:HeaderLength/unit:8&#62;&#62; = B</CODE>.

</LI><BR>
<LI><CODE>{format, Format}</CODE>. The format determines the
function that is applied to binaries in order to create the
terms that will be sorted. The default value is
<CODE>binary_term</CODE>, which is equivalent to
<CODE>fun&#160;binary_to_term/1</CODE>. If <CODE>Format</CODE> is
<CODE>term</CODE>, <CODE>io:read/2</CODE> is called to read terms. In that
case only the default value of the <CODE>header</CODE> option is
allowed. The <CODE>format</CODE> option also determines what is
written to the sorted output file: if <CODE>Format</CODE> is
<CODE>term</CODE> then <CODE>io:format/3</CODE> is called to write each
term, otherwise the binary prefixed by a header is written.
Note that the binary written is the same binary that was read;
the results of applying the <CODE>Format</CODE> function are thrown
away as soon as the terms have been sorted. Reading and
writing terms using the <CODE>io</CODE> module is very much slower
than reading and writing binaries.
         
</LI><BR>
</UL>
<P>Other options are:

<P><UL>
<LI><CODE>{order, Order}</CODE>. The default is to sort terms in
         ascending order, but that can be changed by the value
         <CODE>descending</CODE> or by giving an ordering function
         <CODE>Fun</CODE>. <CODE>Fun(A,&#160;B)</CODE> should return <CODE>true</CODE>
         if <CODE>A</CODE> comes before <CODE>B</CODE> in the ordering,
         <CODE>false</CODE> otherwise. The <CODE>keysort</CODE>, <CODE>keymerge</CODE>
         and <CODE>keycheck</CODE> functions do not accept ordering
         functions. Using an ordering function will slow down the
         sort considerably.

        </LI><BR>
<LI><CODE>{unique, bool()}</CODE>. When sorting or merging files,
         only the first of a sequence of terms that compare equal is
         output if this option is set to <CODE>true</CODE>. The default
         value is <CODE>false</CODE> which implies that all terms that
         compare equal are output. When checking files for
         sortedness, a check that no pair of consecutive terms
         compares equal is done if this option is set to <CODE>true</CODE>.

        </LI><BR>
<LI><CODE>{tmpdir, TempDirectory}</CODE>. The directory where
         temporary files are put can be chosen explicitly. The
         default, implied by the value <CODE>&#34;&#34;</CODE>, is to put temporary
         files on the same directory as the sorted output file. If
         output is a function (see below), the directory returned by
         <CODE>file:get_cwd()</CODE> is used instead. The names of
         temporary files are derived from the pid doing the sort; a
         typical name would be <CODE>file_sorter_0_28_0.17</CODE>, where
         <CODE>17</CODE> is a sequence number. Existing files will be
         overwritten. Temporary files are deleted unless some
         uncaught EXIT signal occurs.

</LI><BR>
<LI><CODE>{compressed, bool()}</CODE>. Temporary files and the
output file may be compressed. The default value
<CODE>false</CODE> implies that written files are not
compressed. Regardless of the value of the <CODE>compressed</CODE>
option, compressed files can always be read. Note that
reading and writing compressed files is significantly slower
than reading and writing uncompressed files.

        </LI><BR>
<LI><CODE>{size, Size}</CODE>. By default approximately 512
         kilobytes read from files are sorted internally. There is
         seldom any need to change this.

        </LI><BR>
<LI><CODE>{no_files, NoFiles}</CODE>. By default 16 files are
         merged at a time. There is seldom any need to change this.

</LI><BR>
</UL>
<P>To summarize, here is the syntax of the options:
<P><UL>
<LI><CODE>Options = [Option] | Option</CODE>
        <BR>
</LI><BR>
<LI><CODE>Option = {header, HeaderLength}
| {format, Format}
         | {order, Order}
         | {unique, bool()}
         | {tmpdir, TempDirectory}
         | {compressed, bool()}
| {size, Size}
         | {no_files, NoFiles}</CODE>
        <BR>
</LI><BR>
<LI><CODE>HeaderLength = int() &#62; 0</CODE>
        <BR>
</LI><BR>
<LI><CODE>Format = binary_term | term | FormatFun</CODE>
        <BR>
</LI><BR>
<LI><CODE>FormatFun = fun(Binary) -&#62; Term</CODE>
        <BR>
</LI><BR>
<LI><CODE>Order = ascending | descending | OrderFun</CODE>
        <BR>
</LI><BR>
<LI><CODE>OrderFun = fun(Term, Term) -&#62; bool()</CODE>
        <BR>
</LI><BR>
<LI><CODE>TempDirectory = &#34;&#34; | file_name()</CODE>
        <BR>
</LI><BR>
<LI><CODE>Size = int() &#62; 0</CODE>
        <BR>
</LI><BR>
<LI><CODE>NoFiles = int() &#62; 1</CODE>
<BR>
</LI><BR>
</UL>
<P>As an alternative to sorting files, a function of one argument
can be given as input. When called with the argument <CODE>read</CODE>
the function is assumed to return <CODE>end_of_input</CODE> when there
is no more input, or <CODE>{Objects, Fun}</CODE>, where <CODE>Objects</CODE>
is a list of binaries or terms depending on the format and
<CODE>Fun</CODE> is a new input function. Any other value is
immediately returned as value of the current call to <CODE>sort</CODE>
or <CODE>keysort</CODE>. Each input function will be called exactly
once, and should an error occur, the last function is called
with the argument <CODE>close</CODE>, the reply of which is ignored.

<P>A function of one argument can be given 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 the argument <CODE>close</CODE>, and the reply is returned
as value of the current call to the sort or merge function.

<P>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.
<PRE>sort(Log) -&#62;
    {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) -&#62;
    fun(close) -&#62;
            ok;
       (read) -&#62;
            case disk_log:chunk(Log, Cont) of
                {error, Reason} -&#62;
                    {error, Reason};
                {Cont2, Terms} -&#62;
                    {Terms, input(Log, Cont2)};
                {Cont2, Terms, _Badbytes} -&#62;
                    {Terms, input(Log, Cont2)};
                eof -&#62;
                    end_of_input
            end
    end.

output(L) -&#62;
    fun(close) -&#62;
            lists:append(lists:reverse(L));
       (Terms) -&#62;
            output([Terms | L])
    end.</PRE>
<P>Further examples of functions as input and output can be found
at the end of the <CODE>file_sorter</CODE> module; the <CODE>term</CODE>
format is implemented with functions.

<P>The possible values of <CODE>Reason</CODE> returned when an error
occurs are:
<P><UL>
<LI><CODE>bad_object</CODE>, <CODE>{bad_object, FileName}</CODE>. 
         Applying the format function failed for some binary, 
         or the key(s) could not be extracted from some term.
         <BR>
</LI><BR>
<LI><CODE>{bad_term, FileName}</CODE>. <CODE>io:read/2</CODE> failed
         to read some term.      
         <BR>
</LI><BR>
<LI><CODE>{file_error, FileName, Reason2}</CODE>. See the
         <CODE>file</CODE> module for an explanation of <CODE>Reason2</CODE>.
         <BR>
</LI><BR>
<LI><CODE>{premature_eof, FileName}</CODE>. End-of-file was 
         encountered inside some binary term.
         <BR>
</LI><BR>
<LI><CODE>{not_a_directory, FileName}</CODE>. The file
         supplied with the <CODE>tmpdir</CODE> option is not a directory.
<BR>
</LI><BR>
</UL>
<P><STRONG>Types</STRONG>
<PRE>Binary = binary()
FileName = file_name()
FileNames = [FileName]
ICommand = read | close
IReply = end_of_input | {[Object], Infun} | InputReply
Infun = fun(ICommand) -&#62; IReply
Input = FileNames | Infun
InputReply = Term
KeyPos = int() &#62; 0 | [int() &#62; 0]
OCommand = [Object] | close
OReply = Outfun | OutputReply
Object = Term | Binary
Outfun = fun(OCommand) -&#62; OReply
Output = FileName | Outfun
OutputReply = Term
Term = term()
    </PRE>
</UL>
<H3>EXPORTS</H3>
<P><A NAME="sort%1"><STRONG><CODE>sort(FileName) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="sort%2"><STRONG><CODE>sort(Input, Output) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="sort%3"><STRONG><CODE>sort(Input, Output, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = ok | {error, Reason} | InputReply | OutputReply</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sorts terms on files. 

<P><CODE>sort(FileName)</CODE> is equivalent to
         <CODE>sort([FileName], FileName)</CODE>.

         <P><CODE>sort(Input, Output)</CODE> is equivalent to
         <CODE>sort(Input, Output, [])</CODE>.

         <P> </UL>
<P><A NAME="keysort%2"><STRONG><CODE>keysort(KeyPos, FileName) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="keysort%3"><STRONG><CODE>keysort(KeyPos, Input, Output) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="keysort%4"><STRONG><CODE>keysort(KeyPos, Input, Output, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = ok | {error, Reason} | InputReply | OutputReply</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sorts tuples on files. The sort is performed on the
element(s) mentioned in <CODE>KeyPos</CODE>. If two tuples
compare equal on one element, next element according to
<CODE>KeyPos</CODE> is compared. The sort is stable.

<P><CODE>keysort(N, FileName)</CODE> is equivalent to
         <CODE>keysort(N, [FileName], FileName)</CODE>.

         <P><CODE>keysort(N, Input, Output)</CODE> is equivalent to
         <CODE>keysort(N, Input, Output, [])</CODE>.

         <P> </UL>
<P><A NAME="merge%2"><STRONG><CODE>merge(FileNames, Output) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="merge%3"><STRONG><CODE>merge(FileNames, Output, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = ok | {error, Reason} | OutputReply</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Merges terms on files. Each input file is assumed to be
sorted.

<P><CODE>merge(FileNames, Output)</CODE> is equivalent to
         <CODE>merge(FileNames, Output, [])</CODE>.

</UL>
<P><A NAME="keymerge%3"><STRONG><CODE>keymerge(KeyPos, FileNames, Output) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="keymerge%4"><STRONG><CODE>keymerge(KeyPos, FileNames, Output, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = ok | {error, Reason} | OutputReply</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Merges tuples on files. Each input file is assumed to be
sorted.

<P><CODE>keymerge(KeyPos, FileNames, Output)</CODE> is equivalent
         to <CODE>keymerge(KeyPos, FileNames, Output, [])</CODE>.

         <P> </UL>
<P><A NAME="check%1"><STRONG><CODE>check(FileName) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="check%2"><STRONG><CODE>check(FileNames, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = {ok, [Result]} | {error, Reason}</CODE></STRONG><BR>
<STRONG><CODE>Result = {FileName, TermPosition, Term}</CODE></STRONG><BR>
<STRONG><CODE>TermPosition = int() &#62; 1</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>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.

<P><CODE>check(FileName)</CODE> is equivalent to
         <CODE>check([FileName], [])</CODE>.

</UL>
<P><A NAME="keycheck%2"><STRONG><CODE>keycheck(KeyPos, FileName) -&#62; CheckReply</CODE></STRONG></A><BR>
<A NAME="keycheck%3"><STRONG><CODE>keycheck(KeyPos, FileNames, Options) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reply = {ok, [Result]} | {error, Reason}</CODE></STRONG><BR>
<STRONG><CODE>Result = {FileName, TermPosition, Term}</CODE></STRONG><BR>
<STRONG><CODE>TermPosition = int() &#62; 1</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>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.

<P><CODE>keycheck(KeyPos, FileName)</CODE> is equivalent
         to <CODE>keycheck(KeyPos, [FileName], [])</CODE>.

         <P> </UL>
<H3>AUTHORS</H3>
<UL>
Hans Bolinder - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright &copy; 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>