Write pseudocode for an iterator that implements a version of the sort-merge algorithm where the result of the final merge is pipelined to its consumers. Your pseudocode must define the standard iterator functions open(), next(), and close(). Show what state information the iterator must maintain between calls.


Let denote the number of blocks in the main memory buffer available for sorting, that is, the number of disk blocks whose contents can be buffered in available main memory.

Let be the relation that we want to sort. Let be the iterator which returns blocks of the relation , or the rest of the relation, whichever is smaller.

The iterator , an integer representing the number of runs that are created and pointers s are the state information which need to be remembered by SortMerge between calls.

SortMerge::open()

  1. .open();
  2. ;
  3. while (.next() false)
    1. move the blocks of memory (or less), from ‘s output buffer to a variable .
    2. sort the in-memory part of the relation (i.e., ).
    3. write the sorted data to run file .
    4. define to be a pointer to the first tuple in .

boolean SortMerge::next()

  1. if ()
    1. choose the first tuple (in sort order) among those pointed by s.
    2. write the tuple to the output and increment the that points to it.
    3. return true;
  2. return false;

SortMerge::close()

  1. .close();

If you want to work on a similar question, check out: 15.7.