|
| 1 | +#include"btree_gist.h" |
| 2 | +#include"btree_utils_var.h" |
| 3 | +#include"utils/builtins.h" |
| 4 | +#include"utils/varbit.h" |
| 5 | + |
| 6 | + |
| 7 | +/* |
| 8 | +** Bit ops |
| 9 | +*/ |
| 10 | +PG_FUNCTION_INFO_V1(gbt_bit_compress); |
| 11 | +PG_FUNCTION_INFO_V1(gbt_bit_union); |
| 12 | +PG_FUNCTION_INFO_V1(gbt_bit_picksplit); |
| 13 | +PG_FUNCTION_INFO_V1(gbt_bit_consistent); |
| 14 | +PG_FUNCTION_INFO_V1(gbt_bit_penalty); |
| 15 | +PG_FUNCTION_INFO_V1(gbt_bit_same); |
| 16 | + |
| 17 | +Datumgbt_bit_compress(PG_FUNCTION_ARGS); |
| 18 | +Datumgbt_bit_union(PG_FUNCTION_ARGS); |
| 19 | +Datumgbt_bit_picksplit(PG_FUNCTION_ARGS); |
| 20 | +Datumgbt_bit_consistent(PG_FUNCTION_ARGS); |
| 21 | +Datumgbt_bit_penalty(PG_FUNCTION_ARGS); |
| 22 | +Datumgbt_bit_same(PG_FUNCTION_ARGS); |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +/* define for comparison */ |
| 27 | + |
| 28 | +staticboolgbt_bitgt (constvoid*a,constvoid*b) |
| 29 | +{ |
| 30 | +return (DatumGetBool(DirectFunctionCall2(bitgt ,PointerGetDatum(a ),PointerGetDatum(b ) ) ) ); |
| 31 | +} |
| 32 | + |
| 33 | +staticboolgbt_bitge (constvoid*a,constvoid*b) |
| 34 | +{ |
| 35 | +return (DatumGetBool(DirectFunctionCall2(bitge ,PointerGetDatum(a ),PointerGetDatum(b ) ) ) ); |
| 36 | +} |
| 37 | + |
| 38 | +staticboolgbt_biteq (constvoid*a,constvoid*b) |
| 39 | +{ |
| 40 | +return (DatumGetBool(DirectFunctionCall2(biteq ,PointerGetDatum(a ),PointerGetDatum(b ) ) ) ); |
| 41 | +} |
| 42 | + |
| 43 | +staticboolgbt_bitle (constvoid*a,constvoid*b) |
| 44 | +{ |
| 45 | +return (DatumGetBool(DirectFunctionCall2(bitle ,PointerGetDatum(a ),PointerGetDatum(b ) ) ) ); |
| 46 | +} |
| 47 | + |
| 48 | +staticboolgbt_bitlt (constvoid*a,constvoid*b) |
| 49 | +{ |
| 50 | +return (DatumGetBool(DirectFunctionCall2(bitlt ,PointerGetDatum(a ),PointerGetDatum(b ) ) ) ); |
| 51 | +} |
| 52 | + |
| 53 | +staticint32gbt_bitcmp (constbytea*a ,constbytea*b ) |
| 54 | +{ |
| 55 | +return |
| 56 | + (DatumGetInt32(DirectFunctionCall2(byteacmp,PointerGetDatum(a),PointerGetDatum(b) ) ) ); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +staticbytea* |
| 61 | +gbt_bit_xfrm (bytea*leaf ) |
| 62 | +{ |
| 63 | +bytea*out=leaf; |
| 64 | +ints=VARBITBYTES(leaf)+VARHDRSZ; |
| 65 | + |
| 66 | +out=palloc (s ); |
| 67 | +VARATT_SIZEP(out)=s; |
| 68 | +memcpy ( (void*)VARDATA(out), (void*)VARBITS(leaf),VARBITBYTES(leaf) ); |
| 69 | +returnout; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +staticGBT_VARKEY*gbt_bit_l2n (GBT_VARKEY*leaf ) |
| 76 | +{ |
| 77 | + |
| 78 | +GBT_VARKEY*out=leaf ; |
| 79 | +GBT_VARKEY_Rr=gbt_var_key_readable (leaf ); |
| 80 | +bytea*o ; |
| 81 | + |
| 82 | +o=gbt_bit_xfrm (r.lower); |
| 83 | +r.upper=r.lower=o; |
| 84 | +out=gbt_var_key_copy(&r, TRUE ); |
| 85 | +pfree(o); |
| 86 | + |
| 87 | +returnout; |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +staticconstgbtree_vinfotinfo= |
| 92 | +{ |
| 93 | +gbt_t_bit, |
| 94 | + FALSE, |
| 95 | + TRUE, |
| 96 | +gbt_bitgt, |
| 97 | +gbt_bitge, |
| 98 | +gbt_biteq, |
| 99 | +gbt_bitle, |
| 100 | +gbt_bitlt, |
| 101 | +gbt_bitcmp, |
| 102 | +gbt_bit_l2n |
| 103 | +}; |
| 104 | + |
| 105 | + |
| 106 | +/************************************************** |
| 107 | + * Bit ops |
| 108 | + **************************************************/ |
| 109 | + |
| 110 | +Datum |
| 111 | +gbt_bit_compress (PG_FUNCTION_ARGS) |
| 112 | +{ |
| 113 | +GISTENTRY*entry= (GISTENTRY*)PG_GETARG_POINTER(0); |
| 114 | +PG_RETURN_POINTER (gbt_var_compress(entry,&tinfo ) ); |
| 115 | +} |
| 116 | + |
| 117 | +Datum |
| 118 | +gbt_bit_consistent(PG_FUNCTION_ARGS) |
| 119 | +{ |
| 120 | +GISTENTRY*entry= (GISTENTRY*)PG_GETARG_POINTER(0); |
| 121 | +GBT_VARKEY*ktst= (GBT_VARKEY*)DatumGetPointer (entry->key ) ; |
| 122 | +GBT_VARKEY*key= (GBT_VARKEY*)DatumGetPointer (PG_DETOAST_DATUM(entry->key ) ); |
| 123 | +void*qtst= (void* )DatumGetPointer(PG_GETARG_DATUM(1) ); |
| 124 | +void*query= (void* )DatumGetByteaP (PG_GETARG_DATUM(1) ); |
| 125 | +StrategyNumberstrategy= (StrategyNumber)PG_GETARG_UINT16(2); |
| 126 | +boolretval= FALSE; |
| 127 | +GBT_VARKEY_Rr=gbt_var_key_readable (key ); |
| 128 | + |
| 129 | +if (GIST_LEAF(entry) ) |
| 130 | + { |
| 131 | +retval=gbt_var_consistent(&r,query,&strategy, TRUE,&tinfo ); |
| 132 | + }else { |
| 133 | +bytea*q=gbt_bit_xfrm ( (bytea* )query ); |
| 134 | +retval=gbt_var_consistent(&r, (void*)q,&strategy, FALSE,&tinfo ); |
| 135 | +pfree(q); |
| 136 | + } |
| 137 | + |
| 138 | +if (ktst!=key ){ |
| 139 | +pfree (key ); |
| 140 | + } |
| 141 | +if (qtst!=query ){ |
| 142 | +pfree (query ); |
| 143 | + } |
| 144 | +PG_RETURN_BOOL(retval); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +Datum |
| 150 | +gbt_bit_union(PG_FUNCTION_ARGS) |
| 151 | +{ |
| 152 | +GistEntryVector*entryvec= (GistEntryVector*)PG_GETARG_POINTER(0); |
| 153 | +int32*size= (int*)PG_GETARG_POINTER(1); |
| 154 | +PG_RETURN_POINTER(gbt_var_union (entryvec ,size ,&tinfo ) ); |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +Datum |
| 159 | +gbt_bit_picksplit(PG_FUNCTION_ARGS) |
| 160 | +{ |
| 161 | +GistEntryVector*entryvec= (GistEntryVector*)PG_GETARG_POINTER(0); |
| 162 | +GIST_SPLITVEC*v= (GIST_SPLITVEC*)PG_GETARG_POINTER(1); |
| 163 | +gbt_var_picksplit (entryvec,v,&tinfo ); |
| 164 | +PG_RETURN_POINTER(v); |
| 165 | +} |
| 166 | + |
| 167 | +Datum |
| 168 | +gbt_bit_same(PG_FUNCTION_ARGS) |
| 169 | +{ |
| 170 | +Datumd1=PG_GETARG_DATUM(0); |
| 171 | +Datumd2=PG_GETARG_DATUM(1); |
| 172 | +bool*result= (bool*)PG_GETARG_POINTER(2); |
| 173 | +PG_RETURN_POINTER(gbt_var_same (result,d1 ,d2 ,&tinfo )); |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +Datum |
| 178 | +gbt_bit_penalty(PG_FUNCTION_ARGS) |
| 179 | +{ |
| 180 | +float*result= (float*)PG_GETARG_POINTER(2); |
| 181 | +GISTENTRY*o= (GISTENTRY*)PG_GETARG_POINTER(0); |
| 182 | +GISTENTRY*n= (GISTENTRY*)PG_GETARG_POINTER(1); |
| 183 | +PG_RETURN_POINTER(gbt_var_penalty (result ,o ,n,&tinfo ) ); |
| 184 | +} |
| 185 | + |