From: Trond Norbye Date: Fri, 25 Sep 2009 09:22:45 +0000 (+0200) Subject: Add an example using innodb as a storage (NOTE: this version currently leaks memorybz... X-Git-Tag: 0.34~17^2~3 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d292bcb63666dad0277e9658916514f64b7200b0;p=m6w6%2Flibmemcached Add an example using innodb as a storage (NOTE: this version currently leaks memorybzr statusbzr status) --- diff --git a/config/config.rpath b/config/config.rpath new file mode 100755 index 00000000..c547c688 --- /dev/null +++ b/config/config.rpath @@ -0,0 +1,666 @@ +#! /bin/sh +# Output a system dependent set of variables, describing how to set the +# run time search path of shared libraries in an executable. +# +# Copyright 1996-2007 Free Software Foundation, Inc. +# Taken from GNU libtool, 2001 +# Originally by Gordon Matzigkeit , 1996 +# +# This file is free software; the Free Software Foundation gives +# unlimited permission to copy and/or distribute it, with or without +# modifications, as long as this notice is preserved. +# +# The first argument passed to this file is the canonical host specification, +# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM +# or +# CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM +# The environment variables CC, GCC, LDFLAGS, LD, with_gnu_ld +# should be set by the caller. +# +# The set of defined variables is at the end of this script. + +# Known limitations: +# - On IRIX 6.5 with CC="cc", the run time search patch must not be longer +# than 256 bytes, otherwise the compiler driver will dump core. The only +# known workaround is to choose shorter directory names for the build +# directory and/or the installation directory. + +# All known linkers require a `.a' archive for static linking (except MSVC, +# which needs '.lib'). +libext=a +shrext=.so + +host="$1" +host_cpu=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'` +host_vendor=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'` +host_os=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'` + +# Code taken from libtool.m4's _LT_CC_BASENAME. + +for cc_temp in $CC""; do + case $cc_temp in + compile | *[\\/]compile | ccache | *[\\/]ccache ) ;; + distcc | *[\\/]distcc | purify | *[\\/]purify ) ;; + \-*) ;; + *) break;; + esac +done +cc_basename=`echo "$cc_temp" | sed -e 's%^.*/%%'` + +# Code taken from libtool.m4's AC_LIBTOOL_PROG_COMPILER_PIC. + +wl= +if test "$GCC" = yes; then + wl='-Wl,' +else + case "$host_os" in + aix*) + wl='-Wl,' + ;; + darwin*) + case $cc_basename in + xlc*) + wl='-Wl,' + ;; + esac + ;; + mingw* | cygwin* | pw32* | os2*) + ;; + hpux9* | hpux10* | hpux11*) + wl='-Wl,' + ;; + irix5* | irix6* | nonstopux*) + wl='-Wl,' + ;; + newsos6) + ;; + linux* | k*bsd*-gnu) + case $cc_basename in + icc* | ecc*) + wl='-Wl,' + ;; + pgcc | pgf77 | pgf90) + wl='-Wl,' + ;; + ccc*) + wl='-Wl,' + ;; + como) + wl='-lopt=' + ;; + *) + case `$CC -V 2>&1 | sed 5q` in + *Sun\ C*) + wl='-Wl,' + ;; + esac + ;; + esac + ;; + osf3* | osf4* | osf5*) + wl='-Wl,' + ;; + rdos*) + ;; + solaris*) + wl='-Wl,' + ;; + sunos4*) + wl='-Qoption ld ' + ;; + sysv4 | sysv4.2uw2* | sysv4.3*) + wl='-Wl,' + ;; + sysv4*MP*) + ;; + sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) + wl='-Wl,' + ;; + unicos*) + wl='-Wl,' + ;; + uts4*) + ;; + esac +fi + +# Code taken from libtool.m4's AC_LIBTOOL_PROG_LD_SHLIBS. + +hardcode_libdir_flag_spec= +hardcode_libdir_separator= +hardcode_direct=no +hardcode_minus_L=no + +case "$host_os" in + cygwin* | mingw* | pw32*) + # FIXME: the MSVC++ port hasn't been tested in a loooong time + # When not using gcc, we currently assume that we are using + # Microsoft Visual C++. + if test "$GCC" != yes; then + with_gnu_ld=no + fi + ;; + interix*) + # we just hope/assume this is gcc and not c89 (= MSVC++) + with_gnu_ld=yes + ;; + openbsd*) + with_gnu_ld=no + ;; +esac + +ld_shlibs=yes +if test "$with_gnu_ld" = yes; then + # Set some defaults for GNU ld with shared library support. These + # are reset later if shared libraries are not supported. Putting them + # here allows them to be overridden if necessary. + # Unlike libtool, we use -rpath here, not --rpath, since the documented + # option of GNU ld is called -rpath, not --rpath. + hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' + case "$host_os" in + aix3* | aix4* | aix5*) + # On AIX/PPC, the GNU linker is very broken + if test "$host_cpu" != ia64; then + ld_shlibs=no + fi + ;; + amigaos*) + hardcode_libdir_flag_spec='-L$libdir' + hardcode_minus_L=yes + # Samuel A. Falvo II reports + # that the semantics of dynamic libraries on AmigaOS, at least up + # to version 4, is to share data among multiple programs linked + # with the same dynamic library. Since this doesn't match the + # behavior of shared libraries on other platforms, we cannot use + # them. + ld_shlibs=no + ;; + beos*) + if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then + : + else + ld_shlibs=no + fi + ;; + cygwin* | mingw* | pw32*) + # hardcode_libdir_flag_spec is actually meaningless, as there is + # no search path for DLLs. + hardcode_libdir_flag_spec='-L$libdir' + if $LD --help 2>&1 | grep 'auto-import' > /dev/null; then + : + else + ld_shlibs=no + fi + ;; + interix[3-9]*) + hardcode_direct=no + hardcode_libdir_flag_spec='${wl}-rpath,$libdir' + ;; + gnu* | linux* | k*bsd*-gnu) + if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then + : + else + ld_shlibs=no + fi + ;; + netbsd*) + ;; + solaris*) + if $LD -v 2>&1 | grep 'BFD 2\.8' > /dev/null; then + ld_shlibs=no + elif $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then + : + else + ld_shlibs=no + fi + ;; + sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) + case `$LD -v 2>&1` in + *\ [01].* | *\ 2.[0-9].* | *\ 2.1[0-5].*) + ld_shlibs=no + ;; + *) + if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then + hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-rpath,$libdir`' + else + ld_shlibs=no + fi + ;; + esac + ;; + sunos4*) + hardcode_direct=yes + ;; + *) + if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then + : + else + ld_shlibs=no + fi + ;; + esac + if test "$ld_shlibs" = no; then + hardcode_libdir_flag_spec= + fi +else + case "$host_os" in + aix3*) + # Note: this linker hardcodes the directories in LIBPATH if there + # are no directories specified by -L. + hardcode_minus_L=yes + if test "$GCC" = yes; then + # Neither direct hardcoding nor static linking is supported with a + # broken collect2. + hardcode_direct=unsupported + fi + ;; + aix4* | aix5*) + if test "$host_cpu" = ia64; then + # On IA64, the linker does run time linking by default, so we don't + # have to do anything special. + aix_use_runtimelinking=no + else + aix_use_runtimelinking=no + # Test if we are trying to use run time linking or normal + # AIX style linking. If -brtl is somewhere in LDFLAGS, we + # need to do runtime linking. + case $host_os in aix4.[23]|aix4.[23].*|aix5*) + for ld_flag in $LDFLAGS; do + if (test $ld_flag = "-brtl" || test $ld_flag = "-Wl,-brtl"); then + aix_use_runtimelinking=yes + break + fi + done + ;; + esac + fi + hardcode_direct=yes + hardcode_libdir_separator=':' + if test "$GCC" = yes; then + case $host_os in aix4.[012]|aix4.[012].*) + collect2name=`${CC} -print-prog-name=collect2` + if test -f "$collect2name" && \ + strings "$collect2name" | grep resolve_lib_name >/dev/null + then + # We have reworked collect2 + : + else + # We have old collect2 + hardcode_direct=unsupported + hardcode_minus_L=yes + hardcode_libdir_flag_spec='-L$libdir' + hardcode_libdir_separator= + fi + ;; + esac + fi + # Begin _LT_AC_SYS_LIBPATH_AIX. + echo 'int main () { return 0; }' > conftest.c + ${CC} ${LDFLAGS} conftest.c -o conftest + aix_libpath=`dump -H conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } +}'` + if test -z "$aix_libpath"; then + aix_libpath=`dump -HX64 conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } +}'` + fi + if test -z "$aix_libpath"; then + aix_libpath="/usr/lib:/lib" + fi + rm -f conftest.c conftest + # End _LT_AC_SYS_LIBPATH_AIX. + if test "$aix_use_runtimelinking" = yes; then + hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" + else + if test "$host_cpu" = ia64; then + hardcode_libdir_flag_spec='${wl}-R $libdir:/usr/lib:/lib' + else + hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" + fi + fi + ;; + amigaos*) + hardcode_libdir_flag_spec='-L$libdir' + hardcode_minus_L=yes + # see comment about different semantics on the GNU ld section + ld_shlibs=no + ;; + bsdi[45]*) + ;; + cygwin* | mingw* | pw32*) + # When not using gcc, we currently assume that we are using + # Microsoft Visual C++. + # hardcode_libdir_flag_spec is actually meaningless, as there is + # no search path for DLLs. + hardcode_libdir_flag_spec=' ' + libext=lib + ;; + darwin* | rhapsody*) + hardcode_direct=no + if test "$GCC" = yes ; then + : + else + case $cc_basename in + xlc*) + ;; + *) + ld_shlibs=no + ;; + esac + fi + ;; + dgux*) + hardcode_libdir_flag_spec='-L$libdir' + ;; + freebsd1*) + ld_shlibs=no + ;; + freebsd2.2*) + hardcode_libdir_flag_spec='-R$libdir' + hardcode_direct=yes + ;; + freebsd2*) + hardcode_direct=yes + hardcode_minus_L=yes + ;; + freebsd* | dragonfly*) + hardcode_libdir_flag_spec='-R$libdir' + hardcode_direct=yes + ;; + hpux9*) + hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' + hardcode_libdir_separator=: + hardcode_direct=yes + # hardcode_minus_L: Not really in the search PATH, + # but as the default location of the library. + hardcode_minus_L=yes + ;; + hpux10*) + if test "$with_gnu_ld" = no; then + hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' + hardcode_libdir_separator=: + hardcode_direct=yes + # hardcode_minus_L: Not really in the search PATH, + # but as the default location of the library. + hardcode_minus_L=yes + fi + ;; + hpux11*) + if test "$with_gnu_ld" = no; then + hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' + hardcode_libdir_separator=: + case $host_cpu in + hppa*64*|ia64*) + hardcode_direct=no + ;; + *) + hardcode_direct=yes + # hardcode_minus_L: Not really in the search PATH, + # but as the default location of the library. + hardcode_minus_L=yes + ;; + esac + fi + ;; + irix5* | irix6* | nonstopux*) + hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' + hardcode_libdir_separator=: + ;; + netbsd*) + hardcode_libdir_flag_spec='-R$libdir' + hardcode_direct=yes + ;; + newsos6) + hardcode_direct=yes + hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' + hardcode_libdir_separator=: + ;; + openbsd*) + if test -f /usr/libexec/ld.so; then + hardcode_direct=yes + if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then + hardcode_libdir_flag_spec='${wl}-rpath,$libdir' + else + case "$host_os" in + openbsd[01].* | openbsd2.[0-7] | openbsd2.[0-7].*) + hardcode_libdir_flag_spec='-R$libdir' + ;; + *) + hardcode_libdir_flag_spec='${wl}-rpath,$libdir' + ;; + esac + fi + else + ld_shlibs=no + fi + ;; + os2*) + hardcode_libdir_flag_spec='-L$libdir' + hardcode_minus_L=yes + ;; + osf3*) + hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' + hardcode_libdir_separator=: + ;; + osf4* | osf5*) + if test "$GCC" = yes; then + hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' + else + # Both cc and cxx compiler support -rpath directly + hardcode_libdir_flag_spec='-rpath $libdir' + fi + hardcode_libdir_separator=: + ;; + solaris*) + hardcode_libdir_flag_spec='-R$libdir' + ;; + sunos4*) + hardcode_libdir_flag_spec='-L$libdir' + hardcode_direct=yes + hardcode_minus_L=yes + ;; + sysv4) + case $host_vendor in + sni) + hardcode_direct=yes # is this really true??? + ;; + siemens) + hardcode_direct=no + ;; + motorola) + hardcode_direct=no #Motorola manual says yes, but my tests say they lie + ;; + esac + ;; + sysv4.3*) + ;; + sysv4*MP*) + if test -d /usr/nec; then + ld_shlibs=yes + fi + ;; + sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[01].[10]* | unixware7* | sco3.2v5.0.[024]*) + ;; + sysv5* | sco3.2v5* | sco5v6*) + hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-R,$libdir`' + hardcode_libdir_separator=':' + ;; + uts4*) + hardcode_libdir_flag_spec='-L$libdir' + ;; + *) + ld_shlibs=no + ;; + esac +fi + +# Check dynamic linker characteristics +# Code taken from libtool.m4's AC_LIBTOOL_SYS_DYNAMIC_LINKER. +# Unlike libtool.m4, here we don't care about _all_ names of the library, but +# only about the one the linker finds when passed -lNAME. This is the last +# element of library_names_spec in libtool.m4, or possibly two of them if the +# linker has special search rules. +library_names_spec= # the last element of library_names_spec in libtool.m4 +libname_spec='lib$name' +case "$host_os" in + aix3*) + library_names_spec='$libname.a' + ;; + aix4* | aix5*) + library_names_spec='$libname$shrext' + ;; + amigaos*) + library_names_spec='$libname.a' + ;; + beos*) + library_names_spec='$libname$shrext' + ;; + bsdi[45]*) + library_names_spec='$libname$shrext' + ;; + cygwin* | mingw* | pw32*) + shrext=.dll + library_names_spec='$libname.dll.a $libname.lib' + ;; + darwin* | rhapsody*) + shrext=.dylib + library_names_spec='$libname$shrext' + ;; + dgux*) + library_names_spec='$libname$shrext' + ;; + freebsd1*) + ;; + freebsd* | dragonfly*) + case "$host_os" in + freebsd[123]*) + library_names_spec='$libname$shrext$versuffix' ;; + *) + library_names_spec='$libname$shrext' ;; + esac + ;; + gnu*) + library_names_spec='$libname$shrext' + ;; + hpux9* | hpux10* | hpux11*) + case $host_cpu in + ia64*) + shrext=.so + ;; + hppa*64*) + shrext=.sl + ;; + *) + shrext=.sl + ;; + esac + library_names_spec='$libname$shrext' + ;; + interix[3-9]*) + library_names_spec='$libname$shrext' + ;; + irix5* | irix6* | nonstopux*) + library_names_spec='$libname$shrext' + case "$host_os" in + irix5* | nonstopux*) + libsuff= shlibsuff= + ;; + *) + case $LD in + *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") libsuff= shlibsuff= ;; + *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") libsuff=32 shlibsuff=N32 ;; + *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") libsuff=64 shlibsuff=64 ;; + *) libsuff= shlibsuff= ;; + esac + ;; + esac + ;; + linux*oldld* | linux*aout* | linux*coff*) + ;; + linux* | k*bsd*-gnu) + library_names_spec='$libname$shrext' + ;; + knetbsd*-gnu) + library_names_spec='$libname$shrext' + ;; + netbsd*) + library_names_spec='$libname$shrext' + ;; + newsos6) + library_names_spec='$libname$shrext' + ;; + nto-qnx*) + library_names_spec='$libname$shrext' + ;; + openbsd*) + library_names_spec='$libname$shrext$versuffix' + ;; + os2*) + libname_spec='$name' + shrext=.dll + library_names_spec='$libname.a' + ;; + osf3* | osf4* | osf5*) + library_names_spec='$libname$shrext' + ;; + rdos*) + ;; + solaris*) + library_names_spec='$libname$shrext' + ;; + sunos4*) + library_names_spec='$libname$shrext$versuffix' + ;; + sysv4 | sysv4.3*) + library_names_spec='$libname$shrext' + ;; + sysv4*MP*) + library_names_spec='$libname$shrext' + ;; + sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) + library_names_spec='$libname$shrext' + ;; + uts4*) + library_names_spec='$libname$shrext' + ;; +esac + +sed_quote_subst='s/\(["`$\\]\)/\\\1/g' +escaped_wl=`echo "X$wl" | sed -e 's/^X//' -e "$sed_quote_subst"` +shlibext=`echo "$shrext" | sed -e 's,^\.,,'` +escaped_libname_spec=`echo "X$libname_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` +escaped_library_names_spec=`echo "X$library_names_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` +escaped_hardcode_libdir_flag_spec=`echo "X$hardcode_libdir_flag_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` + +LC_ALL=C sed -e 's/^\([a-zA-Z0-9_]*\)=/acl_cv_\1=/' <data -= delta; } } - update_cas(item); + + struct item *nitem= create_item(key, keylen, NULL, sizeof(initial), 0, item->exp); + if (item == NULL) + { + rval= PROTOCOL_BINARY_RESPONSE_ENOMEM; + delete_item(key, keylen); + } + else + { + memcpy(nitem->data, item->data, item->size); + delete_item(key, keylen); + put_item(nitem); + item = nitem; + } } response.message.header.response.status= htons(rval); diff --git a/example/interface_v1.c b/example/interface_v1.c index b77a0b61..c5647f84 100644 --- a/example/interface_v1.c +++ b/example/interface_v1.c @@ -105,35 +105,31 @@ static protocol_binary_response_status decrement_handler(const void *cookie, uint64_t *result_cas) { (void)cookie; protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_SUCCESS; + uint64_t val= initial; struct item *item= get_item(key, keylen); - if (item == NULL) + if (item != NULL) { - item= create_item(key, keylen, NULL, sizeof(initial), 0, expiration); - if (item == 0) - { - rval= PROTOCOL_BINARY_RESPONSE_ENOMEM; - } + if (delta > *(uint64_t*)item->data) + val= 0; else - { - memcpy(item->data, &initial, sizeof(initial)); - put_item(item); - *result= initial; - *result_cas= item->cas; - } + val= *(uint64_t*)item->data - delta; + + expiration= (uint32_t)item->exp; + delete_item(key, keylen); + } + + item= create_item(key, keylen, NULL, sizeof(initial), 0, expiration); + if (item == 0) + { + rval= PROTOCOL_BINARY_RESPONSE_ENOMEM; } else { - if (delta > *(uint64_t*)item->data) - { - *(uint64_t*)item->data= 0; - } - else - { - *(uint64_t*)item->data -= delta; - } - *result= (*(uint64_t*)item->data); - /* @todo fix cas */ + memcpy(item->data, &val, sizeof(val)); + put_item(item); + *result= val; + *result_cas= item->cas; } return rval; @@ -198,28 +194,28 @@ static protocol_binary_response_status increment_handler(const void *cookie, uint64_t *result_cas) { (void)cookie; protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_SUCCESS; + uint64_t val= initial; struct item *item= get_item(key, keylen); + if (item != NULL) + { + val= (*(uint64_t*)item->data) + delta; + expiration= (uint32_t)item->exp; + delete_item(key, keylen); + } + + item= create_item(key, keylen, NULL, sizeof(initial), 0, expiration); if (item == NULL) { - item= create_item(key, keylen, NULL, sizeof(initial), 0, expiration); - if (item == 0) - { - rval= PROTOCOL_BINARY_RESPONSE_ENOMEM; - } - else - { - memcpy(item->data, &initial, sizeof(initial)); - put_item(item); - *result= initial; - *result_cas= item->cas; - } + rval= PROTOCOL_BINARY_RESPONSE_ENOMEM; } else { - (*(uint64_t*)item->data) += delta; - *result= (*(uint64_t*)item->data); - update_cas(item); + char buffer[1024] = {0}; + memcpy(buffer, key, keylen); + memcpy(item->data, &val, sizeof(val)); + put_item(item); + *result= val; *result_cas= item->cas; } diff --git a/example/memcached_light.c b/example/memcached_light.c index ff3e4044..743f2d1c 100644 --- a/example/memcached_light.c +++ b/example/memcached_light.c @@ -40,6 +40,7 @@ extern struct memcached_binary_protocol_callback_st interface_v1_impl; static int server_sockets[1024]; static int num_server_sockets= 0; static void* socket_userdata_map[1024]; +static bool verbose= false; /** * Create a socket and bind it to a specific port number @@ -153,25 +154,33 @@ static const char* comcode2str(uint8_t cmd) /** * Print out the command we are about to execute */ -static void pre_execute(const void *cookie, protocol_binary_request_header *header) +static void pre_execute(const void *cookie __attribute__((unused)), + protocol_binary_request_header *header __attribute__((unused))) { - const char *cmd= comcode2str(header->request.opcode); - if (cmd != NULL) - fprintf(stderr, "pre_execute from %p: %s\n", cookie, cmd); - else - fprintf(stderr, "pre_execute from %p: 0x%02x\n", cookie, header->request.opcode); + if (verbose) + { + const char *cmd= comcode2str(header->request.opcode); + if (cmd != NULL) + fprintf(stderr, "pre_execute from %p: %s\n", cookie, cmd); + else + fprintf(stderr, "pre_execute from %p: 0x%02x\n", cookie, header->request.opcode); + } } /** * Print out the command we just executed */ -static void post_execute(const void *cookie, protocol_binary_request_header *header) +static void post_execute(const void *cookie __attribute__((unused)), + protocol_binary_request_header *header __attribute__((unused))) { - const char *cmd= comcode2str(header->request.opcode); - if (cmd != NULL) - fprintf(stderr, "post_execute from %p: %s\n", cookie, cmd); - else - fprintf(stderr, "post_execute from %p: 0x%02x\n", cookie, header->request.opcode); + if (verbose) + { + const char *cmd= comcode2str(header->request.opcode); + if (cmd != NULL) + fprintf(stderr, "post_execute from %p: %s\n", cookie, cmd); + else + fprintf(stderr, "post_execute from %p: 0x%02x\n", cookie, header->request.opcode); + } } /** @@ -211,7 +220,7 @@ int main(int argc, char **argv) int cmd; struct memcached_binary_protocol_callback_st *interface= &interface_v0_impl; - while ((cmd= getopt(argc, argv, "1p:?")) != EOF) + while ((cmd= getopt(argc, argv, "v1p:?")) != EOF) { switch (cmd) { case '1': @@ -221,13 +230,22 @@ int main(int argc, char **argv) port_specified= true; (void)server_socket(optarg); break; + case 'v': + verbose= true; + break; case '?': /* FALLTHROUGH */ default: - (void)fprintf(stderr, "Usage: %s [-p port]\n", argv[0]); + (void)fprintf(stderr, "Usage: %s [-p port] [-v] [-1]\n", argv[0]); return 1; } } + if (!initialize_storage()) + { + /* Error message already printed */ + return 1; + } + if (!port_specified) (void)server_socket("9999"); diff --git a/example/storage.c b/example/storage.c index 18304961..e450774e 100644 --- a/example/storage.c +++ b/example/storage.c @@ -15,36 +15,44 @@ struct list_entry { static struct list_entry *root; static uint64_t cas; +bool initialize_storage(void) { + return true; +} + +void shutdown_storage(void) { + /* Do nothing */ +} + void put_item(struct item* item) { struct list_entry* entry= (void*)item; update_cas(item); - - if (root == NULL) + + if (root == NULL) { entry->next= entry->prev= entry; - } - else + } + else { entry->prev= root->prev; entry->next= root; entry->prev->next= entry; entry->next->prev= entry; } - + root= entry; } struct item* get_item(const void* key, size_t nkey) { struct list_entry *walker= root; - if (root == NULL) + if (root == NULL) { return NULL; - } - - do + } + + do { - if (((struct item*)walker)->nkey == nkey && - memcmp(((struct item*)walker)->key, key, nkey) == 0) + if (((struct item*)walker)->nkey == nkey && + memcmp(((struct item*)walker)->key, key, nkey) == 0) { return (struct item*)walker; } @@ -54,19 +62,19 @@ struct item* get_item(const void* key, size_t nkey) { return NULL; } -struct item* create_item(const void* key, size_t nkey, const void* data, +struct item* create_item(const void* key, size_t nkey, const void* data, size_t size, uint32_t flags, time_t exp) { struct item* ret= calloc(1, sizeof(struct list_entry)); - if (ret != NULL) + if (ret != NULL) { ret->key= malloc(nkey); - if (size > 0) + if (size > 0) { ret->data= malloc(size); } - if (ret->key == NULL || (size > 0 && ret->data == NULL)) + if (ret->key == NULL || (size > 0 && ret->data == NULL)) { free(ret->key); free(ret->data); @@ -75,7 +83,7 @@ struct item* create_item(const void* key, size_t nkey, const void* data, } memcpy(ret->key, key, nkey); - if (data != NULL) + if (data != NULL) { memcpy(ret->data, data, size); } @@ -93,18 +101,18 @@ bool delete_item(const void* key, size_t nkey) { struct item* item= get_item(key, nkey); bool ret= false; - if (item) + if (item) { /* remove from linked list */ struct list_entry *entry= (void*)item; - - if (entry->next == entry) + + if (entry->next == entry) { /* Only one object in the list */ root= NULL; } else - { + { /* ensure that we don't loose track of the root, and this will * change the start position for the next search ;-) */ root= entry->next; @@ -125,17 +133,17 @@ void flush(uint32_t when) { /* FIXME */ (void)when; /* remove the complete linked list */ - if (root == NULL) + if (root == NULL) { return; } root->prev->next= NULL; - while (root != NULL) + while (root != NULL) { struct item* tmp= (void*)root; root= root->next; - + free(tmp->key); free(tmp->data); free(tmp); diff --git a/example/storage.h b/example/storage.h index fbe75474..12955846 100644 --- a/example/storage.h +++ b/example/storage.h @@ -12,12 +12,16 @@ struct item { time_t exp; }; +bool initialize_storage(void); +void shutdown_storage(void); + void update_cas(struct item* item); void put_item(struct item* item); struct item* get_item(const void* key, size_t nkey); -struct item* create_item(const void* key, size_t nkey, const void *data, +struct item* create_item(const void* key, size_t nkey, const void *data, size_t size, uint32_t flags, time_t exp); bool delete_item(const void* key, size_t nkey); void flush(uint32_t when); +void release_item(struct item* item); #endif diff --git a/example/storage_innodb.c b/example/storage_innodb.c new file mode 100644 index 00000000..d2ae6f5a --- /dev/null +++ b/example/storage_innodb.c @@ -0,0 +1,488 @@ +/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "storage.h" + +const char *tablename = "memcached/items"; + +#define key_col_idx 0 +#define data_col_idx 1 +#define flags_col_idx 2 +#define cas_col_idx 3 +#define exp_col_idx 4 + +static uint64_t cas; + +/** + * To avoid cluttering down all the code with error checking I use the + * following macro. It will execute the statement and verify that the + * result of the operation is DB_SUCCESS. If any other error code is + * returned it will print an "assert-like" output and jump to the + * label error_exit. There I release resources before returning out of + * the function. + * + * @param a the expression to execute + * + */ +#define checked(expression) \ +do { \ + ib_err_t checked_err= expression; \ + if (checked_err != DB_SUCCESS) \ + { \ + fprintf(stderr, "ERROR: %s at %u: Failed: <%s>\n\t%s\n", \ + __FILE__, __LINE__, #expression, \ + ib_strerror(checked_err)); \ + goto error_exit; \ + } \ +} while (0); + +/** + * Create the database schema. + * @return true if the database schema was created without any problems + * false otherwise. + */ +static bool create_schema(void) { + ib_tbl_sch_t schema= NULL; + ib_idx_sch_t index= NULL; + + if (ib_database_create("memcached") != IB_TRUE) + { + fprintf(stderr, "Failed to create database\n"); + return false; + } + + ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE); + ib_id_t table_id; + + checked(ib_table_schema_create(tablename, &schema, IB_TBL_COMPACT, 0)); + checked(ib_table_schema_add_col(schema, "key", IB_BLOB, + IB_COL_NOT_NULL, 0, 32767)); + checked(ib_table_schema_add_col(schema, "data", IB_BLOB, + IB_COL_NONE, 0, 1024*1024)); + checked(ib_table_schema_add_col(schema, "flags", IB_INT, + IB_COL_UNSIGNED, 0, 4)); + checked(ib_table_schema_add_col(schema, "cas", IB_INT, + IB_COL_UNSIGNED, 0, 8)); + checked(ib_table_schema_add_col(schema, "exp", IB_INT, + IB_COL_UNSIGNED, 0, 4)); + checked(ib_table_schema_add_index(schema, "PRIMARY_KEY", &index)); + checked(ib_index_schema_add_col(index, "key", 0)); + checked(ib_index_schema_set_clustered(index)); + checked(ib_schema_lock_exclusive(transaction)); + checked(ib_table_create(transaction, schema, &table_id)); + checked(ib_trx_commit(transaction)); + ib_table_schema_delete(schema); + + return true; + + error_exit: + /* @todo release resources! */ + { + ib_err_t error= ib_trx_rollback(transaction); + if (error != DB_SUCCESS) + fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n", + ib_strerror(error)); + } + return false; +} + +/** + * Store an item into the database. Update the CAS id on the item before + * storing it in the database. + * + * @param trx the transaction to use + * @param item the item to store + * @return true if we can go ahead and commit the transaction, false otherwise + */ +bool do_put_item(ib_trx_t trx, struct item* item) { + update_cas(item); + + ib_crsr_t cursor = NULL; + ib_tpl_t tuple= NULL; + bool retval = false; + + checked(ib_cursor_open_table(tablename, trx, &cursor)); + checked(ib_cursor_lock(cursor, IB_LOCK_X)); + tuple= ib_clust_read_tuple_create(cursor); + + checked(ib_col_set_value(tuple, key_col_idx, item->key, item->nkey)); + checked(ib_col_set_value(tuple, data_col_idx, item->data, item->size)); + checked(ib_tuple_write_u32(tuple, flags_col_idx, item->flags)); + checked(ib_tuple_write_u64(tuple, cas_col_idx, item->cas)); + checked(ib_tuple_write_u32(tuple, exp_col_idx, item->exp)); + checked(ib_cursor_insert_row(cursor, tuple)); + + retval= true; + /* Release resources: */ + /* FALLTHROUGH */ + + error_exit: + if (tuple != NULL) + ib_tuple_delete(tuple); + + if (cursor != NULL) + ib_cursor_close(cursor); + + return retval; +} + +/** + * Try to locate an item in the database. Return a cursor and the tuple to + * the item if I found it in the database. + * + * @param trx the transaction to use + * @param key the key of the item to look up + * @param nkey the size of the key + * @param cursor where to store the cursor (OUT) + * @param tuple where to store the tuple (OUT) + * @return true if I found the object, false otherwise + */ +static bool do_locate_item(ib_trx_t trx, + const void* key, + size_t nkey, + ib_crsr_t *cursor) +{ + int res; + ib_tpl_t tuple; + + *cursor = NULL; + + checked(ib_cursor_open_table(tablename, trx, cursor)); + tuple= ib_clust_search_tuple_create(*cursor); + if (tuple == NULL) + { + fprintf(stderr, "Failed to allocate tuple object\n"); + goto error_exit; + } + + checked(ib_col_set_value(tuple, key_col_idx, key, nkey)); + ib_err_t err= ib_cursor_moveto(*cursor, tuple, IB_CUR_GE, &res); + + if (err == DB_SUCCESS && res == 0) + { + ib_tuple_delete(tuple); + return true; + } + else if (err != DB_SUCCESS && + err != DB_RECORD_NOT_FOUND && + err != DB_END_OF_INDEX) + { + fprintf(stderr, "ERROR: ib_cursor_moveto(): %s\n", ib_strerror(err)); + } + /* FALLTHROUGH */ + error_exit: + if (tuple != NULL) + ib_tuple_delete(tuple); + if (*cursor != NULL) + ib_cursor_close(*cursor); + *cursor= NULL; + + return false; +} + +/** + * Try to get an item from the database + * + * @param trx the transaction to use + * @param key the key to get + * @param nkey the lenght of the key + * @return a pointer to the item if I found it in the database + */ +static struct item* do_get_item(ib_trx_t trx, const void* key, size_t nkey) { + ib_crsr_t cursor= NULL; + ib_tpl_t tuple= NULL; + struct item* retval= NULL; + + if (do_locate_item(trx, key, nkey, &cursor)) { + tuple= ib_clust_read_tuple_create(cursor); + if (tuple == NULL) + { + fprintf(stderr, "Failed to create read tuple\n"); + goto error_exit; + } + checked(ib_cursor_read_row(cursor, tuple)); + ib_col_meta_t meta; + ib_ulint_t datalen= ib_col_get_meta(tuple, data_col_idx, &meta); + ib_ulint_t flaglen= ib_col_get_meta(tuple, flags_col_idx, &meta); + ib_ulint_t caslen= ib_col_get_meta(tuple, cas_col_idx, &meta); + ib_ulint_t explen= ib_col_get_meta(tuple, exp_col_idx, &meta); + const void *dataptr= ib_col_get_value(tuple, data_col_idx); + + retval= create_item(key, nkey, dataptr, datalen, 0, 0); + if (retval == NULL) { + fprintf(stderr, "Failed to allocate memory\n"); + goto error_exit; + } + + if (flaglen != 0) { + ib_u32_t val; + checked(ib_tuple_read_u32(tuple, flags_col_idx, &val)); + retval->flags= (uint32_t)val; + } + if (caslen != 0) { + ib_u64_t val; + checked(ib_tuple_read_u64(tuple, cas_col_idx, &val)); + retval->cas= (uint64_t)val; + } + if (explen != 0) { + ib_u32_t val; + checked(ib_tuple_read_u32(tuple, exp_col_idx, &val)); + retval->exp= (uint32_t)val; + } + } + + /* Release resources */ + /* FALLTHROUGH */ + + error_exit: + if (tuple != NULL) + ib_tuple_delete(tuple); + + if (cursor != NULL) + ib_cursor_close(cursor); + + return retval; +} + +/** + * Delete an item from the cache + * @param trx the transaction to use + * @param key the key of the item to delete + * @param nkey the length of the key + * @return true if we should go ahead and commit the transaction + * or false if we should roll back (if the key didn't exists) + */ +static bool do_delete_item(ib_trx_t trx, const void* key, size_t nkey) { + ib_crsr_t cursor= NULL; + bool retval= false; + + if (do_locate_item(trx, key, nkey, &cursor)) + { + checked(ib_cursor_lock(cursor, IB_LOCK_X)); + checked(ib_cursor_delete_row(cursor)); + retval = true; + } + /* Release resources */ + /* FALLTHROUGH */ + + error_exit: + if (cursor != NULL) + ib_cursor_close(cursor); + + return retval; +} + + +/**************************************************************************** + * External interface + ***************************************************************************/ + +/** + * Initialize the database storage + * @return true if the database was initialized successfully, false otherwise + */ +bool initialize_storage(void) { + ib_err_t error; + ib_id_t tid; + + checked(ib_init()); + checked(ib_cfg_set_text("data_home_dir", "/tmp/memcached_light")); + checked(ib_cfg_set_text("log_group_home_dir", "/tmp/memcached_light")); + checked(ib_cfg_set_bool_on("file_per_table")); + checked(ib_startup("barracuda")); + + /* check to see if the table exists or if we should create the schema */ + error= ib_table_get_id(tablename, &tid); + if (error == DB_TABLE_NOT_FOUND) { + if (!create_schema()) { + return false; + } + } else if (error != DB_SUCCESS) { + fprintf(stderr, "Failed to get table id: %s\n", ib_strerror(error)); + return false; + } + + return true; + + error_exit: + return false; +} + +/** + * Shut down this storage engine + */ +void shutdown_storage(void) { + checked(ib_shutdown()); + error_exit: + ; +} + +/** + * Store an item in the databse + * + * @param item the item to store + */ +void put_item(struct item* item) { + ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE); + if (do_put_item(transaction, item)) { + ib_err_t error= ib_trx_commit(transaction); + if (error != DB_SUCCESS) { + fprintf(stderr, "Failed to store key:\n\t%s\n", + ib_strerror(error)); + } + } else { + ib_err_t error= ib_trx_rollback(transaction); + if (error != DB_SUCCESS) + fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n", + ib_strerror(error)); + } +} + +/** + * Get an item from the engine + * @param key the key to grab + * @param nkey number of bytes in the key + * @return pointer to the item if found + */ +struct item* get_item(const void* key, size_t nkey) { + ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE); + struct item* ret= do_get_item(transaction, key, nkey); + ib_err_t error= ib_trx_rollback(transaction); + if (error != DB_SUCCESS) + fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n", + ib_strerror(error)); + + return ret; +} + +/** + * Create an item structure and initialize it with the content + * + * @param key the key for the item + * @param nkey the number of bytes in the key + * @param data pointer to the value for the item (may be NULL) + * @param size the size of the data + * @param flags the flags to store with the data + * @param exp the expiry time for the item + * @return pointer to an initialized item object or NULL if allocation failed + */ +struct item* create_item(const void* key, size_t nkey, const void* data, + size_t size, uint32_t flags, time_t exp) +{ + struct item* ret= calloc(1, sizeof(*ret)); + if (ret != NULL) + { + ret->key= malloc(nkey); + if (size > 0) + { + ret->data= malloc(size); + } + + if (ret->key == NULL || (size > 0 && ret->data == NULL)) + { + free(ret->key); + free(ret->data); + free(ret); + return NULL; + } + + memcpy(ret->key, key, nkey); + if (data != NULL) + { + memcpy(ret->data, data, size); + } + + ret->nkey= nkey; + ret->size= size; + ret->flags= flags; + ret->exp= exp; + } + + return ret; +} + +/** + * Delete an item from the cache + * @param key the key of the item to delete + * @param nkey the length of the key + * @return true if the item was deleted from the cache + */ +bool delete_item(const void* key, size_t nkey) { + ib_trx_t transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ); + + bool ret= do_delete_item(transaction, key, nkey); + + if (ret) + { + /* object found. commit transaction */ + ib_err_t error= ib_trx_commit(transaction); + if (error != DB_SUCCESS) + { + fprintf(stderr, "Failed to delete key:\n\t%s\n", + ib_strerror(error)); + ret= false; + } + } + else + { + ib_err_t error= ib_trx_rollback(transaction); + if (error != DB_SUCCESS) + fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n", + ib_strerror(error)); + } + + return ret; +} + +/** + * Flush the entire cache + * @param when when the cache should be flushed (0 == immediately) + */ +void flush(uint32_t when) { + /* @TODO implement support for when != 0 */ + ib_trx_t transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ); + ib_crsr_t cursor= NULL; + ib_err_t err= DB_SUCCESS; + + checked(ib_cursor_open_table(tablename, transaction, &cursor)); + checked(ib_cursor_first(cursor)); + checked(ib_cursor_lock(cursor, IB_LOCK_X)); + + do { + checked(ib_cursor_delete_row(cursor)); + } while ((err= ib_cursor_next(cursor)) == DB_SUCCESS); + + if (err != DB_END_OF_INDEX) + { + fprintf(stderr, "Failed to flush the cache: %s\n", ib_strerror(err)); + goto error_exit; + } + ib_cursor_close(cursor); + cursor= NULL; + checked(ib_trx_commit(transaction)); + return; + + error_exit: + if (cursor != NULL) + ib_cursor_close(cursor); + + ib_err_t error= ib_trx_rollback(transaction); + if (error != DB_SUCCESS) + fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n", + ib_strerror(error)); +} + +/** + * Update the cas ID in the item structure + * @param item the item to update + */ +void update_cas(struct item* item) { + item->cas= ++cas; +} diff --git a/libmemcached/protocol/protocol_handler.c b/libmemcached/protocol/protocol_handler.c index 67f2b63c..f43788d4 100644 --- a/libmemcached/protocol/protocol_handler.c +++ b/libmemcached/protocol/protocol_handler.c @@ -461,11 +461,6 @@ decrement_command_handler(const void *cookie, uint64_t result; uint64_t cas; - char buffer[1024] = {0}; - memcpy(buffer, key, keylen); - fprintf(stderr, "%s\n", buffer); - - rval= client->root->callback->interface.v1.decrement(cookie, key, keylen, delta, init, timeout, &result, &cas); @@ -681,6 +676,7 @@ increment_command_handler(const void *cookie, .body.value = htonll(result) } }; + rval= response_handler(cookie, header, (void*)&response); } } diff --git a/m4/pandora_have_innodb.m4 b/m4/pandora_have_innodb.m4 new file mode 100644 index 00000000..db50aedd --- /dev/null +++ b/m4/pandora_have_innodb.m4 @@ -0,0 +1,41 @@ +dnl Copyright (C) 2009 Sun Microsystems +dnl This file is free software; Sun Microsystems +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([_PANDORA_SEARCH_LIBINNODB],[ + AC_REQUIRE([AC_LIB_PREFIX]) + + dnl -------------------------------------------------------------------- + dnl Check for libinnodb + dnl -------------------------------------------------------------------- + + AC_ARG_ENABLE([libinnodb], + [AS_HELP_STRING([--disable-libinnodb], + [Build with libinnodb support @<:@default=on@:>@])], + [ac_enable_libinnodb="$enableval"], + [ac_enable_libinnodb="yes"]) + + AS_IF([test "x$ac_enable_libinnodb" = "xyes"],[ + AC_LIB_HAVE_LINKFLAGS(innodb,,[ + #include + ],[ + ib_u64_t + ib_api_version(void); + ]) + ],[ + ac_cv_libinnodb="no" + ]) + + AM_CONDITIONAL(HAVE_LIBINNODB, [test "x${ac_cv_libinnodb}" = "xyes"]) +]) + +AC_DEFUN([PANDORA_HAVE_LIBINNODB],[ + AC_REQUIRE([_PANDORA_SEARCH_LIBINNODB]) +]) + +AC_DEFUN([PANDORA_REQUIRE_LIBINNODB],[ + AC_REQUIRE([PANDORA_HAVE_LIBINNODB]) + AS_IF([test "x${ac_cv_libinnodb}" = "xno"], + AC_MSG_ERROR([libinnodb is required for ${PACKAGE}])) +])