forked from 1a1a11a/libCacheSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsistentHash.h
More file actions
99 lines (83 loc) · 2.17 KB
/
consistentHash.h
File metadata and controls
99 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by Juncheng Yang on 2019-06-21.
//
#ifndef CONSISTENT_HASH_H_
#define CONSISTENT_HASH_H_
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N_VNODE_PER_SERVER 160
typedef struct {
unsigned int point; // point on ring
unsigned int server_id;
} vnode_t;
typedef struct {
unsigned int n_point;
unsigned int n_server;
vnode_t *vnodes;
} ring_t;
// typedef int (*compfn)(const void *, const void *);
/**
* @brief create a consistent hash ring with n servers
*
* @param n_server
* @param weight null if all servers have the same weight
* @return ring_t*
*/
ring_t *ch_ring_create_ring(int n_server, double *weight);
/**
* @brief retrieve the server id from the consistent hash ring
*
* @param key
* @param ring
* @return vnode_t*
*/
// vnode_t *ch_ring_get_server(const char *const key, const ring_t *const ring);
int ch_ring_get_server(const char *const key, const ring_t *const ring);
/**
* @brief retrieve the server id from the consistent hash ring
*
* @param obj_id
* @param ring
* @return int
*/
int ch_ring_get_server_from_uint64(uint64_t obj_id, const ring_t *const ring);
/**
* @brief retrieve the n consecutive server ids from the consistent hash ring
*
* @param key
* @param ring
* @param n
* @param idxs
*/
void ch_ring_get_servers(const char *const key, const ring_t *const ring,
const unsigned int n, unsigned int *idxs);
/**
* @brief retrieve n consecutive available server ids from the consistent hash
* ring
*
* @param key
* @param ring
* @param n
* @param idxs
* @param server_unavailability
*/
void ch_ring_get_available_servers(const char *const key,
const ring_t *const ring,
const unsigned int n, unsigned int *idxs,
const char *server_unavailability);
/**
* @brief destroy consistent hash ring
*
* @param ring
*/
void ch_ring_destroy_ring(ring_t *ring);
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif
#endif // CONSISTENT_HASH_H_