aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-03-09 17:35:37 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-03-09 17:35:37 +0000
commitc422b5ca6b0dd9b8a2d1d7b8b437e14f3ca79052 (patch)
treefc242c201ff8748e98bfbb610b312205c44b769b /src/backend/utils
parent1eb31d197d8eadc5340f0dfe7e2c7169e1005275 (diff)
downloadpostgresql-c422b5ca6b0dd9b8a2d1d7b8b437e14f3ca79052.tar.gz
postgresql-c422b5ca6b0dd9b8a2d1d7b8b437e14f3ca79052.zip
Code review for improved-hashing patch. Fix some portability issues
(char != unsigned char, Datum != uint32); make use of new hash code in dynahash hash tables and hash joins.
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/date.c4
-rw-r--r--src/backend/utils/adt/mac.c4
-rw-r--r--src/backend/utils/adt/timestamp.c4
-rw-r--r--src/backend/utils/adt/varchar.c4
-rw-r--r--src/backend/utils/hash/dynahash.c17
-rw-r--r--src/backend/utils/hash/hashfn.c86
6 files changed, 23 insertions, 96 deletions
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index 044c310fbe1..2c261198b45 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.64 2001/11/21 05:57:33 thomas Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.65 2002/03/09 17:35:35 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1116,7 +1116,7 @@ timetz_hash(PG_FUNCTION_ARGS)
* sizeof(TimeTzADT), so that any garbage pad bytes in the structure
* won't be included in the hash!
*/
- return hash_any((char *) key, sizeof(double) + sizeof(int4));
+ return hash_any((unsigned char *) key, sizeof(double) + sizeof(int4));
}
Datum
diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c
index d9807e781fd..0abd3a71e89 100644
--- a/src/backend/utils/adt/mac.c
+++ b/src/backend/utils/adt/mac.c
@@ -1,7 +1,7 @@
/*
* PostgreSQL type definitions for MAC addresses.
*
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.21 2001/08/21 21:23:21 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.22 2002/03/09 17:35:35 tgl Exp $
*/
#include "postgres.h"
@@ -230,7 +230,7 @@ hashmacaddr(PG_FUNCTION_ARGS)
{
macaddr *key = PG_GETARG_MACADDR_P(0);
- return hash_any((char *) key, sizeof(macaddr));
+ return hash_any((unsigned char *) key, sizeof(macaddr));
}
/*
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index ad36d5610d3..5fbdc5b8d89 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.64 2002/03/06 06:10:18 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.65 2002/03/09 17:35:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1017,7 +1017,7 @@ interval_hash(PG_FUNCTION_ARGS)
* sizeof(Interval), so that any garbage pad bytes in the structure
* won't be included in the hash!
*/
- return hash_any((char *) key, sizeof(double) + sizeof(int4));
+ return hash_any((unsigned char *) key, sizeof(double) + sizeof(int4));
}
/* overlaps_timestamp() --- implements the SQL92 OVERLAPS operator.
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index f25a06e1449..e384b3f6220 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.87 2001/11/18 12:07:07 ishii Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.88 2002/03/09 17:35:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -791,7 +791,7 @@ hashbpchar(PG_FUNCTION_ARGS)
keydata = VARDATA(key);
keylen = bcTruelen(key);
- result = hash_any(keydata, keylen);
+ result = hash_any((unsigned char *) keydata, keylen);
/* Avoid leaking memory for toasted inputs */
PG_FREE_IF_COPY(key, 0);
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 7fb811e6794..4a9c9d86328 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/hash/dynahash.c,v 1.41 2002/03/02 21:39:33 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/hash/dynahash.c,v 1.42 2002/03/09 17:35:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -329,8 +329,7 @@ init_htab(HTAB *hashp, long nelem)
}
#if HASH_DEBUG
- 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",
- "init_htab:",
+ 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",
"TABLE POINTER ", hashp,
"DIRECTORY SIZE ", hctl->dsize,
"SEGMENT SIZE ", hctl->ssize,
@@ -453,7 +452,7 @@ hash_stats(const char *where, HTAB *hashp)
fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n",
where, hashp->hctl->accesses, hashp->hctl->collisions);
- fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %d segmentcount %d\n",
+ fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
hashp->hctl->nentries, hashp->hctl->keysize,
hashp->hctl->max_bucket, hashp->hctl->nsegs);
fprintf(stderr, "%s: total accesses %ld total collisions %ld\n",
@@ -470,7 +469,7 @@ static uint32
call_hash(HTAB *hashp, void *k)
{
HASHHDR *hctl = hashp->hctl;
- long hash_val,
+ uint32 hash_val,
bucket;
hash_val = hashp->hash(k, (int) hctl->keysize);
@@ -479,7 +478,7 @@ call_hash(HTAB *hashp, void *k)
if (bucket > hctl->max_bucket)
bucket = bucket & hctl->low_mask;
- return (uint32) bucket;
+ return bucket;
}
/*----------
@@ -647,7 +646,7 @@ hash_search(HTAB *hashp,
/* caller is expected to fill the data field on return */
/* Check if it is time to split the segment */
- if (++hctl->nentries / (hctl->max_bucket + 1) > hctl->ffactor)
+ if (++hctl->nentries / (long) (hctl->max_bucket + 1) > hctl->ffactor)
{
/*
* NOTE: failure to expand table is not a fatal error, it
@@ -795,10 +794,10 @@ expand_table(HTAB *hashp)
/*
* If we crossed a power of 2, readjust masks.
*/
- if (new_bucket > hctl->high_mask)
+ if ((uint32) new_bucket > hctl->high_mask)
{
hctl->low_mask = hctl->high_mask;
- hctl->high_mask = new_bucket | hctl->low_mask;
+ hctl->high_mask = (uint32) new_bucket | hctl->low_mask;
}
/*
diff --git a/src/backend/utils/hash/hashfn.c b/src/backend/utils/hash/hashfn.c
index 4c7dd4cbb44..f0b8608beb3 100644
--- a/src/backend/utils/hash/hashfn.c
+++ b/src/backend/utils/hash/hashfn.c
@@ -9,14 +9,16 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/hash/hashfn.c,v 1.15 2001/10/25 05:49:51 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/hash/hashfn.c,v 1.16 2002/03/09 17:35:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
+#include "access/hash.h"
#include "utils/hsearch.h"
+
/*
* string_hash: hash function for keys that are null-terminated strings.
*
@@ -27,91 +29,17 @@
*
* NOTE: this is the default hash function if none is specified.
*/
-long
+uint32
string_hash(void *key, int keysize)
{
- unsigned char *k = (unsigned char *) key;
- long h = 0;
-
- while (*k)
- h = (h * PRIME1) ^ (*k++);
-
- h %= PRIME2;
-
- return h;
+ return DatumGetUInt32(hash_any((unsigned char *) key, strlen((char *) key)));
}
/*
* tag_hash: hash function for fixed-size tag values
- *
- * NB: we assume that the supplied key is aligned at least on an 'int'
- * boundary, if its size is >= sizeof(int).
*/
-long
+uint32
tag_hash(void *key, int keysize)
{
- int *k = (int *) key;
- long h = 0;
-
- /*
- * Use four byte chunks in a "jump table" to go a little faster.
- *
- * Currently the maximum keysize is 16 (mar 17 1992). I have put in
- * cases for up to 32. Bigger than this will resort to a for loop
- * (see the default case).
- */
- switch (keysize)
- {
- case 8 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 7 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 6 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 5 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 4 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 3 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case 2 * sizeof(int):
- h = (h * PRIME1) ^(*k++);
- /* fall through */
-
- case sizeof(int):
- h = (h * PRIME1) ^(*k++);
- break;
-
- default:
- /* Do an int at a time */
- for (; keysize >= (int) sizeof(int); keysize -= sizeof(int))
- h = (h * PRIME1) ^ (*k++);
-
- /* Cope with any partial-int leftover bytes */
- if (keysize > 0)
- {
- unsigned char *keybyte = (unsigned char *) k;
-
- do
- h = (h * PRIME1) ^ (*keybyte++);
- while (--keysize > 0);
- }
- break;
- }
-
- h %= PRIME2;
-
- return h;
+ return DatumGetUInt32(hash_any((unsigned char *) key, keysize));
}