PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hstore_op.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "hstore.h"
Include dependency graph for hstore_op.c:

Go to the source code of this file.

Functions

 HSTORE_POLLUTE (hstore_fetchval, fetchval)
 
 HSTORE_POLLUTE (hstore_exists, exists)
 
 HSTORE_POLLUTE (hstore_defined, defined)
 
 HSTORE_POLLUTE (hstore_delete, delete)
 
 HSTORE_POLLUTE (hstore_concat, hs_concat)
 
 HSTORE_POLLUTE (hstore_contains, hs_contains)
 
 HSTORE_POLLUTE (hstore_contained, hs_contained)
 
 HSTORE_POLLUTE (hstore_akeys, akeys)
 
 HSTORE_POLLUTE (hstore_avals, avals)
 
 HSTORE_POLLUTE (hstore_skeys, skeys)
 
 HSTORE_POLLUTE (hstore_svals, svals)
 
 HSTORE_POLLUTE (hstore_each, each)
 
int hstoreFindKey (HStore *hs, int *lowbound, char *key, int keylen)
 
PairshstoreArrayToPairs (ArrayType *a, int *npairs)
 
 PG_FUNCTION_INFO_V1 (hstore_fetchval)
 
Datum hstore_fetchval (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_exists)
 
Datum hstore_exists (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_exists_any)
 
Datum hstore_exists_any (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_exists_all)
 
Datum hstore_exists_all (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_defined)
 
Datum hstore_defined (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_delete)
 
Datum hstore_delete (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_delete_array)
 
Datum hstore_delete_array (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_delete_hstore)
 
Datum hstore_delete_hstore (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_concat)
 
Datum hstore_concat (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_slice_to_array)
 
Datum hstore_slice_to_array (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_slice_to_hstore)
 
Datum hstore_slice_to_hstore (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_akeys)
 
Datum hstore_akeys (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_avals)
 
Datum hstore_avals (PG_FUNCTION_ARGS)
 
static ArrayTypehstore_to_array_internal (HStore *hs, int ndims)
 
 PG_FUNCTION_INFO_V1 (hstore_to_array)
 
Datum hstore_to_array (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_to_matrix)
 
Datum hstore_to_matrix (PG_FUNCTION_ARGS)
 
static void setup_firstcall (FuncCallContext *funcctx, HStore *hs, FunctionCallInfoData *fcinfo)
 
 PG_FUNCTION_INFO_V1 (hstore_skeys)
 
Datum hstore_skeys (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_svals)
 
Datum hstore_svals (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_contains)
 
Datum hstore_contains (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_contained)
 
Datum hstore_contained (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_each)
 
Datum hstore_each (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_cmp)
 
Datum hstore_cmp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_eq)
 
Datum hstore_eq (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_ne)
 
Datum hstore_ne (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_gt)
 
Datum hstore_gt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_ge)
 
Datum hstore_ge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_lt)
 
Datum hstore_lt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_le)
 
Datum hstore_le (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_hash)
 
Datum hstore_hash (PG_FUNCTION_ARGS)
 

Function Documentation

Datum hstore_akeys ( PG_FUNCTION_ARGS  )

Definition at line 697 of file hstore_op.c.

References ARRPTR, construct_array(), construct_empty_array(), cstring_to_text_with_len(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, i, palloc(), PG_GETARG_HS, PG_RETURN_POINTER, PointerGetDatum, STRPTR, and TEXTOID.

698 {
699  HStore *hs = PG_GETARG_HS(0);
700  Datum *d;
701  ArrayType *a;
702  HEntry *entries = ARRPTR(hs);
703  char *base = STRPTR(hs);
704  int count = HS_COUNT(hs);
705  int i;
706 
707  if (count == 0)
708  {
711  }
712 
713  d = (Datum *) palloc(sizeof(Datum) * count);
714 
715  for (i = 0; i < count; ++i)
716  {
717  text *t = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
718  HSTORE_KEYLEN(entries, i));
719 
720  d[i] = PointerGetDatum(t);
721  }
722 
723  a = construct_array(d, count,
724  TEXTOID, -1, false, 'i');
725 
727 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3424
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
uintptr_t Datum
Definition: postgres.h:374
Definition: hstore.h:18
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
Datum hstore_avals ( PG_FUNCTION_ARGS  )

Definition at line 732 of file hstore_op.c.

References ARRPTR, construct_empty_array(), construct_md_array(), cstring_to_text_with_len(), HS_COUNT, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, palloc(), PG_GETARG_HS, PG_RETURN_POINTER, PointerGetDatum, STRPTR, and TEXTOID.

733 {
734  HStore *hs = PG_GETARG_HS(0);
735  Datum *d;
736  bool *nulls;
737  ArrayType *a;
738  HEntry *entries = ARRPTR(hs);
739  char *base = STRPTR(hs);
740  int count = HS_COUNT(hs);
741  int lb = 1;
742  int i;
743 
744  if (count == 0)
745  {
748  }
749 
750  d = (Datum *) palloc(sizeof(Datum) * count);
751  nulls = (bool *) palloc(sizeof(bool) * count);
752 
753  for (i = 0; i < count; ++i)
754  {
755  if (HSTORE_VALISNULL(entries, i))
756  {
757  d[i] = (Datum) 0;
758  nulls[i] = true;
759  }
760  else
761  {
762  text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
763  HSTORE_VALLEN(entries, i));
764 
765  d[i] = PointerGetDatum(item);
766  nulls[i] = false;
767  }
768  }
769 
770  a = construct_md_array(d, nulls, 1, &count, &lb,
771  TEXTOID, -1, false, 'i');
772 
774 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3424
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
uintptr_t Datum
Definition: postgres.h:374
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3340
Datum hstore_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1088 of file hstore_op.c.

References ARRPTR, HS_COUNT, HSE_ENDPOS, HSE_ISNULL, i, Min, PG_FREE_IF_COPY, PG_GETARG_HS, PG_RETURN_INT32, and STRPTR.

Referenced by hstore_eq(), hstore_ge(), hstore_gt(), hstore_le(), hstore_lt(), and hstore_ne().

1089 {
1090  HStore *hs1 = PG_GETARG_HS(0);
1091  HStore *hs2 = PG_GETARG_HS(1);
1092  int hcount1 = HS_COUNT(hs1);
1093  int hcount2 = HS_COUNT(hs2);
1094  int res = 0;
1095 
1096  if (hcount1 == 0 || hcount2 == 0)
1097  {
1098  /*
1099  * if either operand is empty, and the other is nonempty, the nonempty
1100  * one is larger. If both are empty they are equal.
1101  */
1102  if (hcount1 > 0)
1103  res = 1;
1104  else if (hcount2 > 0)
1105  res = -1;
1106  }
1107  else
1108  {
1109  /* here we know both operands are nonempty */
1110  char *str1 = STRPTR(hs1);
1111  char *str2 = STRPTR(hs2);
1112  HEntry *ent1 = ARRPTR(hs1);
1113  HEntry *ent2 = ARRPTR(hs2);
1114  size_t len1 = HSE_ENDPOS(ent1[2 * hcount1 - 1]);
1115  size_t len2 = HSE_ENDPOS(ent2[2 * hcount2 - 1]);
1116 
1117  res = memcmp(str1, str2, Min(len1, len2));
1118 
1119  if (res == 0)
1120  {
1121  if (len1 > len2)
1122  res = 1;
1123  else if (len1 < len2)
1124  res = -1;
1125  else if (hcount1 > hcount2)
1126  res = 1;
1127  else if (hcount2 > hcount1)
1128  res = -1;
1129  else
1130  {
1131  int count = hcount1 * 2;
1132  int i;
1133 
1134  for (i = 0; i < count; ++i)
1135  if (HSE_ENDPOS(ent1[i]) != HSE_ENDPOS(ent2[i]) ||
1136  HSE_ISNULL(ent1[i]) != HSE_ISNULL(ent2[i]))
1137  break;
1138  if (i < count)
1139  {
1140  if (HSE_ENDPOS(ent1[i]) < HSE_ENDPOS(ent2[i]))
1141  res = -1;
1142  else if (HSE_ENDPOS(ent1[i]) > HSE_ENDPOS(ent2[i]))
1143  res = 1;
1144  else if (HSE_ISNULL(ent1[i]))
1145  res = 1;
1146  else if (HSE_ISNULL(ent2[i]))
1147  res = -1;
1148  }
1149  }
1150  }
1151  else
1152  {
1153  res = (res > 0) ? 1 : -1;
1154  }
1155  }
1156 
1157  /*
1158  * this is a btree support function; this is one of the few places where
1159  * memory needs to be explicitly freed.
1160  */
1161  PG_FREE_IF_COPY(hs1, 0);
1162  PG_FREE_IF_COPY(hs2, 1);
1163  PG_RETURN_INT32(res);
1164 }
#define HSE_ENDPOS(he_)
Definition: hstore.h:30
Definition: hstore.h:44
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define HSE_ISNULL(he_)
Definition: hstore.h:29
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: hstore.h:18
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26
Datum hstore_concat ( PG_FUNCTION_ARGS  )

Definition at line 474 of file hstore_op.c.

References ARRPTR, difference(), HS_COPYITEM, HS_COUNT, HS_FINALIZE, HS_FIXSIZE, HS_SETCOUNT, HSHRDSIZE, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VALISNULL, HSTORE_VALLEN, palloc(), PG_GETARG_HS, PG_RETURN_POINTER, s1, s2, SET_VARSIZE, STRPTR, and VARSIZE.

475 {
476  HStore *s1 = PG_GETARG_HS(0);
477  HStore *s2 = PG_GETARG_HS(1);
478  HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
479  char *ps1,
480  *ps2,
481  *bufd,
482  *pd;
483  HEntry *es1,
484  *es2,
485  *ed;
486  int s1idx;
487  int s2idx;
488  int s1count = HS_COUNT(s1);
489  int s2count = HS_COUNT(s2);
490  int outcount = 0;
491 
492  SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2) - HSHRDSIZE);
493  HS_SETCOUNT(out, s1count + s2count);
494 
495  if (s1count == 0)
496  {
497  /* return a copy of the input, unchanged */
498  memcpy(out, s2, VARSIZE(s2));
499  HS_FIXSIZE(out, s2count);
500  HS_SETCOUNT(out, s2count);
501  PG_RETURN_POINTER(out);
502  }
503 
504  if (s2count == 0)
505  {
506  /* return a copy of the input, unchanged */
507  memcpy(out, s1, VARSIZE(s1));
508  HS_FIXSIZE(out, s1count);
509  HS_SETCOUNT(out, s1count);
510  PG_RETURN_POINTER(out);
511  }
512 
513  ps1 = STRPTR(s1);
514  ps2 = STRPTR(s2);
515  bufd = pd = STRPTR(out);
516  es1 = ARRPTR(s1);
517  es2 = ARRPTR(s2);
518  ed = ARRPTR(out);
519 
520  /*
521  * this is in effect a merge between s1 and s2, both of which are already
522  * sorted by (keylen,key); we take s2 for equal keys
523  */
524 
525  for (s1idx = s2idx = 0; s1idx < s1count || s2idx < s2count; ++outcount)
526  {
527  int difference;
528 
529  if (s1idx >= s1count)
530  difference = 1;
531  else if (s2idx >= s2count)
532  difference = -1;
533  else
534  {
535  int s1keylen = HSTORE_KEYLEN(es1, s1idx);
536  int s2keylen = HSTORE_KEYLEN(es2, s2idx);
537 
538  if (s1keylen == s2keylen)
539  difference = memcmp(HSTORE_KEY(es1, ps1, s1idx),
540  HSTORE_KEY(es2, ps2, s2idx),
541  s1keylen);
542  else
543  difference = (s1keylen > s2keylen) ? 1 : -1;
544  }
545 
546  if (difference >= 0)
547  {
548  HS_COPYITEM(ed, bufd, pd,
549  HSTORE_KEY(es2, ps2, s2idx), HSTORE_KEYLEN(es2, s2idx),
550  HSTORE_VALLEN(es2, s2idx), HSTORE_VALISNULL(es2, s2idx));
551  ++s2idx;
552  if (difference == 0)
553  ++s1idx;
554  }
555  else
556  {
557  HS_COPYITEM(ed, bufd, pd,
558  HSTORE_KEY(es1, ps1, s1idx), HSTORE_KEYLEN(es1, s1idx),
559  HSTORE_VALLEN(es1, s1idx), HSTORE_VALISNULL(es1, s1idx));
560  ++s1idx;
561  }
562  }
563 
564  HS_FINALIZE(out, outcount, bufd, pd);
565 
566  PG_RETURN_POINTER(out);
567 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define VARSIZE(PTR)
Definition: postgres.h:306
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
char * s1
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
char * s2
Datum difference(PG_FUNCTION_ARGS)
#define HSHRDSIZE
Definition: hstore.h:71
Definition: hstore.h:18
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
#define HS_COPYITEM(dent_, dbuf_, dptr_, sptr_, klen_, vlen_, vnull_)
Definition: hstore.h:99
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define ARRPTR(x)
Definition: cube.c:26
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62
#define HS_FIXSIZE(hsp_, count_)
Definition: hstore.h:143
Datum hstore_contained ( PG_FUNCTION_ARGS  )

Definition at line 1016 of file hstore_op.c.

References DirectFunctionCall2, hstore_contains(), PG_GETARG_DATUM, and PG_RETURN_DATUM.

1017 {
1019  PG_GETARG_DATUM(1),
1020  PG_GETARG_DATUM(0)
1021  ));
1022 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
Datum hstore_contains(PG_FUNCTION_ARGS)
Definition: hstore_op.c:968
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_contains ( PG_FUNCTION_ARGS  )

Definition at line 968 of file hstore_op.c.

References ARRPTR, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreFindKey(), i, idx(), PG_GETARG_HS, PG_RETURN_BOOL, STRPTR, and val.

Referenced by hstore_contained().

969 {
970  HStore *val = PG_GETARG_HS(0);
971  HStore *tmpl = PG_GETARG_HS(1);
972  bool res = true;
973  HEntry *te = ARRPTR(tmpl);
974  char *tstr = STRPTR(tmpl);
975  HEntry *ve = ARRPTR(val);
976  char *vstr = STRPTR(val);
977  int tcount = HS_COUNT(tmpl);
978  int lastidx = 0;
979  int i;
980 
981  /*
982  * we exploit the fact that keys in "tmpl" are in strictly increasing
983  * order to narrow the hstoreFindKey search; each search can start one
984  * entry past the previous "found" entry, or at the lower bound of the
985  * search
986  */
987 
988  for (i = 0; res && i < tcount; ++i)
989  {
990  int idx = hstoreFindKey(val, &lastidx,
991  HSTORE_KEY(te, tstr, i),
992  HSTORE_KEYLEN(te, i));
993 
994  if (idx >= 0)
995  {
996  bool nullval = HSTORE_VALISNULL(te, i);
997  int vallen = HSTORE_VALLEN(te, i);
998 
999  if (nullval != HSTORE_VALISNULL(ve, idx) ||
1000  (!nullval && (vallen != HSTORE_VALLEN(ve, idx) ||
1001  memcmp(HSTORE_VAL(te, tstr, i),
1002  HSTORE_VAL(ve, vstr, idx),
1003  vallen) != 0)))
1004  res = false;
1005  }
1006  else
1007  res = false;
1008  }
1009 
1010  PG_RETURN_BOOL(res);
1011 }
Definition: hstore.h:44
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26
long val
Definition: informix.c:689
Datum hstore_defined ( PG_FUNCTION_ARGS  )

Definition at line 233 of file hstore_op.c.

References ARRPTR, HSTORE_VALISNULL, hstoreFindKey(), idx(), NULL, PG_GETARG_HS, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

234 {
235  HStore *hs = PG_GETARG_HS(0);
236  text *key = PG_GETARG_TEXT_PP(1);
237  HEntry *entries = ARRPTR(hs);
238  int idx = hstoreFindKey(hs, NULL,
239  VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
240  bool res = (idx >= 0 && !HSTORE_VALISNULL(entries, idx));
241 
242  PG_RETURN_BOOL(res);
243 }
Definition: hstore.h:44
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:226
Definition: hstore.h:18
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
Datum hstore_delete ( PG_FUNCTION_ARGS  )

Definition at line 248 of file hstore_op.c.

References ARRPTR, HS_COPYITEM, HS_COUNT, HS_FINALIZE, HS_SETCOUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VALISNULL, HSTORE_VALLEN, i, palloc(), PG_GETARG_HS, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, SET_VARSIZE, STRPTR, VARDATA_ANY, VARSIZE, and VARSIZE_ANY_EXHDR.

249 {
250  HStore *hs = PG_GETARG_HS(0);
251  text *key = PG_GETARG_TEXT_PP(1);
252  char *keyptr = VARDATA_ANY(key);
253  int keylen = VARSIZE_ANY_EXHDR(key);
254  HStore *out = palloc(VARSIZE(hs));
255  char *bufs,
256  *bufd,
257  *ptrd;
258  HEntry *es,
259  *ed;
260  int i;
261  int count = HS_COUNT(hs);
262  int outcount = 0;
263 
264  SET_VARSIZE(out, VARSIZE(hs));
265  HS_SETCOUNT(out, count); /* temporary! */
266 
267  bufs = STRPTR(hs);
268  es = ARRPTR(hs);
269  bufd = ptrd = STRPTR(out);
270  ed = ARRPTR(out);
271 
272  for (i = 0; i < count; ++i)
273  {
274  int len = HSTORE_KEYLEN(es, i);
275  char *ptrs = HSTORE_KEY(es, bufs, i);
276 
277  if (!(len == keylen && memcmp(ptrs, keyptr, keylen) == 0))
278  {
279  int vallen = HSTORE_VALLEN(es, i);
280 
281  HS_COPYITEM(ed, bufd, ptrd, ptrs, len, vallen,
282  HSTORE_VALISNULL(es, i));
283  ++outcount;
284  }
285  }
286 
287  HS_FINALIZE(out, outcount, bufd, ptrd);
288 
289  PG_RETURN_POINTER(out);
290 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARSIZE(PTR)
Definition: postgres.h:306
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
Definition: hstore.h:18
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define HS_COPYITEM(dent_, dbuf_, dptr_, sptr_, klen_, vlen_, vnull_)
Definition: hstore.h:99
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define ARRPTR(x)
Definition: cube.c:26
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62
Datum hstore_delete_array ( PG_FUNCTION_ARGS  )

Definition at line 295 of file hstore_op.c.

References ARRPTR, difference(), HS_COPYITEM, HS_COUNT, HS_FINALIZE, HS_FIXSIZE, HS_SETCOUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreArrayToPairs(), i, Pairs::keylen, palloc(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_HS, PG_RETURN_POINTER, SET_VARSIZE, STRPTR, and VARSIZE.

296 {
297  HStore *hs = PG_GETARG_HS(0);
298  HStore *out = palloc(VARSIZE(hs));
299  int hs_count = HS_COUNT(hs);
300  char *ps,
301  *bufd,
302  *pd;
303  HEntry *es,
304  *ed;
305  int i,
306  j;
307  int outcount = 0;
308  ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
309  int nkeys;
310  Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
311 
312  SET_VARSIZE(out, VARSIZE(hs));
313  HS_SETCOUNT(out, hs_count); /* temporary! */
314 
315  ps = STRPTR(hs);
316  es = ARRPTR(hs);
317  bufd = pd = STRPTR(out);
318  ed = ARRPTR(out);
319 
320  if (nkeys == 0)
321  {
322  /* return a copy of the input, unchanged */
323  memcpy(out, hs, VARSIZE(hs));
324  HS_FIXSIZE(out, hs_count);
325  HS_SETCOUNT(out, hs_count);
326  PG_RETURN_POINTER(out);
327  }
328 
329  /*
330  * this is in effect a merge between hs and key_pairs, both of which are
331  * already sorted by (keylen,key); we take keys from hs only
332  */
333 
334  for (i = j = 0; i < hs_count;)
335  {
336  int difference;
337 
338  if (j >= nkeys)
339  difference = -1;
340  else
341  {
342  int skeylen = HSTORE_KEYLEN(es, i);
343 
344  if (skeylen == key_pairs[j].keylen)
345  difference = memcmp(HSTORE_KEY(es, ps, i),
346  key_pairs[j].key,
347  key_pairs[j].keylen);
348  else
349  difference = (skeylen > key_pairs[j].keylen) ? 1 : -1;
350  }
351 
352  if (difference > 0)
353  ++j;
354  else if (difference == 0)
355  ++i, ++j;
356  else
357  {
358  HS_COPYITEM(ed, bufd, pd,
359  HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
360  HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
361  ++outcount;
362  ++i;
363  }
364  }
365 
366  HS_FINALIZE(out, outcount, bufd, pd);
367 
368  PG_RETURN_POINTER(out);
369 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define VARSIZE(PTR)
Definition: postgres.h:306
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
Datum difference(PG_FUNCTION_ARGS)
Pairs * hstoreArrayToPairs(ArrayType *a, int *npairs)
Definition: hstore_op.c:74
size_t keylen
Definition: hstore.h:165
Definition: hstore.h:18
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
#define HS_COPYITEM(dent_, dbuf_, dptr_, sptr_, klen_, vlen_, vnull_)
Definition: hstore.h:99
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define ARRPTR(x)
Definition: cube.c:26
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62
#define HS_FIXSIZE(hsp_, count_)
Definition: hstore.h:143
Definition: hstore.h:161
Datum hstore_delete_hstore ( PG_FUNCTION_ARGS  )

Definition at line 374 of file hstore_op.c.

References ARRPTR, difference(), HS_COPYITEM, HS_COUNT, HS_FINALIZE, HS_FIXSIZE, HS_SETCOUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, palloc(), PG_GETARG_HS, PG_RETURN_POINTER, SET_VARSIZE, STRPTR, and VARSIZE.

375 {
376  HStore *hs = PG_GETARG_HS(0);
377  HStore *hs2 = PG_GETARG_HS(1);
378  HStore *out = palloc(VARSIZE(hs));
379  int hs_count = HS_COUNT(hs);
380  int hs2_count = HS_COUNT(hs2);
381  char *ps,
382  *ps2,
383  *bufd,
384  *pd;
385  HEntry *es,
386  *es2,
387  *ed;
388  int i,
389  j;
390  int outcount = 0;
391 
392  SET_VARSIZE(out, VARSIZE(hs));
393  HS_SETCOUNT(out, hs_count); /* temporary! */
394 
395  ps = STRPTR(hs);
396  es = ARRPTR(hs);
397  ps2 = STRPTR(hs2);
398  es2 = ARRPTR(hs2);
399  bufd = pd = STRPTR(out);
400  ed = ARRPTR(out);
401 
402  if (hs2_count == 0)
403  {
404  /* return a copy of the input, unchanged */
405  memcpy(out, hs, VARSIZE(hs));
406  HS_FIXSIZE(out, hs_count);
407  HS_SETCOUNT(out, hs_count);
408  PG_RETURN_POINTER(out);
409  }
410 
411  /*
412  * this is in effect a merge between hs and hs2, both of which are already
413  * sorted by (keylen,key); we take keys from hs only; for equal keys, we
414  * take the value from hs unless the values are equal
415  */
416 
417  for (i = j = 0; i < hs_count;)
418  {
419  int difference;
420 
421  if (j >= hs2_count)
422  difference = -1;
423  else
424  {
425  int skeylen = HSTORE_KEYLEN(es, i);
426  int s2keylen = HSTORE_KEYLEN(es2, j);
427 
428  if (skeylen == s2keylen)
429  difference = memcmp(HSTORE_KEY(es, ps, i),
430  HSTORE_KEY(es2, ps2, j),
431  skeylen);
432  else
433  difference = (skeylen > s2keylen) ? 1 : -1;
434  }
435 
436  if (difference > 0)
437  ++j;
438  else if (difference == 0)
439  {
440  int svallen = HSTORE_VALLEN(es, i);
441  int snullval = HSTORE_VALISNULL(es, i);
442 
443  if (snullval != HSTORE_VALISNULL(es2, j) ||
444  (!snullval && (svallen != HSTORE_VALLEN(es2, j) ||
445  memcmp(HSTORE_VAL(es, ps, i),
446  HSTORE_VAL(es2, ps2, j),
447  svallen) != 0)))
448  {
449  HS_COPYITEM(ed, bufd, pd,
450  HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
451  svallen, snullval);
452  ++outcount;
453  }
454  ++i, ++j;
455  }
456  else
457  {
458  HS_COPYITEM(ed, bufd, pd,
459  HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
460  HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
461  ++outcount;
462  ++i;
463  }
464  }
465 
466  HS_FINALIZE(out, outcount, bufd, pd);
467 
468  PG_RETURN_POINTER(out);
469 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define VARSIZE(PTR)
Definition: postgres.h:306
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
Datum difference(PG_FUNCTION_ARGS)
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
#define HS_COPYITEM(dent_, dbuf_, dptr_, sptr_, klen_, vlen_, vnull_)
Definition: hstore.h:99
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define ARRPTR(x)
Definition: cube.c:26
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62
#define HS_FIXSIZE(hsp_, count_)
Definition: hstore.h:143
Datum hstore_each ( PG_FUNCTION_ARGS  )

Definition at line 1027 of file hstore_op.c.

References ARRPTR, FuncCallContext::call_cntr, cstring_to_text_with_len(), heap_form_tuple(), HeapTupleGetDatum, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, PG_GETARG_HS, PointerGetDatum, setup_firstcall(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, STRPTR, FuncCallContext::tuple_desc, and FuncCallContext::user_fctx.

1028 {
1029  FuncCallContext *funcctx;
1030  HStore *hs;
1031  int i;
1032 
1033  if (SRF_IS_FIRSTCALL())
1034  {
1035  hs = PG_GETARG_HS(0);
1036  funcctx = SRF_FIRSTCALL_INIT();
1037  setup_firstcall(funcctx, hs, fcinfo);
1038  }
1039 
1040  funcctx = SRF_PERCALL_SETUP();
1041  hs = (HStore *) funcctx->user_fctx;
1042  i = funcctx->call_cntr;
1043 
1044  if (i < HS_COUNT(hs))
1045  {
1046  HEntry *entries = ARRPTR(hs);
1047  char *ptr = STRPTR(hs);
1048  Datum res,
1049  dvalues[2];
1050  bool nulls[2] = {false, false};
1051  text *item;
1052  HeapTuple tuple;
1053 
1054  item = cstring_to_text_with_len(HSTORE_KEY(entries, ptr, i),
1055  HSTORE_KEYLEN(entries, i));
1056  dvalues[0] = PointerGetDatum(item);
1057 
1058  if (HSTORE_VALISNULL(entries, i))
1059  {
1060  dvalues[1] = (Datum) 0;
1061  nulls[1] = true;
1062  }
1063  else
1064  {
1065  item = cstring_to_text_with_len(HSTORE_VAL(entries, ptr, i),
1066  HSTORE_VALLEN(entries, i));
1067  dvalues[1] = PointerGetDatum(item);
1068  }
1069 
1070  tuple = heap_form_tuple(funcctx->tuple_desc, dvalues, nulls);
1071  res = HeapTupleGetDatum(tuple);
1072 
1073  SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
1074  }
1075 
1076  SRF_RETURN_DONE(funcctx);
1077 }
uint64 call_cntr
Definition: funcapi.h:65
Definition: hstore.h:44
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
TupleDesc tuple_desc
Definition: funcapi.h:120
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static void setup_firstcall(FuncCallContext *funcctx, HStore *hs, FunctionCallInfoData *fcinfo)
Definition: hstore_op.c:856
uintptr_t Datum
Definition: postgres.h:374
Definition: hstore.h:18
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * user_fctx
Definition: funcapi.h:90
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum hstore_eq ( PG_FUNCTION_ARGS  )

Definition at line 1169 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1170 {
1172  PG_GETARG_DATUM(0),
1173  PG_GETARG_DATUM(1)));
1174 
1175  PG_RETURN_BOOL(res == 0);
1176 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_exists ( PG_FUNCTION_ARGS  )

Definition at line 152 of file hstore_op.c.

References hstoreFindKey(), idx(), NULL, PG_GETARG_HS, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

153 {
154  HStore *hs = PG_GETARG_HS(0);
155  text *key = PG_GETARG_TEXT_PP(1);
156  int idx = hstoreFindKey(hs, NULL,
157  VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
158 
159  PG_RETURN_BOOL(idx >= 0);
160 }
Definition: hstore.h:44
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:226
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum hstore_exists_all ( PG_FUNCTION_ARGS  )

Definition at line 199 of file hstore_op.c.

References hstoreArrayToPairs(), hstoreFindKey(), i, idx(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_HS, and PG_RETURN_BOOL.

200 {
201  HStore *hs = PG_GETARG_HS(0);
202  ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
203  int nkeys;
204  Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
205  int i;
206  int lowbound = 0;
207  bool res = true;
208 
209  /*
210  * we exploit the fact that the pairs list is already sorted into strictly
211  * increasing order to narrow the hstoreFindKey search; each search can
212  * start one entry past the previous "found" entry, or at the lower bound
213  * of the last search.
214  */
215  for (i = 0; i < nkeys; i++)
216  {
217  int idx = hstoreFindKey(hs, &lowbound,
218  key_pairs[i].key, key_pairs[i].keylen);
219 
220  if (idx < 0)
221  {
222  res = false;
223  break;
224  }
225  }
226 
227  PG_RETURN_BOOL(res);
228 }
Definition: hstore.h:44
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Pairs * hstoreArrayToPairs(ArrayType *a, int *npairs)
Definition: hstore_op.c:74
int i
Definition: hstore.h:161
Datum hstore_exists_any ( PG_FUNCTION_ARGS  )

Definition at line 165 of file hstore_op.c.

References hstoreArrayToPairs(), hstoreFindKey(), i, idx(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_HS, and PG_RETURN_BOOL.

166 {
167  HStore *hs = PG_GETARG_HS(0);
168  ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
169  int nkeys;
170  Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
171  int i;
172  int lowbound = 0;
173  bool res = false;
174 
175  /*
176  * we exploit the fact that the pairs list is already sorted into strictly
177  * increasing order to narrow the hstoreFindKey search; each search can
178  * start one entry past the previous "found" entry, or at the lower bound
179  * of the last search.
180  */
181  for (i = 0; i < nkeys; i++)
182  {
183  int idx = hstoreFindKey(hs, &lowbound,
184  key_pairs[i].key, key_pairs[i].keylen);
185 
186  if (idx >= 0)
187  {
188  res = true;
189  break;
190  }
191  }
192 
193  PG_RETURN_BOOL(res);
194 }
Definition: hstore.h:44
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Pairs * hstoreArrayToPairs(ArrayType *a, int *npairs)
Definition: hstore_op.c:74
int i
Definition: hstore.h:161
Datum hstore_fetchval ( PG_FUNCTION_ARGS  )

Definition at line 131 of file hstore_op.c.

References ARRPTR, cstring_to_text_with_len(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreFindKey(), idx(), NULL, PG_GETARG_HS, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, STRPTR, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

132 {
133  HStore *hs = PG_GETARG_HS(0);
134  text *key = PG_GETARG_TEXT_PP(1);
135  HEntry *entries = ARRPTR(hs);
136  text *out;
137  int idx = hstoreFindKey(hs, NULL,
138  VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
139 
140  if (idx < 0 || HSTORE_VALISNULL(entries, idx))
141  PG_RETURN_NULL();
142 
143  out = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), idx),
144  HSTORE_VALLEN(entries, idx));
145 
146  PG_RETURN_TEXT_P(out);
147 }
Definition: hstore.h:44
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define NULL
Definition: c.h:226
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
#define STRPTR(x)
Definition: hstore.h:76
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum hstore_ge ( PG_FUNCTION_ARGS  )

Definition at line 1202 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1203 {
1205  PG_GETARG_DATUM(0),
1206  PG_GETARG_DATUM(1)));
1207 
1208  PG_RETURN_BOOL(res >= 0);
1209 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_gt ( PG_FUNCTION_ARGS  )

Definition at line 1191 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1192 {
1194  PG_GETARG_DATUM(0),
1195  PG_GETARG_DATUM(1)));
1196 
1197  PG_RETURN_BOOL(res > 0);
1198 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_hash ( PG_FUNCTION_ARGS  )

Definition at line 1236 of file hstore_op.c.

References ARRPTR, Assert, CALCDATASIZE, hash_any(), HS_COUNT, HSE_ENDPOS, HSHRDSIZE, PG_FREE_IF_COPY, PG_GETARG_HS, PG_RETURN_DATUM, VARDATA, VARHDRSZ, and VARSIZE.

1237 {
1238  HStore *hs = PG_GETARG_HS(0);
1239  Datum hval = hash_any((unsigned char *) VARDATA(hs),
1240  VARSIZE(hs) - VARHDRSZ);
1241 
1242  /*
1243  * this is the only place in the code that cares whether the overall
1244  * varlena size exactly matches the true data size; this assertion should
1245  * be maintained by all the other code, but we make it explicit here.
1246  */
1247  Assert(VARSIZE(hs) ==
1248  (HS_COUNT(hs) != 0 ?
1249  CALCDATASIZE(HS_COUNT(hs),
1250  HSE_ENDPOS(ARRPTR(hs)[2 * HS_COUNT(hs) - 1])) :
1251  HSHRDSIZE));
1252 
1253  PG_FREE_IF_COPY(hs, 0);
1254  PG_RETURN_DATUM(hval);
1255 }
#define HSE_ENDPOS(he_)
Definition: hstore.h:30
Definition: hstore.h:44
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
uintptr_t Datum
Definition: postgres.h:374
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define HSHRDSIZE
Definition: hstore.h:71
#define Assert(condition)
Definition: c.h:671
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define ARRPTR(x)
Definition: cube.c:26
Datum hstore_le ( PG_FUNCTION_ARGS  )

Definition at line 1224 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1225 {
1227  PG_GETARG_DATUM(0),
1228  PG_GETARG_DATUM(1)));
1229 
1230  PG_RETURN_BOOL(res <= 0);
1231 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_lt ( PG_FUNCTION_ARGS  )

Definition at line 1213 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1214 {
1216  PG_GETARG_DATUM(0),
1217  PG_GETARG_DATUM(1)));
1218 
1219  PG_RETURN_BOOL(res < 0);
1220 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum hstore_ne ( PG_FUNCTION_ARGS  )

Definition at line 1180 of file hstore_op.c.

References DatumGetInt32, DirectFunctionCall2, hstore_cmp(), PG_GETARG_DATUM, and PG_RETURN_BOOL.

1181 {
1183  PG_GETARG_DATUM(0),
1184  PG_GETARG_DATUM(1)));
1185 
1186  PG_RETURN_BOOL(res != 0);
1187 }
#define DatumGetInt32(X)
Definition: postgres.h:480
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum hstore_cmp(PG_FUNCTION_ARGS)
Definition: hstore_op.c:1088
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
HSTORE_POLLUTE ( hstore_fetchval  ,
fetchval   
)
HSTORE_POLLUTE ( hstore_exists  ,
exists   
)
HSTORE_POLLUTE ( hstore_defined  ,
defined   
)
HSTORE_POLLUTE ( hstore_delete  ,
delete   
)
HSTORE_POLLUTE ( hstore_concat  ,
hs_concat   
)
HSTORE_POLLUTE ( hstore_contains  ,
hs_contains   
)
HSTORE_POLLUTE ( hstore_contained  ,
hs_contained   
)
HSTORE_POLLUTE ( hstore_akeys  ,
akeys   
)
HSTORE_POLLUTE ( hstore_avals  ,
avals   
)
HSTORE_POLLUTE ( hstore_skeys  ,
skeys   
)
HSTORE_POLLUTE ( hstore_svals  ,
svals   
)
HSTORE_POLLUTE ( hstore_each  ,
each   
)
Datum hstore_skeys ( PG_FUNCTION_ARGS  )

Definition at line 886 of file hstore_op.c.

References ARRPTR, FuncCallContext::call_cntr, cstring_to_text_with_len(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, i, NULL, PG_GETARG_HS, PointerGetDatum, setup_firstcall(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, STRPTR, and FuncCallContext::user_fctx.

887 {
888  FuncCallContext *funcctx;
889  HStore *hs;
890  int i;
891 
892  if (SRF_IS_FIRSTCALL())
893  {
894  hs = PG_GETARG_HS(0);
895  funcctx = SRF_FIRSTCALL_INIT();
896  setup_firstcall(funcctx, hs, NULL);
897  }
898 
899  funcctx = SRF_PERCALL_SETUP();
900  hs = (HStore *) funcctx->user_fctx;
901  i = funcctx->call_cntr;
902 
903  if (i < HS_COUNT(hs))
904  {
905  HEntry *entries = ARRPTR(hs);
906  text *item;
907 
908  item = cstring_to_text_with_len(HSTORE_KEY(entries, STRPTR(hs), i),
909  HSTORE_KEYLEN(entries, i));
910 
911  SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
912  }
913 
914  SRF_RETURN_DONE(funcctx);
915 }
uint64 call_cntr
Definition: funcapi.h:65
Definition: hstore.h:44
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static void setup_firstcall(FuncCallContext *funcctx, HStore *hs, FunctionCallInfoData *fcinfo)
Definition: hstore_op.c:856
#define NULL
Definition: c.h:226
Definition: hstore.h:18
void * user_fctx
Definition: funcapi.h:90
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum hstore_slice_to_array ( PG_FUNCTION_ARGS  )

Definition at line 572 of file hstore_op.c.

References ARR_DIMS, ARR_LBOUND, ARR_NDIM, ARRPTR, construct_empty_array(), construct_md_array(), cstring_to_text_with_len(), DatumGetPointer, deconstruct_array(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreFindKey(), i, idx(), NULL, palloc(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_HS, PG_RETURN_POINTER, PointerGetDatum, STRPTR, TEXTOID, VARDATA, VARHDRSZ, and VARSIZE.

573 {
574  HStore *hs = PG_GETARG_HS(0);
575  HEntry *entries = ARRPTR(hs);
576  char *ptr = STRPTR(hs);
577  ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
578  ArrayType *aout;
579  Datum *key_datums;
580  bool *key_nulls;
581  Datum *out_datums;
582  bool *out_nulls;
583  int key_count;
584  int i;
585 
586  deconstruct_array(key_array,
587  TEXTOID, -1, false, 'i',
588  &key_datums, &key_nulls, &key_count);
589 
590  if (key_count == 0)
591  {
593  PG_RETURN_POINTER(aout);
594  }
595 
596  out_datums = palloc(sizeof(Datum) * key_count);
597  out_nulls = palloc(sizeof(bool) * key_count);
598 
599  for (i = 0; i < key_count; ++i)
600  {
601  text *key = (text *) DatumGetPointer(key_datums[i]);
602  int idx;
603 
604  if (key_nulls[i])
605  idx = -1;
606  else
607  idx = hstoreFindKey(hs, NULL, VARDATA(key), VARSIZE(key) - VARHDRSZ);
608 
609  if (idx < 0 || HSTORE_VALISNULL(entries, idx))
610  {
611  out_nulls[i] = true;
612  out_datums[i] = (Datum) 0;
613  }
614  else
615  {
616  out_datums[i] = PointerGetDatum(
617  cstring_to_text_with_len(HSTORE_VAL(entries, ptr, idx),
618  HSTORE_VALLEN(entries, idx)));
619  out_nulls[i] = false;
620  }
621  }
622 
623  aout = construct_md_array(out_datums, out_nulls,
624  ARR_NDIM(key_array),
625  ARR_DIMS(key_array),
626  ARR_LBOUND(key_array),
627  TEXTOID, -1, false, 'i');
628 
629  PG_RETURN_POINTER(aout);
630 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define VARDATA(PTR)
Definition: postgres.h:305
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define PointerGetDatum(X)
Definition: postgres.h:564
#define VARHDRSZ
Definition: c.h:441
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3424
#define ARR_LBOUND(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ARR_DIMS(a)
Definition: array.h:275
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_GETARG_HS(x)
Definition: hstore.h:154
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
Definition: hstore.h:18
#define ARR_NDIM(a)
Definition: array.h:271
#define DatumGetPointer(X)
Definition: postgres.h:557
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3340
Datum hstore_slice_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 635 of file hstore_op.c.

References ARRPTR, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreArrayToPairs(), hstoreFindKey(), hstorePairs, i, idx(), Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, NULL, palloc(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_HS, PG_RETURN_POINTER, STRPTR, Pairs::val, and Pairs::vallen.

636 {
637  HStore *hs = PG_GETARG_HS(0);
638  HEntry *entries = ARRPTR(hs);
639  char *ptr = STRPTR(hs);
640  ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
641  HStore *out;
642  int nkeys;
643  Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
644  Pairs *out_pairs;
645  int bufsiz;
646  int lastidx = 0;
647  int i;
648  int out_count = 0;
649 
650  if (nkeys == 0)
651  {
652  out = hstorePairs(NULL, 0, 0);
653  PG_RETURN_POINTER(out);
654  }
655 
656  /* hstoreArrayToPairs() checked overflow */
657  out_pairs = palloc(sizeof(Pairs) * nkeys);
658  bufsiz = 0;
659 
660  /*
661  * we exploit the fact that the pairs list is already sorted into strictly
662  * increasing order to narrow the hstoreFindKey search; each search can
663  * start one entry past the previous "found" entry, or at the lower bound
664  * of the last search.
665  */
666 
667  for (i = 0; i < nkeys; ++i)
668  {
669  int idx = hstoreFindKey(hs, &lastidx,
670  key_pairs[i].key, key_pairs[i].keylen);
671 
672  if (idx >= 0)
673  {
674  out_pairs[out_count].key = key_pairs[i].key;
675  bufsiz += (out_pairs[out_count].keylen = key_pairs[i].keylen);
676  out_pairs[out_count].val = HSTORE_VAL(entries, ptr, idx);
677  bufsiz += (out_pairs[out_count].vallen = HSTORE_VALLEN(entries, idx));
678  out_pairs[out_count].isnull = HSTORE_VALISNULL(entries, idx);
679  out_pairs[out_count].needfree = false;
680  ++out_count;
681  }
682  }
683 
684  /*
685  * we don't use uniquePairs here because we know that the pairs list is
686  * already sorted and uniq'ed.
687  */
688 
689  out = hstorePairs(out_pairs, out_count, bufsiz);
690 
691  PG_RETURN_POINTER(out);
692 }
#define hstorePairs
Definition: hstore_plperl.c:60
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
#define PG_GETARG_HS(x)
Definition: hstore.h:154
Pairs * hstoreArrayToPairs(ArrayType *a, int *npairs)
Definition: hstore_op.c:74
size_t keylen
Definition: hstore.h:165
#define NULL
Definition: c.h:226
bool isnull
Definition: hstore.h:167
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26
Definition: hstore.h:161
Datum hstore_svals ( PG_FUNCTION_ARGS  )

Definition at line 920 of file hstore_op.c.

References ARRPTR, FuncCallContext::call_cntr, cstring_to_text_with_len(), ExprMultipleResult, HS_COUNT, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, ReturnSetInfo::isDone, NULL, PG_GETARG_HS, PG_RETURN_NULL, PointerGetDatum, setup_firstcall(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, STRPTR, and FuncCallContext::user_fctx.

921 {
922  FuncCallContext *funcctx;
923  HStore *hs;
924  int i;
925 
926  if (SRF_IS_FIRSTCALL())
927  {
928  hs = PG_GETARG_HS(0);
929  funcctx = SRF_FIRSTCALL_INIT();
930  setup_firstcall(funcctx, hs, NULL);
931  }
932 
933  funcctx = SRF_PERCALL_SETUP();
934  hs = (HStore *) funcctx->user_fctx;
935  i = funcctx->call_cntr;
936 
937  if (i < HS_COUNT(hs))
938  {
939  HEntry *entries = ARRPTR(hs);
940 
941  if (HSTORE_VALISNULL(entries, i))
942  {
943  ReturnSetInfo *rsi;
944 
945  /* ugly ugly ugly. why no macro for this? */
946  (funcctx)->call_cntr++;
947  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
948  rsi->isDone = ExprMultipleResult;
949  PG_RETURN_NULL();
950  }
951  else
952  {
953  text *item;
954 
955  item = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), i),
956  HSTORE_VALLEN(entries, i));
957 
958  SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
959  }
960  }
961 
962  SRF_RETURN_DONE(funcctx);
963 }
uint64 call_cntr
Definition: funcapi.h:65
Definition: hstore.h:44
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
static void setup_firstcall(FuncCallContext *funcctx, HStore *hs, FunctionCallInfoData *fcinfo)
Definition: hstore_op.c:856
#define NULL
Definition: c.h:226
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * user_fctx
Definition: funcapi.h:90
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
ExprDoneCond isDone
Definition: execnodes.h:202
#define PG_RETURN_NULL()
Definition: fmgr.h:289
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum hstore_to_array ( PG_FUNCTION_ARGS  )

Definition at line 828 of file hstore_op.c.

References hstore_to_array_internal(), PG_GETARG_HS, and PG_RETURN_POINTER.

829 {
830  HStore *hs = PG_GETARG_HS(0);
831  ArrayType *out = hstore_to_array_internal(hs, 1);
832 
833  PG_RETURN_POINTER(out);
834 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define PG_GETARG_HS(x)
Definition: hstore.h:154
static ArrayType * hstore_to_array_internal(HStore *hs, int ndims)
Definition: hstore_op.c:778
static ArrayType* hstore_to_array_internal ( HStore hs,
int  ndims 
)
static

Definition at line 778 of file hstore_op.c.

References ARRPTR, Assert, construct_empty_array(), construct_md_array(), cstring_to_text_with_len(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, palloc(), PointerGetDatum, STRPTR, and TEXTOID.

Referenced by hstore_to_array(), and hstore_to_matrix().

779 {
780  HEntry *entries = ARRPTR(hs);
781  char *base = STRPTR(hs);
782  int count = HS_COUNT(hs);
783  int out_size[2] = {0, 2};
784  int lb[2] = {1, 1};
785  Datum *out_datums;
786  bool *out_nulls;
787  int i;
788 
789  Assert(ndims < 3);
790 
791  if (count == 0 || ndims == 0)
793 
794  out_size[0] = count * 2 / ndims;
795  out_datums = palloc(sizeof(Datum) * count * 2);
796  out_nulls = palloc(sizeof(bool) * count * 2);
797 
798  for (i = 0; i < count; ++i)
799  {
800  text *key = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
801  HSTORE_KEYLEN(entries, i));
802 
803  out_datums[i * 2] = PointerGetDatum(key);
804  out_nulls[i * 2] = false;
805 
806  if (HSTORE_VALISNULL(entries, i))
807  {
808  out_datums[i * 2 + 1] = (Datum) 0;
809  out_nulls[i * 2 + 1] = true;
810  }
811  else
812  {
813  text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
814  HSTORE_VALLEN(entries, i));
815 
816  out_datums[i * 2 + 1] = PointerGetDatum(item);
817  out_nulls[i * 2 + 1] = false;
818  }
819  }
820 
821  return construct_md_array(out_datums, out_nulls,
822  ndims, out_size, lb,
823  TEXTOID, -1, false, 'i');
824 }
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3424
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
uintptr_t Datum
Definition: postgres.h:374
#define Assert(condition)
Definition: c.h:671
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define STRPTR(x)
Definition: hstore.h:76
int i
Definition: c.h:435
#define ARRPTR(x)
Definition: cube.c:26
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3340
Datum hstore_to_matrix ( PG_FUNCTION_ARGS  )

Definition at line 838 of file hstore_op.c.

References hstore_to_array_internal(), PG_GETARG_HS, and PG_RETURN_POINTER.

839 {
840  HStore *hs = PG_GETARG_HS(0);
841  ArrayType *out = hstore_to_array_internal(hs, 2);
842 
843  PG_RETURN_POINTER(out);
844 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
#define PG_GETARG_HS(x)
Definition: hstore.h:154
static ArrayType * hstore_to_array_internal(HStore *hs, int ndims)
Definition: hstore_op.c:778
Pairs* hstoreArrayToPairs ( ArrayType a,
int *  npairs 
)

Definition at line 74 of file hstore_op.c.

References deconstruct_array(), ereport, errcode(), errmsg(), ERROR, hstoreUniquePairs, i, Pairs::isnull, Pairs::key, Pairs::keylen, MaxAllocSize, Pairs::needfree, NULL, palloc(), TEXTOID, Pairs::val, Pairs::vallen, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by hstore_delete_array(), hstore_exists_all(), hstore_exists_any(), and hstore_slice_to_hstore().

75 {
76  Datum *key_datums;
77  bool *key_nulls;
78  int key_count;
79  Pairs *key_pairs;
80  int bufsiz;
81  int i,
82  j;
83 
85  TEXTOID, -1, false, 'i',
86  &key_datums, &key_nulls, &key_count);
87 
88  if (key_count == 0)
89  {
90  *npairs = 0;
91  return NULL;
92  }
93 
94  /*
95  * A text array uses at least eight bytes per element, so any overflow in
96  * "key_count * sizeof(Pairs)" is small enough for palloc() to catch.
97  * However, credible improvements to the array format could invalidate
98  * that assumption. Therefore, use an explicit check rather than relying
99  * on palloc() to complain.
100  */
101  if (key_count > MaxAllocSize / sizeof(Pairs))
102  ereport(ERROR,
103  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
104  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
105  key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
106 
107  key_pairs = palloc(sizeof(Pairs) * key_count);
108 
109  for (i = 0, j = 0; i < key_count; i++)
110  {
111  if (!key_nulls[i])
112  {
113  key_pairs[j].key = VARDATA(key_datums[i]);
114  key_pairs[j].keylen = VARSIZE(key_datums[i]) - VARHDRSZ;
115  key_pairs[j].val = NULL;
116  key_pairs[j].vallen = 0;
117  key_pairs[j].needfree = 0;
118  key_pairs[j].isnull = 1;
119  j++;
120  }
121  }
122 
123  *npairs = hstoreUniquePairs(key_pairs, j, &bufsiz);
124 
125  return key_pairs;
126 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
int errcode(int sqlerrcode)
Definition: elog.c:575
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
#define hstoreUniquePairs
Definition: hstore_plperl.c:59
uintptr_t Datum
Definition: postgres.h:374
size_t keylen
Definition: hstore.h:165
#define NULL
Definition: c.h:226
bool isnull
Definition: hstore.h:167
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: hstore.h:161
int hstoreFindKey ( HStore hs,
int *  lowbound,
char *  key,
int  keylen 
)

Definition at line 37 of file hstore_op.c.

References ARRPTR, difference(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, and STRPTR.

Referenced by hstore_contains(), hstore_defined(), hstore_exists(), hstore_exists_all(), hstore_exists_any(), hstore_fetchval(), hstore_populate_record(), hstore_slice_to_array(), and hstore_slice_to_hstore().

38 {
39  HEntry *entries = ARRPTR(hs);
40  int stopLow = lowbound ? *lowbound : 0;
41  int stopHigh = HS_COUNT(hs);
42  int stopMiddle;
43  char *base = STRPTR(hs);
44 
45  while (stopLow < stopHigh)
46  {
47  int difference;
48 
49  stopMiddle = stopLow + (stopHigh - stopLow) / 2;
50 
51  if (HSTORE_KEYLEN(entries, stopMiddle) == keylen)
52  difference = memcmp(HSTORE_KEY(entries, base, stopMiddle), key, keylen);
53  else
54  difference = (HSTORE_KEYLEN(entries, stopMiddle) > keylen) ? 1 : -1;
55 
56  if (difference == 0)
57  {
58  if (lowbound)
59  *lowbound = stopMiddle + 1;
60  return stopMiddle;
61  }
62  else if (difference < 0)
63  stopLow = stopMiddle + 1;
64  else
65  stopHigh = stopMiddle;
66  }
67 
68  if (lowbound)
69  *lowbound = stopLow;
70  return -1;
71 }
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
Datum difference(PG_FUNCTION_ARGS)
Definition: hstore.h:18
#define STRPTR(x)
Definition: hstore.h:76
#define ARRPTR(x)
Definition: cube.c:26
PG_FUNCTION_INFO_V1 ( hstore_fetchval  )
PG_FUNCTION_INFO_V1 ( hstore_exists  )
PG_FUNCTION_INFO_V1 ( hstore_exists_any  )
PG_FUNCTION_INFO_V1 ( hstore_exists_all  )
PG_FUNCTION_INFO_V1 ( hstore_defined  )
PG_FUNCTION_INFO_V1 ( hstore_delete  )
PG_FUNCTION_INFO_V1 ( hstore_delete_array  )
PG_FUNCTION_INFO_V1 ( hstore_delete_hstore  )
PG_FUNCTION_INFO_V1 ( hstore_concat  )
PG_FUNCTION_INFO_V1 ( hstore_slice_to_array  )
PG_FUNCTION_INFO_V1 ( hstore_slice_to_hstore  )
PG_FUNCTION_INFO_V1 ( hstore_akeys  )
PG_FUNCTION_INFO_V1 ( hstore_avals  )
PG_FUNCTION_INFO_V1 ( hstore_to_array  )
PG_FUNCTION_INFO_V1 ( hstore_to_matrix  )
PG_FUNCTION_INFO_V1 ( hstore_skeys  )
PG_FUNCTION_INFO_V1 ( hstore_svals  )
PG_FUNCTION_INFO_V1 ( hstore_contains  )
PG_FUNCTION_INFO_V1 ( hstore_contained  )
PG_FUNCTION_INFO_V1 ( hstore_each  )
PG_FUNCTION_INFO_V1 ( hstore_cmp  )
PG_FUNCTION_INFO_V1 ( hstore_eq  )
PG_FUNCTION_INFO_V1 ( hstore_ne  )
PG_FUNCTION_INFO_V1 ( hstore_gt  )
PG_FUNCTION_INFO_V1 ( hstore_ge  )
PG_FUNCTION_INFO_V1 ( hstore_lt  )
PG_FUNCTION_INFO_V1 ( hstore_le  )
PG_FUNCTION_INFO_V1 ( hstore_hash  )
static void setup_firstcall ( FuncCallContext funcctx,
HStore hs,
FunctionCallInfoData fcinfo 
)
static

Definition at line 856 of file hstore_op.c.

References BlessTupleDesc(), elog, ERROR, get_call_result_type(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NULL, palloc(), FuncCallContext::tuple_desc, TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, and VARSIZE.

Referenced by hstore_each(), hstore_skeys(), and hstore_svals().

858 {
859  MemoryContext oldcontext;
860  HStore *st;
861 
862  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
863 
864  st = (HStore *) palloc(VARSIZE(hs));
865  memcpy(st, hs, VARSIZE(hs));
866 
867  funcctx->user_fctx = (void *) st;
868 
869  if (fcinfo)
870  {
871  TupleDesc tupdesc;
872 
873  /* Build a tuple descriptor for our result type */
874  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
875  elog(ERROR, "return type must be a row type");
876 
877  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
878  }
879 
880  MemoryContextSwitchTo(oldcontext);
881 }
Definition: hstore.h:44
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define VARSIZE(PTR)
Definition: postgres.h:306
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TupleDesc tuple_desc
Definition: funcapi.h:120
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
#define NULL
Definition: c.h:226
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:891
#define elog
Definition: elog.h:219