1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* dynahash.c
|
2001-10-01 07:36:17 +02:00
|
|
|
* dynamic hash tables
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-03-05 16:59:11 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.67 2006/03/05 15:58:46 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
|
|
|
|
* Coded into C, with minor code improvements, and with hsearch(3) interface,
|
|
|
|
* by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
|
|
|
|
* also, hcreate/hdestroy routines added to simulate hsearch(3).
|
|
|
|
*
|
|
|
|
* These routines simulate hsearch(3) and family, with the important
|
|
|
|
* difference that the hash table is dynamic - can grow indefinitely
|
|
|
|
* beyond its original size (as supplied to hcreate()).
|
|
|
|
*
|
|
|
|
* Performance appears to be comparable to that of hsearch(3).
|
|
|
|
* The 'source-code' options referred to in hsearch(3)'s 'man' page
|
|
|
|
* are not implemented; otherwise functionality is identical.
|
|
|
|
*
|
|
|
|
* Compilation controls:
|
|
|
|
* DEBUG controls some informative traces, mainly for debugging.
|
|
|
|
* HASH_STATISTICS causes HashAccesses and HashCollisions to be maintained;
|
|
|
|
* when combined with HASH_DEBUG, these are displayed by hdestroy().
|
|
|
|
*
|
|
|
|
* Problems & fixes to ejp@ausmelb.oz. WARNING: relies on pre-processor
|
|
|
|
* concatenation property, in probably unnecessary code 'optimisation'.
|
|
|
|
*
|
|
|
|
* Modified margo@postgres.berkeley.edu February 1990
|
1997-09-07 07:04:48 +02:00
|
|
|
* added multiple table interface
|
1996-07-09 08:22:35 +02:00
|
|
|
* Modified by sullivan@postgres.berkeley.edu April 1990
|
1997-09-07 07:04:48 +02:00
|
|
|
* changed ctl structure for shared memory
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-07-17 22:18:55 +02:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
2001-10-01 07:36:17 +02:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
#include "storage/shmem.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "utils/dynahash.h"
|
|
|
|
#include "utils/hsearch.h"
|
|
|
|
#include "utils/memutils.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2001-10-01 07:36:17 +02:00
|
|
|
* Key (also entry) part of a HASHELEMENT
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
#define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/*
|
|
|
|
* Fast MOD arithmetic, assuming that y is a power of 2 !
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#define MOD(x,y) ((x) & ((y)-1))
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Private function prototypes
|
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
static void *DynaHashAlloc(Size size);
|
2001-10-01 07:36:17 +02:00
|
|
|
static HASHSEGMENT seg_alloc(HTAB *hashp);
|
2004-09-28 22:46:37 +02:00
|
|
|
static bool element_alloc(HTAB *hashp, int nelem);
|
2001-10-25 07:50:21 +02:00
|
|
|
static bool dir_realloc(HTAB *hashp);
|
|
|
|
static bool expand_table(HTAB *hashp);
|
2004-10-22 09:21:06 +02:00
|
|
|
static void hdefault(HTAB *hashp);
|
2001-10-25 07:50:21 +02:00
|
|
|
static bool init_htab(HTAB *hashp, long nelem);
|
2001-10-05 19:28:13 +02:00
|
|
|
static void hash_corrupted(HTAB *hashp);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/*
|
2005-05-06 02:19:14 +02:00
|
|
|
* memory allocation support
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-06-22 21:16:24 +02:00
|
|
|
static MemoryContext CurrentDynaHashCxt = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
static void *
|
|
|
|
DynaHashAlloc(Size size)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-22 21:16:24 +02:00
|
|
|
Assert(MemoryContextIsValid(CurrentDynaHashCxt));
|
|
|
|
return MemoryContextAlloc(CurrentDynaHashCxt, size);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#if HASH_STATISTICS
|
1997-09-08 04:41:22 +02:00
|
|
|
static long hash_accesses,
|
|
|
|
hash_collisions,
|
|
|
|
hash_expansions;
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/************************** CREATE ROUTINES **********************/
|
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/*
|
|
|
|
* hash_create -- create a new dynamic hash table
|
|
|
|
*
|
|
|
|
* tabname: a name for the table (for debugging purposes)
|
|
|
|
* nelem: maximum number of elements expected
|
|
|
|
* *info: additional table parameters, as indicated by flags
|
|
|
|
* flags: bitmask indicating which parameters to take from *info
|
|
|
|
*
|
|
|
|
* Note: for a shared-memory hashtable, nelem needs to be a pretty good
|
|
|
|
* estimate, since we can't expand the table on the fly. But an unshared
|
|
|
|
* hashtable can be expanded on-the-fly, so it's better for nelem to be
|
|
|
|
* on the small side and let the table grow if it's exceeded. An overly
|
|
|
|
* large nelem will penalize hash_seq_search speed without buying much.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
HTAB *
|
2001-10-05 19:28:13 +02:00
|
|
|
hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HTAB *hashp;
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* For shared hash tables, we have a local hash header (HTAB struct) that
|
|
|
|
* we allocate in TopMemoryContext; all else is in shared memory.
|
2005-05-06 02:19:14 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* For non-shared hash tables, everything including the hash header is in
|
|
|
|
* a memory context created specially for the hash table --- this makes
|
2005-10-15 04:49:52 +02:00
|
|
|
* hash_destroy very simple. The memory context is made a child of either
|
|
|
|
* a context specified by the caller, or TopMemoryContext if nothing is
|
|
|
|
* specified.
|
2005-05-06 02:19:14 +02:00
|
|
|
*/
|
|
|
|
if (flags & HASH_SHARED_MEM)
|
|
|
|
{
|
|
|
|
/* Set up to allocate the hash header */
|
|
|
|
CurrentDynaHashCxt = TopMemoryContext;
|
|
|
|
}
|
2001-06-22 21:16:24 +02:00
|
|
|
else
|
2005-05-06 02:19:14 +02:00
|
|
|
{
|
|
|
|
/* Create the hash table's private memory context */
|
|
|
|
if (flags & HASH_CONTEXT)
|
|
|
|
CurrentDynaHashCxt = info->hcxt;
|
|
|
|
else
|
|
|
|
CurrentDynaHashCxt = TopMemoryContext;
|
|
|
|
CurrentDynaHashCxt = AllocSetContextCreate(CurrentDynaHashCxt,
|
|
|
|
tabname,
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/* Initialize the hash header, plus a copy of the table name */
|
2005-10-15 04:49:52 +02:00
|
|
|
hashp = (HTAB *) DynaHashAlloc(sizeof(HTAB) + strlen(tabname) +1);
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(hashp, 0, sizeof(HTAB));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
hashp->tabname = (char *) (hashp + 1);
|
2001-10-05 19:28:13 +02:00
|
|
|
strcpy(hashp->tabname, tabname);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (flags & HASH_FUNCTION)
|
|
|
|
hashp->hash = info->hash;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
hashp->hash = string_hash; /* default hash function */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If you don't specify a match function, it defaults to strncmp() if you
|
|
|
|
* used string_hash (either explicitly or by default) and to memcmp()
|
|
|
|
* otherwise. (Prior to PostgreSQL 7.4, memcmp() was always used.)
|
2003-08-19 03:13:41 +02:00
|
|
|
*/
|
|
|
|
if (flags & HASH_COMPARE)
|
|
|
|
hashp->match = info->match;
|
|
|
|
else if (hashp->hash == string_hash)
|
|
|
|
hashp->match = (HashCompareFunc) strncmp;
|
|
|
|
else
|
|
|
|
hashp->match = memcmp;
|
|
|
|
|
2005-06-18 22:51:30 +02:00
|
|
|
/*
|
|
|
|
* Similarly, the key-copying function defaults to strncpy() or memcpy().
|
|
|
|
*/
|
|
|
|
if (flags & HASH_KEYCOPY)
|
|
|
|
hashp->keycopy = info->keycopy;
|
|
|
|
else if (hashp->hash == string_hash)
|
|
|
|
hashp->keycopy = (HashCopyFunc) strncpy;
|
|
|
|
else
|
|
|
|
hashp->keycopy = memcpy;
|
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
if (flags & HASH_ALLOC)
|
|
|
|
hashp->alloc = info->alloc;
|
|
|
|
else
|
|
|
|
hashp->alloc = DynaHashAlloc;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (flags & HASH_SHARED_MEM)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* ctl structure is preallocated for shared memory tables. Note that
|
|
|
|
* HASH_DIRSIZE and HASH_ALLOC had better be set as well.
|
1999-02-22 07:16:57 +01:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->hctl = info->hctl;
|
|
|
|
hashp->dir = info->dir;
|
2001-06-22 21:16:24 +02:00
|
|
|
hashp->hcxt = NULL;
|
2001-10-05 19:28:13 +02:00
|
|
|
hashp->isshared = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* hash table already exists, we're just attaching to it */
|
|
|
|
if (flags & HASH_ATTACH)
|
1998-09-01 05:29:17 +02:00
|
|
|
return hashp;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* setup hash table defaults */
|
1999-02-22 07:16:57 +01:00
|
|
|
hashp->hctl = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
hashp->dir = NULL;
|
2003-08-19 03:13:41 +02:00
|
|
|
hashp->hcxt = CurrentDynaHashCxt;
|
2001-10-05 19:28:13 +02:00
|
|
|
hashp->isshared = false;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!hashp->hctl)
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->hctl = (HASHHDR *) hashp->alloc(sizeof(HASHHDR));
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!hashp->hctl)
|
2004-10-25 02:46:43 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-10-22 09:21:06 +02:00
|
|
|
hdefault(hashp);
|
2001-10-01 07:36:17 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl = hashp->hctl;
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef HASH_STATISTICS
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl->accesses = hctl->collisions = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (flags & HASH_SEGMENT)
|
|
|
|
{
|
|
|
|
hctl->ssize = info->ssize;
|
|
|
|
hctl->sshift = my_log2(info->ssize);
|
1999-05-31 19:01:52 +02:00
|
|
|
/* ssize had better be a power of 2 */
|
|
|
|
Assert(hctl->ssize == (1L << hctl->sshift));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (flags & HASH_FFACTOR)
|
|
|
|
hctl->ffactor = info->ffactor;
|
|
|
|
|
|
|
|
/*
|
1999-02-22 07:16:57 +01:00
|
|
|
* SHM hash tables have fixed directory size passed by the caller.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (flags & HASH_DIRSIZE)
|
|
|
|
{
|
1999-02-22 07:16:57 +01:00
|
|
|
hctl->max_dsize = info->max_dsize;
|
|
|
|
hctl->dsize = info->dsize;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* hash table now allocates space for key and data but you have to say how
|
|
|
|
* much space to allocate
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (flags & HASH_ELEM)
|
|
|
|
{
|
2005-05-06 02:19:14 +02:00
|
|
|
Assert(info->entrysize >= info->keysize);
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl->keysize = info->keysize;
|
2001-10-01 07:36:17 +02:00
|
|
|
hctl->entrysize = info->entrysize;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2001-10-01 07:36:17 +02:00
|
|
|
|
2004-09-28 22:46:37 +02:00
|
|
|
/* Build the hash directory structure */
|
2001-10-01 07:36:17 +02:00
|
|
|
if (!init_htab(hashp, nelem))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
hash_destroy(hashp);
|
2004-10-25 02:46:43 +02:00
|
|
|
elog(ERROR, "failed to initialize hash table");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2004-09-28 22:46:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For a shared hash table, preallocate the requested number of elements.
|
|
|
|
* This reduces problems with run-time out-of-shared-memory conditions.
|
|
|
|
*/
|
|
|
|
if (flags & HASH_SHARED_MEM)
|
|
|
|
{
|
|
|
|
if (!element_alloc(hashp, (int) nelem))
|
|
|
|
{
|
|
|
|
hash_destroy(hashp);
|
2004-10-25 02:46:43 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2004-09-28 22:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return hashp;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-10-01 07:36:17 +02:00
|
|
|
* Set default HASHHDR parameters.
|
1999-02-22 07:16:57 +01:00
|
|
|
*/
|
2004-10-22 09:21:06 +02:00
|
|
|
static void
|
1997-09-08 23:56:23 +02:00
|
|
|
hdefault(HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
MemSet(hctl, 0, sizeof(HASHHDR));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
hctl->ssize = DEF_SEGSIZE;
|
|
|
|
hctl->sshift = DEF_SEGSIZE_SHIFT;
|
|
|
|
hctl->dsize = DEF_DIRSIZE;
|
|
|
|
hctl->ffactor = DEF_FFACTOR;
|
2001-10-01 07:36:17 +02:00
|
|
|
hctl->nentries = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl->nsegs = 0;
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* rather pointless defaults for key & entry size */
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl->keysize = sizeof(char *);
|
2001-10-01 07:36:17 +02:00
|
|
|
hctl->entrysize = 2 * sizeof(char *);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* table has no fixed maximum size */
|
|
|
|
hctl->max_dsize = NO_MAX_DSIZE;
|
|
|
|
|
|
|
|
/* garbage collection for HASH_REMOVE */
|
2001-10-01 07:36:17 +02:00
|
|
|
hctl->freeList = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
static bool
|
|
|
|
init_htab(HTAB *hashp, long nelem)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHSEGMENT *segp;
|
2005-06-27 01:32:34 +02:00
|
|
|
long lnbuckets;
|
1998-02-26 05:46:47 +01:00
|
|
|
int nbuckets;
|
|
|
|
int nsegs;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* Divide number of elements by the fill factor to determine a desired
|
2005-10-15 04:49:52 +02:00
|
|
|
* number of buckets. Allocate space for the next greater power of two
|
|
|
|
* number of buckets
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-27 01:32:34 +02:00
|
|
|
lnbuckets = (nelem - 1) / hctl->ffactor + 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-06-27 01:32:34 +02:00
|
|
|
nbuckets = 1 << my_log2(lnbuckets);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
hctl->max_bucket = hctl->low_mask = nbuckets - 1;
|
|
|
|
hctl->high_mask = (nbuckets << 1) - 1;
|
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Figure number of directory segments needed, round up to a power of 2
|
1999-02-22 07:16:57 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
nsegs = (nbuckets - 1) / hctl->ssize + 1;
|
|
|
|
nsegs = 1 << my_log2(nsegs);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make sure directory is big enough. If pre-allocated directory is too
|
|
|
|
* small, choke (caller screwed up).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-02-22 07:16:57 +01:00
|
|
|
if (nsegs > hctl->dsize)
|
|
|
|
{
|
|
|
|
if (!(hashp->dir))
|
|
|
|
hctl->dsize = nsegs;
|
|
|
|
else
|
2001-10-01 07:36:17 +02:00
|
|
|
return false;
|
1999-02-22 07:16:57 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
/* Allocate a directory */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!(hashp->dir))
|
|
|
|
{
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = hashp->hcxt;
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->dir = (HASHSEGMENT *)
|
|
|
|
hashp->alloc(hctl->dsize * sizeof(HASHSEGMENT));
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!hashp->dir)
|
2001-10-01 07:36:17 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate initial segments */
|
|
|
|
for (segp = hashp->dir; hctl->nsegs < nsegs; hctl->nsegs++, segp++)
|
|
|
|
{
|
|
|
|
*segp = seg_alloc(hashp);
|
2001-10-01 07:36:17 +02:00
|
|
|
if (*segp == NULL)
|
|
|
|
return false;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-06-27 01:32:34 +02:00
|
|
|
/* Choose number of entries to allocate at a time */
|
|
|
|
hctl->nelem_alloc = (int) Min(nelem, HASHELEMENT_ALLOC_MAX);
|
|
|
|
hctl->nelem_alloc = Max(hctl->nelem_alloc, 1);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#if HASH_DEBUG
|
2002-03-09 18:35:37 +01:00
|
|
|
fprintf(stderr, "init_htab:\n%s%p\n%s%ld\n%s%ld\n%s%d\n%s%ld\n%s%u\n%s%x\n%s%x\n%s%ld\n%s%ld\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
"TABLE POINTER ", hashp,
|
|
|
|
"DIRECTORY SIZE ", hctl->dsize,
|
|
|
|
"SEGMENT SIZE ", hctl->ssize,
|
|
|
|
"SEGMENT SHIFT ", hctl->sshift,
|
|
|
|
"FILL FACTOR ", hctl->ffactor,
|
|
|
|
"MAX BUCKET ", hctl->max_bucket,
|
|
|
|
"HIGH MASK ", hctl->high_mask,
|
|
|
|
"LOW MASK ", hctl->low_mask,
|
|
|
|
"NSEGS ", hctl->nsegs,
|
2001-10-01 07:36:17 +02:00
|
|
|
"NENTRIES ", hctl->nentries);
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
2001-10-01 07:36:17 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
/*
|
|
|
|
* Estimate the space needed for a hashtable containing the given number
|
|
|
|
* of entries of given size.
|
|
|
|
* NOTE: this is used to estimate the footprint of hashtables in shared
|
|
|
|
* memory; therefore it does not count HTAB which is in local memory.
|
|
|
|
* NB: assumes that all hash structure parameters have default values!
|
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2003-08-19 03:13:41 +02:00
|
|
|
hash_estimate_size(long num_entries, Size entrysize)
|
1999-02-22 07:16:57 +01:00
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
Size size;
|
1999-05-25 18:15:34 +02:00
|
|
|
long nBuckets,
|
|
|
|
nSegments,
|
|
|
|
nDirEntries,
|
2001-10-01 07:36:17 +02:00
|
|
|
nElementAllocs,
|
2005-06-27 01:32:34 +02:00
|
|
|
elementSize,
|
|
|
|
elementAllocCnt;
|
1999-02-22 07:16:57 +01:00
|
|
|
|
|
|
|
/* estimate number of buckets wanted */
|
|
|
|
nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1);
|
|
|
|
/* # of segments needed for nBuckets */
|
1999-03-06 22:17:56 +01:00
|
|
|
nSegments = 1L << my_log2((nBuckets - 1) / DEF_SEGSIZE + 1);
|
|
|
|
/* directory entries */
|
|
|
|
nDirEntries = DEF_DIRSIZE;
|
|
|
|
while (nDirEntries < nSegments)
|
|
|
|
nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
|
1999-02-22 07:16:57 +01:00
|
|
|
|
|
|
|
/* fixed control info */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = MAXALIGN(sizeof(HASHHDR)); /* but not HTAB, per above */
|
1999-03-06 22:17:56 +01:00
|
|
|
/* directory */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size, mul_size(nDirEntries, sizeof(HASHSEGMENT)));
|
1999-02-22 07:16:57 +01:00
|
|
|
/* segments */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size, mul_size(nSegments,
|
2005-10-15 04:49:52 +02:00
|
|
|
MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET))));
|
2005-06-27 01:32:34 +02:00
|
|
|
/* elements --- allocated in groups of up to HASHELEMENT_ALLOC_MAX */
|
2001-10-01 07:36:17 +02:00
|
|
|
elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
|
2005-06-27 01:32:34 +02:00
|
|
|
elementAllocCnt = Min(num_entries, HASHELEMENT_ALLOC_MAX);
|
|
|
|
elementAllocCnt = Max(elementAllocCnt, 1);
|
|
|
|
nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size,
|
|
|
|
mul_size(nElementAllocs,
|
|
|
|
mul_size(elementAllocCnt, elementSize)));
|
1999-02-22 07:16:57 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2000-02-26 06:25:55 +01:00
|
|
|
/*
|
|
|
|
* Select an appropriate directory size for a hashtable with the given
|
|
|
|
* maximum number of entries.
|
|
|
|
* This is only needed for hashtables in shared memory, whose directories
|
|
|
|
* cannot be expanded dynamically.
|
|
|
|
* NB: assumes that all hash structure parameters have default values!
|
|
|
|
*
|
|
|
|
* XXX this had better agree with the behavior of init_htab()...
|
|
|
|
*/
|
|
|
|
long
|
|
|
|
hash_select_dirsize(long num_entries)
|
|
|
|
{
|
|
|
|
long nBuckets,
|
|
|
|
nSegments,
|
|
|
|
nDirEntries;
|
|
|
|
|
|
|
|
/* estimate number of buckets wanted */
|
|
|
|
nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1);
|
|
|
|
/* # of segments needed for nBuckets */
|
|
|
|
nSegments = 1L << my_log2((nBuckets - 1) / DEF_SEGSIZE + 1);
|
|
|
|
/* directory entries */
|
|
|
|
nDirEntries = DEF_DIRSIZE;
|
|
|
|
while (nDirEntries < nSegments)
|
|
|
|
nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
|
|
|
|
|
|
|
|
return nDirEntries;
|
|
|
|
}
|
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/********************** DESTROY ROUTINES ************************/
|
|
|
|
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
hash_destroy(HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (hashp != NULL)
|
|
|
|
{
|
1999-03-06 22:17:56 +01:00
|
|
|
/* allocation method must be one we know how to free, too */
|
2005-05-06 02:19:14 +02:00
|
|
|
Assert(hashp->alloc == DynaHashAlloc);
|
2001-06-22 21:16:24 +02:00
|
|
|
/* so this hashtable must have it's own context */
|
|
|
|
Assert(hashp->hcxt != NULL);
|
1999-03-06 22:17:56 +01:00
|
|
|
|
|
|
|
hash_stats("destroy", hashp);
|
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
/*
|
2005-05-06 02:19:14 +02:00
|
|
|
* Free everything by destroying the hash table's memory context.
|
2001-06-22 21:16:24 +02:00
|
|
|
*/
|
|
|
|
MemoryContextDelete(hashp->hcxt);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-10-05 19:28:13 +02:00
|
|
|
hash_stats(const char *where, HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
#if HASH_STATISTICS
|
|
|
|
fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n",
|
|
|
|
where, hashp->hctl->accesses, hashp->hctl->collisions);
|
|
|
|
|
2002-03-09 18:35:37 +01:00
|
|
|
fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->hctl->nentries, hashp->hctl->keysize,
|
1997-09-07 07:04:48 +02:00
|
|
|
hashp->hctl->max_bucket, hashp->hctl->nsegs);
|
|
|
|
fprintf(stderr, "%s: total accesses %ld total collisions %ld\n",
|
|
|
|
where, hash_accesses, hash_collisions);
|
|
|
|
fprintf(stderr, "hash_stats: total expansions %ld\n",
|
|
|
|
hash_expansions);
|
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************SEARCH ROUTINES *****************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-08-19 03:13:41 +02:00
|
|
|
/* Convert a hash value to a bucket number */
|
|
|
|
static inline uint32
|
|
|
|
calc_bucket(HASHHDR *hctl, uint32 hash_val)
|
|
|
|
{
|
|
|
|
uint32 bucket;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
bucket = hash_val & hctl->high_mask;
|
|
|
|
if (bucket > hctl->max_bucket)
|
|
|
|
bucket = bucket & hctl->low_mask;
|
|
|
|
|
2002-03-09 18:35:37 +01:00
|
|
|
return bucket;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/*----------
|
1996-07-09 08:22:35 +02:00
|
|
|
* hash_search -- look up key in table and perform action
|
|
|
|
*
|
2001-10-05 19:28:13 +02:00
|
|
|
* action is one of:
|
|
|
|
* HASH_FIND: look up key in table
|
|
|
|
* HASH_ENTER: look up key in table, creating entry if not present
|
2005-05-29 06:23:07 +02:00
|
|
|
* HASH_ENTER_NULL: same, but return NULL if out of memory
|
2001-10-05 19:28:13 +02:00
|
|
|
* HASH_REMOVE: look up key in table, remove entry if present
|
|
|
|
*
|
|
|
|
* Return value is a pointer to the element found/entered/removed if any,
|
2005-05-29 06:23:07 +02:00
|
|
|
* or NULL if no match was found. (NB: in the case of the REMOVE action,
|
2001-10-05 19:28:13 +02:00
|
|
|
* the result is a dangling pointer that shouldn't be dereferenced!)
|
2005-05-29 06:23:07 +02:00
|
|
|
*
|
|
|
|
* HASH_ENTER will normally ereport a generic "out of memory" error if
|
2005-10-15 04:49:52 +02:00
|
|
|
* it is unable to create a new entry. The HASH_ENTER_NULL operation is
|
2005-05-29 06:23:07 +02:00
|
|
|
* the same except it will return NULL if out of memory. Note that
|
|
|
|
* HASH_ENTER_NULL cannot be used with the default palloc-based allocator,
|
|
|
|
* since palloc internally ereports on out-of-memory.
|
2001-10-05 19:28:13 +02:00
|
|
|
*
|
|
|
|
* If foundPtr isn't NULL, then *foundPtr is set TRUE if we found an
|
|
|
|
* existing entry in the table, FALSE otherwise. This is needed in the
|
|
|
|
* HASH_ENTER case, but is redundant with the return value otherwise.
|
|
|
|
*----------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
void *
|
1997-09-08 23:56:23 +02:00
|
|
|
hash_search(HTAB *hashp,
|
2003-08-19 03:13:41 +02:00
|
|
|
const void *keyPtr,
|
2001-10-05 19:28:13 +02:00
|
|
|
HASHACTION action,
|
1997-09-08 23:56:23 +02:00
|
|
|
bool *foundPtr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
2005-05-29 06:23:07 +02:00
|
|
|
Size keysize = hctl->keysize;
|
|
|
|
uint32 hashvalue;
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 bucket;
|
|
|
|
long segment_num;
|
|
|
|
long segment_ndx;
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHSEGMENT segp;
|
|
|
|
HASHBUCKET currBucket;
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHBUCKET *prevBucketPtr;
|
2005-05-29 06:23:07 +02:00
|
|
|
HashCompareFunc match;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
#if HASH_STATISTICS
|
|
|
|
hash_accesses++;
|
2001-10-05 19:28:13 +02:00
|
|
|
hctl->accesses++;
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
2001-10-01 07:36:17 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/*
|
2005-05-29 06:23:07 +02:00
|
|
|
* Do the initial lookup
|
2001-10-05 19:28:13 +02:00
|
|
|
*/
|
2005-05-29 06:23:07 +02:00
|
|
|
hashvalue = hashp->hash(keyPtr, keysize);
|
|
|
|
bucket = calc_bucket(hctl, hashvalue);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
segment_num = bucket >> hctl->sshift;
|
|
|
|
segment_ndx = MOD(bucket, hctl->ssize);
|
2003-08-19 03:13:41 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
segp = hashp->dir[segment_num];
|
2003-08-19 03:13:41 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
if (segp == NULL)
|
|
|
|
hash_corrupted(hashp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
prevBucketPtr = &segp[segment_ndx];
|
|
|
|
currBucket = *prevBucketPtr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
/*
|
|
|
|
* Follow collision chain looking for matching key
|
|
|
|
*/
|
|
|
|
match = hashp->match; /* save one fetch in inner loop */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
while (currBucket != NULL)
|
|
|
|
{
|
|
|
|
if (currBucket->hashvalue == hashvalue &&
|
|
|
|
match(ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
|
|
|
|
break;
|
|
|
|
prevBucketPtr = &(currBucket->link);
|
2001-10-01 07:36:17 +02:00
|
|
|
currBucket = *prevBucketPtr;
|
1997-09-07 07:04:48 +02:00
|
|
|
#if HASH_STATISTICS
|
2005-05-29 06:23:07 +02:00
|
|
|
hash_collisions++;
|
|
|
|
hctl->collisions++;
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
if (foundPtr)
|
|
|
|
*foundPtr = (bool) (currBucket != NULL);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2001-10-05 19:28:13 +02:00
|
|
|
* OK, now what?
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
switch (action)
|
|
|
|
{
|
2001-10-05 19:28:13 +02:00
|
|
|
case HASH_FIND:
|
2001-10-01 07:36:17 +02:00
|
|
|
if (currBucket != NULL)
|
|
|
|
return (void *) ELEMENTKEY(currBucket);
|
2001-10-05 19:28:13 +02:00
|
|
|
return NULL;
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case HASH_REMOVE:
|
2001-10-01 07:36:17 +02:00
|
|
|
if (currBucket != NULL)
|
1997-09-08 04:41:22 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
Assert(hctl->nentries > 0);
|
|
|
|
hctl->nentries--;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
1999-05-31 19:01:52 +02:00
|
|
|
/* remove record from hash bucket's chain. */
|
2001-10-01 07:36:17 +02:00
|
|
|
*prevBucketPtr = currBucket->link;
|
1999-05-31 19:01:52 +02:00
|
|
|
|
|
|
|
/* add the record to the freelist for this table. */
|
2001-10-01 07:36:17 +02:00
|
|
|
currBucket->link = hctl->freeList;
|
|
|
|
hctl->freeList = currBucket;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* better hope the caller is synchronizing access to this
|
2005-10-15 04:49:52 +02:00
|
|
|
* element, because someone else is going to reuse it the next
|
|
|
|
* time something is added to the table
|
1997-09-08 04:41:22 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
return (void *) ELEMENTKEY(currBucket);
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
2001-10-05 19:28:13 +02:00
|
|
|
return NULL;
|
2001-10-01 07:36:17 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
case HASH_ENTER_NULL:
|
|
|
|
/* ENTER_NULL does not work with palloc-based allocator */
|
|
|
|
Assert(hashp->alloc != DynaHashAlloc);
|
|
|
|
/* FALL THRU */
|
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
case HASH_ENTER:
|
|
|
|
/* Return existing element if found, else create one */
|
2001-10-01 07:36:17 +02:00
|
|
|
if (currBucket != NULL)
|
|
|
|
return (void *) ELEMENTKEY(currBucket);
|
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* get the next free element */
|
|
|
|
currBucket = hctl->freeList;
|
|
|
|
if (currBucket == NULL)
|
1997-09-08 04:41:22 +02:00
|
|
|
{
|
2001-10-05 19:28:13 +02:00
|
|
|
/* no free elements. allocate another chunk of buckets */
|
2005-06-27 01:32:34 +02:00
|
|
|
if (!element_alloc(hashp, hctl->nelem_alloc))
|
2005-05-29 06:23:07 +02:00
|
|
|
{
|
|
|
|
/* out of memory */
|
|
|
|
if (action == HASH_ENTER_NULL)
|
|
|
|
return NULL;
|
|
|
|
/* report a generic message */
|
|
|
|
if (hashp->isshared)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of shared memory")));
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
|
|
|
}
|
2001-10-05 19:28:13 +02:00
|
|
|
currBucket = hctl->freeList;
|
|
|
|
Assert(currBucket != NULL);
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
hctl->freeList = currBucket->link;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* link into hashbucket chain */
|
|
|
|
*prevBucketPtr = currBucket;
|
|
|
|
currBucket->link = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* copy key into record */
|
2003-08-19 03:13:41 +02:00
|
|
|
currBucket->hashvalue = hashvalue;
|
2005-06-18 22:51:30 +02:00
|
|
|
hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* caller is expected to fill the data field on return */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* Check if it is time to split the segment */
|
2004-11-21 23:57:00 +01:00
|
|
|
if (++hctl->nentries / (long) (hctl->max_bucket + 1) >= hctl->ffactor)
|
2001-10-05 19:28:13 +02:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* NOTE: failure to expand table is not a fatal error, it just
|
|
|
|
* means we have to run at higher fill factor than we wanted.
|
2001-10-05 19:28:13 +02:00
|
|
|
*/
|
|
|
|
expand_table(hashp);
|
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
return (void *) ELEMENTKEY(currBucket);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2001-10-01 07:36:17 +02:00
|
|
|
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(ERROR, "unrecognized hash action code: %d", (int) action);
|
2001-10-05 19:28:13 +02:00
|
|
|
|
|
|
|
return NULL; /* keep compiler quiet */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-01-02 05:33:24 +01:00
|
|
|
* hash_seq_init/_search
|
|
|
|
* Sequentially search through hash table and return
|
2001-10-05 19:28:13 +02:00
|
|
|
* all the elements one by one, return NULL when no more.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-02 05:33:24 +01:00
|
|
|
* NOTE: caller may delete the returned element before continuing the scan.
|
|
|
|
* However, deleting any other element while the scan is in progress is
|
|
|
|
* UNDEFINED (it might be the one that curIndex is pointing at!). Also,
|
|
|
|
* if elements are added to the table while the scan is in progress, it is
|
|
|
|
* unspecified whether they will be visited by the scan or not.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-01-02 05:33:24 +01:00
|
|
|
void
|
|
|
|
hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
|
|
|
|
{
|
|
|
|
status->hashp = hashp;
|
|
|
|
status->curBucket = 0;
|
2001-10-01 07:36:17 +02:00
|
|
|
status->curEntry = NULL;
|
2001-01-02 05:33:24 +01:00
|
|
|
}
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
void *
|
2001-01-02 05:33:24 +01:00
|
|
|
hash_seq_search(HASH_SEQ_STATUS *status)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-05-06 02:19:14 +02:00
|
|
|
HTAB *hashp;
|
|
|
|
HASHHDR *hctl;
|
|
|
|
uint32 max_bucket;
|
|
|
|
long ssize;
|
|
|
|
long segment_num;
|
|
|
|
long segment_ndx;
|
|
|
|
HASHSEGMENT segp;
|
|
|
|
uint32 curBucket;
|
|
|
|
HASHELEMENT *curElem;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
if ((curElem = status->curEntry) != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-05-06 02:19:14 +02:00
|
|
|
/* Continuing scan of curBucket... */
|
|
|
|
status->curEntry = curElem->link;
|
2005-10-15 04:49:52 +02:00
|
|
|
if (status->curEntry == NULL) /* end of this bucket */
|
2005-05-06 02:19:14 +02:00
|
|
|
++status->curBucket;
|
|
|
|
return (void *) ELEMENTKEY(curElem);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/*
|
|
|
|
* Search for next nonempty bucket starting at curBucket.
|
|
|
|
*/
|
|
|
|
curBucket = status->curBucket;
|
|
|
|
hashp = status->hashp;
|
|
|
|
hctl = hashp->hctl;
|
|
|
|
ssize = hctl->ssize;
|
|
|
|
max_bucket = hctl->max_bucket;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
if (curBucket > max_bucket)
|
2005-10-15 04:49:52 +02:00
|
|
|
return NULL; /* search is done */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/*
|
|
|
|
* first find the right segment in the table directory.
|
|
|
|
*/
|
|
|
|
segment_num = curBucket >> hctl->sshift;
|
|
|
|
segment_ndx = MOD(curBucket, ssize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
segp = hashp->dir[segment_num];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/*
|
2005-05-16 02:19:04 +02:00
|
|
|
* Pick up the first item in this bucket's chain. If chain is not empty
|
|
|
|
* we can begin searching it. Otherwise we have to advance to find the
|
|
|
|
* next nonempty bucket. We try to optimize that case since searching a
|
|
|
|
* near-empty hashtable has to iterate this loop a lot.
|
2005-05-06 02:19:14 +02:00
|
|
|
*/
|
|
|
|
while ((curElem = segp[segment_ndx]) == NULL)
|
|
|
|
{
|
|
|
|
/* empty bucket, advance to next */
|
|
|
|
if (++curBucket > max_bucket)
|
|
|
|
{
|
|
|
|
status->curBucket = curBucket;
|
2005-10-15 04:49:52 +02:00
|
|
|
return NULL; /* search is done */
|
2005-05-06 02:19:14 +02:00
|
|
|
}
|
|
|
|
if (++segment_ndx >= ssize)
|
|
|
|
{
|
|
|
|
segment_num++;
|
|
|
|
segment_ndx = 0;
|
|
|
|
segp = hashp->dir[segment_num];
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-06 02:19:14 +02:00
|
|
|
/* Begin scan of curBucket... */
|
|
|
|
status->curEntry = curElem->link;
|
|
|
|
if (status->curEntry == NULL) /* end of this bucket */
|
|
|
|
++curBucket;
|
|
|
|
status->curBucket = curBucket;
|
|
|
|
return (void *) ELEMENTKEY(curElem);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************* UTILITIES ************************/
|
1999-05-31 19:01:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand the table by adding one more hash bucket.
|
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
static bool
|
1997-09-08 23:56:23 +02:00
|
|
|
expand_table(HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
|
|
|
HASHSEGMENT old_seg,
|
1997-09-08 04:41:22 +02:00
|
|
|
new_seg;
|
|
|
|
long old_bucket,
|
|
|
|
new_bucket;
|
|
|
|
long new_segnum,
|
|
|
|
new_segndx;
|
|
|
|
long old_segnum,
|
|
|
|
old_segndx;
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHBUCKET *oldlink,
|
|
|
|
*newlink;
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHBUCKET currElement,
|
2001-10-01 07:36:17 +02:00
|
|
|
nextElement;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef HASH_STATISTICS
|
1997-09-07 07:04:48 +02:00
|
|
|
hash_expansions++;
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
new_bucket = hctl->max_bucket + 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
new_segnum = new_bucket >> hctl->sshift;
|
|
|
|
new_segndx = MOD(new_bucket, hctl->ssize);
|
|
|
|
|
|
|
|
if (new_segnum >= hctl->nsegs)
|
|
|
|
{
|
1999-02-22 07:16:57 +01:00
|
|
|
/* Allocate new segment if necessary -- could fail if dir full */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new_segnum >= hctl->dsize)
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!dir_realloc(hashp))
|
2001-10-01 07:36:17 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!(hashp->dir[new_segnum] = seg_alloc(hashp)))
|
2001-10-01 07:36:17 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
hctl->nsegs++;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-31 19:01:52 +02:00
|
|
|
/* OK, we created a new bucket */
|
1999-02-22 07:16:57 +01:00
|
|
|
hctl->max_bucket++;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-31 19:01:52 +02:00
|
|
|
/*
|
|
|
|
* *Before* changing masks, find old bucket corresponding to same hash
|
2005-10-15 04:49:52 +02:00
|
|
|
* values; values in that bucket may need to be relocated to new bucket.
|
|
|
|
* Note that new_bucket is certainly larger than low_mask at this point,
|
|
|
|
* so we can skip the first step of the regular hash mask calc.
|
1999-05-31 19:01:52 +02:00
|
|
|
*/
|
|
|
|
old_bucket = (new_bucket & hctl->low_mask);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we crossed a power of 2, readjust masks.
|
|
|
|
*/
|
2002-03-09 18:35:37 +01:00
|
|
|
if ((uint32) new_bucket > hctl->high_mask)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
hctl->low_mask = hctl->high_mask;
|
2002-03-09 18:35:37 +01:00
|
|
|
hctl->high_mask = (uint32) new_bucket | hctl->low_mask;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Relocate records to the new bucket. NOTE: because of the way the hash
|
|
|
|
* masking is done in calc_bucket, only one old bucket can need to be
|
|
|
|
* split at this point. With a different way of reducing the hash value,
|
|
|
|
* that might not be true!
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
old_segnum = old_bucket >> hctl->sshift;
|
|
|
|
old_segndx = MOD(old_bucket, hctl->ssize);
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
old_seg = hashp->dir[old_segnum];
|
|
|
|
new_seg = hashp->dir[new_segnum];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
oldlink = &old_seg[old_segndx];
|
|
|
|
newlink = &new_seg[new_segndx];
|
|
|
|
|
|
|
|
for (currElement = *oldlink;
|
|
|
|
currElement != NULL;
|
|
|
|
currElement = nextElement)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
nextElement = currElement->link;
|
2003-08-19 03:13:41 +02:00
|
|
|
if ((long) calc_bucket(hctl, currElement->hashvalue) == old_bucket)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
*oldlink = currElement;
|
|
|
|
oldlink = &currElement->link;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
*newlink = currElement;
|
|
|
|
newlink = &currElement->link;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-05-31 19:01:52 +02:00
|
|
|
/* don't forget to terminate the rebuilt hash chains... */
|
2001-10-01 07:36:17 +02:00
|
|
|
*oldlink = NULL;
|
|
|
|
*newlink = NULL;
|
|
|
|
|
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
static bool
|
1997-09-08 23:56:23 +02:00
|
|
|
dir_realloc(HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHSEGMENT *p;
|
|
|
|
HASHSEGMENT *old_p;
|
1999-02-22 07:16:57 +01:00
|
|
|
long new_dsize;
|
1997-09-08 04:41:22 +02:00
|
|
|
long old_dirsize;
|
|
|
|
long new_dirsize;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (hashp->hctl->max_dsize != NO_MAX_DSIZE)
|
2001-10-01 07:36:17 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* Reallocate directory */
|
1999-02-22 07:16:57 +01:00
|
|
|
new_dsize = hashp->hctl->dsize << 1;
|
2001-10-01 07:36:17 +02:00
|
|
|
old_dirsize = hashp->hctl->dsize * sizeof(HASHSEGMENT);
|
|
|
|
new_dirsize = new_dsize * sizeof(HASHSEGMENT);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
old_p = hashp->dir;
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = hashp->hcxt;
|
2001-10-01 07:36:17 +02:00
|
|
|
p = (HASHSEGMENT *) hashp->alloc((Size) new_dirsize);
|
1999-02-22 07:16:57 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (p != NULL)
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
memcpy(p, old_p, old_dirsize);
|
|
|
|
MemSet(((char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
|
|
|
|
hashp->dir = p;
|
1999-02-22 07:16:57 +01:00
|
|
|
hashp->hctl->dsize = new_dsize;
|
2005-05-06 02:19:14 +02:00
|
|
|
|
|
|
|
/* XXX assume the allocator is palloc, so we know how to free */
|
|
|
|
Assert(hashp->alloc == DynaHashAlloc);
|
|
|
|
pfree(old_p);
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
return true;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2001-10-01 07:36:17 +02:00
|
|
|
|
|
|
|
return false;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
static HASHSEGMENT
|
1997-09-08 23:56:23 +02:00
|
|
|
seg_alloc(HTAB *hashp)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHSEGMENT segp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = hashp->hcxt;
|
2001-10-01 07:36:17 +02:00
|
|
|
segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * hashp->hctl->ssize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!segp)
|
2001-10-01 07:36:17 +02:00
|
|
|
return NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
MemSet(segp, 0, sizeof(HASHBUCKET) * hashp->hctl->ssize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
return segp;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-10-01 07:36:17 +02:00
|
|
|
* allocate some new elements and link them into the free list
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
static bool
|
2004-09-28 22:46:37 +02:00
|
|
|
element_alloc(HTAB *hashp, int nelem)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
2001-10-01 07:36:17 +02:00
|
|
|
Size elementSize;
|
|
|
|
HASHELEMENT *tmpElement;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* Each element has a HASHELEMENT header plus user data. */
|
|
|
|
elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = hashp->hcxt;
|
2001-10-01 07:36:17 +02:00
|
|
|
tmpElement = (HASHELEMENT *)
|
2004-09-28 22:46:37 +02:00
|
|
|
hashp->alloc(nelem * elementSize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
if (!tmpElement)
|
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* link all the new entries into the freelist */
|
2004-09-28 22:46:37 +02:00
|
|
|
for (i = 0; i < nelem; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
tmpElement->link = hctl->freeList;
|
|
|
|
hctl->freeList = tmpElement;
|
|
|
|
tmpElement = (HASHELEMENT *) (((char *) tmpElement) + elementSize);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
/* complain when we have detected a corrupted hashtable */
|
|
|
|
static void
|
|
|
|
hash_corrupted(HTAB *hashp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the corruption is in a shared hashtable, we'd better force a
|
2001-10-25 07:50:21 +02:00
|
|
|
* systemwide restart. Otherwise, just shut down this one backend.
|
2001-10-05 19:28:13 +02:00
|
|
|
*/
|
|
|
|
if (hashp->isshared)
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(PANIC, "hash table \"%s\" corrupted", hashp->tabname);
|
2001-10-05 19:28:13 +02:00
|
|
|
else
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(FATAL, "hash table \"%s\" corrupted", hashp->tabname);
|
2001-10-05 19:28:13 +02:00
|
|
|
}
|
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
/* calculate ceil(log base 2) of num */
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
|
|
|
my_log2(long num)
|
|
|
|
{
|
1999-02-22 07:16:57 +01:00
|
|
|
int i;
|
|
|
|
long limit;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
for (i = 0, limit = 1; limit < num; i++, limit <<= 1)
|
|
|
|
;
|
1998-09-01 05:29:17 +02:00
|
|
|
return i;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|