2003-08-04 08:43:34 +08:00
|
|
|
/*
|
2003-07-21 18:27:44 +08:00
|
|
|
* simple but fast map from str to Oid
|
|
|
|
* Teodor Sigaev <teodor@sigaev.ru>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2005-05-07 01:24:55 +08:00
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
#include "snmap.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
static int
|
2003-08-04 08:43:34 +08:00
|
|
|
compareSNMapEntry(const void *a, const void *b)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
if (((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp)
|
2004-06-01 00:51:56 +08:00
|
|
|
return -1;
|
2004-08-29 13:07:03 +08:00
|
|
|
else if (((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp)
|
2004-06-01 00:51:56 +08:00
|
|
|
return 1;
|
2004-08-29 13:07:03 +08:00
|
|
|
else
|
2004-06-01 00:51:56 +08:00
|
|
|
return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
void
|
|
|
|
addSNMap(SNMap * map, char *key, Oid value)
|
|
|
|
{
|
|
|
|
if (map->len >= map->reallen)
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
SNMapEntry *tmp;
|
2003-08-04 08:43:34 +08:00
|
|
|
int len = (map->reallen) ? 2 * map->reallen : 16;
|
|
|
|
|
|
|
|
tmp = (SNMapEntry *) realloc(map->list, sizeof(SNMapEntry) * len);
|
|
|
|
if (!tmp)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2003-08-04 08:43:34 +08:00
|
|
|
map->reallen = len;
|
|
|
|
map->list = tmp;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
map->list[map->len].key = strdup(key);
|
|
|
|
if (!map->list[map->len].key)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2004-06-01 00:51:56 +08:00
|
|
|
map->list[map->len].nsp = get_oidnamespace(TSNSP_FunctionOid);
|
2003-08-04 08:43:34 +08:00
|
|
|
map->list[map->len].value = value;
|
2003-07-21 18:27:44 +08:00
|
|
|
map->len++;
|
2003-08-04 08:43:34 +08:00
|
|
|
if (map->len > 1)
|
|
|
|
qsort(map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
void
|
|
|
|
addSNMap_t(SNMap * map, text *key, Oid value)
|
|
|
|
{
|
|
|
|
char *k = text2char(key);
|
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
addSNMap(map, k, value);
|
|
|
|
pfree(k);
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
Oid
|
|
|
|
findSNMap(SNMap * map, char *key)
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
SNMapEntry *ptr;
|
2004-04-02 07:44:38 +08:00
|
|
|
SNMapEntry ks;
|
|
|
|
|
|
|
|
ks.key = key;
|
2004-06-01 00:51:56 +08:00
|
|
|
ks.nsp = get_oidnamespace(TSNSP_FunctionOid);
|
2004-04-02 07:44:38 +08:00
|
|
|
ks.value = 0;
|
2003-08-04 08:43:34 +08:00
|
|
|
|
|
|
|
if (map->len == 0 || !map->list)
|
|
|
|
return 0;
|
|
|
|
ptr = (SNMapEntry *) bsearch(&ks, map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry);
|
2003-07-21 18:27:44 +08:00
|
|
|
return (ptr) ? ptr->value : 0;
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
Oid
|
|
|
|
findSNMap_t(SNMap * map, text *key)
|
|
|
|
{
|
|
|
|
char *k = text2char(key);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = findSNMap(map, k);
|
2003-07-21 18:27:44 +08:00
|
|
|
pfree(k);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
void
|
|
|
|
freeSNMap(SNMap * map)
|
|
|
|
{
|
|
|
|
SNMapEntry *entry = map->list;
|
|
|
|
|
|
|
|
if (map->list)
|
|
|
|
{
|
|
|
|
while (map->len)
|
|
|
|
{
|
|
|
|
if (entry->key)
|
|
|
|
free(entry->key);
|
|
|
|
entry++;
|
|
|
|
map->len--;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
free(map->list);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
memset(map, 0, sizeof(SNMap));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|