PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tsquery_gist.c File Reference
#include "postgres.h"
#include "access/stratnum.h"
#include "access/gist.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.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

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

Definition at line 22 of file tsquery_gist.c.

Referenced by gtsquery_picksplit(), and gtsquery_union().

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

Definition at line 165 of file tsquery_gist.c.

Referenced by gtsquery_picksplit().

Function Documentation

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

Definition at line 157 of file tsquery_gist.c.

Referenced by gtsquery_picksplit().

158 {
159  if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
160  return 0;
161  else
162  return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
163 }
Datum gtsquery_compress ( PG_FUNCTION_ARGS  )

Definition at line 26 of file tsquery_gist.c.

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

27 {
28  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
29  GISTENTRY *retval = entry;
30 
31  if (entry->leafkey)
32  {
34 
35  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
36  sign = makeTSQuerySign(DatumGetTSQuery(entry->key));
37 
38  gistentryinit(*retval, TSQuerySignGetDatum(sign),
39  entry->rel, entry->page,
40  entry->offset, FALSE);
41  }
42 
43  PG_RETURN_POINTER(retval);
44 }
Relation rel
Definition: gist.h:124
#define DatumGetTSQuery(X)
Definition: ts_type.h:235
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define TSQuerySignGetDatum(X)
Definition: ts_utils.h:219
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:219
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:249
char sign
Definition: informix.c:693
Datum key
Definition: gist.h:123
uint64 TSQuerySign
Definition: ts_utils.h:215
bool leafkey
Definition: gist.h:127
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
void * palloc(Size size)
Definition: mcxt.c:848
OffsetNumber offset
Definition: gist.h:126
Datum gtsquery_consistent ( PG_FUNCTION_ARGS  )

Definition at line 52 of file tsquery_gist.c.

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

Referenced by gtsquery_consistent_oldsig().

53 {
54  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
55  TSQuery query = PG_GETARG_TSQUERY(1);
57 
58  /* Oid subtype = PG_GETARG_OID(3); */
59  bool *recheck = (bool *) PG_GETARG_POINTER(4);
60  TSQuerySign key = DatumGetTSQuerySign(entry->key);
61  TSQuerySign sq = makeTSQuerySign(query);
62  bool retval;
63 
64  /* All cases served by this function are inexact */
65  *recheck = true;
66 
67  switch (strategy)
68  {
70  if (GIST_LEAF(entry))
71  retval = (key & sq) == sq;
72  else
73  retval = (key & sq) != 0;
74  break;
76  if (GIST_LEAF(entry))
77  retval = (key & sq) == key;
78  else
79  retval = (key & sq) != 0;
80  break;
81  default:
82  retval = FALSE;
83  }
84  PG_RETURN_BOOL(retval);
85 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
uint16 StrategyNumber
Definition: stratnum.h:22
#define RTContainedByStrategyNumber
Definition: stratnum.h:51
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define FALSE
Definition: c.h:219
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:249
Datum key
Definition: gist.h:123
uint64 TSQuerySign
Definition: ts_utils.h:215
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define RTContainsStrategyNumber
Definition: stratnum.h:50
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetTSQuerySign(X)
Definition: ts_utils.h:220
Datum gtsquery_consistent_oldsig ( PG_FUNCTION_ARGS  )

Definition at line 274 of file tsquery_gist.c.

References gtsquery_consistent().

275 {
276  return gtsquery_consistent(fcinfo);
277 }
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:52
Datum gtsquery_penalty ( PG_FUNCTION_ARGS  )

Definition at line 138 of file tsquery_gist.c.

References DatumGetTSQuerySign, hemdist(), newval, PG_GETARG_POINTER, and PG_RETURN_POINTER.

139 {
142  float *penalty = (float *) PG_GETARG_POINTER(2);
143 
144  *penalty = hemdist(origval, newval);
145 
146  PG_RETURN_POINTER(penalty);
147 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
uint64 TSQuerySign
Definition: ts_utils.h:215
#define newval
static int hemdist(TSQuerySign a, TSQuerySign b)
Definition: tsquery_gist.c:130
#define DatumGetTSQuerySign(X)
Definition: ts_utils.h:220
Datum gtsquery_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 168 of file tsquery_gist.c.

References comparecost(), SPLITCOST::cost, FirstOffsetNumber, GETENTRY, hemdist(), NODE::left, GistEntryVector::n, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, SPLITCOST::pos, qsort, NODE::right, 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.

169 {
172  OffsetNumber maxoff = entryvec->n - 2;
173  OffsetNumber k,
174  j;
175  TSQuerySign datum_l,
176  datum_r;
177  int32 size_alpha,
178  size_beta;
179  int32 size_waste,
180  waste = -1;
181  int32 nbytes;
182  OffsetNumber seed_1 = 0,
183  seed_2 = 0;
184  OffsetNumber *left,
185  *right;
186 
187  SPLITCOST *costvector;
188 
189  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
190  left = v->spl_left = (OffsetNumber *) palloc(nbytes);
191  right = v->spl_right = (OffsetNumber *) palloc(nbytes);
192  v->spl_nleft = v->spl_nright = 0;
193 
194  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
195  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
196  {
197  size_waste = hemdist(GETENTRY(entryvec, j), GETENTRY(entryvec, k));
198  if (size_waste > waste)
199  {
200  waste = size_waste;
201  seed_1 = k;
202  seed_2 = j;
203  }
204  }
205 
206 
207  if (seed_1 == 0 || seed_2 == 0)
208  {
209  seed_1 = 1;
210  seed_2 = 2;
211  }
212 
213  datum_l = GETENTRY(entryvec, seed_1);
214  datum_r = GETENTRY(entryvec, seed_2);
215 
216  maxoff = OffsetNumberNext(maxoff);
217  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
218  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
219  {
220  costvector[j - 1].pos = j;
221  size_alpha = hemdist(GETENTRY(entryvec, seed_1), GETENTRY(entryvec, j));
222  size_beta = hemdist(GETENTRY(entryvec, seed_2), GETENTRY(entryvec, j));
223  costvector[j - 1].cost = abs(size_alpha - size_beta);
224  }
225  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
226 
227  for (k = 0; k < maxoff; k++)
228  {
229  j = costvector[k].pos;
230  if (j == seed_1)
231  {
232  *left++ = j;
233  v->spl_nleft++;
234  continue;
235  }
236  else if (j == seed_2)
237  {
238  *right++ = j;
239  v->spl_nright++;
240  continue;
241  }
242  size_alpha = hemdist(datum_l, GETENTRY(entryvec, j));
243  size_beta = hemdist(datum_r, GETENTRY(entryvec, j));
244 
245  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.05))
246  {
247  datum_l |= GETENTRY(entryvec, j);
248  *left++ = j;
249  v->spl_nleft++;
250  }
251  else
252  {
253  datum_r |= GETENTRY(entryvec, j);
254  *right++ = j;
255  v->spl_nright++;
256  }
257  }
258 
259  *right = *left = FirstOffsetNumber;
260  v->spl_ldatum = TSQuerySignGetDatum(datum_l);
261  v->spl_rdatum = TSQuerySignGetDatum(datum_r);
262 
264 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
OffsetNumber pos
Definition: hstore_gist.c:323
OffsetNumber * spl_left
Definition: gist.h:105
Datum spl_rdatum
Definition: gist.h:112
int32 n
Definition: gist.h:160
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
int spl_nleft
Definition: gist.h:106
signed int int32
Definition: c.h:246
#define TSQuerySignGetDatum(X)
Definition: ts_utils.h:219
int32 cost
Definition: hstore_gist.c:324
uint16 OffsetNumber
Definition: off.h:24
int spl_nright
Definition: gist.h:111
uint64 TSQuerySign
Definition: ts_utils.h:215
#define FirstOffsetNumber
Definition: off.h:27
#define WISH_F(a, b, c)
Definition: tsquery_gist.c:165
static int comparecost(const void *a, const void *b)
Definition: tsquery_gist.c:157
Datum spl_ldatum
Definition: gist.h:107
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
OffsetNumber * spl_right
Definition: gist.h:110
void * palloc(Size size)
Definition: mcxt.c:848
#define GETENTRY(vec, pos)
Definition: tsquery_gist.c:22
#define qsort(a, b, c, d)
Definition: port.h:443
static int hemdist(TSQuerySign a, TSQuerySign b)
Definition: tsquery_gist.c:130
Datum gtsquery_same ( PG_FUNCTION_ARGS  )

Definition at line 106 of file tsquery_gist.c.

References PG_GETARG_POINTER, PG_GETARG_TSQUERYSIGN, PG_RETURN_POINTER, and result.

107 {
110  bool *result = (bool *) PG_GETARG_POINTER(2);
111 
112  *result = (a == b) ? true : false;
113 
114  PG_RETURN_POINTER(result);
115 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
uint64 TSQuerySign
Definition: ts_utils.h:215
#define PG_GETARG_TSQUERYSIGN(n)
Definition: ts_utils.h:222
Datum gtsquery_union ( PG_FUNCTION_ARGS  )

Definition at line 88 of file tsquery_gist.c.

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

89 {
91  int *size = (int *) PG_GETARG_POINTER(1);
93  int i;
94 
95  sign = 0;
96 
97  for (i = 0; i < entryvec->n; i++)
98  sign |= GETENTRY(entryvec, i);
99 
100  *size = sizeof(TSQuerySign);
101 
102  PG_RETURN_TSQUERYSIGN(sign);
103 }
#define PG_RETURN_TSQUERYSIGN(X)
Definition: ts_utils.h:221
int32 n
Definition: gist.h:160
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
char sign
Definition: informix.c:693
uint64 TSQuerySign
Definition: ts_utils.h:215
int i
#define GETENTRY(vec, pos)
Definition: tsquery_gist.c:22
static int hemdist ( TSQuerySign  a,
TSQuerySign  b 
)
static

Definition at line 130 of file tsquery_gist.c.

References sizebitvec().

Referenced by gtsquery_penalty(), and gtsquery_picksplit().

131 {
132  TSQuerySign res = a ^ b;
133 
134  return sizebitvec(res);
135 }
static int sizebitvec(TSQuerySign sign)
Definition: tsquery_gist.c:118
uint64 TSQuerySign
Definition: ts_utils.h:215
static int sizebitvec ( TSQuerySign  sign)
static

Definition at line 118 of file tsquery_gist.c.

References i, and TSQS_SIGLEN.

Referenced by hemdist().

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