Temp save for Monty.
[awesomized/libmemcached] / libhashkit / behavior.c
1 /* HashKit
2 * Copyright (C) 2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include "common.h"
10
11 static hashkit_fn *fetch_hash_fn(hashkit_hash_algorithm_t hash_algorithm)
12 {
13 switch (hash_algorithm)
14 {
15 case HASHKIT_HASH_DEFAULT:
16 return hashkit_default;
17 case HASHKIT_HASH_MD5:
18 return hashkit_md5;
19 case HASHKIT_HASH_CRC:
20 return hashkit_crc32;
21 case HASHKIT_HASH_FNV1_64:
22 return hashkit_fnv1_64;
23 case HASHKIT_HASH_FNV1A_64:
24 return hashkit_fnv1a_64;
25 case HASHKIT_HASH_FNV1_32:
26 return hashkit_fnv1_32;
27 case HASHKIT_HASH_FNV1A_32:
28 return hashkit_fnv1a_32;
29 case HASHKIT_HASH_HSIEH:
30 #ifdef HAVE_HSIEH_HASH
31 return hashkit_hsieh;
32 #else
33 return NULL;
34 #endif
35 case HASHKIT_HASH_MURMUR:
36 return hashkit_murmur;
37 case HASHKIT_HASH_JENKINS:
38 return hashkit_jenkins;
39 case HASHKIT_HASH_MAX:
40 default:
41 #ifdef HAVE_DEBUG
42 fprintf(stderr, "hashkit_hash_t was extended but hashkit_generate_value was not updated\n");
43 fflush(stderr);
44 assert(0);
45 #endif
46 break;
47 }
48
49 return NULL;
50 }
51
52 hashkit_return_t hashkit_behavior_set_distribution(hashkit_st *hashkit, hashkit_distribution_t distribution)
53 {
54 hashkit->distribution= distribution;
55
56 return HASHKIT_SUCCESS;
57 }
58
59
60 hashkit_distribution_t hashkit_behavior_get_distribution(hashkit_st *hashkit)
61 {
62 return hashkit->distribution;
63 }
64
65
66 /**
67 @note For the moment we will not allow the user to set the distribution hash type.
68 */
69 hashkit_return_t hashkit_behavior_set_key_hash_algorithm(hashkit_st *hashkit, hashkit_hash_algorithm_t hash_algorithm)
70 {
71 hashkit_fn *hash_fn= fetch_hash_fn(hash_algorithm);
72
73 if (hash_fn == NULL)
74 return HASHKIT_FAILURE;
75
76 hashkit->hash_fn= hash_fn;
77 hashkit->for_key= hash_algorithm;
78 hashkit->for_distribution= hash_algorithm;
79
80 return HASHKIT_SUCCESS;
81 }
82
83
84 hashkit_hash_algorithm_t hashkit_behavior_get_key_hash_algorithm(hashkit_st *hashkit)
85 {
86 return hashkit->for_key;
87 }
88
89
90 void hashkit_behavior_set_active_fn(hashkit_st *hashkit, hashkit_active_fn *function)
91 {
92 hashkit->active_fn= function;
93 }
94
95
96 hashkit_active_fn * hashkit_behavior_get_active_fn(hashkit_st *hashkit)
97 {
98 return hashkit->active_fn;
99 }
100
101
102 void hashkit_behavior_set_continuum_hash_fn(hashkit_st *hashkit, hashkit_fn *function)
103 {
104 hashkit->continuum_hash_fn= function;
105 }
106
107
108 hashkit_fn * hashkit_behavior_get_continuum_hash_fn(hashkit_st *hashkit)
109 {
110 return hashkit->continuum_hash_fn;
111 }
112
113
114 void hashkit_behavior_set_continuum_key_fn(hashkit_st *hashkit, hashkit_key_fn *function)
115 {
116 hashkit->continuum_key_fn= function;
117 }
118
119
120 hashkit_key_fn * hashkit_behavior_get_continuum_key_fn(hashkit_st *hashkit)
121 {
122 return hashkit->continuum_key_fn;
123 }
124
125
126 void hashkit_behavior_set_sort_fn(hashkit_st *hashkit, hashkit_sort_fn *function)
127 {
128 hashkit->sort_fn= function;
129 }
130
131
132 hashkit_sort_fn * hashkit_behavior_get_sort_fn(hashkit_st *hashkit)
133 {
134 return hashkit->sort_fn;
135 }
136
137
138 void hashkit_behavior_set_weight_fn(hashkit_st *hashkit, hashkit_weight_fn *function)
139 {
140 hashkit->weight_fn= function;
141 }
142
143
144 hashkit_weight_fn * hashkit_behavior_get_weight_fn(hashkit_st *hashkit)
145 {
146 return hashkit->weight_fn;
147 }