PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
buffile.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BufFile BufFile
 

Functions

BufFileBufFileCreateTemp (bool interXact)
 
void BufFileClose (BufFile *file)
 
size_t BufFileRead (BufFile *file, void *ptr, size_t size)
 
size_t BufFileWrite (BufFile *file, void *ptr, size_t size)
 
int BufFileSeek (BufFile *file, int fileno, off_t offset, int whence)
 
void BufFileTell (BufFile *file, int *fileno, off_t *offset)
 
int BufFileSeekBlock (BufFile *file, long blknum)
 

Typedef Documentation

Definition at line 31 of file buffile.h.

Function Documentation

void BufFileClose ( BufFile file)

Definition at line 202 of file buffile.c.

References BufFileFlush(), FileClose(), BufFile::files, i, BufFile::numFiles, BufFile::offsets, and pfree().

Referenced by ExecHashJoinNewBatch(), ExecHashTableDestroy(), gistFreeBuildBuffers(), LogicalTapeSetClose(), tuplestore_clear(), and tuplestore_end().

203 {
204  int i;
205 
206  /* flush any unwritten data */
207  BufFileFlush(file);
208  /* close the underlying file(s) (with delete if it's a temp file) */
209  for (i = 0; i < file->numFiles; i++)
210  FileClose(file->files[i]);
211  /* release the buffer space */
212  pfree(file->files);
213  pfree(file->offsets);
214  pfree(file);
215 }
off_t * offsets
Definition: buffile.c:63
static int BufFileFlush(BufFile *file)
Definition: buffile.c:455
int numFiles
Definition: buffile.c:60
void pfree(void *pointer)
Definition: mcxt.c:992
File * files
Definition: buffile.c:62
void FileClose(File file)
Definition: fd.c:1418
int i
BufFile* BufFileCreateTemp ( bool  interXact)

Definition at line 166 of file buffile.c.

References Assert, BufFile::isInterXact, BufFile::isTemp, makeBufFile(), and OpenTemporaryFile().

Referenced by ExecHashJoinSaveTuple(), gistInitBuildBuffers(), LogicalTapeSetCreate(), and tuplestore_puttuple_common().

167 {
168  BufFile *file;
169  File pfile;
170 
171  pfile = OpenTemporaryFile(interXact);
172  Assert(pfile >= 0);
173 
174  file = makeBufFile(pfile);
175  file->isTemp = true;
176  file->isInterXact = interXact;
177 
178  return file;
179 }
bool isTemp
Definition: buffile.c:70
bool isInterXact
Definition: buffile.c:71
static BufFile * makeBufFile(File firstfile)
Definition: buffile.c:104
#define Assert(condition)
Definition: c.h:670
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1296
int File
Definition: fd.h:51
size_t BufFileRead ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 358 of file buffile.c.

References Assert, BufFile::buffer, BufFileFlush(), BufFileLoadBuffer(), BufFile::curOffset, BufFile::dirty, BufFile::nbytes, and BufFile::pos.

Referenced by ExecHashJoinGetSavedTuple(), getlen(), ltsReadBlock(), ReadTempFileBlock(), and readtup_heap().

359 {
360  size_t nread = 0;
361  size_t nthistime;
362 
363  if (file->dirty)
364  {
365  if (BufFileFlush(file) != 0)
366  return 0; /* could not flush... */
367  Assert(!file->dirty);
368  }
369 
370  while (size > 0)
371  {
372  if (file->pos >= file->nbytes)
373  {
374  /* Try to load more data into buffer. */
375  file->curOffset += file->pos;
376  file->pos = 0;
377  file->nbytes = 0;
378  BufFileLoadBuffer(file);
379  if (file->nbytes <= 0)
380  break; /* no more data available */
381  }
382 
383  nthistime = file->nbytes - file->pos;
384  if (nthistime > size)
385  nthistime = size;
386  Assert(nthistime > 0);
387 
388  memcpy(ptr, file->buffer + file->pos, nthistime);
389 
390  file->pos += nthistime;
391  ptr = (void *) ((char *) ptr + nthistime);
392  size -= nthistime;
393  nread += nthistime;
394  }
395 
396  return nread;
397 }
int nbytes
Definition: buffile.c:88
static int BufFileFlush(BufFile *file)
Definition: buffile.c:455
off_t curOffset
Definition: buffile.c:86
#define Assert(condition)
Definition: c.h:670
bool dirty
Definition: buffile.c:72
char buffer[BLCKSZ]
Definition: buffile.c:89
static void BufFileLoadBuffer(BufFile *file)
Definition: buffile.c:225
int pos
Definition: buffile.c:87
int BufFileSeek ( BufFile file,
int  fileno,
off_t  offset,
int  whence 
)

Definition at line 478 of file buffile.c.

References BufFileFlush(), BufFile::curFile, BufFile::curOffset, elog, ERROR, BufFile::isTemp, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, and BufFile::pos.

Referenced by BufFileSeekBlock(), ExecHashJoinNewBatch(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), tuplestore_rescan(), and tuplestore_select_read_pointer().

479 {
480  int newFile;
481  off_t newOffset;
482 
483  switch (whence)
484  {
485  case SEEK_SET:
486  if (fileno < 0)
487  return EOF;
488  newFile = fileno;
489  newOffset = offset;
490  break;
491  case SEEK_CUR:
492 
493  /*
494  * Relative seek considers only the signed offset, ignoring
495  * fileno. Note that large offsets (> 1 gig) risk overflow in this
496  * add, unless we have 64-bit off_t.
497  */
498  newFile = file->curFile;
499  newOffset = (file->curOffset + file->pos) + offset;
500  break;
501 #ifdef NOT_USED
502  case SEEK_END:
503  /* could be implemented, not needed currently */
504  break;
505 #endif
506  default:
507  elog(ERROR, "invalid whence: %d", whence);
508  return EOF;
509  }
510  while (newOffset < 0)
511  {
512  if (--newFile < 0)
513  return EOF;
514  newOffset += MAX_PHYSICAL_FILESIZE;
515  }
516  if (newFile == file->curFile &&
517  newOffset >= file->curOffset &&
518  newOffset <= file->curOffset + file->nbytes)
519  {
520  /*
521  * Seek is to a point within existing buffer; we can just adjust
522  * pos-within-buffer, without flushing buffer. Note this is OK
523  * whether reading or writing, but buffer remains dirty if we were
524  * writing.
525  */
526  file->pos = (int) (newOffset - file->curOffset);
527  return 0;
528  }
529  /* Otherwise, must reposition buffer, so flush any dirty data */
530  if (BufFileFlush(file) != 0)
531  return EOF;
532 
533  /*
534  * At this point and no sooner, check for seek past last segment. The
535  * above flush could have created a new segment, so checking sooner would
536  * not work (at least not with this code).
537  */
538  if (file->isTemp)
539  {
540  /* convert seek to "start of next seg" to "end of last seg" */
541  if (newFile == file->numFiles && newOffset == 0)
542  {
543  newFile--;
544  newOffset = MAX_PHYSICAL_FILESIZE;
545  }
546  while (newOffset > MAX_PHYSICAL_FILESIZE)
547  {
548  if (++newFile >= file->numFiles)
549  return EOF;
550  newOffset -= MAX_PHYSICAL_FILESIZE;
551  }
552  }
553  if (newFile >= file->numFiles)
554  return EOF;
555  /* Seek is OK! */
556  file->curFile = newFile;
557  file->curOffset = newOffset;
558  file->pos = 0;
559  file->nbytes = 0;
560  return 0;
561 }
int nbytes
Definition: buffile.c:88
static int BufFileFlush(BufFile *file)
Definition: buffile.c:455
#define MAX_PHYSICAL_FILESIZE
Definition: buffile.c:50
bool isTemp
Definition: buffile.c:70
int numFiles
Definition: buffile.c:60
#define ERROR
Definition: elog.h:43
off_t curOffset
Definition: buffile.c:86
int curFile
Definition: buffile.c:85
#define elog
Definition: elog.h:219
int pos
Definition: buffile.c:87
int BufFileSeekBlock ( BufFile file,
long  blknum 
)

Definition at line 582 of file buffile.c.

References BUFFILE_SEG_SIZE, and BufFileSeek().

Referenced by ltsReadBlock(), ltsWriteBlock(), ReadTempFileBlock(), and WriteTempFileBlock().

583 {
584  return BufFileSeek(file,
585  (int) (blknum / BUFFILE_SEG_SIZE),
586  (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
587  SEEK_SET);
588 }
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:478
#define BUFFILE_SEG_SIZE
Definition: buffile.c:51
void BufFileTell ( BufFile file,
int *  fileno,
off_t *  offset 
)

Definition at line 564 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, and BufFile::pos.

Referenced by dumptuples(), tuplestore_copy_read_pointer(), tuplestore_gettuple(), tuplestore_puttuple_common(), and tuplestore_select_read_pointer().

565 {
566  *fileno = file->curFile;
567  *offset = file->curOffset + file->pos;
568 }
off_t curOffset
Definition: buffile.c:86
int curFile
Definition: buffile.c:85
int pos
Definition: buffile.c:87
size_t BufFileWrite ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 405 of file buffile.c.

References Assert, BufFile::buffer, BufFileDumpBuffer(), BufFile::curOffset, BufFile::dirty, BufFile::nbytes, and BufFile::pos.

Referenced by ExecHashJoinSaveTuple(), ltsWriteBlock(), WriteTempFileBlock(), and writetup_heap().

406 {
407  size_t nwritten = 0;
408  size_t nthistime;
409 
410  while (size > 0)
411  {
412  if (file->pos >= BLCKSZ)
413  {
414  /* Buffer full, dump it out */
415  if (file->dirty)
416  {
417  BufFileDumpBuffer(file);
418  if (file->dirty)
419  break; /* I/O error */
420  }
421  else
422  {
423  /* Hmm, went directly from reading to writing? */
424  file->curOffset += file->pos;
425  file->pos = 0;
426  file->nbytes = 0;
427  }
428  }
429 
430  nthistime = BLCKSZ - file->pos;
431  if (nthistime > size)
432  nthistime = size;
433  Assert(nthistime > 0);
434 
435  memcpy(file->buffer + file->pos, ptr, nthistime);
436 
437  file->dirty = true;
438  file->pos += nthistime;
439  if (file->nbytes < file->pos)
440  file->nbytes = file->pos;
441  ptr = (void *) ((char *) ptr + nthistime);
442  size -= nthistime;
443  nwritten += nthistime;
444  }
445 
446  return nwritten;
447 }
int nbytes
Definition: buffile.c:88
static void BufFileDumpBuffer(BufFile *file)
Definition: buffile.c:274
off_t curOffset
Definition: buffile.c:86
#define Assert(condition)
Definition: c.h:670
bool dirty
Definition: buffile.c:72
char buffer[BLCKSZ]
Definition: buffile.c:89
int pos
Definition: buffile.c:87