67bb9947b29d6bd98df17eb6cefb6cf8177cc508
[m6w6/libmemcached] / src / libhashkit / function.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libhashkit/common.h"
17
18 static hashkit_return_t _set_function(struct hashkit_st::hashkit_function_st *self,
19 hashkit_hash_algorithm_t hash_algorithm) {
20 if (self == NULL) {
21 return HASHKIT_INVALID_ARGUMENT;
22 }
23
24 switch (hash_algorithm) {
25 case HASHKIT_HASH_MD5:
26 self->function = hashkit_md5;
27 break;
28
29 case HASHKIT_HASH_CRC:
30 self->function = hashkit_crc32;
31 break;
32
33 case HASHKIT_HASH_FNV1_64:
34 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64)) {
35 self->function = hashkit_fnv1_64;
36 break;
37 }
38 return HASHKIT_INVALID_ARGUMENT;
39
40 case HASHKIT_HASH_FNV1A_64:
41 if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64)) {
42 self->function = hashkit_fnv1a_64;
43 break;
44 }
45 return HASHKIT_INVALID_ARGUMENT;
46
47 case HASHKIT_HASH_FNV1_32:
48 self->function = hashkit_fnv1_32;
49 break;
50
51 case HASHKIT_HASH_FNV1A_32:
52 self->function = hashkit_fnv1a_32;
53 break;
54
55 case HASHKIT_HASH_HSIEH:
56 if (libhashkit_has_algorithm(HASHKIT_HASH_HSIEH)) {
57 self->function = hashkit_hsieh;
58 break;
59 }
60 return HASHKIT_INVALID_ARGUMENT;
61
62 case HASHKIT_HASH_MURMUR3:
63 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR3)) {
64 self->function = hashkit_murmur3;
65 break;
66 }
67 return HASHKIT_INVALID_ARGUMENT;
68 case HASHKIT_HASH_MURMUR:
69 if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR)) {
70 self->function = hashkit_murmur;
71 break;
72 }
73 return HASHKIT_INVALID_ARGUMENT;
74
75 case HASHKIT_HASH_JENKINS:
76 self->function = hashkit_jenkins;
77 break;
78
79 case HASHKIT_HASH_CUSTOM:
80 return HASHKIT_INVALID_ARGUMENT;
81
82 case HASHKIT_HASH_DEFAULT:
83 self->function = hashkit_one_at_a_time;
84 break;
85
86 case HASHKIT_HASH_MAX:
87 self->function = hashkit_one_at_a_time;
88 return HASHKIT_INVALID_HASH;
89 }
90
91 self->context = NULL;
92
93 return HASHKIT_SUCCESS;
94 }
95
96 hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm) {
97 return _set_function(&self->base_hash, hash_algorithm);
98 }
99
100 hashkit_return_t hashkit_set_distribution_function(hashkit_st *self,
101 hashkit_hash_algorithm_t hash_algorithm) {
102 return _set_function(&self->distribution_hash, hash_algorithm);
103 }
104
105 static hashkit_return_t _set_custom_function(struct hashkit_st::hashkit_function_st *self,
106 hashkit_hash_fn function, void *context) {
107 if (self == NULL) {
108 return HASHKIT_INVALID_ARGUMENT;
109 }
110
111 if (function) {
112 self->function = function;
113 self->context = context;
114
115 return HASHKIT_SUCCESS;
116 }
117
118 return HASHKIT_FAILURE;
119 }
120
121 hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function,
122 void *context) {
123 if (self == NULL) {
124 return HASHKIT_INVALID_ARGUMENT;
125 }
126
127 return _set_custom_function(&self->base_hash, function, context);
128 }
129
130 hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self,
131 hashkit_hash_fn function, void *context) {
132 if (self == NULL) {
133 return HASHKIT_INVALID_ARGUMENT;
134 }
135
136 return _set_custom_function(&self->distribution_hash, function, context);
137 }
138
139 static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function) {
140 if (function == hashkit_one_at_a_time) {
141 return HASHKIT_HASH_DEFAULT;
142 } else if (function == hashkit_md5) {
143 return HASHKIT_HASH_MD5;
144 } else if (function == hashkit_crc32) {
145 return HASHKIT_HASH_CRC;
146 } else if (function == hashkit_fnv1_64) {
147 return HASHKIT_HASH_FNV1_64;
148 } else if (function == hashkit_fnv1a_64) {
149 return HASHKIT_HASH_FNV1A_64;
150 } else if (function == hashkit_fnv1_32) {
151 return HASHKIT_HASH_FNV1_32;
152 } else if (function == hashkit_fnv1a_32) {
153 return HASHKIT_HASH_FNV1A_32;
154 } else if (function == hashkit_hsieh) {
155 return HASHKIT_HASH_HSIEH;
156 } else if (function == hashkit_murmur) {
157 return HASHKIT_HASH_MURMUR;
158 } else if (function == hashkit_murmur3) {
159 return HASHKIT_HASH_MURMUR3;
160 } else if (function == hashkit_jenkins) {
161 return HASHKIT_HASH_JENKINS;
162 }
163
164 return HASHKIT_HASH_CUSTOM;
165 }
166
167 hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self) {
168 if (self == NULL) {
169 return HASHKIT_HASH_DEFAULT;
170 }
171
172 return get_function_type(self->base_hash.function);
173 }
174
175 hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self) {
176 if (self == NULL) {
177 return HASHKIT_HASH_DEFAULT;
178 }
179
180 return get_function_type(self->distribution_hash.function);
181 }