|
| 1 | +/* |
| 2 | + * contrib/hstore/hstore.h |
| 3 | + */ |
| 4 | +#ifndef__HSTORE_H__ |
| 5 | +#define__HSTORE_H__ |
| 6 | + |
| 7 | +#include"fmgr.h" |
| 8 | +#include"utils/array.h" |
| 9 | + |
| 10 | + |
| 11 | +/* |
| 12 | + * HEntry: there is one of these for each key _and_ value in an hstore |
| 13 | + * |
| 14 | + * the position offset points to the _end_ so that we can get the length |
| 15 | + * by subtraction from the previous entry.the ISFIRST flag lets us tell |
| 16 | + * whether there is a previous entry. |
| 17 | + */ |
| 18 | +typedefstruct |
| 19 | +{ |
| 20 | +uint32entry; |
| 21 | +}HEntry; |
| 22 | + |
| 23 | +#defineHENTRY_ISFIRST 0x80000000 |
| 24 | +#defineHENTRY_ISNULL 0x40000000 |
| 25 | +#defineHENTRY_POSMASK 0x3FFFFFFF |
| 26 | + |
| 27 | +/* note possible multiple evaluations, also access to prior array element */ |
| 28 | +#defineHSE_ISFIRST(he_) (((he_).entry & HENTRY_ISFIRST) != 0) |
| 29 | +#defineHSE_ISNULL(he_) (((he_).entry & HENTRY_ISNULL) != 0) |
| 30 | +#defineHSE_ENDPOS(he_) ((he_).entry & HENTRY_POSMASK) |
| 31 | +#defineHSE_OFF(he_) (HSE_ISFIRST(he_) ? 0 : HSE_ENDPOS((&(he_))[-1])) |
| 32 | +#defineHSE_LEN(he_) (HSE_ISFIRST(he_)\ |
| 33 | + ? HSE_ENDPOS(he_) \ |
| 34 | + : HSE_ENDPOS(he_) - HSE_ENDPOS((&(he_))[-1])) |
| 35 | + |
| 36 | +/* |
| 37 | + * determined by the size of "endpos" (ie HENTRY_POSMASK), though this is a |
| 38 | + * bit academic since currently varlenas (and hence both the input and the |
| 39 | + * whole hstore) have the same limit |
| 40 | + */ |
| 41 | +#defineHSTORE_MAX_KEY_LEN 0x3FFFFFFF |
| 42 | +#defineHSTORE_MAX_VALUE_LEN 0x3FFFFFFF |
| 43 | + |
| 44 | +typedefstruct |
| 45 | +{ |
| 46 | +int32vl_len_;/* varlena header (do not touch directly!) */ |
| 47 | +uint32size_;/* flags and number of items in hstore */ |
| 48 | +/* array of HEntry follows */ |
| 49 | +}HStore; |
| 50 | + |
| 51 | +/* |
| 52 | + * It's not possible to get more than 2^28 items into an hstore, so we reserve |
| 53 | + * the top few bits of the size field. See hstore_compat.c for one reason |
| 54 | + * why. Some bits are left for future use here. MaxAllocSize makes the |
| 55 | + * practical count limit slightly more than 2^28 / 3, or INT_MAX / 24, the |
| 56 | + * limit for an hstore full of 4-byte keys and null values. Therefore, we |
| 57 | + * don't explicitly check the format-imposed limit. |
| 58 | + */ |
| 59 | +#defineHS_FLAG_NEWVERSION 0x80000000 |
| 60 | + |
| 61 | +#defineHS_COUNT(hsp_) ((hsp_)->size_ & 0x0FFFFFFF) |
| 62 | +#defineHS_SETCOUNT(hsp_,c_) ((hsp_)->size_ = (c_) | HS_FLAG_NEWVERSION) |
| 63 | + |
| 64 | + |
| 65 | +/* |
| 66 | + * "x" comes from an existing HS_COUNT() (as discussed, <= INT_MAX/24) or a |
| 67 | + * Pairs array length (due to MaxAllocSize, <= INT_MAX/40). "lenstr" is no |
| 68 | + * more than INT_MAX, that extreme case arising in hstore_from_arrays(). |
| 69 | + * Therefore, this calculation is limited to about INT_MAX / 5 + INT_MAX. |
| 70 | + */ |
| 71 | +#defineHSHRDSIZE(sizeof(HStore)) |
| 72 | +#defineCALCDATASIZE(x,lenstr) ( (x) * 2 * sizeof(HEntry) + HSHRDSIZE + (lenstr) ) |
| 73 | + |
| 74 | +/* note multiple evaluations of x */ |
| 75 | +#defineARRPTR(x)( (HEntry*) ( (HStore*)(x) + 1 ) ) |
| 76 | +#defineSTRPTR(x)( (char*)(ARRPTR(x) + HS_COUNT((HStore*)(x)) * 2) ) |
| 77 | + |
| 78 | +/* note multiple/non evaluations */ |
| 79 | +#defineHS_KEY(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)])) |
| 80 | +#defineHS_VAL(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)+1])) |
| 81 | +#defineHS_KEYLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)])) |
| 82 | +#defineHS_VALLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)+1])) |
| 83 | +#defineHS_VALISNULL(arr_,i_) (HSE_ISNULL((arr_)[2*(i_)+1])) |
| 84 | + |
| 85 | +/* |
| 86 | + * currently, these following macros are the _only_ places that rely |
| 87 | + * on internal knowledge of HEntry. Everything else should be using |
| 88 | + * the above macros. Exception: the in-place upgrade in hstore_compat.c |
| 89 | + * messes with entries directly. |
| 90 | + */ |
| 91 | + |
| 92 | +/* |
| 93 | + * copy one key/value pair (which must be contiguous starting at |
| 94 | + * sptr_) into an under-construction hstore; dent_ is an HEntry*, |
| 95 | + * dbuf_ is the destination's string buffer, dptr_ is the current |
| 96 | + * position in the destination. lots of modification and multiple |
| 97 | + * evaluation here. |
| 98 | + */ |
| 99 | +#defineHS_COPYITEM(dent_,dbuf_,dptr_,sptr_,klen_,vlen_,vnull_)\ |
| 100 | +do {\ |
| 101 | +memcpy((dptr_), (sptr_), (klen_)+(vlen_));\ |
| 102 | +(dptr_) += (klen_)+(vlen_);\ |
| 103 | +(dent_)++->entry = ((dptr_) - (dbuf_) - (vlen_)) & HENTRY_POSMASK; \ |
| 104 | +(dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK)\ |
| 105 | + | ((vnull_) ? HENTRY_ISNULL : 0));\ |
| 106 | +} while(0) |
| 107 | + |
| 108 | +/* |
| 109 | + * add one key/item pair, from a Pairs structure, into an |
| 110 | + * under-construction hstore |
| 111 | + */ |
| 112 | +#defineHS_ADDITEM(dent_,dbuf_,dptr_,pair_)\ |
| 113 | +do {\ |
| 114 | +memcpy((dptr_), (pair_).key, (pair_).keylen);\ |
| 115 | +(dptr_) += (pair_).keylen;\ |
| 116 | +(dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK;\ |
| 117 | +if ((pair_).isnull)\ |
| 118 | +(dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK)\ |
| 119 | + | HENTRY_ISNULL);\ |
| 120 | +else\ |
| 121 | +{\ |
| 122 | +memcpy((dptr_), (pair_).val, (pair_).vallen);\ |
| 123 | +(dptr_) += (pair_).vallen;\ |
| 124 | +(dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK;\ |
| 125 | +}\ |
| 126 | +} while (0) |
| 127 | + |
| 128 | +/* finalize a newly-constructed hstore */ |
| 129 | +#defineHS_FINALIZE(hsp_,count_,buf_,ptr_)\ |
| 130 | +do {\ |
| 131 | +int buflen = (ptr_) - (buf_);\ |
| 132 | +if ((count_))\ |
| 133 | +ARRPTR(hsp_)[0].entry |= HENTRY_ISFIRST;\ |
| 134 | +if ((count_) != HS_COUNT((hsp_)))\ |
| 135 | +{\ |
| 136 | +HS_SETCOUNT((hsp_),(count_));\ |
| 137 | +memmove(STRPTR(hsp_), (buf_), buflen);\ |
| 138 | +}\ |
| 139 | +SET_VARSIZE((hsp_), CALCDATASIZE((count_), buflen));\ |
| 140 | +} while (0) |
| 141 | + |
| 142 | +/* ensure the varlena size of an existing hstore is correct */ |
| 143 | +#defineHS_FIXSIZE(hsp_,count_)\ |
| 144 | +do {\ |
| 145 | +int bl = (count_) ? HSE_ENDPOS(ARRPTR(hsp_)[2*(count_)-1]) : 0; \ |
| 146 | +SET_VARSIZE((hsp_), CALCDATASIZE((count_),bl));\ |
| 147 | +} while (0) |
| 148 | + |
| 149 | +/* DatumGetHStoreP includes support for reading old-format hstore values */ |
| 150 | +externHStore*hstoreUpgrade(Datumorig); |
| 151 | + |
| 152 | +#defineDatumGetHStoreP(d) hstoreUpgrade(d) |
| 153 | + |
| 154 | +#definePG_GETARG_HS(x) DatumGetHStoreP(PG_GETARG_DATUM(x)) |
| 155 | + |
| 156 | + |
| 157 | +/* |
| 158 | + * Pairs is a "decompressed" representation of one key/value pair. |
| 159 | + * The two strings are not necessarily null-terminated. |
| 160 | + */ |
| 161 | +typedefstruct |
| 162 | +{ |
| 163 | +char*key; |
| 164 | +char*val; |
| 165 | +size_tkeylen; |
| 166 | +size_tvallen; |
| 167 | +boolisnull;/* value is null? */ |
| 168 | +boolneedfree;/* need to pfree the value? */ |
| 169 | +}Pairs; |
| 170 | + |
| 171 | +externinthstoreUniquePairs(Pairs*a,int32l,int32*buflen); |
| 172 | +externHStore*hstorePairs(Pairs*pairs,int32pcount,int32buflen); |
| 173 | + |
| 174 | +externsize_thstoreCheckKeyLen(size_tlen); |
| 175 | +externsize_thstoreCheckValLen(size_tlen); |
| 176 | + |
| 177 | +externinthstoreFindKey(HStore*hs,int*lowbound,char*key,intkeylen); |
| 178 | +externPairs*hstoreArrayToPairs(ArrayType*a,int*npairs); |
| 179 | + |
| 180 | +#defineHStoreContainsStrategyNumber7 |
| 181 | +#defineHStoreExistsStrategyNumber9 |
| 182 | +#defineHStoreExistsAnyStrategyNumber10 |
| 183 | +#defineHStoreExistsAllStrategyNumber11 |
| 184 | +#defineHStoreOldContainsStrategyNumber 13/* backwards compatibility */ |
| 185 | + |
| 186 | +/* |
| 187 | + * defining HSTORE_POLLUTE_NAMESPACE=0 will prevent use of old function names; |
| 188 | + * for now, we default to on for the benefit of people restoring old dumps |
| 189 | + */ |
| 190 | +#ifndefHSTORE_POLLUTE_NAMESPACE |
| 191 | +#defineHSTORE_POLLUTE_NAMESPACE 1 |
| 192 | +#endif |
| 193 | + |
| 194 | +#ifHSTORE_POLLUTE_NAMESPACE |
| 195 | +#defineHSTORE_POLLUTE(newname_,oldname_) \ |
| 196 | +PG_FUNCTION_INFO_V1(oldname_); \ |
| 197 | +Datum oldname_(PG_FUNCTION_ARGS); \ |
| 198 | +Datum newname_(PG_FUNCTION_ARGS); \ |
| 199 | +Datum oldname_(PG_FUNCTION_ARGS) { return newname_(fcinfo); } \ |
| 200 | +extern int no_such_variable |
| 201 | +#else |
| 202 | +#defineHSTORE_POLLUTE(newname_,oldname_) \ |
| 203 | +extern int no_such_variable |
| 204 | +#endif |
| 205 | + |
| 206 | +#endif/* __HSTORE_H__ */ |