#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "utils/dsa.h"
+#include "utils/hsearch.h"
#include "utils/memutils.h"
/*
/*
* Create a new hash table backed by the given dynamic shared area, with the
* given parameters. The returned object is allocated in backend-local memory
- * using the current MemoryContext. If 'arg' is non-null, the arg variants of
- * hash and compare functions must be provided in 'params' and 'arg' will be
- * passed down to them.
+ * using the current MemoryContext. 'arg' will be passed through to the
+ * compare and hash functions.
*/
dshash_table *
dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
dshash_table *hash_table;
dsa_pointer control;
- /* Sanity checks on the set of supplied functions. */
- Assert((params->compare_function != NULL) ^
- (params->compare_arg_function != NULL));
- Assert((params->hash_function != NULL) ^
- (params->hash_arg_function != NULL));
- Assert(arg == NULL || (params->compare_arg_function != NULL));
- Assert(arg == NULL || (params->hash_arg_function != NULL));
-
/* Allocate the backend-local object representing the hash table. */
hash_table = palloc(sizeof(dshash_table));
/*
* Attach to an existing hash table using a handle. The returned object is
- * allocated in backend-local memory using the current MemoryContext. If
- * 'arg' is non-null, the arg variants of hash and compare functions must be
- * provided in 'params' and 'arg' will be passed down to them.
+ * allocated in backend-local memory using the current MemoryContext. 'arg'
+ * will be passed through to the compare and hash functions.
*/
dshash_table *
dshash_attach(dsa_area *area, const dshash_parameters *params,
dshash_table *hash_table;
dsa_pointer control;
- /* Sanity checks on the set of supplied functions. */
- Assert((params->compare_function != NULL) ^
- (params->compare_arg_function != NULL));
- Assert((params->hash_function != NULL) ^
- (params->hash_arg_function != NULL));
- Assert(arg == NULL || (params->compare_arg_function != NULL));
- Assert(arg == NULL || (params->hash_arg_function != NULL));
-
/* Allocate the backend-local object representing the hash table. */
hash_table = palloc(sizeof(dshash_table));
LWLockRelease(PARTITION_LOCK(hash_table, partition_index));
}
+/*
+ * A compare function that forwards to memcmp.
+ */
+int
+dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
+{
+ return memcmp(a, b, size);
+}
+
+/*
+ * A hash function that forwards to tag_hash.
+ */
+dshash_hash
+dshash_memhash(const void *v, size_t size, void *arg)
+{
+ return tag_hash(v, size);
+}
+
/*
* Print debugging information about the internal state of the hash table to
* stderr. The caller must hold no partition locks.
static inline dshash_hash
hash_key(dshash_table *hash_table, const void *key)
{
- if (hash_table->params.hash_arg_function != NULL)
- return hash_table->params.hash_arg_function(key, hash_table->arg);
- else
- return hash_table->params.hash_function(key,
- hash_table->params.key_size);
+ return hash_table->params.hash_function(key,
+ hash_table->params.key_size,
+ hash_table->arg);
}
/*
static inline bool
equal_keys(dshash_table *hash_table, const void *a, const void *b)
{
- int r;
-
- if (hash_table->params.compare_arg_function != NULL)
- r = hash_table->params.compare_arg_function(a, b, hash_table->arg);
- else
- r = hash_table->params.compare_function(a, b,
- hash_table->params.key_size);
-
- return r == 0;
+ return hash_table->params.compare_function(a, b,
+ hash_table->params.key_size,
+ hash_table->arg) == 0;
}
/* The type for hash values. */
typedef uint32 dshash_hash;
-/*
- * A function used for comparing keys. This version is compatible with
- * HashCompareFunction in hsearch.h and memcmp.
- */
-typedef int (*dshash_compare_function) (const void *a, const void *b, size_t size);
+/* A function type for comparing keys. */
+typedef int (*dshash_compare_function) (const void *a, const void *b,
+ size_t size, void *arg);
-/*
- * A function type used for comparing keys. This version allows compare
- * functions to receive a pointer to arbitrary user data that was given to the
- * create or attach function. Similar to qsort_arg_comparator.
- */
-typedef int (*dshash_compare_arg_function) (const void *a, const void *b, void *arg);
-
-/*
- * A function type for computing hash values for keys. This version is
- * compatible with HashValueFunc in hsearch.h and hash functions like
- * tag_hash.
- */
-typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size);
-
-/*
- * A function type for computing hash values for keys. This version allows
- * hash functions to receive a pointer to arbitrary user data that was given
- * to the create or attach function.
- */
-typedef dshash_hash (*dshash_hash_arg_function) (const void *v, void *arg);
+/* A function type for computing hash values for keys. */
+typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
+ void *arg);
/*
* The set of parameters needed to create or attach to a hash table. The
size_t key_size; /* Size of the key (initial bytes of entry) */
size_t entry_size; /* Total size of entry */
dshash_compare_function compare_function; /* Compare function */
- dshash_compare_arg_function compare_arg_function; /* Arg version */
dshash_hash_function hash_function; /* Hash function */
- dshash_hash_arg_function hash_arg_function; /* Arg version */
int tranche_id; /* The tranche ID to use for locks */
} dshash_parameters;
extern void dshash_delete_entry(dshash_table *hash_table, void *entry);
extern void dshash_release_lock(dshash_table *hash_table, void *entry);
+/* Convenience hash and compare functions wrapping memcmp and tag_hash. */
+extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
+extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
+
/* Debugging support. */
extern void dshash_dump(dshash_table *hash_table);