Refactor getWeights to write to caller-supplied buffer
This gets rid of the static result buffer. Reviewed-by: Robert Haas Discussion: https://www.postgresql.org/message-id/7f86e06a-98c5-4ce3-8ec9-3885c8de0358@iki.fi
This commit is contained in:
parent
01e51ed780
commit
f822be3962
@ -21,7 +21,8 @@
|
|||||||
#include "utils/array.h"
|
#include "utils/array.h"
|
||||||
#include "utils/fmgrprotos.h"
|
#include "utils/fmgrprotos.h"
|
||||||
|
|
||||||
static const float weights[] = {0.1f, 0.2f, 0.4f, 1.0f};
|
#define NUM_WEIGHTS 4
|
||||||
|
static const float default_weights[NUM_WEIGHTS] = {0.1f, 0.2f, 0.4f, 1.0f};
|
||||||
|
|
||||||
#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
|
#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
|
||||||
|
|
||||||
@ -396,22 +397,24 @@ calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const float *
|
/*
|
||||||
getWeights(ArrayType *win)
|
* Extract weights from an array. The weights are stored in *ws, which must
|
||||||
|
* have space for NUM_WEIGHTS elements.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
getWeights(ArrayType *win, float *ws)
|
||||||
{
|
{
|
||||||
static float ws[lengthof(weights)];
|
|
||||||
int i;
|
int i;
|
||||||
float4 *arrdata;
|
float4 *arrdata;
|
||||||
|
|
||||||
if (win == NULL)
|
Assert(win != NULL);
|
||||||
return weights;
|
|
||||||
|
|
||||||
if (ARR_NDIM(win) != 1)
|
if (ARR_NDIM(win) != 1)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
||||||
errmsg("array of weight must be one-dimensional")));
|
errmsg("array of weight must be one-dimensional")));
|
||||||
|
|
||||||
if (ArrayGetNItems(ARR_NDIM(win), ARR_DIMS(win)) < lengthof(weights))
|
if (ArrayGetNItems(ARR_NDIM(win), ARR_DIMS(win)) < NUM_WEIGHTS)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
||||||
errmsg("array of weight is too short")));
|
errmsg("array of weight is too short")));
|
||||||
@ -422,16 +425,14 @@ getWeights(ArrayType *win)
|
|||||||
errmsg("array of weight must not contain nulls")));
|
errmsg("array of weight must not contain nulls")));
|
||||||
|
|
||||||
arrdata = (float4 *) ARR_DATA_PTR(win);
|
arrdata = (float4 *) ARR_DATA_PTR(win);
|
||||||
for (i = 0; i < lengthof(weights); i++)
|
for (i = 0; i < NUM_WEIGHTS; i++)
|
||||||
{
|
{
|
||||||
ws[i] = (arrdata[i] >= 0) ? arrdata[i] : weights[i];
|
ws[i] = (arrdata[i] >= 0) ? arrdata[i] : default_weights[i];
|
||||||
if (ws[i] > 1.0)
|
if (ws[i] > 1.0)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||||
errmsg("weight out of range")));
|
errmsg("weight out of range")));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ws;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Datum
|
Datum
|
||||||
@ -441,9 +442,11 @@ ts_rank_wttf(PG_FUNCTION_ARGS)
|
|||||||
TSVector txt = PG_GETARG_TSVECTOR(1);
|
TSVector txt = PG_GETARG_TSVECTOR(1);
|
||||||
TSQuery query = PG_GETARG_TSQUERY(2);
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
||||||
int method = PG_GETARG_INT32(3);
|
int method = PG_GETARG_INT32(3);
|
||||||
|
float weights[NUM_WEIGHTS];
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank(getWeights(win), txt, query, method);
|
getWeights(win, weights);
|
||||||
|
res = calc_rank(weights, txt, query, method);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(win, 0);
|
PG_FREE_IF_COPY(win, 0);
|
||||||
PG_FREE_IF_COPY(txt, 1);
|
PG_FREE_IF_COPY(txt, 1);
|
||||||
@ -457,9 +460,11 @@ ts_rank_wtt(PG_FUNCTION_ARGS)
|
|||||||
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
||||||
TSVector txt = PG_GETARG_TSVECTOR(1);
|
TSVector txt = PG_GETARG_TSVECTOR(1);
|
||||||
TSQuery query = PG_GETARG_TSQUERY(2);
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
||||||
|
float weights[NUM_WEIGHTS];
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank(getWeights(win), txt, query, DEF_NORM_METHOD);
|
getWeights(win, weights);
|
||||||
|
res = calc_rank(weights, txt, query, DEF_NORM_METHOD);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(win, 0);
|
PG_FREE_IF_COPY(win, 0);
|
||||||
PG_FREE_IF_COPY(txt, 1);
|
PG_FREE_IF_COPY(txt, 1);
|
||||||
@ -475,7 +480,7 @@ ts_rank_ttf(PG_FUNCTION_ARGS)
|
|||||||
int method = PG_GETARG_INT32(2);
|
int method = PG_GETARG_INT32(2);
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank(getWeights(NULL), txt, query, method);
|
res = calc_rank(default_weights, txt, query, method);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(txt, 0);
|
PG_FREE_IF_COPY(txt, 0);
|
||||||
PG_FREE_IF_COPY(query, 1);
|
PG_FREE_IF_COPY(query, 1);
|
||||||
@ -489,7 +494,7 @@ ts_rank_tt(PG_FUNCTION_ARGS)
|
|||||||
TSQuery query = PG_GETARG_TSQUERY(1);
|
TSQuery query = PG_GETARG_TSQUERY(1);
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank(getWeights(NULL), txt, query, DEF_NORM_METHOD);
|
res = calc_rank(default_weights, txt, query, DEF_NORM_METHOD);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(txt, 0);
|
PG_FREE_IF_COPY(txt, 0);
|
||||||
PG_FREE_IF_COPY(query, 1);
|
PG_FREE_IF_COPY(query, 1);
|
||||||
@ -855,16 +860,16 @@ calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
|
|||||||
doclen = 0;
|
doclen = 0;
|
||||||
CoverExt ext;
|
CoverExt ext;
|
||||||
double Wdoc = 0.0;
|
double Wdoc = 0.0;
|
||||||
double invws[lengthof(weights)];
|
double invws[NUM_WEIGHTS];
|
||||||
double SumDist = 0.0,
|
double SumDist = 0.0,
|
||||||
PrevExtPos = 0.0;
|
PrevExtPos = 0.0;
|
||||||
int NExtent = 0;
|
int NExtent = 0;
|
||||||
QueryRepresentation qr;
|
QueryRepresentation qr;
|
||||||
|
|
||||||
|
|
||||||
for (i = 0; i < lengthof(weights); i++)
|
for (i = 0; i < NUM_WEIGHTS; i++)
|
||||||
{
|
{
|
||||||
invws[i] = ((double) ((arrdata[i] >= 0) ? arrdata[i] : weights[i]));
|
invws[i] = ((double) ((arrdata[i] >= 0) ? arrdata[i] : default_weights[i]));
|
||||||
if (invws[i] > 1.0)
|
if (invws[i] > 1.0)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||||
@ -956,9 +961,11 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS)
|
|||||||
TSVector txt = PG_GETARG_TSVECTOR(1);
|
TSVector txt = PG_GETARG_TSVECTOR(1);
|
||||||
TSQuery query = PG_GETARG_TSQUERY(2);
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
||||||
int method = PG_GETARG_INT32(3);
|
int method = PG_GETARG_INT32(3);
|
||||||
|
float weights[NUM_WEIGHTS];
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank_cd(getWeights(win), txt, query, method);
|
getWeights(win, weights);
|
||||||
|
res = calc_rank_cd(weights, txt, query, method);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(win, 0);
|
PG_FREE_IF_COPY(win, 0);
|
||||||
PG_FREE_IF_COPY(txt, 1);
|
PG_FREE_IF_COPY(txt, 1);
|
||||||
@ -972,9 +979,11 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS)
|
|||||||
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
||||||
TSVector txt = PG_GETARG_TSVECTOR(1);
|
TSVector txt = PG_GETARG_TSVECTOR(1);
|
||||||
TSQuery query = PG_GETARG_TSQUERY(2);
|
TSQuery query = PG_GETARG_TSQUERY(2);
|
||||||
|
float weights[NUM_WEIGHTS];
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank_cd(getWeights(win), txt, query, DEF_NORM_METHOD);
|
getWeights(win, weights);
|
||||||
|
res = calc_rank_cd(weights, txt, query, DEF_NORM_METHOD);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(win, 0);
|
PG_FREE_IF_COPY(win, 0);
|
||||||
PG_FREE_IF_COPY(txt, 1);
|
PG_FREE_IF_COPY(txt, 1);
|
||||||
@ -990,7 +999,7 @@ ts_rankcd_ttf(PG_FUNCTION_ARGS)
|
|||||||
int method = PG_GETARG_INT32(2);
|
int method = PG_GETARG_INT32(2);
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank_cd(getWeights(NULL), txt, query, method);
|
res = calc_rank_cd(default_weights, txt, query, method);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(txt, 0);
|
PG_FREE_IF_COPY(txt, 0);
|
||||||
PG_FREE_IF_COPY(query, 1);
|
PG_FREE_IF_COPY(query, 1);
|
||||||
@ -1004,7 +1013,7 @@ ts_rankcd_tt(PG_FUNCTION_ARGS)
|
|||||||
TSQuery query = PG_GETARG_TSQUERY(1);
|
TSQuery query = PG_GETARG_TSQUERY(1);
|
||||||
float res;
|
float res;
|
||||||
|
|
||||||
res = calc_rank_cd(getWeights(NULL), txt, query, DEF_NORM_METHOD);
|
res = calc_rank_cd(default_weights, txt, query, DEF_NORM_METHOD);
|
||||||
|
|
||||||
PG_FREE_IF_COPY(txt, 0);
|
PG_FREE_IF_COPY(txt, 0);
|
||||||
PG_FREE_IF_COPY(query, 1);
|
PG_FREE_IF_COPY(query, 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user