Skip to content
Browse files

Bundle libvbucket

Change-Id: I95ad7f5e79a5ebbdca5ca8203175759a9a367856
Reviewed-on: http://review.couchbase.org/19353
Tested-by: Trond Norbye <trond.norbye@gmail.com>
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
1 parent 1059017 commit 00f91bfd36ca642d2bf58603197fff19840836c7 @trondn trondn committed Aug 8, 2012
View
32 Makefile.am
@@ -26,12 +26,11 @@
ACLOCAL_AMFLAGS = -I m4 --force
lib_LTLIBRARIES = libcouchbase.la
-noinst_LTLIBRARIES = libhttpparser.la
+noinst_LTLIBRARIES = libhttpparser.la libvbucket.la libketama.la libcrc32.la
if BUILD_TOOLS
bin_PROGRAMS = tools/cbc
endif
-
pkginclude_HEADERS = \
include/libcouchbase/behavior.h \
include/libcouchbase/callbacks.h \
@@ -49,6 +48,22 @@ libhttpparser_la_SOURCES = \
contrib/http_parser/http_parser.c \
contrib/http_parser/http_parser.h
+libvbucket_la_SOURCES = \
+ include/libvbucket/vbucket.h \
+ include/libvbucket/visibility.h \
+ contrib/libvbucket/hash.h \
+ contrib/libvbucket/cJSON.c \
+ contrib/libvbucket/cJSON.h \
+ contrib/libvbucket/vbucket.c
+libvbucket_la_LIBADD = libketama.la libcrc32.la
+libvbucket_la_DEPENDENCIES = libketama.la libcrc32.la
+
+libketama_la_SOURCES = contrib/libvbucket/ketama.c
+libketama_la_CFLAGS = $(AM_CFLAGS) ${NO_WERROR}
+
+libcrc32_la_SOURCES = contrib/libvbucket/crc32.c
+libcrc32_la_CFLAGS = $(AM_CFLAGS) ${NO_WERROR}
+
libcouchbase_la_SOURCES = \
include/ep-engine/command_ids.h \
include/memcached/protocol_binary.h \
@@ -98,8 +113,8 @@ endif
libcouchbase_la_LDFLAGS= $(AM_LDFLAGS) -version-info $(LIBCOUCHBASE_API_CURRENT):$(LIBCOUCHBASE_API_REVISION):$(LIBCOUCHBASE_API_AGE) -no-undefined $(AM_PROFILE_SOLDFLAGS)
libcouchbase_la_CPPFLAGS=$(AM_CPPFLAGS) $(CPPFLAGS) -DLIBCOUCHBASE_INTERNAL=1
-libcouchbase_la_DEPENDENCIES=libhttpparser.la
-libcouchbase_la_LIBADD=libhttpparser.la -lvbucket $(LIBSASL)
+libcouchbase_la_DEPENDENCIES=libhttpparser.la libvbucket.la
+libcouchbase_la_LIBADD=libhttpparser.la libvbucket.la $(LIBSASL)
if LIBCOUCHBASE_ENABLE_TRACE
libcouchbase_la_SOURCES += \
@@ -206,13 +221,15 @@ endif
tests_getopt_test_SOURCES = tests/getopt-test.cc
tests_config_test_SOURCES = tests/test.h tests/config-test.c
-tests_config_test_LDADD = libcouchbase.la libmockserver.la
-tests_config_test_LDFLAGS = $(AM_LDFLAGS) -lvbucket
+tests_config_test_DEPENDENCIES = libcouchbase.la libmockserver.la libvbucket.la
+tests_config_test_LDADD = $(tests_config_test_DEPENDENCIES)
+tests_config_test_LDFLAGS = $(AM_LDFLAGS)
tests_smoke_test_SOURCES = tests/test.h tests/smoke-test.c
tests_smoke_test_LDADD = libcouchbase.la libmockserver.la
-tools_cbc_LDADD = libcouchbase.la
+tools_cbc_LDADD = libcouchbase.la libvbucket.la
+tools_cbc_DEPENDENCIES = $(tools_cbc_LDADD)
if HAVE_LIBYAJL2
tools_cbc_LDADD += -lyajl
@@ -246,6 +263,7 @@ EXTRA_DIST = \
NMakefile \
include/libcouchbase/configuration.h.in \
src/iofactory_win32.c \
+ contrib/libvbucket/rfc1321 \
tests/start_mock.sh \
win32
View
5 NMakefile
@@ -36,7 +36,10 @@ libcouchbase_SOURCES = src\arithmetic.c src\base64.c src\behavior.c \
src\timeout.c src\timings.c src\touch.c src\utilities.c \
src\wait.c src\gethrtime.c plugins\io\win32\plugin-win32.c src\isasl.c \
src\compat.c contrib\http_parser\http_parser.c src\http.c \
- src\observe.c src\timer.c src\verbosity.c
+ src\observe.c src\timer.c src\verbosity.c \
+ contrib\libvbucket\crc32.c contrib\libvbucket\cJSON.c \
+ contrib\libvbucket\vbucket.c contrib\libvbucket\ketama.c \
+ contrib\libvbucket\rfc1321\md5c.c
# Unfortunately nmake is a bit limited in its substitute functions.
# Work around that by using dobj to represent debug object files ;)
View
28 configure.ac
@@ -60,6 +60,9 @@ AC_SUBST(LIBCOUCHBASE_VERSION_HEX)
AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(gethostbyname, nsl)
AC_SEARCH_LIBS(dlopen, dl)
+AC_SEARCH_LIBS(floor, m)
+AC_SEARCH_LIBS(pow, m)
+AC_SEARCH_LIBS(fabs, m)
AC_PATH_PROG(JAVA, java, no)
AC_PATH_PROG(WGET, wget, no)
@@ -87,34 +90,11 @@ AC_ARG_WITH([couchbasemock],
AC_CHECK_HEADERS_ONCE([mach/mach_time.h sys/socket.h sys/time.h
netinet/in.h inttypes.h netdb.h unistd.h
- ws2tcpip.h winsock2.h libvbucket/vbucket.h
- event.h stdint.h sys/types.h])
+ ws2tcpip.h winsock2.h event.h stdint.h sys/types.h])
AS_IF([test "x$ac_cv_header_stdint_h" != "xyes"],
[AC_MSG_ERROR(Failed to locate stdint.h)])
-AS_IF([test "x$ac_cv_header_libvbucket_vbucket_h" != "xyes"],
- [AC_MSG_ERROR(Failed to locate libvbucket/vbucket.h)])
-
-dnl Check that we're able to find a usable libvbucket
-AC_CACHE_CHECK([for libvbucket >= 1.8.0.2], [ac_cv_have_libvbucket],
- [ saved_libs="$LIBS"
- LIBS=-lvbucket
- AC_TRY_LINK([
- #include <libvbucket/vbucket.h>
- ],[
- VBUCKET_CONFIG_HANDLE config = vbucket_config_create();
- ],[
- ac_cv_have_libvbucket="yes"
- ], [
- ac_cv_have_libvbucket="no"
- ])
- LIBS="$saved_libs"
- ])
-
-AS_IF([test "x${ac_cv_have_libvbucket}" != "xyes" -o "x$ac_cv_header_libvbucket_vbucket_h" != "xyes"],
- [AC_MSG_ERROR(Failed to locate libvbucket >= 1.8.0.2)])
-
dnl Check that we can use winsock2 plugin
AC_CACHE_CHECK([for winsock2], [ac_cv_have_winsock2],
[ saved_libs="$LIBS"
View
490 contrib/libvbucket/cJSON.c
@@ -0,0 +1,490 @@
+/*
+ Copyright (c) 2009 Dave Gamble
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+/* cJSON */
+/* JSON parser in C. */
+
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <float.h>
+#include <limits.h>
+#include <ctype.h>
+#include "cJSON.h"
+
+static int cJSON_strcasecmp(const char *s1,const char *s2)
+{
+ if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
+ for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
+ return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
+}
+
+static void *(*cJSON_malloc)(size_t sz) = malloc;
+static void (*cJSON_free)(void *ptr) = free;
+
+static char* cJSON_strdup(const char* str)
+{
+ size_t len;
+ char* copy;
+
+ len = strlen(str) + 1;
+ if (!(copy = (char*)cJSON_malloc(len))) return 0;
+ memcpy(copy,str,len);
+ return copy;
+}
+
+void cJSON_InitHooks(cJSON_Hooks* hooks)
+{
+ if (!hooks) { /* Reset hooks */
+ cJSON_malloc = malloc;
+ cJSON_free = free;
+ return;
+ }
+
+ cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
+ cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
+}
+
+/* Internal constructor. */
+static cJSON *cJSON_New_Item(void)
+{
+ cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
+ if (node) memset(node,0,sizeof(cJSON));
+ return node;
+}
+
+/* Delete a cJSON structure. */
+void cJSON_Delete(cJSON *c)
+{
+ cJSON *next;
+ while (c)
+ {
+ next=c->next;
+ if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
+ if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
+ if (c->string) cJSON_free(c->string);
+ cJSON_free(c);
+ c=next;
+ }
+}
+
+/* Parse the input text to generate a number, and populate the result into item. */
+static const char *parse_number(cJSON *item,const char *num)
+{
+ double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
+
+ /* Could use sscanf for this? */
+ if (*num=='-') sign=-1,num++; /* Has sign? */
+ if (*num=='0') num++; /* is zero */
+ if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
+ if (*num=='.') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
+ if (*num=='e' || *num=='E') /* Exponent? */
+ { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
+ while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
+ }
+
+ n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
+
+ item->valuedouble=n;
+ item->valueint=(int)n;
+ item->type=cJSON_Number;
+ return num;
+}
+
+/* Render the number nicely from the given item into a string. */
+static char *print_number(cJSON *item)
+{
+ char *str;
+ double d=item->valuedouble;
+ if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
+ {
+ str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
+ sprintf(str,"%d",item->valueint);
+ }
+ else
+ {
+ str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
+ if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d);
+ else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
+ else sprintf(str,"%f",d);
+ }
+ return str;
+}
+
+/* Parse the input text into an unescaped cstring, and populate item. */
+static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
+static const char *parse_string(cJSON *item,const char *str)
+{
+ const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc;
+ if (*str!='\"') return 0; /* not a string! */
+
+ while (*ptr!='\"' && (unsigned char)*ptr>31 && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
+
+ out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
+ if (!out) return 0;
+
+ ptr=str+1;ptr2=out;
+ while (*ptr!='\"' && (unsigned char)*ptr>31)
+ {
+ if (*ptr!='\\') *ptr2++=*ptr++;
+ else
+ {
+ ptr++;
+ switch (*ptr)
+ {
+ case 'b': *ptr2++='\b'; break;
+ case 'f': *ptr2++='\f'; break;
+ case 'n': *ptr2++='\n'; break;
+ case 'r': *ptr2++='\r'; break;
+ case 't': *ptr2++='\t'; break;
+ case 'u': /* transcode utf16 to utf8. DOES NOT SUPPORT SURROGATE PAIRS CORRECTLY. */
+ sscanf(ptr+1,"%4x",&uc); /* get the unicode char. */
+ len=3;if (uc<0x80) len=1;else if (uc<0x800) len=2;ptr2+=len;
+
+ switch (len) {
+ case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
+ case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
+ case 1: *--ptr2 =(uc | firstByteMark[len]);
+ }
+ ptr2+=len;ptr+=4;
+ break;
+ default: *ptr2++=*ptr; break;
+ }
+ ptr++;
+ }
+ }
+ *ptr2=0;
+ if (*ptr=='\"') ptr++;
+ item->valuestring=out;
+ item->type=cJSON_String;
+ return ptr;
+}
+
+/* Render the cstring provided to an escaped version that can be printed. */
+static char *print_string_ptr(const char *str)
+{
+ const char *ptr;char *ptr2,*out;int len=0;
+
+ if (!str) return cJSON_strdup("");
+ ptr=str;while (*ptr && ++len) {if ((unsigned char)*ptr<32 || *ptr=='\"' || *ptr=='\\') len++;ptr++;}
+
+ out=(char*)cJSON_malloc(len+3);
+ ptr2=out;ptr=str;
+ *ptr2++='\"';
+ while (*ptr)
+ {
+ if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
+ else
+ {
+ *ptr2++='\\';
+ switch (*ptr++)
+ {
+ case '\\': *ptr2++='\\'; break;
+ case '\"': *ptr2++='\"'; break;
+ case '\b': *ptr2++='b'; break;
+ case '\f': *ptr2++='f'; break;
+ case '\n': *ptr2++='n'; break;
+ case '\r': *ptr2++='r'; break;
+ case '\t': *ptr2++='t'; break;
+ default: ptr2--; break; /* eviscerate with prejudice. */
+ }
+ }
+ }
+ *ptr2++='\"';*ptr2++=0;
+ return out;
+}
+/* Invote print_string_ptr (which is useful) on an item. */
+static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
+
+/* Predeclare these prototypes. */
+static const char *parse_value(cJSON *item,const char *value);
+static char *print_value(cJSON *item,int depth,int fmt);
+static const char *parse_array(cJSON *item,const char *value);
+static char *print_array(cJSON *item,int depth,int fmt);
+static const char *parse_object(cJSON *item,const char *value);
+static char *print_object(cJSON *item,int depth,int fmt);
+
+/* Utility to jump whitespace and cr/lf */
+static const char *skip(const char *in) {while (in && (unsigned char)*in<=32) in++; return in;}
+
+/* Parse an object - create a new root, and populate. */
+cJSON *cJSON_Parse(const char *value)
+{
+ cJSON *c=cJSON_New_Item();
+ if (!c) return 0; /* memory fail */
+
+ if (!parse_value(c,skip(value))) {cJSON_Delete(c);return 0;}
+ return c;
+}
+
+/* Render a cJSON item/entity/structure to text. */
+char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
+char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
+
+/* Parser core - when encountering text, process appropriately. */
+static const char *parse_value(cJSON *item,const char *value)
+{
+ if (!value) return 0; /* Fail on null. */
+ if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
+ if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
+ if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
+ if (*value=='\"') { return parse_string(item,value); }
+ if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
+ if (*value=='[') { return parse_array(item,value); }
+ if (*value=='{') { return parse_object(item,value); }
+
+ return 0; /* failure. */
+}
+
+/* Render a value to text. */
+static char *print_value(cJSON *item,int depth,int fmt)
+{
+ char *out=0;
+ if (!item) return 0;
+ switch ((item->type)&255)
+ {
+ case cJSON_NULL: out=cJSON_strdup("null"); break;
+ case cJSON_False: out=cJSON_strdup("false");break;
+ case cJSON_True: out=cJSON_strdup("true"); break;
+ case cJSON_Number: out=print_number(item);break;
+ case cJSON_String: out=print_string(item);break;
+ case cJSON_Array: out=print_array(item,depth,fmt);break;
+ case cJSON_Object: out=print_object(item,depth,fmt);break;
+ }
+ return out;
+}
+
+/* Build an array from input text. */
+static const char *parse_array(cJSON *item,const char *value)
+{
+ cJSON *child;
+ if (*value!='[') return 0; /* not an array! */
+
+ item->type=cJSON_Array;
+ value=skip(value+1);
+ if (*value==']') return value+1; /* empty array. */
+
+ item->child=child=cJSON_New_Item();
+ if (!item->child) return 0; /* memory fail */
+ value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
+ if (!value) return 0;
+
+ while (*value==',')
+ {
+ cJSON *new_item;
+ if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
+ child->next=new_item;new_item->prev=child;child=new_item;
+ value=skip(parse_value(child,skip(value+1)));
+ if (!value) return 0; /* memory fail */
+ }
+
+ if (*value==']') return value+1; /* end of array */
+ return 0; /* malformed. */
+}
+
+/* Render an array to text */
+static char *print_array(cJSON *item,int depth,int fmt)
+{
+ char **entries;
+ char *out=0,*ptr,*ret;int len=5;
+ cJSON *child=item->child;
+ int numentries=0,i=0,fail=0;
+
+ /* How many entries in the array? */
+ while (child) numentries++,child=child->next;
+ /* Allocate an array to hold the values for each */
+ entries=(char**)cJSON_malloc(numentries*sizeof(char*));
+ if (!entries) return 0;
+ memset(entries,0,numentries*sizeof(char*));
+ /* Retrieve all the results: */
+ child=item->child;
+ while (child && !fail)
+ {
+ ret=print_value(child,depth+1,fmt);
+ entries[i++]=ret;
+ if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
+ child=child->next;
+ }
+
+ /* If we didn't fail, try to malloc the output string */
+ if (!fail) out=cJSON_malloc(len);
+ /* If that fails, we fail. */
+ if (!out) fail=1;
+
+ /* Handle failure. */
+ if (fail)
+ {
+ for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
+ cJSON_free(entries);
+ return 0;
+ }
+
+ /* Compose the output array. */
+ *out='[';
+ ptr=out+1;*ptr=0;
+ for (i=0;i<numentries;i++)
+ {
+ strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
+ if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
+ cJSON_free(entries[i]);
+ }
+ cJSON_free(entries);
+ *ptr++=']';*ptr++=0;
+ return out;
+}
+
+/* Build an object from the text. */
+static const char *parse_object(cJSON *item,const char *value)
+{
+ cJSON *child;
+ if (*value!='{') return 0; /* not an object! */
+
+ item->type=cJSON_Object;
+ value=skip(value+1);
+ if (*value=='}') return value+1; /* empty array. */
+
+ item->child=child=cJSON_New_Item();
+ value=skip(parse_string(child,skip(value)));
+ if (!value) return 0;
+ child->string=child->valuestring;child->valuestring=0;
+ if (*value!=':') return 0; /* fail! */
+ value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
+ if (!value) return 0;
+
+ while (*value==',')
+ {
+ cJSON *new_item;
+ if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
+ child->next=new_item;new_item->prev=child;child=new_item;
+ value=skip(parse_string(child,skip(value+1)));
+ if (!value) return 0;
+ child->string=child->valuestring;child->valuestring=0;
+ if (*value!=':') return 0; /* fail! */
+ value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
+ if (!value) return 0;
+ }
+
+ if (*value=='}') return value+1; /* end of array */
+ return 0; /* malformed. */
+}
+
+/* Render an object to text. */
+static char *print_object(cJSON *item,int depth,int fmt)
+{
+ char **entries=0,**names=0;
+ char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
+ cJSON *child=item->child;
+ int numentries=0,fail=0;
+ /* Count the number of entries. */
+ while (child) numentries++,child=child->next;
+ /* Allocate space for the names and the objects */
+ entries=(char**)cJSON_malloc(numentries*sizeof(char*));
+ if (!entries) return 0;
+ names=(char**)cJSON_malloc(numentries*sizeof(char*));
+ if (!names) {cJSON_free(entries);return 0;}
+ memset(entries,0,sizeof(char*)*numentries);
+ memset(names,0,sizeof(char*)*numentries);
+
+ /* Collect all the results into our arrays: */
+ child=item->child;depth++;if (fmt) len+=depth;
+ while (child)
+ {
+ names[i]=str=print_string_ptr(child->string);
+ entries[i++]=ret=print_value(child,depth,fmt);
+ if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
+ child=child->next;
+ }
+
+ /* Try to allocate the output string */
+ if (!fail) out=(char*)cJSON_malloc(len);
+ if (!out) fail=1;
+
+ /* Handle failure */
+ if (fail)
+ {
+ for (i=0;i<numentries;i++) {if (names[i]) free(names[i]);if (entries[i]) free(entries[i]);}
+ free(names);free(entries);
+ return 0;
+ }
+
+ /* Compose the output: */
+ *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
+ for (i=0;i<numentries;i++)
+ {
+ if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
+ strcpy(ptr,names[i]);ptr+=strlen(names[i]);
+ *ptr++=':';if (fmt) *ptr++='\t';
+ strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
+ if (i!=numentries-1) *ptr++=',';
+ if (fmt) *ptr++='\n';*ptr=0;
+ cJSON_free(names[i]);cJSON_free(entries[i]);
+ }
+
+ cJSON_free(names);cJSON_free(entries);
+ if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
+ *ptr++='}';*ptr++=0;
+ return out;
+}
+
+/* Get Array size/item / object item. */
+int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
+cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
+cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
+
+/* Utility for array list handling. */
+static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
+/* Utility for handling references. */
+static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
+
+/* Add item to array/object. */
+void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
+void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
+void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
+void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
+
+cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
+ if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
+void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
+cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
+void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
+
+/* Replace array/object items with new ones. */
+void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
+ newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
+ if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
+void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
+
+/* Create basic types: */
+cJSON *cJSON_CreateNull() {cJSON *item=cJSON_New_Item();item->type=cJSON_NULL;return item;}
+cJSON *cJSON_CreateTrue() {cJSON *item=cJSON_New_Item();item->type=cJSON_True;return item;}
+cJSON *cJSON_CreateFalse() {cJSON *item=cJSON_New_Item();item->type=cJSON_False;return item;}
+cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;return item;}
+cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();item->type=cJSON_String;item->valuestring=cJSON_strdup(string);return item;}
+cJSON *cJSON_CreateArray() {cJSON *item=cJSON_New_Item();item->type=cJSON_Array;return item;}
+cJSON *cJSON_CreateObject() {cJSON *item=cJSON_New_Item();item->type=cJSON_Object;return item;}
+
+/* Create Arrays: */
+cJSON *cJSON_CreateIntArray(int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
+cJSON *cJSON_CreateFloatArray(float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
+cJSON *cJSON_CreateDoubleArray(double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
+cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
View
136 contrib/libvbucket/cJSON.h
@@ -0,0 +1,136 @@
+/*
+ Copyright (c) 2009 Dave Gamble
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+#ifndef cJSON__h
+#define cJSON__h
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* cJSON Types: */
+#define cJSON_False 0
+#define cJSON_True 1
+#define cJSON_NULL 2
+#define cJSON_Number 3
+#define cJSON_String 4
+#define cJSON_Array 5
+#define cJSON_Object 6
+
+#define cJSON_IsReference 256
+
+/* The cJSON structure: */
+typedef struct cJSON {
+ struct cJSON *next,*prev; /* next/prev allow you to walk
+ array/object
+ chains. Alternatively, use
+ GetArraySize/GetArrayItem/GetObjectItem */
+ struct cJSON *child; /* An array or object item will have a
+ child pointer pointing to a chain of
+ the items in the array/object. */
+
+ int type; /* The type of the item, as above. */
+
+ char *valuestring; /* The item's string, if type==cJSON_String */
+ int valueint; /* The item's number, if type==cJSON_Number */
+ double valuedouble; /* The item's number, if type==cJSON_Number */
+
+ char *string; /* The item's name string, if this item is the
+ child of, or is in the list of subitems of an
+ object. */
+} cJSON;
+
+typedef struct cJSON_Hooks {
+ void *(*malloc_fn)(size_t sz);
+ void (*free_fn)(void *ptr);
+} cJSON_Hooks;
+
+/* Supply malloc, realloc and free functions to cJSON */
+extern void cJSON_InitHooks(cJSON_Hooks* hooks);
+
+
+/* Supply a block of JSON, and this returns a cJSON object you can
+ interrogate. Call cJSON_Delete when finished. */
+extern cJSON *cJSON_Parse(const char *value);
+/* Render a cJSON entity to text for transfer/storage. Free the char*
+ when finished. */
+extern char *cJSON_Print(cJSON *item);
+/* Render a cJSON entity to text for transfer/storage without any
+ formatting. Free the char* when finished. */
+extern char *cJSON_PrintUnformatted(cJSON *item);
+/* Delete a cJSON entity and all subentities. */
+extern void cJSON_Delete(cJSON *c);
+
+/* Returns the number of items in an array (or object). */
+extern int cJSON_GetArraySize(cJSON *array);
+/* Retrieve item number "item" from array "array". Returns NULL if
+ unsuccessful. */
+extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
+/* Get item "string" from object. Case insensitive. */
+extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
+
+/* These calls create a cJSON item of the appropriate type. */
+extern cJSON *cJSON_CreateNull(void);
+extern cJSON *cJSON_CreateTrue(void);
+extern cJSON *cJSON_CreateFalse(void);
+extern cJSON *cJSON_CreateNumber(double num);
+extern cJSON *cJSON_CreateString(const char *string);
+extern cJSON *cJSON_CreateArray(void);
+extern cJSON *cJSON_CreateObject(void);
+
+/* These utilities create an Array of count items. */
+extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
+extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
+extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
+extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
+
+/* Append item to the specified array/object. */
+extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
+extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
+/* Append reference to item to the specified array/object. Use this
+ when you want to add an existing cJSON to a new cJSON, but don't
+ want to corrupt your existing cJSON. */
+extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
+extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
+
+/* Remove/Detatch items from Arrays/Objects. */
+extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
+extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
+extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
+extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
+
+/* Update array items. */
+extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
+extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
+
+#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
+#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
+#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
+#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
+#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
86 contrib/libvbucket/crc32.c
@@ -0,0 +1,86 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/* The crc32 functions and data was originally written by Spencer
+ * Garrett <srg@quick.com> and was gleaned from the PostgreSQL source
+ * tree via the files contrib/ltree/crc32.[ch] and from FreeBSD at
+ * src/usr.bin/cksum/crc32.c.
+ */
+
+#include "hash.h"
+
+static const uint32_t crc32tab[256] = {
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
+ 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
+ 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
+ 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
+ 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
+ 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
+ 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
+ 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
+ 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
+ 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
+ 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
+ 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
+ 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
+ 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
+ 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
+ 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
+ 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
+ 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
+ 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
+ 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
+ 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
+ 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
+ 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
+ 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
+ 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
+ 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
+ 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
+ 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
+ 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
+ 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
+ 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
+ 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
+ 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
+ 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
+ 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
+ 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
+ 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
+ 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
+ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
+};
+
+uint32_t hash_crc32(const char *key, size_t key_length)
+{
+ uint64_t x;
+ uint32_t crc= UINT32_MAX;
+
+ for (x= 0; x < key_length; x++)
+ crc= (crc >> 8) ^ crc32tab[(crc ^ (uint64_t)key[x]) & 0xff];
+
+ return ((~crc) >> 16) & 0x7fff;
+}
View
39 contrib/libvbucket/hash.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2010 NorthScale, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIBVBUCKET_HASH_H
+#define LIBVBUCKET_HASH_H 1
+
+#include "config.h"
+
+#ifndef HAVE_STDINT_H
+#include "win_stdint.h"
+#else
+#include <stdint.h>
+#endif
+
+#include <sys/types.h>
+#include <stdio.h>
+
+uint32_t hash_crc32(const char *key, size_t key_length);
+uint32_t hash_ketama(const char *key, size_t key_length);
+void hash_md5(const char *key, size_t key_length, unsigned char *result);
+
+void* hash_md5_update(void *ctx, const char *key, size_t key_length);
+void hash_md5_final(void *ctx, unsigned char *result);
+
+#endif
View
55 contrib/libvbucket/ketama.c
@@ -0,0 +1,55 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+#include <stdlib.h>
+#include "hash.h"
+
+/* Force md5 functions to be static. The compiler could show warnings but
+ * it ok, we did it because we need to keep files in vendor/ directory
+ * unmodified. */
+static void MD5Init();
+static void MD5Update();
+static void MD5Final();
+
+/* This library uses the reference MD5 implementation from [RFC1321] */
+#define PROTOTYPES 1
+#include "rfc1321/md5c.c"
+#undef PROTOTYPES
+
+void hash_md5(const char *key, size_t key_length, unsigned char *result)
+{
+ MD5_CTX ctx;
+
+ MD5Init(&ctx);
+ MD5Update(&ctx, (unsigned char *)key, key_length);
+ MD5Final(result, &ctx);
+}
+
+void* hash_md5_update(void *ctx, const char *key, size_t key_length)
+{
+ if (ctx == NULL) {
+ ctx = calloc(1, sizeof(MD5_CTX));
+ MD5Init(ctx);
+ }
+ MD5Update(ctx, (unsigned char *)key, key_length);
+ return ctx;
+}
+
+void hash_md5_final(void *ctx, unsigned char *result)
+{
+ if (ctx == NULL) {
+ return;
+ }
+ MD5Final(result, ctx);
+ free(ctx);
+}
+
+uint32_t hash_ketama(const char *key, size_t key_length)
+{
+ unsigned char digest[16];
+
+ hash_md5(key, key_length, digest);
+
+ return (uint32_t) ( (digest[3] << 24)
+ |(digest[2] << 16)
+ |(digest[1] << 8)
+ | digest[0]);
+}
View
38 contrib/libvbucket/rfc1321/global.h
@@ -0,0 +1,38 @@
+/* GLOBAL.H - RSAREF types and constants
+*/
+
+/* PROTOTYPES should be set to one if and only if the compiler supports
+ function argument prototyping.
+ The following makes PROTOTYPES default to 0 if it has not already
+ been defined with C compiler flags.
+ */
+#ifndef PROTOTYPES
+#define PROTOTYPES 0
+#endif
+
+#include "config.h"
+
+#ifndef HAVE_STDINT_H
+#include "win_stdint.h"
+#else
+#include <stdint.h>
+#endif
+
+/* POINTER defines a generic pointer type */
+typedef unsigned char *POINTER;
+
+/* UINT2 defines a two byte word */
+typedef uint16_t UINT2;
+
+/* UINT4 defines a four byte word */
+typedef uint32_t UINT4;
+
+/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
+ If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
+ returns an empty list.
+ */
+#if PROTOTYPES
+#define PROTO_LIST(list) list
+#else
+#define PROTO_LIST(list) ()
+#endif
View
35 contrib/libvbucket/rfc1321/md5.h
@@ -0,0 +1,35 @@
+/* MD5.H - header file for MD5C.C
+*/
+
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+ rights reserved.
+
+ License to copy and use this software is granted provided that it
+ is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+ Algorithm" in all material mentioning or referencing this software
+ or this function.
+
+ License is also granted to make and use derivative works provided
+ that such works are identified as "derived from the RSA Data
+ Security, Inc. MD5 Message-Digest Algorithm" in all material
+ mentioning or referencing the derived work.
+
+ RSA Data Security, Inc. makes no representations concerning either
+ the merchantability of this software or the suitability of this
+ software for any particular purpose. It is provided "as is"
+ without express or implied warranty of any kind.
+
+ These notices must be retained in any copies of any part of this
+ documentation and/or software.
+ */
+
+/* MD5 context. */
+typedef struct {
+ UINT4 state[4]; /* state (ABCD) */
+ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
+ unsigned char buffer[64]; /* input buffer */
+} MD5_CTX;
+
+void MD5Init PROTO_LIST ((MD5_CTX *));
+void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int));
+void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
View
335 contrib/libvbucket/rfc1321/md5c.c
@@ -0,0 +1,335 @@
+/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
+*/
+
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+ rights reserved.
+
+ License to copy and use this software is granted provided that it
+ is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+ Algorithm" in all material mentioning or referencing this software
+ or this function.
+
+ License is also granted to make and use derivative works provided
+ that such works are identified as "derived from the RSA Data
+ Security, Inc. MD5 Message-Digest Algorithm" in all material
+ mentioning or referencing the derived work.
+
+ RSA Data Security, Inc. makes no representations concerning either
+ the merchantability of this software or the suitability of this
+ software for any particular purpose. It is provided "as is"
+ without express or implied warranty of any kind.
+
+ These notices must be retained in any copies of any part of this
+ documentation and/or software.
+ */
+
+#include "global.h"
+#include "md5.h"
+
+/* Constants for MD5Transform routine.
+*/
+
+#define S11 7
+#define S12 12
+#define S13 17
+#define S14 22
+#define S21 5
+#define S22 9
+#define S23 14
+#define S24 20
+#define S31 4
+#define S32 11
+#define S33 16
+#define S34 23
+#define S41 6
+#define S42 10
+#define S43 15
+#define S44 21
+
+static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+static void Encode PROTO_LIST
+((unsigned char *, UINT4 *, unsigned int));
+static void Decode PROTO_LIST
+((UINT4 *, unsigned char *, unsigned int));
+static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+
+static unsigned char PADDING[64] = {
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* F, G, H and I are basic MD5 functions.
+*/
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define I(x, y, z) ((y) ^ ((x) | (~z)))
+
+/* ROTATE_LEFT rotates x left n bits.
+*/
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+
+/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ Rotation is separate from addition to prevent recomputation.
+ */
+#define FF(a, b, c, d, x, s, ac) { \
+ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+}
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+}
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+}
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+}
+
+/* MD5 initialization. Begins an MD5 operation, writing a new context.
+*/
+void MD5Init (context)
+ MD5_CTX *context; /* context */
+{
+ context->count[0] = context->count[1] = 0;
+ /* Load magic initialization constants.
+ */
+ context->state[0] = 0x67452301;
+ context->state[1] = 0xefcdab89;
+ context->state[2] = 0x98badcfe;
+ context->state[3] = 0x10325476;
+}
+
+/* MD5 block update operation. Continues an MD5 message-digest
+ operation, processing another message block, and updating the
+ context.
+ */
+void MD5Update (context, input, inputLen)
+ MD5_CTX *context; /* context */
+ unsigned char *input; /* input block */
+ unsigned int inputLen; /* length of input block */
+{
+ unsigned int i, index, partLen;
+
+ /* Compute number of bytes mod 64 */
+ index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+
+ /* Update number of bits */
+ if ((context->count[0] += ((UINT4)inputLen << 3))
+ < ((UINT4)inputLen << 3))
+ context->count[1]++;
+ context->count[1] += ((UINT4)inputLen >> 29);
+
+ partLen = 64 - index;
+
+ /* Transform as many times as possible.
+ */
+ if (inputLen >= partLen) {
+ MD5_memcpy
+ ((POINTER)&context->buffer[index], (POINTER)input, partLen);
+ MD5Transform (context->state, context->buffer);
+
+ for (i = partLen; i + 63 < inputLen; i += 64)
+ MD5Transform (context->state, &input[i]);
+
+ index = 0;
+ }
+ else
+ i = 0;
+
+ /* Buffer remaining input */
+ MD5_memcpy
+ ((POINTER)&context->buffer[index], (POINTER)&input[i],
+ inputLen-i);
+}
+
+/* MD5 finalization. Ends an MD5 message-digest operation, writing the
+ the message digest and zeroizing the context.
+ */
+void MD5Final (digest, context)
+ unsigned char digest[16]; /* message digest */
+ MD5_CTX *context; /* context */
+{
+ unsigned char bits[8];
+ unsigned int index, padLen;
+
+ /* Save number of bits */
+ Encode (bits, context->count, 8);
+
+ /* Pad out to 56 mod 64.
+ */
+ index = (unsigned int)((context->count[0] >> 3) & 0x3f);
+ padLen = (index < 56) ? (56 - index) : (120 - index);
+ MD5Update (context, PADDING, padLen);
+
+ /* Append length (before padding) */
+ MD5Update (context, bits, 8);
+
+ /* Store state in digest */
+ Encode (digest, context->state, 16);
+
+ /* Zeroize sensitive information.
+ */
+ MD5_memset ((POINTER)context, 0, sizeof (*context));
+}
+
+/* MD5 basic transformation. Transforms state based on block.
+*/
+static void MD5Transform (state, block)
+ UINT4 state[4];
+ unsigned char block[64];
+{
+ UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
+
+ Decode (x, block, 64);
+
+ /* Round 1 */
+ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
+ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
+ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
+ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
+ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
+ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
+ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
+ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
+ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
+ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
+ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
+ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
+ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
+ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
+ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
+ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
+
+ /* Round 2 */
+ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
+ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
+ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
+ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
+ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
+ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
+ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
+ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
+ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
+ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
+ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
+ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
+ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
+ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
+ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
+ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
+
+ /* Round 3 */
+ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
+ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
+ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
+ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
+ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
+ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
+ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
+ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
+ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
+ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
+ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
+ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
+ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
+ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
+ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
+ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
+
+ /* Round 4 */
+ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
+ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
+ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
+ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
+ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
+ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
+ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
+ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
+ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
+ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
+ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
+ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
+ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
+ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
+ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
+ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
+
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+
+ /* Zeroize sensitive information.
+ */
+ MD5_memset ((POINTER)x, 0, sizeof (x));
+}
+
+/* Encodes input (UINT4) into output (unsigned char). Assumes len is
+ a multiple of 4.
+ */
+static void Encode (output, input, len)
+ unsigned char *output;
+ UINT4 *input;
+ unsigned int len;
+{
+ unsigned int i, j;
+
+ for (i = 0, j = 0; j < len; i++, j += 4) {
+ output[j] = (unsigned char)(input[i] & 0xff);
+ output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
+ output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
+ output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
+ }
+}
+
+/* Decodes input (unsigned char) into output (UINT4). Assumes len is
+ a multiple of 4.
+ */
+static void Decode (output, input, len)
+ UINT4 *output;
+ unsigned char *input;
+ unsigned int len;
+{
+ unsigned int i, j;
+
+ for (i = 0, j = 0; j < len; i++, j += 4)
+ output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
+ (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
+}
+
+/* Note: Replace "for loop" with standard memcpy if possible.
+*/
+
+static void MD5_memcpy (output, input, len)
+ POINTER output;
+ POINTER input;
+ unsigned int len;
+{
+ unsigned int i;
+
+ for (i = 0; i < len; i++)
+ output[i] = input[i];
+}
+
+/* Note: Replace "for loop" with standard memset if possible.
+*/
+static void MD5_memset (output, value, len)
+ POINTER output;
+ int value;
+ unsigned int len;
+{
+ unsigned int i;
+
+ for (i = 0; i < len; i++)
+ ((char *)output)[i] = (char)value;
+}
View
821 contrib/libvbucket/vbucket.c
@@ -0,0 +1,821 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2010 NorthScale, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+#include "config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <errno.h>
+
+#include "cJSON.h"
+#include "hash.h"
+#include <libvbucket/vbucket.h>
+
+#define MAX_CONFIG_SIZE 100 * 1048576
+#define MAX_VBUCKETS 65536
+#define MAX_REPLICAS 4
+#define MAX_AUTORITY_SIZE 100
+#define STRINGIFY_(X) #X
+#define STRINGIFY(X) STRINGIFY_(X)
+
+struct server_st {
+ char *authority; /* host:port */
+ char *rest_api_authority;
+ char *couchdb_api_base;
+};
+
+struct vbucket_st {
+ int servers[MAX_REPLICAS + 1];
+};
+
+struct continuum_item_st {
+ uint32_t index; /* server index */
+ uint32_t point; /* point on the ketama continuum */
+};
+
+struct vbucket_config_st {
+ char *errmsg;
+ VBUCKET_DISTRIBUTION_TYPE distribution;
+ int num_vbuckets;
+ int mask;
+ int num_servers;
+ int num_replicas;
+ char *user;
+ char *password;
+ int num_continuum; /* count of continuum points */
+ struct continuum_item_st *continuum; /* ketama continuum */
+ struct server_st *servers;
+ struct vbucket_st *fvbuckets;
+ struct vbucket_st *vbuckets;
+};
+
+static char *errstr = NULL;
+
+const char *vbucket_get_error() {
+ return errstr;
+}
+
+static int continuum_item_cmp(const void *t1, const void *t2)
+{
+ const struct continuum_item_st *ct1 = t1, *ct2 = t2;
+
+ if (ct1->point == ct2->point) {
+ return 0;
+ } else if (ct1->point > ct2->point) {
+ return 1;
+ } else {
+ return -1;
+ }
+}
+
+static void update_ketama_continuum(VBUCKET_CONFIG_HANDLE vb)
+{
+ char host[MAX_AUTORITY_SIZE+10] = "";
+ int nhost;
+ int pp, hh, ss, nn;
+ unsigned char digest[16];
+ struct continuum_item_st *new_continuum, *old_continuum;
+
+ new_continuum = calloc(160 * vb->num_servers,
+ sizeof(struct continuum_item_st));
+
+ /* 40 hashes, 4 numbers per hash = 160 points per server */
+ for (ss = 0, pp = 0; ss < vb->num_servers; ++ss) {
+ /* we can add more points to server which have more memory */
+ for (hh = 0; hh < 40; ++hh) {
+ nhost = snprintf(host, MAX_AUTORITY_SIZE+10, "%s-%u",
+ vb->servers[ss].authority, hh);
+ hash_md5(host, nhost, digest);
+ for (nn = 0; nn < 4; ++nn, ++pp) {
+ new_continuum[pp].index = ss;
+ new_continuum[pp].point = ((uint32_t) (digest[3 + nn * 4] & 0xFF) << 24)
+ | ((uint32_t) (digest[2 + nn * 4] & 0xFF) << 16)
+ | ((uint32_t) (digest[1 + nn * 4] & 0xFF) << 8)
+ | (digest[0 + nn * 4] & 0xFF);
+ }
+ }
+ }
+
+ qsort(new_continuum, pp, sizeof(struct continuum_item_st), continuum_item_cmp);
+
+ old_continuum = vb->continuum;
+ vb->continuum = new_continuum;
+ vb->num_continuum = pp;
+ if (old_continuum) {
+ free(old_continuum);
+ }
+}
+
+void vbucket_config_destroy(VBUCKET_CONFIG_HANDLE vb) {
+ int i;
+ for (i = 0; i < vb->num_servers; ++i) {
+ free(vb->servers[i].authority);
+ free(vb->servers[i].rest_api_authority);
+ free(vb->servers[i].couchdb_api_base);
+ }
+ free(vb->servers);
+ free(vb->user);
+ free(vb->password);
+ free(vb->fvbuckets);
+ free(vb->vbuckets);
+ free(vb->continuum);
+ free(vb->errmsg);
+ memset(vb, 0xff, sizeof(struct vbucket_config_st));
+ free(vb);
+}
+
+static int populate_servers(struct vbucket_config_st *vb, cJSON *c) {
+ int i;
+
+ vb->servers = calloc(vb->num_servers, sizeof(struct server_st));
+ if (vb->servers == NULL) {
+ vbucket_config_destroy(vb);
+ vb->errmsg = strdup("Failed to allocate servers array");
+ return -1;
+ }
+ for (i = 0; i < vb->num_servers; ++i) {
+ char *server;
+ cJSON *jServer = cJSON_GetArrayItem(c, i);
+ if (jServer == NULL || jServer->type != cJSON_String) {
+ vb->errmsg = strdup("Expected array of strings for serverList");
+ return -1;
+ }
+ server = strdup(jServer->valuestring);
+ if (server == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for server string");
+ return -1;
+ }
+ vb->servers[i].authority = server;
+ }
+ return 0;
+}
+
+static int get_node_authority(struct vbucket_config_st *vb, cJSON *node, char *buf, size_t nbuf) {
+ cJSON *json;
+ char *hostname = NULL, *colon = NULL;
+ int port = -1;
+
+ json = cJSON_GetObjectItem(node, "hostname");
+ if (json == NULL || json->type != cJSON_String) {
+ vb->errmsg = strdup("Expected string for node's hostname");
+ return -1;
+ }
+ hostname = json->valuestring;
+ json = cJSON_GetObjectItem(node, "ports");
+ if (json == NULL || json->type != cJSON_Object) {
+ vb->errmsg = strdup("Expected json object for node's ports");
+ return -1;
+ }
+ json = cJSON_GetObjectItem(json, "direct");
+ if (json == NULL || json->type != cJSON_Number) {
+ vb->errmsg = strdup("Expected number for node's direct port");
+ return -1;
+ }
+ port = json->valueint;
+
+ snprintf(buf, nbuf - 7, "%s", hostname);
+ colon = strchr(buf, ':');
+ if (!colon) {
+ colon = buf + strlen(buf);
+ }
+ snprintf(colon, 7, ":%d", port);
+
+ return 0;
+}
+
+static int lookup_server_struct(struct vbucket_config_st *vb, cJSON *c) {
+ char *authority = NULL;
+ int idx = -1, ii;
+
+ authority = calloc(MAX_AUTORITY_SIZE, sizeof(char));
+ if (authority == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for authority string");
+ return -1;
+ }
+ if (get_node_authority(vb, c, authority, MAX_AUTORITY_SIZE) < 0) {
+ free(authority);
+ return -1;
+ }
+
+ for (ii = 0; ii < vb->num_servers; ++ii) {
+ if (strcmp(vb->servers[ii].authority, authority) == 0) {
+ idx = ii;
+ break;
+ }
+ }
+
+ free(authority);
+ return idx;
+}
+
+static int update_server_info(struct vbucket_config_st *vb, cJSON *config) {
+ int idx, ii;
+ cJSON *node, *json;
+
+ for (ii = 0; ii < cJSON_GetArraySize(config); ++ii) {
+ node = cJSON_GetArrayItem(config, ii);
+ if (node) {
+ if (node->type != cJSON_Object) {
+ vb->errmsg = strdup("Expected json object for nodes array item");
+ return -1;
+ }
+
+ if ((idx = lookup_server_struct(vb, node)) >= 0) {
+ json = cJSON_GetObjectItem(node, "couchApiBase");
+ if (json != NULL) {
+ char *value = strdup(json->valuestring);
+ if (value == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for couchApiBase string");
+ return -1;
+ }
+ vb->servers[idx].couchdb_api_base = value;
+ }
+ json = cJSON_GetObjectItem(node, "hostname");
+ if (json != NULL) {
+ char *value = strdup(json->valuestring);
+ if (value == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for hostname string");
+ return -1;
+ }
+ vb->servers[idx].rest_api_authority = value;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+static int populate_buckets(struct vbucket_config_st *vb, cJSON *c, int is_forward)
+{
+ int i, j;
+ struct vbucket_st *vb_map = NULL;
+
+ if (is_forward) {
+ if (!(vb->fvbuckets = vb_map = calloc(vb->num_vbuckets, sizeof(struct vbucket_st)))) {
+ vb->errmsg = strdup("Failed to allocate storage for forward vbucket map");
+ return -1;
+ }
+ } else {
+ if (!(vb->vbuckets = vb_map = calloc(vb->num_vbuckets, sizeof(struct vbucket_st)))) {
+ vb->errmsg = strdup("Failed to allocate storage for vbucket map");
+ return -1;
+ }
+ }
+
+ for (i = 0; i < vb->num_vbuckets; ++i) {
+ cJSON *jBucket = cJSON_GetArrayItem(c, i);
+ if (jBucket == NULL || jBucket->type != cJSON_Array ||
+ cJSON_GetArraySize(jBucket) != vb->num_replicas + 1) {
+ vb->errmsg = strdup("Expected array of arrays each with numReplicas + 1 ints for vBucketMap");
+ return -1;
+ }
+ for (j = 0; j < vb->num_replicas + 1; ++j) {
+ cJSON *jServerId = cJSON_GetArrayItem(jBucket, j);
+ if (jServerId == NULL || jServerId->type != cJSON_Number ||
+ jServerId->valueint < -1 || jServerId->valueint >= vb->num_servers) {
+ vb->errmsg = strdup("Server ID must be >= -1 and < num_servers");
+ return -1;
+ }
+ vb_map[i].servers[j] = jServerId->valueint;
+ }
+ }
+ return 0;
+}
+
+static int parse_vbucket_config(VBUCKET_CONFIG_HANDLE vb, cJSON *c)
+{
+ cJSON *json, *config;
+
+ config = cJSON_GetObjectItem(c, "vBucketServerMap");
+ if (config == NULL || config->type != cJSON_Object) {
+ /* seems like config without envelop, try to parse it */
+ config = c;
+ }
+
+ json = cJSON_GetObjectItem(config, "numReplicas");
+ if (json == NULL || json->type != cJSON_Number ||
+ json->valueint > MAX_REPLICAS) {
+ vb->errmsg = strdup("Expected number <= " STRINGIFY(MAX_REPLICAS) " for numReplicas");
+ return -1;
+ }
+ vb->num_replicas = json->valueint;
+
+ json = cJSON_GetObjectItem(config, "serverList");
+ if (json == NULL || json->type != cJSON_Array) {
+ vb->errmsg = strdup("Expected array for serverList");
+ return -1;
+ }
+ vb->num_servers = cJSON_GetArraySize(json);
+ if (vb->num_servers == 0) {
+ vb->errmsg = strdup("Empty serverList");
+ return -1;
+ }
+ if (populate_servers(vb, json) != 0) {
+ return -1;
+ }
+ /* optionally update server info using envelop (couchdb_api_base etc.) */
+ json = cJSON_GetObjectItem(c, "nodes");
+ if (json) {
+ if (json->type != cJSON_Array) {
+ vb->errmsg = strdup("Expected array for nodes");
+ return -1;
+ }
+ if (update_server_info(vb, json) != 0) {
+ return -1;
+ }
+ }
+
+ json = cJSON_GetObjectItem(config, "vBucketMap");
+ if (json == NULL || json->type != cJSON_Array) {
+ vb->errmsg = strdup("Expected array for vBucketMap");
+ return -1;
+ }
+ vb->num_vbuckets = cJSON_GetArraySize(json);
+ if (vb->num_vbuckets == 0 || (vb->num_vbuckets & (vb->num_vbuckets - 1)) != 0) {
+ vb->errmsg = strdup("Number of vBuckets must be a power of two > 0 and <= " STRINGIFY(MAX_VBUCKETS));
+ return -1;
+ }
+ vb->mask = vb->num_vbuckets - 1;
+ if (populate_buckets(vb, json, 0) != 0) {
+ return -1;
+ }
+
+ /* vbucket forward map could possibly be null */
+ json = cJSON_GetObjectItem(config, "vBucketMapForward");
+ if (json) {
+ if (json->type != cJSON_Array) {
+ vb->errmsg = strdup("Expected array for vBucketMapForward");
+ return -1;
+ }
+ if (populate_buckets(vb, json, 1) !=0) {
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int server_cmp(const void *s1, const void *s2)
+{
+ return strcmp(((const struct server_st *)s1)->authority,
+ ((const struct server_st *)s2)->authority);
+}
+
+static int parse_ketama_config(VBUCKET_CONFIG_HANDLE vb, cJSON *config)
+{
+ cJSON *json, *node, *hostname;
+ char *buf;
+ int ii;
+
+ json = cJSON_GetObjectItem(config, "nodes");
+ if (json == NULL || json->type != cJSON_Array) {
+ vb->errmsg = strdup("Expected array for nodes");
+ return -1;
+ }
+
+ vb->num_servers = cJSON_GetArraySize(json);
+ if (vb->num_servers == 0) {
+ vb->errmsg = strdup("Empty serverList");
+ return -1;
+ }
+ vb->servers = calloc(vb->num_servers, sizeof(struct server_st));
+ for (ii = 0; ii < vb->num_servers; ++ii) {
+ node = cJSON_GetArrayItem(json, ii);
+ if (node == NULL || node->type != cJSON_Object) {
+ vb->errmsg = strdup("Expected object for nodes array item");
+ return -1;
+ }
+ buf = calloc(MAX_AUTORITY_SIZE, sizeof(char));
+ if (buf == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for node authority");
+ return -1;
+ }
+ if (get_node_authority(vb, node, buf, MAX_AUTORITY_SIZE) < 0) {
+ return -1;
+ }
+ vb->servers[ii].authority = buf;
+ hostname = cJSON_GetObjectItem(node, "hostname");
+ if (hostname == NULL || hostname->type != cJSON_String) {
+ vb->errmsg = strdup("Expected string for node's hostname");
+ return -1;
+ }
+ buf = strdup(hostname->valuestring);
+ if (buf == NULL) {
+ vb->errmsg = strdup("Failed to allocate storage for hostname string");
+ return -1;
+ }
+ vb->servers[ii].rest_api_authority = buf;
+ }
+ qsort(vb->servers, vb->num_servers, sizeof(struct server_st), server_cmp);
+
+ update_ketama_continuum(vb);
+ return 0;
+}
+
+static int parse_cjson(VBUCKET_CONFIG_HANDLE handle, cJSON *config)
+{
+ cJSON *json;
+
+ /* set optional credentials */
+ json = cJSON_GetObjectItem(config, "name");
+ if (json != NULL && json->type == cJSON_String && strcmp(json->valuestring, "default") != 0) {
+ handle->user = strdup(json->valuestring);
+ }
+ json = cJSON_GetObjectItem(config, "saslPassword");
+ if (json != NULL && json->type == cJSON_String) {
+ handle->password = strdup(json->valuestring);
+ }
+
+ /* by default it uses vbucket distribution to map keys to servers */
+ handle->distribution = VBUCKET_DISTRIBUTION_VBUCKET;
+
+ json = cJSON_GetObjectItem(config, "nodeLocator");
+ if (json == NULL) {
+ /* special case: it migth be config without envelope */
+ if (parse_vbucket_config(handle, config) == -1) {
+ return -1;
+ }
+ } else if (json->type == cJSON_String) {
+ if (strcmp(json->valuestring, "vbucket") == 0) {
+ handle->distribution = VBUCKET_DISTRIBUTION_VBUCKET;
+ if (parse_vbucket_config(handle, config) == -1) {
+ return -1;
+ }
+ } else if (strcmp(json->valuestring, "ketama") == 0) {
+ handle->distribution = VBUCKET_DISTRIBUTION_KETAMA;
+ if (parse_ketama_config(handle, config) == -1) {
+ return -1;
+ }
+ }
+ } else {
+ handle->errmsg = strdup("Expected string for nodeLocator");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int parse_from_memory(VBUCKET_CONFIG_HANDLE handle, const char *data) {
+ int ret;
+ cJSON *c = cJSON_Parse(data);
+ if (c == NULL) {
+ handle->errmsg = strdup("Failed to parse data. Invalid JSON?");
+ return -1;
+ }
+
+ ret = parse_cjson(handle, c);
+
+ cJSON_Delete(c);
+ return ret;
+}
+
+static int do_read_file(FILE *fp, char *data, size_t size)
+{
+ size_t offset = 0;
+ size_t nread;
+
+ do {
+ nread = fread(data + offset, 1, size, fp);
+ if (nread != (size_t)-1 && nread != 0) {
+ offset += nread;
+ size -= nread;
+ } else {
+ return -1;
+ }
+ } while (size > 0);
+
+ return 0;
+}
+
+static int parse_from_file(VBUCKET_CONFIG_HANDLE handle, const char *filename)
+{
+ long size;
+ char *data;
+ int ret;
+ FILE *f = fopen(filename, "rb");
+ if (f == NULL) {
+ char msg[1024];
+ snprintf(msg, sizeof(msg), "Unable to open file \"%s\": %s", filename,
+ strerror(errno));
+ handle->errmsg = strdup(msg);
+ return -1;
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (size > MAX_CONFIG_SIZE) {
+ char msg[1024];
+ snprintf(msg, sizeof(msg), "File too large: \"%s\"", filename);
+ handle->errmsg = strdup(msg);
+ fclose(f);
+ return -1;
+ }
+ data = calloc(size+1, sizeof(char));
+ if (data == NULL) {
+ char msg[1024];
+ snprintf(msg, sizeof(msg), "Failed to allocate buffer to read: \"%s\"", filename);
+ handle->errmsg = strdup(msg);
+ fclose(f);
+ return -1;
+ }
+ if (do_read_file(f, data, size) == -1) {
+ char msg[1024];
+ snprintf(msg, sizeof(msg), "Failed to read entire file: \"%s\": %s",
+ filename, strerror(errno));
+ handle->errmsg = strdup(msg);
+ fclose(f);
+ free(data);
+ return -1;
+ }
+
+ fclose(f);
+ ret = parse_from_memory(handle, data);
+ free(data);
+ return ret;
+}
+
+VBUCKET_CONFIG_HANDLE vbucket_config_create(void)
+{
+ return calloc(1, sizeof(struct vbucket_config_st));
+}
+
+int vbucket_config_parse(VBUCKET_CONFIG_HANDLE handle,
+ vbucket_source_t data_source,
+ const char *data)
+{
+ if (data_source == LIBVBUCKET_SOURCE_FILE) {
+ return parse_from_file(handle, data);
+ } else {
+ return parse_from_memory(handle, data);
+ }
+}
+
+const char *vbucket_get_error_message(VBUCKET_CONFIG_HANDLE handle)
+{
+ return handle->errmsg;
+}
+
+static VBUCKET_CONFIG_HANDLE backwards_compat(vbucket_source_t source, const char *data)
+{
+ VBUCKET_CONFIG_HANDLE ret = vbucket_config_create();
+ if (ret == NULL) {
+ return NULL;
+ }
+
+ if (vbucket_config_parse(ret, source, data) != 0) {
+ errstr = strdup(ret->errmsg);
+ vbucket_config_destroy(ret);
+ ret = NULL;
+ }
+
+ return ret;
+}
+
+VBUCKET_CONFIG_HANDLE vbucket_config_parse_file(const char *filename)
+{
+ return backwards_compat(LIBVBUCKET_SOURCE_FILE, filename);
+}
+
+VBUCKET_CONFIG_HANDLE vbucket_config_parse_string(const char *data)
+{
+ return backwards_compat(LIBVBUCKET_SOURCE_MEMORY, data);
+}
+
+int vbucket_map(VBUCKET_CONFIG_HANDLE vb, const void *key, size_t nkey,
+ int *vbucket_id, int *server_idx)
+{
+ uint32_t digest, mid, prev;
+ struct continuum_item_st *beginp, *endp, *midp, *highp, *lowp;
+
+ if (vb->distribution == VBUCKET_DISTRIBUTION_KETAMA) {
+ assert(vb->continuum);
+ if (vbucket_id) {
+ *vbucket_id = 0;
+ }
+ digest = hash_ketama(key, nkey);
+ beginp = lowp = vb->continuum;
+ endp = highp = vb->continuum + vb->num_continuum;
+
+ /* divide and conquer array search to find server with next biggest
+ * point after what this key hashes to */
+ while (1)
+ {
+ /* pick the middle point */
+ midp = lowp + (highp - lowp) / 2;
+
+ if (midp == endp) {
+ /* if at the end, roll back to zeroth */
+ *server_idx = beginp->index;
+ break;
+ }
+
+ mid = midp->point;
+ prev = (midp == beginp) ? 0 : (midp-1)->point;
+
+ if (digest <= mid && digest > prev) {
+ /* we found nearest server */
+ *server_idx = midp->index;
+ break;
+ }
+
+ /* adjust the limits */
+ if (mid < digest) {
+ lowp = midp + 1;
+ } else {
+ highp = midp - 1;
+ }
+
+ if (lowp > highp) {
+ *server_idx = beginp->index;
+ break;
+ }
+ }
+ } else {
+ *vbucket_id = vbucket_get_vbucket_by_key(vb, key, nkey);
+ *server_idx = vbucket_get_master(vb, *vbucket_id);
+ }
+ return 0;
+}
+
+
+int vbucket_config_get_num_replicas(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->num_replicas;
+}
+
+int vbucket_config_get_num_vbuckets(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->num_vbuckets;
+}
+
+int vbucket_config_get_num_servers(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->num_servers;
+}
+
+const char *vbucket_config_get_couch_api_base(VBUCKET_CONFIG_HANDLE vb, int i) {
+ return vb->servers[i].couchdb_api_base;
+}
+
+const char *vbucket_config_get_rest_api_server(VBUCKET_CONFIG_HANDLE vb, int i) {
+ return vb->servers[i].rest_api_authority;
+}
+
+VBUCKET_DISTRIBUTION_TYPE vbucket_config_get_distribution_type(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->distribution;
+}
+
+const char *vbucket_config_get_server(VBUCKET_CONFIG_HANDLE vb, int i) {
+ return vb->servers[i].authority;
+}
+
+const char *vbucket_config_get_user(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->user;
+}
+
+const char *vbucket_config_get_password(VBUCKET_CONFIG_HANDLE vb) {
+ return vb->password;
+}
+
+int vbucket_get_vbucket_by_key(VBUCKET_CONFIG_HANDLE vb, const void *key, size_t nkey) {
+ /* call crc32 directly here it could be changed to some more general
+ * function when vbucket distribution will support multiple hashing
+ * algorithms */
+ uint32_t digest = hash_crc32(key, nkey);
+ return digest & vb->mask;
+}
+
+int vbucket_get_master(VBUCKET_CONFIG_HANDLE vb, int vbucket) {
+ return vb->vbuckets[vbucket].servers[0];
+}
+
+int vbucket_get_replica(VBUCKET_CONFIG_HANDLE vb, int vbucket, int i) {
+ return vb->vbuckets[vbucket].servers[i+1];
+}
+
+int vbucket_found_incorrect_master(VBUCKET_CONFIG_HANDLE vb, int vbucket,
+ int wrongserver) {
+ int mappedServer = vb->vbuckets[vbucket].servers[0];
+ int rv = mappedServer;
+ /*
+ * if a forward table exists, then return the vbucket id from the forward table
+ * and update that information in the current table. We also need to Update the
+ * replica information for that vbucket
+ */
+ if (vb->fvbuckets) {
+ int i = 0;
+ rv = vb->vbuckets[vbucket].servers[0] = vb->fvbuckets[vbucket].servers[0];
+ for (i = 0; i < vb->num_replicas; i++) {
+ vb->vbuckets[vbucket].servers[i+1] = vb->fvbuckets[vbucket].servers[i+1];
+ }
+ } else if (mappedServer == wrongserver) {
+ rv = (rv + 1) % vb->num_servers;
+ vb->vbuckets[vbucket].servers[0] = rv;
+ }
+
+ return rv;
+}
+
+static void compute_vb_list_diff(VBUCKET_CONFIG_HANDLE from,
+ VBUCKET_CONFIG_HANDLE to,
+ char **out) {
+ int offset = 0;
+ int i, j;
+ for (i = 0; i < to->num_servers; i++) {
+ int found = 0;
+ const char *sn = vbucket_config_get_server(to, i);
+ for (j = 0; !found && j < from->num_servers; j++) {
+ const char *sn2 = vbucket_config_get_server(from, j);
+ found |= (strcmp(sn2, sn) == 0);
+ }
+ if (!found) {
+ out[offset] = strdup(sn);
+ assert(out[offset]);
+ ++offset;
+ }
+ }
+}
+
+VBUCKET_CONFIG_DIFF* vbucket_compare(VBUCKET_CONFIG_HANDLE from,
+ VBUCKET_CONFIG_HANDLE to) {
+ VBUCKET_CONFIG_DIFF *rv = calloc(1, sizeof(VBUCKET_CONFIG_DIFF));
+ int num_servers = (from->num_servers > to->num_servers
+ ? from->num_servers : to->num_servers) + 1;
+ assert(rv);
+ rv->servers_added = calloc(num_servers, sizeof(char*));
+ rv->servers_removed = calloc(num_servers, sizeof(char*));
+
+ /* Compute the added and removed servers */
+ compute_vb_list_diff(from, to, rv->servers_added);
+ compute_vb_list_diff(to, from, rv->servers_removed);
+
+ /* Verify the servers are equal in their positions */
+ if (to->num_servers == from->num_servers) {
+ int i;
+ for (i = 0; i < from->num_servers; i++) {
+ rv->sequence_changed |= (0 != strcmp(vbucket_config_get_server(from, i),
+ vbucket_config_get_server(to, i)));
+
+ }
+ } else {
+ /* Just say yes */
+ rv->sequence_changed = 1;
+ }
+
+ /* Consider the sequence changed if the auth credentials changed */
+ if (from->user != NULL && to->user != NULL) {
+ rv->sequence_changed |= (strcmp(from->user, to->user) != 0);
+ } else {
+ rv->sequence_changed |= ((from->user != NULL) ^ (to->user != NULL));
+ }
+
+ if (from->password != NULL && to->password != NULL) {
+ rv->sequence_changed |= (strcmp(from->password, to->password) != 0);
+ } else {
+ rv->sequence_changed |= ((from->password != NULL) ^ (to->password != NULL));
+ }
+
+ /* Count the number of vbucket differences */
+ if (to->num_vbuckets == from->num_vbuckets) {
+ int i;
+ for (i = 0; i < to->num_vbuckets; i++) {
+ rv->n_vb_changes += (vbucket_get_master(from, i)
+ == vbucket_get_master(to, i)) ? 0 : 1;
+ }
+ } else {
+ rv->n_vb_changes = -1;
+ }
+
+ return rv;
+}
+
+static void free_array_helper(char **l) {
+ int i;
+ for (i = 0; l[i]; i++) {
+ free(l[i]);
+ }
+ free(l);
+}
+
+void vbucket_free_diff(VBUCKET_CONFIG_DIFF *diff) {
+ assert(diff);
+ free_array_helper(diff->servers_added);
+ free_array_helper(diff->servers_removed);
+ free(diff);
+}
View
366 include/libvbucket/vbucket.h
@@ -0,0 +1,366 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2010 NorthScale, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*! \mainpage libvbucket
+ *
+ * \section intro_sec Introduction
+ *
+ * libvbucket helps you understand and make use of vbuckets for
+ * scaling kv services.
+ *
+ * \section docs_sec API Documentation
+ *
+ * Jump right into <a href="modules.html">the modules docs</a> to get started.
+ */
+
+/**
+ * VBucket Utility Library.
+ *
+ * \defgroup CD Creation and Destruction
+ * \defgroup cfg Config Access
+ * \defgroup cfgcmp Config Comparison
+ * \defgroup vb VBucket Access
+ * \defgroup err Error handling
+ */
+
+#ifndef LIBVBUCKET_VBUCKET_H
+#define LIBVBUCKET_VBUCKET_H 1
+
+#include <stddef.h>
+#include "visibility.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ struct vbucket_config_st;
+
+ /**
+ * Opaque config representation.
+ */
+ typedef struct vbucket_config_st* VBUCKET_CONFIG_HANDLE;
+
+ /**
+ * Type of distribution used to map keys to servers. It is possible to
+ * select algorithm using "locator" key in config.
+ */
+ typedef enum {
+ VBUCKET_DISTRIBUTION_VBUCKET = 0,
+ VBUCKET_DISTRIBUTION_KETAMA = 1
+ } VBUCKET_DISTRIBUTION_TYPE;
+
+ /**
+ * \addtogroup cfgcmp
+ * @{
+ */
+
+ /**
+ * Difference between two vbucket configs.
+ */
+ typedef struct {
+ /**
+ * NULL-terminated list of server names that were added.
+ */
+ char **servers_added;
+ /**
+ * NULL-terminated list of server names that were removed.
+ */
+ char **servers_removed;
+ /**
+ * Number of vbuckets that changed. -1 if the total number changed
+ */
+ int n_vb_changes;
+ /**
+ * non-null if the sequence of servers changed.
+ */
+ int sequence_changed;
+ } VBUCKET_CONFIG_DIFF;
+
+ /**
+ * @}
+ */
+
+ /**
+ * \addtogroup CD
+ * @{
+ */
+
+ /**
+ * Create a new vbucket config handle
+ * @return handle or NULL if there is no more memory
+ */
+ LIBVBUCKET_PUBLIC_API
+ VBUCKET_CONFIG_HANDLE vbucket_config_create(void);
+
+ typedef enum {
+ LIBVBUCKET_SOURCE_FILE,
+ LIBVBUCKET_SOURCE_MEMORY
+ } vbucket_source_t;
+
+ /**
+ * Parse a vbucket configuration
+ * @param handle the vbucket config handle to store the result
+ * @param data_source what kind of datasource to parse
+ * @param data A zero terminated string representing the data to parse.
+ * For LIBVBUCKET_SOURCE_FILE this is the file to parse,
+ * for LIBVBUCKET_SOURCE_MEMORY it is the actual JSON body.
+ * @return 0 for success, the appropriate error code otherwise
+ */
+ LIBVBUCKET_PUBLIC_API
+ int vbucket_config_parse(VBUCKET_CONFIG_HANDLE handle,
+ vbucket_source_t data_source,
+ const char *data);
+
+ LIBVBUCKET_PUBLIC_API
+ const char *vbucket_get_error_message(VBUCKET_CONFIG_HANDLE handle);
+
+
+ /**
+ * Create an instance of vbucket config from a file.
+ *
+ * @param filename the vbucket config to parse
+ */
+ LIBVBUCKET_PUBLIC_API
+ VBUCKET_CONFIG_HANDLE vbucket_config_parse_file(const char *filename);
+
+ /**
+ * Create an instance of vbucket config from a string.
+ *
+ * @param data a vbucket config string.
+ */
+ LIBVBUCKET_PUBLIC_API
+ VBUCKET_CONFIG_HANDLE vbucket_config_parse_string(const char *data);
+
+ /**
+ * Destroy a vbucket config.
+ *
+ * @param h the vbucket config handle
+ */
+ LIBVBUCKET_PUBLIC_API
+ void vbucket_config_destroy(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * @}
+ */
+
+ /**
+ * \addtogroup err
+ * @{
+ */
+
+ /**
+ * Get the most recent vbucket error.
+ *
+ * This is currently not threadsafe.
+ */
+ LIBVBUCKET_PUBLIC_API
+ const char *vbucket_get_error(void);
+
+ /**
+ * Tell libvbucket it told you the wrong server ID.
+ *
+ * This will cause libvbucket to do whatever is necessary to try
+ * to figure out a better answer.
+ *
+ * @param h the vbucket config handle.
+ * @param vbucket the vbucket ID
+ * @param wrongserver the incorrect server ID
+ *
+ * @return the correct server ID
+ */
+ LIBVBUCKET_PUBLIC_API
+ int vbucket_found_incorrect_master(VBUCKET_CONFIG_HANDLE h,
+ int vbucket,
+ int wrongserver);
+
+ /**
+ * @}
+ */
+
+ /**
+ * \addtogroup cfg
+ * @{
+ */
+
+ /**
+ * Get the number of replicas configured for each vbucket.
+ */
+ LIBVBUCKET_PUBLIC_API
+ int vbucket_config_get_num_replicas(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * Get the total number of vbuckets;
+ */
+ LIBVBUCKET_PUBLIC_API
+ int vbucket_config_get_num_vbuckets(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * Get the total number of known servers.
+ */
+ LIBVBUCKET_PUBLIC_API
+ int vbucket_config_get_num_servers(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * Get the optional SASL user.
+ *
+ * @return a string or NULL.
+ */
+ LIBVBUCKET_PUBLIC_API
+ const char *vbucket_config_get_user(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * Get the optional SASL password.
+ *
+ * @return a string or NULL.
+ */
+ LIBVBUCKET_PUBLIC_API
+ const char *vbucket_config_get_password(VBUCKET_CONFIG_HANDLE h);
+
+ /**
+ * Get the server at the given index.
+ *
+ * @return a string in the form of hostname:port
+ */
+ LIBVBUCKET_PUBLIC_API
+ const char *