Commit 26a7b48e authored by Tom Lane's avatar Tom Lane

Eliminate some repetitive coding in tuplesort.c.

Use a macro LogicalTapeReadExact() to encapsulate the error check when
we want to read an exact number of bytes from a "tape".  Per a suggestion
of Takahiro Itagaki.
parent 3ba11d3d
...@@ -120,9 +120,9 @@ ...@@ -120,9 +120,9 @@
/* sort-type codes for sort__start probes */ /* sort-type codes for sort__start probes */
#define HEAP_SORT 0 #define HEAP_SORT 0
#define INDEX_SORT 1 #define INDEX_SORT 1
#define DATUM_SORT 2 #define DATUM_SORT 2
#define CLUSTER_SORT 3 #define CLUSTER_SORT 3
/* GUC variables */ /* GUC variables */
...@@ -435,6 +435,13 @@ struct Tuplesortstate ...@@ -435,6 +435,13 @@ struct Tuplesortstate
* a lot better than what we were doing before 7.3. * a lot better than what we were doing before 7.3.
*/ */
/* When using this macro, beware of double evaluation of len */
#define LogicalTapeReadExact(tapeset, tapenum, ptr, len) \
do { \
if (LogicalTapeRead(tapeset, tapenum, ptr, len) != (size_t) (len)) \
elog(ERROR, "unexpected end of data"); \
} while(0)
static Tuplesortstate *tuplesort_begin_common(int workMem, bool randomAccess); static Tuplesortstate *tuplesort_begin_common(int workMem, bool randomAccess);
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple); static void puttuple_common(Tuplesortstate *state, SortTuple *tuple);
...@@ -2576,8 +2583,8 @@ getlen(Tuplesortstate *state, int tapenum, bool eofOK) ...@@ -2576,8 +2583,8 @@ getlen(Tuplesortstate *state, int tapenum, bool eofOK)
{ {
unsigned int len; unsigned int len;
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &len, if (LogicalTapeRead(state->tapeset, tapenum,
sizeof(len)) != sizeof(len)) &len, sizeof(len)) != sizeof(len))
elog(ERROR, "unexpected end of tape"); elog(ERROR, "unexpected end of tape");
if (len == 0 && !eofOK) if (len == 0 && !eofOK)
elog(ERROR, "unexpected end of data"); elog(ERROR, "unexpected end of data");
...@@ -2810,14 +2817,11 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup, ...@@ -2810,14 +2817,11 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup,
USEMEM(state, GetMemoryChunkSpace(tuple)); USEMEM(state, GetMemoryChunkSpace(tuple));
/* read in the tuple proper */ /* read in the tuple proper */
tuple->t_len = tuplen; tuple->t_len = tuplen;
if (LogicalTapeRead(state->tapeset, tapenum, LogicalTapeReadExact(state->tapeset, tapenum,
(void *) tupbody, tupbody, tupbodylen);
tupbodylen) != (size_t) tupbodylen)
elog(ERROR, "unexpected end of data");
if (state->randomAccess) /* need trailing length word? */ if (state->randomAccess) /* need trailing length word? */
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, LogicalTapeReadExact(state->tapeset, tapenum,
sizeof(tuplen)) != sizeof(tuplen)) &tuplen, sizeof(tuplen));
elog(ERROR, "unexpected end of data");
stup->tuple = (void *) tuple; stup->tuple = (void *) tuple;
/* set up first-column key value */ /* set up first-column key value */
htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET; htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET;
...@@ -2998,20 +3002,16 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup, ...@@ -2998,20 +3002,16 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup,
/* Reconstruct the HeapTupleData header */ /* Reconstruct the HeapTupleData header */
tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
tuple->t_len = t_len; tuple->t_len = t_len;
if (LogicalTapeRead(state->tapeset, tapenum, LogicalTapeReadExact(state->tapeset, tapenum,
&tuple->t_self, &tuple->t_self, sizeof(ItemPointerData));
sizeof(ItemPointerData)) != sizeof(ItemPointerData))
elog(ERROR, "unexpected end of data");
/* We don't currently bother to reconstruct t_tableOid */ /* We don't currently bother to reconstruct t_tableOid */
tuple->t_tableOid = InvalidOid; tuple->t_tableOid = InvalidOid;
/* Read in the tuple body */ /* Read in the tuple body */
if (LogicalTapeRead(state->tapeset, tapenum, LogicalTapeReadExact(state->tapeset, tapenum,
tuple->t_data, tuple->t_len) != tuple->t_len) tuple->t_data, tuple->t_len);
elog(ERROR, "unexpected end of data");
if (state->randomAccess) /* need trailing length word? */ if (state->randomAccess) /* need trailing length word? */
if (LogicalTapeRead(state->tapeset, tapenum, &tuplen, LogicalTapeReadExact(state->tapeset, tapenum,
sizeof(tuplen)) != sizeof(tuplen)) &tuplen, sizeof(tuplen));
elog(ERROR, "unexpected end of data");
stup->tuple = (void *) tuple; stup->tuple = (void *) tuple;
/* set up first-column key value, if it's a simple column */ /* set up first-column key value, if it's a simple column */
if (state->indexInfo->ii_KeyAttrNumbers[0] != 0) if (state->indexInfo->ii_KeyAttrNumbers[0] != 0)
...@@ -3243,13 +3243,11 @@ readtup_index(Tuplesortstate *state, SortTuple *stup, ...@@ -3243,13 +3243,11 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
IndexTuple tuple = (IndexTuple) palloc(tuplen); IndexTuple tuple = (IndexTuple) palloc(tuplen);
USEMEM(state, GetMemoryChunkSpace(tuple)); USEMEM(state, GetMemoryChunkSpace(tuple));
if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple, LogicalTapeReadExact(state->tapeset, tapenum,
tuplen) != tuplen) tuple, tuplen);
elog(ERROR, "unexpected end of data");
if (state->randomAccess) /* need trailing length word? */ if (state->randomAccess) /* need trailing length word? */
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, LogicalTapeReadExact(state->tapeset, tapenum,
sizeof(tuplen)) != sizeof(tuplen)) &tuplen, sizeof(tuplen));
elog(ERROR, "unexpected end of data");
stup->tuple = (void *) tuple; stup->tuple = (void *) tuple;
/* set up first-column key value */ /* set up first-column key value */
stup->datum1 = index_getattr(tuple, stup->datum1 = index_getattr(tuple,
...@@ -3357,9 +3355,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup, ...@@ -3357,9 +3355,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup,
else if (state->datumTypeByVal) else if (state->datumTypeByVal)
{ {
Assert(tuplen == sizeof(Datum)); Assert(tuplen == sizeof(Datum));
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &stup->datum1, LogicalTapeReadExact(state->tapeset, tapenum,
tuplen) != tuplen) &stup->datum1, tuplen);
elog(ERROR, "unexpected end of data");
stup->isnull1 = false; stup->isnull1 = false;
stup->tuple = NULL; stup->tuple = NULL;
} }
...@@ -3367,9 +3364,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup, ...@@ -3367,9 +3364,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup,
{ {
void *raddr = palloc(tuplen); void *raddr = palloc(tuplen);
if (LogicalTapeRead(state->tapeset, tapenum, raddr, LogicalTapeReadExact(state->tapeset, tapenum,
tuplen) != tuplen) raddr, tuplen);
elog(ERROR, "unexpected end of data");
stup->datum1 = PointerGetDatum(raddr); stup->datum1 = PointerGetDatum(raddr);
stup->isnull1 = false; stup->isnull1 = false;
stup->tuple = raddr; stup->tuple = raddr;
...@@ -3377,9 +3373,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup, ...@@ -3377,9 +3373,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup,
} }
if (state->randomAccess) /* need trailing length word? */ if (state->randomAccess) /* need trailing length word? */
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, LogicalTapeReadExact(state->tapeset, tapenum,
sizeof(tuplen)) != sizeof(tuplen)) &tuplen, sizeof(tuplen));
elog(ERROR, "unexpected end of data");
} }
static void static void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment