diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/access/hash/hashfunc.c | 167 | ||||
-rw-r--r-- | src/backend/access/nbtree/nbtcompare.c | 30 | ||||
-rw-r--r-- | src/backend/utils/adt/date.c | 30 | ||||
-rw-r--r-- | src/backend/utils/adt/timestamp.c | 19 | ||||
-rw-r--r-- | src/backend/utils/adt/varchar.c | 96 | ||||
-rw-r--r-- | src/backend/utils/cache/catcache.c | 4 |
6 files changed, 98 insertions, 248 deletions
diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index 9a6d36f2055..362738e6764 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.26 2000/06/05 07:28:35 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.27 2000/06/19 03:54:17 tgl Exp $ * * NOTES * These functions are stored in pg_amproc. For each operator class @@ -21,10 +21,17 @@ #include "access/hash.h" + +Datum +hashchar(PG_FUNCTION_ARGS) +{ + PG_RETURN_UINT32(~ ((uint32) PG_GETARG_CHAR(0))); +} + Datum hashint2(PG_FUNCTION_ARGS) { - PG_RETURN_UINT32((uint32) ~ PG_GETARG_INT16(0)); + PG_RETURN_UINT32(~ ((uint32) PG_GETARG_INT16(0))); } Datum @@ -37,116 +44,37 @@ Datum hashint8(PG_FUNCTION_ARGS) { /* we just use the low 32 bits... */ - PG_RETURN_UINT32(~((uint32) PG_GETARG_INT64(0))); + PG_RETURN_UINT32(~ ((uint32) PG_GETARG_INT64(0))); +} + +Datum +hashoid(PG_FUNCTION_ARGS) +{ + PG_RETURN_UINT32(~ ((uint32) PG_GETARG_OID(0))); } -/* Hash function from Chris Torek. */ Datum hashfloat4(PG_FUNCTION_ARGS) { float4 key = PG_GETARG_FLOAT4(0); - char *kp = (char *) &key; - int len = sizeof(key); - int loop; - uint32 h; - -#define HASH4a h = (h << 5) - h + *kp++; -#define HASH4b h = (h << 5) + h + *kp++; -#define HASH4 HASH4b - - h = 0; - /* - * This is a tad silly, given that we expect len = 4, but a smart - * compiler should be able to eliminate the redundant code... - */ - loop = (len + 8 - 1) >> 3; - switch (len & (8 - 1)) - { - case 0: - do - { /* All fall throughs */ - HASH4; - case 7: - HASH4; - case 6: - HASH4; - case 5: - HASH4; - case 4: - HASH4; - case 3: - HASH4; - case 2: - HASH4; - case 1: - HASH4; - } while (--loop); - } - PG_RETURN_UINT32(h); + return hash_any((char *) &key, sizeof(key)); } Datum hashfloat8(PG_FUNCTION_ARGS) { float8 key = PG_GETARG_FLOAT8(0); - char *kp = (char *) &key; - int len = sizeof(key); - int loop; - uint32 h; -#define HASH4a h = (h << 5) - h + *kp++; -#define HASH4b h = (h << 5) + h + *kp++; -#define HASH4 HASH4b - - h = 0; - /* - * This is a tad silly, given that we expect len = 8, but a smart - * compiler should be able to eliminate the redundant code... - */ - loop = (len + 8 - 1) >> 3; - - switch (len & (8 - 1)) - { - case 0: - do - { /* All fall throughs */ - HASH4; - case 7: - HASH4; - case 6: - HASH4; - case 5: - HASH4; - case 4: - HASH4; - case 3: - HASH4; - case 2: - HASH4; - case 1: - HASH4; - } while (--loop); - } - PG_RETURN_UINT32(h); -} - -Datum -hashoid(PG_FUNCTION_ARGS) -{ - PG_RETURN_UINT32(~(uint32) PG_GETARG_OID(0)); + return hash_any((char *) &key, sizeof(key)); } Datum hashoidvector(PG_FUNCTION_ARGS) { Oid *key = (Oid *) PG_GETARG_POINTER(0); - int i; - uint32 result = 0; - for (i = INDEX_MAX_KEYS; --i >= 0;) - result = (result << 1) ^ (~(uint32) key[i]); - PG_RETURN_UINT32(result); + return hash_any((char *) key, INDEX_MAX_KEYS * sizeof(Oid)); } /* @@ -158,69 +86,53 @@ Datum hashint2vector(PG_FUNCTION_ARGS) { int16 *key = (int16 *) PG_GETARG_POINTER(0); - int i; - uint32 result = 0; - for (i = INDEX_MAX_KEYS; --i >= 0;) - result = (result << 1) ^ (~(uint32) key[i]); - PG_RETURN_UINT32(result); + return hash_any((char *) key, INDEX_MAX_KEYS * sizeof(int16)); } - -#define PRIME1 37 -#define PRIME2 1048583 - Datum -hashchar(PG_FUNCTION_ARGS) +hashname(PG_FUNCTION_ARGS) { - uint32 h; - - /* Convert char to integer */ - h = (PG_GETARG_CHAR(0) - ' '); - h %= PRIME2; + char *key = NameStr(* PG_GETARG_NAME(0)); - PG_RETURN_UINT32(h); + return hash_any((char *) key, NAMEDATALEN); } +/* + * hashvarlena() can be used for any varlena datatype in which there are + * no non-significant bits, ie, distinct bitpatterns never compare as equal. + */ Datum -hashname(PG_FUNCTION_ARGS) +hashvarlena(PG_FUNCTION_ARGS) { - char *key = NameStr(* PG_GETARG_NAME(0)); - int len = NAMEDATALEN; - uint32 h; + struct varlena *key = PG_GETARG_VARLENA_P(0); - h = 0; - /* Convert string to integer */ - while (len--) - h = h * PRIME1 ^ (*key++ - ' '); - h %= PRIME2; - - PG_RETURN_UINT32(h); + return hash_any(VARDATA(key), VARSIZE(key) - VARHDRSZ); } + /* + * hash_any --- compute a hash function for any specified chunk of memory + * + * This can be used as the underlying hash function for any pass-by-reference + * data type in which there are no non-significant bits. + * * (Comment from the original db3 hashing code: ) * * "This is INCREDIBLY ugly, but fast. We break the string up into 8 byte * units. On the first time through the loop we get the 'leftover bytes' - * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle + * (strlen % 8). On every later iteration, we perform 8 HASHC's so we handle * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If * this routine is heavily used enough, it's worth the ugly coding. * * "OZ's original sdbm hash" */ Datum -hashtext(PG_FUNCTION_ARGS) +hash_any(char *keydata, int keylen) { - text *key = PG_GETARG_TEXT_P(0); - int keylen; - char *keydata; uint32 n; int loop; - keydata = VARDATA(key); - keylen = VARSIZE(key) - VARHDRSZ; - #define HASHC n = *keydata++ + 65599 * n n = 0; @@ -251,5 +163,8 @@ hashtext(PG_FUNCTION_ARGS) } while (--loop); } } + +#undef HASHC + PG_RETURN_UINT32(n); } diff --git a/src/backend/access/nbtree/nbtcompare.c b/src/backend/access/nbtree/nbtcompare.c index 375fc127bb4..73f52cb861c 100644 --- a/src/backend/access/nbtree/nbtcompare.c +++ b/src/backend/access/nbtree/nbtcompare.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtcompare.c,v 1.37 2000/06/15 03:31:54 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtcompare.c,v 1.38 2000/06/19 03:54:22 tgl Exp $ * * NOTES * @@ -82,34 +82,6 @@ btint8cmp(PG_FUNCTION_ARGS) } Datum -btint24cmp(PG_FUNCTION_ARGS) -{ - int16 a = PG_GETARG_INT16(0); - int32 b = PG_GETARG_INT32(1); - - if (a > b) - PG_RETURN_INT32(1); - else if (a == b) - PG_RETURN_INT32(0); - else - PG_RETURN_INT32(-1); -} - -Datum -btint42cmp(PG_FUNCTION_ARGS) -{ - int32 a = PG_GETARG_INT32(0); - int16 b = PG_GETARG_INT16(1); - - if (a > b) - PG_RETURN_INT32(1); - else if (a == b) - PG_RETURN_INT32(0); - else - PG_RETURN_INT32(-1); -} - -Datum btfloat4cmp(PG_FUNCTION_ARGS) { float4 a = PG_GETARG_FLOAT4(0); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 128216f2b84..952a8e6d4d6 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -8,23 +8,27 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.47 2000/06/15 04:10:23 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.48 2000/06/19 03:54:27 tgl Exp $ * *------------------------------------------------------------------------- */ -#include <limits.h> -#include <time.h> #include "postgres.h" + +#include <limits.h> +#include <time.h> #ifdef HAVE_FLOAT_H #include <float.h> #endif + +#include "access/hash.h" #include "miscadmin.h" #include "utils/date.h" #include "utils/nabstime.h" -static int - date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn); + +static int date2tm(DateADT dateVal, int *tzp, struct tm * tm, + double *fsec, char **tzn); /***************************************************************************** @@ -762,6 +766,22 @@ timetz_cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(0); } +/* + * timetz, being an unusual size, needs a specialized hash function. + */ +Datum +timetz_hash(PG_FUNCTION_ARGS) +{ + TimeTzADT *key = PG_GETARG_TIMETZADT_P(0); + + /* + * Specify hash length as sizeof(double) + sizeof(int4), not as + * 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)); +} + Datum timetz_larger(PG_FUNCTION_ARGS) { diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 7715942a28a..71549bb9256 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.29 2000/06/09 01:11:09 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.30 2000/06/19 03:54:27 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -29,6 +29,7 @@ #include <sys/timeb.h> #endif +#include "access/hash.h" #include "access/xact.h" #include "miscadmin.h" #include "utils/builtins.h" @@ -816,6 +817,22 @@ interval_cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0); } +/* + * interval, being an unusual size, needs a specialized hash function. + */ +Datum +interval_hash(PG_FUNCTION_ARGS) +{ + Interval *key = PG_GETARG_INTERVAL_P(0); + + /* + * Specify hash length as sizeof(double) + sizeof(int4), not as + * 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)); +} + /* overlaps_timestamp() * Implements the SQL92 OVERLAPS operator. * Algorithm from Date and Darwen, 1997 diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 00e4c8ef39f..53c6ec41677 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -8,13 +8,14 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.65 2000/06/15 03:32:29 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.66 2000/06/19 03:54:28 tgl Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" +#include "access/hash.h" #include "catalog/pg_type.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -797,94 +798,19 @@ varcharcmp(char *arg1, char *arg2) return (int32) (cmp); } -/***************************************************************************** - * Hash functions (modified from hashtext in access/hash/hashfunc.c) - *****************************************************************************/ - -uint32 -hashbpchar(struct varlena * key) +/* + * bpchar needs a specialized hash function because we want to ignore + * trailing blanks in comparisons. (varchar can use plain hashvarlena.) + */ +Datum +hashbpchar(PG_FUNCTION_ARGS) { - int keylen; + BpChar *key = PG_GETARG_BPCHAR_P(0); char *keydata; - uint32 n; - int loop; - - keydata = VARDATA(key); - keylen = bcTruelen((char *) key); - -#define HASHC n = *keydata++ + 65599 * n - - n = 0; - if (keylen > 0) - { - loop = (keylen + 8 - 1) >> 3; - - switch (keylen & (8 - 1)) - { - case 0: - do - { /* All fall throughs */ - HASHC; - case 7: - HASHC; - case 6: - HASHC; - case 5: - HASHC; - case 4: - HASHC; - case 3: - HASHC; - case 2: - HASHC; - case 1: - HASHC; - } while (--loop); - } - } - return n; -} - -uint32 -hashvarchar(struct varlena * key) -{ int keylen; - char *keydata; - uint32 n; - int loop; keydata = VARDATA(key); - keylen = VARSIZE(key) - VARHDRSZ; - -#define HASHC n = *keydata++ + 65599 * n + keylen = bcTruelen((char *) key); - n = 0; - if (keylen > 0) - { - loop = (keylen + 8 - 1) >> 3; - - switch (keylen & (8 - 1)) - { - case 0: - do - { /* All fall throughs */ - HASHC; - case 7: - HASHC; - case 6: - HASHC; - case 5: - HASHC; - case 4: - HASHC; - case 3: - HASHC; - case 2: - HASHC; - case 1: - HASHC; - } while (--loop); - } - } - return n; + return hash_any(keydata, keylen); } diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c index 52a16e5292f..a1c2d5b7bbb 100644 --- a/src/backend/utils/cache/catcache.c +++ b/src/backend/utils/cache/catcache.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.66 2000/06/17 04:56:32 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.67 2000/06/19 03:54:31 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -104,7 +104,7 @@ GetCCHashFunc(Oid keytype) case INT4OID: return hashint4; case TEXTOID: - return hashtext; + return hashvarlena; case REGPROCOID: case OIDOID: return hashoid; |