PostgreSQL Source Code  git master
tsquery_gist.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/stratnum.h"
#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/fmgrprotos.h"
Include dependency graph for tsquery_gist.c:

Go to the source code of this file.

Data Structures

struct  SPLITCOST
 

Macros

#define GETENTRY(vec, pos)   DatumGetTSQuerySign((vec)->vector[pos].key)
 
#define WISH_F(a, b, c)   (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
 

Functions

Datum gtsquery_compress (PG_FUNCTION_ARGS)
 
Datum gtsquery_consistent (PG_FUNCTION_ARGS)
 
Datum gtsquery_union (PG_FUNCTION_ARGS)
 
Datum gtsquery_same (PG_FUNCTION_ARGS)
 
static int sizebitvec (TSQuerySign sign)
 
static int hemdist (TSQuerySign a, TSQuerySign b)
 
Datum gtsquery_penalty (PG_FUNCTION_ARGS)
 
static int comparecost (const void *a, const void *b)
 
Datum gtsquery_picksplit (PG_FUNCTION_ARGS)
 
Datum gtsquery_consistent_oldsig (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ GETENTRY

#define GETENTRY (   vec,
  pos 
)    DatumGetTSQuerySign((vec)->vector[pos].key)

Definition at line 23 of file tsquery_gist.c.

◆ WISH_F

#define WISH_F (   a,
  b,
  c 
)    (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )

Definition at line 164 of file tsquery_gist.c.

Function Documentation

◆ comparecost()

static int comparecost ( const void *  a,
const void *  b 
)
static

Definition at line 158 of file tsquery_gist.c.

159 {
160  return pg_cmp_s32(((const SPLITCOST *) a)->cost,
161  ((const SPLITCOST *) b)->cost);
162 }
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:483
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, and pg_cmp_s32().

Referenced by gtsquery_picksplit().

◆ gtsquery_compress()

Datum gtsquery_compress ( PG_FUNCTION_ARGS  )

Definition at line 27 of file tsquery_gist.c.

28 {
29  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
30  GISTENTRY *retval = entry;
31 
32  if (entry->leafkey)
33  {
35 
36  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
38 
40  entry->rel, entry->page,
41  entry->offset, false);
42  }
43 
44  PG_RETURN_POINTER(retval);
45 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:242
char sign
Definition: informix.c:674
void * palloc(Size size)
Definition: mcxt.c:1316
OffsetNumber offset
Definition: gist.h:161
Datum key
Definition: gist.h:158
Page page
Definition: gist.h:160
Relation rel
Definition: gist.h:159
bool leafkey
Definition: gist.h:162
static TSQuery DatumGetTSQuery(Datum X)
Definition: ts_type.h:249
static Datum TSQuerySignGetDatum(TSQuerySign X)
Definition: ts_utils.h:254
uint64 TSQuerySign
Definition: ts_utils.h:249
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:250

References DatumGetTSQuery(), gistentryinit, GISTENTRY::key, GISTENTRY::leafkey, makeTSQuerySign(), GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, GISTENTRY::rel, sign, and TSQuerySignGetDatum().

◆ gtsquery_consistent()

Datum gtsquery_consistent ( PG_FUNCTION_ARGS  )

Definition at line 53 of file tsquery_gist.c.

54 {
55  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
56  TSQuery query = PG_GETARG_TSQUERY(1);
58 
59  /* Oid subtype = PG_GETARG_OID(3); */
60  bool *recheck = (bool *) PG_GETARG_POINTER(4);
62  TSQuerySign sq = makeTSQuerySign(query);
63  bool retval;
64 
65  /* All cases served by this function are inexact */
66  *recheck = true;
67 
68  switch (strategy)
69  {
71  if (GIST_LEAF(entry))
72  retval = (key & sq) == sq;
73  else
74  retval = (key & sq) != 0;
75  break;
77  if (GIST_LEAF(entry))
78  retval = (key & sq) == key;
79  else
80  retval = (key & sq) != 0;
81  break;
82  default:
83  retval = false;
84  }
85  PG_RETURN_BOOL(retval);
86 }
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:168
uint16 StrategyNumber
Definition: stratnum.h:22
#define RTContainsStrategyNumber
Definition: stratnum.h:57
#define RTContainedByStrategyNumber
Definition: stratnum.h:58
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:266
static TSQuerySign DatumGetTSQuerySign(Datum X)
Definition: ts_utils.h:260

References DatumGetTSQuerySign(), GIST_LEAF, GISTENTRY::key, sort-test::key, makeTSQuerySign(), PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_GETARG_UINT16, PG_RETURN_BOOL, RTContainedByStrategyNumber, and RTContainsStrategyNumber.

Referenced by gtsquery_consistent_oldsig().

◆ gtsquery_consistent_oldsig()

Datum gtsquery_consistent_oldsig ( PG_FUNCTION_ARGS  )

Definition at line 273 of file tsquery_gist.c.

274 {
275  return gtsquery_consistent(fcinfo);
276 }
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:53

References gtsquery_consistent().

◆ gtsquery_penalty()

Datum gtsquery_penalty ( PG_FUNCTION_ARGS  )

Definition at line 139 of file tsquery_gist.c.

140 {
143  float *penalty = (float *) PG_GETARG_POINTER(2);
144 
145  *penalty = hemdist(origval, newval);
146 
147  PG_RETURN_POINTER(penalty);
148 }
#define newval
static int hemdist(TSQuerySign a, TSQuerySign b)
Definition: tsquery_gist.c:131

References DatumGetTSQuerySign(), hemdist(), sort-test::key, newval, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ gtsquery_picksplit()

Datum gtsquery_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 167 of file tsquery_gist.c.

168 {
171  OffsetNumber maxoff = entryvec->n - 2;
172  OffsetNumber k,
173  j;
174  TSQuerySign datum_l,
175  datum_r;
176  int32 size_alpha,
177  size_beta;
178  int32 size_waste,
179  waste = -1;
180  int32 nbytes;
181  OffsetNumber seed_1 = 0,
182  seed_2 = 0;
183  OffsetNumber *left,
184  *right;
185 
186  SPLITCOST *costvector;
187 
188  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
189  left = v->spl_left = (OffsetNumber *) palloc(nbytes);
190  right = v->spl_right = (OffsetNumber *) palloc(nbytes);
191  v->spl_nleft = v->spl_nright = 0;
192 
193  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
194  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
195  {
196  size_waste = hemdist(GETENTRY(entryvec, j), GETENTRY(entryvec, k));
197  if (size_waste > waste)
198  {
199  waste = size_waste;
200  seed_1 = k;
201  seed_2 = j;
202  }
203  }
204 
205 
206  if (seed_1 == 0 || seed_2 == 0)
207  {
208  seed_1 = 1;
209  seed_2 = 2;
210  }
211 
212  datum_l = GETENTRY(entryvec, seed_1);
213  datum_r = GETENTRY(entryvec, seed_2);
214 
215  maxoff = OffsetNumberNext(maxoff);
216  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
217  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
218  {
219  costvector[j - 1].pos = j;
220  size_alpha = hemdist(GETENTRY(entryvec, seed_1), GETENTRY(entryvec, j));
221  size_beta = hemdist(GETENTRY(entryvec, seed_2), GETENTRY(entryvec, j));
222  costvector[j - 1].cost = abs(size_alpha - size_beta);
223  }
224  qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
225 
226  for (k = 0; k < maxoff; k++)
227  {
228  j = costvector[k].pos;
229  if (j == seed_1)
230  {
231  *left++ = j;
232  v->spl_nleft++;
233  continue;
234  }
235  else if (j == seed_2)
236  {
237  *right++ = j;
238  v->spl_nright++;
239  continue;
240  }
241  size_alpha = hemdist(datum_l, GETENTRY(entryvec, j));
242  size_beta = hemdist(datum_r, GETENTRY(entryvec, j));
243 
244  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.05))
245  {
246  datum_l |= GETENTRY(entryvec, j);
247  *left++ = j;
248  v->spl_nleft++;
249  }
250  else
251  {
252  datum_r |= GETENTRY(entryvec, j);
253  *right++ = j;
254  v->spl_nright++;
255  }
256  }
257 
258  *right = *left = FirstOffsetNumber;
259  v->spl_ldatum = TSQuerySignGetDatum(datum_l);
260  v->spl_rdatum = TSQuerySignGetDatum(datum_r);
261 
263 }
signed int int32
Definition: c.h:494
int j
Definition: isn.c:74
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define qsort(a, b, c, d)
Definition: port.h:449
int spl_nleft
Definition: gist.h:141
OffsetNumber * spl_right
Definition: gist.h:145
Datum spl_ldatum
Definition: gist.h:142
Datum spl_rdatum
Definition: gist.h:147
int spl_nright
Definition: gist.h:146
OffsetNumber * spl_left
Definition: gist.h:140
int32 n
Definition: gist.h:233
int32 cost
Definition: hstore_gist.c:354
OffsetNumber pos
Definition: hstore_gist.c:353
#define WISH_F(a, b, c)
Definition: tsquery_gist.c:164
#define GETENTRY(vec, pos)
Definition: tsquery_gist.c:23
static int comparecost(const void *a, const void *b)
Definition: tsquery_gist.c:158

References comparecost(), SPLITCOST::cost, FirstOffsetNumber, GETENTRY, hemdist(), j, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, SPLITCOST::pos, qsort, GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, TSQuerySignGetDatum(), and WISH_F.

◆ gtsquery_same()

Datum gtsquery_same ( PG_FUNCTION_ARGS  )

Definition at line 107 of file tsquery_gist.c.

108 {
111  bool *result = (bool *) PG_GETARG_POINTER(2);
112 
113  *result = (a == b);
114 
115  PG_RETURN_POINTER(result);
116 }
#define PG_GETARG_TSQUERYSIGN(n)
Definition: ts_utils.h:266

References a, b, PG_GETARG_POINTER, PG_GETARG_TSQUERYSIGN, and PG_RETURN_POINTER.

◆ gtsquery_union()

Datum gtsquery_union ( PG_FUNCTION_ARGS  )

Definition at line 89 of file tsquery_gist.c.

90 {
92  int *size = (int *) PG_GETARG_POINTER(1);
94  int i;
95 
96  sign = 0;
97 
98  for (i = 0; i < entryvec->n; i++)
99  sign |= GETENTRY(entryvec, i);
100 
101  *size = sizeof(TSQuerySign);
102 
104 }
int i
Definition: isn.c:73
static pg_noinline void Size size
Definition: slab.c:607
#define PG_RETURN_TSQUERYSIGN(X)
Definition: ts_utils.h:265

References GETENTRY, i, GistEntryVector::n, PG_GETARG_POINTER, PG_RETURN_TSQUERYSIGN, sign, and size.

◆ hemdist()

static int hemdist ( TSQuerySign  a,
TSQuerySign  b 
)
static

Definition at line 131 of file tsquery_gist.c.

132 {
133  TSQuerySign res = a ^ b;
134 
135  return sizebitvec(res);
136 }
static int sizebitvec(TSQuerySign sign)
Definition: tsquery_gist.c:119

References a, b, res, and sizebitvec().

Referenced by gtsquery_penalty(), and gtsquery_picksplit().

◆ sizebitvec()

static int sizebitvec ( TSQuerySign  sign)
static

Definition at line 119 of file tsquery_gist.c.

120 {
121  int size = 0,
122  i;
123 
124  for (i = 0; i < TSQS_SIGLEN; i++)
125  size += 0x01 & (sign >> i);
126 
127  return size;
128 }
#define TSQS_SIGLEN
Definition: ts_utils.h:251

References i, sign, size, and TSQS_SIGLEN.

Referenced by hemdist().