2008-05-12 08:00:54 +08:00
|
|
|
/*
|
2010-02-26 10:01:40 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/hstore/hstore_gin.c,v 1.8 2010/02/26 02:00:32 momjian Exp $
|
2008-05-12 08:00:54 +08:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
#include "access/gin.h"
|
2009-10-01 03:50:22 +08:00
|
|
|
#include "catalog/pg_type.h"
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "hstore.h"
|
|
|
|
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
#define KEYFLAG 'K'
|
|
|
|
#define VALFLAG 'V'
|
|
|
|
#define NULLFLAG 'N'
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_extract_hstore);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum gin_extract_hstore(PG_FUNCTION_ARGS);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
static text *
|
|
|
|
makeitem(char *str, int len)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
text *item;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
item = (text *) palloc(VARHDRSZ + len + 1);
|
2007-03-14 22:21:53 +08:00
|
|
|
SET_VARSIZE(item, VARHDRSZ + len + 1);
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
if (str && len > 0)
|
|
|
|
memcpy(VARDATA(item) + 1, str, len);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_extract_hstore(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
HStore *hs = PG_GETARG_HS(0);
|
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
|
|
|
Datum *entries = NULL;
|
2010-02-26 10:01:40 +08:00
|
|
|
HEntry *hsent = ARRPTR(hs);
|
|
|
|
char *ptr = STRPTR(hs);
|
|
|
|
int count = HS_COUNT(hs);
|
|
|
|
int i;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
*nentries = 2 * count;
|
|
|
|
if (count)
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * 2 * count);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
for (i = 0; i < count; ++i)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
text *item;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2010-02-26 10:01:40 +08:00
|
|
|
item = makeitem(HS_KEY(hsent, ptr, i), HS_KEYLEN(hsent, i));
|
2009-10-01 03:50:22 +08:00
|
|
|
*VARDATA(item) = KEYFLAG;
|
2010-02-26 10:01:40 +08:00
|
|
|
entries[2 * i] = PointerGetDatum(item);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2010-02-26 10:01:40 +08:00
|
|
|
if (HS_VALISNULL(hsent, i))
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
item = makeitem(NULL, 0);
|
|
|
|
*VARDATA(item) = NULLFLAG;
|
2007-03-14 22:21:53 +08:00
|
|
|
}
|
2009-10-01 03:50:22 +08:00
|
|
|
else
|
|
|
|
{
|
2010-02-26 10:01:40 +08:00
|
|
|
item = makeitem(HS_VAL(hsent, ptr, i), HS_VALLEN(hsent, i));
|
2009-10-01 03:50:22 +08:00
|
|
|
*VARDATA(item) = VALFLAG;
|
|
|
|
}
|
2010-02-26 10:01:40 +08:00
|
|
|
entries[2 * i + 1] = PointerGetDatum(item);
|
2007-03-14 22:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_extract_hstore_query);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum gin_extract_hstore_query(PG_FUNCTION_ARGS);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_extract_hstore_query(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(2);
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
if (strategy == HStoreContainsStrategyNumber)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(gin_extract_hstore,
|
2007-11-16 05:14:46 +08:00
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)
|
|
|
|
));
|
2007-03-14 22:21:53 +08:00
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
else if (strategy == HStoreExistsStrategyNumber)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
text *item,
|
2009-10-01 03:50:22 +08:00
|
|
|
*query = PG_GETARG_TEXT_PP(0);
|
2007-11-16 05:14:46 +08:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
|
|
|
Datum *entries = NULL;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
*nentries = 1;
|
2007-11-16 05:14:46 +08:00
|
|
|
entries = (Datum *) palloc(sizeof(Datum));
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
item = makeitem(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
|
2007-03-14 22:21:53 +08:00
|
|
|
*VARDATA(item) = KEYFLAG;
|
|
|
|
entries[0] = PointerGetDatum(item);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
2009-10-01 03:50:22 +08:00
|
|
|
else if (strategy == HStoreExistsAnyStrategyNumber ||
|
|
|
|
strategy == HStoreExistsAllStrategyNumber)
|
|
|
|
{
|
2010-02-26 10:01:40 +08:00
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
Datum *key_datums;
|
|
|
|
bool *key_nulls;
|
|
|
|
int key_count;
|
|
|
|
int i,
|
|
|
|
j;
|
2009-10-01 03:50:22 +08:00
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
|
|
|
Datum *entries = NULL;
|
2010-02-26 10:01:40 +08:00
|
|
|
text *item;
|
2009-10-01 03:50:22 +08:00
|
|
|
|
|
|
|
deconstruct_array(query,
|
|
|
|
TEXTOID, -1, false, 'i',
|
|
|
|
&key_datums, &key_nulls, &key_count);
|
|
|
|
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * key_count);
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < key_count; ++i)
|
|
|
|
{
|
|
|
|
if (key_nulls[i])
|
|
|
|
continue;
|
|
|
|
item = makeitem(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
|
|
|
|
*VARDATA(item) = KEYFLAG;
|
|
|
|
entries[j++] = PointerGetDatum(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
*nentries = j ? j : -1;
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
2007-03-14 22:21:53 +08:00
|
|
|
else
|
|
|
|
elog(ERROR, "Unsupported strategy number: %d", strategy);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_consistent_hstore);
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum gin_consistent_hstore(PG_FUNCTION_ARGS);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_consistent_hstore(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2008-04-15 01:05:34 +08:00
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
2007-03-14 22:21:53 +08:00
|
|
|
StrategyNumber strategy = PG_GETARG_UINT16(1);
|
2010-02-26 10:01:40 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
/* HStore *query = PG_GETARG_HS(2); */
|
|
|
|
int32 nkeys = PG_GETARG_INT32(3);
|
2010-02-26 10:01:40 +08:00
|
|
|
|
2009-03-26 06:19:02 +08:00
|
|
|
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(5);
|
2007-11-16 05:14:46 +08:00
|
|
|
bool res = true;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
*recheck = false;
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
if (strategy == HStoreContainsStrategyNumber)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
int i;
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/*
|
2009-06-11 22:49:15 +08:00
|
|
|
* Index lost information about correspondence of keys and values, so
|
2009-10-01 03:50:22 +08:00
|
|
|
* we need recheck (pre-8.4 this is handled at SQL level)
|
2008-04-15 01:05:34 +08:00
|
|
|
*/
|
|
|
|
*recheck = true;
|
2009-10-01 03:50:22 +08:00
|
|
|
for (i = 0; res && i < nkeys; i++)
|
2007-11-16 05:14:46 +08:00
|
|
|
if (check[i] == false)
|
2007-03-14 22:21:53 +08:00
|
|
|
res = false;
|
|
|
|
}
|
2007-11-16 05:14:46 +08:00
|
|
|
else if (strategy == HStoreExistsStrategyNumber)
|
2008-04-15 01:05:34 +08:00
|
|
|
{
|
|
|
|
/* Existence of key is guaranteed */
|
2007-03-14 22:21:53 +08:00
|
|
|
res = true;
|
2008-04-15 01:05:34 +08:00
|
|
|
}
|
2009-10-01 03:50:22 +08:00
|
|
|
else if (strategy == HStoreExistsAnyStrategyNumber)
|
|
|
|
{
|
|
|
|
/* Existence of key is guaranteed */
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
else if (strategy == HStoreExistsAllStrategyNumber)
|
|
|
|
{
|
2010-02-26 10:01:40 +08:00
|
|
|
int i;
|
2009-10-01 03:50:22 +08:00
|
|
|
|
|
|
|
for (i = 0; res && i < nkeys; ++i)
|
|
|
|
if (!check[i])
|
|
|
|
res = false;
|
|
|
|
}
|
2007-03-14 22:21:53 +08:00
|
|
|
else
|
|
|
|
elog(ERROR, "Unsupported strategy number: %d", strategy);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|