Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit919c74f

Browse files
author
Maxim Orlov
committed
Get rid of copying code from pg core.
1 parentdb8b9d6 commit919c74f

File tree

2 files changed

+96
-334
lines changed

2 files changed

+96
-334
lines changed

‎src/rumsort.c

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
#include"rum.h"/* RumItem */
2929

3030
#ifPG_VERSION_NUM >=160000
31-
#include"tuplesort16.c"
32-
#undef TRACE_SORT
31+
/*
32+
* After allocating a public interface for Tuplesortstate, no need to include
33+
* source code from pg-core.
34+
*/
3335
#elifPG_VERSION_NUM >=150000
3436
#include"tuplesort15.c"
3537
#elifPG_VERSION_NUM >=140000
@@ -46,6 +48,26 @@
4648
#include"tuplesort96.c"
4749
#endif
4850

51+
/*
52+
* In case of using custom compare function we should store function pointer in
53+
* sort stare in order to use it later.
54+
*/
55+
56+
#ifPG_VERSION_NUM >=160000
57+
/*
58+
* After allocating a public interface for Tuplesortstate we may use
59+
* TuplesortPublic->arg filed to store pointer to the compare function.
60+
*/
61+
62+
/* GUC variables */
63+
#ifdefTRACE_SORT
64+
externPGDLLIMPORTbooltrace_sort;
65+
#endif
66+
67+
/* All memory management should be inside Tuplesortstate module. */
68+
#defineUSEMEM(state,amt)do {} while(0)
69+
70+
#else/* PG_VERSION_NUM >= 160000 */
4971
/*
5072
* We need extra field in a state structure but we should not modify struct
5173
* RumTuplesortstate which is inherited from Tuplesortstate core function.
@@ -55,6 +77,7 @@ typedef struct RumTuplesortstateExt
5577
RumTuplesortstatets;
5678
FmgrInfo*cmp;
5779
}RumTuplesortstateExt;
80+
#endif/* PG_VERSION_NUM < 160000 */
5881

5982
staticintcomparetup_rum(constSortTuple*a,constSortTuple*b,
6083
RumTuplesortstate*state,boolcompareItemPointer);
@@ -70,12 +93,18 @@ static void copytup_rumitem(RumTuplesortstate *state, SortTuple *stup,
7093
staticvoid*rum_tuplesort_getrum_internal(RumTuplesortstate*state,
7194
boolforward,bool*should_free);
7295

96+
/*
97+
* Tuplesortstate handling should be done through this macro.
98+
*/
7399
#ifPG_VERSION_NUM >=160000
74100
#defineTSS_GET(state)TuplesortstateGetPublic((state))
75101
#else
76102
#defineTSS_GET(state)(state)
77103
#endif
78104

105+
/*
106+
* Logical tape handling should be done through this macro.
107+
*/
79108
#ifPG_VERSION_NUM >=150000
80109
#defineLT_TYPE LogicalTape *
81110
#defineLT_ARG tape
@@ -86,6 +115,25 @@ static void *rum_tuplesort_getrum_internal(RumTuplesortstate *state,
86115
#defineTAPE(state,LT_ARG) state->tapeset, LT_ARG
87116
#endif
88117

118+
/*
119+
* Just for convenience and uniformity.
120+
*/
121+
#ifPG_VERSION_NUM >=110000
122+
#definetuplesort_begin_common(x,y) tuplesort_begin_common((x), NULL, (y))
123+
#endif
124+
125+
/*
126+
* Trace log wrapper.
127+
*/
128+
#ifdefTRACE_SORT
129+
#defineLOG_SORT(...)\
130+
if (trace_sort)\
131+
ereport(LOG, errmsg_internal(__VA_ARGS__))
132+
#else
133+
#defineLOG_SORT(...)\
134+
{}
135+
#endif
136+
89137
staticinlineint
90138
compare_rum_itempointer(ItemPointerDatap1,ItemPointerDatap2)
91139
{
@@ -156,19 +204,29 @@ comparetup_rum_false(const SortTuple *a, const SortTuple *b,
156204
returncomparetup_rum(a,b,state, false);
157205
}
158206

207+
staticinlineFmgrInfo*
208+
comparetup_rumitem_custom_fun(RumTuplesortstate*state)
209+
{
210+
#ifPG_VERSION_NUM >=160000
211+
return (FmgrInfo*)TSS_GET(state)->arg;
212+
#else
213+
return ((RumTuplesortstateExt*)state)->cmp;
214+
#endif
215+
}
216+
159217
staticint
160218
comparetup_rumitem(constSortTuple*a,constSortTuple*b,
161219
RumTuplesortstate*state)
162220
{
163-
RumItem*i1,
164-
*i2;
165-
FmgrInfo*cmp;
221+
RumItem*i1,
222+
*i2;
223+
FmgrInfo*cmp;
166224

167225
/* Extract RumItem from RumScanItem */
168226
i1= (RumItem*)a->tuple;
169227
i2= (RumItem*)b->tuple;
170228

171-
cmp=((RumTuplesortstateExt*)state)->cmp;
229+
cmp=comparetup_rumitem_custom_fun(state);
172230
if (cmp!=NULL)
173231
{
174232
if (i1->addInfoIsNull||i2->addInfoIsNull)
@@ -242,17 +300,21 @@ writetup_rum_internal(RumTuplesortstate *state, LT_TYPE LT_ARG,
242300
void*item=stup->tuple;
243301
size_tsize=rum_item_size(state);
244302
unsignedintwrittenlen=size+sizeof(unsignedint);
303+
boolrandomAccess;
245304

246305
LogicalTapeWrite(TAPE(state,LT_ARG),
247306
(void*)&writtenlen,sizeof(writtenlen));
248307
LogicalTapeWrite(TAPE(state,LT_ARG),
249308
(void*)item,size);
250-
#ifPG_VERSION_NUM >=150000
251-
if (TSS_GET(state)->sortopt&TUPLESORT_RANDOMACCESS)/* need trailing
252-
* length word? */
253-
#else
254-
if (TSS_GET(state)->randomAccess)/* need trailing length word? */
255-
#endif
309+
310+
randomAccess=
311+
#ifPG_VERSION_NUM >=150000
312+
(TSS_GET(state)->sortopt&TUPLESORT_RANDOMACCESS)!=0;
313+
#else
314+
TSS_GET(state)->randomAccess;
315+
#endif
316+
317+
if (randomAccess)
256318
LogicalTapeWrite(TAPE(TSS_GET(state),LT_ARG), (void*)&writtenlen,
257319
sizeof(writtenlen));
258320
}
@@ -280,6 +342,7 @@ readtup_rum_internal(RumTuplesortstate *state, SortTuple *stup,
280342
Assert(tuplen==size);
281343

282344
USEMEM(state,GetMemoryChunkSpace(item));
345+
283346
#ifPG_VERSION_NUM >=150000
284347
LogicalTapeReadExact(LT_ARG,item,size);
285348
#else
@@ -316,10 +379,6 @@ readtup_rumitem(RumTuplesortstate *state, SortTuple *stup, LT_TYPE LT_ARG,
316379
readtup_rum_internal(state,stup,LT_ARG,len, true);
317380
}
318381

319-
#ifPG_VERSION_NUM >=110000
320-
#definetuplesort_begin_common(x,y) tuplesort_begin_common((x), NULL, (y))
321-
#endif
322-
323382
RumTuplesortstate*
324383
rum_tuplesort_begin_rum(intworkMem,intnKeys,boolrandomAccess,
325384
boolcompareItemPointer)
@@ -336,12 +395,8 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
336395

337396
oldcontext=MemoryContextSwitchTo(TSS_GET(state)->sortcontext);
338397

339-
#ifdefTRACE_SORT
340-
if (trace_sort)
341-
elog(LOG,
342-
"begin rum sort: nKeys = %d, workMem = %d, randomAccess = %c",
398+
LOG_SORT("begin rum sort: nKeys = %d, workMem = %d, randomAccess = %c",
343399
nKeys,workMem,randomAccess ?'t' :'f');
344-
#endif
345400

346401
TSS_GET(state)->nKeys=nKeys;
347402
TSS_GET(state)->comparetup=compareItemPointer ?comparetup_rum_true :
@@ -357,6 +412,23 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
357412
RumTuplesortstate*
358413
rum_tuplesort_begin_rumitem(intworkMem,FmgrInfo*cmp)
359414
{
415+
#ifPG_VERSION_NUM >=160000
416+
RumTuplesortstate*state=tuplesort_begin_common(workMem, false);
417+
MemoryContextoldcontext;
418+
419+
oldcontext=MemoryContextSwitchTo(TSS_GET(state)->sortcontext);
420+
421+
LOG_SORT("begin rumitem sort: workMem = %d",workMem);
422+
423+
TSS_GET(state)->comparetup=comparetup_rumitem;
424+
TSS_GET(state)->writetup=writetup_rumitem;
425+
TSS_GET(state)->readtup=readtup_rumitem;
426+
TSS_GET(state)->arg=cmp;
427+
428+
MemoryContextSwitchTo(oldcontext);
429+
430+
returnstate;
431+
#else
360432
RumTuplesortstate*state=tuplesort_begin_common(workMem, false);
361433
RumTuplesortstateExt*rs;
362434
MemoryContextoldcontext;
@@ -366,11 +438,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
366438
/* Allocate extended state in the same context as state */
367439
rs=palloc(sizeof(*rs));
368440

369-
#ifdefTRACE_SORT
370-
if (trace_sort)
371-
elog(LOG,
372-
"begin rumitem sort: workMem = %d",workMem);
373-
#endif
441+
LOG_SORT("begin rumitem sort: workMem = %d",workMem);
374442

375443
rs->cmp=cmp;
376444
TSS_GET(state)->comparetup=comparetup_rumitem;
@@ -383,6 +451,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
383451
MemoryContextSwitchTo(oldcontext);
384452

385453
return (RumTuplesortstate*)rs;
454+
#endif
386455
}
387456

388457
/*
@@ -397,7 +466,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
397466
void
398467
rum_tuplesort_end(RumTuplesortstate*state)
399468
{
400-
#ifPG_VERSION_NUM >=130000
469+
#ifPG_VERSION_NUM<160000&&PG_VERSION_NUM>=130000
401470
tuplesort_free(state);
402471
#else
403472
tuplesort_end(state);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp