Skip to content
Snippets Groups Projects
Commit df3c7507 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Removed now deprecated CK hashtable.

parent db6b5b55
No related branches found
No related tags found
No related merge requests found
......@@ -20,12 +20,6 @@ src/libknot/edns.c
src/libknot/nsec3.h
src/libknot/nsec3.c
src/libknot/consts.h
src/libknot/hash/cuckoo-hash-table.c
src/libknot/hash/cuckoo-hash-table.h
src/libknot/hash/hash-functions.c
src/libknot/hash/hash-functions.h
src/libknot/hash/universal-system.c
src/libknot/hash/universal-system.h
src/libknot/nameserver/name-server.c
src/libknot/nameserver/name-server.h
src/libknot/packet/packet.h
......
......@@ -162,12 +162,6 @@ libknot_la_SOURCES = \
libknot/zone/node.c \
libknot/zone/zone-diff.h \
libknot/zone/zone-diff.c \
libknot/hash/hash-functions.c \
libknot/hash/cuckoo-hash-table.c \
libknot/hash/universal-system.c \
libknot/hash/universal-system.h \
libknot/hash/cuckoo-hash-table.h \
libknot/hash/hash-functions.h \
libknot/nameserver/name-server.h \
libknot/nameserver/name-server.c \
libknot/updates/changesets.h \
......
This diff is collapsed.
/*!
* \file cuckoo-hash-table.h
*
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* \brief Implementation of Cuckoo hashing scheme.
*
* Uses d-ary Cuckoo hashing with stash.
*
* \todo Maybe provide some way to resize the whole table if the number of items
* grows too much.
* \todo Check size of integers, the table size may be larger than unsigned int.
* \todo Maybe do not return ck_hash_table_item from ck_find_item(), but only
* its value.
* \todo When hashing an item, only the first table is tried for this item.
* We may try all tables. (But it is not neccessary.)
*
* \addtogroup hashing
* @{
*/
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _KNOT_CUCKOO_HASH_TABLE_H_
#define _KNOT_CUCKOO_HASH_TABLE_H_
#include <stdint.h> /* uint32_t */
#include <stdlib.h> /* size_t */
#include <pthread.h>
#include "hash/universal-system.h"
/*----------------------------------------------------------------------------*/
/*! \brief Macro for getting one hash table size. */
#define hashsize(n) ((uint32_t)1 << (n))
/*!
* \brief Max number of hash tables - must be the same as number of the hash
* functions in each generation of the universal system.
*/
#define MAX_TABLES US_FNC_COUNT
/*! \brief Default stash size. */
static const uint STASH_SIZE = 10;
/*! \brief Maximum stash size. When achieved, rehashing is needed. */
static const uint STASH_SIZE_MAX = 30;
/*----------------------------------------------------------------------------*/
/* Public structures */
/*----------------------------------------------------------------------------*/
/*!
* \brief Structure for storing the hashed data.
*/
struct ck_hash_table_item {
const char *key; /*!< Key of the item, used for hashing. */
size_t key_length; /*!< Length of the key in octets. */
void *value; /*!< The actual item stored in the table. */
/*!
* \brief Flags. Currently used for keeping the generation of the item,
* i.e. the generation of the functions used for hashing this
* item.
*
* Form: 000000xy;
* xy - generation; may be 01 (1) or 10 (2).
*/
uint8_t timestamp;
};
typedef struct ck_hash_table_item ck_hash_table_item_t;
struct ck_stash_item {
ck_hash_table_item_t *item;
struct ck_stash_item *next;
};
typedef struct ck_stash_item ck_stash_item_t;
/*----------------------------------------------------------------------------*/
/*!
* \brief Hash table structure which uses cuckoo hashing.
*
* Keys are expected to be strings of characters (char *), not necesarily
* null-terminated. It uses the Fowler/Noll/Vo (FNV) hash function to
* obtain a 32bit unsigned integer from the character data and a function
* randomly chosen from an universal system (see universal-system.h) to obtain
* the final hash. The FNV hash was taken from
* http://home.comcast.net/~bretm/hash/6.html and the universal system is
* constructed according to Katajainen J., Lykke M., Experiments with universal
* hashing (obtained from
* http://www.diku.dk/OLD/publikationer/tekniske.rapporter/rapporter/96-08.pdf).
*
* The table uses either 3-ary or 4-ary cuckoo hashing (and thus 3 or 4 tables)
* with stash, according to the number of items provided to ck_create_table()
* function. The number of table pointers is however set to be the larger value
* (4) always, so the \a tables array may be statically allocated. Size of one
* table is always a power of 2 (due to the character of the hash function).
* The stash has a default size STASH_SIZE, but can be resized if needed.
* However, the resizing is only done in rehashing process, if the items do not
* fit into the table and the original stash.
*
* Rehashing is done when the stash gets full (actually, last item is always
* free and is used in the rehashing process as a temporary variable).
*/
struct ck_hash_table {
uint table_count; /*!< Actual number of hash tables used. */
/*!
* \brief Exponent of one table's size (2^table_size_exp is table size).
*/
int table_size_exp;
ck_hash_table_item_t **tables[MAX_TABLES]; /*!< Array of hash tables. */
//da_array_t stash; /*!< Stash implemented as a dynamic array. */
ck_stash_item_t *stash;
/*! \brief Temporary storage for item being hashed. */
ck_hash_table_item_t *hashed;
/*! \brief Mutex for avoiding multiple insertions / rehashes at once. */
pthread_mutex_t mtx_table;
/*!
* \brief Flags used for determining which hash functions are currently
* used
*
* Form: 00000xyz.
* x - rehash flag (1 if rehashing is in progress)
* yz - generation (may be 10 = 2, or 01 = 1)
*
* There are always two sets of hash functions available via the
* us_hash() function (see universal-hashing.h). Normally all items in
* the table are hashed using one set of functions. However, during
* rehash, the other set is used for rehashing. In this case the rehash
* flag (x) is set, so the lookup function (ck_find_item()) tries to use
* both sets of functions when searching for item.
*/
uint8_t generation;
us_system_t hash_system; /*!< Universal system of hash functions. */
size_t items;
size_t items_in_stash;
};
typedef struct ck_hash_table ck_hash_table_t;
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
/*!
* \brief Creates and initializes the hash table structure.
*
* All hash tables are allocated and their items initialized to 0 (NULL).
* A stash of default size is also created. The \a generation flags are set to
* 0.
*
* \param items Number of items to be hashed to the table. This number
* determines the size of the hash table that will be created.
*
*
* \return Pointer to the initialized hash table.
*/
ck_hash_table_t *ck_create_table(uint items);
/*----------------------------------------------------------------------------*/
/*!
* \brief Destroys the whole hash table together with the saved values.
*
* \param table Pointer to pointer to the hash table.
* \param dtor_value Destructor function for the values that are be stored in
* the hash table. Set to NULL if you do not want the values
* to be deleted.
* \param delete_key Set to 0 if you do not want the function to delete the
* key of the item (e.g. when used elsewhere). Set to any
* other value otherwise.
*
* \note Make sure the table and its items are not used anymore when calling
* this function.
*/
void ck_destroy_table(ck_hash_table_t **table,
void (*dtor_value)(void *value), int delete_key);
/*!
* \brief Destroys the table structures, but does not remove the individual
* hash table items.
*/
void ck_table_free(ck_hash_table_t **table);
/*----------------------------------------------------------------------------*/
/*!
* \brief Inserts item into the hash table.
*
* Insertion starts always by trying to hash the item into the first table. The
* possible displaced item is then hashed into randomly chosen other table,
* etc., until a free place is found or a loop occured. A loop occurs when one
* position in one table is tried more than twice.
*
* \param table Hash table the item should be inserted into.
* \param key Item's key. It can be any string of octets. The key is not copied
* by the function.
* \param length Length of the key in bytes (octets).
* \param value Pointer to the actual item to be inserted into the hash table.
*
* \note This function does not copy the key.
* \note This function may trigger rehash of the whole table in case the stash
* gets full.
*
* \retval 0 No error.
* \retval -1 Insertion failed. This may occur only when the rehashing fails.
* In this case it is necessary to somehow manually force another
* rehash as no other rehash would be possible.
*/
int ck_insert_item(ck_hash_table_t *table, const char *key, size_t length,
void *value);
/*----------------------------------------------------------------------------*/
/*!
* \brief Finds item in table.
*
* \param table Hash table to search in.
* \param key Key of the item. It can be an arbitrary string of octets.
* \param length Length of the key in bytes (octets).
*
* \return Pointer to the item if found. NULL otherwise.
*/
const ck_hash_table_item_t *ck_find_item(const ck_hash_table_t *table,
const char *key, size_t length);
/*----------------------------------------------------------------------------*/
/*!
* \brief Updates item with the given key by replacing its value.
*
* The update process is synchronized using RCU mechanism, so the old item's
* value will not be deleted while some thread is using it.
*
* \param table Hash table where to search for the item.
* \param key Key of the item to be updated. It can be an arbitrary string of
* octets.
* \param length Length of the key in bytes (octets).
* \param new_value New value for the item with key \a key.
* \param dtor_value Destructor function for the values that are be stored in
* the hash table. Set to NULL if you do not want the values
* to be deleted.
*
* \retval 0 If successful.
* \retval -1 If the item was not found in the table. No changes are made.
*/
int ck_update_item(const ck_hash_table_t *table, const char *key, size_t length,
void *new_value, void (*dtor_value)(void *value));
/*----------------------------------------------------------------------------*/
/*!
* \brief Removes item with the given key from table.
*
* The deletion process is synchronized using RCU mechanism, so the old item
* will not be deleted while some thread is using it.
*
* \param table Hash table where to search for the item.
* \param key Key of the item to be removed. It can be an arbitrary string of
* octets.
* \param length Length of the key in bytes (octets).
* \param dtor_value Destructor function for the values that are be stored in
* the hash table. Set to NULL if you do not want the values
* to be deleted.
* \param delete_key Set to 0 if you do not want the function to delete the
* key of the item (e.g. when used elsewhere). Set to any
* other value otherwise.
*
* \retval 0 If successful.
* \retval -1 If the item was not found in the table.
*/
int ck_delete_item(const ck_hash_table_t *table, const char *key, size_t length,
void (*dtor_value)(void *value), int delete_key);
ck_hash_table_item_t *ck_remove_item(ck_hash_table_t *table, const char *key,
size_t length);
/*!
* \brief Creates a shallow copy of the cuckoo hash table.
*
* This function creates just the ck_hash_table_t structure and its tables and
* stash. It does not copy individual ck_hash_table_item_t structures.
*
* \param from Table to copy.
* \param to The new copy will be stored here.
*
* \retval 0 if successful.
* \retval
*/
int ck_shallow_copy(const ck_hash_table_t *from, ck_hash_table_t **to);
int ck_deep_copy(ck_hash_table_t *from, ck_hash_table_t **to);
int ck_apply(ck_hash_table_t *table,
void (*function)(ck_hash_table_item_t *item, void *data),
void *data);
/*----------------------------------------------------------------------------*/
int ck_rehash(ck_hash_table_t *table);
// for testing purposes only
int ck_resize_table(ck_hash_table_t *table);
/*----------------------------------------------------------------------------*/
/*!
* \brief Dumps the whole hash table to the standard output.
*/
void ck_dump_table(const ck_hash_table_t *table);
/*----------------------------------------------------------------------------*/
#endif /* _KNOT_CUCKOO_HASH_TABLE_H_ */
/*! @} */
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "hash-functions.h"
/*--------------------------------- FNV HASH ---------------------------------*/
unsigned long int fnv_hash(const char *data, int size, int bits)
{
int shift, i;
unsigned long int mask;
unsigned long int hash = 2166136261;
if (bits == -1) {
shift = 0;
mask = 0xFFFFFFFF;
} else {
shift = 32 - bits;
mask = (1U << shift) - 1U;
}
for (i = 0; i < size; i++) {
hash = (hash * 16777619) ^ data[i];
}
if (shift == 0) {
return hash;
}
return (hash ^(hash >> shift)) & mask;
}
/*------------------------------- JENKINS HASH -------------------------------*/
/* The mixing step */
/*
#define mix(a,b,c) \
{ \
a=a-b; a=a-c; a=a^(c>>13); \
b=b-c; b=b-a; b=b^(a<<8); \
c=c-a; c=c-b; c=c^(b>>13); \
a=a-b; a=a-c; a=a^(c>>12); \
b=b-c; b=b-a; b=b^(a<<16); \
c=c-a; c=c-b; c=c^(b>>5); \
a=a-b; a=a-c; a=a^(c>>3); \
b=b-c; b=b-a; b=b^(a<<10); \
c=c-a; c=c-b; c=c^(b>>15); \
}
*/
///* The whole new hash function */
//u4 jhash(register u1 *k, u4 length, u4 initval)
//{
// register u4 a, b, c; /* the internal state */
// u4 len; /* how many key bytes still need mixing */
// /* Set up the internal state */
// len = length;
// a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
// c = initval; /* variable initialization of internal state */
// /*---------------------------------------- handle most of the key */
// while (len >= 12) {
// a = a + (k[0] + ((u4)k[1] << 8)
// + ((u4)k[2] << 16) + ((u4)k[3] << 24));
// b = b + (k[4] + ((u4)k[5] << 8)
// + ((u4)k[6] << 16) + ((u4)k[7] << 24));
// c = c + (k[8] + ((u4)k[9] << 8)
// + ((u4)k[10] << 16) + ((u4)k[11] << 24));
// mix(a, b, c);
// k = k + 12;
// len = len - 12;
// }
// /*------------------------------------- handle the last 11 bytes */
// c = c + length;
// switch (len) { /* all the case statements fall through */
// case 11:
// c = c + ((u4)k[10] << 24);
// case 10:
// c = c + ((u4)k[9] << 16);
// case 9 :
// c = c + ((u4)k[8] << 8);
// /* the first byte of c is reserved for the length */
// case 8 :
// b = b + ((u4)k[7] << 24);
// case 7 :
// b = b + ((u4)k[6] << 16);
// case 6 :
// b = b + ((u4)k[5] << 8);
// case 5 :
// b = b + k[4];
// case 4 :
// a = a + ((u4)k[3] << 24);
// case 3 :
// a = a + ((u4)k[2] << 16);
// case 2 :
// a = a + ((u4)k[1] << 8);
// case 1 :
// a = a + k[0];
// /* case 0: nothing left to add */
// }
// mix(a, b, c);
// /*-------------------------------------------- report the result */
// return c;
//}
#define hashsize(n) ((ub4)1<<(n))
#define hashmask(n) (hashsize(n)-1)
/*
--------------------------------------------------------------------
mix -- mix 3 32-bit values reversibly.
For every delta with one or two bits set, and the deltas of all three
high bits or all three low bits, whether the original value of a,b,c
is almost all zero or is uniformly distributed,
* If mix() is run forward or backward, at least 32 bits in a,b,c
have at least 1/4 probability of changing.
* If mix() is run forward, every bit of c will change between 1/3 and
2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
mix() was built out of 36 single-cycle latency instructions in a
structure that could supported 2x parallelism, like so:
a -= b;
a -= c; x = (c>>13);
b -= c; a ^= x;
b -= a; x = (a<<8);
c -= a; b ^= x;
c -= b; x = (b>>13);
...
Unfortunately, superscalar Pentiums and Sparcs can't take advantage
of that parallelism. They've also turned some of those single-cycle
latency instructions into multi-cycle latency instructions. Still,
this is the fastest good hash I could find. There were about 2^^68
to choose from. I only looked at a billion or so.
--------------------------------------------------------------------
*/
#define mix(a,b,c) \
{ \
a -= b; a -= c; a ^= (c>>13); \
b -= c; b -= a; b ^= (a<<8); \
c -= a; c -= b; c ^= (b>>13); \
a -= b; a -= c; a ^= (c>>12); \
b -= c; b -= a; b ^= (a<<16); \
c -= a; c -= b; c ^= (b>>5); \
a -= b; a -= c; a ^= (c>>3); \
b -= c; b -= a; b ^= (a<<10); \
c -= a; c -= b; c ^= (b>>15); \
}
/*
--------------------------------------------------------------------
hash() -- hash a variable-length key into a 32-bit value
k : the key (the unaligned variable-length array of bytes)
len : the length of the key, counting by bytes
initval : can be any 4-byte value
Returns a 32-bit value. Every bit of the key affects every bit of
the return value. Every 1-bit and 2-bit delta achieves avalanche.
About 6*len+35 instructions.
The best hash table sizes are powers of 2. There is no need to do
mod a prime (mod is sooo slow!). If you need less than 32 bits,
use a bitmask. For example, if you need only 10 bits, do
h = (h & hashmask(10));
In which case, the hash table should have hashsize(10) elements.
If you are hashing n strings (ub1 **)k, do it like this:
for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
code any way you wish, private, educational, or commercial. It's free.
See http://burtleburtle.net/bob/hash/evahash.html
Use for hash table lookup, or anything where one collision in 2^^32 is
acceptable. Do NOT use for cryptographic purposes.
--------------------------------------------------------------------
*/
ub4 jhash(k, length, initval)
register ub1 *k; /* the key */
register ub4 length; /* the length of the key */
register ub4 initval; /* the previous hash, or an arbitrary value */
{
register ub4 a,b,c,len;
/* Set up the internal state */
len = length;
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
c = initval; /* the previous hash value */
/*---------------------------------------- handle most of the key */
while (len >= 12)
{
a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
mix(a,b,c);
k += 12; len -= 12;
}
/*------------------------------------- handle the last 11 bytes */
c += length;
switch(len) /* all the case statements fall through */
{
case 11: c+=((ub4)k[10]<<24);
case 10: c+=((ub4)k[9]<<16);
case 9 : c+=((ub4)k[8]<<8);
/* the first byte of c is reserved for the length */
case 8 : b+=((ub4)k[7]<<24);
case 7 : b+=((ub4)k[6]<<16);
case 6 : b+=((ub4)k[5]<<8);
case 5 : b+=k[4];
case 4 : a+=((ub4)k[3]<<24);
case 3 : a+=((ub4)k[2]<<16);
case 2 : a+=((ub4)k[1]<<8);
case 1 : a+=k[0];
/* case 0: nothing left to add */
}
mix(a,b,c);
/*-------------------------------------------- report the result */
return c;
}
#undef hashsize
#undef hashmask
/*!
* \file hash-functions.h
*
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* \brief Various hash functions.
*
* All of the hash functions are downloaded from various sources.
*
* \todo Add references to sources.
*
* \addtogroup hashing
* @{
*/
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _KNOT_HASH_FUNCTIONS_H_
#define _KNOT_HASH_FUNCTIONS_H_
#include <stdint.h>
#include <string.h>
/*
* Fowler / Noll / Vo Hash (FNV Hash)
* http://www.isthe.com/chongo/tech/comp/fnv/
*
* This is an implementation of the algorithms posted above.
* This file is placed in the public domain by Peter Wemm.
*
* $FreeBSD: src/sys/sys/fnv_hash.h,v 1.2.2.1 2001/03/21 10:50:59 peter Exp $
*/
typedef uint32_t Fnv32_t;
#define FNV1_32_INIT ((Fnv32_t) 33554467UL)
#define FNV_32_PRIME ((Fnv32_t) 0x01000193UL)
static __inline Fnv32_t
fnv_32_buf(const void *buf, size_t len, Fnv32_t hval)
{
const uint8_t *s = (const uint8_t *)buf;
while (len-- != 0) {
hval *= FNV_32_PRIME;
hval ^= *s++;
}
return hval;
}
/*!
* \brief Jenkins hash function.
*
* Downloaded from http://burtleburtle.net/bob/hash/evahash.html
*
* \param k Data to hash
* \param length Size of the data in bytes.
* \param initval The previous hash or an arbitrary value.
*
* \return Hash of the data.
*
* \todo Add source.
*/
typedef unsigned long int ub4; /* unsigned 4-byte quantities */
typedef unsigned char ub1; /* unsigned 1-byte quantities */
ub4 jhash(register ub1 *k, register ub4 length, register ub4 initval);
#endif /* _KNOT_HASH_FUNCTIONS_H_ */
/*! @} */
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <limits.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "universal-system.h"
#include "common.h"
#include "util/utils.h"
/*----------------------------------------------------------------------------*/
const uint MAX_UINT_EXP = 32;
const unsigned long MAX_UINT_MY = UINT32_MAX; /* 4294967295 */
/*----------------------------------------------------------------------------*/
/* Private functions */
/*----------------------------------------------------------------------------*/
/*!
* \brief Generates new set of coeficients.
*
* \param system Universal system to generate the coeficients for.
* \param from First coeficient to be replaced.
* \param to Up to this the coeficients will be replaced.
*/
static void us_generate_coefs(us_system_t *system, uint from, uint to)
{
assert(system != NULL);
for (uint i = from; i < to; ++i) {
int used = 0;
do {
// generate random odd number
system->coefs[i] = knot_quick_rand() % MAX_UINT_MY;
if (system->coefs[i] % 2 == 0) {
system->coefs[i] = (system->coefs[i] == 0)
? 1
: system->coefs[i] - 1;
}
// check if this coeficient is already used
uint j = from;
while (used == 0 && j < i) {
if (system->coefs[j++] == system->coefs[i]) {
used = 1;
}
}
// if already used, generate again
} while (used != 0);
}
}
/*----------------------------------------------------------------------------*/
/* Public functions */
/*----------------------------------------------------------------------------*/
void us_initialize(us_system_t *system)
{
assert(system != NULL);
assert(UINT_MAX == MAX_UINT_MY);
// Initialize both generations of functions by generating random odd
// numbers
us_generate_coefs(system, 0, US_FNC_COUNT * GEN_COUNT);
}
/*----------------------------------------------------------------------------*/
/*!
* \note \a generation starts from 1
*/
int us_next(us_system_t *system, uint generation)
{
assert(system != NULL);
// generate new coeficients for the new generation
us_generate_coefs(system, (generation - 1) * US_FNC_COUNT,
generation * US_FNC_COUNT);
return 0;
}
/*----------------------------------------------------------------------------*/
uint32_t us_hash(const us_system_t *system, uint32_t value, uint table_exp,
uint fnc, uint generation)
{
/*
* multiplication should overflow if larger than MAX_UINT
* this is the same as (coef * value) mod MAX_UINT
*
* TODO: maybe we should not rely on this
*/
assert(system != NULL);
assert(table_exp <= 32);
assert(fnc < US_FNC_COUNT);
assert(generation <= GEN_COUNT);
return ((system->coefs[((generation - 1) * US_FNC_COUNT) + fnc] * value)
>> (MAX_UINT_EXP - table_exp));
}
/*!
* \file universal-system.h
*
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* This file provides interface to a 2-universal system of hash functions that
* hash from 32-bit unsigned integer to a 32-bit unsigned integer within a given
* range. The range is always a power of two and is given by the exponent (see
* function us_hash().
*
* Before using the system, it must be initialized by calling us_initialize().
* The system stores 2 sets (generations), each of US_FNC_COUNT functions.
* For generating a new set of coeficients (i.e. hash functions) use the
* us_next() function.
*
* For hashing use the us_hash() function.
*
* \todo What if all numbers are tried and still need rehash?
* (that means 2mld rehashes - we can probably live with that ;)
* \todo Consider counting generations from 0, will be easier!
* \todo Check out some better random number generator.
*
* \addtogroup hashing
* @{
*/
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _KNOT_UNIVERSAL_SYSTEM_H_
#define _KNOT_UNIVERSAL_SYSTEM_H_
#include <stdint.h>
#include "common.h"
enum { US_FNC_COUNT = 4 /*!< Number of functions for one generation. */ };
enum { GEN_COUNT = 2 /*!< Number of generations. */ };
/*----------------------------------------------------------------------------*/
/*! \brief Analytically defined universal system of hashing functions. */
struct us_system {
/*! \brief Coeficients for the functions */
uint coefs[US_FNC_COUNT * GEN_COUNT];
};
typedef struct us_system us_system_t;
/*----------------------------------------------------------------------------*/
/*!
* \brief Initializes the universal system by generating coeficients for all
* hash functions and all generations.
*
* \param system Universal system to be used.
*/
void us_initialize(us_system_t *system);
/*----------------------------------------------------------------------------*/
/*!
* \brief Generates new hash functions' coeficients for the given \a generation.
*
* \param system Universal system to be used.
* \param generation Generation for which to generate the new coeficients.
*
* \return 0
*/
int us_next(us_system_t *system, uint generation);
/*----------------------------------------------------------------------------*/
/*!
* \brief Hashes the \a value using the given \a exponent and function.
*
* The actual formula of the hash is:
* h = ((coef * value) mod 2^32) / 2^(32 - table_exp)
* where \a coef is the proper coeficient.
*
* \param system Universal system to be used.
* \param value Value to be hashed.
* \param table_exp Determines the upper bound for the result - the hash will
* be between 0 and 2^(32 - table_exp).
* \param fnc Which function from the set should be used.
* \param generation Which set (generation) of functions should be used.
*
* \todo Make inline?
*
* \return Hash value (32bit unsigned).
*/
uint32_t us_hash(const us_system_t *system, uint32_t value, uint table_exp,
uint fnc, uint generation);
/*----------------------------------------------------------------------------*/
#endif /* _KNOT_UNIVERSAL_SYSTEM_H_ */
/*! @} */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment