Disable hsieh algorithm by default
[awesomized/libmemcached] / libmemcached / common.h
1 /*
2 Common include file for libmemached
3 */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <ctype.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 #include <unistd.h>
19 #include <limits.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sys/un.h>
23 #include <netinet/tcp.h>
24
25 #ifdef TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 # else
32 # include <time.h>
33 # endif
34 #endif
35
36
37
38 #include <memcached.h>
39 #include "memcached_io.h"
40
41 #include "memcached/protocol_binary.h"
42 #include "libmemcached_config.h"
43
44 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
45
46 #define likely(x) if((x))
47 #define unlikely(x) if((x))
48
49 #else
50
51 #define likely(x) if(__builtin_expect(!!(x), 1))
52 #define unlikely(x) if(__builtin_expect((x), 0))
53 #endif
54
55 #include "libmemcached_probes.h"
56
57 #define MEMCACHED_BLOCK_SIZE 1024
58 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
59 #define SMALL_STRING_LEN 1024
60 #define HUGE_STRING_LEN 8196
61
62
63 typedef enum {
64 MEM_NO_BLOCK= (1 << 0),
65 MEM_TCP_NODELAY= (1 << 1),
66 MEM_REUSE_MEMORY= (1 << 2),
67 MEM_USE_MD5= (1 << 3),
68 /* 4 was once Ketama */
69 MEM_USE_CRC= (1 << 5),
70 MEM_USE_CACHE_LOOKUPS= (1 << 6),
71 MEM_SUPPORT_CAS= (1 << 7),
72 MEM_BUFFER_REQUESTS= (1 << 8),
73 MEM_USE_SORT_HOSTS= (1 << 9),
74 MEM_VERIFY_KEY= (1 << 10),
75 /* 11 used for weighted ketama */
76 MEM_KETAMA_WEIGHTED= (1 << 11),
77 MEM_BINARY_PROTOCOL= (1 << 12),
78 MEM_HASH_WITH_PREFIX_KEY= (1 << 13),
79 MEM_NOREPLY= (1 << 14),
80 MEM_USE_UDP= (1 << 15)
81 } memcached_flags;
82
83 /* Hashing algo */
84 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
85 uint32_t hash_crc32(const char *data,
86 size_t data_len);
87 #ifdef HAVE_HSIEH_HASH
88 uint32_t hsieh_hash(const char *key, size_t key_length);
89 #endif
90 uint32_t murmur_hash(const char *key, size_t key_length);
91 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
92
93 memcached_return memcached_connect(memcached_server_st *ptr);
94 memcached_return memcached_response(memcached_server_st *ptr,
95 char *buffer, size_t buffer_length,
96 memcached_result_st *result);
97 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length);
98 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death);
99
100 #define memcached_server_response_increment(A) (A)->cursor_active++
101 #define memcached_server_response_decrement(A) (A)->cursor_active--
102 #define memcached_server_response_reset(A) (A)->cursor_active=0
103
104 memcached_return memcached_do(memcached_server_st *ptr, const void *commmand,
105 size_t command_length, uint8_t with_flush);
106 memcached_return memcached_version(memcached_st *ptr);
107 memcached_return value_fetch(memcached_server_st *ptr,
108 char *buffer,
109 memcached_result_st *result);
110 void server_list_free(memcached_st *ptr, memcached_server_st *servers);
111
112 memcached_return memcached_key_test(char **keys, size_t *key_length,
113 unsigned int number_of_keys);
114
115 memcached_return run_distribution(memcached_st *ptr);
116
117 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
118 memcached_return memcached_server_remove(memcached_server_st *st_ptr);
119
120 #ifndef HAVE_HTONLL
121 extern uint64_t ntohll(uint64_t);
122 extern uint64_t htonll(uint64_t);
123 #endif
124
125 memcached_return memcached_purge(memcached_server_st *ptr);
126
127 static inline memcached_return memcached_validate_key_length(size_t key_length,
128 bool binary) {
129 unlikely (key_length == 0)
130 return MEMCACHED_BAD_KEY_PROVIDED;
131
132 if (binary)
133 {
134 unlikely (key_length > 0xffff)
135 return MEMCACHED_BAD_KEY_PROVIDED;
136 }
137 else
138 {
139 unlikely (key_length >= MEMCACHED_MAX_KEY)
140 return MEMCACHED_BAD_KEY_PROVIDED;
141 }
142
143 return MEMCACHED_SUCCESS;
144 }
145
146 #endif /* __COMMON_H__ */