/* * Add another component temp file. */ static void extendBufFile(BufFile *file) { File pfile; ResourceOwner oldowner; /* Be sure to associate the file with the BufFile's resource owner */ oldowner = CurrentResourceOwner; CurrentResourceOwner = file->resowner; if (file->fileset == NULL) pfile = OpenTemporaryFile(file->isInterXact); else pfile = MakeNewSharedSegment(file, file->numFiles); Assert(pfile >= 0); CurrentResourceOwner = oldowner; 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++; }
/* * Create a BufFile for a new temporary file (which will expand to become * multiple temporary files if more than MAX_PHYSICAL_FILESIZE bytes are * written to it). * * If interXact is true, the temp file will not be automatically deleted * at end of transaction. * * Note: if interXact is true, the caller had better be calling us in a * memory context that will survive across transaction boundaries. */ BufFile * BufFileCreateTemp(bool interXact) { BufFile *file; File pfile; pfile = OpenTemporaryFile(interXact); Assert(pfile >= 0); file = makeBufFile(pfile); file->isTemp = true; file->isInterXact = interXact; /* * add by cywang * * to record the time of read data from disk */ file->instr_fileload = InstrAlloc(1,1); file->fileLoadNum = 0; file->instr_filedump = InstrAlloc(1,1); file->fileDumpNum = 0; return file; }
/* * Create a BufFile for a new temporary file (which will expand to become * multiple temporary files if more than MAX_PHYSICAL_FILESIZE bytes are * written to it). * * If interXact is true, the temp file will not be automatically deleted * at end of transaction. * * Note: if interXact is true, the caller had better be calling us in a * memory context, and with a resource owner, that will survive across * transaction boundaries. */ BufFile * BufFileCreateTemp(bool interXact) { BufFile *file; File pfile; pfile = OpenTemporaryFile(interXact); Assert(pfile >= 0); file = makeBufFile(pfile); file->isInterXact = interXact; return file; }
/* * Add another component temp file. */ static void extendBufFile(BufFile *file) { 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++; }
/* * Create a BufFile for a new temporary file. * * Adds the pgsql_tmp/ prefix to the file path before creating. * * Note: if interXact is true, the caller had better be calling us in a * memory context that will survive across transaction boundaries. */ BufFile * BufFileCreateTemp(const char *filePrefix, bool interXact) { BufFile *file; File pfile; bool closeAtEOXact = !interXact; pfile = OpenTemporaryFile(filePrefix, true, /* makenameunique */ true, /* create */ true, /* delOnClose */ closeAtEOXact); /* closeAtEOXact */ Assert(pfile >= 0); file = makeBufFile(pfile); file->isTemp = true; return file; }
/* * Create a BufFile for a new temporary file used for writer-reader exchange. * * Adds the pgsql_tmp/ prefix to the file path before creating. * */ BufFile * BufFileCreateTemp_ReaderWriter(const char *filePrefix, bool isWriter, bool interXact) { bool closeAtEOXact = !interXact; File pfile = OpenTemporaryFile(filePrefix, false, /* makenameunique */ isWriter, /* create */ isWriter, /* delOnClose */ closeAtEOXact); /* closeAtEOXact */ if (pfile < 0) { elog(ERROR, "could not open temporary file \"%s\": %m", filePrefix); } BufFile *file = makeBufFile(pfile); file->isTemp = true; return file; }
int main(void) { const char * const MESSAGE = "Error in WriteStructures or ReadStructures or both:\n"; UChar *cp; int elemNo; size_t byteNo; FILE *temporaryFile; int gotError = 0; struct Test x[] = /* an array of structures */ { /* declare and initialize */ {23.6F, -425e-6, (void *)0x2345}, /* structure 0 */ {2, 1, 0}, /* structure 1 */ {-6, 3.3, NULL} /* structure 2 */ }; struct Test y[Elements(x)] = /* an array of structures */ { {123.456F, 789.12, (void *)0x9876}, /* structure 0 */ {69.0F, 24, (void *)0x1928A}, /* structure 1 */ {-59.2F, 89.48e-16, (void *)0xABC} /* structure 2 */ }; /* file for unaltered structures */ temporaryFile = OpenTemporaryFile(); /* write multiple structures */ WriteStructures(x, Elements(x), temporaryFile); /* back to beginning */ rewind(temporaryFile); /* read multiple structures */ ReadStructures(y, Elements(y), temporaryFile); fclose(temporaryFile); printf("\n"); /* Structure member values before and after write/read. */ for (elemNo = 0; elemNo < (int)Elements(x); ++elemNo) { if (y[elemNo].flt != x[elemNo].flt) { printf(MESSAGE); printf("Original structure[%d].flt: %g\n", elemNo, x[elemNo].flt); printf("Readback structure[%d].flt: %g\n\n", elemNo, y[elemNo].flt); gotError = 1; } if (y[elemNo].dbl != x[elemNo].dbl) { printf(MESSAGE); printf("Original structure[%d].dbl: %g\n", elemNo, x[elemNo].dbl); printf("Readback structure[%d].dbl: %g\n\n", elemNo, y[elemNo].dbl); gotError = 1; } if (y[elemNo].vp != x[elemNo].vp) { printf(MESSAGE); printf("Original structure[%d].vp: %p\n", elemNo, x[elemNo].vp); printf("Readback structure[%d].vp: %p\n\n", elemNo, y[elemNo].vp); gotError = 1; } } if (gotError) return EXIT_SUCCESS; /* Print bytes in structures before & after reversal. */ printf("Structure bytes before (1st line) & after (2nd line) reversal:\n\n"); for (elemNo = 0; elemNo < (int)Elements(x); ++elemNo, putchar('\n')) { y[elemNo] = x[elemNo]; ReverseMembersEndian(&y[elemNo]); printf("Element %d:\n", elemNo); printf(" "); cp = (UChar *)&x[elemNo]; for (byteNo = 0; byteNo < sizeof(*x); ++byteNo) printf(" %02x", cp[byteNo]); printf("\n "); cp = (UChar *)&y[elemNo]; for (byteNo = 0; byteNo < sizeof(*y); ++byteNo) printf(" %02x", cp[byteNo]); } printf("\n\n" "PLEASE BE SURE YOU HAVE ANSWERED THE FOLLOWING QUESTIONS:\n" " 1. Were the results you got correct for your implemenation?\n" " 2. How many padding bytes were in your structure?\n\n"); return EXIT_SUCCESS; }