2006-09-06 02:00:58 +08:00
|
|
|
#include "hstore.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include <access/heapam.h>
|
|
|
|
#include <fmgr.h>
|
|
|
|
|
|
|
|
|
|
|
|
static HEntry *
|
2006-10-04 08:30:14 +08:00
|
|
|
findkey(HStore * hs, char *key, int keylen)
|
|
|
|
{
|
|
|
|
HEntry *StopLow = ARRPTR(hs);
|
|
|
|
HEntry *StopHigh = StopLow + hs->size;
|
|
|
|
HEntry *StopMiddle;
|
|
|
|
int difference;
|
|
|
|
char *base = STRPTR(hs);
|
|
|
|
|
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (StopMiddle->keylen == keylen)
|
|
|
|
difference = strncmp(base + StopMiddle->pos, key, StopMiddle->keylen);
|
2006-09-06 02:00:58 +08:00
|
|
|
else
|
2006-10-04 08:30:14 +08:00
|
|
|
difference = (StopMiddle->keylen > keylen) ? 1 : -1;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
if (difference == 0)
|
|
|
|
return StopMiddle;
|
|
|
|
else if (difference < 0)
|
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(fetchval);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum fetchval(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
fetchval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
text *key = PG_GETARG_TEXT_P(1);
|
|
|
|
HEntry *entry;
|
|
|
|
text *out;
|
|
|
|
|
|
|
|
if ((entry = findkey(hs, VARDATA(key), VARSIZE(key) - VARHDRSZ)) == NULL || entry->valisnull)
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
out = cstring_to_text_with_len(STRPTR(hs) + entry->pos + entry->keylen,
|
|
|
|
entry->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(out);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(exists);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum exists(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
exists(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
text *key = PG_GETARG_TEXT_P(1);
|
|
|
|
HEntry *entry;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
entry = findkey(hs, VARDATA(key), VARSIZE(key) - VARHDRSZ);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(defined);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum defined(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
defined(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
text *key = PG_GETARG_TEXT_P(1);
|
|
|
|
HEntry *entry;
|
|
|
|
bool res;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
entry = findkey(hs, VARDATA(key), VARSIZE(key) - VARHDRSZ);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = (entry && !entry->valisnull) ? true : false;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(delete);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum delete(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
delete(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
text *key = PG_GETARG_TEXT_P(1);
|
2007-03-01 06:44:38 +08:00
|
|
|
HStore *out = palloc(VARSIZE(hs));
|
2006-10-04 08:30:14 +08:00
|
|
|
char *ptrs,
|
|
|
|
*ptrd;
|
|
|
|
HEntry *es,
|
|
|
|
*ed;
|
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(out, VARSIZE(hs));
|
2006-12-06 01:36:00 +08:00
|
|
|
out->size = hs->size; /* temporary! */
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ptrs = STRPTR(hs);
|
|
|
|
es = ARRPTR(hs);
|
|
|
|
ptrd = STRPTR(out);
|
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
|
|
|
while (es - ARRPTR(hs) < hs->size)
|
|
|
|
{
|
|
|
|
if (!(es->keylen == VARSIZE(key) - VARHDRSZ && strncmp(ptrs, VARDATA(key), es->keylen) == 0))
|
|
|
|
{
|
|
|
|
memcpy(ed, es, sizeof(HEntry));
|
|
|
|
memcpy(ptrd, ptrs, es->keylen + ((es->valisnull) ? 0 : es->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = ptrd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
ptrd += es->keylen + ((es->valisnull) ? 0 : es->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
ptrs += es->keylen + ((es->valisnull) ? 0 : es->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ed - ARRPTR(out) != out->size)
|
|
|
|
{
|
|
|
|
int buflen = ptrd - STRPTR(out);
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
ptrd = STRPTR(out);
|
|
|
|
|
|
|
|
out->size = ed - ARRPTR(out);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
memmove(STRPTR(out), ptrd, buflen);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hs_concat);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum hs_concat(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
hs_concat(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *s1 = PG_GETARG_HS(0);
|
|
|
|
HStore *s2 = PG_GETARG_HS(1);
|
2007-03-01 06:44:38 +08:00
|
|
|
HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
|
2006-10-04 08:30:14 +08:00
|
|
|
char *ps1,
|
|
|
|
*ps2,
|
|
|
|
*pd;
|
|
|
|
HEntry *es1,
|
|
|
|
*es2,
|
|
|
|
*ed;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2));
|
2006-09-06 02:00:58 +08:00
|
|
|
out->size = s1->size + s2->size;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ps1 = STRPTR(s1);
|
|
|
|
ps2 = STRPTR(s2);
|
|
|
|
pd = STRPTR(out);
|
|
|
|
es1 = ARRPTR(s1);
|
|
|
|
es2 = ARRPTR(s2);
|
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
|
|
|
while (es1 - ARRPTR(s1) < s1->size && es2 - ARRPTR(s2) < s2->size)
|
|
|
|
{
|
|
|
|
int difference;
|
|
|
|
|
|
|
|
if (es1->keylen == es2->keylen)
|
|
|
|
difference = strncmp(ps1, ps2, es1->keylen);
|
2006-09-06 02:00:58 +08:00
|
|
|
else
|
2006-10-04 08:30:14 +08:00
|
|
|
difference = (es1->keylen > es2->keylen) ? 1 : -1;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (difference == 0)
|
|
|
|
{
|
|
|
|
memcpy(ed, es2, sizeof(HEntry));
|
|
|
|
memcpy(pd, ps2, es2->keylen + ((es2->valisnull) ? 0 : es2->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = pd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es1++;
|
2006-10-04 08:30:14 +08:00
|
|
|
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es2++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (difference > 0)
|
|
|
|
{
|
|
|
|
memcpy(ed, es2, sizeof(HEntry));
|
|
|
|
memcpy(pd, ps2, es2->keylen + ((es2->valisnull) ? 0 : es2->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = pd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es2++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(ed, es1, sizeof(HEntry));
|
|
|
|
memcpy(pd, ps1, es1->keylen + ((es1->valisnull) ? 0 : es1->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = pd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
pd += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (es1 - ARRPTR(s1) < s1->size)
|
|
|
|
{
|
|
|
|
memcpy(ed, es1, sizeof(HEntry));
|
|
|
|
memcpy(pd, ps1, es1->keylen + ((es1->valisnull) ? 0 : es1->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = pd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
pd += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es1++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (es2 - ARRPTR(s2) < s2->size)
|
|
|
|
{
|
|
|
|
memcpy(ed, es2, sizeof(HEntry));
|
|
|
|
memcpy(pd, ps2, es2->keylen + ((es2->valisnull) ? 0 : es2->vallen));
|
2006-09-06 02:00:58 +08:00
|
|
|
ed->pos = pd - STRPTR(out);
|
2006-10-04 08:30:14 +08:00
|
|
|
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
ed++;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
es2++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ed - ARRPTR(out) != out->size)
|
|
|
|
{
|
|
|
|
int buflen = pd - STRPTR(out);
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
pd = STRPTR(out);
|
|
|
|
|
|
|
|
out->size = ed - ARRPTR(out);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
memmove(STRPTR(out), pd, buflen);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(s1, 0);
|
|
|
|
PG_FREE_IF_COPY(s2, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tconvert);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum tconvert(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
tconvert(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-03-14 22:21:53 +08:00
|
|
|
text *key;
|
|
|
|
text *val = NULL;
|
2006-10-04 08:30:14 +08:00
|
|
|
int len;
|
|
|
|
HStore *out;
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
if (PG_ARGISNULL(0))
|
2007-03-14 22:21:53 +08:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
key = PG_GETARG_TEXT_P(0);
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
if (PG_ARGISNULL(1))
|
|
|
|
len = CALCDATASIZE(1, VARSIZE(key));
|
2007-03-14 22:21:53 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
val = PG_GETARG_TEXT_P(1);
|
|
|
|
len = CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2 * VARHDRSZ);
|
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
out = palloc(len);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(out, len);
|
2006-10-04 08:30:14 +08:00
|
|
|
out->size = 1;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
ARRPTR(out)->keylen = VARSIZE(key) - VARHDRSZ;
|
2007-11-16 05:14:46 +08:00
|
|
|
if (PG_ARGISNULL(1))
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
|
|
|
ARRPTR(out)->vallen = 0;
|
|
|
|
ARRPTR(out)->valisnull = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARRPTR(out)->vallen = VARSIZE(val) - VARHDRSZ;
|
|
|
|
ARRPTR(out)->valisnull = false;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
ARRPTR(out)->pos = 0;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
memcpy(STRPTR(out), VARDATA(key), ARRPTR(out)->keylen);
|
2007-03-14 22:21:53 +08:00
|
|
|
if (!PG_ARGISNULL(1))
|
|
|
|
{
|
|
|
|
memcpy(STRPTR(out) + ARRPTR(out)->keylen, VARDATA(val), ARRPTR(out)->vallen);
|
|
|
|
PG_FREE_IF_COPY(val, 1);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(key, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(akeys);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum akeys(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
akeys(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
Datum *d;
|
|
|
|
ArrayType *a;
|
|
|
|
HEntry *ptr = ARRPTR(hs);
|
|
|
|
char *base = STRPTR(hs);
|
|
|
|
|
|
|
|
d = (Datum *) palloc(sizeof(Datum) * (hs->size + 1));
|
|
|
|
while (ptr - ARRPTR(hs) < hs->size)
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
text *item;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(base + ptr->pos, ptr->keylen);
|
2006-10-04 08:30:14 +08:00
|
|
|
d[ptr - ARRPTR(hs)] = PointerGetDatum(item);
|
2006-09-06 02:00:58 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
a = construct_array(d,
|
2006-10-04 08:30:14 +08:00
|
|
|
hs->size,
|
|
|
|
TEXTOID,
|
|
|
|
-1,
|
|
|
|
false,
|
2008-03-26 06:42:46 +08:00
|
|
|
'i');
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ptr = ARRPTR(hs);
|
|
|
|
while (ptr - ARRPTR(hs) < hs->size)
|
|
|
|
{
|
|
|
|
pfree(DatumGetPointer(d[ptr - ARRPTR(hs)]));
|
2006-09-06 02:00:58 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(d);
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(avals);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum avals(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
avals(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
Datum *d;
|
|
|
|
ArrayType *a;
|
|
|
|
HEntry *ptr = ARRPTR(hs);
|
|
|
|
char *base = STRPTR(hs);
|
|
|
|
|
|
|
|
d = (Datum *) palloc(sizeof(Datum) * (hs->size + 1));
|
|
|
|
while (ptr - ARRPTR(hs) < hs->size)
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
text *item;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(base + ptr->pos + ptr->keylen,
|
|
|
|
(ptr->valisnull) ? 0 : ptr->vallen);
|
2006-10-04 08:30:14 +08:00
|
|
|
d[ptr - ARRPTR(hs)] = PointerGetDatum(item);
|
2006-09-06 02:00:58 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
a = construct_array(d,
|
2006-10-04 08:30:14 +08:00
|
|
|
hs->size,
|
|
|
|
TEXTOID,
|
|
|
|
-1,
|
|
|
|
false,
|
2008-03-26 06:42:46 +08:00
|
|
|
'i');
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
ptr = ARRPTR(hs);
|
|
|
|
while (ptr - ARRPTR(hs) < hs->size)
|
|
|
|
{
|
|
|
|
pfree(DatumGetPointer(d[ptr - ARRPTR(hs)]));
|
2006-09-06 02:00:58 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(d);
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
HStore *hs;
|
|
|
|
int i;
|
|
|
|
} AKStore;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
setup_firstcall(FuncCallContext *funcctx, HStore * hs)
|
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
AKStore *st;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
st = (AKStore *) palloc(sizeof(AKStore));
|
|
|
|
st->i = 0;
|
2007-03-01 06:44:38 +08:00
|
|
|
st->hs = (HStore *) palloc(VARSIZE(hs));
|
|
|
|
memcpy(st->hs, hs, VARSIZE(hs));
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
funcctx->user_fctx = (void *) st;
|
2006-09-06 02:00:58 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(skeys);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum skeys(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
skeys(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
AKStore *st;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
setup_firstcall(funcctx, hs);
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2006-10-04 08:30:14 +08:00
|
|
|
st = (AKStore *) funcctx->user_fctx;
|
|
|
|
|
|
|
|
if (st->i < st->hs->size)
|
|
|
|
{
|
|
|
|
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
|
2008-03-26 06:42:46 +08:00
|
|
|
text *item;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos,
|
|
|
|
ptr->keylen);
|
2006-09-06 02:00:58 +08:00
|
|
|
st->i++;
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(st->hs);
|
|
|
|
pfree(st);
|
|
|
|
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(svals);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum svals(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
svals(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
AKStore *st;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
setup_firstcall(funcctx, hs);
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2006-10-04 08:30:14 +08:00
|
|
|
st = (AKStore *) funcctx->user_fctx;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (st->i < st->hs->size)
|
|
|
|
{
|
|
|
|
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
|
|
|
|
|
|
|
|
if (ptr->valisnull)
|
|
|
|
{
|
|
|
|
ReturnSetInfo *rsi;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
st->i++;
|
|
|
|
(funcctx)->call_cntr++;
|
|
|
|
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
|
|
|
|
rsi->isDone = ExprMultipleResult;
|
|
|
|
PG_RETURN_NULL();
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
text *item;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen,
|
|
|
|
ptr->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
st->i++;
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(st->hs);
|
|
|
|
pfree(st);
|
|
|
|
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hs_contains);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum hs_contains(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
hs_contains(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *val = PG_GETARG_HS(0);
|
|
|
|
HStore *tmpl = PG_GETARG_HS(1);
|
|
|
|
bool res = true;
|
|
|
|
HEntry *te = ARRPTR(tmpl);
|
|
|
|
char *vv = STRPTR(val);
|
|
|
|
char *tv = STRPTR(tmpl);
|
|
|
|
|
|
|
|
while (res && te - ARRPTR(tmpl) < tmpl->size)
|
|
|
|
{
|
|
|
|
HEntry *entry = findkey(val, tv + te->pos, te->keylen);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
if (te->valisnull || entry->valisnull)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
if (!(te->valisnull && entry->valisnull))
|
2007-03-14 22:21:53 +08:00
|
|
|
res = false;
|
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
else if (te->vallen != entry->vallen ||
|
|
|
|
strncmp(
|
|
|
|
vv + entry->pos + entry->keylen,
|
|
|
|
tv + te->pos + te->keylen,
|
|
|
|
te->vallen)
|
|
|
|
)
|
|
|
|
res = false;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
2006-09-06 02:00:58 +08:00
|
|
|
res = false;
|
|
|
|
te++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(val, 0);
|
|
|
|
PG_FREE_IF_COPY(tmpl, 1);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
PG_FUNCTION_INFO_V1(hs_contained);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum hs_contained(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
hs_contained(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(
|
|
|
|
hs_contains,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(each);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum each(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
each(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
AKStore *st;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
2006-10-04 08:30:14 +08:00
|
|
|
st = (AKStore *) palloc(sizeof(AKStore));
|
|
|
|
st->i = 0;
|
2007-03-01 06:44:38 +08:00
|
|
|
st->hs = (HStore *) palloc(VARSIZE(hs));
|
|
|
|
memcpy(st->hs, hs, VARSIZE(hs));
|
2006-10-04 08:30:14 +08:00
|
|
|
funcctx->user_fctx = (void *) st;
|
|
|
|
|
2007-08-27 07:36:32 +08:00
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2006-10-04 08:30:14 +08:00
|
|
|
st = (AKStore *) funcctx->user_fctx;
|
|
|
|
|
|
|
|
if (st->i < st->hs->size)
|
|
|
|
{
|
|
|
|
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
|
|
|
|
Datum res,
|
|
|
|
dvalues[2];
|
|
|
|
char nulls[] = {' ', ' '};
|
|
|
|
text *item;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos, ptr->keylen);
|
2006-09-06 02:00:58 +08:00
|
|
|
dvalues[0] = PointerGetDatum(item);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ptr->valisnull)
|
|
|
|
{
|
|
|
|
dvalues[1] = (Datum) 0;
|
|
|
|
nulls[1] = 'n';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen,
|
|
|
|
ptr->vallen);
|
2006-09-06 02:00:58 +08:00
|
|
|
dvalues[1] = PointerGetDatum(item);
|
|
|
|
}
|
|
|
|
st->i++;
|
|
|
|
|
|
|
|
tuple = heap_formtuple(funcctx->attinmeta->tupdesc, dvalues, nulls);
|
2007-08-27 07:36:32 +08:00
|
|
|
res = HeapTupleGetDatum(tuple);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(DatumGetPointer(dvalues[0]));
|
|
|
|
if (nulls[1] != 'n')
|
|
|
|
pfree(DatumGetPointer(dvalues[1]));
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(st->hs);
|
|
|
|
pfree(st);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|