Header And Logo

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

Typedefs | Functions

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

typedef struct BufFile BufFile

Definition at line 31 of file buffile.h.


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;
}

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;
}