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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>file_sorter</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
<STYLE TYPE="text/css">
<!--
.REFBODY { margin-left: 13mm }
.REFTYPES { margin-left: 8mm }
-->
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
<IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>file_sorter</H1>
</CENTER>
<H3>MODULE</H3>
<DIV CLASS=REFBODY>
file_sorter
</DIV>
<H3>MODULE SUMMARY</H3>
<DIV CLASS=REFBODY>
File Sorter
</DIV>
<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>
<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><<Size:HeaderLength/unit:8>> = B</CODE>.
</LI>
<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>funbinary_to_term/1</CODE>. The value <CODE>binary</CODE> is
equivalent to <CODE>fun(X) -> X end</CODE>, which means that the
binaries will be sorted as they are. This is the fastest
format. 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>
</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,B)</CODE> should return <CODE>true</CODE>
if <CODE>A</CODE> comes before <CODE>B</CODE> in the ordering,
<CODE>false</CODE> otherwise. Using an ordering function will slow
down the sort considerably. The <CODE>keysort</CODE>,
<CODE>keymerge</CODE> and <CODE>keycheck</CODE> functions do not accept
ordering functions.
</LI>
<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>
<LI>
<CODE>{tmpdir, TempDirectory}</CODE>. The directory where
temporary files are put can be chosen explicitly. The
default, implied by the value <CODE>""</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 Erlang nodename
(<CODE>node()</CODE>), the process identifier of the current Erlang
emulator (<CODE>os:getpid()</CODE>), and a timestamp
(<CODE>erlang:now()</CODE>); a typical name would be
<CODE>fs_mynode@myhost_1763_1043_337000_266005.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>
<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>
<LI>
<CODE>{size, Size}</CODE>. By default approximately 512*1024
bytes read from files are sorted internally. This option
should rarely be needed.
</LI>
<LI>
<CODE>{no_files, NoFiles}</CODE>. By default 16 files are
merged at a time. This option should rarely be needed.
</LI>
</UL>
<P>To summarize, here is the syntax of the options:
<P>
<UL>
<LI>
<CODE>Options = [Option] | Option</CODE>
<BR>
</LI>
<LI>
<CODE>Option = {header, HeaderLength}
| {format, Format}
| {order, Order}
| {unique, bool()}
| {tmpdir, TempDirectory}
| {compressed, bool()}
| {size, Size}
| {no_files, NoFiles}</CODE>
<BR>
</LI>
<LI>
<CODE>HeaderLength = int() > 0</CODE>
<BR>
</LI>
<LI>
<CODE>Format = binary_term | term | binary | FormatFun</CODE>
<BR>
</LI>
<LI>
<CODE>FormatFun = fun(Binary) -> Term</CODE>
<BR>
</LI>
<LI>
<CODE>Order = ascending | descending | OrderFun</CODE>
<BR>
</LI>
<LI>
<CODE>OrderFun = fun(Term, Term) -> bool()</CODE>
<BR>
</LI>
<LI>
<CODE>TempDirectory = "" | file_name()</CODE>
<BR>
</LI>
<LI>
<CODE>Size = int() > 0</CODE>
<BR>
</LI>
<LI>
<CODE>NoFiles = int() > 1</CODE>
<BR>
</LI>
</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> or
<CODE>{end_of_input, Value}}</CODE> when there is no more input
(<CODE>Value</CODE> is explained below), 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. If a
function is given as input and the last input function returns
<CODE>{end_of_input, Value}</CODE>, the function given as output will
be called with the argument <CODE>{value, Value}</CODE>. This makes it
easy to initiate the sequence of output functions with a value
calculated by the input functions.
<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) ->
{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.
</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>
<LI>
<CODE>{bad_term, FileName}</CODE>. <CODE>io:read/2</CODE> failed
to read some term.
<BR>
</LI>
<LI>
<CODE>{file_error, FileName, Reason2}</CODE>. See
<CODE>file(3)</CODE> for an explanation of <CODE>Reason2</CODE>.
<BR>
</LI>
<LI>
<CODE>{premature_eof, FileName}</CODE>. End-of-file was
encountered inside some binary term.
<BR>
</LI>
<LI>
<CODE>{not_a_directory, FileName}</CODE>. The file
supplied with the <CODE>tmpdir</CODE> option is not a directory.
<BR>
</LI>
</UL>
<P><STRONG>Types</STRONG>
<PRE>
Binary = binary()
FileName = file_name()
FileNames = [FileName]
ICommand = read | close
IReply = end_of_input | {end_of_input, Value} | {[Object], Infun} | InputReply
Infun = fun(ICommand) -> IReply
Input = FileNames | Infun
InputReply = Term
KeyPos = int() > 0 | [int() > 0]
OCommand = {value, Value} | [Object] | close
OReply = Outfun | OutputReply
Object = Term | Binary
Outfun = fun(OCommand) -> OReply
Output = FileName | Outfun
OutputReply = Term
Term = term()
Value = Term
</PRE>
</DIV>
<H3>EXPORTS</H3>
<P><A NAME="sort/1"><STRONG><CODE>sort(FileName) -> Reply</CODE></STRONG></A><BR>
<A NAME="sort/2"><STRONG><CODE>sort(Input, Output) -> Reply</CODE></STRONG></A><BR>
<A NAME="sort/3"><STRONG><CODE>sort(Input, Output, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = ok | {error, Reason} | InputReply | OutputReply</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="keysort/2"><STRONG><CODE>keysort(KeyPos, FileName) -> Reply</CODE></STRONG></A><BR>
<A NAME="keysort/3"><STRONG><CODE>keysort(KeyPos, Input, Output) -> Reply</CODE></STRONG></A><BR>
<A NAME="keysort/4"><STRONG><CODE>keysort(KeyPos, Input, Output, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = ok | {error, Reason} | InputReply | OutputReply</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<P><A NAME="merge/2"><STRONG><CODE>merge(FileNames, Output) -> Reply</CODE></STRONG></A><BR>
<A NAME="merge/3"><STRONG><CODE>merge(FileNames, Output, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = ok | {error, Reason} | OutputReply</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>.
</DIV>
<P><A NAME="keymerge/3"><STRONG><CODE>keymerge(KeyPos, FileNames, Output) -> Reply</CODE></STRONG></A><BR>
<A NAME="keymerge/4"><STRONG><CODE>keymerge(KeyPos, FileNames, Output, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = ok | {error, Reason} | OutputReply</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<P>Merges tuples on files. Each input file is assumed to be
sorted on key(s).
<P><CODE>keymerge(KeyPos, FileNames, Output)</CODE> is equivalent
to <CODE>keymerge(KeyPos, FileNames, Output, [])</CODE>.
<P>
</DIV>
<P><A NAME="check/1"><STRONG><CODE>check(FileName) -> Reply</CODE></STRONG></A><BR>
<A NAME="check/2"><STRONG><CODE>check(FileNames, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = {ok, [Result]} | {error, Reason}</CODE></STRONG><BR>
<STRONG><CODE>Result = {FileName, TermPosition, Term}</CODE></STRONG><BR>
<STRONG><CODE>TermPosition = int() > 1</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>.
</DIV>
<P><A NAME="keycheck/2"><STRONG><CODE>keycheck(KeyPos, FileName) -> CheckReply</CODE></STRONG></A><BR>
<A NAME="keycheck/3"><STRONG><CODE>keycheck(KeyPos, FileNames, Options) -> Reply</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY><P>Types:
<DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Reply = {ok, [Result]} | {error, Reason}</CODE></STRONG><BR>
<STRONG><CODE>Result = {FileName, TermPosition, Term}</CODE></STRONG><BR>
<STRONG><CODE>TermPosition = int() > 1</CODE></STRONG><BR>
</DIV>
</DIV>
<DIV CLASS=REFBODY>
<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>
</DIV>
<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Hans Bolinder - support@erlang.ericsson.se<BR>
</DIV>
<CENTER>
<HR>
<SMALL>stdlib 1.14.2<BR>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|