nutcracker 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/README.md +22 -0
  2. data/Rakefile +55 -0
  3. data/bin/nutcracker +2 -0
  4. data/ext/nutcracker/ChangeLog +66 -0
  5. data/ext/nutcracker/LICENSE +177 -0
  6. data/ext/nutcracker/Makefile.am +7 -0
  7. data/ext/nutcracker/Makefile.in +726 -0
  8. data/ext/nutcracker/NOTICE +124 -0
  9. data/ext/nutcracker/README.md +240 -0
  10. data/ext/nutcracker/aclocal.m4 +956 -0
  11. data/ext/nutcracker/conf/nutcracker.leaf.yml +10 -0
  12. data/ext/nutcracker/conf/nutcracker.root.yml +8 -0
  13. data/ext/nutcracker/conf/nutcracker.yml +67 -0
  14. data/ext/nutcracker/config.h.in +316 -0
  15. data/ext/nutcracker/config/config.guess +1561 -0
  16. data/ext/nutcracker/config/config.sub +1686 -0
  17. data/ext/nutcracker/config/depcomp +630 -0
  18. data/ext/nutcracker/config/install-sh +520 -0
  19. data/ext/nutcracker/config/ltmain.sh +8413 -0
  20. data/ext/nutcracker/config/missing +376 -0
  21. data/ext/nutcracker/configure +18862 -0
  22. data/ext/nutcracker/configure.ac +155 -0
  23. data/ext/nutcracker/contrib/Makefile.am +3 -0
  24. data/ext/nutcracker/contrib/Makefile.in +560 -0
  25. data/ext/nutcracker/contrib/yaml-0.1.4.tar.gz +0 -0
  26. data/ext/nutcracker/contrib/yaml-0.1.4/LICENSE +19 -0
  27. data/ext/nutcracker/contrib/yaml-0.1.4/Makefile.am +20 -0
  28. data/ext/nutcracker/contrib/yaml-0.1.4/Makefile.in +736 -0
  29. data/ext/nutcracker/contrib/yaml-0.1.4/README +27 -0
  30. data/ext/nutcracker/contrib/yaml-0.1.4/aclocal.m4 +956 -0
  31. data/ext/nutcracker/contrib/yaml-0.1.4/config.h.in +80 -0
  32. data/ext/nutcracker/contrib/yaml-0.1.4/config/config.guess +1561 -0
  33. data/ext/nutcracker/contrib/yaml-0.1.4/config/config.sub +1686 -0
  34. data/ext/nutcracker/contrib/yaml-0.1.4/config/depcomp +630 -0
  35. data/ext/nutcracker/contrib/yaml-0.1.4/config/install-sh +520 -0
  36. data/ext/nutcracker/contrib/yaml-0.1.4/config/ltmain.sh +8406 -0
  37. data/ext/nutcracker/contrib/yaml-0.1.4/config/missing +376 -0
  38. data/ext/nutcracker/contrib/yaml-0.1.4/configure +13085 -0
  39. data/ext/nutcracker/contrib/yaml-0.1.4/configure.ac +75 -0
  40. data/ext/nutcracker/contrib/yaml-0.1.4/doc/doxygen.cfg +222 -0
  41. data/ext/nutcracker/contrib/yaml-0.1.4/include/yaml.h +1971 -0
  42. data/ext/nutcracker/contrib/yaml-0.1.4/m4/libtool.m4 +7357 -0
  43. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltoptions.m4 +368 -0
  44. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltsugar.m4 +123 -0
  45. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltversion.m4 +23 -0
  46. data/ext/nutcracker/contrib/yaml-0.1.4/m4/lt~obsolete.m4 +92 -0
  47. data/ext/nutcracker/contrib/yaml-0.1.4/src/Makefile.am +4 -0
  48. data/ext/nutcracker/contrib/yaml-0.1.4/src/Makefile.in +484 -0
  49. data/ext/nutcracker/contrib/yaml-0.1.4/src/api.c +1392 -0
  50. data/ext/nutcracker/contrib/yaml-0.1.4/src/dumper.c +394 -0
  51. data/ext/nutcracker/contrib/yaml-0.1.4/src/emitter.c +2329 -0
  52. data/ext/nutcracker/contrib/yaml-0.1.4/src/loader.c +432 -0
  53. data/ext/nutcracker/contrib/yaml-0.1.4/src/parser.c +1374 -0
  54. data/ext/nutcracker/contrib/yaml-0.1.4/src/reader.c +465 -0
  55. data/ext/nutcracker/contrib/yaml-0.1.4/src/scanner.c +3570 -0
  56. data/ext/nutcracker/contrib/yaml-0.1.4/src/writer.c +141 -0
  57. data/ext/nutcracker/contrib/yaml-0.1.4/src/yaml_private.h +640 -0
  58. data/ext/nutcracker/contrib/yaml-0.1.4/tests/Makefile.am +8 -0
  59. data/ext/nutcracker/contrib/yaml-0.1.4/tests/Makefile.in +675 -0
  60. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-deconstructor-alt.c +800 -0
  61. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-deconstructor.c +1130 -0
  62. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-reformatter-alt.c +217 -0
  63. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-reformatter.c +202 -0
  64. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-dumper.c +311 -0
  65. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-emitter.c +327 -0
  66. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-loader.c +63 -0
  67. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-parser.c +63 -0
  68. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-scanner.c +63 -0
  69. data/ext/nutcracker/contrib/yaml-0.1.4/tests/test-reader.c +354 -0
  70. data/ext/nutcracker/contrib/yaml-0.1.4/tests/test-version.c +29 -0
  71. data/ext/nutcracker/extconf.rb +5 -0
  72. data/ext/nutcracker/m4/libtool.m4 +7376 -0
  73. data/ext/nutcracker/m4/ltoptions.m4 +368 -0
  74. data/ext/nutcracker/m4/ltsugar.m4 +123 -0
  75. data/ext/nutcracker/m4/ltversion.m4 +23 -0
  76. data/ext/nutcracker/m4/lt~obsolete.m4 +92 -0
  77. data/ext/nutcracker/notes/c-styleguide.txt +425 -0
  78. data/ext/nutcracker/notes/debug.txt +96 -0
  79. data/ext/nutcracker/notes/memcache.txt +123 -0
  80. data/ext/nutcracker/notes/recommendation.md +118 -0
  81. data/ext/nutcracker/notes/redis.md +415 -0
  82. data/ext/nutcracker/notes/socket.txt +131 -0
  83. data/ext/nutcracker/scripts/multi_get.sh +26 -0
  84. data/ext/nutcracker/scripts/nutcracker.init +73 -0
  85. data/ext/nutcracker/scripts/nutcracker.spec +52 -0
  86. data/ext/nutcracker/scripts/pipelined_read.sh +23 -0
  87. data/ext/nutcracker/scripts/pipelined_write.sh +29 -0
  88. data/ext/nutcracker/scripts/populate_memcached.sh +24 -0
  89. data/ext/nutcracker/scripts/redis-check.py +23 -0
  90. data/ext/nutcracker/scripts/redis-check.sh +564 -0
  91. data/ext/nutcracker/src/Makefile.am +46 -0
  92. data/ext/nutcracker/src/Makefile.in +726 -0
  93. data/ext/nutcracker/src/hashkit/Makefile.am +22 -0
  94. data/ext/nutcracker/src/hashkit/Makefile.in +501 -0
  95. data/ext/nutcracker/src/hashkit/nc_crc32.c +105 -0
  96. data/ext/nutcracker/src/hashkit/nc_fnv.c +82 -0
  97. data/ext/nutcracker/src/hashkit/nc_hashkit.h +74 -0
  98. data/ext/nutcracker/src/hashkit/nc_hsieh.c +93 -0
  99. data/ext/nutcracker/src/hashkit/nc_jenkins.c +230 -0
  100. data/ext/nutcracker/src/hashkit/nc_ketama.c +240 -0
  101. data/ext/nutcracker/src/hashkit/nc_md5.c +379 -0
  102. data/ext/nutcracker/src/hashkit/nc_modula.c +144 -0
  103. data/ext/nutcracker/src/hashkit/nc_murmur.c +99 -0
  104. data/ext/nutcracker/src/hashkit/nc_one_at_a_time.c +51 -0
  105. data/ext/nutcracker/src/hashkit/nc_random.c +146 -0
  106. data/ext/nutcracker/src/nc.c +573 -0
  107. data/ext/nutcracker/src/nc_array.c +204 -0
  108. data/ext/nutcracker/src/nc_array.h +73 -0
  109. data/ext/nutcracker/src/nc_client.c +189 -0
  110. data/ext/nutcracker/src/nc_client.h +28 -0
  111. data/ext/nutcracker/src/nc_conf.c +1766 -0
  112. data/ext/nutcracker/src/nc_conf.h +134 -0
  113. data/ext/nutcracker/src/nc_connection.c +392 -0
  114. data/ext/nutcracker/src/nc_connection.h +99 -0
  115. data/ext/nutcracker/src/nc_core.c +334 -0
  116. data/ext/nutcracker/src/nc_core.h +131 -0
  117. data/ext/nutcracker/src/nc_event.c +214 -0
  118. data/ext/nutcracker/src/nc_event.h +39 -0
  119. data/ext/nutcracker/src/nc_log.c +254 -0
  120. data/ext/nutcracker/src/nc_log.h +120 -0
  121. data/ext/nutcracker/src/nc_mbuf.c +285 -0
  122. data/ext/nutcracker/src/nc_mbuf.h +67 -0
  123. data/ext/nutcracker/src/nc_message.c +828 -0
  124. data/ext/nutcracker/src/nc_message.h +253 -0
  125. data/ext/nutcracker/src/nc_proxy.c +359 -0
  126. data/ext/nutcracker/src/nc_proxy.h +34 -0
  127. data/ext/nutcracker/src/nc_queue.h +788 -0
  128. data/ext/nutcracker/src/nc_rbtree.c +348 -0
  129. data/ext/nutcracker/src/nc_rbtree.h +47 -0
  130. data/ext/nutcracker/src/nc_request.c +588 -0
  131. data/ext/nutcracker/src/nc_response.c +332 -0
  132. data/ext/nutcracker/src/nc_server.c +841 -0
  133. data/ext/nutcracker/src/nc_server.h +143 -0
  134. data/ext/nutcracker/src/nc_signal.c +131 -0
  135. data/ext/nutcracker/src/nc_signal.h +34 -0
  136. data/ext/nutcracker/src/nc_stats.c +1188 -0
  137. data/ext/nutcracker/src/nc_stats.h +206 -0
  138. data/ext/nutcracker/src/nc_string.c +109 -0
  139. data/ext/nutcracker/src/nc_string.h +112 -0
  140. data/ext/nutcracker/src/nc_util.c +619 -0
  141. data/ext/nutcracker/src/nc_util.h +214 -0
  142. data/ext/nutcracker/src/proto/Makefile.am +14 -0
  143. data/ext/nutcracker/src/proto/Makefile.in +482 -0
  144. data/ext/nutcracker/src/proto/nc_memcache.c +1306 -0
  145. data/ext/nutcracker/src/proto/nc_proto.h +155 -0
  146. data/ext/nutcracker/src/proto/nc_redis.c +2102 -0
  147. data/lib/nutcracker.rb +7 -0
  148. data/lib/nutcracker/version.rb +3 -0
  149. metadata +194 -0
@@ -0,0 +1,105 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /*
19
+ * The crc32 functions and data was originally written by Spencer
20
+ * Garrett <srg@quick.com> and was gleaned from the PostgreSQL source
21
+ * tree via the files contrib/ltree/crc32.[ch] and from FreeBSD at
22
+ * src/usr.bin/cksum/crc32.c.
23
+ */
24
+
25
+ #include <nc_core.h>
26
+
27
+ static const uint32_t crc32tab[256] = {
28
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
29
+ 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
30
+ 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
31
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
32
+ 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
33
+ 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
34
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
35
+ 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
36
+ 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
37
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
38
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
39
+ 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
40
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
41
+ 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
42
+ 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
43
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
44
+ 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
45
+ 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
46
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
47
+ 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
48
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
49
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
50
+ 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
51
+ 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
52
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
53
+ 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
54
+ 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
55
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
56
+ 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
57
+ 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
58
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
59
+ 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
60
+ 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
61
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
62
+ 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
63
+ 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
64
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
65
+ 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
66
+ 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
67
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
68
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
69
+ 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
70
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
71
+ 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
72
+ 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
73
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
74
+ 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
75
+ 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
76
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
77
+ 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
78
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
79
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
80
+ 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
81
+ 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
82
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
83
+ 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
84
+ 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
85
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
86
+ 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
87
+ 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
88
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
89
+ 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
90
+ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
91
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
92
+ };
93
+
94
+ uint32_t
95
+ hash_crc32(const char *key, size_t key_length)
96
+ {
97
+ uint64_t x;
98
+ uint32_t crc = UINT32_MAX;
99
+
100
+ for (x= 0; x < key_length; x++) {
101
+ crc = (crc >> 8) ^ crc32tab[(crc ^ (uint64_t)key[x]) & 0xff];
102
+ }
103
+
104
+ return ((~crc) >> 16) & 0x7fff;
105
+ }
@@ -0,0 +1,82 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include <nc_core.h>
19
+
20
+ static uint64_t FNV_64_INIT = UINT64_C(0xcbf29ce484222325);
21
+ static uint64_t FNV_64_PRIME = UINT64_C(0x100000001b3);
22
+ static uint32_t FNV_32_INIT = 2166136261UL;
23
+ static uint32_t FNV_32_PRIME = 16777619;
24
+
25
+ uint32_t
26
+ hash_fnv1_64(const char *key, size_t key_length)
27
+ {
28
+ uint64_t hash = FNV_64_INIT;
29
+ size_t x;
30
+
31
+ for (x = 0; x < key_length; x++) {
32
+ hash *= FNV_64_PRIME;
33
+ hash ^= (uint64_t)key[x];
34
+ }
35
+
36
+ return (uint32_t)hash;
37
+ }
38
+
39
+ uint32_t
40
+ hash_fnv1a_64(const char *key, size_t key_length)
41
+ {
42
+ uint32_t hash = (uint32_t) FNV_64_INIT;
43
+ size_t x;
44
+
45
+ for (x = 0; x < key_length; x++) {
46
+ uint32_t val = (uint32_t)key[x];
47
+ hash ^= val;
48
+ hash *= (uint32_t) FNV_64_PRIME;
49
+ }
50
+
51
+ return hash;
52
+ }
53
+
54
+ uint32_t
55
+ hash_fnv1_32(const char *key, size_t key_length)
56
+ {
57
+ uint32_t hash = FNV_32_INIT;
58
+ size_t x;
59
+
60
+ for (x = 0; x < key_length; x++) {
61
+ uint32_t val = (uint32_t)key[x];
62
+ hash *= FNV_32_PRIME;
63
+ hash ^= val;
64
+ }
65
+
66
+ return hash;
67
+ }
68
+
69
+ uint32_t
70
+ hash_fnv1a_32(const char *key, size_t key_length)
71
+ {
72
+ uint32_t hash = FNV_32_INIT;
73
+ size_t x;
74
+
75
+ for (x= 0; x < key_length; x++) {
76
+ uint32_t val = (uint32_t)key[x];
77
+ hash ^= val;
78
+ hash *= FNV_32_PRIME;
79
+ }
80
+
81
+ return hash;
82
+ }
@@ -0,0 +1,74 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #ifndef _NC_HASHKIT_H_
19
+ #define _NC_HASHKIT_H_
20
+
21
+ #include <nc_core.h>
22
+ #include <nc_server.h>
23
+
24
+ #define HASH_CODEC(ACTION) \
25
+ ACTION( HASH_ONE_AT_A_TIME, one_at_a_time ) \
26
+ ACTION( HASH_MD5, md5 ) \
27
+ ACTION( HASH_CRC32, crc32 ) \
28
+ ACTION( HASH_FNV1_64, fnv1_64 ) \
29
+ ACTION( HASH_FNV1A_64, fnv1a_64 ) \
30
+ ACTION( HASH_FNV1_32, fnv1_32 ) \
31
+ ACTION( HASH_FNV1A_32, fnv1a_32 ) \
32
+ ACTION( HASH_HSIEH, hsieh ) \
33
+ ACTION( HASH_MURMUR, murmur ) \
34
+ ACTION( HASH_JENKINS, jenkins ) \
35
+
36
+ #define DIST_CODEC(ACTION) \
37
+ ACTION( DIST_KETAMA, ketama ) \
38
+ ACTION( DIST_MODULA, modula ) \
39
+ ACTION( DIST_RANDOM, random ) \
40
+
41
+ #define DEFINE_ACTION(_hash, _name) _hash,
42
+ typedef enum hash_type {
43
+ HASH_CODEC( DEFINE_ACTION )
44
+ HASH_SENTINEL
45
+ } hash_type_t;
46
+ #undef DEFINE_ACTION
47
+
48
+ #define DEFINE_ACTION(_dist, _name) _dist,
49
+ typedef enum dist_type {
50
+ DIST_CODEC( DEFINE_ACTION )
51
+ DIST_SENTINEL
52
+ } dist_type_t;
53
+ #undef DEFINE_ACTION
54
+
55
+ uint32_t hash_one_at_a_time(const char *key, size_t key_length);
56
+ void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
57
+ uint32_t hash_md5(const char *key, size_t key_length);
58
+ uint32_t hash_crc32(const char *key, size_t key_length);
59
+ uint32_t hash_fnv1_64(const char *key, size_t key_length);
60
+ uint32_t hash_fnv1a_64(const char *key, size_t key_length);
61
+ uint32_t hash_fnv1_32(const char *key, size_t key_length);
62
+ uint32_t hash_fnv1a_32(const char *key, size_t key_length);
63
+ uint32_t hash_hsieh(const char *key, size_t key_length);
64
+ uint32_t hash_jenkins(const char *key, size_t length);
65
+ uint32_t hash_murmur(const char *key, size_t length);
66
+
67
+ rstatus_t ketama_update(struct server_pool *pool);
68
+ uint32_t ketama_dispatch(struct continuum *continuum, uint32_t ncontinuum, uint32_t hash);
69
+ rstatus_t modula_update(struct server_pool *pool);
70
+ uint32_t modula_dispatch(struct continuum *continuum, uint32_t ncontinuum, uint32_t hash);
71
+ rstatus_t random_update(struct server_pool *pool);
72
+ uint32_t random_dispatch(struct continuum *continuum, uint32_t ncontinuum, uint32_t hash);
73
+
74
+ #endif
@@ -0,0 +1,93 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /*
19
+ * By Paul Hsieh (C) 2004, 2005. Covered under the Paul Hsieh
20
+ * derivative license.
21
+ * See: http://www.azillionmonkeys.com/qed/weblicense.html for license
22
+ * details.
23
+ * http://www.azillionmonkeys.com/qed/hash.html
24
+ */
25
+
26
+ #include <nc_core.h>
27
+
28
+ #undef get16bits
29
+ #if (defined(__GNUC__) && defined(__i386__))
30
+ #define get16bits(d) (*((const uint16_t *) (d)))
31
+ #endif
32
+
33
+ #if !defined (get16bits)
34
+ #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
35
+ +(uint32_t)(((const uint8_t *)(d))[0]) )
36
+ #endif
37
+
38
+ uint32_t
39
+ hash_hsieh(const char *key, size_t key_length)
40
+ {
41
+ uint32_t hash = 0, tmp;
42
+ int rem;
43
+
44
+ if (key_length <= 0 || key == NULL) {
45
+ return 0;
46
+ }
47
+
48
+ rem = key_length & 3;
49
+ key_length >>= 2;
50
+
51
+ /* Main loop */
52
+ for (;key_length > 0; key_length--) {
53
+ hash += get16bits (key);
54
+ tmp = (get16bits (key+2) << 11) ^ hash;
55
+ hash = (hash << 16) ^ tmp;
56
+ key += 2*sizeof (uint16_t);
57
+ hash += hash >> 11;
58
+ }
59
+
60
+ /* Handle end cases */
61
+ switch (rem) {
62
+ case 3:
63
+ hash += get16bits (key);
64
+ hash ^= hash << 16;
65
+ hash ^= (uint32_t)key[sizeof (uint16_t)] << 18;
66
+ hash += hash >> 11;
67
+ break;
68
+
69
+ case 2:
70
+ hash += get16bits (key);
71
+ hash ^= hash << 11;
72
+ hash += hash >> 17;
73
+ break;
74
+
75
+ case 1:
76
+ hash += (unsigned char)(*key);
77
+ hash ^= hash << 10;
78
+ hash += hash >> 1;
79
+
80
+ default:
81
+ break;
82
+ }
83
+
84
+ /* Force "avalanching" of final 127 bits */
85
+ hash ^= hash << 3;
86
+ hash += hash >> 5;
87
+ hash ^= hash << 4;
88
+ hash += hash >> 17;
89
+ hash ^= hash << 25;
90
+ hash += hash >> 6;
91
+
92
+ return hash;
93
+ }
@@ -0,0 +1,230 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /*
19
+ * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
20
+ * code any way you wish, private, educational, or commercial. It's free.
21
+ * Use for hash table lookup, or anything where one collision in 2^^32 is
22
+ * acceptable. Do NOT use for cryptographic purposes.
23
+ * http://burtleburtle.net/bob/hash/index.html
24
+ *
25
+ * Modified by Brian Pontz for libmemcached
26
+ * TODO:
27
+ * Add big endian support
28
+ */
29
+
30
+ #include <nc_core.h>
31
+
32
+ #define hashsize(n) ((uint32_t)1<<(n))
33
+ #define hashmask(n) (hashsize(n)-1)
34
+ #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
35
+
36
+ #define mix(a,b,c) \
37
+ { \
38
+ a -= c; a ^= rot(c, 4); c += b; \
39
+ b -= a; b ^= rot(a, 6); a += c; \
40
+ c -= b; c ^= rot(b, 8); b += a; \
41
+ a -= c; a ^= rot(c,16); c += b; \
42
+ b -= a; b ^= rot(a,19); a += c; \
43
+ c -= b; c ^= rot(b, 4); b += a; \
44
+ }
45
+
46
+ #define final(a,b,c) \
47
+ { \
48
+ c ^= b; c -= rot(b,14); \
49
+ a ^= c; a -= rot(c,11); \
50
+ b ^= a; b -= rot(a,25); \
51
+ c ^= b; c -= rot(b,16); \
52
+ a ^= c; a -= rot(c,4); \
53
+ b ^= a; b -= rot(a,14); \
54
+ c ^= b; c -= rot(b,24); \
55
+ }
56
+
57
+ #define JENKINS_INITVAL 13
58
+
59
+ /*
60
+ * jenkins_hash() -- hash a variable-length key into a 32-bit value
61
+ * k : the key (the unaligned variable-length array of bytes)
62
+ * length : the length of the key, counting by bytes
63
+ * initval : can be any 4-byte value
64
+ * Returns a 32-bit value. Every bit of the key affects every bit of
65
+ * the return value. Two keys differing by one or two bits will have
66
+ * totally different hash values.
67
+
68
+ * The best hash table sizes are powers of 2. There is no need to do
69
+ * mod a prime (mod is sooo slow!). If you need less than 32 bits,
70
+ * use a bitmask. For example, if you need only 10 bits, do
71
+ * h = (h & hashmask(10));
72
+ * In which case, the hash table should have hashsize(10) elements.
73
+ */
74
+
75
+ uint32_t
76
+ hash_jenkins(const char *key, size_t length)
77
+ {
78
+ uint32_t a,b,c; /* internal state */
79
+ union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
80
+
81
+ /* Set up the internal state */
82
+ a = b = c = 0xdeadbeef + ((uint32_t)length) + JENKINS_INITVAL;
83
+
84
+ u.ptr = key;
85
+ #ifndef WORDS_BIGENDIAN
86
+ if ((u.i & 0x3) == 0)
87
+ {
88
+ const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
89
+
90
+ /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
91
+ while (length > 12)
92
+ {
93
+ a += k[0];
94
+ b += k[1];
95
+ c += k[2];
96
+ mix(a,b,c);
97
+ length -= 12;
98
+ k += 3;
99
+ }
100
+
101
+ /*----------------------------- handle the last (probably partial) block */
102
+ /*
103
+ * "k[2]&0xffffff" actually reads beyond the end of the string, but
104
+ * then masks off the part it's not allowed to read. Because the
105
+ * string is aligned, the masked-off tail is in the same word as the
106
+ * rest of the string. Every machine with memory protection I've seen
107
+ * does it on word boundaries, so is OK with this. But VALGRIND will
108
+ * still catch it and complain. The masking trick does make the hash
109
+ * noticably faster for short strings (like English words).
110
+ */
111
+ switch(length)
112
+ {
113
+ case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
114
+ case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
115
+ case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
116
+ case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
117
+ case 8 : b+=k[1]; a+=k[0]; break;
118
+ case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
119
+ case 6 : b+=k[1]&0xffff; a+=k[0]; break;
120
+ case 5 : b+=k[1]&0xff; a+=k[0]; break;
121
+ case 4 : a+=k[0]; break;
122
+ case 3 : a+=k[0]&0xffffff; break;
123
+ case 2 : a+=k[0]&0xffff; break;
124
+ case 1 : a+=k[0]&0xff; break;
125
+ case 0 : return c; /* zero length strings require no mixing */
126
+ default: return c;
127
+ }
128
+
129
+ }
130
+ else if ((u.i & 0x1) == 0)
131
+ {
132
+ const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
133
+ const uint8_t *k8;
134
+
135
+ /*--------------- all but last block: aligned reads and different mixing */
136
+ while (length > 12)
137
+ {
138
+ a += k[0] + (((uint32_t)k[1])<<16);
139
+ b += k[2] + (((uint32_t)k[3])<<16);
140
+ c += k[4] + (((uint32_t)k[5])<<16);
141
+ mix(a,b,c);
142
+ length -= 12;
143
+ k += 6;
144
+ }
145
+
146
+ /*----------------------------- handle the last (probably partial) block */
147
+ k8 = (const uint8_t *)k;
148
+ switch(length)
149
+ {
150
+ case 12: c+=k[4]+(((uint32_t)k[5])<<16);
151
+ b+=k[2]+(((uint32_t)k[3])<<16);
152
+ a+=k[0]+(((uint32_t)k[1])<<16);
153
+ break;
154
+ case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
155
+ case 10: c+=k[4];
156
+ b+=k[2]+(((uint32_t)k[3])<<16);
157
+ a+=k[0]+(((uint32_t)k[1])<<16);
158
+ break;
159
+ case 9 : c+=k8[8]; /* fall through */
160
+ case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
161
+ a+=k[0]+(((uint32_t)k[1])<<16);
162
+ break;
163
+ case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
164
+ case 6 : b+=k[2];
165
+ a+=k[0]+(((uint32_t)k[1])<<16);
166
+ break;
167
+ case 5 : b+=k8[4]; /* fall through */
168
+ case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
169
+ break;
170
+ case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
171
+ case 2 : a+=k[0];
172
+ break;
173
+ case 1 : a+=k8[0];
174
+ break;
175
+ case 0 : return c; /* zero length requires no mixing */
176
+ default: return c;
177
+ }
178
+
179
+ }
180
+ else
181
+ { /* need to read the key one byte at a time */
182
+ #endif /* little endian */
183
+ const uint8_t *k = (const uint8_t *)key;
184
+
185
+ /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
186
+ while (length > 12)
187
+ {
188
+ a += k[0];
189
+ a += ((uint32_t)k[1])<<8;
190
+ a += ((uint32_t)k[2])<<16;
191
+ a += ((uint32_t)k[3])<<24;
192
+ b += k[4];
193
+ b += ((uint32_t)k[5])<<8;
194
+ b += ((uint32_t)k[6])<<16;
195
+ b += ((uint32_t)k[7])<<24;
196
+ c += k[8];
197
+ c += ((uint32_t)k[9])<<8;
198
+ c += ((uint32_t)k[10])<<16;
199
+ c += ((uint32_t)k[11])<<24;
200
+ mix(a,b,c);
201
+ length -= 12;
202
+ k += 12;
203
+ }
204
+
205
+ /*-------------------------------- last block: affect all 32 bits of (c) */
206
+ switch(length) /* all the case statements fall through */
207
+ {
208
+ case 12: c+=((uint32_t)k[11])<<24;
209
+ case 11: c+=((uint32_t)k[10])<<16;
210
+ case 10: c+=((uint32_t)k[9])<<8;
211
+ case 9 : c+=k[8];
212
+ case 8 : b+=((uint32_t)k[7])<<24;
213
+ case 7 : b+=((uint32_t)k[6])<<16;
214
+ case 6 : b+=((uint32_t)k[5])<<8;
215
+ case 5 : b+=k[4];
216
+ case 4 : a+=((uint32_t)k[3])<<24;
217
+ case 3 : a+=((uint32_t)k[2])<<16;
218
+ case 2 : a+=((uint32_t)k[1])<<8;
219
+ case 1 : a+=k[0];
220
+ break;
221
+ case 0 : return c;
222
+ default : return c;
223
+ }
224
+ #ifndef WORDS_BIGENDIAN
225
+ }
226
+ #endif
227
+
228
+ final(a,b,c);
229
+ return c;
230
+ }