postgresql/contrib/hstore/hstore_op.c

648 lines
13 KiB
C
Raw Normal View History

/*
* $PostgreSQL
*/
#include "postgres.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "hstore.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)
{
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);
else
2006-10-04 08:30:14 +08:00
difference = (StopMiddle->keylen > keylen) ? 1 : -1;
if (difference == 0)
return StopMiddle;
else if (difference < 0)
StopLow = StopMiddle + 1;
else
StopHigh = StopMiddle;
}
2006-10-04 08:30:14 +08:00
return NULL;
}
PG_FUNCTION_INFO_V1(fetchval);
2006-10-04 08:30:14 +08:00
Datum fetchval(PG_FUNCTION_ARGS);
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);
PG_RETURN_NULL();
}
out = cstring_to_text_with_len(STRPTR(hs) + entry->pos + entry->keylen,
entry->vallen);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_FREE_IF_COPY(key, 1);
PG_RETURN_TEXT_P(out);
}
PG_FUNCTION_INFO_V1(exists);
2006-10-04 08:30:14 +08:00
Datum exists(PG_FUNCTION_ARGS);
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-10-04 08:30:14 +08:00
entry = findkey(hs, VARDATA(key), VARSIZE(key) - VARHDRSZ);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_FREE_IF_COPY(key, 1);
PG_RETURN_BOOL(entry);
}
PG_FUNCTION_INFO_V1(defined);
2006-10-04 08:30:14 +08:00
Datum defined(PG_FUNCTION_ARGS);
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-10-04 08:30:14 +08:00
entry = findkey(hs, VARDATA(key), VARSIZE(key) - VARHDRSZ);
2006-10-04 08:30:14 +08:00
res = (entry && !entry->valisnull) ? true : false;
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_FREE_IF_COPY(key, 1);
PG_RETURN_BOOL(res);
}
PG_FUNCTION_INFO_V1(delete);
2006-10-04 08:30:14 +08:00
Datum delete(PG_FUNCTION_ARGS);
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);
HStore *out = palloc(VARSIZE(hs));
2006-10-04 08:30:14 +08:00
char *ptrs,
*ptrd;
HEntry *es,
*ed;
SET_VARSIZE(out, VARSIZE(hs));
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));
ed->pos = ptrd - STRPTR(out);
2006-10-04 08:30:14 +08:00
ptrd += es->keylen + ((es->valisnull) ? 0 : es->vallen);
ed++;
}
2006-10-04 08:30:14 +08:00
ptrs += es->keylen + ((es->valisnull) ? 0 : es->vallen);
es++;
}
2006-10-04 08:30:14 +08:00
if (ed - ARRPTR(out) != out->size)
{
int buflen = ptrd - STRPTR(out);
ptrd = STRPTR(out);
out->size = ed - ARRPTR(out);
2006-10-04 08:30:14 +08:00
memmove(STRPTR(out), ptrd, buflen);
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
}
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_FREE_IF_COPY(key, 1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(hs_concat);
2006-10-04 08:30:14 +08:00
Datum hs_concat(PG_FUNCTION_ARGS);
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);
HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
2006-10-04 08:30:14 +08:00
char *ps1,
*ps2,
*pd;
HEntry *es1,
*es2,
*ed;
SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2));
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);
else
2006-10-04 08:30:14 +08:00
difference = (es1->keylen > es2->keylen) ? 1 : -1;
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));
ed->pos = pd - STRPTR(out);
2006-10-04 08:30:14 +08:00
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
ed++;
2006-10-04 08:30:14 +08:00
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
es1++;
2006-10-04 08:30:14 +08:00
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
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));
ed->pos = pd - STRPTR(out);
2006-10-04 08:30:14 +08:00
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
ed++;
2006-10-04 08:30:14 +08:00
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
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));
ed->pos = pd - STRPTR(out);
2006-10-04 08:30:14 +08:00
pd += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
ed++;
2006-10-04 08:30:14 +08:00
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
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));
ed->pos = pd - STRPTR(out);
2006-10-04 08:30:14 +08:00
pd += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
ed++;
2006-10-04 08:30:14 +08:00
ps1 += es1->keylen + ((es1->valisnull) ? 0 : es1->vallen);
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));
ed->pos = pd - STRPTR(out);
2006-10-04 08:30:14 +08:00
pd += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
ed++;
2006-10-04 08:30:14 +08:00
ps2 += es2->keylen + ((es2->valisnull) ? 0 : es2->vallen);
es2++;
}
2006-10-04 08:30:14 +08:00
if (ed - ARRPTR(out) != out->size)
{
int buflen = pd - STRPTR(out);
pd = STRPTR(out);
out->size = ed - ARRPTR(out);
2006-10-04 08:30:14 +08:00
memmove(STRPTR(out), pd, buflen);
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
}
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(s1, 0);
PG_FREE_IF_COPY(s2, 1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(tconvert);
2006-10-04 08:30:14 +08:00
Datum tconvert(PG_FUNCTION_ARGS);
Datum
2006-10-04 08:30:14 +08:00
tconvert(PG_FUNCTION_ARGS)
{
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))
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));
else
{
val = PG_GETARG_TEXT_P(1);
len = CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2 * VARHDRSZ);
}
out = palloc(len);
SET_VARSIZE(out, len);
2006-10-04 08:30:14 +08:00
out->size = 1;
ARRPTR(out)->keylen = VARSIZE(key) - VARHDRSZ;
2007-11-16 05:14:46 +08:00
if (PG_ARGISNULL(1))
{
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-10-04 08:30:14 +08:00
memcpy(STRPTR(out), VARDATA(key), ARRPTR(out)->keylen);
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);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(akeys);
2006-10-04 08:30:14 +08:00
Datum akeys(PG_FUNCTION_ARGS);
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)
{
text *item;
2006-10-04 08:30:14 +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);
ptr++;
}
2006-10-04 08:30:14 +08:00
a = construct_array(d,
2006-10-04 08:30:14 +08:00
hs->size,
TEXTOID,
-1,
false,
'i');
2006-10-04 08:30:14 +08:00
ptr = ARRPTR(hs);
while (ptr - ARRPTR(hs) < hs->size)
{
pfree(DatumGetPointer(d[ptr - ARRPTR(hs)]));
ptr++;
}
pfree(d);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_RETURN_POINTER(a);
}
PG_FUNCTION_INFO_V1(avals);
2006-10-04 08:30:14 +08:00
Datum avals(PG_FUNCTION_ARGS);
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)
{
text *item;
2006-10-04 08:30:14 +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);
ptr++;
}
2006-10-04 08:30:14 +08:00
a = construct_array(d,
2006-10-04 08:30:14 +08:00
hs->size,
TEXTOID,
-1,
false,
'i');
2006-10-04 08:30:14 +08:00
ptr = ARRPTR(hs);
while (ptr - ARRPTR(hs) < hs->size)
{
pfree(DatumGetPointer(d[ptr - ARRPTR(hs)]));
ptr++;
}
pfree(d);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
PG_RETURN_POINTER(a);
}
2006-10-04 08:30:14 +08:00
typedef struct
{
HStore *hs;
int i;
} AKStore;
static void
2006-10-04 08:30:14 +08:00
setup_firstcall(FuncCallContext *funcctx, HStore * hs)
{
MemoryContext oldcontext;
AKStore *st;
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2006-10-04 08:30:14 +08:00
st = (AKStore *) palloc(sizeof(AKStore));
st->i = 0;
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;
MemoryContextSwitchTo(oldcontext);
}
PG_FUNCTION_INFO_V1(skeys);
2006-10-04 08:30:14 +08:00
Datum skeys(PG_FUNCTION_ARGS);
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);
funcctx = SRF_FIRSTCALL_INIT();
setup_firstcall(funcctx, hs);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
}
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]);
text *item;
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos,
ptr->keylen);
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);
}
PG_FUNCTION_INFO_V1(svals);
2006-10-04 08:30:14 +08:00
Datum svals(PG_FUNCTION_ARGS);
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);
funcctx = SRF_FIRSTCALL_INIT();
setup_firstcall(funcctx, hs);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
}
funcctx = SRF_PERCALL_SETUP();
2006-10-04 08:30:14 +08:00
st = (AKStore *) funcctx->user_fctx;
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;
st->i++;
(funcctx)->call_cntr++;
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
rsi->isDone = ExprMultipleResult;
PG_RETURN_NULL();
2006-10-04 08:30:14 +08:00
}
else
{
text *item;
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen,
ptr->vallen);
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);
}
PG_FUNCTION_INFO_V1(hs_contains);
2006-10-04 08:30:14 +08:00
Datum hs_contains(PG_FUNCTION_ARGS);
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))
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
res = false;
te++;
}
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(val, 0);
PG_FREE_IF_COPY(tmpl, 1);
PG_RETURN_BOOL(res);
}
2006-10-04 08:30:14 +08:00
PG_FUNCTION_INFO_V1(hs_contained);
2006-10-04 08:30:14 +08:00
Datum hs_contained(PG_FUNCTION_ARGS);
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)
));
}
PG_FUNCTION_INFO_V1(each);
2006-10-04 08:30:14 +08:00
Datum each(PG_FUNCTION_ARGS);
Datum
2006-10-04 08:30:14 +08:00
each(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
AKStore *st;
2006-10-04 08:30:14 +08:00
if (SRF_IS_FIRSTCALL())
{
TupleDesc tupdesc;
MemoryContext oldcontext;
HStore *hs = PG_GETARG_HS(0);
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;
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;
/* 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");
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
MemoryContextSwitchTo(oldcontext);
2006-10-04 08:30:14 +08:00
PG_FREE_IF_COPY(hs, 0);
}
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];
bool nulls[2] = {false, false};
2006-10-04 08:30:14 +08:00
text *item;
HeapTuple tuple;
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos, ptr->keylen);
dvalues[0] = PointerGetDatum(item);
2006-10-04 08:30:14 +08:00
if (ptr->valisnull)
{
dvalues[1] = (Datum) 0;
nulls[1] = true;
2006-10-04 08:30:14 +08:00
}
else
{
item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen,
ptr->vallen);
dvalues[1] = PointerGetDatum(item);
}
st->i++;
tuple = heap_form_tuple(funcctx->attinmeta->tupdesc, dvalues, nulls);
res = HeapTupleGetDatum(tuple);
2006-10-04 08:30:14 +08:00
pfree(DatumGetPointer(dvalues[0]));
if (!nulls[1])
2006-10-04 08:30:14 +08:00
pfree(DatumGetPointer(dvalues[1]));
SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
}
2006-10-04 08:30:14 +08:00
pfree(st->hs);
pfree(st);
2006-10-04 08:30:14 +08:00
SRF_RETURN_DONE(funcctx);
}