📄 tuplestore.c
字号:
state->writepos_file, state->writepos_offset, SEEK_SET) != 0) elog(ERROR, "seek to EOF failed"); state->status = TSS_WRITEFILE; WRITETUP(state, tuple); break; default: elog(ERROR, "invalid tuplestore state"); break; }}/* * Fetch the next tuple in either forward or back direction. * Returns NULL if no more tuples. If should_free is set, the * caller must pfree the returned tuple when done with it. */void *tuplestore_gettuple(Tuplestorestate *state, bool forward, bool *should_free){ unsigned int tuplen; void *tup; Assert(forward || state->randomAccess); switch (state->status) { case TSS_INMEM: *should_free = false; if (forward) { if (state->current < state->memtupcount) return state->memtuples[state->current++]; state->eof_reached = true; return NULL; } else { if (state->current <= 0) return NULL; /* * if all tuples are fetched already then we return last * tuple, else - tuple before last returned. */ if (state->eof_reached) state->eof_reached = false; else { state->current--; /* last returned tuple */ if (state->current <= 0) return NULL; } return state->memtuples[state->current - 1]; } break; case TSS_WRITEFILE: /* Skip state change if we'll just return NULL */ if (state->eof_reached && forward) return NULL; /* * Switch from writing to reading. */ BufFileTell(state->myfile, &state->writepos_file, &state->writepos_offset); if (!state->eof_reached) if (BufFileSeek(state->myfile, state->readpos_file, state->readpos_offset, SEEK_SET) != 0) elog(ERROR, "seek failed"); state->status = TSS_READFILE; /* FALL THRU into READFILE case */ case TSS_READFILE: *should_free = true; if (forward) { if ((tuplen = getlen(state, true)) != 0) { tup = READTUP(state, tuplen); return tup; } else { state->eof_reached = true; return NULL; } } /* * Backward. * * if all tuples are fetched already then we return last tuple, * else - tuple before last returned. * * Back up to fetch previously-returned tuple's ending length * word. If seek fails, assume we are at start of file. */ if (BufFileSeek(state->myfile, 0, -(long) sizeof(unsigned int), SEEK_CUR) != 0) return NULL; tuplen = getlen(state, false); if (state->eof_reached) { state->eof_reached = false; /* We will return the tuple returned before returning NULL */ } else { /* * Back up to get ending length word of tuple before it. */ if (BufFileSeek(state->myfile, 0, -(long) (tuplen + 2 * sizeof(unsigned int)), SEEK_CUR) != 0) { /* * If that fails, presumably the prev tuple is the first * in the file. Back up so that it becomes next to read * in forward direction (not obviously right, but that is * what in-memory case does). */ if (BufFileSeek(state->myfile, 0, -(long) (tuplen + sizeof(unsigned int)), SEEK_CUR) != 0) elog(ERROR, "bogus tuple length in backward scan"); return NULL; } tuplen = getlen(state, false); } /* * Now we have the length of the prior tuple, back up and read it. * Note: READTUP expects we are positioned after the initial * length word of the tuple, so back up to that point. */ if (BufFileSeek(state->myfile, 0, -(long) tuplen, SEEK_CUR) != 0) elog(ERROR, "bogus tuple length in backward scan"); tup = READTUP(state, tuplen); return tup; default: elog(ERROR, "invalid tuplestore state"); return NULL; /* keep compiler quiet */ }}/* * dumptuples - remove tuples from memory and write to tape * * As a side effect, we must set readpos and markpos to the value * corresponding to "current"; otherwise, a dump would lose the current read * position. */static voiddumptuples(Tuplestorestate *state){ int i; for (i = 0;; i++) { if (i == state->current) BufFileTell(state->myfile, &state->readpos_file, &state->readpos_offset); if (i == state->markpos_current) BufFileTell(state->myfile, &state->markpos_file, &state->markpos_offset); if (i >= state->memtupcount) break; WRITETUP(state, state->memtuples[i]); } state->memtupcount = 0;}/* * tuplestore_rescan - rewind and replay the scan */voidtuplestore_rescan(Tuplestorestate *state){ switch (state->status) { case TSS_INMEM: state->eof_reached = false; state->current = 0; break; case TSS_WRITEFILE: state->eof_reached = false; state->readpos_file = 0; state->readpos_offset = 0L; break; case TSS_READFILE: state->eof_reached = false; if (BufFileSeek(state->myfile, 0, 0L, SEEK_SET) != 0) elog(ERROR, "seek to start failed"); break; default: elog(ERROR, "invalid tuplestore state"); break; }}/* * tuplestore_markpos - saves current position in the tuple sequence */voidtuplestore_markpos(Tuplestorestate *state){ switch (state->status) { case TSS_INMEM: state->markpos_current = state->current; break; case TSS_WRITEFILE: if (state->eof_reached) { /* Need to record the implicit read position */ BufFileTell(state->myfile, &state->markpos_file, &state->markpos_offset); } else { state->markpos_file = state->readpos_file; state->markpos_offset = state->readpos_offset; } break; case TSS_READFILE: BufFileTell(state->myfile, &state->markpos_file, &state->markpos_offset); break; default: elog(ERROR, "invalid tuplestore state"); break; }}/* * tuplestore_restorepos - restores current position in tuple sequence to * last saved position */voidtuplestore_restorepos(Tuplestorestate *state){ switch (state->status) { case TSS_INMEM: state->eof_reached = false; state->current = state->markpos_current; break; case TSS_WRITEFILE: state->eof_reached = false; state->readpos_file = state->markpos_file; state->readpos_offset = state->markpos_offset; break; case TSS_READFILE: state->eof_reached = false; if (BufFileSeek(state->myfile, state->markpos_file, state->markpos_offset, SEEK_SET) != 0) elog(ERROR, "tuplestore_restorepos failed"); break; default: elog(ERROR, "invalid tuplestore state"); break; }}/* * Tape interface routines */static unsigned intgetlen(Tuplestorestate *state, bool eofOK){ unsigned int len; size_t nbytes; nbytes = BufFileRead(state->myfile, (void *) &len, sizeof(len)); if (nbytes == sizeof(len)) return len; if (nbytes != 0) elog(ERROR, "unexpected end of tape"); if (!eofOK) elog(ERROR, "unexpected end of data"); return 0;}/* * Routines specialized for HeapTuple case */static void *copytup_heap(Tuplestorestate *state, void *tup){ HeapTuple tuple = (HeapTuple) tup; tuple = heap_copytuple(tuple); USEMEM(state, GetMemoryChunkSpace(tuple)); return (void *) tuple;}/* * We don't bother to write the HeapTupleData part of the tuple. */static voidwritetup_heap(Tuplestorestate *state, void *tup){ HeapTuple tuple = (HeapTuple) tup; unsigned int tuplen; tuplen = tuple->t_len + sizeof(tuplen); if (BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen)) != sizeof(tuplen)) elog(ERROR, "write failed"); if (BufFileWrite(state->myfile, (void *) tuple->t_data, tuple->t_len) != (size_t) tuple->t_len) elog(ERROR, "write failed"); if (state->randomAccess) /* need trailing length word? */ if (BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen)) != sizeof(tuplen)) elog(ERROR, "write failed"); FREEMEM(state, GetMemoryChunkSpace(tuple)); heap_freetuple(tuple);}static void *readtup_heap(Tuplestorestate *state, unsigned int len){ unsigned int tuplen = len - sizeof(unsigned int) + HEAPTUPLESIZE; HeapTuple tuple = (HeapTuple) palloc(tuplen); USEMEM(state, GetMemoryChunkSpace(tuple)); /* reconstruct the HeapTupleData portion */ tuple->t_len = len - sizeof(unsigned int); ItemPointerSetInvalid(&(tuple->t_self)); tuple->t_datamcxt = CurrentMemoryContext; tuple->t_data = (HeapTupleHeader) (((char *) tuple) + HEAPTUPLESIZE); /* read in the tuple proper */ if (BufFileRead(state->myfile, (void *) tuple->t_data, tuple->t_len) != (size_t) tuple->t_len) elog(ERROR, "unexpected end of data"); if (state->randomAccess) /* need trailing length word? */ if (BufFileRead(state->myfile, (void *) &tuplen, sizeof(tuplen)) != sizeof(tuplen)) elog(ERROR, "unexpected end of data"); return (void *) tuple;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -