PostgreSQL Source Code  git master
logtape.h File Reference
Include dependency graph for logtape.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TapeShare
 

Typedefs

typedef struct LogicalTapeSet LogicalTapeSet
 
typedef struct LogicalTape LogicalTape
 
typedef struct TapeShare TapeShare
 

Functions

LogicalTapeSetLogicalTapeSetCreate (bool preallocate, SharedFileSet *fileset, int worker)
 
void LogicalTapeClose (LogicalTape *lt)
 
void LogicalTapeSetClose (LogicalTapeSet *lts)
 
LogicalTapeLogicalTapeCreate (LogicalTapeSet *lts)
 
LogicalTapeLogicalTapeImport (LogicalTapeSet *lts, int worker, TapeShare *shared)
 
void LogicalTapeSetForgetFreeSpace (LogicalTapeSet *lts)
 
size_t LogicalTapeRead (LogicalTape *lt, void *ptr, size_t size)
 
void LogicalTapeWrite (LogicalTape *lt, void *ptr, size_t size)
 
void LogicalTapeRewindForRead (LogicalTape *lt, size_t buffer_size)
 
void LogicalTapeFreeze (LogicalTape *lt, TapeShare *share)
 
size_t LogicalTapeBackspace (LogicalTape *lt, size_t size)
 
void LogicalTapeSeek (LogicalTape *lt, long blocknum, int offset)
 
void LogicalTapeTell (LogicalTape *lt, long *blocknum, int *offset)
 
long LogicalTapeSetBlocks (LogicalTapeSet *lts)
 

Typedef Documentation

◆ LogicalTape

typedef struct LogicalTape LogicalTape

Definition at line 1 of file logtape.h.

◆ LogicalTapeSet

Definition at line 1 of file logtape.h.

◆ TapeShare

typedef struct TapeShare TapeShare

Function Documentation

◆ LogicalTapeBackspace()

size_t LogicalTapeBackspace ( LogicalTape lt,
size_t  size 
)

Definition at line 1063 of file logtape.c.

1064 {
1065  size_t seekpos = 0;
1066 
1067  Assert(lt->frozen);
1068  Assert(lt->buffer_size == BLCKSZ);
1069 
1070  if (lt->buffer == NULL)
1071  ltsInitReadBuffer(lt);
1072 
1073  /*
1074  * Easy case for seek within current block.
1075  */
1076  if (size <= (size_t) lt->pos)
1077  {
1078  lt->pos -= (int) size;
1079  return size;
1080  }
1081 
1082  /*
1083  * Not-so-easy case, have to walk back the chain of blocks. This
1084  * implementation would be pretty inefficient for long seeks, but we
1085  * really aren't doing that (a seek over one tuple is typical).
1086  */
1087  seekpos = (size_t) lt->pos; /* part within this block */
1088  while (size > seekpos)
1089  {
1090  long prev = TapeBlockGetTrailer(lt->buffer)->prev;
1091 
1092  if (prev == -1L)
1093  {
1094  /* Tried to back up beyond the beginning of tape. */
1095  if (lt->curBlockNumber != lt->firstBlockNumber)
1096  elog(ERROR, "unexpected end of tape");
1097  lt->pos = 0;
1098  return seekpos;
1099  }
1100 
1101  ltsReadBlock(lt->tapeSet, prev, (void *) lt->buffer);
1102 
1103  if (TapeBlockGetTrailer(lt->buffer)->next != lt->curBlockNumber)
1104  elog(ERROR, "broken tape, next of block %ld is %ld, expected %ld",
1105  prev,
1106  TapeBlockGetTrailer(lt->buffer)->next,
1107  lt->curBlockNumber);
1108 
1110  lt->curBlockNumber = prev;
1111  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1112 
1113  seekpos += TapeBlockPayloadSize;
1114  }
1115 
1116  /*
1117  * 'seekpos' can now be greater than 'size', because it points to the
1118  * beginning the target block. The difference is the position within the
1119  * page.
1120  */
1121  lt->pos = seekpos - size;
1122  return size;
1123 }
#define ERROR
Definition: elog.h:35
Assert(fmt[strlen(fmt) - 1] !='\n')
#define TapeBlockGetTrailer(buf)
Definition: logtape.c:104
#define TapeBlockPayloadSize
Definition: logtape.c:103
static void ltsInitReadBuffer(LogicalTape *lt)
Definition: logtape.c:529
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:282
while(p+4<=pend)
long curBlockNumber
Definition: logtape.c:157
char * buffer
Definition: logtape.c:164
bool frozen
Definition: logtape.c:142
long nextBlockNumber
Definition: logtape.c:158
int buffer_size
Definition: logtape.c:165
long firstBlockNumber
Definition: logtape.c:156
LogicalTapeSet * tapeSet
Definition: logtape.c:139
int nbytes
Definition: logtape.c:168

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, elog(), ERROR, LogicalTape::firstBlockNumber, LogicalTape::frozen, ltsInitReadBuffer(), ltsReadBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::pos, TapeBlockGetTrailer, TapeBlockPayloadSize, LogicalTape::tapeSet, and while().

Referenced by tuplesort_gettuple_common().

◆ LogicalTapeClose()

void LogicalTapeClose ( LogicalTape lt)

Definition at line 734 of file logtape.c.

735 {
736  if (lt->buffer)
737  pfree(lt->buffer);
738  pfree(lt);
739 }
void pfree(void *pointer)
Definition: mcxt.c:1306

References LogicalTape::buffer, and pfree().

Referenced by agg_refill_hash_table(), mergeruns(), and tuplesort_gettuple_common().

◆ LogicalTapeCreate()

LogicalTape* LogicalTapeCreate ( LogicalTapeSet lts)

Definition at line 681 of file logtape.c.

682 {
683  /*
684  * The only thing that currently prevents creating new tapes in leader is
685  * the fact that BufFiles opened using BufFileOpenShared() are read-only
686  * by definition, but that could be changed if it seemed worthwhile. For
687  * now, writing to the leader tape will raise a "Bad file descriptor"
688  * error, so tuplesort must avoid writing to the leader tape altogether.
689  */
690  if (lts->fileset && lts->worker == -1)
691  elog(ERROR, "cannot create new tapes in leader process");
692 
693  return ltsCreateTape(lts);
694 }
static LogicalTape * ltsCreateTape(LogicalTapeSet *lts)
Definition: logtape.c:697
SharedFileSet * fileset
Definition: logtape.c:190

References elog(), ERROR, LogicalTapeSet::fileset, ltsCreateTape(), and LogicalTapeSet::worker.

Referenced by hashagg_spill_init(), and selectnewtape().

◆ LogicalTapeFreeze()

void LogicalTapeFreeze ( LogicalTape lt,
TapeShare share 
)

Definition at line 982 of file logtape.c.

983 {
984  LogicalTapeSet *lts = lt->tapeSet;
985 
986  Assert(lt->writing);
987  Assert(lt->offsetBlockNumber == 0L);
988 
989  /*
990  * Completion of a write phase. Flush last partial data block, and rewind
991  * for nondestructive read.
992  */
993  if (lt->dirty)
994  {
995  /*
996  * As long as we've filled the buffer at least once, its contents are
997  * entirely defined from valgrind's point of view, even though
998  * contents beyond the current end point may be stale. But it's
999  * possible - at least in the case of a parallel sort - to sort such
1000  * small amount of data that we do not fill the buffer even once. Tell
1001  * valgrind that its contents are defined, so it doesn't bleat.
1002  */
1004  lt->buffer_size - lt->nbytes);
1005 
1006  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
1007  ltsWriteBlock(lt->tapeSet, lt->curBlockNumber, (void *) lt->buffer);
1008  }
1009  lt->writing = false;
1010  lt->frozen = true;
1011 
1012  /*
1013  * The seek and backspace functions assume a single block read buffer.
1014  * That's OK with current usage. A larger buffer is helpful to make the
1015  * read pattern of the backing file look more sequential to the OS, when
1016  * we're reading from multiple tapes. But at the end of a sort, when a
1017  * tape is frozen, we only read from a single tape anyway.
1018  */
1019  if (!lt->buffer || lt->buffer_size != BLCKSZ)
1020  {
1021  if (lt->buffer)
1022  pfree(lt->buffer);
1023  lt->buffer = palloc(BLCKSZ);
1024  lt->buffer_size = BLCKSZ;
1025  }
1026 
1027  /* Read the first block, or reset if tape is empty */
1028  lt->curBlockNumber = lt->firstBlockNumber;
1029  lt->pos = 0;
1030  lt->nbytes = 0;
1031 
1032  if (lt->firstBlockNumber == -1L)
1033  lt->nextBlockNumber = -1L;
1034  ltsReadBlock(lt->tapeSet, lt->curBlockNumber, (void *) lt->buffer);
1035  if (TapeBlockIsLast(lt->buffer))
1036  lt->nextBlockNumber = -1L;
1037  else
1038  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1039  lt->nbytes = TapeBlockGetNBytes(lt->buffer);
1040 
1041  /* Handle extra steps when caller is to share its tapeset */
1042  if (share)
1043  {
1045  share->firstblocknumber = lt->firstBlockNumber;
1046  }
1047 }
void BufFileExportFileSet(BufFile *file)
Definition: buffile.c:389
#define TapeBlockIsLast(buf)
Definition: logtape.c:107
#define TapeBlockGetNBytes(buf)
Definition: logtape.c:108
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
Definition: logtape.c:238
#define TapeBlockSetNBytes(buf, nbytes)
Definition: logtape.c:111
void * palloc(Size size)
Definition: mcxt.c:1199
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
BufFile * pfile
Definition: logtape.c:189
long offsetBlockNumber
Definition: logtape.c:159
bool writing
Definition: logtape.c:141
bool dirty
Definition: logtape.c:143
long firstblocknumber
Definition: logtape.h:54

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, BufFileExportFileSet(), LogicalTape::curBlockNumber, LogicalTape::dirty, LogicalTape::firstBlockNumber, TapeShare::firstblocknumber, LogicalTape::frozen, ltsReadBlock(), ltsWriteBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::offsetBlockNumber, palloc(), LogicalTapeSet::pfile, pfree(), LogicalTape::pos, TapeBlockGetNBytes, TapeBlockGetTrailer, TapeBlockIsLast, TapeBlockSetNBytes, LogicalTape::tapeSet, VALGRIND_MAKE_MEM_DEFINED, and LogicalTape::writing.

Referenced by mergeruns(), and worker_freeze_result_tape().

◆ LogicalTapeImport()

LogicalTape* LogicalTapeImport ( LogicalTapeSet lts,
int  worker,
TapeShare shared 
)

Definition at line 610 of file logtape.c.

611 {
612  LogicalTape *lt;
613  long tapeblocks;
614  char filename[MAXPGPATH];
615  BufFile *file;
616  int64 filesize;
617 
618  lt = ltsCreateTape(lts);
619 
620  /*
621  * build concatenated view of all buffiles, remembering the block number
622  * where each source file begins.
623  */
624  pg_itoa(worker, filename);
625  file = BufFileOpenFileSet(&lts->fileset->fs, filename, O_RDONLY, false);
626  filesize = BufFileSize(file);
627 
628  /*
629  * Stash first BufFile, and concatenate subsequent BufFiles to that. Store
630  * block offset into each tape as we go.
631  */
632  lt->firstBlockNumber = shared->firstblocknumber;
633  if (lts->pfile == NULL)
634  {
635  lts->pfile = file;
636  lt->offsetBlockNumber = 0L;
637  }
638  else
639  {
640  lt->offsetBlockNumber = BufFileAppend(lts->pfile, file);
641  }
642  /* Don't allocate more for read buffer than could possibly help */
643  lt->max_size = Min(MaxAllocSize, filesize);
644  tapeblocks = filesize / BLCKSZ;
645 
646  /*
647  * Update # of allocated blocks and # blocks written to reflect the
648  * imported BufFile. Allocated/written blocks include space used by holes
649  * left between concatenated BufFiles. Also track the number of hole
650  * blocks so that we can later work backwards to calculate the number of
651  * physical blocks for instrumentation.
652  */
654 
655  lts->nBlocksAllocated = lt->offsetBlockNumber + tapeblocks;
656  lts->nBlocksWritten = lts->nBlocksAllocated;
657 
658  return lt;
659 }
long BufFileAppend(BufFile *target, BufFile *source)
Definition: buffile.c:872
BufFile * BufFileOpenFileSet(FileSet *fileset, const char *name, int mode, bool missing_ok)
Definition: buffile.c:286
int64 BufFileSize(BufFile *file)
Definition: buffile.c:833
#define Min(x, y)
Definition: c.h:937
#define MaxAllocSize
Definition: memutils.h:40
int pg_itoa(int16 i, char *a)
Definition: numutils.c:332
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
long nBlocksAllocated
Definition: logtape.c:203
long nHoleBlocks
Definition: logtape.c:205
long nBlocksWritten
Definition: logtape.c:204
int max_size
Definition: logtape.c:166

References BufFileAppend(), BufFileOpenFileSet(), BufFileSize(), filename, LogicalTapeSet::fileset, LogicalTape::firstBlockNumber, TapeShare::firstblocknumber, SharedFileSet::fs, ltsCreateTape(), LogicalTape::max_size, MaxAllocSize, MAXPGPATH, Min, LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nBlocksWritten, LogicalTapeSet::nHoleBlocks, LogicalTape::offsetBlockNumber, LogicalTapeSet::pfile, and pg_itoa().

Referenced by leader_takeover_tapes().

◆ LogicalTapeRead()

size_t LogicalTapeRead ( LogicalTape lt,
void *  ptr,
size_t  size 
)

Definition at line 929 of file logtape.c.

930 {
931  size_t nread = 0;
932  size_t nthistime;
933 
934  Assert(!lt->writing);
935 
936  if (lt->buffer == NULL)
937  ltsInitReadBuffer(lt);
938 
939  while (size > 0)
940  {
941  if (lt->pos >= lt->nbytes)
942  {
943  /* Try to load more data into buffer. */
944  if (!ltsReadFillBuffer(lt))
945  break; /* EOF */
946  }
947 
948  nthistime = lt->nbytes - lt->pos;
949  if (nthistime > size)
950  nthistime = size;
951  Assert(nthistime > 0);
952 
953  memcpy(ptr, lt->buffer + lt->pos, nthistime);
954 
955  lt->pos += nthistime;
956  ptr = (void *) ((char *) ptr + nthistime);
957  size -= nthistime;
958  nread += nthistime;
959  }
960 
961  return nread;
962 }
static bool ltsReadFillBuffer(LogicalTape *lt)
Definition: logtape.c:305

References Assert(), LogicalTape::buffer, ltsInitReadBuffer(), ltsReadFillBuffer(), LogicalTape::nbytes, LogicalTape::pos, and LogicalTape::writing.

Referenced by getlen(), and hashagg_batch_read().

◆ LogicalTapeRewindForRead()

void LogicalTapeRewindForRead ( LogicalTape lt,
size_t  buffer_size 
)

Definition at line 847 of file logtape.c.

848 {
849  LogicalTapeSet *lts = lt->tapeSet;
850 
851  /*
852  * Round and cap buffer_size if needed.
853  */
854  if (lt->frozen)
855  buffer_size = BLCKSZ;
856  else
857  {
858  /* need at least one block */
859  if (buffer_size < BLCKSZ)
860  buffer_size = BLCKSZ;
861 
862  /* palloc() larger than max_size is unlikely to be helpful */
863  if (buffer_size > lt->max_size)
864  buffer_size = lt->max_size;
865 
866  /* round down to BLCKSZ boundary */
867  buffer_size -= buffer_size % BLCKSZ;
868  }
869 
870  if (lt->writing)
871  {
872  /*
873  * Completion of a write phase. Flush last partial data block, and
874  * rewind for normal (destructive) read.
875  */
876  if (lt->dirty)
877  {
878  /*
879  * As long as we've filled the buffer at least once, its contents
880  * are entirely defined from valgrind's point of view, even though
881  * contents beyond the current end point may be stale. But it's
882  * possible - at least in the case of a parallel sort - to sort
883  * such small amount of data that we do not fill the buffer even
884  * once. Tell valgrind that its contents are defined, so it
885  * doesn't bleat.
886  */
888  lt->buffer_size - lt->nbytes);
889 
890  TapeBlockSetNBytes(lt->buffer, lt->nbytes);
891  ltsWriteBlock(lt->tapeSet, lt->curBlockNumber, (void *) lt->buffer);
892  }
893  lt->writing = false;
894  }
895  else
896  {
897  /*
898  * This is only OK if tape is frozen; we rewind for (another) read
899  * pass.
900  */
901  Assert(lt->frozen);
902  }
903 
904  if (lt->buffer)
905  pfree(lt->buffer);
906 
907  /* the buffer is lazily allocated, but set the size here */
908  lt->buffer = NULL;
909  lt->buffer_size = buffer_size;
910 
911  /* free the preallocation list, and return unused block numbers */
912  if (lt->prealloc != NULL)
913  {
914  for (int i = lt->nprealloc; i > 0; i--)
915  ltsReleaseBlock(lts, lt->prealloc[i - 1]);
916  pfree(lt->prealloc);
917  lt->prealloc = NULL;
918  lt->nprealloc = 0;
919  lt->prealloc_size = 0;
920  }
921 }
int i
Definition: isn.c:73
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
Definition: logtape.c:476
long * prealloc
Definition: logtape.c:175
int nprealloc
Definition: logtape.c:176
int prealloc_size
Definition: logtape.c:177

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, LogicalTape::frozen, i, ltsReleaseBlock(), ltsWriteBlock(), LogicalTape::max_size, LogicalTape::nbytes, LogicalTape::nprealloc, pfree(), LogicalTape::prealloc, LogicalTape::prealloc_size, TapeBlockSetNBytes, LogicalTape::tapeSet, VALGRIND_MAKE_MEM_DEFINED, and LogicalTape::writing.

Referenced by hashagg_spill_finish(), mergeruns(), and tuplesort_rescan().

◆ LogicalTapeSeek()

void LogicalTapeSeek ( LogicalTape lt,
long  blocknum,
int  offset 
)

Definition at line 1134 of file logtape.c.

1135 {
1136  Assert(lt->frozen);
1137  Assert(offset >= 0 && offset <= TapeBlockPayloadSize);
1138  Assert(lt->buffer_size == BLCKSZ);
1139 
1140  if (lt->buffer == NULL)
1141  ltsInitReadBuffer(lt);
1142 
1143  if (blocknum != lt->curBlockNumber)
1144  {
1145  ltsReadBlock(lt->tapeSet, blocknum, (void *) lt->buffer);
1146  lt->curBlockNumber = blocknum;
1148  lt->nextBlockNumber = TapeBlockGetTrailer(lt->buffer)->next;
1149  }
1150 
1151  if (offset > lt->nbytes)
1152  elog(ERROR, "invalid tape seek position");
1153  lt->pos = offset;
1154 }

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, elog(), ERROR, LogicalTape::frozen, ltsInitReadBuffer(), ltsReadBlock(), LogicalTape::nbytes, LogicalTape::nextBlockNumber, LogicalTape::pos, TapeBlockGetTrailer, TapeBlockPayloadSize, and LogicalTape::tapeSet.

Referenced by tuplesort_restorepos().

◆ LogicalTapeSetBlocks()

long LogicalTapeSetBlocks ( LogicalTapeSet lts)

Definition at line 1184 of file logtape.c.

1185 {
1186  return lts->nBlocksWritten - lts->nHoleBlocks;
1187 }

References LogicalTapeSet::nBlocksWritten, and LogicalTapeSet::nHoleBlocks.

Referenced by hash_agg_update_metrics(), tuplesort_free(), and tuplesort_updatemax().

◆ LogicalTapeSetClose()

void LogicalTapeSetClose ( LogicalTapeSet lts)

Definition at line 668 of file logtape.c.

669 {
670  BufFileClose(lts->pfile);
671  pfree(lts->freeBlocks);
672  pfree(lts);
673 }
void BufFileClose(BufFile *file)
Definition: buffile.c:407
long * freeBlocks
Definition: logtape.c:216

References BufFileClose(), LogicalTapeSet::freeBlocks, LogicalTapeSet::pfile, and pfree().

Referenced by hashagg_reset_spill_state(), and tuplesort_free().

◆ LogicalTapeSetCreate()

LogicalTapeSet* LogicalTapeSetCreate ( bool  preallocate,
SharedFileSet fileset,
int  worker 
)

Definition at line 557 of file logtape.c.

558 {
559  LogicalTapeSet *lts;
560 
561  /*
562  * Create top-level struct including per-tape LogicalTape structs.
563  */
564  lts = (LogicalTapeSet *) palloc(sizeof(LogicalTapeSet));
565  lts->nBlocksAllocated = 0L;
566  lts->nBlocksWritten = 0L;
567  lts->nHoleBlocks = 0L;
568  lts->forgetFreeSpace = false;
569  lts->freeBlocksLen = 32; /* reasonable initial guess */
570  lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
571  lts->nFreeBlocks = 0;
572  lts->enable_prealloc = preallocate;
573 
574  lts->fileset = fileset;
575  lts->worker = worker;
576 
577  /*
578  * Create temp BufFile storage as required.
579  *
580  * In leader, we hijack the BufFile of the first tape that's imported, and
581  * concatenate the BufFiles of any subsequent tapes to that. Hence don't
582  * create a BufFile here. Things are simpler for the worker case and the
583  * serial case, though. They are generally very similar -- workers use a
584  * shared fileset, whereas serial sorts use a conventional serial BufFile.
585  */
586  if (fileset && worker == -1)
587  lts->pfile = NULL;
588  else if (fileset)
589  {
590  char filename[MAXPGPATH];
591 
592  pg_itoa(worker, filename);
593  lts->pfile = BufFileCreateFileSet(&fileset->fs, filename);
594  }
595  else
596  lts->pfile = BufFileCreateTemp(false);
597 
598  return lts;
599 }
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:188
BufFile * BufFileCreateFileSet(FileSet *fileset, const char *name)
Definition: buffile.c:262
long nFreeBlocks
Definition: logtape.c:217
bool forgetFreeSpace
Definition: logtape.c:215
Size freeBlocksLen
Definition: logtape.c:218
bool enable_prealloc
Definition: logtape.c:219

References BufFileCreateFileSet(), BufFileCreateTemp(), LogicalTapeSet::enable_prealloc, filename, LogicalTapeSet::fileset, LogicalTapeSet::forgetFreeSpace, LogicalTapeSet::freeBlocks, LogicalTapeSet::freeBlocksLen, SharedFileSet::fs, MAXPGPATH, LogicalTapeSet::nBlocksAllocated, LogicalTapeSet::nBlocksWritten, LogicalTapeSet::nFreeBlocks, LogicalTapeSet::nHoleBlocks, palloc(), LogicalTapeSet::pfile, pg_itoa(), and LogicalTapeSet::worker.

Referenced by hash_agg_enter_spill_mode(), inittapes(), and leader_takeover_tapes().

◆ LogicalTapeSetForgetFreeSpace()

void LogicalTapeSetForgetFreeSpace ( LogicalTapeSet lts)

Definition at line 751 of file logtape.c.

752 {
753  lts->forgetFreeSpace = true;
754 }

References LogicalTapeSet::forgetFreeSpace.

Referenced by mergeruns().

◆ LogicalTapeTell()

void LogicalTapeTell ( LogicalTape lt,
long *  blocknum,
int *  offset 
)

Definition at line 1163 of file logtape.c.

1164 {
1165  if (lt->buffer == NULL)
1166  ltsInitReadBuffer(lt);
1167 
1168  Assert(lt->offsetBlockNumber == 0L);
1169 
1170  /* With a larger buffer, 'pos' wouldn't be the same as offset within page */
1171  Assert(lt->buffer_size == BLCKSZ);
1172 
1173  *blocknum = lt->curBlockNumber;
1174  *offset = lt->pos;
1175 }

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, ltsInitReadBuffer(), LogicalTape::offsetBlockNumber, and LogicalTape::pos.

Referenced by tuplesort_markpos().

◆ LogicalTapeWrite()

void LogicalTapeWrite ( LogicalTape lt,
void *  ptr,
size_t  size 
)

Definition at line 762 of file logtape.c.

763 {
764  LogicalTapeSet *lts = lt->tapeSet;
765  size_t nthistime;
766 
767  Assert(lt->writing);
768  Assert(lt->offsetBlockNumber == 0L);
769 
770  /* Allocate data buffer and first block on first write */
771  if (lt->buffer == NULL)
772  {
773  lt->buffer = (char *) palloc(BLCKSZ);
774  lt->buffer_size = BLCKSZ;
775  }
776  if (lt->curBlockNumber == -1)
777  {
778  Assert(lt->firstBlockNumber == -1);
779  Assert(lt->pos == 0);
780 
781  lt->curBlockNumber = ltsGetBlock(lts, lt);
783 
784  TapeBlockGetTrailer(lt->buffer)->prev = -1L;
785  }
786 
787  Assert(lt->buffer_size == BLCKSZ);
788  while (size > 0)
789  {
790  if (lt->pos >= (int) TapeBlockPayloadSize)
791  {
792  /* Buffer full, dump it out */
793  long nextBlockNumber;
794 
795  if (!lt->dirty)
796  {
797  /* Hmm, went directly from reading to writing? */
798  elog(ERROR, "invalid logtape state: should be dirty");
799  }
800 
801  /*
802  * First allocate the next block, so that we can store it in the
803  * 'next' pointer of this block.
804  */
805  nextBlockNumber = ltsGetBlock(lt->tapeSet, lt);
806 
807  /* set the next-pointer and dump the current block. */
808  TapeBlockGetTrailer(lt->buffer)->next = nextBlockNumber;
809  ltsWriteBlock(lt->tapeSet, lt->curBlockNumber, (void *) lt->buffer);
810 
811  /* initialize the prev-pointer of the next block */
812  TapeBlockGetTrailer(lt->buffer)->prev = lt->curBlockNumber;
813  lt->curBlockNumber = nextBlockNumber;
814  lt->pos = 0;
815  lt->nbytes = 0;
816  }
817 
818  nthistime = TapeBlockPayloadSize - lt->pos;
819  if (nthistime > size)
820  nthistime = size;
821  Assert(nthistime > 0);
822 
823  memcpy(lt->buffer + lt->pos, ptr, nthistime);
824 
825  lt->dirty = true;
826  lt->pos += nthistime;
827  if (lt->nbytes < lt->pos)
828  lt->nbytes = lt->pos;
829  ptr = (void *) ((char *) ptr + nthistime);
830  size -= nthistime;
831  }
832 }
static long ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt)
Definition: logtape.c:365

References Assert(), LogicalTape::buffer, LogicalTape::buffer_size, LogicalTape::curBlockNumber, LogicalTape::dirty, elog(), ERROR, LogicalTape::firstBlockNumber, ltsGetBlock(), ltsWriteBlock(), LogicalTape::nbytes, LogicalTape::offsetBlockNumber, palloc(), LogicalTape::pos, TapeBlockGetTrailer, TapeBlockPayloadSize, LogicalTape::tapeSet, and LogicalTape::writing.

Referenced by hashagg_spill_tuple(), markrunend(), writetup_cluster(), writetup_datum(), writetup_heap(), and writetup_index().