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_decompress (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 166 of file tsquery_gist.c.

Referenced by gtsquery_picksplit().

Function Documentation

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

Definition at line 158 of file tsquery_gist.c.

Referenced by gtsquery_picksplit().

159 {
160  if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
161  return 0;
162  else
163  return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
164 }
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:305
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define TSQuerySignGetDatum(X)
Definition: ts_utils.h:220
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:218
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:216
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:891
OffsetNumber offset
Definition: gist.h:126
Datum gtsquery_consistent ( PG_FUNCTION_ARGS  )

Definition at line 53 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().

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);
61  TSQuerySign key = DatumGetTSQuerySign(entry->key);
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 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:232
#define FALSE
Definition: c.h:218
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:249
Datum key
Definition: gist.h:123
uint64 TSQuerySign
Definition: ts_utils.h:216
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define RTContainsStrategyNumber
Definition: stratnum.h:50
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
#define DatumGetTSQuerySign(X)
Definition: ts_utils.h:221
Datum gtsquery_consistent_oldsig ( PG_FUNCTION_ARGS  )

Definition at line 275 of file tsquery_gist.c.

References gtsquery_consistent().

276 {
277  return gtsquery_consistent(fcinfo);
278 }
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:53
Datum gtsquery_decompress ( PG_FUNCTION_ARGS  )

Definition at line 47 of file tsquery_gist.c.

References PG_GETARG_DATUM, and PG_RETURN_DATUM.

48 {
50 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
Datum gtsquery_penalty ( PG_FUNCTION_ARGS  )

Definition at line 139 of file tsquery_gist.c.

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

140 {
143  float *penalty = (float *) PG_GETARG_POINTER(2);
144 
145  *penalty = hemdist(origval, newval);
146 
147  PG_RETURN_POINTER(penalty);
148 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
uint64 TSQuerySign
Definition: ts_utils.h:216
#define newval
static int hemdist(TSQuerySign a, TSQuerySign b)
Definition: tsquery_gist.c:131
#define DatumGetTSQuerySign(X)
Definition: ts_utils.h:221
Datum gtsquery_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 169 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.

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

Definition at line 107 of file tsquery_gist.c.

References PG_GETARG_POINTER, PG_GETARG_TSQUERYSIGN, and PG_RETURN_POINTER.

108 {
111  bool *result = (bool *) PG_GETARG_POINTER(2);
112 
113  *result = (a == b) ? true : false;
114 
115  PG_RETURN_POINTER(result);
116 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
uint64 TSQuerySign
Definition: ts_utils.h:216
#define PG_GETARG_TSQUERYSIGN(n)
Definition: ts_utils.h:223
Datum gtsquery_union ( PG_FUNCTION_ARGS  )

Definition at line 89 of file tsquery_gist.c.

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

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 
103  PG_RETURN_TSQUERYSIGN(sign);
104 }
#define PG_RETURN_TSQUERYSIGN(X)
Definition: ts_utils.h:222
int32 n
Definition: gist.h:160
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
char sign
Definition: informix.c:693
uint64 TSQuerySign
Definition: ts_utils.h:216
int i
#define GETENTRY(vec, pos)
Definition: tsquery_gist.c:22
static int hemdist ( TSQuerySign  a,
TSQuerySign  b 
)
static

Definition at line 131 of file tsquery_gist.c.

References sizebitvec().

Referenced by gtsquery_penalty(), and gtsquery_picksplit().

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

Definition at line 119 of file tsquery_gist.c.

References i, and TSQS_SIGLEN.

Referenced by hemdist().

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:218
char sign
Definition: informix.c:693
int i