Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / contrib / btree_gist / btree_cash.c
blob6cebd1a5a5e2f0b3356879f1634a7bacdf16deda
1 /*
2 * $PostgreSQL$
3 */
4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6 #include "utils/cash.h"
8 typedef struct
10 Cash lower;
11 Cash upper;
12 } cashKEY;
15 ** Cash ops
17 PG_FUNCTION_INFO_V1(gbt_cash_compress);
18 PG_FUNCTION_INFO_V1(gbt_cash_union);
19 PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
20 PG_FUNCTION_INFO_V1(gbt_cash_consistent);
21 PG_FUNCTION_INFO_V1(gbt_cash_penalty);
22 PG_FUNCTION_INFO_V1(gbt_cash_same);
24 Datum gbt_cash_compress(PG_FUNCTION_ARGS);
25 Datum gbt_cash_union(PG_FUNCTION_ARGS);
26 Datum gbt_cash_picksplit(PG_FUNCTION_ARGS);
27 Datum gbt_cash_consistent(PG_FUNCTION_ARGS);
28 Datum gbt_cash_penalty(PG_FUNCTION_ARGS);
29 Datum gbt_cash_same(PG_FUNCTION_ARGS);
31 static bool
32 gbt_cashgt(const void *a, const void *b)
34 return (*((Cash *) a) > *((Cash *) b));
36 static bool
37 gbt_cashge(const void *a, const void *b)
39 return (*((Cash *) a) >= *((Cash *) b));
41 static bool
42 gbt_casheq(const void *a, const void *b)
44 return (*((Cash *) a) == *((Cash *) b));
46 static bool
47 gbt_cashle(const void *a, const void *b)
49 return (*((Cash *) a) <= *((Cash *) b));
51 static bool
52 gbt_cashlt(const void *a, const void *b)
54 return (*((Cash *) a) < *((Cash *) b));
57 static int
58 gbt_cashkey_cmp(const void *a, const void *b)
61 if (*(Cash *) &(((Nsrt *) a)->t[0]) > *(Cash *) &(((Nsrt *) b)->t[0]))
62 return 1;
63 else if (*(Cash *) &(((Nsrt *) a)->t[0]) < *(Cash *) &(((Nsrt *) b)->t[0]))
64 return -1;
65 return 0;
70 static const gbtree_ninfo tinfo =
72 gbt_t_cash,
73 sizeof(Cash),
74 gbt_cashgt,
75 gbt_cashge,
76 gbt_casheq,
77 gbt_cashle,
78 gbt_cashlt,
79 gbt_cashkey_cmp
83 /**************************************************
84 * Cash ops
85 **************************************************/
88 Datum
89 gbt_cash_compress(PG_FUNCTION_ARGS)
91 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
92 GISTENTRY *retval = NULL;
94 PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
98 Datum
99 gbt_cash_consistent(PG_FUNCTION_ARGS)
101 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
102 Cash query = PG_GETARG_CASH(1);
103 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
105 /* Oid subtype = PG_GETARG_OID(3); */
106 bool *recheck = (bool *) PG_GETARG_POINTER(4);
107 cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
108 GBT_NUMKEY_R key;
110 /* All cases served by this function are exact */
111 *recheck = false;
113 key.lower = (GBT_NUMKEY *) &kkk->lower;
114 key.upper = (GBT_NUMKEY *) &kkk->upper;
116 PG_RETURN_BOOL(
117 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
122 Datum
123 gbt_cash_union(PG_FUNCTION_ARGS)
125 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
126 void *out = palloc(sizeof(cashKEY));
128 *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
129 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
133 Datum
134 gbt_cash_penalty(PG_FUNCTION_ARGS)
136 cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
137 cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
138 float *result = (float *) PG_GETARG_POINTER(2);
140 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
142 PG_RETURN_POINTER(result);
146 Datum
147 gbt_cash_picksplit(PG_FUNCTION_ARGS)
149 PG_RETURN_POINTER(gbt_num_picksplit(
150 (GistEntryVector *) PG_GETARG_POINTER(0),
151 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
152 &tinfo
156 Datum
157 gbt_cash_same(PG_FUNCTION_ARGS)
159 cashKEY *b1 = (cashKEY *) PG_GETARG_POINTER(0);
160 cashKEY *b2 = (cashKEY *) PG_GETARG_POINTER(1);
161 bool *result = (bool *) PG_GETARG_POINTER(2);
163 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
164 PG_RETURN_POINTER(result);