2008-05-12 08:00:54 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/hstore/hstore.h
|
2008-05-12 08:00:54 +08:00
|
|
|
*/
|
2006-09-06 02:00:58 +08:00
|
|
|
#ifndef __HSTORE_H__
|
|
|
|
#define __HSTORE_H__
|
|
|
|
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "fmgr.h"
|
2009-10-01 03:50:22 +08:00
|
|
|
#include "utils/array.h"
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
/*
|
|
|
|
* HEntry: there is one of these for each key _and_ value in an hstore
|
|
|
|
*
|
|
|
|
* the position offset points to the _end_ so that we can get the length
|
|
|
|
* by subtraction from the previous entry. the ISFIRST flag lets us tell
|
|
|
|
* whether there is a previous entry.
|
|
|
|
*/
|
2006-09-06 02:00:58 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
uint32 entry;
|
2006-09-06 02:00:58 +08:00
|
|
|
} HEntry;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
#define HENTRY_ISFIRST 0x80000000
|
|
|
|
#define HENTRY_ISNULL 0x40000000
|
|
|
|
#define HENTRY_POSMASK 0x3FFFFFFF
|
2009-03-16 06:05:17 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
/* note possible multiple evaluations, also access to prior array element */
|
|
|
|
#define HSE_ISFIRST(he_) (((he_).entry & HENTRY_ISFIRST) != 0)
|
|
|
|
#define HSE_ISNULL(he_) (((he_).entry & HENTRY_ISNULL) != 0)
|
|
|
|
#define HSE_ENDPOS(he_) ((he_).entry & HENTRY_POSMASK)
|
|
|
|
#define HSE_OFF(he_) (HSE_ISFIRST(he_) ? 0 : HSE_ENDPOS((&(he_))[-1]))
|
|
|
|
#define HSE_LEN(he_) (HSE_ISFIRST(he_) \
|
|
|
|
? HSE_ENDPOS(he_) \
|
|
|
|
: HSE_ENDPOS(he_) - HSE_ENDPOS((&(he_))[-1]))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* determined by the size of "endpos" (ie HENTRY_POSMASK), though this is a
|
|
|
|
* bit academic since currently varlenas (and hence both the input and the
|
|
|
|
* whole hstore) have the same limit
|
|
|
|
*/
|
|
|
|
#define HSTORE_MAX_KEY_LEN 0x3FFFFFFF
|
|
|
|
#define HSTORE_MAX_VALUE_LEN 0x3FFFFFFF
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2009-10-01 03:50:22 +08:00
|
|
|
uint32 size_; /* flags and number of items in hstore */
|
|
|
|
/* array of HEntry follows */
|
2006-09-06 02:00:58 +08:00
|
|
|
} HStore;
|
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
/*
|
2014-02-17 22:33:31 +08:00
|
|
|
* It's not possible to get more than 2^28 items into an hstore, so we reserve
|
|
|
|
* the top few bits of the size field. See hstore_compat.c for one reason
|
|
|
|
* why. Some bits are left for future use here. MaxAllocSize makes the
|
|
|
|
* practical count limit slightly more than 2^28 / 3, or INT_MAX / 24, the
|
|
|
|
* limit for an hstore full of 4-byte keys and null values. Therefore, we
|
|
|
|
* don't explicitly check the format-imposed limit.
|
2009-10-01 03:50:22 +08:00
|
|
|
*/
|
|
|
|
#define HS_FLAG_NEWVERSION 0x80000000
|
|
|
|
|
|
|
|
#define HS_COUNT(hsp_) ((hsp_)->size_ & 0x0FFFFFFF)
|
|
|
|
#define HS_SETCOUNT(hsp_,c_) ((hsp_)->size_ = (c_) | HS_FLAG_NEWVERSION)
|
|
|
|
|
|
|
|
|
2014-02-17 22:33:31 +08:00
|
|
|
/*
|
|
|
|
* "x" comes from an existing HS_COUNT() (as discussed, <= INT_MAX/24) or a
|
|
|
|
* Pairs array length (due to MaxAllocSize, <= INT_MAX/40). "lenstr" is no
|
|
|
|
* more than INT_MAX, that extreme case arising in hstore_from_arrays().
|
|
|
|
* Therefore, this calculation is limited to about INT_MAX / 5 + INT_MAX.
|
|
|
|
*/
|
2009-10-01 03:50:22 +08:00
|
|
|
#define HSHRDSIZE (sizeof(HStore))
|
|
|
|
#define CALCDATASIZE(x, lenstr) ( (x) * 2 * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
|
|
|
|
|
|
|
|
/* note multiple evaluations of x */
|
|
|
|
#define ARRPTR(x) ( (HEntry*) ( (HStore*)(x) + 1 ) )
|
|
|
|
#define STRPTR(x) ( (char*)(ARRPTR(x) + HS_COUNT((HStore*)(x)) * 2) )
|
|
|
|
|
|
|
|
/* note multiple/non evaluations */
|
2015-11-20 03:54:05 +08:00
|
|
|
#define HSTORE_KEY(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)]))
|
|
|
|
#define HSTORE_VAL(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)+1]))
|
|
|
|
#define HSTORE_KEYLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)]))
|
|
|
|
#define HSTORE_VALLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)+1]))
|
|
|
|
#define HSTORE_VALISNULL(arr_,i_) (HSE_ISNULL((arr_)[2*(i_)+1]))
|
2009-10-01 03:50:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* currently, these following macros are the _only_ places that rely
|
|
|
|
* on internal knowledge of HEntry. Everything else should be using
|
|
|
|
* the above macros. Exception: the in-place upgrade in hstore_compat.c
|
|
|
|
* messes with entries directly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy one key/value pair (which must be contiguous starting at
|
|
|
|
* sptr_) into an under-construction hstore; dent_ is an HEntry*,
|
|
|
|
* dbuf_ is the destination's string buffer, dptr_ is the current
|
|
|
|
* position in the destination. lots of modification and multiple
|
|
|
|
* evaluation here.
|
|
|
|
*/
|
|
|
|
#define HS_COPYITEM(dent_,dbuf_,dptr_,sptr_,klen_,vlen_,vnull_) \
|
|
|
|
do { \
|
|
|
|
memcpy((dptr_), (sptr_), (klen_)+(vlen_)); \
|
|
|
|
(dptr_) += (klen_)+(vlen_); \
|
|
|
|
(dent_)++->entry = ((dptr_) - (dbuf_) - (vlen_)) & HENTRY_POSMASK; \
|
|
|
|
(dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK) \
|
|
|
|
| ((vnull_) ? HENTRY_ISNULL : 0)); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add one key/item pair, from a Pairs structure, into an
|
|
|
|
* under-construction hstore
|
|
|
|
*/
|
|
|
|
#define HS_ADDITEM(dent_,dbuf_,dptr_,pair_) \
|
|
|
|
do { \
|
|
|
|
memcpy((dptr_), (pair_).key, (pair_).keylen); \
|
|
|
|
(dptr_) += (pair_).keylen; \
|
|
|
|
(dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK; \
|
|
|
|
if ((pair_).isnull) \
|
|
|
|
(dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK) \
|
|
|
|
| HENTRY_ISNULL); \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
memcpy((dptr_), (pair_).val, (pair_).vallen); \
|
|
|
|
(dptr_) += (pair_).vallen; \
|
|
|
|
(dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* finalize a newly-constructed hstore */
|
|
|
|
#define HS_FINALIZE(hsp_,count_,buf_,ptr_) \
|
|
|
|
do { \
|
|
|
|
int buflen = (ptr_) - (buf_); \
|
|
|
|
if ((count_)) \
|
|
|
|
ARRPTR(hsp_)[0].entry |= HENTRY_ISFIRST; \
|
|
|
|
if ((count_) != HS_COUNT((hsp_))) \
|
|
|
|
{ \
|
|
|
|
HS_SETCOUNT((hsp_),(count_)); \
|
|
|
|
memmove(STRPTR(hsp_), (buf_), buflen); \
|
|
|
|
} \
|
|
|
|
SET_VARSIZE((hsp_), CALCDATASIZE((count_), buflen)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* ensure the varlena size of an existing hstore is correct */
|
|
|
|
#define HS_FIXSIZE(hsp_,count_) \
|
|
|
|
do { \
|
|
|
|
int bl = (count_) ? HSE_ENDPOS(ARRPTR(hsp_)[2*(count_)-1]) : 0; \
|
|
|
|
SET_VARSIZE((hsp_), CALCDATASIZE((count_),bl)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* DatumGetHStoreP includes support for reading old-format hstore values */
|
|
|
|
extern HStore *hstoreUpgrade(Datum orig);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
#define DatumGetHStoreP(d) hstoreUpgrade(d)
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2017-09-19 03:21:23 +08:00
|
|
|
#define PG_GETARG_HSTORE_P(x) DatumGetHStoreP(PG_GETARG_DATUM(x))
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pairs is a "decompressed" representation of one key/value pair.
|
|
|
|
* The two strings are not necessarily null-terminated.
|
|
|
|
*/
|
2006-09-06 02:00:58 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *val;
|
2009-10-01 03:50:22 +08:00
|
|
|
size_t keylen;
|
|
|
|
size_t vallen;
|
|
|
|
bool isnull; /* value is null? */
|
|
|
|
bool needfree; /* need to pfree the value? */
|
2006-09-06 02:00:58 +08:00
|
|
|
} Pairs;
|
|
|
|
|
2012-06-25 06:51:46 +08:00
|
|
|
extern int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen);
|
|
|
|
extern HStore *hstorePairs(Pairs *pairs, int32 pcount, int32 buflen);
|
2009-10-01 03:50:22 +08:00
|
|
|
|
|
|
|
extern size_t hstoreCheckKeyLen(size_t len);
|
|
|
|
extern size_t hstoreCheckValLen(size_t len);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
extern int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen);
|
|
|
|
extern Pairs *hstoreArrayToPairs(ArrayType *a, int *npairs);
|
2009-03-16 06:05:17 +08:00
|
|
|
|
2007-03-14 22:21:53 +08:00
|
|
|
#define HStoreContainsStrategyNumber 7
|
|
|
|
#define HStoreExistsStrategyNumber 9
|
2009-10-01 03:50:22 +08:00
|
|
|
#define HStoreExistsAnyStrategyNumber 10
|
|
|
|
#define HStoreExistsAllStrategyNumber 11
|
|
|
|
#define HStoreOldContainsStrategyNumber 13 /* backwards compatibility */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* defining HSTORE_POLLUTE_NAMESPACE=0 will prevent use of old function names;
|
|
|
|
* for now, we default to on for the benefit of people restoring old dumps
|
|
|
|
*/
|
|
|
|
#ifndef HSTORE_POLLUTE_NAMESPACE
|
|
|
|
#define HSTORE_POLLUTE_NAMESPACE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HSTORE_POLLUTE_NAMESPACE
|
|
|
|
#define HSTORE_POLLUTE(newname_,oldname_) \
|
|
|
|
PG_FUNCTION_INFO_V1(oldname_); \
|
2016-11-07 23:19:22 +08:00
|
|
|
Datum newname_(PG_FUNCTION_ARGS); \
|
2009-10-01 03:50:22 +08:00
|
|
|
Datum oldname_(PG_FUNCTION_ARGS) { return newname_(fcinfo); } \
|
|
|
|
extern int no_such_variable
|
|
|
|
#else
|
|
|
|
#define HSTORE_POLLUTE(newname_,oldname_) \
|
|
|
|
extern int no_such_variable
|
|
|
|
#endif
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2008-05-12 08:00:54 +08:00
|
|
|
#endif /* __HSTORE_H__ */
|