src/backend/utils/sort/logtape.c
/*
 * Reset a logical tape's indirect-block hierarchy after a write pass
 * to prepare for reading.  We dump out partly-filled blocks except
 * at the top of the hierarchy, and we rewind each level to the start.
 * This call returns the first data block number, or -1L if the tape
 * is empty.
 *
 * Unless 'freezing' is true, release indirect blocks to the free pool after
 * reading them.
 */
Reset a logical tape's indirect-block hierarchy after a write pass
to prepare for reading.  We dump out partly-filled blocks except
at the top of the hierarchy, and we rewind each level to the start.
This call returns the first data block number, or -1L if the tape
is empty.

Unless 'freezing' is true, release indirect blocks to the free pool after
reading them.
Reset a logical tape ' s indirect - block hierarchy after a write pass to prepare for reading . We dump out partly - filled blocks except at the top of the hierarchy and we rewind each level to the start . This call returns the first data block number or - 1L if the tape is empty . Unless ' freezing ' is true release indirect blocks to the free pool after reading them .
static long
ltsRewindIndirectBlock(LogicalTapeSet *lts,
					   IndirectBlock *indirect,
					   bool freezing)
{
	if (indirect == NULL)
		return -1L;
	if (indirect->nextSlot < BLOCKS_PER_INDIR_BLOCK)
		indirect->ptrs[indirect->nextSlot] = -1L;
	if (indirect->nextup != NULL)
	{
		long		indirblock = ltsGetFreeBlock(lts);
		ltsWriteBlock(lts, indirblock, (void *) indirect->ptrs);
		ltsRecordBlockNum(lts, indirect->nextup, indirblock);
		indirblock = ltsRewindIndirectBlock(lts, indirect->nextup, freezing);
		Assert(indirblock != -1L);
		ltsReadBlock(lts, indirblock, (void *) indirect->ptrs);
		if (!freezing)
			ltsReleaseBlock(lts, indirblock);
	}
	indirect->nextSlot = 0;
	if (indirect->ptrs[0] == -1L)
		return -1L;
	return indirect->ptrs[indirect->nextSlot++];
}
static long ltsRewindIndirectBlock ( LogicalTapeSet * lts , IndirectBlock * indirect , bool freezing ) { if ( indirect == NULL ) return -1L ; if ( indirect -> nextSlot < BLOCKS_PER_INDIR_BLOCK ) indirect -> ptrs [ indirect -> nextSlot ] = -1L ; if ( indirect -> nextup != NULL ) { long indirblock = ltsGetFreeBlock ( lts ) ; ltsWriteBlock ( lts , indirblock , ( void * ) indirect -> ptrs ) ; ltsRecordBlockNum ( lts , indirect -> nextup , indirblock ) ; indirblock = ltsRewindIndirectBlock ( lts , indirect -> nextup , freezing ) ; Assert ( indirblock != -1L ) ; ltsReadBlock ( lts , indirblock , ( void * ) indirect -> ptrs ) ; if ( ! freezing ) ltsReleaseBlock ( lts , indirblock ) ; } indirect -> nextSlot = 0 ; if ( indirect -> ptrs [ 0 ] == -1L ) return -1L ; return indirect -> ptrs [ indirect -> nextSlot ++ ] ; }
Reset a logical tape's indirect-block hierarchy after a write pass
 to prepare for reading.
Reset a logical tape ' s indirect - block hierarchy after a write pass to prepare for reading .
/* Handle case of never-written-to tape */ /* Insert sentinel if block is not full */ /*
	 * If block is not topmost, write it out, and recurse to obtain address of
	 * first block in this hierarchy level.  Read that one in.
	 */ /*
	 * Reset my next-block pointer, and then fetch a block number if any.
	 */