~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/stdlib/doc/src/file_sorter.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
 
3
 
 
4
<erlref>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2001</year>
 
8
      <year>2007</year>
 
9
      <holder>Ericsson AB, All Rights Reserved</holder>
 
10
    </copyright>
 
11
    <legalnotice>
 
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/.
 
17
 
 
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
 
21
  under the License.
 
22
 
 
23
  The Initial Developer of the Original Code is Ericsson AB.
 
24
    </legalnotice>
 
25
 
 
26
    <title>file_sorter</title>
 
27
    <prepared>Hans Bolinder</prepared>
 
28
    <responsible>nobody</responsible>
 
29
    <docno></docno>
 
30
    <approved>nobody</approved>
 
31
    <checked>no</checked>
 
32
    <date>2001-03-13</date>
 
33
    <rev>PA1</rev>
 
34
    <file>file_sorter.sgml</file>
 
35
  </header>
 
36
  <module>file_sorter</module>
 
37
  <modulesummary>File Sorter</modulesummary>
 
38
  <description>
 
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.
 
46
      </p>
 
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:
 
49
      </p>
 
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
 
56
       is calculated as
 
57
      <c><![CDATA[<<Size:HeaderLength/unit:8>> = B]]></c>.
 
58
      </item>
 
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&nbsp;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.
 
77
      </item>
 
78
    </list>
 
79
    <p>Other options are:
 
80
      </p>
 
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,&nbsp;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.
 
91
      </item>
 
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>.
 
99
      </item>
 
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.
 
114
      </item>
 
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.
 
122
      </item>
 
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.
 
126
      </item>
 
127
      <item><c>{no_files, NoFiles}</c>. By default 16 files are
 
128
       merged at a time. This option should rarely be needed.
 
129
      </item>
 
130
    </list>
 
131
    <p>To summarize, here is the syntax of the options:</p>
 
132
    <list type="bulleted">
 
133
      <item>
 
134
        <p><c>Options = [Option] | Option</c></p>
 
135
      </item>
 
136
      <item>
 
137
        <p><c>Option = {header, HeaderLength} | {format, Format} | {order, Order} | {unique, bool()} | {tmpdir, TempDirectory} | {compressed, bool()} | {size, Size} | {no_files, NoFiles}</c></p>
 
138
      </item>
 
139
      <item>
 
140
        <p><c>HeaderLength = int() > 0</c></p>
 
141
      </item>
 
142
      <item>
 
143
        <p><c>Format = binary_term | term | binary | FormatFun</c></p>
 
144
      </item>
 
145
      <item>
 
146
        <p><c>FormatFun = fun(Binary) -> Term</c></p>
 
147
      </item>
 
148
      <item>
 
149
        <p><c>Order = ascending | descending | OrderFun</c></p>
 
150
      </item>
 
151
      <item>
 
152
        <p><c>OrderFun = fun(Term, Term) -> bool()</c></p>
 
153
      </item>
 
154
      <item>
 
155
        <p><c>TempDirectory = "" | file_name()</c></p>
 
156
      </item>
 
157
      <item>
 
158
        <p><c>Size = int() >= 0</c></p>
 
159
      </item>
 
160
      <item>
 
161
        <p><c>NoFiles = int() > 1</c></p>
 
162
      </item>
 
163
    </list>
 
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
 
175
      which is ignored.
 
176
      </p>
 
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.
 
193
      </p>
 
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
 
197
      a list of terms.</p>
 
198
    <pre>
 
199
sort(Log) ->
 
200
    {ok, _} = disk_log:open([{name,Log}, {mode,read_only}]),
 
201
    Input = input(Log, start),
 
202
    Output = output([]),
 
203
    Reply = file_sorter:sort(Input, Output, {format,term}),
 
204
    ok = disk_log:close(Log),
 
205
    Reply.
 
206
 
 
207
input(Log, Cont) ->
 
208
    fun(close) ->
 
209
\011    ok;
 
210
       (read) ->
 
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)};
 
218
\011\011eof ->
 
219
\011\011    end_of_input
 
220
\011    end
 
221
    end.
 
222
 
 
223
output(L) ->
 
224
    fun(close) ->
 
225
\011    lists:append(lists:reverse(L));
 
226
       (Terms) ->
 
227
\011    output([Terms | L])
 
228
    end.    </pre>
 
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.
 
232
      </p>
 
233
    <p>The possible values of <c>Reason</c> returned when an error
 
234
      occurs are:</p>
 
235
    <list type="bulleted">
 
236
      <item>
 
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>
 
240
      </item>
 
241
      <item>
 
242
        <p><c>{bad_term, FileName}</c>. <c>io:read/2</c> failed
 
243
          to read some term.\011   </p>
 
244
      </item>
 
245
      <item>
 
246
        <p><c>{file_error, FileName, Reason2}</c>. See
 
247
          <c>file(3)</c> for an explanation of <c>Reason2</c>.</p>
 
248
      </item>
 
249
      <item>
 
250
        <p><c>{premature_eof, FileName}</c>. End-of-file was 
 
251
          encountered inside some binary term.</p>
 
252
      </item>
 
253
    </list>
 
254
    <p><em>Types</em></p>
 
255
    <pre>
 
256
Binary = binary()
 
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
 
263
InputReply = Term
 
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
 
270
OutputReply = Term
 
271
Term = term()
 
272
Value = Term</pre>
 
273
  </description>
 
274
  <funcs>
 
275
    <func>
 
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>
 
280
      <type>
 
281
        <v>Reply = ok | {error, Reason} | InputReply | OutputReply</v>
 
282
      </type>
 
283
      <desc>
 
284
        <p>Sorts terms on files. 
 
285
          </p>
 
286
        <p><c>sort(FileName)</c> is equivalent to
 
287
          <c>sort([FileName], FileName)</c>.
 
288
          </p>
 
289
        <p><c>sort(Input, Output)</c> is equivalent to
 
290
          <c>sort(Input, Output, [])</c>.
 
291
          </p>
 
292
        <p></p>
 
293
      </desc>
 
294
    </func>
 
295
    <func>
 
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>
 
300
      <type>
 
301
        <v>Reply = ok | {error, Reason} | InputReply | OutputReply</v>
 
302
      </type>
 
303
      <desc>
 
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.
 
308
          </p>
 
309
        <p><c>keysort(N, FileName)</c> is equivalent to
 
310
          <c>keysort(N, [FileName], FileName)</c>.
 
311
          </p>
 
312
        <p><c>keysort(N, Input, Output)</c> is equivalent to
 
313
          <c>keysort(N, Input, Output, [])</c>.
 
314
          </p>
 
315
        <p></p>
 
316
      </desc>
 
317
    </func>
 
318
    <func>
 
319
      <name>merge(FileNames, Output) -> Reply</name>
 
320
      <name>merge(FileNames, Output, Options) -> Reply</name>
 
321
      <fsummary>Merge terms on files.</fsummary>
 
322
      <type>
 
323
        <v>Reply = ok | {error, Reason} | OutputReply</v>
 
324
      </type>
 
325
      <desc>
 
326
        <p>Merges terms on files. Each input file is assumed to be
 
327
          sorted.
 
328
          </p>
 
329
        <p><c>merge(FileNames, Output)</c> is equivalent to
 
330
          <c>merge(FileNames, Output, [])</c>.
 
331
          </p>
 
332
      </desc>
 
333
    </func>
 
334
    <func>
 
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>
 
338
      <type>
 
339
        <v>Reply = ok | {error, Reason} | OutputReply</v>
 
340
      </type>
 
341
      <desc>
 
342
        <p>Merges tuples on files. Each input file is assumed to be
 
343
          sorted on key(s).
 
344
          </p>
 
345
        <p><c>keymerge(KeyPos, FileNames, Output)</c> is equivalent
 
346
          to <c>keymerge(KeyPos, FileNames, Output, [])</c>.
 
347
          </p>
 
348
        <p></p>
 
349
      </desc>
 
350
    </func>
 
351
    <func>
 
352
      <name>check(FileName) -> Reply</name>
 
353
      <name>check(FileNames, Options) -> Reply</name>
 
354
      <fsummary>Check whether terms on files are sorted.</fsummary>
 
355
      <type>
 
356
        <v>Reply = {ok, [Result]} | {error, Reason}</v>
 
357
        <v>Result = {FileName, TermPosition, Term}</v>
 
358
        <v>TermPosition = int() > 1</v>
 
359
      </type>
 
360
      <desc>
 
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
 
363
          file has position 1.
 
364
          </p>
 
365
        <p><c>check(FileName)</c> is equivalent to
 
366
          <c>check([FileName], [])</c>.
 
367
          </p>
 
368
      </desc>
 
369
    </func>
 
370
    <func>
 
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>
 
374
      <type>
 
375
        <v>Reply = {ok, [Result]} | {error, Reason}</v>
 
376
        <v>Result = {FileName, TermPosition, Term}</v>
 
377
        <v>TermPosition = int() > 1</v>
 
378
      </type>
 
379
      <desc>
 
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
 
382
          file has position 1.
 
383
          </p>
 
384
        <p><c>keycheck(KeyPos, FileName)</c> is equivalent
 
385
          to <c>keycheck(KeyPos, [FileName], [])</c>.
 
386
          </p>
 
387
        <p></p>
 
388
      </desc>
 
389
    </func>
 
390
  </funcs>
 
391
</erlref>
 
392