Skip to content

Commit 26a7b48

Browse files
committed
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.
1 parent 3ba11d3 commit 26a7b48

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

src/backend/utils/sort/tuplesort.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@
120120

121121

122122
/* sort-type codes for sort__start probes */
123-
#define HEAP_SORT 0
124-
#define INDEX_SORT 1
125-
#define DATUM_SORT 2
123+
#define HEAP_SORT 0
124+
#define INDEX_SORT 1
125+
#define DATUM_SORT 2
126126
#define CLUSTER_SORT 3
127127

128128
/* GUC variables */
@@ -435,6 +435,13 @@ struct Tuplesortstate
435435
* a lot better than what we were doing before 7.3.
436436
*/
437437

438+
/* When using this macro, beware of double evaluation of len */
439+
#define LogicalTapeReadExact(tapeset, tapenum, ptr, len) \
440+
do { \
441+
if (LogicalTapeRead(tapeset, tapenum, ptr, len) != (size_t) (len)) \
442+
elog(ERROR, "unexpected end of data"); \
443+
} while(0)
444+
438445

439446
static Tuplesortstate *tuplesort_begin_common(int workMem, bool randomAccess);
440447
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple);
@@ -2576,8 +2583,8 @@ getlen(Tuplesortstate *state, int tapenum, bool eofOK)
25762583
{
25772584
unsigned int len;
25782585

2579-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &len,
2580-
sizeof(len)) != sizeof(len))
2586+
if (LogicalTapeRead(state->tapeset, tapenum,
2587+
&len, sizeof(len)) != sizeof(len))
25812588
elog(ERROR, "unexpected end of tape");
25822589
if (len == 0 && !eofOK)
25832590
elog(ERROR, "unexpected end of data");
@@ -2810,14 +2817,11 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup,
28102817
USEMEM(state, GetMemoryChunkSpace(tuple));
28112818
/* read in the tuple proper */
28122819
tuple->t_len = tuplen;
2813-
if (LogicalTapeRead(state->tapeset, tapenum,
2814-
(void *) tupbody,
2815-
tupbodylen) != (size_t) tupbodylen)
2816-
elog(ERROR, "unexpected end of data");
2820+
LogicalTapeReadExact(state->tapeset, tapenum,
2821+
tupbody, tupbodylen);
28172822
if (state->randomAccess) /* need trailing length word? */
2818-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
2819-
sizeof(tuplen)) != sizeof(tuplen))
2820-
elog(ERROR, "unexpected end of data");
2823+
LogicalTapeReadExact(state->tapeset, tapenum,
2824+
&tuplen, sizeof(tuplen));
28212825
stup->tuple = (void *) tuple;
28222826
/* set up first-column key value */
28232827
htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET;
@@ -2998,20 +3002,16 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup,
29983002
/* Reconstruct the HeapTupleData header */
29993003
tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
30003004
tuple->t_len = t_len;
3001-
if (LogicalTapeRead(state->tapeset, tapenum,
3002-
&tuple->t_self,
3003-
sizeof(ItemPointerData)) != sizeof(ItemPointerData))
3004-
elog(ERROR, "unexpected end of data");
3005+
LogicalTapeReadExact(state->tapeset, tapenum,
3006+
&tuple->t_self, sizeof(ItemPointerData));
30053007
/* We don't currently bother to reconstruct t_tableOid */
30063008
tuple->t_tableOid = InvalidOid;
30073009
/* Read in the tuple body */
3008-
if (LogicalTapeRead(state->tapeset, tapenum,
3009-
tuple->t_data, tuple->t_len) != tuple->t_len)
3010-
elog(ERROR, "unexpected end of data");
3010+
LogicalTapeReadExact(state->tapeset, tapenum,
3011+
tuple->t_data, tuple->t_len);
30113012
if (state->randomAccess) /* need trailing length word? */
3012-
if (LogicalTapeRead(state->tapeset, tapenum, &tuplen,
3013-
sizeof(tuplen)) != sizeof(tuplen))
3014-
elog(ERROR, "unexpected end of data");
3013+
LogicalTapeReadExact(state->tapeset, tapenum,
3014+
&tuplen, sizeof(tuplen));
30153015
stup->tuple = (void *) tuple;
30163016
/* set up first-column key value, if it's a simple column */
30173017
if (state->indexInfo->ii_KeyAttrNumbers[0] != 0)
@@ -3243,13 +3243,11 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
32433243
IndexTuple tuple = (IndexTuple) palloc(tuplen);
32443244

32453245
USEMEM(state, GetMemoryChunkSpace(tuple));
3246-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple,
3247-
tuplen) != tuplen)
3248-
elog(ERROR, "unexpected end of data");
3246+
LogicalTapeReadExact(state->tapeset, tapenum,
3247+
tuple, tuplen);
32493248
if (state->randomAccess) /* need trailing length word? */
3250-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
3251-
sizeof(tuplen)) != sizeof(tuplen))
3252-
elog(ERROR, "unexpected end of data");
3249+
LogicalTapeReadExact(state->tapeset, tapenum,
3250+
&tuplen, sizeof(tuplen));
32533251
stup->tuple = (void *) tuple;
32543252
/* set up first-column key value */
32553253
stup->datum1 = index_getattr(tuple,
@@ -3357,29 +3355,26 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup,
33573355
else if (state->datumTypeByVal)
33583356
{
33593357
Assert(tuplen == sizeof(Datum));
3360-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &stup->datum1,
3361-
tuplen) != tuplen)
3362-
elog(ERROR, "unexpected end of data");
3358+
LogicalTapeReadExact(state->tapeset, tapenum,
3359+
&stup->datum1, tuplen);
33633360
stup->isnull1 = false;
33643361
stup->tuple = NULL;
33653362
}
33663363
else
33673364
{
33683365
void *raddr = palloc(tuplen);
33693366

3370-
if (LogicalTapeRead(state->tapeset, tapenum, raddr,
3371-
tuplen) != tuplen)
3372-
elog(ERROR, "unexpected end of data");
3367+
LogicalTapeReadExact(state->tapeset, tapenum,
3368+
raddr, tuplen);
33733369
stup->datum1 = PointerGetDatum(raddr);
33743370
stup->isnull1 = false;
33753371
stup->tuple = raddr;
33763372
USEMEM(state, GetMemoryChunkSpace(raddr));
33773373
}
33783374

33793375
if (state->randomAccess) /* need trailing length word? */
3380-
if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
3381-
sizeof(tuplen)) != sizeof(tuplen))
3382-
elog(ERROR, "unexpected end of data");
3376+
LogicalTapeReadExact(state->tapeset, tapenum,
3377+
&tuplen, sizeof(tuplen));
33833378
}
33843379

33853380
static void

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy