postgresql/contrib/hstore/hstore_op.c

631 lines
13 KiB
C
Raw Normal View History

#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)
{
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();
}
2006-10-04 08:30:14 +08:00
out = palloc(VARHDRSZ + entry->vallen);
memcpy(VARDATA(out), STRPTR(hs) + entry->pos + entry->keylen, entry->vallen);
VARATT_SIZEP(out) = VARHDRSZ + entry->vallen;
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(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(hs->len);
char *ptrs,
*ptrd;
HEntry *es,
*ed;
out->len = hs->len;
out->size = hs->size; /* temprorary! */
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);
out->len = 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(s1->len + s2->len);
char *ps1,
*ps2,
*pd;
HEntry *es1,
*es2,
*ed;
out->len = s1->len + s2->len;
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);
out->len = 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 = PG_GETARG_TEXT_P(0);
text *val = PG_GETARG_TEXT_P(1);
int len;
HStore *out;
len = CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2 * VARHDRSZ);
out = palloc(len);
2006-10-04 08:30:14 +08:00
out->len = len;
out->size = 1;
ARRPTR(out)->keylen = hstoreCheckKeyLen(VARSIZE(key) - VARHDRSZ);
ARRPTR(out)->vallen = hstoreCheckValLen(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);
memcpy(STRPTR(out) + ARRPTR(out)->keylen, VARDATA(val), ARRPTR(out)->vallen);
PG_FREE_IF_COPY(key, 0);
PG_FREE_IF_COPY(val, 1);
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 = (text *) palloc(VARHDRSZ + ptr->keylen);
VARATT_SIZEP(item) = VARHDRSZ + ptr->keylen;
memcpy(VARDATA(item), 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(
2006-10-04 08:30:14 +08:00
d,
hs->size,
TEXTOID,
-1,
false,
'i'
);
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)
{
int vallen = (ptr->valisnull) ? 0 : ptr->vallen;
text *item = (text *) palloc(VARHDRSZ + vallen);
VARATT_SIZEP(item) = VARHDRSZ + vallen;
memcpy(VARDATA(item), base + ptr->pos + ptr->keylen, 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(
2006-10-04 08:30:14 +08:00
d,
hs->size,
TEXTOID,
-1,
false,
'i'
);
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(hs->len);
memcpy(st->hs, hs, hs->len);
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 = (text *) palloc(VARHDRSZ + ptr->keylen);
2006-10-04 08:30:14 +08:00
VARATT_SIZEP(item) = VARHDRSZ + ptr->keylen;
memcpy(VARDATA(item), 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
{
int vallen = ptr->vallen;
text *item = (text *) palloc(VARHDRSZ + vallen);
2006-10-04 08:30:14 +08:00
VARATT_SIZEP(item) = VARHDRSZ + vallen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, 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)
{
if (!te->valisnull)
{
if (entry->valisnull || !(
te->vallen == entry->vallen &&
strncmp(
vv + entry->pos + entry->keylen,
tv + te->pos + te->keylen,
te->vallen) == 0
))
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(hs->len);
memcpy(st->hs, hs, hs->len);
funcctx->user_fctx = (void *) st;
tupdesc = RelationNameGetTupleDesc("hs_each");
funcctx->slot = TupleDescGetSlot(tupdesc);
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];
char nulls[] = {' ', ' '};
text *item;
HeapTuple tuple;
item = (text *) palloc(VARHDRSZ + ptr->keylen);
VARATT_SIZEP(item) = VARHDRSZ + ptr->keylen;
memcpy(VARDATA(item), 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] = 'n';
}
else
{
int vallen = ptr->vallen;
2006-10-04 08:30:14 +08:00
item = (text *) palloc(VARHDRSZ + vallen);
VARATT_SIZEP(item) = VARHDRSZ + vallen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, vallen);
dvalues[1] = PointerGetDatum(item);
}
st->i++;
tuple = heap_formtuple(funcctx->attinmeta->tupdesc, dvalues, nulls);
res = TupleGetDatum(funcctx->slot, tuple);
2006-10-04 08:30:14 +08:00
pfree(DatumGetPointer(dvalues[0]));
if (nulls[1] != 'n')
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);
}