diff --git a/src/backend/distributed/utils/hash_helpers.c b/src/backend/distributed/utils/hash_helpers.c new file mode 100644 index 000000000..e74616b37 --- /dev/null +++ b/src/backend/distributed/utils/hash_helpers.c @@ -0,0 +1,34 @@ +/*------------------------------------------------------------------------- + * + * hash_helpers.c + * Helpers for dynahash.c style hash tables. + * + * Copyright (c) 2016, Citus Data, Inc. + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "distributed/hash_helpers.h" +#include "utils/hsearch.h" + + +/* + * Empty a hash, without destroying the hash table itself. + */ +void +hash_delete_all(HTAB *htab) +{ + HASH_SEQ_STATUS status; + void *entry = NULL; + + hash_seq_init(&status, htab); + while ((entry = hash_seq_search(&status)) != 0) + { + bool found = false; + + hash_search(htab, entry, HASH_REMOVE, &found); + Assert(found); + } +} diff --git a/src/include/distributed/hash_helpers.h b/src/include/distributed/hash_helpers.h new file mode 100644 index 000000000..0fecf0c03 --- /dev/null +++ b/src/include/distributed/hash_helpers.h @@ -0,0 +1,30 @@ +/*------------------------------------------------------------------------- + * hash_helpers.h + * + * Copyright (c) 2016, Citus Data, Inc. + * + *------------------------------------------------------------------------- + */ + +#ifndef HASH_HELPERS_H +#define HASH_HELPERS_H + +#include "utils/hsearch.h" + +/* + * Combine two hash values, resulting in another hash value, with decent bit + * mixing. + * + * Similar to boost's hash_combine(). + */ +static inline uint32 +hash_combine(uint32 a, uint32 b) +{ + a ^= b + 0x9e3779b9 + (a << 6) + (a >> 2); + return a; +} + + +extern void hash_delete_all(HTAB *htab); + +#endif