src/backend/utils/sort/logtape.c
/*
 * Write to a logical tape.
 *
 * There are no error returns; we ereport() on failure.
 */
Write to a logical tape.
There are no error returns; we ereport() on failure.
Write to a logical tape . There are no error returns ; we ereport () on failure .
void
LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
				 void *ptr, size_t size)
{
	LogicalTape *lt;
	size_t		nthistime;
	Assert(tapenum >= 0 && tapenum < lts->nTapes);
	lt = &lts->tapes[tapenum];
	Assert(lt->writing);
	if (lt->buffer == NULL)
	{
		lt->buffer = (char *) palloc(BLCKSZ);
		lt->buffer_size = BLCKSZ;
	}
	if (lt->indirect == NULL)
	{
		lt->indirect = (IndirectBlock *) palloc(sizeof(IndirectBlock));
		lt->indirect->nextSlot = 0;
		lt->indirect->nextup = NULL;
	}
	Assert(lt->buffer_size == BLCKSZ);
	while (size > 0)
	{
		if (lt->pos >= BLCKSZ)
		{
			if (lt->dirty)
				ltsDumpBuffer(lts, lt);
			else
			{
				elog(ERROR, "invalid logtape state: should be dirty");
			}
			lt->numFullBlocks++;
			lt->curBlockNumber++;
			lt->pos = 0;
			lt->nbytes = 0;
		}
		nthistime = BLCKSZ - lt->pos;
		if (nthistime > size)
			nthistime = size;
		Assert(nthistime > 0);
		memcpy(lt->buffer + lt->pos, ptr, nthistime);
		lt->dirty = true;
		lt->pos += nthistime;
		if (lt->nbytes < lt->pos)
			lt->nbytes = lt->pos;
		ptr = (void *) ((char *) ptr + nthistime);
		size -= nthistime;
	}
}
void LogicalTapeWrite ( LogicalTapeSet * lts , int tapenum , void * ptr , size_t size ) { LogicalTape * lt ; size_t nthistime ; Assert ( tapenum >= 0 && tapenum < lts -> nTapes ) ; lt = & lts -> tapes [ tapenum ] ; Assert ( lt -> writing ) ; if ( lt -> buffer == NULL ) { lt -> buffer = ( char * ) palloc ( BLCKSZ ) ; lt -> buffer_size = BLCKSZ ; } if ( lt -> indirect == NULL ) { lt -> indirect = ( IndirectBlock * ) palloc ( sizeof ( IndirectBlock ) ) ; lt -> indirect -> nextSlot = 0 ; lt -> indirect -> nextup = NULL ; } Assert ( lt -> buffer_size == BLCKSZ ) ; while ( size > 0 ) { if ( lt -> pos >= BLCKSZ ) { if ( lt -> dirty ) ltsDumpBuffer ( lts , lt ) ; else { elog ( ERROR , " " ) ; } lt -> numFullBlocks ++ ; lt -> curBlockNumber ++ ; lt -> pos = 0 ; lt -> nbytes = 0 ; } nthistime = BLCKSZ - lt -> pos ; if ( nthistime > size ) nthistime = size ; Assert ( nthistime > 0 ) ; memcpy ( lt -> buffer + lt -> pos , ptr , nthistime ) ; lt -> dirty = true ; lt -> pos += nthistime ; if ( lt -> nbytes < lt -> pos ) lt -> nbytes = lt -> pos ; ptr = ( void * ) ( ( char * ) ptr + nthistime ) ; size -= nthistime ; } }
Write to a logical tape.
Write to a logical tape .
/* Allocate data buffer and first indirect block on first write */ /* Buffer full, dump it out */ /* Hmm, went directly from reading to writing? */