X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libhashkit%2Fhashkit.c;h=47c9e580c7b50df985b16ad7457939399c6eb24d;hb=31ed3cfdb9f1b7cf00a93dfefbe067b468a67507;hp=a87e712b19f57c482fe0e3f099e34d3198fb401e;hpb=a81bddebd55105aefa57af7cc87adbda3d1a022e;p=awesomized%2Flibmemcached diff --git a/libhashkit/hashkit.c b/libhashkit/hashkit.c index a87e712b..47c9e580 100644 --- a/libhashkit/hashkit.c +++ b/libhashkit/hashkit.c @@ -10,16 +10,24 @@ static const hashkit_st global_default_hash= { .base_hash= { - .type= HASHKIT_HASH_DEFAULT, .function= hashkit_one_at_a_time, .context= NULL }, + .flags= { + .is_base_same_distributed= false, + } }; -/** - @note We make no assumptions that "hashk" has been, or not been allocated from heap/stack. We just know we didn't do it. -*/ -hashkit_st *hashkit_create(hashkit_st *self) +static inline bool _hashkit_init(hashkit_st *self) +{ + self->base_hash= global_default_hash.base_hash; + self->distribution_hash= global_default_hash.base_hash; + self->flags= global_default_hash.flags; + + return true; +} + +static inline hashkit_st *_hashkit_create(hashkit_st *self) { if (self == NULL) { @@ -36,7 +44,19 @@ hashkit_st *hashkit_create(hashkit_st *self) self->options.is_allocated= false; } - self->base_hash= global_default_hash.base_hash; + return self; +} + +hashkit_st *hashkit_create(hashkit_st *self) +{ + self= _hashkit_create(self); + if (! self) + return self; + + if (! _hashkit_init(self)) + { + hashkit_free(self); + } return self; } @@ -50,9 +70,6 @@ void hashkit_free(hashkit_st *self) } } -/** - @note We do assume source is valid. If source does not exist, we allocate. -*/ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) { if (source == NULL) @@ -61,7 +78,7 @@ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) } /* new_clone will be a pointer to destination */ - destination= hashkit_create(destination); + destination= _hashkit_create(destination); // Should only happen on allocation failure. if (destination == NULL) @@ -70,53 +87,22 @@ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) } destination->base_hash= source->base_hash; + destination->distribution_hash= source->distribution_hash; + destination->flags= source->flags; return destination; } - -uint32_t hashkit_generate_value(const hashkit_st *self, const char *key, size_t key_length) -{ - return self->base_hash.function(key, key_length); -} - -uint32_t libhashkit_generate_value(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm) +bool hashkit_compare(const hashkit_st *first, const hashkit_st *second) { - switch (hash_algorithm) + if (first->base_hash.function == second->base_hash.function && + first->base_hash.context == second->base_hash.context && + first->distribution_hash.function == second->distribution_hash.function && + first->distribution_hash.context == second->distribution_hash.context && + first->flags.is_base_same_distributed == second->flags.is_base_same_distributed) { - case HASHKIT_HASH_DEFAULT: - return hashkit_one_at_a_time(key, key_length); - case HASHKIT_HASH_MD5: - return hashkit_md5(key, key_length); - case HASHKIT_HASH_CRC: - return hashkit_crc32(key, key_length); - case HASHKIT_HASH_FNV1_64: - return hashkit_fnv1_64(key, key_length); - case HASHKIT_HASH_FNV1A_64: - return hashkit_fnv1a_64(key, key_length); - case HASHKIT_HASH_FNV1_32: - return hashkit_fnv1_32(key, key_length); - case HASHKIT_HASH_FNV1A_32: - return hashkit_fnv1a_32(key, key_length); - case HASHKIT_HASH_HSIEH: -#ifdef HAVE_HSIEH_HASH - return hashkit_hsieh(key, key_length); -#else - return 1; -#endif - case HASHKIT_HASH_MURMUR: - return hashkit_murmur(key, key_length); - case HASHKIT_HASH_JENKINS: - return hashkit_jenkins(key, key_length); - case HASHKIT_HASH_MAX: - default: -#ifdef HAVE_DEBUG - fprintf(stderr, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n"); - fflush(stderr); - assert(0); -#endif - break; + return true; } - return 1; + return false; }