#ifndef __NVN_CONSISTENT_HASH_H__
#define __NVN_CONSISTENT_HASH_H__

#include <stdint.h>

#ifndef CONHASH_MAX_POINTS_PER_BUCKET
#define CONHASH_MAX_POINTS_PER_BUCKET 128
#endif

#ifdef __cplusplus
extern "C"
{
#endif

enum CONHASH_ERRCODE
{
	CONHASH_OK = 0,
	CONHASH_ERR_INVALID_ARGS = -1,
	CONHASH_BUCKET_NOT_FOUND = -2,
	CONHASH_BUCKET_ALREADY_EXIST=-3,
	CONHASH_NO_VALID_BUCKETS=-4,
};

struct conhash_bucket
{
	uint32_t bucket_id;
	uint32_t point_num;    /*should be not more than CONHASH_MAX_POINTS_PER_BUCKET*/
	void* tag;
};
struct consistent_hash;

/*API线程不安全*/
struct consistent_hash *conhash_instance_new(const struct conhash_bucket *buckets, uint32_t bucket_num);
void conhash_instance_free(struct consistent_hash *ch);
struct consistent_hash *conhash_instance_copy(struct consistent_hash *ch);

enum CONHASH_ERRCODE conhash_insert_bucket(struct consistent_hash *ch,const struct conhash_bucket* bucket);
enum CONHASH_ERRCODE conhash_remove_bucket(struct consistent_hash *ch, u_int32_t bucket_id, void (*free_cb)(void *tag, u_int32_t point_num));
enum CONHASH_ERRCODE conhash_renew_bucket(struct consistent_hash *ch, struct conhash_bucket* bucket); /*更新point_num和tag*/
enum CONHASH_ERRCODE conhash_lookup_bucket(struct consistent_hash *ch, const void* key, int len, struct conhash_bucket *result/*OUT*/);
enum CONHASH_ERRCODE conhash_lookup_bucket_int(struct consistent_hash *ch, u_int64_t randint, struct conhash_bucket* result);

double conhash_calulate_CVRSMD(struct consistent_hash *p);
u_int32_t conhash_get_bucket_num(struct consistent_hash *ch);

#ifdef __cplusplus
}
#endif

#endif
