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
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/hash/dynahash.c,v 1.41 2002/03/02 21:39:33 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
|
|
|
|
|
|
|
#include <sys/types.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 uint32 call_hash(HTAB *hashp, void *k);
|
|
|
|
static HASHSEGMENT seg_alloc(HTAB *hashp);
|
2001-10-25 07:50:21 +02:00
|
|
|
static bool element_alloc(HTAB *hashp);
|
|
|
|
static bool dir_realloc(HTAB *hashp);
|
|
|
|
static bool expand_table(HTAB *hashp);
|
|
|
|
static bool hdefault(HTAB *hashp);
|
|
|
|
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
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* memory allocation routines
|
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
static MemoryContext DynaHashCxt = NULL;
|
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
|
|
|
#define MEM_ALLOC DynaHashAlloc
|
|
|
|
#define MEM_FREE pfree
|
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 **********************/
|
|
|
|
|
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
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* First time through, create a memory context for hash tables */
|
2001-06-22 21:16:24 +02:00
|
|
|
if (!DynaHashCxt)
|
|
|
|
DynaHashCxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"DynaHash",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* Select allocation context for this hash table */
|
2001-06-22 21:16:24 +02:00
|
|
|
if (flags & HASH_CONTEXT)
|
|
|
|
CurrentDynaHashCxt = info->hcxt;
|
|
|
|
else
|
|
|
|
CurrentDynaHashCxt = DynaHashCxt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/* Initialize the hash header */
|
2000-06-28 05:33:33 +02:00
|
|
|
hashp = (HTAB *) MEM_ALLOC(sizeof(HTAB));
|
2001-10-01 07:36:17 +02:00
|
|
|
if (!hashp)
|
|
|
|
return NULL;
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(hashp, 0, sizeof(HTAB));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
hashp->tabname = (char *) MEM_ALLOC(strlen(tabname) + 1);
|
|
|
|
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
|
|
|
|
|
|
|
if (flags & HASH_SHARED_MEM)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* ctl structure is preallocated for shared memory tables. Note
|
|
|
|
* that HASH_DIRSIZE 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;
|
1997-09-07 07:04:48 +02:00
|
|
|
hashp->alloc = info->alloc;
|
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;
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->alloc = MEM_ALLOC;
|
2001-06-22 21:16:24 +02:00
|
|
|
hashp->hcxt = DynaHashCxt;
|
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)
|
2001-10-01 07:36:17 +02:00
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!hdefault(hashp))
|
2001-10-01 07:36:17 +02:00
|
|
|
return NULL;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hash table now allocates space for key and data but you have to say
|
|
|
|
* how much space to allocate
|
|
|
|
*/
|
|
|
|
if (flags & HASH_ELEM)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (flags & HASH_ALLOC)
|
|
|
|
hashp->alloc = info->alloc;
|
2001-06-22 21:16:24 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (flags & HASH_CONTEXT)
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
/* hash table structures live in child of given context */
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = AllocSetContextCreate(info->hcxt,
|
2001-10-25 07:50:21 +02:00
|
|
|
"DynaHashTable",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
2001-06-22 21:16:24 +02:00
|
|
|
hashp->hcxt = CurrentDynaHashCxt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
/* hash table structures live in child of DynaHashCxt */
|
2001-06-22 21:16:24 +02:00
|
|
|
CurrentDynaHashCxt = AllocSetContextCreate(DynaHashCxt,
|
2001-10-25 07:50:21 +02:00
|
|
|
"DynaHashTable",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
2001-06-22 21:16:24 +02:00
|
|
|
hashp->hcxt = CurrentDynaHashCxt;
|
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
if (!init_htab(hashp, nelem))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
hash_destroy(hashp);
|
2001-10-01 07:36:17 +02:00
|
|
|
return NULL;
|
1997-09-07 07:04:48 +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
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
static bool
|
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;
|
|
|
|
|
|
|
|
/* I added these MS. */
|
|
|
|
|
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;
|
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-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;
|
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
|
|
|
|
* number of buckets. Allocate space for the next greater power of
|
|
|
|
* two number of buckets
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
nelem = (nelem - 1) / hctl->ffactor + 1;
|
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
nbuckets = 1 << my_log2(nelem);
|
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
|
|
|
/*
|
1999-05-25 18:15:34 +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);
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +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
|
|
|
|
|
|
|
#if HASH_DEBUG
|
2001-10-01 07:36:17 +02:00
|
|
|
fprintf(stderr, "%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
"init_htab:",
|
|
|
|
"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!
|
|
|
|
*/
|
|
|
|
long
|
2001-10-01 07:36:17 +02:00
|
|
|
hash_estimate_size(long num_entries, long entrysize)
|
1999-02-22 07:16:57 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
long size = 0;
|
|
|
|
long nBuckets,
|
|
|
|
nSegments,
|
|
|
|
nDirEntries,
|
2001-10-01 07:36:17 +02:00
|
|
|
nElementAllocs,
|
|
|
|
elementSize;
|
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 */
|
2001-10-25 07:50:21 +02:00
|
|
|
size += MAXALIGN(sizeof(HASHHDR)); /* but not HTAB, per above */
|
1999-03-06 22:17:56 +01:00
|
|
|
/* directory */
|
2001-10-01 07:36:17 +02:00
|
|
|
size += MAXALIGN(nDirEntries * sizeof(HASHSEGMENT));
|
1999-02-22 07:16:57 +01:00
|
|
|
/* segments */
|
2001-10-01 07:36:17 +02:00
|
|
|
size += nSegments * MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET));
|
|
|
|
/* elements --- allocated in groups of HASHELEMENT_ALLOC_INCR */
|
|
|
|
elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
|
|
|
|
nElementAllocs = (num_entries - 1) / HASHELEMENT_ALLOC_INCR + 1;
|
|
|
|
size += nElementAllocs * HASHELEMENT_ALLOC_INCR * 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 */
|
2000-06-28 05:33:33 +02:00
|
|
|
Assert(hashp->alloc == MEM_ALLOC);
|
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
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Free buckets, dir etc. by destroying the hash table's memory
|
|
|
|
* context.
|
2001-06-22 21:16:24 +02:00
|
|
|
*/
|
|
|
|
MemoryContextDelete(hashp->hcxt);
|
|
|
|
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Free the HTAB and control structure, which are allocated in the
|
|
|
|
* parent context (DynaHashCxt or the context given by the caller
|
|
|
|
* of hash_create()).
|
2001-06-22 21:16:24 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
MEM_FREE(hashp->hctl);
|
2001-10-05 19:28:13 +02:00
|
|
|
MEM_FREE(hashp->tabname);
|
2001-10-01 07:36:17 +02:00
|
|
|
MEM_FREE(hashp);
|
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);
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %d segmentcount %d\n",
|
|
|
|
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-08 04:41:22 +02:00
|
|
|
static uint32
|
2001-10-01 07:36:17 +02:00
|
|
|
call_hash(HTAB *hashp, void *k)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
1997-09-08 04:41:22 +02:00
|
|
|
long hash_val,
|
|
|
|
bucket;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-31 19:01:52 +02:00
|
|
|
hash_val = hashp->hash(k, (int) hctl->keysize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
bucket = hash_val & hctl->high_mask;
|
|
|
|
if (bucket > hctl->max_bucket)
|
|
|
|
bucket = bucket & hctl->low_mask;
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
return (uint32) 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
|
|
|
|
* HASH_REMOVE: look up key in table, remove entry if present
|
|
|
|
* HASH_FIND_SAVE: look up key in table, also save in static var
|
|
|
|
* HASH_REMOVE_SAVED: remove entry saved by HASH_FIND_SAVE
|
|
|
|
*
|
|
|
|
* Return value is a pointer to the element found/entered/removed if any,
|
|
|
|
* or NULL if no match was found. (NB: in the case of the REMOVE actions,
|
|
|
|
* the result is a dangling pointer that shouldn't be dereferenced!)
|
|
|
|
* A NULL result for HASH_ENTER implies we ran out of memory.
|
|
|
|
*
|
|
|
|
* 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-05 19:28:13 +02:00
|
|
|
* The HASH_FIND_SAVE/HASH_REMOVE_SAVED interface is a hack to save one
|
2001-10-25 07:50:21 +02:00
|
|
|
* table lookup in a find/process/remove scenario. Note that no other
|
2001-10-05 19:28:13 +02:00
|
|
|
* addition or removal in the table can safely happen in between.
|
|
|
|
*----------
|
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,
|
2001-10-01 07:36:17 +02:00
|
|
|
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;
|
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;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static struct State
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHBUCKET currBucket;
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHBUCKET *prevBucketPtr;
|
1997-09-08 04:41:22 +02:00
|
|
|
} saveState;
|
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
|
|
|
/*
|
|
|
|
* Do the initial lookup (or recall result of prior lookup)
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (action == HASH_REMOVE_SAVED)
|
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
currBucket = saveState.currBucket;
|
|
|
|
prevBucketPtr = saveState.prevBucketPtr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to catch subsequent errors
|
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
Assert(currBucket && !(saveState.currBucket = NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
1999-05-31 19:01:52 +02:00
|
|
|
bucket = call_hash(hashp, keyPtr);
|
1997-09-07 07:04:48 +02:00
|
|
|
segment_num = bucket >> hctl->sshift;
|
1999-05-31 19:01:52 +02:00
|
|
|
segment_ndx = MOD(bucket, hctl->ssize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
segp = hashp->dir[segment_num];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
if (segp == NULL)
|
|
|
|
hash_corrupted(hashp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
prevBucketPtr = &segp[segment_ndx];
|
|
|
|
currBucket = *prevBucketPtr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2001-10-01 07:36:17 +02:00
|
|
|
* Follow collision chain looking for matching key
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
while (currBucket != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-01 07:36:17 +02:00
|
|
|
if (memcmp(ELEMENTKEY(currBucket), keyPtr, hctl->keysize) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
2001-10-01 07:36:17 +02:00
|
|
|
prevBucketPtr = &(currBucket->link);
|
|
|
|
currBucket = *prevBucketPtr;
|
1997-09-07 07:04:48 +02:00
|
|
|
#if HASH_STATISTICS
|
|
|
|
hash_collisions++;
|
2001-10-05 19:28:13 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
case HASH_FIND_SAVE:
|
|
|
|
if (currBucket != NULL)
|
|
|
|
{
|
|
|
|
saveState.currBucket = currBucket;
|
|
|
|
saveState.prevBucketPtr = prevBucketPtr;
|
|
|
|
return (void *) ELEMENTKEY(currBucket);
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-10-01 07:36:17 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case HASH_REMOVE:
|
|
|
|
case HASH_REMOVE_SAVED:
|
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
|
|
|
|
* element, because someone else is going to reuse it the
|
|
|
|
* next time something is added to the table
|
|
|
|
*/
|
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
|
|
|
|
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 */
|
|
|
|
if (!element_alloc(hashp))
|
2001-10-28 07:26:15 +01:00
|
|
|
return NULL; /* 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 */
|
|
|
|
memcpy(ELEMENTKEY(currBucket), keyPtr, hctl->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 */
|
|
|
|
if (++hctl->nentries / (hctl->max_bucket + 1) > hctl->ffactor)
|
|
|
|
{
|
|
|
|
/*
|
2001-10-25 07:50:21 +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
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
elog(ERROR, "hash_search: bogus action %d", (int) action);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2001-01-02 05:33:24 +01:00
|
|
|
HTAB *hashp = status->hashp;
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHHDR *hctl = hashp->hctl;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-01-02 05:33:24 +01:00
|
|
|
while (status->curBucket <= hctl->max_bucket)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-01-02 05:33:24 +01:00
|
|
|
long segment_num;
|
|
|
|
long segment_ndx;
|
2001-10-25 07:50:21 +02:00
|
|
|
HASHSEGMENT segp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
if (status->curEntry != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-01-02 05:33:24 +01:00
|
|
|
/* Continuing scan of curBucket... */
|
2001-10-01 07:36:17 +02:00
|
|
|
HASHELEMENT *curElem;
|
2001-01-02 05:33:24 +01:00
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
curElem = status->curEntry;
|
|
|
|
status->curEntry = curElem->link;
|
2001-10-25 07:50:21 +02:00
|
|
|
if (status->curEntry == NULL) /* end of this bucket */
|
2001-01-02 05:33:24 +01:00
|
|
|
++status->curBucket;
|
2001-10-01 07:36:17 +02:00
|
|
|
return (void *) ELEMENTKEY(curElem);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the search within this bucket.
|
|
|
|
*/
|
2001-01-02 05:33:24 +01:00
|
|
|
segment_num = status->curBucket >> hctl->sshift;
|
|
|
|
segment_ndx = MOD(status->curBucket, hctl->ssize);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* first find the right segment in the table directory.
|
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
segp = hashp->dir[segment_num];
|
1997-09-07 07:04:48 +02:00
|
|
|
if (segp == NULL)
|
2001-10-05 19:28:13 +02:00
|
|
|
hash_corrupted(hashp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now find the right index into the segment for the first item in
|
|
|
|
* this bucket's chain. if the bucket is not empty (its entry in
|
|
|
|
* the dir is valid), we know this must correspond to a valid
|
|
|
|
* element and not a freed element because it came out of the
|
|
|
|
* directory of valid stuff. if there are elements in the bucket
|
|
|
|
* chains that point to the freelist we're in big trouble.
|
|
|
|
*/
|
2001-10-01 07:36:17 +02:00
|
|
|
status->curEntry = segp[segment_ndx];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
if (status->curEntry == NULL) /* empty bucket */
|
2001-01-02 05:33:24 +01:00
|
|
|
++status->curBucket;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-05 19:28:13 +02:00
|
|
|
return NULL; /* out of buckets */
|
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
|
2000-04-12 19:17:23 +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.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (new_bucket > hctl->high_mask)
|
|
|
|
{
|
|
|
|
hctl->low_mask = hctl->high_mask;
|
|
|
|
hctl->high_mask = new_bucket | hctl->low_mask;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Relocate records to the new bucket. NOTE: because of the way the
|
1999-05-31 19:01:52 +02:00
|
|
|
* hash masking is done in call_hash, only one old bucket can need to
|
2000-04-12 19:17:23 +02:00
|
|
|
* be split at this point. With a different way of reducing the hash
|
1999-05-31 19:01:52 +02:00
|
|
|
* 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;
|
|
|
|
if ((long) call_hash(hashp, (void *) ELEMENTKEY(currElement))
|
|
|
|
== 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);
|
1999-02-22 07:16:57 +01:00
|
|
|
MEM_FREE((char *) old_p);
|
2001-10-01 07:36:17 +02:00
|
|
|
hashp->dir = p;
|
1999-02-22 07:16:57 +01:00
|
|
|
hashp->hctl->dsize = new_dsize;
|
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
|
|
|
|
element_alloc(HTAB *hashp)
|
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 *)
|
|
|
|
hashp->alloc(HASHELEMENT_ALLOC_INCR * 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 */
|
|
|
|
for (i = 0; i < HASHELEMENT_ALLOC_INCR; 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)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(PANIC, "Hash table '%s' corrupted", hashp->tabname);
|
2001-10-05 19:28:13 +02:00
|
|
|
else
|
|
|
|
elog(FATAL, "Hash table '%s' corrupted", hashp->tabname);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|