Skip to content

Commit 5719f28

Browse files
author
Maxim Orlov
committed
Refactoring to be pg16 ready.
1 parent 1ffa817 commit 5719f28

File tree

1 file changed

+115
-69
lines changed

1 file changed

+115
-69
lines changed

src/rumsort.c

Lines changed: 115 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
#endif
4545

4646
/*
47-
* We need extra field in a state structure but we should not modify struct RumTuplesortstate
48-
* which is inherited from Tuplesortstate core function.
47+
* We need extra field in a state structure but we should not modify struct
48+
* RumTuplesortstate which is inherited from Tuplesortstate core function.
4949
*/
5050
typedef struct RumTuplesortstateExt
5151
{
@@ -55,18 +55,28 @@ typedef struct RumTuplesortstateExt
5555

5656
static int compare_rum_itempointer(ItemPointerData p1, ItemPointerData p2);
5757
static int comparetup_rum(const SortTuple *a, const SortTuple *b,
58-
RumTuplesortstate * state, bool compareItemPointer);
58+
RumTuplesortstate *state, bool compareItemPointer);
5959
static int comparetup_rum_true(const SortTuple *a, const SortTuple *b,
60-
RumTuplesortstate * state);
60+
RumTuplesortstate *state);
6161
static int comparetup_rum_false(const SortTuple *a, const SortTuple *b,
62-
RumTuplesortstate * state);
62+
RumTuplesortstate *state);
6363
static int comparetup_rumitem(const SortTuple *a, const SortTuple *b,
64-
RumTuplesortstate * state);
65-
static void copytup_rum(RumTuplesortstate * state, SortTuple *stup, void *tup);
66-
static void copytup_rumitem(RumTuplesortstate * state, SortTuple *stup, void *tup);
67-
static void *rum_tuplesort_getrum_internal(RumTuplesortstate * state, bool forward, bool *should_free);
64+
RumTuplesortstate *state);
65+
static void copytup_rum(RumTuplesortstate *state, SortTuple *stup, void *tup);
66+
static void copytup_rumitem(RumTuplesortstate *state, SortTuple *stup,
67+
void *tup);
68+
static void *rum_tuplesort_getrum_internal(RumTuplesortstate *state,
69+
bool forward, bool *should_free);
70+
71+
#if PG_VERSION_NUM >= 160000
72+
# define TSS_GET(state) \
73+
TuplesortstateGetPublic((state))
74+
#else
75+
# define TSS_GET(state) \
76+
(state)
77+
#endif
6878

69-
static int
79+
static inline int
7080
compare_rum_itempointer(ItemPointerData p1, ItemPointerData p2)
7181
{
7282
if (p1.ip_blkid.bi_hi < p2.ip_blkid.bi_hi)
@@ -88,7 +98,8 @@ compare_rum_itempointer(ItemPointerData p1, ItemPointerData p2)
8898
}
8999

90100
static int
91-
comparetup_rum(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state, bool compareItemPointer)
101+
comparetup_rum(const SortTuple *a, const SortTuple *b,
102+
RumTuplesortstate *state, bool compareItemPointer)
92103
{
93104
RumSortItem *i1,
94105
*i2;
@@ -104,7 +115,7 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state
104115
i1 = (RumSortItem *) a->tuple;
105116
i2 = (RumSortItem *) b->tuple;
106117

107-
for (i = 1; i < state->nKeys; i++)
118+
for (i = 1; i < TSS_GET(state)->nKeys; i++)
108119
{
109120
if (i1->data[i] < i2->data[i])
110121
return -1;
@@ -122,19 +133,22 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state
122133
}
123134

124135
static int
125-
comparetup_rum_true(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state)
136+
comparetup_rum_true(const SortTuple *a, const SortTuple *b,
137+
RumTuplesortstate *state)
126138
{
127139
return comparetup_rum(a, b, state, true);
128140
}
129141

130142
static int
131-
comparetup_rum_false(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state)
143+
comparetup_rum_false(const SortTuple *a, const SortTuple *b,
144+
RumTuplesortstate *state)
132145
{
133146
return comparetup_rum(a, b, state, false);
134147
}
135148

136149
static int
137-
comparetup_rumitem(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state)
150+
comparetup_rumitem(const SortTuple *a, const SortTuple *b,
151+
RumTuplesortstate *state)
138152
{
139153
RumItem *i1,
140154
*i2;
@@ -171,23 +185,24 @@ comparetup_rumitem(const SortTuple *a, const SortTuple *b, RumTuplesortstate * s
171185
}
172186

173187
static void
174-
copytup_rum(RumTuplesortstate * state, SortTuple *stup, void *tup)
188+
copytup_rum(RumTuplesortstate *state, SortTuple *stup, void *tup)
175189
{
176190
RumSortItem *item = (RumSortItem *) tup;
191+
int nKeys = TSS_GET(state)->nKeys;
177192

178-
stup->datum1 = Float8GetDatum(state->nKeys > 0 ? item->data[0] : 0);
193+
stup->datum1 = Float8GetDatum(nKeys > 0 ? item->data[0] : 0);
179194
stup->isnull1 = false;
180195
stup->tuple = tup;
181-
USEMEM(state, GetMemoryChunkSpace(tup));
196+
//USEMEM(state, GetMemoryChunkSpace(tup));
182197
}
183198

184199
static void
185-
copytup_rumitem(RumTuplesortstate * state, SortTuple *stup, void *tup)
200+
copytup_rumitem(RumTuplesortstate *state, SortTuple *stup, void *tup)
186201
{
187202
stup->isnull1 = true;
188203
stup->tuple = palloc(sizeof(RumScanItem));
189204
memcpy(stup->tuple, tup, sizeof(RumScanItem));
190-
USEMEM(state, GetMemoryChunkSpace(stup->tuple));
205+
//USEMEM(state, GetMemoryChunkSpace(stup->tuple));
191206
}
192207

193208
#if PG_VERSION_NUM >= 150000
@@ -200,19 +215,27 @@ copytup_rumitem(RumTuplesortstate * state, SortTuple *stup, void *tup)
200215
#define TAPE(state, LT_ARG) state->tapeset, LT_ARG
201216
#endif
202217

218+
static void readtup_rum(RumTuplesortstate *state, SortTuple *stup,
219+
LT_TYPE LT_ARG, unsigned int len);
220+
221+
static void readtup_rumitem(RumTuplesortstate *state, SortTuple *stup,
222+
LT_TYPE LT_ARG, unsigned int len);
223+
203224
static Size
204-
rum_item_size(RumTuplesortstate * state)
225+
rum_item_size(RumTuplesortstate *state)
205226
{
206-
if (state->copytup == copytup_rum)
207-
return RumSortItemSize(state->nKeys);
208-
else if (state->copytup == copytup_rumitem)
227+
if (TSS_GET(state)->readtup == readtup_rum)
228+
return RumSortItemSize(TSS_GET(state)->nKeys);
229+
else if (TSS_GET(state)->readtup == readtup_rumitem)
209230
return sizeof(RumScanItem);
210231
else
211232
elog (FATAL, "Unknown RUM state");
233+
return 0; /* Silence compiler */
212234
}
213235

214236
static void
215-
writetup_rum_internal(RumTuplesortstate * state, LT_TYPE LT_ARG, SortTuple *stup)
237+
writetup_rum_internal(RumTuplesortstate *state, LT_TYPE LT_ARG,
238+
SortTuple *stup)
216239
{
217240
void *item = stup->tuple;
218241
size_t size = rum_item_size(state);
@@ -223,28 +246,29 @@ writetup_rum_internal(RumTuplesortstate * state, LT_TYPE LT_ARG, SortTuple *stup
223246
LogicalTapeWrite(TAPE(state, LT_ARG),
224247
(void *) item, size);
225248
#if PG_VERSION_NUM >= 150000
226-
if (state->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */
249+
if (TSS_GET(state)->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing
250+
* length word? */
227251
#else
228-
if (state->randomAccess) /* need trailing length word? */
252+
if (TSS_GET(state)->randomAccess) /* need trailing length word? */
229253
#endif
230-
LogicalTapeWrite(TAPE(state, LT_ARG),
231-
(void *) &writtenlen, sizeof(writtenlen));
254+
LogicalTapeWrite(TAPE(TSS_GET(state), LT_ARG), (void *) &writtenlen,
255+
sizeof(writtenlen));
232256
}
233257

234258
static void
235-
writetup_rum(RumTuplesortstate * state, LT_TYPE LT_ARG, SortTuple *stup)
259+
writetup_rum(RumTuplesortstate *state, LT_TYPE LT_ARG, SortTuple *stup)
236260
{
237261
writetup_rum_internal(state, LT_ARG, stup);
238262
}
239263

240264
static void
241-
writetup_rumitem(RumTuplesortstate * state, LT_TYPE LT_ARG, SortTuple *stup)
265+
writetup_rumitem(RumTuplesortstate *state, LT_TYPE LT_ARG, SortTuple *stup)
242266
{
243267
writetup_rum_internal(state, LT_ARG, stup);
244268
}
245269

246270
static void
247-
readtup_rum_internal(RumTuplesortstate * state, SortTuple *stup,
271+
readtup_rum_internal(RumTuplesortstate *state, SortTuple *stup,
248272
LT_TYPE LT_ARG, unsigned int len, bool is_item)
249273
{
250274
unsigned int tuplen = len - sizeof(unsigned int);
@@ -253,36 +277,39 @@ readtup_rum_internal(RumTuplesortstate * state, SortTuple *stup,
253277

254278
Assert(tuplen == size);
255279

256-
USEMEM(state, GetMemoryChunkSpace(item));
280+
//USEMEM(state, GetMemoryChunkSpace(item));
257281
#if PG_VERSION_NUM >= 150000
258282
LogicalTapeReadExact(LT_ARG, item, size);
259283
#else
260-
LogicalTapeReadExact(state->tapeset, LT_ARG, item, size);
284+
LogicalTapeReadExact(TSS_GET(state)->tapeset, LT_ARG, item, size);
261285
#endif
262286
stup->tuple = item;
263287
stup->isnull1 = is_item;
264288

265289
if (!is_item)
266-
stup->datum1 = Float8GetDatum(state->nKeys > 0 ? ((RumSortItem *) item)->data[0] : 0);
290+
stup->datum1 = Float8GetDatum(TSS_GET(state)->nKeys > 0 ?
291+
((RumSortItem *) item)->data[0] : 0);
267292
#if PG_VERSION_NUM >= 150000
268-
if (state->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */
293+
if (TSS_GET(state)->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing
294+
* length word? */
269295
LogicalTapeReadExact(LT_ARG, &tuplen, sizeof(tuplen));
270296
#else
271-
if (state->randomAccess)
272-
LogicalTapeReadExact(state->tapeset, LT_ARG, &tuplen, sizeof(tuplen));
297+
if (TSS_GET(state)->randomAccess)
298+
LogicalTapeReadExact(TSS_GET(state)->tapeset, LT_ARG, &tuplen,
299+
sizeof(tuplen));
273300
#endif
274301
}
275302

276303
static void
277-
readtup_rum(RumTuplesortstate * state, SortTuple *stup,
278-
LT_TYPE LT_ARG, unsigned int len)
304+
readtup_rum(RumTuplesortstate *state, SortTuple *stup, LT_TYPE LT_ARG,
305+
unsigned int len)
279306
{
280307
readtup_rum_internal(state, stup, LT_ARG, len, false);
281308
}
282309

283310
static void
284-
readtup_rumitem(RumTuplesortstate * state, SortTuple *stup,
285-
LT_TYPE LT_ARG, unsigned int len)
311+
readtup_rumitem(RumTuplesortstate *state, SortTuple *stup, LT_TYPE LT_ARG,
312+
unsigned int len)
286313
{
287314
readtup_rum_internal(state, stup, LT_ARG, len, true);
288315
}
@@ -305,7 +332,7 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
305332
#endif
306333
MemoryContext oldcontext;
307334

308-
oldcontext = MemoryContextSwitchTo(state->sortcontext);
335+
oldcontext = MemoryContextSwitchTo(TSS_GET(state)->sortcontext);
309336

310337
#ifdef TRACE_SORT
311338
if (trace_sort)
@@ -314,12 +341,11 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
314341
nKeys, workMem, randomAccess ? 't' : 'f');
315342
#endif
316343

317-
state->nKeys = nKeys;
318-
319-
state->comparetup = compareItemPointer ? comparetup_rum_true : comparetup_rum_false;
320-
state->copytup = copytup_rum;
321-
state->writetup = writetup_rum;
322-
state->readtup = readtup_rum;
344+
TSS_GET(state)->nKeys = nKeys;
345+
TSS_GET(state)->comparetup = compareItemPointer ? comparetup_rum_true :
346+
comparetup_rum_false;
347+
TSS_GET(state)->writetup = writetup_rum;
348+
TSS_GET(state)->readtup = readtup_rum;
323349

324350
MemoryContextSwitchTo(oldcontext);
325351

@@ -333,7 +359,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
333359
RumTuplesortstateExt *rs;
334360
MemoryContext oldcontext;
335361

336-
oldcontext = MemoryContextSwitchTo(state->sortcontext);
362+
oldcontext = MemoryContextSwitchTo(TSS_GET(state)->sortcontext);
337363

338364
/* Allocate extended state in the same context as state */
339365
rs = palloc(sizeof(*rs));
@@ -345,10 +371,9 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
345371
#endif
346372

347373
rs->cmp = cmp;
348-
state->comparetup = comparetup_rumitem;
349-
state->copytup = copytup_rumitem;
350-
state->writetup = writetup_rumitem;
351-
state->readtup = readtup_rumitem;
374+
TSS_GET(state)->comparetup = comparetup_rumitem;
375+
TSS_GET(state)->writetup = writetup_rumitem;
376+
TSS_GET(state)->readtup = readtup_rumitem;
352377
memcpy(&rs->ts, state, sizeof(RumTuplesortstate));
353378
pfree(state); /* just to be sure *state isn't used anywhere
354379
* else */
@@ -368,7 +393,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
368393
* pointers afterwards!
369394
*/
370395
void
371-
rum_tuplesort_end(RumTuplesortstate * state)
396+
rum_tuplesort_end(RumTuplesortstate *state)
372397
{
373398
#if PG_VERSION_NUM >= 130000
374399
tuplesort_free(state);
@@ -382,39 +407,57 @@ rum_tuplesort_end(RumTuplesortstate * state)
382407
* RumSortItem.
383408
*/
384409
MemoryContext
385-
rum_tuplesort_get_memorycontext(RumTuplesortstate * state)
410+
rum_tuplesort_get_memorycontext(RumTuplesortstate *state)
386411
{
387-
return state->sortcontext;
412+
return TSS_GET(state)->sortcontext;
388413
}
389414

390415
void
391416
rum_tuplesort_putrum(RumTuplesortstate *state, RumSortItem *item)
392417
{
393-
tuplesort_puttupleslot(state, (TupleTableSlot *) item);
418+
MemoryContext oldcontext;
419+
SortTuple stup;
420+
421+
oldcontext = MemoryContextSwitchTo(rum_tuplesort_get_memorycontext(state));
422+
copytup_rum(state, &stup, item);
423+
puttuple_common(state, &stup);
424+
425+
MemoryContextSwitchTo(oldcontext);
394426
}
395427

396428
void
397429
rum_tuplesort_putrumitem(RumTuplesortstate *state, RumScanItem *item)
398430
{
399-
tuplesort_puttupleslot(state, (TupleTableSlot *) item);
431+
MemoryContext oldcontext;
432+
SortTuple stup;
433+
434+
oldcontext = MemoryContextSwitchTo(rum_tuplesort_get_memorycontext(state));
435+
copytup_rumitem(state, &stup, item);
436+
puttuple_common(state, &stup);
437+
438+
MemoryContextSwitchTo(oldcontext);
400439
}
401440

402441
void
403-
rum_tuplesort_performsort(RumTuplesortstate * state)
442+
rum_tuplesort_performsort(RumTuplesortstate *state)
404443
{
405444
tuplesort_performsort(state);
406445
}
407446

408447
/*
409-
* Internal routine to fetch the next index tuple in either forward or back direction.
410-
* Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory context. Caller may not rely on tuple remaining valid after any further manipulation of tuplesort.
448+
* Internal routine to fetch the next index tuple in either forward or back
449+
* direction. Returns NULL if no more tuples. Returned tuple belongs to
450+
* tuplesort memory context. Caller may not rely on tuple remaining valid after
451+
* any further manipulation of tuplesort.
452+
*
411453
* If *should_free is set, the caller must pfree stup.tuple when done with it.
412454
*
413-
* NOTE: in PG 10 and newer tuple is always allocated tuple in tuplesort context and
414-
* should not be freed by caller.
455+
* NOTE: in PG 10 and newer tuple is always allocated tuple in tuplesort context
456+
* and should not be freed by caller.
415457
*/
416458
static void *
417-
rum_tuplesort_getrum_internal(RumTuplesortstate * state, bool forward, bool *should_free)
459+
rum_tuplesort_getrum_internal(RumTuplesortstate *state, bool forward,
460+
bool *should_free)
418461
{
419462
#if PG_VERSION_NUM >= 100000
420463
*should_free = false;
@@ -425,13 +468,16 @@ rum_tuplesort_getrum_internal(RumTuplesortstate * state, bool forward, bool *sho
425468
}
426469

427470
RumSortItem *
428-
rum_tuplesort_getrum(RumTuplesortstate * state, bool forward, bool *should_free)
471+
rum_tuplesort_getrum(RumTuplesortstate *state, bool forward, bool *should_free)
429472
{
430-
return (RumSortItem *) rum_tuplesort_getrum_internal(state, forward, should_free);
473+
return (RumSortItem *) rum_tuplesort_getrum_internal(state, forward,
474+
should_free);
431475
}
432476

433477
RumScanItem *
434-
rum_tuplesort_getrumitem(RumTuplesortstate * state, bool forward, bool *should_free)
478+
rum_tuplesort_getrumitem(RumTuplesortstate *state, bool forward,
479+
bool *should_free)
435480
{
436-
return (RumScanItem *) rum_tuplesort_getrum_internal(state, forward, should_free);
481+
return (RumScanItem *) rum_tuplesort_getrum_internal(state, forward,
482+
should_free);
437483
}

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