1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* hashfunc.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Comparison functions for hash access method.
|
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
|
2002-02-25 05:06:52 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.31 2002/02/25 04:06:47 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* These functions are stored in pg_amproc. For each operator class
|
|
|
|
* defined on hash tables, they compute the hash value of the argument.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-10-20 08:34:30 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-11-10 04:06:38 +01:00
|
|
|
#include "access/hash.h"
|
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
Datum
|
|
|
|
hashchar(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_UINT32(~((uint32) PG_GETARG_CHAR(0)));
|
2000-06-19 05:55:01 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint2(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_UINT32(~((uint32) PG_GETARG_INT16(0)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint4(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_UINT32(~PG_GETARG_UINT32(0));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint8(PG_FUNCTION_ARGS)
|
1999-03-14 06:09:05 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
/* we just use the low 32 bits... */
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_UINT32(~((uint32) PG_GETARG_INT64(0)));
|
2000-06-19 05:55:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
hashoid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_UINT32(~((uint32) PG_GETARG_OID(0)));
|
1999-03-14 06:09:05 +01:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashfloat4(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
float4 key = PG_GETARG_FLOAT4(0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
return hash_any((char *) &key, sizeof(key));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashfloat8(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
float8 key = PG_GETARG_FLOAT8(0);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
return hash_any((char *) &key, sizeof(key));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashoidvector(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid *key = (Oid *) PG_GETARG_POINTER(0);
|
1998-08-19 04:04:17 +02:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
return hash_any((char *) key, INDEX_MAX_KEYS * sizeof(Oid));
|
2000-02-21 04:36:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: hashint2vector currently can't be used as a user hash table
|
2000-04-12 19:17:23 +02:00
|
|
|
* hash function, because it has no pg_proc entry. We only need it
|
2000-02-21 04:36:59 +01:00
|
|
|
* for catcache indexing.
|
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint2vector(PG_FUNCTION_ARGS)
|
2000-02-21 04:36:59 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
int16 *key = (int16 *) PG_GETARG_POINTER(0);
|
2000-02-21 04:36:59 +01:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
return hash_any((char *) key, INDEX_MAX_KEYS * sizeof(int16));
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
2000-06-19 05:55:01 +02:00
|
|
|
hashname(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
char *key = NameStr(*PG_GETARG_NAME(0));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-02-25 05:06:52 +01:00
|
|
|
return hash_any(key, strlen(key));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
/*
|
|
|
|
* hashvarlena() can be used for any varlena datatype in which there are
|
|
|
|
* no non-significant bits, ie, distinct bitpatterns never compare as equal.
|
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
2000-06-19 05:55:01 +02:00
|
|
|
hashvarlena(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-19 05:55:01 +02:00
|
|
|
struct varlena *key = PG_GETARG_VARLENA_P(0);
|
2000-12-09 00:57:03 +01:00
|
|
|
Datum result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-09 00:57:03 +01:00
|
|
|
result = hash_any(VARDATA(key), VARSIZE(key) - VARHDRSZ);
|
|
|
|
|
|
|
|
/* Avoid leaking memory for toasted inputs */
|
|
|
|
PG_FREE_IF_COPY(key, 0);
|
|
|
|
|
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-06-19 05:55:01 +02:00
|
|
|
* 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.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* (Comment from the original db3 hashing code: )
|
|
|
|
*
|
2002-02-25 05:06:52 +01:00
|
|
|
* This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
|
1996-07-09 08:22:35 +02:00
|
|
|
* units. On the first time through the loop we get the 'leftover bytes'
|
2000-06-19 05:55:01 +02:00
|
|
|
* (strlen % 8). On every later iteration, we perform 8 HASHC's so we handle
|
1996-07-09 08:22:35 +02:00
|
|
|
* 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"
|
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
2002-02-25 05:06:52 +01:00
|
|
|
hash_any(const char *keydata, int keylen)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 n;
|
|
|
|
int loop;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
#define HASHC n = *keydata++ + 65599 * n
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (keylen > 0)
|
|
|
|
{
|
|
|
|
loop = (keylen + 8 - 1) >> 3;
|
|
|
|
|
|
|
|
switch (keylen & (8 - 1))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
#undef HASHC
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(n);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|