Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions

buffile.c File Reference

#include "postgres.h"
#include "executor/instrument.h"
#include "storage/fd.h"
#include "storage/buffile.h"
#include "storage/buf_internals.h"
Include dependency graph for buffile.c:

Go to the source code of this file.

Data Structures

struct  BufFile

Defines

#define MAX_PHYSICAL_FILESIZE   0x40000000
#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Functions

static BufFilemakeBufFile (File firstfile)
static void extendBufFile (BufFile *file)
static void BufFileLoadBuffer (BufFile *file)
static void BufFileDumpBuffer (BufFile *file)
static int BufFileFlush (BufFile *file)
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)

Define Documentation

#define BUFFILE_SEG_SIZE   (MAX_PHYSICAL_FILESIZE / BLCKSZ)

Definition at line 48 of file buffile.c.

Referenced by BufFileSeekBlock().

#define MAX_PHYSICAL_FILESIZE   0x40000000

Definition at line 47 of file buffile.c.

Referenced by BufFileDumpBuffer(), BufFileLoadBuffer(), and BufFileSeek().


Function Documentation

void BufFileClose ( BufFile file  ) 

Definition at line 183 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().

{
    int         i;

    /* flush any unwritten data */
    BufFileFlush(file);
    /* close the underlying file(s) (with delete if it's a temp file) */
    for (i = 0; i < file->numFiles; i++)
        FileClose(file->files[i]);
    /* release the buffer space */
    pfree(file->files);
    pfree(file->offsets);
    pfree(file);
}

BufFile* BufFileCreateTemp ( bool  interXact  ) 

Definition at line 147 of file buffile.c.

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

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

{
    BufFile    *file;
    File        pfile;

    pfile = OpenTemporaryFile(interXact);
    Assert(pfile >= 0);

    file = makeBufFile(pfile);
    file->isTemp = true;
    file->isInterXact = interXact;

    return file;
}

static void BufFileDumpBuffer ( BufFile file  )  [static]

Definition at line 255 of file buffile.c.

References Assert, BufFile::buffer, BufFile::curFile, BufFile::curOffset, BufFile::dirty, extendBufFile(), BufFile::files, FileSeek(), FileWrite(), BufFile::isTemp, MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, BufFile::pos, BufferUsage::temp_blks_written, and wpos.

Referenced by BufFileFlush(), and BufFileWrite().

{
    int         wpos = 0;
    int         bytestowrite;
    File        thisfile;

    /*
     * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
     * crosses a component-file boundary; so we need a loop.
     */
    while (wpos < file->nbytes)
    {
        /*
         * Advance to next component file if necessary and possible.
         */
        if (file->curOffset >= MAX_PHYSICAL_FILESIZE && file->isTemp)
        {
            while (file->curFile + 1 >= file->numFiles)
                extendBufFile(file);
            file->curFile++;
            file->curOffset = 0L;
        }

        /*
         * Enforce per-file size limit only for temp files, else just try to
         * write as much as asked...
         */
        bytestowrite = file->nbytes - wpos;
        if (file->isTemp)
        {
            off_t       availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;

            if ((off_t) bytestowrite > availbytes)
                bytestowrite = (int) availbytes;
        }

        /*
         * May need to reposition physical file.
         */
        thisfile = file->files[file->curFile];
        if (file->curOffset != file->offsets[file->curFile])
        {
            if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
                return;         /* seek failed, give up */
            file->offsets[file->curFile] = file->curOffset;
        }
        bytestowrite = FileWrite(thisfile, file->buffer + wpos, bytestowrite);
        if (bytestowrite <= 0)
            return;             /* failed to write */
        file->offsets[file->curFile] += bytestowrite;
        file->curOffset += bytestowrite;
        wpos += bytestowrite;

        pgBufferUsage.temp_blks_written++;
    }
    file->dirty = false;

    /*
     * At this point, curOffset has been advanced to the end of the buffer,
     * ie, its original value + nbytes.  We need to make it point to the
     * logical file position, ie, original value + pos, in case that is less
     * (as could happen due to a small backwards seek in a dirty buffer!)
     */
    file->curOffset -= (file->nbytes - file->pos);
    if (file->curOffset < 0)    /* handle possible segment crossing */
    {
        file->curFile--;
        Assert(file->curFile >= 0);
        file->curOffset += MAX_PHYSICAL_FILESIZE;
    }

    /*
     * Now we can set the buffer empty without changing the logical position
     */
    file->pos = 0;
    file->nbytes = 0;
}

static int BufFileFlush ( BufFile file  )  [static]

Definition at line 436 of file buffile.c.

References BufFileDumpBuffer(), and BufFile::dirty.

Referenced by BufFileClose(), BufFileRead(), and BufFileSeek().

{
    if (file->dirty)
    {
        BufFileDumpBuffer(file);
        if (file->dirty)
            return EOF;
    }

    return 0;
}

static void BufFileLoadBuffer ( BufFile file  )  [static]

Definition at line 206 of file buffile.c.

References BufFile::buffer, BufFile::curFile, BufFile::curOffset, FileRead(), BufFile::files, FileSeek(), MAX_PHYSICAL_FILESIZE, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, pgBufferUsage, and BufferUsage::temp_blks_read.

Referenced by BufFileRead().

{
    File        thisfile;

    /*
     * Advance to next component file if necessary and possible.
     *
     * This path can only be taken if there is more than one component, so it
     * won't interfere with reading a non-temp file that is over
     * MAX_PHYSICAL_FILESIZE.
     */
    if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
        file->curFile + 1 < file->numFiles)
    {
        file->curFile++;
        file->curOffset = 0L;
    }

    /*
     * May need to reposition physical file.
     */
    thisfile = file->files[file->curFile];
    if (file->curOffset != file->offsets[file->curFile])
    {
        if (FileSeek(thisfile, file->curOffset, SEEK_SET) != file->curOffset)
            return;             /* seek failed, read nothing */
        file->offsets[file->curFile] = file->curOffset;
    }

    /*
     * Read whatever we can get, up to a full bufferload.
     */
    file->nbytes = FileRead(thisfile, file->buffer, sizeof(file->buffer));
    if (file->nbytes < 0)
        file->nbytes = 0;
    file->offsets[file->curFile] += file->nbytes;
    /* we choose not to advance curOffset here */

    pgBufferUsage.temp_blks_read++;
}

size_t BufFileRead ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 339 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().

{
    size_t      nread = 0;
    size_t      nthistime;

    if (file->dirty)
    {
        if (BufFileFlush(file) != 0)
            return 0;           /* could not flush... */
        Assert(!file->dirty);
    }

    while (size > 0)
    {
        if (file->pos >= file->nbytes)
        {
            /* Try to load more data into buffer. */
            file->curOffset += file->pos;
            file->pos = 0;
            file->nbytes = 0;
            BufFileLoadBuffer(file);
            if (file->nbytes <= 0)
                break;          /* no more data available */
        }

        nthistime = file->nbytes - file->pos;
        if (nthistime > size)
            nthistime = size;
        Assert(nthistime > 0);

        memcpy(ptr, file->buffer + file->pos, nthistime);

        file->pos += nthistime;
        ptr = (void *) ((char *) ptr + nthistime);
        size -= nthistime;
        nread += nthistime;
    }

    return nread;
}

int BufFileSeek ( BufFile file,
int  fileno,
off_t  offset,
int  whence 
)

Definition at line 459 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().

{
    int         newFile;
    off_t       newOffset;

    switch (whence)
    {
        case SEEK_SET:
            if (fileno < 0)
                return EOF;
            newFile = fileno;
            newOffset = offset;
            break;
        case SEEK_CUR:

            /*
             * Relative seek considers only the signed offset, ignoring
             * fileno. Note that large offsets (> 1 gig) risk overflow in this
             * add, unless we have 64-bit off_t.
             */
            newFile = file->curFile;
            newOffset = (file->curOffset + file->pos) + offset;
            break;
#ifdef NOT_USED
        case SEEK_END:
            /* could be implemented, not needed currently */
            break;
#endif
        default:
            elog(ERROR, "invalid whence: %d", whence);
            return EOF;
    }
    while (newOffset < 0)
    {
        if (--newFile < 0)
            return EOF;
        newOffset += MAX_PHYSICAL_FILESIZE;
    }
    if (newFile == file->curFile &&
        newOffset >= file->curOffset &&
        newOffset <= file->curOffset + file->nbytes)
    {
        /*
         * Seek is to a point within existing buffer; we can just adjust
         * pos-within-buffer, without flushing buffer.  Note this is OK
         * whether reading or writing, but buffer remains dirty if we were
         * writing.
         */
        file->pos = (int) (newOffset - file->curOffset);
        return 0;
    }
    /* Otherwise, must reposition buffer, so flush any dirty data */
    if (BufFileFlush(file) != 0)
        return EOF;

    /*
     * At this point and no sooner, check for seek past last segment. The
     * above flush could have created a new segment, so checking sooner would
     * not work (at least not with this code).
     */
    if (file->isTemp)
    {
        /* convert seek to "start of next seg" to "end of last seg" */
        if (newFile == file->numFiles && newOffset == 0)
        {
            newFile--;
            newOffset = MAX_PHYSICAL_FILESIZE;
        }
        while (newOffset > MAX_PHYSICAL_FILESIZE)
        {
            if (++newFile >= file->numFiles)
                return EOF;
            newOffset -= MAX_PHYSICAL_FILESIZE;
        }
    }
    if (newFile >= file->numFiles)
        return EOF;
    /* Seek is OK! */
    file->curFile = newFile;
    file->curOffset = newOffset;
    file->pos = 0;
    file->nbytes = 0;
    return 0;
}

int BufFileSeekBlock ( BufFile file,
long  blknum 
)

Definition at line 563 of file buffile.c.

References BUFFILE_SEG_SIZE, and BufFileSeek().

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

{
    return BufFileSeek(file,
                       (int) (blknum / BUFFILE_SEG_SIZE),
                       (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
                       SEEK_SET);
}

void BufFileTell ( BufFile file,
int *  fileno,
off_t *  offset 
)
size_t BufFileWrite ( BufFile file,
void *  ptr,
size_t  size 
)

Definition at line 386 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().

{
    size_t      nwritten = 0;
    size_t      nthistime;

    while (size > 0)
    {
        if (file->pos >= BLCKSZ)
        {
            /* Buffer full, dump it out */
            if (file->dirty)
            {
                BufFileDumpBuffer(file);
                if (file->dirty)
                    break;      /* I/O error */
            }
            else
            {
                /* Hmm, went directly from reading to writing? */
                file->curOffset += file->pos;
                file->pos = 0;
                file->nbytes = 0;
            }
        }

        nthistime = BLCKSZ - file->pos;
        if (nthistime > size)
            nthistime = size;
        Assert(nthistime > 0);

        memcpy(file->buffer + file->pos, ptr, nthistime);

        file->dirty = true;
        file->pos += nthistime;
        if (file->nbytes < file->pos)
            file->nbytes = file->pos;
        ptr = (void *) ((char *) ptr + nthistime);
        size -= nthistime;
        nwritten += nthistime;
    }

    return nwritten;
}

static void extendBufFile ( BufFile file  )  [static]

Definition at line 118 of file buffile.c.

References Assert, BufFile::files, BufFile::isInterXact, BufFile::isTemp, BufFile::numFiles, BufFile::offsets, OpenTemporaryFile(), and repalloc().

Referenced by BufFileDumpBuffer().

{
    File        pfile;

    Assert(file->isTemp);
    pfile = OpenTemporaryFile(file->isInterXact);
    Assert(pfile >= 0);

    file->files = (File *) repalloc(file->files,
                                    (file->numFiles + 1) * sizeof(File));
    file->offsets = (off_t *) repalloc(file->offsets,
                                       (file->numFiles + 1) * sizeof(off_t));
    file->files[file->numFiles] = pfile;
    file->offsets[file->numFiles] = 0L;
    file->numFiles++;
}

static BufFile * makeBufFile ( File  firstfile  )  [static]

Definition at line 94 of file buffile.c.

References BufFile::curFile, BufFile::curOffset, BufFile::dirty, BufFile::files, BufFile::isInterXact, BufFile::isTemp, BufFile::nbytes, BufFile::numFiles, BufFile::offsets, palloc(), and BufFile::pos.

Referenced by BufFileCreateTemp().

{
    BufFile    *file = (BufFile *) palloc(sizeof(BufFile));

    file->numFiles = 1;
    file->files = (File *) palloc(sizeof(File));
    file->files[0] = firstfile;
    file->offsets = (off_t *) palloc(sizeof(off_t));
    file->offsets[0] = 0L;
    file->isTemp = false;
    file->isInterXact = false;
    file->dirty = false;
    file->curFile = 0;
    file->curOffset = 0L;
    file->pos = 0;
    file->nbytes = 0;

    return file;
}