/* * tuplestore_clear * * Delete all the contents of a tuplestore, and reset its read pointers * to the start. */ void tuplestore_clear(Tuplestorestate *state) { int i; TSReadPointer *readptr; if (state->myfile) BufFileClose(state->myfile); state->myfile = NULL; if (state->memtuples) { for (i = state->memtupdeleted; i < state->memtupcount; i++) { FREEMEM(state, GetMemoryChunkSpace(state->memtuples[i])); pfree(state->memtuples[i]); } } state->status = TSS_INMEM; state->truncated = false; state->memtupdeleted = 0; state->memtupcount = 0; state->tuples = 0; readptr = state->readptrs; for (i = 0; i < state->readptrcount; readptr++, i++) { readptr->eof_reached = false; readptr->current = 0; } }
/* * Free buffering build data structure. */ void gistFreeBuildBuffers(GISTBuildBuffers *gfbb) { /* Close buffers file. */ BufFileClose(gfbb->pfile); /* All other things will be freed on memory context release */ }
FontFilePtr FontFileOpen (const char *name) { int fd; int len; BufFilePtr raw, cooked; fd = open (name, O_BINARY|O_CLOEXEC); if (fd < 0) return 0; raw = BufFileOpenRead (fd); if (!raw) { close (fd); return 0; } len = strlen (name); if (len > 2 && !strcmp (name + len - 2, ".Z")) { cooked = BufFilePushCompressed (raw); if (!cooked) { BufFileClose (raw, TRUE); return 0; } raw = cooked; #ifdef X_GZIP_FONT_COMPRESSION } else if (len > 3 && !strcmp (name + len - 3, ".gz")) { cooked = BufFilePushZIP (raw); if (!cooked) { BufFileClose (raw, TRUE); return 0; } raw = cooked; #endif #ifdef X_BZIP2_FONT_COMPRESSION } else if (len > 4 && !strcmp (name + len - 4, ".bz2")) { cooked = BufFilePushBZIP2 (raw); if (!cooked) { BufFileClose (raw, TRUE); return 0; } raw = cooked; #endif } return (FontFilePtr) raw; }
/* * Free any shared snapshot files. */ void AtEOXact_SharedSnapshot(void) { ListCell *lc; List *oldlist; oldlist = shared_snapshot_files; shared_snapshot_files = NIL; foreach(lc, oldlist) { BufFileClose((BufFile * ) lfirst(lc)); }
/* * tuplestore_end * * Release resources and clean up. */ void tuplestore_end(Tuplestorestate *state) { int i; if (state->myfile) BufFileClose(state->myfile); if (state->memtuples) { for (i = state->memtupdeleted; i < state->memtupcount; i++) pfree(state->memtuples[i]); pfree(state->memtuples); } pfree(state->readptrs); pfree(state); }
/* * Close a logical tape set and release all resources. */ void LogicalTapeSetClose(LogicalTapeSet *lts) { LogicalTape *lt; int i; BufFileClose(lts->pfile); for (i = 0; i < lts->nTapes; i++) { lt = <s->tapes[i]; if (lt->buffer) pfree(lt->buffer); } pfree(lts->freeBlocks); pfree(lts); }
/* * tuplestore_end * * Release resources and clean up. */ void tuplestore_end(Tuplestorestate *state) { int i; /* * CDB: Report statistics to EXPLAIN ANALYZE. */ if (state->instrument) { double nbytes; /* How close did we come to the work_mem limit? */ FREEMEM(state, 0); /* update low-water mark */ nbytes = state->allowedMem - state->availMemMin; state->instrument->workmemused = Max(state->instrument->workmemused, nbytes); /* How much work_mem would be enough to hold all tuples in memory? */ if (state->spilledBytes > 0) { nbytes = state->allowedMem - state->availMem + state->spilledBytes; state->instrument->workmemwanted = Max(state->instrument->workmemwanted, nbytes); } } if (state->myfile) BufFileClose(state->myfile); if (state->memtuples) { for (i = 0; i < state->memtupcount; i++) { if(state->memtuples[i]) pfree(state->memtuples[i]); } pfree(state->memtuples); } pfree(state); }
/* * Close a logical tape set and release all resources. */ void LogicalTapeSetClose(LogicalTapeSet *lts) { LogicalTape *lt; IndirectBlock *ib, *nextib; int i; BufFileClose(lts->pfile); for (i = 0; i < lts->nTapes; i++) { lt = <s->tapes[i]; for (ib = lt->indirect; ib != NULL; ib = nextib) { nextib = ib->nextup; pfree(ib); } if (lt->buffer) pfree(lt->buffer); } pfree(lts->freeBlocks); pfree(lts); }
/* * ExecWorkFile_Close * close the work file, and release the space. * * Returns the actual size of the file on disk upon closing */ int64 ExecWorkFile_Close(ExecWorkFile *workfile) { Assert(workfile != NULL); bfz_t *bfz_file = NULL; switch(workfile->fileType) { case BUFFILE: BufFileClose((BufFile *)workfile->file); break; case BFZ: bfz_file = (bfz_t *)workfile->file; Assert(bfz_file != NULL); if (bfz_file->mode == BFZ_MODE_APPEND) { /* Flush data out to disk if we were writing */ int64 file_size = bfz_append_end(bfz_file); /* Adjust the size with WorkfileDiskspace to our actual size */ ExecWorkFile_AdjustBFZSize(workfile, file_size); } bfz_close(bfz_file, true, true); break; default: insist_log(false, "invalid work file type: %d", workfile->fileType); } int64 size = ExecWorkFile_GetSize(workfile); pfree(workfile->fileName); pfree(workfile); return size; }
int FontFileClose (FontFilePtr f) { return BufFileClose ((BufFilePtr) f, TRUE); }
void readSharedLocalSnapshot_forCursor(Snapshot snapshot) { BufFile *f; char *fname=NULL; Size count=0, sanity; uint8 *p, *buffer=NULL; pid_t writerPid; TransactionId localXid; CommandId localCid; TimestampTz localXactStartTimestamp; uint32 combocidcnt; ComboCidKeyData tmp_combocids[MaxComboCids]; uint32 sub_size; uint32 read_size; int64 subcnt; TransactionId *subxids = NULL; Assert(Gp_role == GP_ROLE_EXECUTE); Assert(!Gp_is_writer); Assert(SharedLocalSnapshotSlot != NULL); Assert(snapshot->xip != NULL); Assert(snapshot->subxip != NULL); /* * Open our dump-file, this will either return a valid file, or * throw an error. * * NOTE: this is always run *after* the dump by the writer is * guaranteed to have completed. */ fname = sharedLocalSnapshot_filename(QEDtxContextInfo.distributedXid, QEDtxContextInfo.curcid, QEDtxContextInfo.segmateSync); f = BufFileCreateTemp_ReaderWriter(fname, false, false); /* we have our file. */ #define FileReadOK(file, ptr, size) (BufFileRead(file, ptr, size) == size) /* Read the file-length info */ if (!FileReadOK(f, &count, sizeof(count))) elog(ERROR, "Cursor snapshot: failed to read size"); elog(DEBUG1, "Reading in cursor-snapshot %u bytes", (unsigned int)count); buffer = palloc(count); /* * Seek back to the beginning: * We're going to read this all in one go, the size * of this buffer should be more than a few hundred bytes. */ if (BufFileSeek(f, 0 /* offset */, SEEK_SET) != 0) elog(ERROR, "Cursor snapshot: failed to seek."); if (!FileReadOK(f, buffer, count)) elog(ERROR, "Cursor snapshot: failed to read content"); /* we've got the entire snapshot read into our buffer. */ p = buffer; /* sanity check count */ memcpy(&sanity, p, sizeof(sanity)); if (sanity != count) elog(ERROR, "cursor snapshot failed sanity %u != %u", (unsigned int)sanity, (unsigned int)count); p += sizeof(sanity); memcpy(&sub_size, p, sizeof(uint32)); p += sizeof(uint32); /* see dumpSharedLocalSnapshot_forCursor() for the correct order here */ memcpy(&writerPid, p, sizeof(writerPid)); p += sizeof(writerPid); memcpy(&localXid, p, sizeof(localXid)); p += sizeof(localXid); memcpy(&localCid, p, sizeof(localCid)); p += sizeof(localCid); memcpy(&localXactStartTimestamp, p, sizeof(localXactStartTimestamp)); p += sizeof(localXactStartTimestamp); memcpy(&combocidcnt, p, sizeof(combocidcnt)); p += sizeof(combocidcnt); memcpy(tmp_combocids, p, sizeof(tmp_combocids)); p += sizeof(tmp_combocids); /* handle the combocid stuff (same as in GetSnapshotData()) */ if (usedComboCids != combocidcnt) { if (usedComboCids == 0) { MemoryContext oldCtx = MemoryContextSwitchTo(TopTransactionContext); comboCids = palloc(combocidcnt * sizeof(ComboCidKeyData)); MemoryContextSwitchTo(oldCtx); } else repalloc(comboCids, combocidcnt * sizeof(ComboCidKeyData)); } memcpy(comboCids, tmp_combocids, combocidcnt * sizeof(ComboCidKeyData)); usedComboCids = ((combocidcnt < MaxComboCids) ? combocidcnt : MaxComboCids); memcpy(&snapshot->xmin, p, sizeof(snapshot->xmin)); p += sizeof(snapshot->xmin); memcpy(&snapshot->xmax, p, sizeof(snapshot->xmax)); p += sizeof(snapshot->xmax); memcpy(&snapshot->xcnt, p, sizeof(snapshot->xcnt)); p += sizeof(snapshot->xcnt); memcpy(snapshot->xip, p, snapshot->xcnt * sizeof(TransactionId)); p += snapshot->xcnt * sizeof(TransactionId); /* zero out the slack in the xip-array */ memset(snapshot->xip + snapshot->xcnt, 0, (xipEntryCount - snapshot->xcnt)*sizeof(TransactionId)); memcpy(&snapshot->curcid, p, sizeof(snapshot->curcid)); /* Now we're done with the buffer */ pfree(buffer); /* * Now read the subtransaction ids. This can be a big number, so cannot * allocate memory all at once. */ sub_size *= sizeof(TransactionId); ResetXidBuffer(&subxbuf); if (sub_size) { subxids = palloc(MAX_XIDBUF_SIZE); } while (sub_size > 0) { read_size = sub_size > MAX_XIDBUF_SIZE ? MAX_XIDBUF_SIZE : sub_size; if (!FileReadOK(f, (char *)subxids, read_size)) { elog(ERROR, "Error in Reading Subtransaction file."); } subcnt = read_size/sizeof(TransactionId); AddSortedToXidBuffer(&subxbuf, subxids, subcnt); sub_size -= read_size; } if (subxids) { pfree(subxids); } /* we're done with file. */ BufFileClose(f); SetSharedTransactionId_reader(localXid, snapshot->curcid); return; }