1 /*
2  * contrib/btree_gist/btree_bytea.c
3  */
4 #include "postgres.h"
5 
6 #include "btree_gist.h"
7 #include "btree_utils_var.h"
8 #include "utils/builtins.h"
9 #include "utils/bytea.h"
10 
11 
12 /*
13 ** Bytea ops
14 */
15 PG_FUNCTION_INFO_V1(gbt_bytea_compress);
16 PG_FUNCTION_INFO_V1(gbt_bytea_union);
17 PG_FUNCTION_INFO_V1(gbt_bytea_picksplit);
18 PG_FUNCTION_INFO_V1(gbt_bytea_consistent);
19 PG_FUNCTION_INFO_V1(gbt_bytea_penalty);
20 PG_FUNCTION_INFO_V1(gbt_bytea_same);
21 
22 
23 /* define for comparison */
24 
25 static bool
26 gbt_byteagt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
27 {
28 	return DatumGetBool(DirectFunctionCall2(byteagt,
29 											PointerGetDatum(a),
30 											PointerGetDatum(b)));
31 }
32 
33 static bool
34 gbt_byteage(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
35 {
36 	return DatumGetBool(DirectFunctionCall2(byteage,
37 											PointerGetDatum(a),
38 											PointerGetDatum(b)));
39 }
40 
41 static bool
42 gbt_byteaeq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
43 {
44 	return DatumGetBool(DirectFunctionCall2(byteaeq,
45 											PointerGetDatum(a),
46 											PointerGetDatum(b)));
47 }
48 
49 static bool
50 gbt_byteale(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
51 {
52 	return DatumGetBool(DirectFunctionCall2(byteale,
53 											PointerGetDatum(a),
54 											PointerGetDatum(b)));
55 }
56 
57 static bool
58 gbt_bytealt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
59 {
60 	return DatumGetBool(DirectFunctionCall2(bytealt,
61 											PointerGetDatum(a),
62 											PointerGetDatum(b)));
63 }
64 
65 static int32
66 gbt_byteacmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
67 {
68 	return DatumGetInt32(DirectFunctionCall2(byteacmp,
69 											 PointerGetDatum(a),
70 											 PointerGetDatum(b)));
71 }
72 
73 
74 static const gbtree_vinfo tinfo =
75 {
76 	gbt_t_bytea,
77 	0,
78 	true,
79 	gbt_byteagt,
80 	gbt_byteage,
81 	gbt_byteaeq,
82 	gbt_byteale,
83 	gbt_bytealt,
84 	gbt_byteacmp,
85 	NULL
86 };
87 
88 
89 /**************************************************
90  * Text ops
91  **************************************************/
92 
93 
94 Datum
95 gbt_bytea_compress(PG_FUNCTION_ARGS)
96 {
97 	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
98 
99 	PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
100 }
101 
102 
103 
104 Datum
105 gbt_bytea_consistent(PG_FUNCTION_ARGS)
106 {
107 	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
108 	void	   *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
109 	StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
110 
111 	/* Oid		subtype = PG_GETARG_OID(3); */
112 	bool	   *recheck = (bool *) PG_GETARG_POINTER(4);
113 	bool		retval;
114 	GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
115 	GBT_VARKEY_R r = gbt_var_key_readable(key);
116 
117 	/* All cases served by this function are exact */
118 	*recheck = false;
119 
120 	retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
121 								GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
122 	PG_RETURN_BOOL(retval);
123 }
124 
125 
126 
127 Datum
128 gbt_bytea_union(PG_FUNCTION_ARGS)
129 {
130 	GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
131 	int32	   *size = (int *) PG_GETARG_POINTER(1);
132 
133 	PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
134 									&tinfo, fcinfo->flinfo));
135 }
136 
137 
138 Datum
139 gbt_bytea_picksplit(PG_FUNCTION_ARGS)
140 {
141 	GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
142 	GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
143 
144 	gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
145 					  &tinfo, fcinfo->flinfo);
146 	PG_RETURN_POINTER(v);
147 }
148 
149 Datum
150 gbt_bytea_same(PG_FUNCTION_ARGS)
151 {
152 	Datum		d1 = PG_GETARG_DATUM(0);
153 	Datum		d2 = PG_GETARG_DATUM(1);
154 	bool	   *result = (bool *) PG_GETARG_POINTER(2);
155 
156 	*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
157 	PG_RETURN_POINTER(result);
158 }
159 
160 
161 Datum
162 gbt_bytea_penalty(PG_FUNCTION_ARGS)
163 {
164 	GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
165 	GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
166 	float	   *result = (float *) PG_GETARG_POINTER(2);
167 
168 	PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
169 									  &tinfo, fcinfo->flinfo));
170 }
171