Go to the documentation of this file.00001
00002
00003
00004 #include "postgres.h"
00005
00006 #include "btree_gist.h"
00007 #include "btree_utils_num.h"
00008
00009 typedef struct
00010 {
00011 Oid lower;
00012 Oid upper;
00013 } oidKEY;
00014
00015
00016
00017
00018 PG_FUNCTION_INFO_V1(gbt_oid_compress);
00019 PG_FUNCTION_INFO_V1(gbt_oid_union);
00020 PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
00021 PG_FUNCTION_INFO_V1(gbt_oid_consistent);
00022 PG_FUNCTION_INFO_V1(gbt_oid_distance);
00023 PG_FUNCTION_INFO_V1(gbt_oid_penalty);
00024 PG_FUNCTION_INFO_V1(gbt_oid_same);
00025
00026 Datum gbt_oid_compress(PG_FUNCTION_ARGS);
00027 Datum gbt_oid_union(PG_FUNCTION_ARGS);
00028 Datum gbt_oid_picksplit(PG_FUNCTION_ARGS);
00029 Datum gbt_oid_consistent(PG_FUNCTION_ARGS);
00030 Datum gbt_oid_distance(PG_FUNCTION_ARGS);
00031 Datum gbt_oid_penalty(PG_FUNCTION_ARGS);
00032 Datum gbt_oid_same(PG_FUNCTION_ARGS);
00033
00034
00035 static bool
00036 gbt_oidgt(const void *a, const void *b)
00037 {
00038 return (*((const Oid *) a) > *((const Oid *) b));
00039 }
00040 static bool
00041 gbt_oidge(const void *a, const void *b)
00042 {
00043 return (*((const Oid *) a) >= *((const Oid *) b));
00044 }
00045 static bool
00046 gbt_oideq(const void *a, const void *b)
00047 {
00048 return (*((const Oid *) a) == *((const Oid *) b));
00049 }
00050 static bool
00051 gbt_oidle(const void *a, const void *b)
00052 {
00053 return (*((const Oid *) a) <= *((const Oid *) b));
00054 }
00055 static bool
00056 gbt_oidlt(const void *a, const void *b)
00057 {
00058 return (*((const Oid *) a) < *((const Oid *) b));
00059 }
00060
00061 static int
00062 gbt_oidkey_cmp(const void *a, const void *b)
00063 {
00064 oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
00065 oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
00066
00067 if (ia->lower == ib->lower)
00068 {
00069 if (ia->upper == ib->upper)
00070 return 0;
00071
00072 return (ia->upper > ib->upper) ? 1 : -1;
00073 }
00074
00075 return (ia->lower > ib->lower) ? 1 : -1;
00076 }
00077
00078 static float8
00079 gbt_oid_dist(const void *a, const void *b)
00080 {
00081 Oid aa = *(const Oid *) a;
00082 Oid bb = *(const Oid *) b;
00083
00084 if (aa < bb)
00085 return (float8) (bb - aa);
00086 else
00087 return (float8) (aa - bb);
00088 }
00089
00090
00091 static const gbtree_ninfo tinfo =
00092 {
00093 gbt_t_oid,
00094 sizeof(Oid),
00095 gbt_oidgt,
00096 gbt_oidge,
00097 gbt_oideq,
00098 gbt_oidle,
00099 gbt_oidlt,
00100 gbt_oidkey_cmp,
00101 gbt_oid_dist
00102 };
00103
00104
00105 PG_FUNCTION_INFO_V1(oid_dist);
00106 Datum oid_dist(PG_FUNCTION_ARGS);
00107 Datum
00108 oid_dist(PG_FUNCTION_ARGS)
00109 {
00110 Oid a = PG_GETARG_OID(0);
00111 Oid b = PG_GETARG_OID(1);
00112 Oid res;
00113
00114 if (a < b)
00115 res = b - a;
00116 else
00117 res = a - b;
00118 PG_RETURN_OID(res);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127 Datum
00128 gbt_oid_compress(PG_FUNCTION_ARGS)
00129 {
00130 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00131 GISTENTRY *retval = NULL;
00132
00133 PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
00134 }
00135
00136
00137 Datum
00138 gbt_oid_consistent(PG_FUNCTION_ARGS)
00139 {
00140 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00141 Oid query = PG_GETARG_OID(1);
00142 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
00143
00144
00145 bool *recheck = (bool *) PG_GETARG_POINTER(4);
00146 oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
00147 GBT_NUMKEY_R key;
00148
00149
00150 *recheck = false;
00151
00152 key.lower = (GBT_NUMKEY *) &kkk->lower;
00153 key.upper = (GBT_NUMKEY *) &kkk->upper;
00154
00155 PG_RETURN_BOOL(
00156 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
00157 );
00158 }
00159
00160
00161 Datum
00162 gbt_oid_distance(PG_FUNCTION_ARGS)
00163 {
00164 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
00165 Oid query = PG_GETARG_OID(1);
00166
00167
00168 oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
00169 GBT_NUMKEY_R key;
00170
00171 key.lower = (GBT_NUMKEY *) &kkk->lower;
00172 key.upper = (GBT_NUMKEY *) &kkk->upper;
00173
00174 PG_RETURN_FLOAT8(
00175 gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo)
00176 );
00177 }
00178
00179
00180 Datum
00181 gbt_oid_union(PG_FUNCTION_ARGS)
00182 {
00183 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
00184 void *out = palloc(sizeof(oidKEY));
00185
00186 *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
00187 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
00188 }
00189
00190
00191 Datum
00192 gbt_oid_penalty(PG_FUNCTION_ARGS)
00193 {
00194 oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
00195 oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
00196 float *result = (float *) PG_GETARG_POINTER(2);
00197
00198 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
00199
00200 PG_RETURN_POINTER(result);
00201 }
00202
00203 Datum
00204 gbt_oid_picksplit(PG_FUNCTION_ARGS)
00205 {
00206 PG_RETURN_POINTER(gbt_num_picksplit(
00207 (GistEntryVector *) PG_GETARG_POINTER(0),
00208 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
00209 &tinfo
00210 ));
00211 }
00212
00213 Datum
00214 gbt_oid_same(PG_FUNCTION_ARGS)
00215 {
00216 oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
00217 oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
00218 bool *result = (bool *) PG_GETARG_POINTER(2);
00219
00220 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
00221 PG_RETURN_POINTER(result);
00222 }