postgresql/contrib/hstore/hstore_gin.c

136 lines
2.7 KiB
C
Raw Normal View History

#include "hstore.h"
2007-11-15 22:14:46 +01:00
#include "access/gin.h"
2007-11-15 22:14:46 +01:00
#define KEYFLAG 'K'
#define VALFLAG 'V'
#define NULLFLAG 'N'
PG_FUNCTION_INFO_V1(gin_extract_hstore);
2007-11-15 22:14:46 +01:00
Datum gin_extract_hstore(PG_FUNCTION_ARGS);
2007-11-15 22:14:46 +01:00
static text *
makeitem(char *str, int len)
{
2007-11-15 22:14:46 +01:00
text *item;
2007-11-15 22:14:46 +01:00
item = (text *) palloc(VARHDRSZ + len + 1);
SET_VARSIZE(item, VARHDRSZ + len + 1);
2007-11-15 22:14:46 +01:00
if (str && len > 0)
memcpy(VARDATA(item) + 1, str, len);
return item;
}
Datum
gin_extract_hstore(PG_FUNCTION_ARGS)
{
2007-11-15 22:14:46 +01:00
HStore *hs = PG_GETARG_HS(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
2007-11-15 22:14:46 +01:00
*nentries = 2 * hs->size;
2007-11-15 22:14:46 +01:00
if (hs->size > 0)
{
2007-11-15 22:14:46 +01:00
HEntry *ptr = ARRPTR(hs);
char *words = STRPTR(hs);
int i = 0;
2007-11-15 22:14:46 +01:00
entries = (Datum *) palloc(sizeof(Datum) * 2 * hs->size);
while (ptr - ARRPTR(hs) < hs->size)
{
2007-11-15 22:14:46 +01:00
text *item;
2007-11-15 22:14:46 +01:00
item = makeitem(words + ptr->pos, ptr->keylen);
*VARDATA(item) = KEYFLAG;
entries[i++] = PointerGetDatum(item);
2007-11-15 22:14:46 +01:00
if (ptr->valisnull)
{
2007-11-15 22:14:46 +01:00
item = makeitem(NULL, 0);
*VARDATA(item) = NULLFLAG;
}
else
{
2007-11-15 22:14:46 +01:00
item = makeitem(words + ptr->pos + ptr->keylen, ptr->vallen);
*VARDATA(item) = VALFLAG;
}
entries[i++] = PointerGetDatum(item);
ptr++;
}
}
2007-11-15 22:14:46 +01:00
PG_FREE_IF_COPY(hs, 0);
PG_RETURN_POINTER(entries);
}
PG_FUNCTION_INFO_V1(gin_extract_hstore_query);
2007-11-15 22:14:46 +01:00
Datum gin_extract_hstore_query(PG_FUNCTION_ARGS);
Datum
gin_extract_hstore_query(PG_FUNCTION_ARGS)
{
StrategyNumber strategy = PG_GETARG_UINT16(2);
2007-11-15 22:14:46 +01:00
if (strategy == HStoreContainsStrategyNumber)
{
2007-11-15 22:14:46 +01:00
PG_RETURN_DATUM(DirectFunctionCall2(
gin_extract_hstore,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
));
}
2007-11-15 22:14:46 +01:00
else if (strategy == HStoreExistsStrategyNumber)
{
2007-11-15 22:14:46 +01:00
text *item,
*q = PG_GETARG_TEXT_P(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
*nentries = 1;
2007-11-15 22:14:46 +01:00
entries = (Datum *) palloc(sizeof(Datum));
2007-11-15 22:14:46 +01:00
item = makeitem(VARDATA(q), VARSIZE(q) - VARHDRSZ);
*VARDATA(item) = KEYFLAG;
entries[0] = PointerGetDatum(item);
PG_RETURN_POINTER(entries);
}
else
elog(ERROR, "Unsupported strategy number: %d", strategy);
PG_RETURN_POINTER(NULL);
}
PG_FUNCTION_INFO_V1(gin_consistent_hstore);
2007-11-15 22:14:46 +01:00
Datum gin_consistent_hstore(PG_FUNCTION_ARGS);
Datum
gin_consistent_hstore(PG_FUNCTION_ARGS)
{
StrategyNumber strategy = PG_GETARG_UINT16(1);
2007-11-15 22:14:46 +01:00
bool res = true;
2007-11-15 22:14:46 +01:00
if (strategy == HStoreContainsStrategyNumber)
{
2007-11-15 22:14:46 +01:00
bool *check = (bool *) PG_GETARG_POINTER(0);
HStore *query = PG_GETARG_HS(2);
int i;
2007-11-15 22:14:46 +01:00
for (i = 0; res && i < 2 * query->size; i++)
if (check[i] == false)
res = false;
}
2007-11-15 22:14:46 +01:00
else if (strategy == HStoreExistsStrategyNumber)
res = true;
else
elog(ERROR, "Unsupported strategy number: %d", strategy);
PG_RETURN_BOOL(res);
}