8000 Refactor gin_extact functions · postgrespro/pgsphere@c289a46 · GitHub
[go: up one dir, main page]

Skip to content

Commit c289a46

Browse files
df7cbmsdemlei
authored andcommitted
Refactor gin_extact functions
1 parent 43d5734 commit c289a46

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

moc.c

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,13 +1020,11 @@ smoc_spoly(PG_FUNCTION_ARGS)
10201020

10211021
/* GIN index ***********************************/
10221022

1023-
Datum
1024-
smoc_gin_extract_value(PG_FUNCTION_ARGS)
1023+
static Datum
1024+
smoc_gin_extract_internal(Smoc *moc_a, int32 *nkeys, int gin_order)
10251025
{
1026-
Smoc* moc_a = (Smoc *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
10271026
char* moc_a_base = MOC_BASE(moc_a);
10281027
int32 moc_a_end = VARSIZE(moc_a) - VARHDRSZ;
1029-
int32* nkeys = (int32 *) PG_GETARG_POINTER(1);
10301028
int32 nalloc = 4;
10311029
Datum* keys = palloc(nalloc * sizeof(Datum));
10321030

@@ -1036,7 +1034,7 @@ smoc_gin_extract_value(PG_FUNCTION_ARGS)
10361034
{
10371035
moc_interval *x = MOC_INTERVAL(moc_a_base, a);
10381036

1039-
int shift = 2 * (HEALPIX_MAX_ORDER - MOC_GIN_ORDER); // degrade to MOC_GIN_ORDER
1037+
int shift = 2 * (HEALPIX_MAX_ORDER - gin_order); // degrade to MOC_GIN_ORDER
10401038
int32 first = (x->first >> shift); // set low bits to zero
10411039
hpint64 low_bits_one = (1L << shift) - 1;
10421040
int32 second = ((x->second + low_bits_one) >> shift); // round low bits up
@@ -1060,49 +1058,27 @@ smoc_gin_extract_value(PG_FUNCTION_ARGS)
10601058
PG_RETURN_POINTER(keys);
10611059
}
10621060

1061+
Datum
1062+
smoc_gin_extract_value(PG_FUNCTION_ARGS)
1063+
{
1064+
Smoc* moc_a = (Smoc *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1065+
int32* nkeys = (int32 *) PG_GETARG_POINTER(1);
1066+
1067+
PG_RETURN_DATUM(smoc_gin_extract_internal(moc_a, nkeys, MOC_GIN_ORDER));
1068+
}
1069+
10631070
Datum
10641071
smoc_gin_extract_query(PG_FUNCTION_ARGS)
10651072
{
10661073
Smoc* moc_a = (Smoc *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1067-
char* moc_a_base = MOC_BASE(moc_a);
1068-
int32 moc_a_end = VARSIZE(moc_a) - VARHDRSZ;
10691074
int32* nkeys = (int32 *) PG_GETARG_POINTER(1);
10701075
StrategyNumber st = PG_GETARG_UINT16(2);
10711076
int32* searchmode = (int32 *) PG_GETARG_POINTER(6);
1072-
int32 nalloc = 4;
1073-
Datum* keys = palloc(nalloc * sizeof(Datum));
1074-
1075-
*nkeys = 0;
10761077

10771078
if (st == MOC_GIN_STRATEGY_SUBSET)
10781079
*searchmode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
10791080

1080-
for (int32 a = moc_a->data_begin; a < moc_a_end; a = next_interval(a))
1081-
{
1082-
moc_interval *x = MOC_INTERVAL(moc_a_base, a);
1083-
1084-
int shift = 2 * (HEALPIX_MAX_ORDER - MOC_GIN_ORDER); // degrade to MOC_GIN_ORDER
1085-
int32 first = (x->first >> shift); // set low bits to zero
1086-
hpint64 low_bits_one = (1L << shift) - 1;
1087-
int32 second = ((x->second + low_bits_one) >> shift); // round low bits up
1088-
Assert(shift > 32); // internal GIN datatype isn't 64 bits
1089-
1090-
// split interval into individual pixels of order MOC_GIN_ORDER
1091-
for (int32 p = first; p < second; p++)
1092-
{
1093-
if (*nkeys > 0 && keys[*nkeys - 1] == p) // has (larger) pixel already been added?
1094-
continue;
1095-
if (*nkeys >= nalloc)
1096-
{
1097-
nalloc *= 2;
1098-
Assert(nalloc < 1000000);
1099-
keys = repalloc(keys, nalloc * sizeof(Datum));
1100-
}
1101-
keys[(*nkeys)++] = Int32GetDatum(p);
1102-
}
1103-
}
1104-
1105-
PG_RETURN_POINTER(keys);
1081+
PG_RETURN_DATUM(smoc_gin_extract_internal(moc_a, nkeys, MOC_GIN_ORDER));
11061082
}
11071083

11081084
Datum

0 commit comments

Comments
 (0)
0