From 4eaad644c57033606736e7f7fc830ed1642410d5 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Tue, 14 Jan 2014 07:21:12 +0100
Subject: [PATCH 01/22] Remove LibRiack dependency

---
 LibRiack | 1 -
 1 file changed, 1 deletion(-)
 delete mode 160000 LibRiack

diff --git a/LibRiack b/LibRiack
deleted file mode 160000
index 542ec11..0000000
--- a/LibRiack
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 542ec11be88bfd30481655e95b51be40db986006

From e9b7dd49c60ef44f8ccce6df34ed4d3633477459 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Tue, 14 Jan 2014 07:22:11 +0100
Subject: [PATCH 02/22] remove git sub

---
 .gitmodules | 3 ---
 1 file changed, 3 deletions(-)
 delete mode 100644 .gitmodules

diff --git a/.gitmodules b/.gitmodules
deleted file mode 100644
index 8a236f9..0000000
--- a/.gitmodules
+++ /dev/null
@@ -1,3 +0,0 @@
-[submodule "LibRiack"]
-	path = LibRiack
-	url = https://github.com/trifork/riack.git 

From 62b1c3ec401fab41ae3cc813b088188fa5fecb3d Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Tue, 21 Jan 2014 13:26:31 +0100
Subject: [PATCH 03/22] Prepared build, fixed pool but still not close to
 compiling

---
 README.md            |  12 +--
 config.m4            | 108 +++++++++++++---------
 php_riak.c           |  62 +++++++++++--
 php_riak.files       |  48 ----------
 php_riak.h           |   2 +-
 php_riak_internal.h  |  21 +++--
 riak/bucket.c        |   2 -
 riak/output/output.c |   2 +-
 riak/pool.c          | 214 +++++++++++++++++++++++--------------------
 riak/pool.h          |  21 +++--
 10 files changed, 267 insertions(+), 225 deletions(-)

diff --git a/README.md b/README.md
index 967a14e..30b2954 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
 Discussion forum: http://ost.io/@php-riak/php_riak  
 Stubs for editor autocompletion at https://github.com/php-riak/php_riak_stub  
 
-# Riak PHP extension
+# Riak PHP extension (Basho C client branch)
 PHP extension for interfacing with Riak.  
 ## Features
 - Protobuffer client
@@ -12,15 +12,11 @@ PHP extension for interfacing with Riak.
 - Riak PHP session store replacement, that can be enabled directly in php.ini no code needed.
 
 ## Building
-First make sure you have the LibRiack submodule checked out, if not do it:
-
-	git submodule init
-	git submodule update
-
-In a terminal make sure you are in project root, then run the following commands
+In a terminal make sure you are in project root, then run the following commands.
+Giving the riak-c path is mandatory at the moment.
 
 	phpize
-	./configure
+        ./configure --with-riak-c-dir=/some/path/riak-c-client/src
 	sudo make install
 
 Module is now installed but it needs to be added to the appropriate php.ini as well, this can be done by adding the following line to your php.ini  
diff --git a/config.m4 b/config.m4
index c1b13be..ab51d09 100644
--- a/config.m4
+++ b/config.m4
@@ -2,12 +2,52 @@ dnl lines starting with "dnl" are comments
 
 PHP_ARG_ENABLE(riak, whether to enable Riak extension, [  --enable-riak   Enable Riak extension])
 PHP_ARG_ENABLE(riak-session, whether to enable riak sessions, [ --disable-riak-session Disable riak session support], yes, no)
-if test "$PHP_RIAK" != "no"; then
-  if test "$PHP_RIAK_SESSION" != "no"; then
-    AC_DEFINE(PHP_RIAK_SESSION,1,[riak session])
+
+dnl
+dnl Checks for the configure options
+dnl
+
+if test -z "$PHP_RIAK_C_DIR"; then
+  PHP_ARG_WITH(riak-c-dir, for the location of Riak C,
+  [  --with-riak-c-dir[=DIR]     Set the path to riak-c install prefix.], no, no)
+fi
+
+AC_DEFUN(RIAK_C_LIB,[
+  if test "$PHP_RIAK_C_DIR" != "no"; then
+    LIB_RIAK_C_NAME=riak_c_client-0.1
+
+    for i in $PHP_RIAK_C_DIR /usr/local /usr; do
+      if test -f "$i/include/riak.h"; then
+        PHP_RIAK_C_DIR=$i
+        break;
+      fi
+    done
+
+    PHP_CHECK_LIBRARY($LIB_RIAK_C_NAME, riak_ping,
+      [
+        PHP_ADD_INCLUDE($PHP_RIAK_C_DIR)
+        PHP_ADD_INCLUDE($PHP_RIAK_C_DIR/include)
+        PHP_ADD_LIBRARY_WITH_PATH($LIB_RIAK_C_NAME, $PHP_RIAK_C_DIR/lib, RIAK_SHARED_LIBADD)
+      ],[
+        AC_MSG_ERROR([Problem with $LIB_RIAK_C_NAME.(a|so). Please check config.log for more information.])
+      ],[
+        -L$PHP_RIAK_C_DIR/lib
+      ])
+  else
+    AC_MSG_RESULT([If configure fails try --with-riak-c-dir=<DIR>])
   fi
+])
+
+if test "$PHP_RIAK_SESSION" != "no"; then
+  AC_DEFINE(PHP_RIAK_SESSION,1,[riak session])
+fi
+
+dnl
+dnl this defines the extension
+dnl
+if test "$PHP_RIAK" != "no"; then
+  RIAK_C_LIB
 
-  dnl this defines the extension
   PHP_NEW_EXTENSION(riak, php_riak.c \
     ht_utils.c \
     riak_session.c \
@@ -61,48 +101,27 @@ if test "$PHP_RIAK" != "no"; then
     riak/map_reduce/functions/base_function.c \
     riak/map_reduce/functions/javascript_function.c \
     riak/map_reduce/functions/erlang_function.c \
-    LibRiack/src/riack_sock.c \
-    LibRiack/src/riack.c \
-    LibRiack/src/riack_kv.c \
-    LibRiack/src/riack_search.c \
-    LibRiack/src/riack_msg.c \
-    LibRiack/src/riack_mem.c \
-    LibRiack/src/riack_helpers.c \
-    LibRiack/src/protocol/riak_msg_codes.c \
-    LibRiack/src/protocol/riak_search.pb-c.c \
-    LibRiack/src/protocol/riak.pb-c.c \
-    LibRiack/src/protocol/riak_kv.pb-c.c \
-    LibRiack/src/google/protobuf-c/protobuf-c.c, $ext_shared)
+    , $ext_shared)
 
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/crdt], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/crdt/input], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/exception], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/input], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/output], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/property], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/property/replication_mode], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/functions], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/input], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/output], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/phase], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/search], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/query], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/search/input], 1)
-    PHP_ADD_BUILD_DIR([$ext_builddir/riak/search/output], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/crdt], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/crdt/input], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/exception], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/input], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/output], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/property], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/property/replication_mode], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/functions], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/input], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/output], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/map_reduce/phase], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/search], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/query], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/search/input], 1)
+  PHP_ADD_BUILD_DIR([$ext_builddir/riak/search/output], 1)
 
-  PHP_ADD_BUILD_DIR([$ext_builddir/LibRiack/src], 1)
-  PHP_ADD_INCLUDE([$ext_builddir/LibRiack/src])
-  PHP_ADD_INCLUDE([$ext_srcdir/LibRiack/src])
-
-  PHP_ADD_BUILD_DIR([$ext_builddir/LibRiack/src/protocol], 1)
-  PHP_ADD_INCLUDE([$ext_builddir/LibRiack/src/protocol])
-  PHP_ADD_INCLUDE([$ext_srcdir/LibRiack/src/protocol])
-
-  PHP_ADD_BUILD_DIR([$ext_builddir/LibRiack/src/google/protobuf-c], 1)
-  PHP_ADD_INCLUDE([$ext_builddir/LibRiack/src/google/protobuf-c])
-  PHP_ADD_INCLUDE([$ext_srcdir/LibRiack/src/google/protobuf-c])
+  PHP_SUBST(RIAK_SHARED_LIBADD)
 
   dnl this is boilerplate to make the extension work on OS X
   case $build_os in
@@ -117,4 +136,5 @@ if test "$PHP_RIAK" != "no"; then
     AC_MSG_RESULT([yes])
     ;;
   esac
+
 fi
diff --git a/php_riak.c b/php_riak.c
index 69f0450..30e6144 100644
--- a/php_riak.c
+++ b/php_riak.c
@@ -223,9 +223,13 @@ PHP_GSHUTDOWN_FUNCTION(riak) /* {{{ */
 #endif
 }
 /* {{{ */
-void riak_throw_exception(struct RIACK_CLIENT* client, int errorStatus TSRMLS_DC)/* {{{ */
+void riak_throw_exception(riak_connection* cnx, riak_error* err TSRMLS_DC)/* {{{ */
 {
-    if (errorStatus == RIACK_ERROR_COMMUNICATION) {
+    // TODO Fix for basho branch
+    // For now just throw a random communication exception
+    zend_throw_exception(riak_communication_exception_ce, "Unknown error", 1001 TSRMLS_CC);
+
+/*    if (errorStatus == RIACK_ERROR_COMMUNICATION) {
         zend_throw_exception(riak_communication_exception_ce, "Communication error", 1001 TSRMLS_CC);
     } else if (errorStatus == RIACK_ERROR_RESPONSE) {
         if (client->last_error) {
@@ -233,7 +237,7 @@ void riak_throw_exception(struct RIACK_CLIENT* client, int errorStatus TSRMLS_DC
         } else {
             zend_throw_exception(riak_response_exception_ce, "Unexpected response from riak", 1002 TSRMLS_CC);
         }
-    }
+    }*/
 }
 /* }}} */
 
@@ -241,17 +245,21 @@ void riak_throw_exception(struct RIACK_CLIENT* client, int errorStatus TSRMLS_DC
  * Riack allocator
 */
 
-void *riack_php_alloc(void* ptr, size_t size)/* {{{ */
+void *riak_c_alloc(size_t size)/* {{{ */
 {
     if (size == 0) {
         return 0;
     }
-
     return pemalloc(size, 0);
 }
 /* }}} */
 
-void riack_php_free(void *ptr, void *data)/* {{{ */
+void *riak_c_realloc(void* data, size_t size) {/* {{{ */
+    return perealloc(data, size, 0);
+}
+/* }}} */
+
+void *riak_c_free(void* data)/* {{{ */
 {
     if (data) {
         pefree(data, 0);
@@ -259,17 +267,55 @@ void riack_php_free(void *ptr, void *data)/* {{{ */
 }
 /* }}} */
 
-void *riack_php_persistent_alloc(void *ptr, size_t size)/* {{{ */
+void *riak_c_persistent_alloc(size_t size)/* {{{ */
+{
+    if (size == 0) {
+        return 0;
+    }
+    return pemalloc(size, 1);
+}
+/* }}} */
+
+void *riak_c_persistent_realloc(void* data, size_t size) {/* {{{ */
+    return perealloc(data, size, 1);
+}
+/* }}} */
+
+void *riak_c_persistent_free(void* data)/* {{{ */
+{
+    if (data) {
+        pefree(data, 1);
+    }
+}
+/* }}} */
+
+void *riak_c_pb_alloc(void* ptr, size_t size)/* {{{ */
 {
     if (size == 0) {
         return 0;
     }
+    return pemalloc(size, 0);
+}
+/* }}} */
 
+void riak_c_pb_free(void *ptr, void *data)/* {{{ */
+{
+    if (data) {
+        pefree(data, 0);
+    }
+}
+/* }}} */
+
+void *riak_c_pb_persistent_alloc(void *ptr, size_t size)/* {{{ */
+{
+    if (size == 0) {
+        return 0;
+    }
     return pemalloc(size, 1);
 }
 /* }}} */
 
-void riack_php_persistent_free (void *ptr, void *data)/* {{{ */
+void riak_c_pb_persistent_free (void *ptr, void *data)/* {{{ */
 {
     if (data) {
         pefree(data, 1);
diff --git a/php_riak.files b/php_riak.files
index abdba8f..59e4741 100644
--- a/php_riak.files
+++ b/php_riak.files
@@ -1,51 +1,3 @@
-LibRiack/src/cmake/Modules/FindProtoBufC.cmake
-LibRiack/src/protocol/riak.pb-c.c
-LibRiack/src/protocol/riak.pb-c.h
-LibRiack/src/protocol/riak.proto
-LibRiack/src/protocol/riak_kv.pb-c.c
-LibRiack/src/protocol/riak_kv.pb-c.h
-LibRiack/src/protocol/riak_kv.proto
-LibRiack/src/protocol/riak_msg_codes.c
-LibRiack/src/protocol/riak_msg_codes.h
-LibRiack/src/protocol/riak_search.pb-c.c
-LibRiack/src/protocol/riak_search.pb-c.h
-LibRiack/src/protocol/riak_search.proto
-LibRiack/src/tests/test.c
-LibRiack/src/tests/test.h
-LibRiack/src/tests/test_2i.c
-LibRiack/src/tests/test_bucket.c
-LibRiack/src/tests/test_clientid.c
-LibRiack/src/tests/test_delete.c
-LibRiack/src/tests/test_get_put.c
-LibRiack/src/tests/test_load.c
-LibRiack/src/tests/test_mapred.c
-LibRiack/src/tests/test_mem.c
-LibRiack/src/tests/test_meta_links.c
-LibRiack/src/tests/test_misc.c
-LibRiack/src/tests/test_ping.c
-LibRiack/src/CMakeLists.txt
-LibRiack/src/riack.c
-LibRiack/src/riack.h
-LibRiack/src/riack_defines.h
-LibRiack/src/riack_helpers.c
-LibRiack/src/riack_helpers.h
-LibRiack/src/riack_kv.c
-LibRiack/src/riack_mem.c
-LibRiack/src/riack_msg.c
-LibRiack/src/riack_msg.h
-LibRiack/src/riack_sock.c
-LibRiack/src/riack_sock.h
-LibRiack/testdata/make_c_friendly.php
-LibRiack/cmake_install.cmake
-LibRiack/CMakeCache.txt
-LibRiack/CTestTestfile.cmake
-LibRiack/install_manifest.txt
-LibRiack/README.md
-LibRiack/src/google/protobuf-c/protobuf-c.h
-LibRiack/src/google/protobuf-c/protobuf-c-private.h
-LibRiack/src/google/protobuf-c/protobuf-c.c
-LibRiack/src/riack_search.c
-LibRiack/src/ints.h
 script/build_module.sh
 tests/bucket_props.phpt
 tests/client_ctor.phpt
diff --git a/php_riak.h b/php_riak.h
index 7421c2e..023ff65 100644
--- a/php_riak.h
+++ b/php_riak.h
@@ -23,7 +23,7 @@
 #endif
 
 #define PHP_RIAK_EXTNAME "riak"
-#define PHP_RIAK_VERSION "1.1.3"
+#define PHP_RIAK_VERSION "1.2.0"
 
 PHP_MINIT_FUNCTION(riak);
 PHP_MSHUTDOWN_FUNCTION(riak);
diff --git a/php_riak_internal.h b/php_riak_internal.h
index e078589..18b7fb5 100644
--- a/php_riak_internal.h
+++ b/php_riak_internal.h
@@ -24,7 +24,8 @@
 #endif
 
 #include <php.h>
-#include <riack.h>
+#include <riak.h>
+#include <riak_config.h>
 
 /*************************************************
 * Macros
@@ -140,7 +141,6 @@
 # define RIAK_GLOBAL(v) (riak_globals.v)
 #endif
 
-
 /*************************************************
 * Vars
 *************************************************/
@@ -173,12 +173,19 @@ ZEND_EXTERN_MODULE_GLOBALS(riak)
 * Functions
 *************************************************/
 
-void *riack_php_alloc(void *allocator_data, size_t size);
-void riack_php_free (void *allocator_data, void *data);
+void *riak_c_alloc(size_t size);
+void *riak_c_realloc(void* data, size_t size);
+void *riak_c_free(void* data);
+void *riak_c_persistent_alloc(size_t size);
+void *riak_c_persistent_realloc(void* data, size_t size);
+void *riak_c_persistent_free(void* data);
+
+void *riak_c_pb_alloc(void *allocator_data, size_t size);
+void riak_c_pb_free (void *allocator_data, void *data);
 
-void *riack_php_persistent_alloc(void *allocator_data, size_t size);
-void riack_php_persistent_free (void *allocator_data, void *data);
+void *riak_c_pb_persistent_alloc(void *allocator_data, size_t size);
+void riak_c_pb_persistent_free (void *allocator_data, void *data);
 
-void riak_throw_exception(struct RIACK_CLIENT* client, int errorStatus TSRMLS_DC);
+void riak_throw_exception(riak_connection* cnx, riak_error* err TSRMLS_DC);
 
 #endif // PHP_RIAK_INTERNAL_H
diff --git a/riak/bucket.c b/riak/bucket.c
index 5eb92bd..99ce751 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -406,7 +406,6 @@ PHP_METHOD(RiakBucket, indexQuery)
     if (RSTR_HAS_CONTENT(continuation)) {
         RSTR_SAFE_FREE(connection->client, continuation);
     }
-
     RETURN_ZVAL(zresult, 0, 1);
 }
 /* }}} */
@@ -438,7 +437,6 @@ void riak_set_commit_hooks_properties(riak_connection *connection, zval *zhook_l
         zval *ziter, *zcount;
         MAKE_STD_ZVAL(zcount);
         RIAK_CALL_METHOD(RiakCommitHookList, count, zcount, zhook_list)
-
         MAKE_STD_ZVAL(ziter);
         object_init(ziter);
         RIAK_CALL_METHOD(RiakCommitHookList, getIterator, ziter, zhook_list)
diff --git a/riak/output/output.c b/riak/output/output.c
index 7cb8ca4..3f90e66 100644
--- a/riak/output/output.c
+++ b/riak/output/output.c
@@ -187,7 +187,7 @@ PHP_METHOD(Riak_Output_Output, getFirstObject)
 Retrieves a unique riak object */
 PHP_METHOD(Riak_Output_Output, getObject)
 {
-    zval *zCount, *zResolver, *zObjectList, *zObject, *zBucket, *zVClock, *zPutInput, *zOutput, *zBool;
+    zval *zCount, *zResolver, *zObjectList, *zObject;
 
     zObjectList = zend_read_property(riak_output_ce, getThis(), "objectList", sizeof("objectList")-1, 1 TSRMLS_CC);
     if (Z_TYPE_P(zObjectList) != IS_OBJECT) {
diff --git a/riak/pool.c b/riak/pool.c
index 1a39c68..ab5d4c4 100644
--- a/riak/pool.c
+++ b/riak/pool.c
@@ -15,46 +15,59 @@
    limitations under the License.
 */
 #include "pool.h"
+#include <string.h>
 
-zend_bool ensure_connected(riak_connection *connection TSRMLS_DC) /* {{{ */
+zend_bool ensure_connected(riak_context *context TSRMLS_DC) /* {{{ */
 {
   zend_bool result = 1;
-  if (difftime(time(NULL), connection->last_used_at) > RIAK_GLOBAL(persistent_timeout)) {
-      connection->needs_reconnect = 1;
+  if (difftime(time(NULL), context->last_used_at) > RIAK_GLOBAL(persistent_timeout)) {
+      context->needs_reconnect = 1;
    }
-   if (connection->needs_reconnect) {
-      if (riack_reconnect(connection->client) == RIACK_SUCCESS) {
-         connection->needs_reconnect = 0;
-      } else {
-         result = 0;
-      }
+   if (context->needs_reconnect) {
+       if (context->connection) {
+           riak_connection_free(&(context->connection));
+           context->connection = 0;
+       }
+       char szport[20];
+       sprintf(szport, "%d", context->port);
+       riak_error err = riak_connection_new(context->config,
+                                            context->connection,
+                                            context->szhost, szport, NULL);
+       if (err = ERIAK_OK) {
+           connection->needs_reconnect = 0;
+       } else {
+           result = 0;
+       }
       RIAK_GLOBAL(reconnects)++;
    }
    return result;
 }
 /* }}} */
 
-zend_bool ensure_connected_init(riak_connection *connection, char* host, int host_len, int port TSRMLS_DC) /* {{{ */
+zend_bool ensure_connected_init(riak_context *context, char* host, int host_len, int port TSRMLS_DC) /* {{{ */
 {
     char *szHost;
     zend_bool result;
     result = 0;
 
-    if (connection->client->sockfd <= 0) {
-        struct RIACK_CONNECTION_OPTIONS options;
+    if (!context->szhost ||
+            strncmp(context->szhost, host, host_len) != 0 ||
+            context->port != port) {
+        riak_c_persistent_free(context->szhost);
+        char *szhost;
+        // to be on the safe side make a copy with zero termination
+        context->szhost = riak_c_persistent_alloc(host_len+1);
+        memcpy(context->szhost, host, host_len);
+        szhost[host_len] = 0;
+        context->port = port;
+        connection->needs_reconnect = 1;
+    }
+    result = ensure_connected(context TSRMLS_CC);
+/* TODO
         options.send_timeout_ms = RIAK_GLOBAL(send_timeout);
         options.recv_timeout_ms = RIAK_GLOBAL(recv_timeout);
         options.keep_alive_enabled = RIAK_GLOBAL(keep_alive);
-
-        szHost = pestrndup(host, host_len, 0);
-        if (riack_connect(connection->client, szHost, port, &options) == RIACK_SUCCESS) {
-            connection->needs_reconnect = 0;
-            result = 1;
-        }
-        pefree(szHost, 0);
-    } else {
-        result = ensure_connected(connection TSRMLS_CC);
-    }
+*/
     return result;
 }
 /* }}} */
@@ -90,23 +103,24 @@ void unlock_pool(TSRMLS_D) /* {{{ */
 }
 /* }}} */
 
-void release_connection(riak_connection *connection TSRMLS_DC) /* {{{ */
+void release_connection(riak_context *context TSRMLS_DC) /* {{{ */
 {
    riak_connection_pool* pool = NULL;
    RIAK_GLOBAL(open_connections)--;
-   if (connection->persistent) {
+   if (context->persistent) {
       /* If we fail to lock we might have a stuck client, find a way to deal with this. */
       if (lock_pool(TSRMLS_C)) {
-         connection->last_used_at = time(NULL);
+         context->last_used_at = time(NULL);
          RIAK_GLOBAL(open_connections_persistent)--;
-         pool = pool_for_host_port(connection->client->host, 
-            strlen(connection->client->host), connection->client->port TSRMLS_CC);
-         release_connection_from_pool(pool, connection);
+         pool = pool_for_host_port(context->host,
+            strlen(context->szhost), context->port TSRMLS_CC);
+         release_connection_from_pool(pool, context);
          unlock_pool(TSRMLS_C);
       }
    } else {
-      if (connection->client) {
-         riack_free(connection->client);
+      if (context->connection) {
+          riak_connection_free(&(context->connection));
+          context->connection = 0;
       }
       pefree(connection, 0);
    }
@@ -115,7 +129,7 @@ void release_connection(riak_connection *connection TSRMLS_DC) /* {{{ */
 
 riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /* {{{ */
 {
-   riak_connection* connection;
+   riak_context* context;
    riak_connection_pool* pool;
    riak_connection_pool_entry *entry = NULL;
 
@@ -125,8 +139,8 @@ riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /
       unlock_pool(TSRMLS_C);
    }
    if (entry) {
-      connection = &entry->connection;
-      if (!ensure_connected_init(connection, host, host_len, port TSRMLS_CC)) {
+      context = &entry->context;
+      if (!ensure_connected_init(context, host, host_len, port TSRMLS_CC)) {
          connection->needs_reconnect = 1;
          release_connection_from_pool(pool, connection);
          return NULL;
@@ -134,13 +148,12 @@ riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /
       RIAK_GLOBAL(open_connections_persistent)++;
    } else {
       /* We could not get a persistent connection, make a new non persistent connection. */
-      connection = pemalloc(sizeof(riak_connection), 0);
-      memset(connection, 0, sizeof(riak_connection));
-      connection->persistent = 0;
-      connection->client = riack_new_client(&riack_php_allocator);
-      connection->last_used_at = time(NULL);
-      if (!ensure_connected_init(connection, host, host_len, port TSRMLS_CC)) {
-         release_connection(connection TSRMLS_CC);
+      context = pemalloc(sizeof(riak_context), 0);
+      memset(context, 0, sizeof(riak_context));
+      context->persistent = 0;
+      context->last_used_at = time(NULL);
+      if (!ensure_connected_init(context, host, host_len, port TSRMLS_CC)) {
+         release_connection(context TSRMLS_CC);
          return NULL;
       }
    }
@@ -153,86 +166,91 @@ riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /
 
 void release_connection_from_pool(riak_connection_pool* pool, riak_connection *connection) /* {{{ */
 {
-  int i;
-  riak_connection_pool_entry* current_entry;
-  for (i=0; i<pool->count; ++i) {
-    current_entry = &pool->entries[i];
-    if (&(current_entry->connection) == connection) {
-      current_entry->in_use = 0;
+    int i;
+    riak_connection_pool_entry* current_entry;
+    for (i=0; i<pool->count; ++i) {
+        current_entry = &pool->entries[i];
+        if (&(current_entry->connection) == connection) {
+            current_entry->in_use = 0;
+        }
     }
-  }
-  /* Not found we should warn about this. */
+    /* Not found we should warn about this. */
 }
 /* }}} */
 
 riak_connection_pool_entry *take_connection_entry_from_pool(riak_connection_pool *pool) /* {{{ */
 {
-  int i;
-  riak_connection_pool_entry* current_entry;
-  for (i=0; i<pool->count; ++i) {
-    current_entry = &pool->entries[i];
-    if (!current_entry->in_use) {
-      current_entry->in_use = 1;
-      current_entry->connection.persistent = 1;
-      if (!current_entry->connection.client) {
-         current_entry->connection.last_used_at = time(NULL);
-         current_entry->connection.client = riack_new_client(&riack_php_persistent_allocator);
-      }
-      return current_entry;
+    int i;
+    riak_connection_pool_entry* current_entry;
+    for (i=0; i<pool->count; ++i) {
+        current_entry = &pool->entries[i];
+        if (!current_entry->in_use) {
+            current_entry->in_use = 1;
+            current_entry->context.persistent = 1;
+            if (!current_entry->context.config) {
+                riak_config_new(&current_entry->context.config,
+                                riak_c_persistent_alloc,
+                                riak_c_persistent_realloc,
+                                riak_c_persistent_free,
+                                riak_c_pb_persistent_alloc,
+                                riak_c_pb_persistent_free);
+            }
+            return current_entry;
+        }
     }
-  }
-  return NULL;
+    return NULL;
 }
 /* }}} */
 
 riak_connection_pool *pool_for_host_port(char* host, int host_len, int port TSRMLS_DC) /* {{{ */
 {
-  char *szHost;
-  char szConnection[512];
-  zend_rsrc_list_entry *le;
-  riak_connection_pool *pool;
-  zend_rsrc_list_entry nle;
-
-  szHost = pestrndup(host, host_len, 0);
-  snprintf(szConnection, sizeof(szConnection), "%s:%d", szHost, port );
-  pefree(szHost, 0);
-
-  if (zend_hash_find(&EG(persistent_list), szConnection, strlen(szConnection)+1, (void**)&le) == FAILURE) {
-    pool = initialize_pool(TSRMLS_C);
-    nle.ptr = pool;
-    nle.type = le_riak_connection_list;
-    nle.refcount = 1;
-    zend_hash_update(&EG(persistent_list), szConnection, strlen(szConnection)+1, (void*)&nle, sizeof(zend_rsrc_list_entry), NULL);
-  } else {
-    pool = (riak_connection_pool*)le->ptr;
-  }
-  return pool;
+    char *szHost;
+    char szConnection[512];
+    zend_rsrc_list_entry *le;
+    riak_connection_pool *pool;
+    zend_rsrc_list_entry nle;
+    szHost = pestrndup(host, host_len, 0);
+    snprintf(szConnection, sizeof(szConnection), "%s:%d", szHost, port );
+    pefree(szHost, 0);
+    if (zend_hash_find(&EG(persistent_list), szConnection, strlen(szConnection)+1, (void**)&le) == FAILURE) {
+        pool = initialize_pool(TSRMLS_C);
+        nle.ptr = pool;
+        nle.type = le_riak_connection_list;
+        nle.refcount = 1;
+        zend_hash_update(&EG(persistent_list), szConnection, strlen(szConnection)+1, (void*)&nle, sizeof(zend_rsrc_list_entry), NULL);
+    } else {
+        pool = (riak_connection_pool*)le->ptr;
+    }
+    return pool;
 }
 /* }}} */
 
 riak_connection_pool* initialize_pool(TSRMLS_D) /* {{{ */
 {
-  riak_connection_pool* pool;
-  pool = pemalloc(sizeof(riak_connection_pool), 1);
-  pool->count = RIAK_GLOBAL(persistent_connections);
-  pool->entries = pemalloc(pool->count * sizeof(riak_connection_pool_entry), 1);
-  memset(pool->entries, 0, pool->count * sizeof(riak_connection_pool_entry));
-  return pool;
+    riak_connection_pool* pool;
+    pool = pemalloc(sizeof(riak_connection_pool), 1);
+    pool->count = RIAK_GLOBAL(persistent_connections);
+    pool->entries = pemalloc(pool->count * sizeof(riak_connection_pool_entry), 1);
+    memset(pool->entries, 0, pool->count * sizeof(riak_connection_pool_entry));
+    return pool;
 }
 /* }}} */
 
 void le_riak_connections_pefree(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
 {
-  int i;
-  riak_connection_pool *pool = (riak_connection_pool*)rsrc->ptr;
-  if (pool->entries) {
-    for (i=0; i<pool->count; ++i) {
-      if (pool->entries[i].connection.client) {
-        riack_free(pool->entries[i].connection.client);
-      }
+    int i;
+    riak_connection_pool *pool = (riak_connection_pool*)rsrc->ptr;
+    if (pool->entries) {
+        for (i=0; i<pool->count; ++i) {
+            if (pool->entries[i].context.client) {
+                riak_connection_free(&(pool->entries[i].context.connection));
+            }
+            if (pool->entries[i].context.config) {
+                riak_config_free(&(pool->entries[i].context.config));
+            }
+        }
+        pefree(pool->entries, 1);
     }
-    pefree(pool->entries, 1);
-  }
-  pefree(pool, 1);
+    pefree(pool, 1);
 }
 /* }}} */
diff --git a/riak/pool.h b/riak/pool.h
index b6979cd..bb6ea3d 100644
--- a/riak/pool.h
+++ b/riak/pool.h
@@ -22,16 +22,21 @@
 #include <php.h>
 #include <time.h>
 
-typedef struct _riak_connection {
-  struct RIACK_CLIENT *client;
+typedef struct {
+  riak_connection* connection;
+  riak_config* config;
+
+  char* szhost;
+  int port;
+
   zend_bool needs_reconnect;
   zend_bool persistent;
   time_t last_used_at;
-} riak_connection;
+} riak_context;
 
 typedef struct _riak_connection_pool_entry {
   zend_bool in_use;
-  riak_connection connection;
+  riak_context context;
 } riak_connection_pool_entry;
 
 typedef struct _riak_connection_pool {
@@ -39,11 +44,11 @@ typedef struct _riak_connection_pool {
   riak_connection_pool_entry *entries;
 } riak_connection_pool;
 
-zend_bool ensure_connected(riak_connection *connection TSRMLS_DC);
-zend_bool ensure_connected_init(riak_connection *connection, char* host, int host_len, int port TSRMLS_DC);
-void mark_for_reconnect(riak_connection *connection);
+zend_bool ensure_connected(riak_context *context TSRMLS_DC);
+zend_bool ensure_connected_init(riak_context *context, char* host, int host_len, int port TSRMLS_DC);
+void mark_for_reconnect(riak_context *context);
 
-void release_connection(riak_connection *connection TSRMLS_DC);
+void release_connection(riak_context *context TSRMLS_DC);
 riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC);
 
 void le_riak_connections_pefree(zend_rsrc_list_entry *rsrc TSRMLS_DC);

From 541ca81159be63816dbae32340face194befd44b Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Tue, 21 Jan 2014 15:28:35 +0100
Subject: [PATCH 04/22] Delete ported

---
 README.md                            |   7 +-
 php_riak.c                           |  21 +--
 php_riak_internal.h                  |  12 +-
 riak/bucket.c                        | 208 +++++++++++++++------------
 riak/bucket.h                        |   7 +-
 riak/connection.c                    |  21 ++-
 riak/connection.h                    |   4 +-
 riak/crdt/counter.c                  |  22 +--
 riak/map_reduce/output/output.c      |  10 +-
 riak/map_reduce/output/output.h      |   2 +-
 riak/object.c                        | 123 ++++++++--------
 riak/object.h                        |   6 +-
 riak/output/get_output.c             |   6 +-
 riak/output/get_output.h             |   2 +-
 riak/output/index_output.c           |   7 +-
 riak/output/index_output.h           |   2 +-
 riak/output/output.c                 |   8 +-
 riak/output/output.h                 |   2 +-
 riak/output/put_output.c             |   6 +-
 riak/output/put_output.h             |   2 +-
 riak/pool.c                          |  60 ++++----
 riak/pool.h                          |   6 +-
 riak/search/output/document_output.c |  54 +++----
 riak/search/output/document_output.h |   2 +-
 riak/search/search.c                 |  23 +--
 riak/search/search.h                 |   8 +-
 26 files changed, 330 insertions(+), 301 deletions(-)

diff --git a/README.md b/README.md
index 30b2954..ef6aaa2 100644
--- a/README.md
+++ b/README.md
@@ -12,11 +12,12 @@ PHP extension for interfacing with Riak.
 - Riak PHP session store replacement, that can be enabled directly in php.ini no code needed.
 
 ## Building
-In a terminal make sure you are in project root, then run the following commands.
-Giving the riak-c path is mandatory at the moment.
+Download and compile riak-c-client from here https://github.com/basho/riak-c-client.  
+In a terminal make sure you are in project root, then run the following commands.  
+Giving the riak-c path is mandatory at the moment.  
 
 	phpize
-        ./configure --with-riak-c-dir=/some/path/riak-c-client/src
+	./configure --with-riak-c-dir=/some/path/riak-c-client/src
 	sudo make install
 
 Module is now installed but it needs to be added to the appropriate php.ini as well, this can be done by adding the following line to your php.ini  
diff --git a/php_riak.c b/php_riak.c
index 30e6144..6fa08c2 100644
--- a/php_riak.c
+++ b/php_riak.c
@@ -83,18 +83,6 @@ zend_function_entry riak_functions[] = {
   { NULL, NULL, NULL }
 };
 
-struct RIACK_ALLOCATOR riack_php_persistent_allocator = 
-{
-  riack_php_persistent_alloc,
-  riack_php_persistent_free,
-};
-
-struct RIACK_ALLOCATOR riack_php_allocator =
-{
-  riack_php_alloc,
-  riack_php_free
-};
-
 zend_module_entry riak_module_entry = {
   STANDARD_MODULE_HEADER,
   PHP_RIAK_EXTNAME,
@@ -126,7 +114,7 @@ PHP_INI_END()
 PHP_MINIT_FUNCTION(riak) /* {{{ */
 {
     REGISTER_INI_ENTRIES();
-    riack_init();
+
     le_riak_connection_list = zend_register_list_destructors_ex(NULL, le_riak_connections_pefree, "Persistent clients", module_number);
     riak_connection_init(TSRMLS_C);
     riak_object_init(TSRMLS_C);
@@ -193,7 +181,6 @@ PHP_MINIT_FUNCTION(riak) /* {{{ */
 
 PHP_MSHUTDOWN_FUNCTION(riak) /* {{{ */
 {
-    riack_cleanup();
     UNREGISTER_INI_ENTRIES();
     return SUCCESS;
 }
@@ -223,7 +210,7 @@ PHP_GSHUTDOWN_FUNCTION(riak) /* {{{ */
 #endif
 }
 /* {{{ */
-void riak_throw_exception(riak_connection* cnx, riak_error* err TSRMLS_DC)/* {{{ */
+void riak_throw_exception(riak_connection* cnx, riak_error err TSRMLS_DC)/* {{{ */
 {
     // TODO Fix for basho branch
     // For now just throw a random communication exception
@@ -259,7 +246,7 @@ void *riak_c_realloc(void* data, size_t size) {/* {{{ */
 }
 /* }}} */
 
-void *riak_c_free(void* data)/* {{{ */
+void riak_c_free(void* data)/* {{{ */
 {
     if (data) {
         pefree(data, 0);
@@ -281,7 +268,7 @@ void *riak_c_persistent_realloc(void* data, size_t size) {/* {{{ */
 }
 /* }}} */
 
-void *riak_c_persistent_free(void* data)/* {{{ */
+void riak_c_persistent_free(void* data)/* {{{ */
 {
     if (data) {
         pefree(data, 1);
diff --git a/php_riak_internal.h b/php_riak_internal.h
index 18b7fb5..a7f159a 100644
--- a/php_riak_internal.h
+++ b/php_riak_internal.h
@@ -134,6 +134,12 @@
        RIACK_STR.len = Z_STRLEN_P(ZVAL); \
        RIACK_STR.value = Z_STRVAL_P(ZVAL); } else
 
+#define CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(CONTEXT, ERR) \
+    if ( ERR != ERIAK_OK) { \
+      CONTEXT->needs_reconnect = 1; \
+      riak_throw_exception(CONTEXT->connection,  ERR TSRMLS_CC); \
+    return; }
+
 #ifdef ZTS
 #include <TSRM.h>
 # define RIAK_GLOBAL(v) TSRMG(riak_globals_id, zend_riak_globals *, v)
@@ -175,10 +181,10 @@ ZEND_EXTERN_MODULE_GLOBALS(riak)
 
 void *riak_c_alloc(size_t size);
 void *riak_c_realloc(void* data, size_t size);
-void *riak_c_free(void* data);
+void riak_c_free(void* data);
 void *riak_c_persistent_alloc(size_t size);
 void *riak_c_persistent_realloc(void* data, size_t size);
-void *riak_c_persistent_free(void* data);
+void riak_c_persistent_free(void* data);
 
 void *riak_c_pb_alloc(void *allocator_data, size_t size);
 void riak_c_pb_free (void *allocator_data, void *data);
@@ -186,6 +192,6 @@ void riak_c_pb_free (void *allocator_data, void *data);
 void *riak_c_pb_persistent_alloc(void *allocator_data, size_t size);
 void riak_c_pb_persistent_free (void *allocator_data, void *data);
 
-void riak_throw_exception(riak_connection* cnx, riak_error* err TSRMLS_DC);
+void riak_throw_exception(riak_connection* cnx, riak_error err TSRMLS_DC);
 
 #endif // PHP_RIAK_INTERNAL_H
diff --git a/riak/bucket.c b/riak/bucket.c
index 99ce751..c90fa43 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -36,6 +36,7 @@
 #include "query/index_query.h"
 #include "output/key_stream_output.h"
 #include "crdt/counter.h"
+#include <messages/riak_delete.h>
 #include <ext/spl/spl_iterators.h>
 #include <ext/spl/spl_array.h>
 
@@ -43,13 +44,14 @@ riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC);
 
 zend_class_entry *riak_bucket_ce;
 
-#define RIAK_REQ_PROP_SET_LONG(CLASS_ALIAS, ZV, GETTER, TARGET) \
+#define RIAK_REQ_PROP_SET_LONG(CLASS_ALIAS, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
     RIAK_CALL_METHOD(CLASS_ALIAS, GETTER, &ZV, zinput); \
-    if (Z_TYPE(ZV) == IS_LONG) { TARGET##_use = 1; TARGET = Z_LVAL(ZV); }
+    if (Z_TYPE(ZV) == IS_LONG) { \
+        riak_delete_options_set_##TARGET_NAME(TARGET_STRUCT, Z_LVAL(ZV)); }
 
-#define RIAK_REQ_PROP_SET_BOOL(CLASS_ALIAS, ZV, GETTER, TARGET) \
+#define RIAK_REQ_PROP_SET_BOOL(CLASS_ALIAS, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
     RIAK_CALL_METHOD(CLASS_ALIAS, GETTER, &ZV, zinput); \
-    if (Z_TYPE(ZV) == IS_BOOL && Z_BVAL(ZV)) { TARGET##_use = TARGET = 1; }
+    if (Z_TYPE(ZV) == IS_BOOL && Z_BVAL(ZV)) { riak_delete_options_set_##TARGET_NAME(TARGET_STRUCT, Z_BVAL(ZV)); }
 
 #define RIAK_GET_STR_COPY_TO(CLASSNAME, GETTER, ZOBJ, TARGET, TARGET_LEN) { \
     zval *z_getter_res; MAKE_STD_ZVAL(z_getter_res); \
@@ -187,8 +189,9 @@ struct riak_stream_key_cb_param {/* {{{ */
 };
 /* }}} */
 
-void riak_stream_key_cb(struct RIACK_CLIENT* c, void* p, RIACK_STRING key) {/* {{{ */
-    zval *zkey, zret, zfuncname;
+
+// void riak_stream_key_cb(struct RIACK_CLIENT* c, void* p, RIACK_STRING key) {/* {{{ */
+   /* zval *zkey, zret, zfuncname;
 
     struct riak_stream_key_cb_param *param = (struct riak_stream_key_cb_param*)p;
     MAKE_STD_ZVAL(zkey);
@@ -200,15 +203,15 @@ void riak_stream_key_cb(struct RIACK_CLIENT* c, void* p, RIACK_STRING key) {/* {
     call_user_function(NULL, &param->zstreamer, &zfuncname, &zret, 1, &zkey);
 #endif
 
-    zval_ptr_dtor(&zkey);
-}
+    zval_ptr_dtor(&zkey);*/
+// }
 /* }}} */
 
 /* {{{ proto void Riak\Bucket->getKeyStream(Riak\Output\KeyStreamOutput streamer)
 Streams all keys in the bucket */
 PHP_METHOD(RiakBucket, getKeyStream)
 {
-    struct riak_stream_key_cb_param cb_params;
+/*    struct riak_stream_key_cb_param cb_params;
     RIACK_STRING rsbucket;
     riak_connection *connection, *stream_connection;
     zval* zstreamer;
@@ -232,7 +235,7 @@ PHP_METHOD(RiakBucket, getKeyStream)
     // Not gonna do a retry here on purpose... no reason to retry a mistake in the first place
     riackstatus = riack_stream_keys(stream_connection->client, rsbucket, riak_stream_key_cb, &cb_params);
     CHECK_RIACK_STATUS_THROW_ON_ERROR(stream_connection, riackstatus);
-    release_connection(stream_connection TSRMLS_CC);
+    release_connection(stream_connection TSRMLS_CC);*/
 }
 /* }}} */
 
@@ -241,6 +244,7 @@ PHP_METHOD(RiakBucket, getKeyStream)
 List all keys in the bucket */
 PHP_METHOD(RiakBucket, getKeyList)
 {
+    /*
     struct RIACK_STRING_LINKED_LIST* resultlist, *curr;
     RIACK_STRING rsbucket;
     riak_connection *connection;
@@ -262,6 +266,7 @@ PHP_METHOD(RiakBucket, getKeyList)
     riack_free_string_linked_list(connection->client, &resultlist);
 
     RETURN_ZVAL(zresultarr, 0, 1);
+    */
 }
 /* }}} */
 
@@ -270,6 +275,7 @@ PHP_METHOD(RiakBucket, getKeyList)
 Creates a new counter on this bucket */
 PHP_METHOD(RiakBucket, counter)
 {
+    /*
     zval *zcounter, *zkey;
     char *key;
     int keylen;
@@ -287,12 +293,14 @@ PHP_METHOD(RiakBucket, counter)
     zval_ptr_dtor(&zkey);
 
     RETURN_ZVAL(zcounter, 0, 1);
+    */
 }
 
 /* {{{ proto array Riak\Bucket->index(string $index, string $from [, string $to])
 Perform a secondary index lookup */
 PHP_METHOD(RiakBucket, index)
 {
+    /*
     RIACK_STRING rsbucket, rsindex, rsfrom, rsto;
     RIACK_STRING_LIST resultlist;
     char *index, *from, *to;
@@ -327,6 +335,7 @@ PHP_METHOD(RiakBucket, index)
     }
     riack_free_string_list(connection->client, &resultlist);
     RETURN_ZVAL(zresult, 0, 1);
+    */
 }
 /* }}} */
 
@@ -335,6 +344,7 @@ PHP_METHOD(RiakBucket, index)
 Apply given properties to this bucket */
 PHP_METHOD(RiakBucket, indexQuery)
 {
+    /*
     riak_connection *connection;
     struct RIACK_2I_QUERY_REQ req;
     RIACK_STRING_LIST result_keys;
@@ -407,13 +417,14 @@ PHP_METHOD(RiakBucket, indexQuery)
         RSTR_SAFE_FREE(connection->client, continuation);
     }
     RETURN_ZVAL(zresult, 0, 1);
+    */
 }
 /* }}} */
 
 /* {{{ */
-void riak_set_riack_commit_hook(riak_connection *connection, zval* zhook, struct RIACK_COMMIT_HOOK* hook TSRMLS_DC)
-{
-    zval *zjs;
+// void riak_set_riack_commit_hook(riak_connection *connection, zval* zhook, struct RIACK_COMMIT_HOOK* hook TSRMLS_DC)
+// {
+/*    zval *zjs;
     memset(hook, 0, sizeof(struct RIACK_COMMIT_HOOK));
     MAKE_STD_ZVAL(zjs);
     RIAK_CALL_METHOD(RiakCommitHook, isJavascript, zjs, zhook);
@@ -423,14 +434,15 @@ void riak_set_riack_commit_hook(riak_connection *connection, zval* zhook, struct
         RIAK_GET_STR_COPY_TO(RiakCommitHook, getErlModule, zhook, hook->modfun.module.value, hook->modfun.module.len);
         RIAK_GET_STR_COPY_TO(RiakCommitHook, getErlFunction, zhook, hook->modfun.function.value, hook->modfun.function.len);
     }
-    zval_ptr_dtor(&zjs);
-}
+    zval_ptr_dtor(&zjs); */
+// }
 /* }}} */
 
 /* {{{ */
-void riak_set_commit_hooks_properties(riak_connection *connection, zval *zhook_list,  uint8_t *has_hooks,
-                                      size_t* hook_count, struct RIACK_COMMIT_HOOK** hooks TSRMLS_DC)
-{
+// void riak_set_commit_hooks_properties(riak_connection *connection, zval *zhook_list,  uint8_t *has_hooks,
+//                                       size_t* hook_count, struct RIACK_COMMIT_HOOK** hooks TSRMLS_DC)
+// {
+/*
     *has_hooks = 0;
     *hook_count = 0;
     if (Z_TYPE_P(zhook_list) != IS_NULL && Z_TYPE_P(zhook_list) == IS_OBJECT) {
@@ -476,14 +488,15 @@ void riak_set_commit_hooks_properties(riak_connection *connection, zval *zhook_l
         }
         zval_ptr_dtor(&ziter);
         zval_ptr_dtor(&zcount);
-    }
-}
+    }  */
+// }
 /* }}} */
 
 /* {{{ proto Riak\Bucket Riak\Bucket->setPropertyList(Riak\BucketProperties $properties)
 Apply given properties to this bucket */
 PHP_METHOD(RiakBucket, setPropertyList)
 {
+    /*
 #define SET_PROPLIST_LONG(GETTER_NAME, PROP_NAME) MAKE_STD_ZVAL(ztmp); \
                                                   RIAK_CALL_METHOD(RiakBucketProperties, GETTER_NAME, ztmp, zprop_obj); \
                                                   if (Z_TYPE_P(ztmp) == IS_LONG) { \
@@ -596,12 +609,13 @@ PHP_METHOD(RiakBucket, setPropertyList)
     RFREE(connection->client, properties.precommit_hooks);
     RFREE(connection->client, properties.postcommit_hooks);
  	CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackResult);
-
+*/
     RIAK_RETURN_THIS
 }
 /* }}} */
 
-zval *riak_linkfun_from_riack(struct RIACK_MODULE_FUNCTION* modfun TSRMLS_DC) {
+
+/*zval *riak_linkfun_from_riack(struct RIACK_MODULE_FUNCTION* modfun TSRMLS_DC) {
     zval *zmodfun, *zmod, *zfunc;
     MAKE_STD_ZVAL(zmodfun);
 
@@ -617,10 +631,10 @@ zval *riak_linkfun_from_riack(struct RIACK_MODULE_FUNCTION* modfun TSRMLS_DC) {
     zval_ptr_dtor(&zmod);
     zval_ptr_dtor(&zfunc);
     return zmodfun;
-}
+}*/
 
 /* {{{ */
-zval* zhook_list_from_riack(size_t hook_count, struct RIACK_COMMIT_HOOK* hooks TSRMLS_DC)
+/*zval* zhook_list_from_riack(size_t hook_count, struct RIACK_COMMIT_HOOK* hooks TSRMLS_DC)
 {
     zval *zhook_list;
     size_t i;
@@ -667,6 +681,7 @@ zval* zhook_list_from_riack(size_t hook_count, struct RIACK_COMMIT_HOOK* hooks T
 Fetch and return a RiakBucketProperties object with properties for this bucket */
 PHP_METHOD(RiakBucket, getPropertyList)
 {
+/*
 #define GET_PROP_SET_ON_LIST(ZVAL_METHOD, SETTER_NAME, PROP_NAME) if (PROP_NAME##_use) { \
                                                         MAKE_STD_ZVAL(ztmp); \
                                                         ZVAL_METHOD(ztmp, PROP_NAME); \
@@ -762,6 +777,7 @@ PHP_METHOD(RiakBucket, getPropertyList)
     }
     riack_free_bucket_properties(connection->client, &properties);
     RETURN_ZVAL(zbucket_props, 0, 1);
+    */
 }
 /* }}} */
 
@@ -769,14 +785,13 @@ PHP_METHOD(RiakBucket, getPropertyList)
 Deletes given object from riak */
 PHP_METHOD(RiakBucket, delete)
 {
-    struct RIACK_DEL_PROPERTIES props;
-    riak_connection *connection;
-    zval *zparam, *zinput, zKey;
-    RIACK_STRING bucketName, key;
-    int riackResult;
-
-    zinput = NULL;
-
+    riak_context *ctx;
+    riak_binary *bucket, *key, *vclock;
+    riak_delete_options *opts;
+    riak_error result;
+    zval *zparam, *zinput, zkey;
+    zinput = 0;
+    vclock = 0;
     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|o", &zparam, &zinput) == FAILURE) {
         zend_throw_exception(riak_badarguments_exception_ce, "Bad or missing argument", 500 TSRMLS_CC);
         return;
@@ -786,62 +801,59 @@ PHP_METHOD(RiakBucket, delete)
         zend_throw_exception(riak_badarguments_exception_ce, "Argument 1 passed to Riak\\Bucket#delete() must be a string or an instance of Riak\\Object.", 5001 TSRMLS_CC);
         return;
     }
+    ctx = get_riak_context(getThis());
+    bucket = binary_bucket_name_new(ctx, getThis());
+    opts = riak_delete_options_new(ctx->config);
 
-    memset(&props, 0, sizeof(props));
-
-    // Set connection and bucket name
-    connection = get_riak_connection(getThis() TSRMLS_CC);
-    bucketName = riack_name_from_bucket(getThis() TSRMLS_CC);
-
-    key.len   = 0;
-    key.value = NULL;
-
+    ZVAL_NULL(&zkey);
+    key = 0;
     // Set input properties if provided
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
         zval zget;
+        /*
+        riak c does not provides setters for r values
         RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getR, props.r);
         RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getPR, props.pr);
         RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getRW, props.rw);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getW, props.w);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getDW, props.dw);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getPW, props.pw);
+        */
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getW, opts, w);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getDW, opts, dw);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getPW, opts, pw);
 
         RIAK_CALL_METHOD(Riak_Input_DeleteInput, getVClock, &zget, zinput);
         if (Z_TYPE(zget) == IS_STRING) {
-            RMALLOCCOPY(connection->client, props.vclock.clock, props.vclock.len, Z_STRVAL(zget), Z_STRLEN(zget));
+            vclock = riak_binary_new(ctx->config, Z_STRLEN(zget), (riak_uint8_t *)Z_STRVAL(zget));
         }
         zval_dtor(&zget);
     }
-
-    ZVAL_NULL(&zKey);
     if (Z_TYPE_P(zparam) == IS_OBJECT) {
-        RIAK_CALL_METHOD(RiakObject, getKey, &zKey, zparam);
-        if (Z_TYPE(zKey) == IS_STRING && Z_STRVAL(zKey) > 0) {
-            key.len   = Z_STRLEN(zKey);
-            key.value = Z_STRVAL(zKey);
+        RIAK_CALL_METHOD(RiakObject, getKey, &zkey, zparam);
+        if (Z_TYPE(zkey) == IS_STRING && Z_STRVAL(zkey) > 0) {
+            key = riak_binary_new(ctx->config, Z_STRLEN(zkey), (riak_uint8_t *)Z_STRVAL(zkey));
         }
-
         // If vclock is not set retrieve from object
-        if (!props.vclock.clock) {
-            zval zObjVClock;
-            RIAK_CALL_METHOD(RiakObject, getVClock, &zObjVClock, zparam);
-            if (Z_TYPE(zObjVClock) == IS_STRING) {
-                RMALLOCCOPY(connection->client, props.vclock.clock, props.vclock.len, Z_STRVAL(zObjVClock), Z_STRLEN(zObjVClock));
+        if (!vclock) {
+            zval zobjvclock;
+            RIAK_CALL_METHOD(RiakObject, getVClock, &zobjvclock, zparam);
+            if (Z_TYPE(zobjvclock) == IS_STRING) {
+                // Set the vclock
+                vclock = riak_binary_new(ctx->config, Z_STRLEN(zobjvclock), (riak_uint8_t *)Z_STRVAL(zobjvclock));
+                zval_dtor(&zobjvclock);
             }
-            zval_dtor(&zObjVClock);
         }
     } else if (Z_TYPE_P(zparam) == IS_STRING && Z_STRVAL_P(zparam) > 0) {
-        key.len   = Z_STRLEN_P(zparam);
-        key.value = Z_STRVAL_P(zparam);
+        key = riak_binary_new(ctx->config, Z_STRLEN_P(zparam), (riak_uint8_t *)Z_STRVAL_P(zparam));
     }
-
-    if (key.len == 0) {
-        zend_throw_exception(riak_badarguments_exception_ce, "Unable to resolve the object key.", 5001 TSRMLS_CC);
+    if (vclock) {
+        riak_delete_options_set_vclock(ctx->config, opts, vclock);
     }
-    RIACK_RETRY_OP(riackResult, riack_delete(connection->client, bucketName, key, &props));
-    RFREE(connection->client, props.vclock.clock);
-    zval_dtor(&zKey);
-    CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackResult);
+    result = riak_delete(ctx->connection, bucket, key, opts);
+
+    if (vclock) riak_binary_free(ctx->config, &vclock);
+    riak_binary_free(ctx->config, &key);
+    riak_binary_free(ctx->config, &bucket);
+    riak_delete_options_free(ctx->config, &opts);
+    CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(ctx, result);
 }
 /* }}} */
 
@@ -849,6 +861,7 @@ PHP_METHOD(RiakBucket, delete)
 Store a RiakObject in riak, if something goes wrong an RiakException is thrown */
 PHP_METHOD(RiakBucket, put)
 {
+    /*
     int riackResult;
     zval *zObject, *zTmp, *zinput, *zout, zvclock;
 	struct RIACK_OBJECT obj, returnedObj;
@@ -870,7 +883,7 @@ PHP_METHOD(RiakBucket, put)
 	memset(&riackContent, 0, sizeof(riackContent));
     memset(&props, 0, sizeof(props));
     /* fill content */
-    set_riak_content_from_object(&riackContent, zObject, connection->client TSRMLS_CC);
+    /*set_riak_content_from_object(&riackContent, zObject, connection->client TSRMLS_CC);
     ZVAL_NULL(&zvclock);
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
         zval zget;
@@ -893,11 +906,11 @@ PHP_METHOD(RiakBucket, put)
     }
 
     /* Set bucket name */
-    obj.bucket = riack_name_from_bucket(getThis() TSRMLS_CC);
+    /*obj.bucket = riack_name_from_bucket(getThis() TSRMLS_CC);
 	obj.content_count = 1;
 	obj.content = &riackContent;
     /* No ket provided on function call, get it from RiakObject */
-    GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(riak_object_ce, zObject, "key", zTmp, obj.key) {
+    /*GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(riak_object_ce, zObject, "key", zTmp, obj.key) {
         // Key is null this is ok
         obj.key.len = 0;
         obj.key.value = 0;
@@ -919,7 +932,7 @@ PHP_METHOD(RiakBucket, put)
     RETVAL_ZVAL(zout, 0, 1);
 
     zval_ptr_dtor(&zTmp);
-    riack_free_object(connection->client, &returnedObj);
+    riack_free_object(connection->client, &returnedObj);*/
 }
 /* }}} */
 
@@ -927,7 +940,7 @@ PHP_METHOD(RiakBucket, put)
 Get value from riak */
 PHP_METHOD(RiakBucket, get)
 {
-	char *key;
+/*	char *key;
     int keyLen, riackResult;
     zval *zKey, *zinput;
 	struct RIACK_GET_PROPERTIES props;
@@ -995,6 +1008,7 @@ PHP_METHOD(RiakBucket, get)
         }
 	}
     zval_ptr_dtor(&zKey);
+*/
 }
 /* }}} */
 
@@ -1037,17 +1051,17 @@ PHP_METHOD(RiakBucket, setConflictResolver)
 }
 /* }}} */
 
-zval* object_from_riak_content(zval* key, struct RIACK_CONTENT* content TSRMLS_DC)/* {{{ */
-{
-	zval *object;
-	MAKE_STD_ZVAL(object);
-	object_init_ex(object, riak_object_ce);
-	RIAK_CALL_METHOD1(RiakObject, __construct, object, object, key);
-
-	set_object_from_riak_content(object, content TSRMLS_CC);
-
-	return object;
-}
+// zval* object_from_riak_content(zval* key, struct RIACK_CONTENT* content TSRMLS_DC)/* {{{ */
+// {
+// 	zval *object;
+// 	MAKE_STD_ZVAL(object);
+// 	object_init_ex(object, riak_object_ce);
+// 	RIAK_CALL_METHOD1(RiakObject, __construct, object, object, key);
+//
+// 	set_object_from_riak_content(object, content TSRMLS_CC);
+//
+// 	return object;
+// }
 /* }}} */
 
 void riak_name_from_bucket(zval* bucket, char **name, int *namelen TSRMLS_DC)/* {{{ */
@@ -1063,25 +1077,33 @@ void riak_name_from_bucket(zval* bucket, char **name, int *namelen TSRMLS_DC)/*
 }
 /* }}} */
 
-RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC)/* {{{ */
+riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket)
 {
-    RIACK_STRING bucketName;
+    char* name;
     int namelen;
-    riak_name_from_bucket(bucket, &bucketName.value, &namelen TSRMLS_CC);
-    bucketName.len = namelen;
-    return bucketName;
+    riak_name_from_bucket(zbucket, &name, &namelen);
+    return riak_binary_new(ctx->config, namelen, (riak_uint8_t*)name);
 }
+
+// RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC)/* {{{ */
+// {
+//     RIACK_STRING bucketName;
+//     int namelen;
+//     riak_name_from_bucket(bucket, &bucketName.value, &namelen TSRMLS_CC);
+//     bucketName.len = namelen;
+//     return bucketName;
+// }
 /* }}} */
 
-riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC)/* {{{ */
+riak_context *get_riak_context(zval *zbucket TSRMLS_DC)/* {{{ */
 {
     zval *zclient;
-    riak_connection *connection = NULL;
+    riak_context *context = NULL;
     zclient = zend_read_property(riak_bucket_ce, zbucket, "connection", sizeof("connection")-1, 1 TSRMLS_CC);
     if (zclient) {
-        GET_RIAK_CONNECTION(zclient, connection);
-		ensure_connected(connection TSRMLS_CC);
+        GET_RIAK_CONTEXT(zclient, context);
+        ensure_connected(context TSRMLS_CC);
     }
-	return connection;
+    return context;
 }
 /* }}} */
diff --git a/riak/bucket.h b/riak/bucket.h
index 9e74e77..e9703c9 100644
--- a/riak/bucket.h
+++ b/riak/bucket.h
@@ -45,9 +45,10 @@ PHP_METHOD(RiakBucket, getConnection);
 PHP_METHOD(RiakBucket, getConflictResolver);
 PHP_METHOD(RiakBucket, setConflictResolver);
 
-riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC);
-zval* object_from_riak_content(zval* key, struct RIACK_CONTENT* content TSRMLS_DC);
-RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC);
+riak_context *get_riak_context(zval *zbucket TSRMLS_DC);
+// zval* object_from_riak_content(zval* key, struct RIACK_CONTENT* content TSRMLS_DC);
+// RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC);
 void riak_name_from_bucket(zval* bucket, char **name, int *namelen TSRMLS_DC);
+riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket);
 
 #endif
diff --git a/riak/connection.c b/riak/connection.c
index a9b13c4..56f5fac 100644
--- a/riak/connection.c
+++ b/riak/connection.c
@@ -95,8 +95,8 @@ void free_client_data(void *object TSRMLS_DC) /* {{{ */
 
 	zend_object_std_dtor(&data->std TSRMLS_CC);
 
-	if (data->connection) {
-		release_connection(data->connection TSRMLS_CC);
+    if (data->context) {
+        release_context(data->context TSRMLS_CC);
 	}
 
 	efree(data);
@@ -148,9 +148,9 @@ PHP_METHOD(RiakConnection, __construct)
 
 	data = (client_data*)zend_object_store_get_object(getThis() TSRMLS_CC);
 
-	data->connection = take_connection(host, hostLen, port TSRMLS_CC);
+    data->context = take_connection(host, hostLen, port TSRMLS_CC);
 
-	if (!data->connection) {
+    if (!data->context) {
 		zend_throw_exception(riak_connection_exception_ce, "Connection error", 1000 TSRMLS_CC);
 	}
 }
@@ -172,15 +172,14 @@ PHP_METHOD(RiakConnection, getServerInfo)
 Ping riak to see if it is alive, an exception is thrown if no response is received */
 PHP_METHOD(RiakConnection, ping)
 {
-	int pingStatus;
-	riak_connection *connection;
+    riak_error result;
+    riak_context *context;
 
-	GET_RIAK_CONNECTION(getThis(), connection);
-	ensure_connected(connection TSRMLS_CC);
+    GET_RIAK_CONTEXT(getThis(), context);
+    ensure_connected(context TSRMLS_CC);
 
-	pingStatus = riack_ping(connection->client);
-
-	CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, pingStatus);
+    result = riak_ping(context->connection);
+    CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(context, result);
 }
 /* }}} */
 
diff --git a/riak/connection.h b/riak/connection.h
index 1fcdd37..898c707 100644
--- a/riak/connection.h
+++ b/riak/connection.h
@@ -27,10 +27,10 @@ typedef struct _client_data {
   /* required */
   zend_object std;
 
-  riak_connection *connection;
+  riak_context *context;
 } client_data;
 
-#define GET_RIAK_CONNECTION(ZOBJ, VAR) VAR = ((client_data*)zend_object_store_get_object(ZOBJ TSRMLS_CC))->connection
+#define GET_RIAK_CONTEXT(ZOBJ, VAR) VAR = ((client_data*)zend_object_store_get_object(ZOBJ TSRMLS_CC))->context
 
 /*************************************************
 * Constants
diff --git a/riak/crdt/counter.c b/riak/crdt/counter.c
index d8c315f..d98cb56 100644
--- a/riak/crdt/counter.c
+++ b/riak/crdt/counter.c
@@ -81,8 +81,8 @@ PHP_METHOD(Riak_Crdt_Counter, __construct)
 /* }}} */
 
 
-void riak_update_riack_counter_inc_props_from_input(struct RIACK_COUNTER_UPDATE_PROPERTIES *props, zval* zinput TSRMLS_DC) { /* {{{ */
-#define SET_RIAK_CNT_UPDATE_PROP_LONG(GETTER_NAME, PROP_NAME) MAKE_STD_ZVAL(ztmp); \
+// void riak_update_riack_counter_inc_props_from_input(struct RIACK_COUNTER_UPDATE_PROPERTIES *props, zval* zinput TSRMLS_DC) { /* {{{ */
+/*#define SET_RIAK_CNT_UPDATE_PROP_LONG(GETTER_NAME, PROP_NAME) MAKE_STD_ZVAL(ztmp); \
                                                   RIAK_CALL_METHOD(Riak_Crdt_Input_UpdateInput, GETTER_NAME, ztmp, zinput); \
                                                   props->PROP_NAME##_use = 1; \
                                                   props->PROP_NAME = Z_LVAL_P(ztmp); \
@@ -92,15 +92,15 @@ void riak_update_riack_counter_inc_props_from_input(struct RIACK_COUNTER_UPDATE_
         SET_RIAK_CNT_UPDATE_PROP_LONG(getDW, dw);
         SET_RIAK_CNT_UPDATE_PROP_LONG(getPW, pw);
         SET_RIAK_CNT_UPDATE_PROP_LONG(getW, w);
-    }
-}/* }}} */
+    }*/
+// }/* }}} */
 
 
 /* {{{ proto void Riak\Crdt\Counter->increment($amount [, Riak\Crdt\Input\UpdateInput $input])
 Increment counter, use negative amount to decrement */
 PHP_METHOD(Riak_Crdt_Counter, increment)
 {
-    struct RIACK_COUNTER_UPDATE_PROPERTIES props;
+/*    struct RIACK_COUNTER_UPDATE_PROPERTIES props;
     zval *zinput, *zbucket, *zkey;
     riak_connection *connection;
     RIACK_STRING bucket, key;
@@ -116,7 +116,7 @@ PHP_METHOD(Riak_Crdt_Counter, increment)
     RIAK_COUNTER_GET_BUCKET_KEY_CONN;
 
     riackresult = riack_counter_increment(connection->client, bucket, key, (int64_t)amount, &props, 0);
-    CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);
+    CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);*/
 }/* }}} */
 
 
@@ -124,8 +124,8 @@ PHP_METHOD(Riak_Crdt_Counter, increment)
 Increment counter, use negative amount to decrement, returns updated value */
 PHP_METHOD(Riak_Crdt_Counter, incrementAndGet)
 {
-    struct RIACK_COUNTER_UPDATE_PROPERTIES props;
-    zval *zinput, *zbucket, *zkey;
+/*    struct RIACK_COUNTER_UPDATE_PROPERTIES props;
+    zval *zinput;
     riak_connection *connection;
     RIACK_STRING bucket, key;
     long amount;
@@ -142,7 +142,7 @@ PHP_METHOD(Riak_Crdt_Counter, incrementAndGet)
 
     riackresult = riack_counter_increment(connection->client, bucket, key, (int64_t)amount, &props, &return_val);
     CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);
-    RETURN_LONG((long)return_val);
+    RETURN_LONG((long)return_val);*/
 }/* }}} */
 
 
@@ -150,7 +150,7 @@ PHP_METHOD(Riak_Crdt_Counter, incrementAndGet)
 Get counter value from riak */
 PHP_METHOD(Riak_Crdt_Counter, get)
 {
-#define SET_RIAK_CNT_GET_PROP_LONG(GETTER_NAME, PROP_NAME) MAKE_STD_ZVAL(ztmp); \
+/*#define SET_RIAK_CNT_GET_PROP_LONG(GETTER_NAME, PROP_NAME) MAKE_STD_ZVAL(ztmp); \
                                                   RIAK_CALL_METHOD(Riak_Crdt_Input_GetInput, GETTER_NAME, ztmp, zinput); \
                                                   props.PROP_NAME##_use = 1; \
                                                   props.PROP_NAME = Z_LVAL_P(ztmp); \
@@ -182,5 +182,5 @@ PHP_METHOD(Riak_Crdt_Counter, get)
     }
     riackresult = riack_counter_get(connection->client, bucket, key, &props, &result);
     CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);
-    RETURN_LONG((long)result);
+    RETURN_LONG((long)result);*/
 }/* }}} */
diff --git a/riak/map_reduce/output/output.c b/riak/map_reduce/output/output.c
index 51e5a18..d896cfd 100644
--- a/riak/map_reduce/output/output.c
+++ b/riak/map_reduce/output/output.c
@@ -47,9 +47,9 @@ void riak_map_reduce_output_output_init(TSRMLS_D)/* {{{ */
 /* }}} */
 
 
-zval *riak_mroutput_from_riack_mapred(struct RIACK_MAPRED_RESPONSE *mapresult TSRMLS_DC)/* {{{ */
-{
-    zval *zresult, *zvalue, *zphase;
+// zval *riak_mroutput_from_riack_mapred(struct RIACK_MAPRED_RESPONSE *mapresult TSRMLS_DC)/* {{{ */
+// {
+   /* zval *zresult, *zvalue, *zphase;
     MAKE_STD_ZVAL(zresult);
     MAKE_STD_ZVAL(zvalue);
 
@@ -65,8 +65,8 @@ zval *riak_mroutput_from_riack_mapred(struct RIACK_MAPRED_RESPONSE *mapresult TS
         RIAK_CALL_METHOD1(Riak_MapReduce_Output_Output, __construct, zresult, zresult, zvalue);
     }
     zval_ptr_dtor(&zvalue);
-    return zresult;
-}
+    return zresult; */
+// }
 /* }}} */
 
 /* {{{ proto void Riak\MapReduce\Output\Output->__construct($value, [, int $phase])
diff --git a/riak/map_reduce/output/output.h b/riak/map_reduce/output/output.h
index 7b90b08..040f85a 100644
--- a/riak/map_reduce/output/output.h
+++ b/riak/map_reduce/output/output.h
@@ -23,7 +23,7 @@ extern zend_class_entry *riak_mroutput_ce;
 
 void riak_map_reduce_output_output_init(TSRMLS_D);
 
-zval *riak_mroutput_from_riack_mapred(struct RIACK_MAPRED_RESPONSE *mapresult TSRMLS_DC);
+// zval *riak_mroutput_from_riack_mapred(struct RIACK_MAPRED_RESPONSE *mapresult TSRMLS_DC);
 
 PHP_METHOD(Riak_MapReduce_Output_Output, __construct);
 PHP_METHOD(Riak_MapReduce_Output_Output, getValue);
diff --git a/riak/object.c b/riak/object.c
index e5e0e52..d0f7618 100644
--- a/riak/object.c
+++ b/riak/object.c
@@ -393,9 +393,9 @@ void riak_key_from_object(zval *zobject, char** key, int* keylen TSRMLS_DC)/* {{
 }
 /* }}} */
 
-zval *links_from_content(struct RIACK_CONTENT* content TSRMLS_DC)/* {{{ */
-{
-    struct RIACK_LINK *curr_link;
+// zval *links_from_content(struct RIACK_CONTENT* content TSRMLS_DC)/* {{{ */
+// {
+   /* struct RIACK_LINK *curr_link;
     char *tag, *bucket, *key;
     zval *zlinkarr, *zlink;
     size_t i;
@@ -413,39 +413,40 @@ zval *links_from_content(struct RIACK_CONTENT* content TSRMLS_DC)/* {{{ */
             pefree(bucket, 0); pefree(key, 0); pefree(tag, 0);
         }
     }
-    return zlinkarr;
-}
+    return zlinkarr;*/
+// }
 /* }}} */
 
 /* Get metadata array from returned content */
-zval *assoc_array_from_riack_pairs(struct RIACK_PAIR* pairs, size_t pairscnt TSRMLS_DC) /* {{{ */
-{
-    zval *zresultarr;
-	size_t i;
-    struct RIACK_PAIR* currentpair;
-	char* currentKey;
-    MAKE_STD_ZVAL(zresultarr);
-    array_init(zresultarr);
-    for (i=0; i<pairscnt; ++i) {
-        currentpair = &(pairs[i]);
-        if (currentpair) {
-            currentKey = pestrndup(currentpair->key.value, currentpair->key.len, 0);
-            if (currentpair->value_present) {
-                add_assoc_stringl(zresultarr, currentKey, (char*)currentpair->value, currentpair->value_len, 1);
-			} else {
-                add_assoc_null(zresultarr, currentKey);
-			}
-			pefree(currentKey, 0);
-		}
-	}
-    return zresultarr;
-}
+// zval *assoc_array_from_riack_pairs(struct RIACK_PAIR* pairs, size_t pairscnt TSRMLS_DC) /* {{{ */
+// {
+//     zval *zresultarr;
+// 	size_t i;
+//     struct RIACK_PAIR* currentpair;
+// 	char* currentKey;
+//     MAKE_STD_ZVAL(zresultarr);
+//     array_init(zresultarr);
+//     for (i=0; i<pairscnt; ++i) {
+//         currentpair = &(pairs[i]);
+//         if (currentpair) {
+//             currentKey = pestrndup(currentpair->key.value, currentpair->key.len, 0);
+//             if (currentpair->value_present) {
+//                 add_assoc_stringl(zresultarr, currentKey, (char*)currentpair->value, currentpair->value_len, 1);
+// 			} else {
+//                 add_assoc_null(zresultarr, currentKey);
+// 			}
+// 			pefree(currentKey, 0);
+// 		}
+// 	}
+//     return zresultarr;
+// }
 /* }}} */
 
 
 /* Set object properties from returned content */
-void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC) /* {{{ */
-{
+// void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC) /* {{{ */
+// {
+/*
     zval* zmetadata, *zlinks, *zindexes;
 
     zend_update_property_stringl(riak_object_ce, object, "content", sizeof("content")-1,
@@ -485,14 +486,14 @@ void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TS
 
     zlinks = links_from_content(content TSRMLS_CC);
     zend_update_property(riak_object_ce, object, "links", sizeof("links")-1, zlinks TSRMLS_CC);
-    zval_ptr_dtor(&zlinks);
-}
+    zval_ptr_dtor(&zlinks);*/
+// }
 /* }}} */
 
 /* Called once for each link in the links property of RiakObject */
-void set_links_from_object_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
-{
-    zval *zbucket, *ztag, *zkey;
+// void set_links_from_object_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
+// {
+  /*  zval *zbucket, *ztag, *zkey;
     RIACK_STRING rbucket, rtag, rkey;
     struct RIACK_CLIENT* client = (struct RIACK_CLIENT*)callingObj;
     struct RIACK_CONTENT* content = (struct RIACK_CONTENT*)custom_ptr;
@@ -510,28 +511,28 @@ void set_links_from_object_cb(void* callingObj, void* custom_ptr, char* key, uin
     ztag = zend_read_property(riak_link_ce, *data, "tag", sizeof("tag")-1, 1 TSRMLS_CC);
     rtag.len = Z_STRLEN_P(ztag);
     rtag.value = Z_STRVAL_P(ztag);
-    content->links[cnt].tag = riack_copy_string(client, rtag);
-}
+    content->links[cnt].tag = riack_copy_string(client, rtag);*/
+// }
 /* }}} */
 
 /* Copy all links from link array to a content structure */
-void set_links_from_object(struct RIACK_CONTENT* content, zval* zlinksarr, struct RIACK_CLIENT* client TSRMLS_DC)/* {{{ */
-{
-    if (zlinksarr && Z_TYPE_P(zlinksarr) == IS_ARRAY) {
+// void set_links_from_object(struct RIACK_CONTENT* content, zval* zlinksarr, struct RIACK_CLIENT* client TSRMLS_DC)/* {{{ */
+// {
+/*    if (zlinksarr && Z_TYPE_P(zlinksarr) == IS_ARRAY) {
         content->link_count = zend_hash_num_elements(Z_ARRVAL_P(zlinksarr));
         if (content->link_count > 0) {
             content->links = RMALLOC(client, sizeof(struct RIACK_LINK) * content->link_count);
             memset(content->links, 0, sizeof(struct RIACK_LINK) * content->link_count);
             foreach_in_hashtable(client, content, Z_ARRVAL_P(zlinksarr), &set_links_from_object_cb TSRMLS_CC);
         }
-    }
-}
+    } */
+// }
 /* }}} */
 
 /* Called once for each metadata or index entry in the RiakObject */
-void set_pairs_from_object_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
-{
-    zval *tmp;
+// void set_pairs_from_object_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
+// {
+/*    zval *tmp;
     RIACK_STRING rkey;
     struct RIACK_CLIENT* client = (struct RIACK_CLIENT*)callingObj;
     struct RIACK_PAIR* pairs = (struct RIACK_PAIR*)custom_ptr;
@@ -560,42 +561,42 @@ void set_pairs_from_object_cb(void* callingObj, void* custom_ptr, char* key, uin
         RMALLOCCOPY(client, pairs[cnt].value, pairs[cnt].value_len, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
         zval_ptr_dtor(&tmp);
     }
-
-}
+*/
+// }
 /* }}} */
 
 /* Copy all metadata from metadata array to a content structure */
-void set_metadata_from_object(struct RIACK_CONTENT* content, zval* zMetadata, struct RIACK_CLIENT* client TSRMLS_DC) /* {{{ */
-{
-    if (zMetadata && Z_TYPE_P(zMetadata) == IS_ARRAY) {
+// void set_metadata_from_object(struct RIACK_CONTENT* content, zval* zMetadata, struct RIACK_CLIENT* client TSRMLS_DC) /* {{{ */
+// {
+/*    if (zMetadata && Z_TYPE_P(zMetadata) == IS_ARRAY) {
         content->usermeta_count = zend_hash_num_elements(Z_ARRVAL_P(zMetadata));
         if (content->usermeta_count > 0) {
 			content->usermetas = RMALLOC(client, sizeof(struct RIACK_PAIR) * content->usermeta_count);
 			memset(content->usermetas, 0, sizeof(struct RIACK_PAIR) * content->usermeta_count);
             foreach_in_hashtable(client, content->usermetas, Z_ARRVAL_P(zMetadata), &set_pairs_from_object_cb TSRMLS_CC);
 		}
-	}
-}
+    }*/
+// }
 /* }}} */
 
 /* Copy all indexes from array to a content structure */
-void set_indexes_from_object(struct RIACK_CONTENT* content, zval* zindexsarray, struct RIACK_CLIENT* client TSRMLS_DC) /* {{{ */
-{
-    if (zindexsarray && Z_TYPE_P(zindexsarray)) {
+// void set_indexes_from_object(struct RIACK_CONTENT* content, zval* zindexsarray, struct RIACK_CLIENT* client TSRMLS_DC) /* {{{ */
+// {
+  /*  if (zindexsarray && Z_TYPE_P(zindexsarray)) {
         content->index_count = zend_hash_num_elements(Z_ARRVAL_P(zindexsarray));
         if (content->index_count > 0) {
             content->indexes = RMALLOC(client, sizeof(struct RIACK_PAIR) * content->index_count);
             memset(content->indexes, 0, sizeof(struct RIACK_PAIR) * content->index_count);
             foreach_in_hashtable(client, content->indexes, Z_ARRVAL_P(zindexsarray), &set_pairs_from_object_cb TSRMLS_CC);
         }
-    }
-}
+    }*/
+// }
 /* }}} */
 
 /* Fill out members of a content struct with this objects values */
-void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC)/* {{{ */
-{
-	zval* zTmp;
+// void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC)/* {{{ */
+// {
+    /*zval* zTmp;
     zTmp = zend_read_property(riak_object_ce, object, "content", sizeof("content")-1, 1 TSRMLS_CC);
 	if (Z_TYPE_P(zTmp) == IS_STRING) {
 		content->data_len = Z_STRLEN_P(zTmp);
@@ -613,6 +614,6 @@ void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, s
 	set_metadata_from_object(content, zTmp, client TSRMLS_CC);
 
     zTmp = zend_read_property(riak_object_ce, object, "links", sizeof("links")-1, 1 TSRMLS_CC);
-    set_links_from_object(content, zTmp, client TSRMLS_CC);
-}
+    set_links_from_object(content, zTmp, client TSRMLS_CC); */
+// }
 /* }}} */
diff --git a/riak/object.h b/riak/object.h
index f5528b4..39d0b18 100644
--- a/riak/object.h
+++ b/riak/object.h
@@ -25,8 +25,10 @@ extern zend_class_entry *riak_object_ce;
 zval* create_object_object(const char* key TSRMLS_DC);
 
 void riak_object_init(TSRMLS_D);
-void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC);
-void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC);
+// TODO
+//  void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC);
+// TODO
+//  void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC);
 
 void riak_key_from_object(zval *zobject, char** key, int* keylen  TSRMLS_DC);
 
diff --git a/riak/output/get_output.c b/riak/output/get_output.c
index 46321b0..a1ed98b 100644
--- a/riak/output/get_output.c
+++ b/riak/output/get_output.c
@@ -36,8 +36,8 @@ void riak_output_get_output_init(TSRMLS_D)/* {{{ */
 }
 /* }}} */
 
-zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC) /* {{{ */
-{
+// zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC) /* {{{ */
+/*{
     zval *zoutput;
     MAKE_STD_ZVAL(zoutput);
     object_init_ex(zoutput, riak_get_output_ce);
@@ -46,7 +46,7 @@ zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zk
     }
     riak_set_output_properties(zoutput, zkey, &getobj->object TSRMLS_CC);
     return zoutput;
-}
+}*/
 /* }}} */
 
 
diff --git a/riak/output/get_output.h b/riak/output/get_output.h
index cb81c06..a9329ba 100644
--- a/riak/output/get_output.h
+++ b/riak/output/get_output.h
@@ -22,7 +22,7 @@
 extern zend_class_entry *riak_get_output_ce;
 
 void riak_output_get_output_init(TSRMLS_D);
-zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC);
+// zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC);
 
 PHP_METHOD(Riak_Output_GetOutput, isUnchanged);
 
diff --git a/riak/output/index_output.c b/riak/output/index_output.c
index 0f296ea..ca30a42 100644
--- a/riak/output/index_output.c
+++ b/riak/output/index_output.c
@@ -47,6 +47,7 @@ void riak_output_index_output_init(TSRMLS_D)
     zend_declare_property_null(riak_index_output_ce, "continuation", sizeof("continuation")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
 }
 
+/*
 zval *get_index_output_from_riack_string_list(RIACK_STRING_LIST *result_keys TSRMLS_DC)
 {
     zval *zresult;
@@ -72,8 +73,8 @@ zval *get_index_output_from_riack_string_list(RIACK_STRING_LIST *result_keys TSR
         zval_ptr_dtor(&zcurrent);
     }
     return zresult;
-}
-
+}*/
+/*
 zval *riak_index_output_from_string_list_and_continuation(RIACK_STRING_LIST *result_keys, RIACK_STRING *continuation TSRMLS_DC) {
     zval* zlist, *zresult;
     MAKE_STD_ZVAL(zresult);
@@ -91,7 +92,7 @@ zval *riak_index_output_from_string_list_and_continuation(RIACK_STRING_LIST *res
     }
     zval_ptr_dtor(&zlist);
     return zresult;
-}
+}*/
 
 PHP_METHOD(Riak_Index_Output, __construct)
 {
diff --git a/riak/output/index_output.h b/riak/output/index_output.h
index 1c95a82..9a2f7ae 100644
--- a/riak/output/index_output.h
+++ b/riak/output/index_output.h
@@ -23,7 +23,7 @@ extern zend_class_entry *riak_index_output_ce;
 
 void riak_output_index_output_init(TSRMLS_D);
 
-zval *riak_index_output_from_string_list_and_continuation(RIACK_STRING_LIST *result_keys, RIACK_STRING *continuation TSRMLS_DC);
+// zval *riak_index_output_from_string_list_and_continuation(RIACK_STRING_LIST *result_keys, RIACK_STRING *continuation TSRMLS_DC);
 
 PHP_METHOD(Riak_Index_Output, __construct);
 PHP_METHOD(Riak_Index_Output, getResultList);
diff --git a/riak/output/output.c b/riak/output/output.c
index 3f90e66..f6608c3 100644
--- a/riak/output/output.c
+++ b/riak/output/output.c
@@ -56,8 +56,9 @@ void riak_output_init(TSRMLS_D)/* {{{ */
 }
 /* }}} */
 
-void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC) /* {{{ */
-{
+// void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC) /* {{{ */
+// {
+/*
     size_t content_cnt, i;
     zval *zobjectlist;
     if (obj->vclock.len > 0) {
@@ -99,7 +100,8 @@ void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT*
     }
     zend_update_property(riak_output_ce, zoutput, "objectList", sizeof("objectList")-1, zobjectlist TSRMLS_CC);
     zval_ptr_dtor(&zobjectlist);
-}
+    */
+// }
 /* }}} */
 
 /*************************************************************
diff --git a/riak/output/output.h b/riak/output/output.h
index 75d285e..ee55099 100644
--- a/riak/output/output.h
+++ b/riak/output/output.h
@@ -23,7 +23,7 @@ extern zend_class_entry *riak_output_ce;
 
 void riak_output_init(TSRMLS_D);
 
-void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC);
+// void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC);
 
 PHP_METHOD(Riak_Output_Output, getObjectList);
 PHP_METHOD(Riak_Output_Output, getVClock);
diff --git a/riak/output/put_output.c b/riak/output/put_output.c
index fb2a9a4..92b49a4 100644
--- a/riak/output/put_output.c
+++ b/riak/output/put_output.c
@@ -33,13 +33,13 @@ void riak_output_put_output_init(TSRMLS_D)/* {{{ */
 /* }}} */
 
 
-zval *put_output_from_riack_object(struct RIACK_OBJECT* obj, zval* zkey TSRMLS_DC) /* {{{ */
-{
+// zval *put_output_from_riack_object(struct RIACK_OBJECT* obj, zval* zkey TSRMLS_DC) /* {{{ */
+/*{
     zval *zoutput;
     MAKE_STD_ZVAL(zoutput);
     object_init_ex(zoutput, riak_put_output_ce);
     riak_set_output_properties(zoutput, zkey, obj TSRMLS_CC);
     return zoutput;
-}
+}*/
 /* }}} */
 
diff --git a/riak/output/put_output.h b/riak/output/put_output.h
index 8047000..d29761f 100644
--- a/riak/output/put_output.h
+++ b/riak/output/put_output.h
@@ -22,6 +22,6 @@
 extern zend_class_entry *riak_put_output_ce;
 
 void riak_output_put_output_init(TSRMLS_D);
-zval *put_output_from_riack_object(struct RIACK_OBJECT* obj, zval* zkey TSRMLS_DC);
+// zval *put_output_from_riack_object(struct RIACK_OBJECT* obj, zval* zkey TSRMLS_DC);
 
 #endif //RIAK_OUTPUT__PUT_OUTPUT__H__
diff --git a/riak/pool.c b/riak/pool.c
index ab5d4c4..ac58ca2 100644
--- a/riak/pool.c
+++ b/riak/pool.c
@@ -31,10 +31,10 @@ zend_bool ensure_connected(riak_context *context TSRMLS_DC) /* {{{ */
        char szport[20];
        sprintf(szport, "%d", context->port);
        riak_error err = riak_connection_new(context->config,
-                                            context->connection,
+                                            &context->connection,
                                             context->szhost, szport, NULL);
-       if (err = ERIAK_OK) {
-           connection->needs_reconnect = 0;
+       if (err == ERIAK_OK) {
+           context->needs_reconnect = 0;
        } else {
            result = 0;
        }
@@ -54,13 +54,12 @@ zend_bool ensure_connected_init(riak_context *context, char* host, int host_len,
             strncmp(context->szhost, host, host_len) != 0 ||
             context->port != port) {
         riak_c_persistent_free(context->szhost);
-        char *szhost;
         // to be on the safe side make a copy with zero termination
         context->szhost = riak_c_persistent_alloc(host_len+1);
         memcpy(context->szhost, host, host_len);
-        szhost[host_len] = 0;
+        context->szhost[host_len] = 0;
         context->port = port;
-        connection->needs_reconnect = 1;
+        context->needs_reconnect = 1;
     }
     result = ensure_connected(context TSRMLS_CC);
 /* TODO
@@ -72,9 +71,9 @@ zend_bool ensure_connected_init(riak_context *context, char* host, int host_len,
 }
 /* }}} */
 
-void mark_for_reconnect(riak_connection *connection) /* {{{ */
+void mark_for_reconnect(riak_context *context) /* {{{ */
 {
-   connection->needs_reconnect = 1;
+   context->needs_reconnect = 1;
 }
 /* }}} */
 
@@ -85,11 +84,11 @@ void mark_for_reconnect(riak_connection *connection) /* {{{ */
 zend_bool lock_pool(TSRMLS_D) /* {{{ */
 {
 #ifdef ZTS
-  if (tsrm_mutex_lock(RIAK_GLOBAL(pool_mutex)) == 0) {
-    return 1;
-  } else {
-    return 0;
-  }
+    if (tsrm_mutex_lock(RIAK_GLOBAL(pool_mutex)) == 0) {
+        return 1;
+    } else {
+        return 0;
+    }
 #endif
   return 1;
 }
@@ -98,12 +97,12 @@ zend_bool lock_pool(TSRMLS_D) /* {{{ */
 void unlock_pool(TSRMLS_D) /* {{{ */
 {
 #ifdef ZTS
-  tsrm_mutex_unlock(RIAK_GLOBAL(pool_mutex));
+    tsrm_mutex_unlock(RIAK_GLOBAL(pool_mutex));
 #endif
 }
 /* }}} */
 
-void release_connection(riak_context *context TSRMLS_DC) /* {{{ */
+void release_context(riak_context *context TSRMLS_DC) /* {{{ */
 {
    riak_connection_pool* pool = NULL;
    RIAK_GLOBAL(open_connections)--;
@@ -112,9 +111,9 @@ void release_connection(riak_context *context TSRMLS_DC) /* {{{ */
       if (lock_pool(TSRMLS_C)) {
          context->last_used_at = time(NULL);
          RIAK_GLOBAL(open_connections_persistent)--;
-         pool = pool_for_host_port(context->host,
+         pool = pool_for_host_port(context->szhost,
             strlen(context->szhost), context->port TSRMLS_CC);
-         release_connection_from_pool(pool, context);
+         release_context_from_pool(pool, context);
          unlock_pool(TSRMLS_C);
       }
    } else {
@@ -122,12 +121,11 @@ void release_connection(riak_context *context TSRMLS_DC) /* {{{ */
           riak_connection_free(&(context->connection));
           context->connection = 0;
       }
-      pefree(connection, 0);
    }
 }
 /* }}} */
 
-riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /* {{{ */
+riak_context *take_connection(char* host, int host_len, int port TSRMLS_DC) /* {{{ */
 {
    riak_context* context;
    riak_connection_pool* pool;
@@ -141,8 +139,8 @@ riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /
    if (entry) {
       context = &entry->context;
       if (!ensure_connected_init(context, host, host_len, port TSRMLS_CC)) {
-         connection->needs_reconnect = 1;
-         release_connection_from_pool(pool, connection);
+         context->needs_reconnect = 1;
+         release_context_from_pool(pool, context);
          return NULL;
       }
       RIAK_GLOBAL(open_connections_persistent)++;
@@ -153,24 +151,24 @@ riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC) /
       context->persistent = 0;
       context->last_used_at = time(NULL);
       if (!ensure_connected_init(context, host, host_len, port TSRMLS_CC)) {
-         release_connection(context TSRMLS_CC);
+         release_context(context TSRMLS_CC);
          return NULL;
       }
    }
-   if (connection) {
+   if (context) {
       RIAK_GLOBAL(open_connections)++;
    }
-   return connection;
+   return context;
 }
 /* }}} */
 
-void release_connection_from_pool(riak_connection_pool* pool, riak_connection *connection) /* {{{ */
+void release_context_from_pool(riak_connection_pool* pool, riak_context *context) /* {{{ */
 {
     int i;
     riak_connection_pool_entry* current_entry;
     for (i=0; i<pool->count; ++i) {
         current_entry = &pool->entries[i];
-        if (&(current_entry->connection) == connection) {
+        if (&(current_entry->context) == context) {
             current_entry->in_use = 0;
         }
     }
@@ -194,6 +192,14 @@ riak_connection_pool_entry *take_connection_entry_from_pool(riak_connection_pool
                                 riak_c_persistent_free,
                                 riak_c_pb_persistent_alloc,
                                 riak_c_pb_persistent_free);
+/*
+riak_config_new(riak_config     **cfg,
+                riak_alloc_fn     alloc,
+                riak_realloc_fn   realloc,
+                riak_free_fn      freeme,
+                riak_pb_alloc_fn  pb_alloc,
+                riak_pb_free_fn   pb_free);
+*/
             }
             return current_entry;
         }
@@ -242,7 +248,7 @@ void le_riak_connections_pefree(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
     riak_connection_pool *pool = (riak_connection_pool*)rsrc->ptr;
     if (pool->entries) {
         for (i=0; i<pool->count; ++i) {
-            if (pool->entries[i].context.client) {
+            if (pool->entries[i].context.connection) {
                 riak_connection_free(&(pool->entries[i].context.connection));
             }
             if (pool->entries[i].context.config) {
diff --git a/riak/pool.h b/riak/pool.h
index bb6ea3d..7d56dfd 100644
--- a/riak/pool.h
+++ b/riak/pool.h
@@ -48,13 +48,13 @@ zend_bool ensure_connected(riak_context *context TSRMLS_DC);
 zend_bool ensure_connected_init(riak_context *context, char* host, int host_len, int port TSRMLS_DC);
 void mark_for_reconnect(riak_context *context);
 
-void release_connection(riak_context *context TSRMLS_DC);
-riak_connection *take_connection(char* host, int host_len, int port TSRMLS_DC);
+void release_context(riak_context *context TSRMLS_DC);
+riak_context *take_connection(char* host, int host_len, int port TSRMLS_DC);
 
 void le_riak_connections_pefree(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
 /* Internal */
-void release_connection_from_pool(riak_connection_pool* pool, riak_connection *connection);
+void release_context_from_pool(riak_connection_pool* pool, riak_context *context);
 riak_connection_pool *pool_for_host_port(char* host, int host_len, int port TSRMLS_DC);
 riak_connection_pool_entry *take_connection_entry_from_pool(riak_connection_pool *pool);
 riak_connection_pool* initialize_pool(TSRMLS_D);
diff --git a/riak/search/output/document_output.c b/riak/search/output/document_output.c
index c020125..6590a06 100644
--- a/riak/search/output/document_output.c
+++ b/riak/search/output/document_output.c
@@ -34,38 +34,38 @@ void riak_search_output_document_output_init(TSRMLS_D) /* {{{  */
 }
 /* }}} */
 
-zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC) /* {{{  */
-{
-    int cnt, i;
-    zval *zresult, *zarr;
-    MAKE_STD_ZVAL(zarr)
-    array_init(zarr);
+// zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC) /* {{{  */
+// {
+//     int cnt, i;
+//     zval *zresult, *zarr;
+//     MAKE_STD_ZVAL(zarr)
+//     array_init(zarr);
 
-    MAKE_STD_ZVAL(zresult)
-    object_init_ex(zresult, riak_search_doc_ce);
+//     MAKE_STD_ZVAL(zresult)
+//     object_init_ex(zresult, riak_search_doc_ce);
 
-    cnt = document->field_count;
-    for (i=0; i<cnt; ++i) {
-        char* szkey;
-        size_t key_len;
-        struct RIACK_PAIR *current_pair = &document->fields[i];
-        key_len = current_pair->key.len;
+//     cnt = document->field_count;
+//     for (i=0; i<cnt; ++i) {
+//         char* szkey;
+//         size_t key_len;
+//         struct RIACK_PAIR *current_pair = &document->fields[i];
+//         key_len = current_pair->key.len;
         // Silly... we need to make a copy because add_assoc relies on zero terminated string
         // riack will not zero terminate since it includes a length.
         // However estrndup will be nice enough to add a zero termination for us...
-        szkey = estrndup(current_pair->key.value, key_len);
-        if (current_pair->value_present) {
-            add_assoc_stringl_ex(zarr, szkey, key_len + 1,
-                                 (char*)current_pair->value, current_pair->value_len, 1);
-        } else {
-            add_assoc_null_ex(zarr,  szkey, key_len+1);
-        }
-        efree(szkey);
-    }
-    zend_update_property(riak_search_doc_ce, zresult, "fields", sizeof("fields")-1, zarr TSRMLS_CC);
-    zval_ptr_dtor(&zarr);
-    return zresult;
-}
+//         szkey = estrndup(current_pair->key.value, key_len);
+//         if (current_pair->value_present) {
+//             add_assoc_stringl_ex(zarr, szkey, key_len + 1,
+//                                  (char*)current_pair->value, current_pair->value_len, 1);
+//         } else {
+//             add_assoc_null_ex(zarr,  szkey, key_len+1);
+//         }
+//         efree(szkey);
+//     }
+//     zend_update_property(riak_search_doc_ce, zresult, "fields", sizeof("fields")-1, zarr TSRMLS_CC);
+//     zval_ptr_dtor(&zarr);
+//     return zresult;
+// }
 /* }}} */
 
 
diff --git a/riak/search/output/document_output.h b/riak/search/output/document_output.h
index 87cb4ad..a2c5b16 100644
--- a/riak/search/output/document_output.h
+++ b/riak/search/output/document_output.h
@@ -22,7 +22,7 @@
 extern zend_class_entry *riak_search_doc_ce;
 
 void riak_search_output_document_output_init(TSRMLS_D);
-zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC);
+// zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC);
 
 
 /* Riak\Search\Output\DocumentOutput */
diff --git a/riak/search/search.c b/riak/search/search.c
index 78f359b..36ad21e 100644
--- a/riak/search/search.c
+++ b/riak/search/search.c
@@ -73,7 +73,7 @@ PHP_METHOD(Riak_Search, __construct)
 Create a new Search object */
 PHP_METHOD(Riak_Search, search)
 {
-    riak_connection *connection;
+/*    riak_context *connection;
     struct RIACK_SEARCH_OPTIONAL_PARAMETERS search_params;
     struct RIACK_SEARCH_RESULT search_result;
     RIACK_STRING rsquery, rsindex;
@@ -98,7 +98,7 @@ PHP_METHOD(Riak_Search, search)
     zresult = riak_search_output_from_riack_search_result(&search_result TSRMLS_CC);
     riack_free_search_result(connection->client, &search_result);
 
-    RETURN_ZVAL(zresult, 0, 1);
+    RETURN_ZVAL(zresult, 0, 1);*/
 }
 /* }}} */
 
@@ -119,15 +119,16 @@ PHP_METHOD(Riak_Search, search)
         TARGET##_present = 1; \
         TARGET= Z_LVAL_P(ZTMP); }
 
-void riak_search_free_optional_params(struct RIACK_CLIENT *client, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC)
-{
-    size_t cnt = search_params->field_limits_count;
-    if (cnt > 0) {
-        RFREE(client, search_params->field_limits);
+// void riak_search_free_optional_params(struct RIACK_CLIENT *client, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC)
+// {
+//     size_t cnt = search_params->field_limits_count;
+//     if (cnt > 0) {
+//         RFREE(client, search_params->field_limits);
         /* The actual strings was not copied so they do not need to be freed */
-    }
-}
+//     }
+// }
 
+/*
 void riak_search_set_optional_params(struct RIACK_CLIENT *client, zval* zparams, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC)
 {
     zval *zprop;
@@ -150,7 +151,7 @@ void riak_search_set_optional_params(struct RIACK_CLIENT *client, zval* zparams,
             for (i=0; i<cnt; ++i) {
                 zval **zflpp;
                 /* important these string are not copied so only the array should be freed later */
-                if (zend_hash_index_find(Z_ARRVAL_P(zprop), i, (void**)&zflpp) == SUCCESS) {
+               /* if (zend_hash_index_find(Z_ARRVAL_P(zprop), i, (void**)&zflpp) == SUCCESS) {
                     if (Z_TYPE_PP(zflpp) == IS_STRING) {
                         search_params->field_limits[i].len = Z_STRLEN_PP(zflpp);
                         search_params->field_limits[i].value = Z_STRVAL_PP(zflpp);
@@ -159,4 +160,4 @@ void riak_search_set_optional_params(struct RIACK_CLIENT *client, zval* zparams,
             }
         }
     }
-}
+}*/
diff --git a/riak/search/search.h b/riak/search/search.h
index 437146c..32eeac1 100644
--- a/riak/search/search.h
+++ b/riak/search/search.h
@@ -24,11 +24,11 @@ extern zend_class_entry *riak_search_ce;
 /* helpers */
 
 void riak_search_init(TSRMLS_D);
-void riak_search_set_optional_params(struct RIACK_CLIENT *client, zval* zparams, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC);
-void riak_search_free_optional_params(struct RIACK_CLIENT *client, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC);
+// void riak_search_set_optional_params(struct RIACK_CLIENT *client, zval* zparams, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC);
+// void riak_search_free_optional_params(struct RIACK_CLIENT *client, struct RIACK_SEARCH_OPTIONAL_PARAMETERS* search_params TSRMLS_DC);
 
-zval* riak_search_output_from_riack_search_result(struct RIACK_SEARCH_RESULT *search_result TSRMLS_DC);
-zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC);
+// zval* riak_search_output_from_riack_search_result(struct RIACK_SEARCH_RESULT *search_result TSRMLS_DC);
+// zval *riak_search_document_from_riack_document(struct RIACK_SEARCH_DOCUMENT* document TSRMLS_DC);
 
 /* Search */
 

From 2afadc14b0a45032fb77fc78cf38a75e7545be12 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:09:52 +0100
Subject: [PATCH 05/22] More work

---
 config.m4           |  1 +
 php_riak.files      |  2 ++
 php_riak_internal.h | 48 +++++++++++++++----------
 riak/bucket.c       | 88 ++++++++++++++++++++++-----------------------
 riak/object.c       | 46 ++++++++++++------------
 riak/object.h       |  2 ++
 riak/pool.h         | 12 -------
 riak_c_helpers.c    | 41 +++++++++++++++++++++
 riak_c_helpers.h    | 25 +++++++++++++
 9 files changed, 167 insertions(+), 98 deletions(-)
 create mode 100644 riak_c_helpers.c
 create mode 100644 riak_c_helpers.h

diff --git a/config.m4 b/config.m4
index ab51d09..0af2d52 100644
--- a/config.m4
+++ b/config.m4
@@ -51,6 +51,7 @@ if test "$PHP_RIAK" != "no"; then
   PHP_NEW_EXTENSION(riak, php_riak.c \
     ht_utils.c \
     riak_session.c \
+    riak_c_helpers.c \
     riak/bucket.c \
     riak/connection.c \
     riak/object.c \
diff --git a/php_riak.files b/php_riak.files
index 59e4741..7f30d5e 100644
--- a/php_riak.files
+++ b/php_riak.files
@@ -204,3 +204,5 @@ tests/bucket.phpt
 tests/bucket_props.phpt
 tests/bucket_props_ext.phpt
 tests/builtin_conflict_resolver_db_objs.phpt
+riak_c_helpers.h
+riak_c_helpers.c
diff --git a/php_riak_internal.h b/php_riak_internal.h
index a7f159a..c61fc76 100644
--- a/php_riak_internal.h
+++ b/php_riak_internal.h
@@ -31,10 +31,10 @@
 * Macros
 *************************************************/
 
-#define RIACK_RETRY_OP(RIACK_RESULT_VAR, OPERATION) { long retry_cnt; \
-        retry_cnt = RIAK_GLOBAL(default_retries); \
-        do { RIACK_RESULT_VAR = OPERATION; retry_cnt--; } \
-        while (RIACK_RESULT_VAR != RIACK_SUCCESS && retry_cnt >= 0); }
+// #define RIACK_RETRY_OP(RIACK_RESULT_VAR, OPERATION) { long retry_cnt; \
+//         retry_cnt = RIAK_GLOBAL(default_retries); \
+//         do { RIACK_RESULT_VAR = OPERATION; retry_cnt--; } \
+//         while (RIACK_RESULT_VAR != RIACK_SUCCESS && retry_cnt >= 0); }
 
 #define RIAK_PUSH_PARAM(arg) zend_vm_stack_push(arg TSRMLS_CC)
 #define RIAK_POP_PARAM() (void)zend_vm_stack_pop(TSRMLS_C)
@@ -117,22 +117,22 @@
   RIAK_CALL_METHOD_HELPER(classname, name, retval, thisptr, 3, param3);     \
   RIAK_POP_PARAM(); RIAK_POP_PARAM();
 
-#define CHECK_RIACK_STATUS_THROW_ON_ERROR(CONNECTION, STATUS) \
-  if ( STATUS != RIACK_SUCCESS) { \
-    CONNECTION->needs_reconnect = 1; \
-    riak_throw_exception(CONNECTION->client,  STATUS TSRMLS_CC); \
-  }
+// #define CHECK_RIACK_STATUS_THROW_ON_ERROR(CONNECTION, STATUS) \
+//   if ( STATUS != RIACK_SUCCESS) { \
+//     CONNECTION->needs_reconnect = 1; \
+//     riak_throw_exception(CONNECTION->client,  STATUS TSRMLS_CC); \
+//   }
 
-#define CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(CONNECTION, STATUS) \
-  if ( STATUS != RIACK_SUCCESS) { \
-    CONNECTION->needs_reconnect = 1; \
-    riak_throw_exception(CONNECTION->client,  STATUS TSRMLS_CC); \
-  return; }
+//#define CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(CONNECTION, STATUS) \
+//  if ( STATUS != RIACK_SUCCESS) { \
+//    CONNECTION->needs_reconnect = 1; \
+//    riak_throw_exception(CONNECTION->client,  STATUS TSRMLS_CC); \
+//  return; }
 
-#define GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(CE, ZOBJ, KEY, ZVAL, RIACK_STR) ZVAL = zend_read_property(CE, ZOBJ, KEY, sizeof(KEY)-1, 1 TSRMLS_CC); \
-     if (Z_TYPE_P(ZVAL) == IS_STRING) { \
-       RIACK_STR.len = Z_STRLEN_P(ZVAL); \
-       RIACK_STR.value = Z_STRVAL_P(ZVAL); } else
+//#define GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(CE, ZOBJ, KEY, ZVAL, RIACK_STR) ZVAL = zend_read_property(CE, ZOBJ, KEY, sizeof(KEY)-1, 1 TSRMLS_CC); \
+//     if (Z_TYPE_P(ZVAL) == IS_STRING) { \
+//       RIACK_STR.len = Z_STRLEN_P(ZVAL); \
+//       RIACK_STR.value = Z_STRVAL_P(ZVAL); } else
 
 #define CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(CONTEXT, ERR) \
     if ( ERR != ERIAK_OK) { \
@@ -175,6 +175,18 @@ ZEND_BEGIN_MODULE_GLOBALS(riak)
 ZEND_END_MODULE_GLOBALS(riak)
 ZEND_EXTERN_MODULE_GLOBALS(riak)
 
+typedef struct {
+  riak_connection* connection;
+  riak_config* config;
+
+  char* szhost;
+  int port;
+
+  zend_bool needs_reconnect;
+  zend_bool persistent;
+  time_t last_used_at;
+} riak_context;
+
 /*************************************************
 * Functions
 *************************************************/
diff --git a/riak/bucket.c b/riak/bucket.c
index c90fa43..c7ce348 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -36,9 +36,12 @@
 #include "query/index_query.h"
 #include "output/key_stream_output.h"
 #include "crdt/counter.h"
+#include "riak_c_helpers.h"
 #include <messages/riak_delete.h>
+#include <messages/riak_put.h>
 #include <ext/spl/spl_iterators.h>
 #include <ext/spl/spl_array.h>
+#include <riak_object.h>
 
 riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC);
 
@@ -801,7 +804,7 @@ PHP_METHOD(RiakBucket, delete)
         zend_throw_exception(riak_badarguments_exception_ce, "Argument 1 passed to Riak\\Bucket#delete() must be a string or an instance of Riak\\Object.", 5001 TSRMLS_CC);
         return;
     }
-    ctx = get_riak_context(getThis());
+    ctx = get_riak_context(getThis() TSRMLS_CC);
     bucket = binary_bucket_name_new(ctx, getThis());
     opts = riak_delete_options_new(ctx->config);
 
@@ -822,14 +825,14 @@ PHP_METHOD(RiakBucket, delete)
 
         RIAK_CALL_METHOD(Riak_Input_DeleteInput, getVClock, &zget, zinput);
         if (Z_TYPE(zget) == IS_STRING) {
-            vclock = riak_binary_new(ctx->config, Z_STRLEN(zget), (riak_uint8_t *)Z_STRVAL(zget));
+            vclock = riak_binary_deep_from_stringl(ctx->config, Z_STRLEN(zget), Z_STRVAL(zget));
         }
         zval_dtor(&zget);
     }
     if (Z_TYPE_P(zparam) == IS_OBJECT) {
         RIAK_CALL_METHOD(RiakObject, getKey, &zkey, zparam);
         if (Z_TYPE(zkey) == IS_STRING && Z_STRVAL(zkey) > 0) {
-            key = riak_binary_new(ctx->config, Z_STRLEN(zkey), (riak_uint8_t *)Z_STRVAL(zkey));
+            key = riak_binary_deep_from_stringl(ctx->config, Z_STRLEN(zkey), Z_STRVAL(zkey));
         }
         // If vclock is not set retrieve from object
         if (!vclock) {
@@ -837,20 +840,20 @@ PHP_METHOD(RiakBucket, delete)
             RIAK_CALL_METHOD(RiakObject, getVClock, &zobjvclock, zparam);
             if (Z_TYPE(zobjvclock) == IS_STRING) {
                 // Set the vclock
-                vclock = riak_binary_new(ctx->config, Z_STRLEN(zobjvclock), (riak_uint8_t *)Z_STRVAL(zobjvclock));
+                vclock = riak_binary_deep_from_stringl(ctx->config, Z_STRLEN(zobjvclock), Z_STRVAL(zobjvclock));
                 zval_dtor(&zobjvclock);
             }
         }
     } else if (Z_TYPE_P(zparam) == IS_STRING && Z_STRVAL_P(zparam) > 0) {
-        key = riak_binary_new(ctx->config, Z_STRLEN_P(zparam), (riak_uint8_t *)Z_STRVAL_P(zparam));
+        key = riak_binary_deep_from_stringl(ctx->config, Z_STRLEN_P(zparam), Z_STRVAL_P(zparam));
     }
     if (vclock) {
         riak_delete_options_set_vclock(ctx->config, opts, vclock);
     }
     result = riak_delete(ctx->connection, bucket, key, opts);
 
-    if (vclock) riak_binary_free(ctx->config, &vclock);
-    riak_binary_free(ctx->config, &key);
+    if (vclock) riak_binary_deep_free(ctx->config, &vclock);
+    riak_binary_deep_free(ctx->config, &key);
     riak_binary_free(ctx->config, &bucket);
     riak_delete_options_free(ctx->config, &opts);
     CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(ctx, result);
@@ -861,32 +864,28 @@ PHP_METHOD(RiakBucket, delete)
 Store a RiakObject in riak, if something goes wrong an RiakException is thrown */
 PHP_METHOD(RiakBucket, put)
 {
-    /*
-    int riackResult;
-    zval *zObject, *zTmp, *zinput, *zout, zvclock;
-	struct RIACK_OBJECT obj, returnedObj;
-	struct RIACK_CONTENT riackContent;
-	struct RIACK_PUT_PROPERTIES props;
-	riak_connection *connection;
-    long options;
+    riak_context *ctx;
+    riak_error result;
+    zval *zobject, *zinput, zvclock;
+    riak_put_options *opts;
+    riak_put_response *response;
+    riak_object *obj;
+    riak_binary *bucket, *key;
 
-    options = 0;
     zinput = NULL;
-    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|O", &zObject, riak_object_ce, &zinput, riak_put_input_ce) == FAILURE) {
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|O", &zobject, riak_object_ce, &zinput, riak_put_input_ce) == FAILURE) {
         zend_throw_exception(riak_badarguments_exception_ce, "Bad or missing argument", 500 TSRMLS_CC);
-		return;
-	}
-    connection = get_riak_connection(getThis() TSRMLS_CC);
+        return;
+    }
+    ctx = get_riak_context(getThis() TSRMLS_CC);
+    opts = riak_put_options_new(ctx->config);
+    obj = riak_object_new(ctx->config);
+    bucket = binary_bucket_name_new(ctx, getThis() TSRMLS_CC);
+    key = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobject, "key");
 
-	memset(&obj, 0, sizeof(obj));
-	memset(&returnedObj, 0, sizeof(returnedObj));
-	memset(&riackContent, 0, sizeof(riackContent));
-    memset(&props, 0, sizeof(props));
-    /* fill content */
-    /*set_riak_content_from_object(&riackContent, zObject, connection->client TSRMLS_CC);
     ZVAL_NULL(&zvclock);
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
-        zval zget;
+        /*
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_PutInput, zget, getReturnHead, props.return_head);
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_PutInput, zget, getReturnBody, props.return_body);
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_PutInput, zget, getIfNotModified, props.if_not_modified);
@@ -895,32 +894,31 @@ PHP_METHOD(RiakBucket, put)
         RIAK_REQ_PROP_SET_LONG(Riak_Input_PutInput, zget, getDW, props.dw);
         RIAK_REQ_PROP_SET_LONG(Riak_Input_PutInput, zget, getPW, props.pw);
         RIAK_CALL_METHOD(Riak_Input_PutInput, getVClock, &zvclock, zinput);
+*/
     }
     if (Z_TYPE(zvclock) == IS_NULL) {
-        // vclock was not set from input, try getting it from object instead
-        RIAK_CALL_METHOD(RiakObject, getVClock, &zvclock, zObject);
+        RIAK_CALL_METHOD(RiakObject, getVClock, &zvclock, zobject);
     }
     if (Z_TYPE(zvclock) == IS_STRING) {
-        RMALLOCCOPY(connection->client, obj.vclock.clock, obj.vclock.len, Z_STRVAL(zvclock), Z_STRLEN(zvclock));
+        //RMALLOCCOPY(connection->client, obj.vclock.clock, obj.vclock.len, Z_STRVAL(zvclock), Z_STRLEN(zvclock));
         zval_dtor(&zvclock);
     }
 
-    /* Set bucket name */
-    /*obj.bucket = riack_name_from_bucket(getThis() TSRMLS_CC);
-	obj.content_count = 1;
-	obj.content = &riackContent;
-    /* No ket provided on function call, get it from RiakObject */
-    /*GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(riak_object_ce, zObject, "key", zTmp, obj.key) {
-        // Key is null this is ok
-        obj.key.len = 0;
-        obj.key.value = 0;
-    }
-    RIACK_RETRY_OP(riackResult, riack_put(connection->client, obj, &returnedObj, &props));
-    if (obj.vclock.clock) {
-        RFREE(connection->client, obj.vclock.clock);
-    }
-	CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackResult);
+    riak_object_properties_set(obj, zobject, ctx TSRMLS_CC);
+    riak_object_set_bucket(obj, bucket);
+    if (key) riak_object_set_key(obj, key);
+    //
+    result = riak_put(ctx->connection, obj, opts, &response);
+    // TODO Handle response values!!
+    riak_put_options_free(ctx->config, &opts);
+    riak_object_free(ctx->config, &obj);
+    riak_put_response_free(ctx->config, &response);
+    riak_binary_free(ctx->config, &bucket);
+    riak_binary_free(ctx->config, &key);
 
+    CHECK_RIAK_STATUS_THROW_AND_RETURN_ON_ERROR(ctx, result);
+
+    /*
     // Now make put output from the response
     MAKE_STD_ZVAL(zTmp);
     if (returnedObj.key.len > 0) {
diff --git a/riak/object.c b/riak/object.c
index d0f7618..08dde01 100644
--- a/riak/object.c
+++ b/riak/object.c
@@ -19,6 +19,7 @@
 #include "link.h"
 #include "ht_utils.h"
 #include "exception/exception.h"
+#include "riak_c_helpers.h"
 
 zend_class_entry *riak_object_ce;
 
@@ -593,27 +594,26 @@ void riak_key_from_object(zval *zobject, char** key, int* keylen TSRMLS_DC)/* {{
 // }
 /* }}} */
 
-/* Fill out members of a content struct with this objects values */
-// void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC)/* {{{ */
-// {
-    /*zval* zTmp;
-    zTmp = zend_read_property(riak_object_ce, object, "content", sizeof("content")-1, 1 TSRMLS_CC);
-	if (Z_TYPE_P(zTmp) == IS_STRING) {
-		content->data_len = Z_STRLEN_P(zTmp);
-		content->data = (uint8_t*)Z_STRVAL_P(zTmp);
-	}
-	// Set content type
-	GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(riak_object_ce, object, "contentType", zTmp, content->content_type) ;
-	// Set content encoding
-	GET_PROPERTY_INTO_RIACK_STR_OR_ELSE(riak_object_ce, object, "contentEncoding", zTmp, content->content_encoding) ;
-
-    zTmp = zend_read_property(riak_object_ce, object, "indexes", sizeof("indexes")-1, 1 TSRMLS_CC);
-    set_indexes_from_object(content, zTmp, client TSRMLS_CC);
-
-	zTmp = zend_read_property(riak_object_ce, object, "metadata", sizeof("metadata")-1, 1 TSRMLS_CC);
-	set_metadata_from_object(content, zTmp, client TSRMLS_CC);
-
-    zTmp = zend_read_property(riak_object_ce, object, "links", sizeof("links")-1, 1 TSRMLS_CC);
-    set_links_from_object(content, zTmp, client TSRMLS_CC); */
-// }
+void riak_object_properties_set(riak_object *obj, zval* zobj, riak_context *ctx TSRMLS_DC)/* {{{ */
+{
+    zval* ztmp;
+    riak_binary *bin;
+    ztmp = zend_read_property(riak_object_ce, zobj, "content", sizeof("content")-1, 1 TSRMLS_CC);
+    if (Z_TYPE_P(ztmp) == IS_STRING) {
+        bin = riak_binary_new(ctx->config, Z_STRLEN_P(ztmp), (riak_uint8_t*)Z_STRVAL_P(ztmp));
+        riak_object_set_value(obj, bin);
+    }
+    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentType");
+    riak_object_set_content_type(obj, bin);
+    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentEncoding");
+    riak_object_set_encoding(obj, bin);
+
+    ztmp = zend_read_property(riak_object_ce, zobj, "indexes", sizeof("indexes")-1, 1 TSRMLS_CC);
+    // TODO Set set_indexes_from_object
+    ztmp = zend_read_property(riak_object_ce, zobj, "metadata", sizeof("metadata")-1, 1 TSRMLS_CC);
+    // TODO Set set_metadata_from_object
+    ztmp = zend_read_property(riak_object_ce, zobj, "links", sizeof("links")-1, 1 TSRMLS_CC);
+    // TODO Set set_links_from_object
+}
 /* }}} */
+
diff --git a/riak/object.h b/riak/object.h
index 39d0b18..4086457 100644
--- a/riak/object.h
+++ b/riak/object.h
@@ -19,6 +19,7 @@
 #define RIAK_OBJECT__H__
 
 #include "php_riak_internal.h"
+#include <riak_object.h>
 
 extern zend_class_entry *riak_object_ce;
 
@@ -29,6 +30,7 @@ void riak_object_init(TSRMLS_D);
 //  void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC);
 // TODO
 //  void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC);
+void riak_object_properties_set(riak_object *obj, zval* zobj, riak_context *ctx TSRMLS_DC);
 
 void riak_key_from_object(zval *zobject, char** key, int* keylen  TSRMLS_DC);
 
diff --git a/riak/pool.h b/riak/pool.h
index 7d56dfd..7b01cf5 100644
--- a/riak/pool.h
+++ b/riak/pool.h
@@ -22,18 +22,6 @@
 #include <php.h>
 #include <time.h>
 
-typedef struct {
-  riak_connection* connection;
-  riak_config* config;
-
-  char* szhost;
-  int port;
-
-  zend_bool needs_reconnect;
-  zend_bool persistent;
-  time_t last_used_at;
-} riak_context;
-
 typedef struct _riak_connection_pool_entry {
   zend_bool in_use;
   riak_context context;
diff --git a/riak_c_helpers.c b/riak_c_helpers.c
new file mode 100644
index 0000000..c908d0b
--- /dev/null
+++ b/riak_c_helpers.c
@@ -0,0 +1,41 @@
+/*
+   Copyright 2014: Kaspar Bach Pedersen
+
+   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 "riak_c_helpers.h"
+
+riak_binary* riak_binary_shallow_from_property(riak_config *cfg,
+        zend_class_entry *scope, zval* zobj, char* property_name TSRMLS_DC)/* {{{ */
+{
+    zval *zproperty;
+    riak_binary *bin;
+    zproperty = zend_read_property(scope, zobj, property_name, sizeof(property_name)-1, 1 TSRMLS_CC);
+    if (Z_TYPE_P(zproperty) == IS_STRING) {
+        bin = riak_binary_new(cfg, Z_STRLEN_P(zproperty), (riak_uint8_t*)Z_STRVAL_P(zproperty));
+    } else {
+        bin = 0;
+    }
+    return bin;
+}
+/* }}} */
+
+riak_binary* riak_binary_deep_from_stringl(riak_config *cfg, int len, const char* val)/* {{{ */
+{
+    riak_binary* bin;
+    bin = riak_binary_new(cfg, 0, 0);
+    riak_binary_from_stringl(bin, (riak_size_t)len, (riak_uint8_t*)val);
+    return bin;
+}
+/* }}} */
diff --git a/riak_c_helpers.h b/riak_c_helpers.h
new file mode 100644
index 0000000..f35c91d
--- /dev/null
+++ b/riak_c_helpers.h
@@ -0,0 +1,25 @@
+/*
+   Copyright 2014: Kaspar Bach Pedersen
+
+   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 RIAK_C_HELPERS_H
+#define RIAK_C_HELPERS_H
+
+#include "php_riak_internal.h"
+#include <riak_binary.h>
+
+riak_binary* riak_binary_deep_from_stringl(riak_config *cfg, int len, const char* val);
+riak_binary* riak_binary_shallow_from_property(riak_config *cfg, zend_class_entry *scope, zval* zobj, char* property_name TSRMLS_DC);
+
+#endif // RIAK_C_HELPERS_H

From 86497329e220ed5301fe274dc4285afa2c4902a5 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:41:57 +0100
Subject: [PATCH 06/22] Little cleanup and travis update

---
 .travis.yml            |  1 -
 riak/bucket.c          | 72 +++++++++++++++++-------------------------
 script/build_module.sh |  7 +++-
 3 files changed, 35 insertions(+), 45 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 14c1782..cb4c247 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -12,7 +12,6 @@ before_install:
   - ulimit -n 2048
   - sudo service riak start
 before_script:
-  - git submodule update --init --recursive
   - ./script/build_module.sh
   - search-cmd install testsearch
 script:
diff --git a/riak/bucket.c b/riak/bucket.c
index c7ce348..fe69478 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -39,9 +39,10 @@
 #include "riak_c_helpers.h"
 #include <messages/riak_delete.h>
 #include <messages/riak_put.h>
+#include <messages/riak_get.h>
+#include <riak_object.h>
 #include <ext/spl/spl_iterators.h>
 #include <ext/spl/spl_array.h>
-#include <riak_object.h>
 
 riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC);
 
@@ -938,26 +939,26 @@ PHP_METHOD(RiakBucket, put)
 Get value from riak */
 PHP_METHOD(RiakBucket, get)
 {
-/*	char *key;
-    int keyLen, riackResult;
-    zval *zKey, *zinput;
-	struct RIACK_GET_PROPERTIES props;
-	struct RIACK_GET_OBJECT getResult;
-	RIACK_STRING rsBucket, rsKey;
-	riak_connection *connection;
+    riak_error result;
+    riak_context *ctx;
+    riak_get_response *response;
+    riak_get_options *opts;
+    riak_binary *bucket, *key;
+    zval *zinput;
+    char *skey;
+    int skeylen;
 
     zinput = NULL;
-    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|o", &key, &keyLen, &zinput) == FAILURE) {
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|o", &skey, &skeylen, &zinput) == FAILURE) {
         zend_throw_exception(riak_badarguments_exception_ce, "Bad or missing argument", 500 TSRMLS_CC);
-		return;
+        return;
     }
-    connection = get_riak_connection(getThis() TSRMLS_CC);
-
-	MAKE_STD_ZVAL(zKey);
-	ZVAL_STRINGL(zKey, key, keyLen, 1);
-	memset(&props, 0, sizeof(props));
-    memset(&getResult, 0, sizeof(getResult));
+    ctx = get_riak_context(getThis() TSRMLS_CC);
+    bucket = binary_bucket_name_new(ctx, getThis() TSRMLS_CC);
+    key = riak_binary_new(ctx->config, (riak_uint32_t)skeylen, (riak_uint8_t*)key);
+    opts = riak_get_options_new(ctx->config);
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
+/*
         zval zget;
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, zget, getReturnHead, props.head);
         RIAK_REQ_PROP_SET_LONG(Riak_Input_GetInput, zget, getR, props.r);
@@ -971,42 +972,37 @@ PHP_METHOD(RiakBucket, get)
             RMALLOCCOPY(connection->client, props.if_modified.clock, props.if_modified.len, Z_STRVAL(zget), Z_STRLEN(zget));
             zval_dtor(&zget);
         }
+*/
     }
-
-    rsKey.len   = keyLen;
-    rsKey.value = key;
-    rsBucket    = riack_name_from_bucket(getThis() TSRMLS_CC);
-
-    RIACK_RETRY_OP(riackResult, riack_get(connection->client, rsBucket, rsKey, &props, &getResult));
-    if (props.if_modified.clock) {
-        RFREE(connection->client, props.if_modified.clock);
-    }
-
-    if (riackResult == RIACK_SUCCESS) {
+    result = riak_get(ctx->connection, bucket, key, opts, &response);
+    if (result == ERIAK_OK) {
         zval *zout, *zResolver;
         zout      = get_output_from_riack_get_object(&getResult, zKey TSRMLS_CC);
         zResolver = (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT)
             ? zend_read_property(riak_get_resolver_input_ce, zinput, "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC)
             : zend_read_property(riak_bucket_ce, getThis(), "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC);
-
+        /*
         if (Z_TYPE_P(zResolver) == IS_OBJECT) {
             zend_update_property(riak_output_ce, zout, "conflictResolver", sizeof("conflictResolver")-1, zResolver TSRMLS_CC);
         }
         zend_update_property(riak_output_ce, zout, "bucket", sizeof("bucket")-1, getThis() TSRMLS_CC);
         RETVAL_ZVAL(zout, 0, 1);
         riack_free_get_object(connection->client, &getResult);
+*/
     } else {
-        connection->needs_reconnect = 1;
+        ctx->needs_reconnect = 1;
         // Hack warning, same as above
         if (!EG(current_execute_data)) {
             // No stack dont throw
             RETVAL_NULL();
         } else {
-            riak_throw_exception(connection->client,  riackResult TSRMLS_CC);
+            riak_throw_exception(ctx->connection, result TSRMLS_CC);
         }
-	}
-    zval_ptr_dtor(&zKey);
-*/
+    }
+    riak_get_response_free(ctx->config, &response);
+    riak_get_options_free(ctx->config, &opts);
+    riak_binary_free(ctx->config, &bucket);
+    riak_binary_free(ctx->config, &key);
 }
 /* }}} */
 
@@ -1083,16 +1079,6 @@ riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket)
     return riak_binary_new(ctx->config, namelen, (riak_uint8_t*)name);
 }
 
-// RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC)/* {{{ */
-// {
-//     RIACK_STRING bucketName;
-//     int namelen;
-//     riak_name_from_bucket(bucket, &bucketName.value, &namelen TSRMLS_CC);
-//     bucketName.len = namelen;
-//     return bucketName;
-// }
-/* }}} */
-
 riak_context *get_riak_context(zval *zbucket TSRMLS_DC)/* {{{ */
 {
     zval *zclient;
diff --git a/script/build_module.sh b/script/build_module.sh
index 97e401e..7136caa 100755
--- a/script/build_module.sh
+++ b/script/build_module.sh
@@ -1,6 +1,11 @@
 #!/bin/bash
+git clone https://github.com/TriKaspar/riak-c-client.git
+cd riak-c-client
+./autogen.sh
+./configure
+make
 
 phpize
-./configure --quiet
+./configure --with-riak-c-dir=./riak-c-client/src --quiet
 make install
 echo "extension=riak.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

From a1a97439191b6cf08f31f7557c806a4d9b9e0801 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:46:20 +0100
Subject: [PATCH 07/22] fun with travis

---
 .travis.yml | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index cb4c247..8399d4d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,10 +7,14 @@ before_install:
   - curl http://apt.basho.com/gpg/basho.apt.key | sudo apt-key add -
   - sudo bash -c "echo deb http://apt.basho.com $(lsb_release -sc) main > /etc/apt/sources.list.d/basho.list"
   - sudo apt-get update
-  - sudo apt-get install -qq valgrind
+  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler dev-libprotobuf dev-libprotoc libcunit1 libcunit1-ncurses-dev
   - "yes n | sudo apt-get install riak"
   - ulimit -n 2048
   - sudo service riak start
+  - wget https://protobuf-c.googlecode.com/files/protobuf-c-0.15.tar.gz
+  - cd protobuf-c-0.15
+  - "./configure && make && sudo make install"
+  - cd ..
 before_script:
   - ./script/build_module.sh
   - search-cmd install testsearch

From b501fc8d2838bb86a30496473194248d259a6b39 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:51:33 +0100
Subject: [PATCH 08/22] fun with travis

---
 .travis.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index 8399d4d..0f7ba32 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,7 +7,7 @@ before_install:
   - curl http://apt.basho.com/gpg/basho.apt.key | sudo apt-key add -
   - sudo bash -c "echo deb http://apt.basho.com $(lsb_release -sc) main > /etc/apt/sources.list.d/basho.list"
   - sudo apt-get update
-  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler dev-libprotobuf dev-libprotoc libcunit1 libcunit1-ncurses-dev
+  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libprotobuf libprotoc libcunit1 libcunit1-ncurses-dev
   - "yes n | sudo apt-get install riak"
   - ulimit -n 2048
   - sudo service riak start

From 0590857235c6b5fa2aac59f2465ea13375ae99ad Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:54:33 +0100
Subject: [PATCH 09/22] Maybe i should spin up a ubuntu to get the right
 package names..

---
 .travis.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index 0f7ba32..be0d915 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,7 +7,7 @@ before_install:
   - curl http://apt.basho.com/gpg/basho.apt.key | sudo apt-key add -
   - sudo bash -c "echo deb http://apt.basho.com $(lsb_release -sc) main > /etc/apt/sources.list.d/basho.list"
   - sudo apt-get update
-  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libprotobuf libprotoc libcunit1 libcunit1-ncurses-dev
+  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libprotobuf-dev libprotoc-dev libcunit1 libcunit1-ncurses-dev
   - "yes n | sudo apt-get install riak"
   - ulimit -n 2048
   - sudo service riak start

From 54a168b6cbd4c6916fc0a437fbb4ef39fc10d8a4 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 12:58:19 +0100
Subject: [PATCH 10/22] asdf

---
 .travis.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index be0d915..3efa6dc 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,7 +7,7 @@ before_install:
   - curl http://apt.basho.com/gpg/basho.apt.key | sudo apt-key add -
   - sudo bash -c "echo deb http://apt.basho.com $(lsb_release -sc) main > /etc/apt/sources.list.d/basho.list"
   - sudo apt-get update
-  - sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libprotobuf-dev libprotoc-dev libcunit1 libcunit1-ncurses-dev
+  - "sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libcunit1 libcunit1-ncurses-dev"
   - "yes n | sudo apt-get install riak"
   - ulimit -n 2048
   - sudo service riak start

From 7398d0f8f195316750a92e650a3bba7d19a89c51 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 13:00:43 +0100
Subject: [PATCH 11/22] asdf

---
 .travis.yml | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index 3efa6dc..9c43e88 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,7 +7,13 @@ before_install:
   - curl http://apt.basho.com/gpg/basho.apt.key | sudo apt-key add -
   - sudo bash -c "echo deb http://apt.basho.com $(lsb_release -sc) main > /etc/apt/sources.list.d/basho.list"
   - sudo apt-get update
-  - "sudo apt-get install -qq valgrind libevent-dev protobuf-c-compiler libcunit1 libcunit1-ncurses-dev"
+  - sudo apt-get install -qq valgrind
+  - sudo apt-get install -qq libevent-dev
+  - sudo apt-get install -qq protobuf-c-compiler
+  - sudo apt-get install -qq libprotobuf-dev
+  - sudo apt-get install -qq libprotoc-dev
+  - sudo apt-get install -qq libcunit1
+  - sudo apt-get install -qq libcunit1-ncurses-dev
   - "yes n | sudo apt-get install riak"
   - ulimit -n 2048
   - sudo service riak start

From 1eb7f932b83feba39a8d740f966d9ec815ba3b9b Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 13:04:06 +0100
Subject: [PATCH 12/22] asdf2

---
 .travis.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.travis.yml b/.travis.yml
index 9c43e88..1c2fa66 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -18,6 +18,7 @@ before_install:
   - ulimit -n 2048
   - sudo service riak start
   - wget https://protobuf-c.googlecode.com/files/protobuf-c-0.15.tar.gz
+  - tar xvzf protobuf-c-0.15.tar.gz
   - cd protobuf-c-0.15
   - "./configure && make && sudo make install"
   - cd ..

From fbca9da44e7c6b8fadc069b3f255caf9f5668882 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 13:07:51 +0100
Subject: [PATCH 13/22] Plappe

---
 .travis.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.travis.yml b/.travis.yml
index 1c2fa66..418d64c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,6 +9,7 @@ before_install:
   - sudo apt-get update
   - sudo apt-get install -qq valgrind
   - sudo apt-get install -qq libevent-dev
+  - sudo apt-get install -qq protobuf-compiler
   - sudo apt-get install -qq protobuf-c-compiler
   - sudo apt-get install -qq libprotobuf-dev
   - sudo apt-get install -qq libprotoc-dev

From 0a39218a9ca3c41213f6561132923bfc7d922b14 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 13:48:32 +0100
Subject: [PATCH 14/22] more travis

---
 .travis.yml            | 2 +-
 script/build_module.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 418d64c..f198565 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -16,7 +16,7 @@ before_install:
   - sudo apt-get install -qq libcunit1
   - sudo apt-get install -qq libcunit1-ncurses-dev
   - "yes n | sudo apt-get install riak"
-  - ulimit -n 2048
+  - ulimit -n 4096
   - sudo service riak start
   - wget https://protobuf-c.googlecode.com/files/protobuf-c-0.15.tar.gz
   - tar xvzf protobuf-c-0.15.tar.gz
diff --git a/script/build_module.sh b/script/build_module.sh
index 7136caa..c90a671 100755
--- a/script/build_module.sh
+++ b/script/build_module.sh
@@ -4,7 +4,7 @@ cd riak-c-client
 ./autogen.sh
 ./configure
 make
-
+cd ..
 phpize
 ./configure --with-riak-c-dir=./riak-c-client/src --quiet
 make install

From a037dd1ef6ca55637b5682dd8012da883cdcef17 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 14:53:16 +0100
Subject: [PATCH 15/22] go travis

---
 script/build_module.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/script/build_module.sh b/script/build_module.sh
index c90a671..11a5b9e 100755
--- a/script/build_module.sh
+++ b/script/build_module.sh
@@ -4,6 +4,7 @@ cd riak-c-client
 ./autogen.sh
 ./configure
 make
+sudo make install
 cd ..
 phpize
 ./configure --with-riak-c-dir=./riak-c-client/src --quiet

From 715190ef376dc1765dffab148ce0ba4a8c53c33b Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Wed, 22 Jan 2014 14:54:14 +0100
Subject: [PATCH 16/22] fix compile

---
 riak/bucket.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/riak/bucket.c b/riak/bucket.c
index fe69478..f632212 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -977,10 +977,10 @@ PHP_METHOD(RiakBucket, get)
     result = riak_get(ctx->connection, bucket, key, opts, &response);
     if (result == ERIAK_OK) {
         zval *zout, *zResolver;
-        zout      = get_output_from_riack_get_object(&getResult, zKey TSRMLS_CC);
-        zResolver = (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT)
-            ? zend_read_property(riak_get_resolver_input_ce, zinput, "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC)
-            : zend_read_property(riak_bucket_ce, getThis(), "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC);
+        //zout      = get_output_from_riack_get_object(&getResult, zkey TSRMLS_CC);
+        //zResolver = (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT)
+        //    ? zend_read_property(riak_get_resolver_input_ce, zinput, "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC)
+        //    : zend_read_property(riak_bucket_ce, getThis(), "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC);
         /*
         if (Z_TYPE_P(zResolver) == IS_OBJECT) {
             zend_update_property(riak_output_ce, zout, "conflictResolver", sizeof("conflictResolver")-1, zResolver TSRMLS_CC);

From 32c2d18462f906c1542c88f44e0f297a0de92872 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kaspar@inka-apps.com>
Date: Wed, 22 Jan 2014 18:38:59 +0100
Subject: [PATCH 17/22] zts compile

---
 .gitignore                  |  1 +
 riak/bucket.c               |  8 ++++----
 riak/bucket.h               |  2 +-
 riak/map_reduce/mapreduce.c | 26 +++++++++++++-------------
 riak/object.c               |  4 ++--
 riak/search/output/output.c |  4 ++--
 riak/server_info.c          | 12 ++++++------
 7 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/.gitignore b/.gitignore
index 59c1883..4c2c14b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@ modules/
 build/
 include/
 autom4te.cache/
+riak-c-client
 
 *.iml
 .idea/
diff --git a/riak/bucket.c b/riak/bucket.c
index f632212..33e0ced 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -806,7 +806,7 @@ PHP_METHOD(RiakBucket, delete)
         return;
     }
     ctx = get_riak_context(getThis() TSRMLS_CC);
-    bucket = binary_bucket_name_new(ctx, getThis());
+    bucket = binary_bucket_name_new(ctx, getThis() TSRMLS_CC);
     opts = riak_delete_options_new(ctx->config);
 
     ZVAL_NULL(&zkey);
@@ -882,7 +882,7 @@ PHP_METHOD(RiakBucket, put)
     opts = riak_put_options_new(ctx->config);
     obj = riak_object_new(ctx->config);
     bucket = binary_bucket_name_new(ctx, getThis() TSRMLS_CC);
-    key = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobject, "key");
+    key = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobject, "key" TSRMLS_CC);
 
     ZVAL_NULL(&zvclock);
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
@@ -1071,11 +1071,11 @@ void riak_name_from_bucket(zval* bucket, char **name, int *namelen TSRMLS_DC)/*
 }
 /* }}} */
 
-riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket)
+riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket TSRMLS_DC)
 {
     char* name;
     int namelen;
-    riak_name_from_bucket(zbucket, &name, &namelen);
+    riak_name_from_bucket(zbucket, &name, &namelen TSRMLS_CC);
     return riak_binary_new(ctx->config, namelen, (riak_uint8_t*)name);
 }
 
diff --git a/riak/bucket.h b/riak/bucket.h
index e9703c9..86aa8c4 100644
--- a/riak/bucket.h
+++ b/riak/bucket.h
@@ -49,6 +49,6 @@ riak_context *get_riak_context(zval *zbucket TSRMLS_DC);
 // zval* object_from_riak_content(zval* key, struct RIACK_CONTENT* content TSRMLS_DC);
 // RIACK_STRING riack_name_from_bucket(zval* bucket TSRMLS_DC);
 void riak_name_from_bucket(zval* bucket, char **name, int *namelen TSRMLS_DC);
-riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket);
+riak_binary* binary_bucket_name_new(riak_context *ctx, zval* zbucket TSRMLS_DC);
 
 #endif
diff --git a/riak/map_reduce/mapreduce.c b/riak/map_reduce/mapreduce.c
index 9d9a752..cddabe1 100644
--- a/riak/map_reduce/mapreduce.c
+++ b/riak/map_reduce/mapreduce.c
@@ -123,8 +123,8 @@ struct riak_mr_stream_params {/* {{{ */
 /* }}} */
 
 
-void riak_mr_result_cb(struct RIACK_CLIENT* client, void* arg, struct RIACK_MAPRED_RESPONSE* response) /* {{{ */
-{
+//void riak_mr_result_cb(struct RIACK_CLIENT* client, void* arg, struct RIACK_MAPRED_RESPONSE* response) /* {{{ */
+/*{
     zval zfuncname, *zresponse, zret;
     struct riak_mr_stream_params *params = (struct riak_mr_stream_params*)arg;
     ZVAL_STRING(&zfuncname, "receive", 0);
@@ -136,14 +136,14 @@ void riak_mr_result_cb(struct RIACK_CLIENT* client, void* arg, struct RIACK_MAPR
     call_user_function(NULL, &params->zstreamer, &zfuncname, &zret, 1, &zresponse);
 #endif
     zval_ptr_dtor(&zresponse);
-}
+}*/
 /* }}} */
 
 /* {{{ proto array Riak\MapReduce\MapReduce->run([RiakMrStreamer $streamer])
 Runs the mapreduce query and returns the results as an array of Riak_MapReduce_Output_Output */
 PHP_METHOD(RiakMapreduce, run)
 {
-    zval* zjson, *zclient, *zresult, *zstreamer;
+/*    zval* zjson, *zclient, *zresult, *zstreamer;
     riak_connection *connection, *stream_connection;
     struct RIACK_MAPRED_RESPONSE_LIST *mapresult;
     struct RIACK_MAPRED_RESPONSE_LIST *mapresult_iter;
@@ -200,12 +200,12 @@ PHP_METHOD(RiakMapreduce, run)
             }
         }
     }
-    zval_ptr_dtor(&zjson);
+    zval_ptr_dtor(&zjson);*/
 }
 /* }}} */
 
-void riak_mr_to_array_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
-{
+//void riak_mr_to_array_cb(void* callingObj, void* custom_ptr, char* key, uint keylen, uint index, zval** data, int cnt TSRMLS_DC)/* {{{ */
+/*{
     zval *zarr, *ztargetarr, zfuncname;
     ztargetarr = (zval*)custom_ptr;
 
@@ -215,15 +215,15 @@ void riak_mr_to_array_cb(void* callingObj, void* custom_ptr, char* key, uint key
     //call_user_function(NULL, &zfunc, &zname, zfuncarray, 0, NULL TSRMLS_CC);
     if (zarr && Z_TYPE_P(zarr) == IS_ARRAY) {
         add_next_index_zval(ztargetarr, zarr);
-    }
-}
+   }
+}*/
 /* }}} */
 
 /* {{{ proto array Riak\MapReduce\MapReduce->toArray()
 Returns the current mapreduce query as an array, this is mostly usefull when debugging failing mr queries */
 PHP_METHOD(RiakMapreduce, toArray)
 {
-    zval *zinput, *zinputval, *zphasearr, *zarray, zfuncname;
+/*    zval *zinput, *zinputval, *zphasearr, *zarray, zfuncname;
     zval *zqueryarr;
 
     zinput = zend_read_property(riak_mapreduce_ce, getThis(), "input", sizeof("input")-1, 1 TSRMLS_CC);
@@ -241,14 +241,14 @@ PHP_METHOD(RiakMapreduce, toArray)
     MAKE_STD_ZVAL(zqueryarr);
     array_init(zqueryarr);
     foreach_in_hashtable(getThis(), zqueryarr, Z_ARRVAL_P(zphasearr), &riak_mr_to_array_cb TSRMLS_CC);
-
+*/
     /* Build result array */
-    MAKE_STD_ZVAL(zarray);
+/*    MAKE_STD_ZVAL(zarray);
     array_init(zarray);
     add_assoc_zval_ex(zarray, "inputs", sizeof("inputs"), zinputval);
     add_assoc_zval_ex(zarray, "query", sizeof("query"), zqueryarr);
 
-    RETURN_ZVAL(zarray, 0, 1);
+    RETURN_ZVAL(zarray, 0, 1);*/
 }
 /* }}} */
 
diff --git a/riak/object.c b/riak/object.c
index 08dde01..9666ec5 100644
--- a/riak/object.c
+++ b/riak/object.c
@@ -603,9 +603,9 @@ void riak_object_properties_set(riak_object *obj, zval* zobj, riak_context *ctx
         bin = riak_binary_new(ctx->config, Z_STRLEN_P(ztmp), (riak_uint8_t*)Z_STRVAL_P(ztmp));
         riak_object_set_value(obj, bin);
     }
-    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentType");
+    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentType" TSRMLS_CC);
     riak_object_set_content_type(obj, bin);
-    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentEncoding");
+    bin = riak_binary_shallow_from_property(ctx->config, riak_object_ce, zobj, "contentEncoding" TSRMLS_CC);
     riak_object_set_encoding(obj, bin);
 
     ztmp = zend_read_property(riak_object_ce, zobj, "indexes", sizeof("indexes")-1, 1 TSRMLS_CC);
diff --git a/riak/search/output/output.c b/riak/search/output/output.c
index 022ab1e..e00e5aa 100644
--- a/riak/search/output/output.c
+++ b/riak/search/output/output.c
@@ -45,7 +45,7 @@ void riak_search_output_output_init(TSRMLS_D) /* {{{  */
 * Implementation: Riak\Search\Output\Output
 *************************************************************/
 
-zval* riak_search_output_from_riack_search_result(struct RIACK_SEARCH_RESULT *search_result TSRMLS_DC)
+/*zval* riak_search_output_from_riack_search_result(struct RIACK_SEARCH_RESULT *search_result TSRMLS_DC)
 {
     zval *zresult;
     MAKE_STD_ZVAL(zresult);
@@ -71,7 +71,7 @@ zval* riak_search_output_from_riack_search_result(struct RIACK_SEARCH_RESULT *se
     }
     return zresult;
 }
-
+*/
 PHP_METHOD(Riak_Search_Output_Output, hasMaxScore)
 {
     zval *zprop = zend_read_property(riak_search_output_ce, getThis(), "maxScore", sizeof("maxScore")-1, 1 TSRMLS_CC);
diff --git a/riak/server_info.c b/riak/server_info.c
index e192630..899b042 100644
--- a/riak/server_info.c
+++ b/riak/server_info.c
@@ -55,7 +55,7 @@ PHP_METHOD(Riak_Server_Info, __construct)/* {{{ */
 }
 /* }}} */
 
-int riak_get_server_info_as_zvals(riak_connection **connection, zval** znode,
+/*int riak_get_server_info_as_zvals(riak_connection **connection, zval** znode,
                                   zval **zserver_version, zval* zthis TSRMLS_DC)
 {
     zval *zconn;
@@ -78,10 +78,10 @@ int riak_get_server_info_as_zvals(riak_connection **connection, zval** znode,
     RSTR_SAFE_FREE((*connection)->client, server_version);
     return riackresult;
 }
-
+*/
 PHP_METHOD(Riak_Server_Info, getNode)/* {{{ */
 {
-    riak_connection *connection;
+/*    riak_connection *connection;
     zval *znode, *zserver;
     int riackresult;
     riackresult = riak_get_server_info_as_zvals(&connection, &znode, &zserver, getThis() TSRMLS_CC);
@@ -92,13 +92,13 @@ PHP_METHOD(Riak_Server_Info, getNode)/* {{{ */
         CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);
     } else {
         RETURN_ZVAL(znode, 0, 1);
-    }
+    }*/
 }
 /* }}} */
 
 PHP_METHOD(Riak_Server_Info, getServerVersion)/* {{{ */
 {
-    riak_connection *connection;
+/*    riak_connection *connection;
     zval *znode, *zserver;
     int riackresult;
     riackresult = riak_get_server_info_as_zvals(&connection, &znode, &zserver, getThis() TSRMLS_CC);
@@ -109,6 +109,6 @@ PHP_METHOD(Riak_Server_Info, getServerVersion)/* {{{ */
         CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackresult);
     } else {
         RETURN_ZVAL(zserver, 0, 1);
-    }
+    }*/
 }
 /* }}} */

From fd7247221cdc03b6441f48ceb3fd598a83058fce Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Thu, 23 Jan 2014 15:38:12 +0100
Subject: [PATCH 18/22] prefix

---
 script/build_module.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/script/build_module.sh b/script/build_module.sh
index 11a5b9e..dff1c1e 100755
--- a/script/build_module.sh
+++ b/script/build_module.sh
@@ -2,7 +2,7 @@
 git clone https://github.com/TriKaspar/riak-c-client.git
 cd riak-c-client
 ./autogen.sh
-./configure
+./configure --prefix=/usr
 make
 sudo make install
 cd ..

From e3d53101be683f4bdf0fbc4ae2ae80c6acc38e3e Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Thu, 23 Jan 2014 15:44:54 +0100
Subject: [PATCH 19/22] prefix

---
 .travis.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index f198565..7ea90eb 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -21,7 +21,7 @@ before_install:
   - wget https://protobuf-c.googlecode.com/files/protobuf-c-0.15.tar.gz
   - tar xvzf protobuf-c-0.15.tar.gz
   - cd protobuf-c-0.15
-  - "./configure && make && sudo make install"
+  - "./configure --prefix=/usr && make && sudo make install"
   - cd ..
 before_script:
   - ./script/build_module.sh

From 83b6644930570e6b706bcf9a6598651b61be1c7a Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kaspar@inka-apps.com>
Date: Thu, 23 Jan 2014 16:24:51 +0100
Subject: [PATCH 20/22] clone correct branch

---
 script/build_module.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/script/build_module.sh b/script/build_module.sh
index dff1c1e..735098a 100755
--- a/script/build_module.sh
+++ b/script/build_module.sh
@@ -1,5 +1,5 @@
 #!/bin/bash
-git clone https://github.com/TriKaspar/riak-c-client.git
+git clone https://github.com/TriKaspar/riak-c-client.git --branch=riak-binary
 cd riak-c-client
 ./autogen.sh
 ./configure --prefix=/usr

From 599b46d265b1470ad916e23d975447e89085f947 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kpn@trifork.com>
Date: Fri, 24 Jan 2014 21:56:50 +0100
Subject: [PATCH 21/22] More work on get

---
 riak/bucket.c            |  4 ++
 riak/object.c            | 97 ++++++++++++++++++++++++++++------------
 riak/object.h            |  3 +-
 riak/output/get_output.c | 16 ++++---
 riak/output/get_output.h |  3 +-
 riak/output/output.c     | 27 +++++------
 riak/output/output.h     |  1 +
 7 files changed, 97 insertions(+), 54 deletions(-)

diff --git a/riak/bucket.c b/riak/bucket.c
index 33e0ced..408c04b 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -911,6 +911,10 @@ PHP_METHOD(RiakBucket, put)
     //
     result = riak_put(ctx->connection, obj, opts, &response);
     // TODO Handle response values!!
+    if (result == ERIAK_OK) {
+        //
+    }
+
     riak_put_options_free(ctx->config, &opts);
     riak_object_free(ctx->config, &obj);
     riak_put_response_free(ctx->config, &response);
diff --git a/riak/object.c b/riak/object.c
index 9666ec5..6a77c7e 100644
--- a/riak/object.c
+++ b/riak/object.c
@@ -444,39 +444,80 @@ void riak_key_from_object(zval *zobject, char** key, int* keylen TSRMLS_DC)/* {{
 /* }}} */
 
 
+void set_object_properties_from_riak_object(zval* zobject, riak_object* robject TSRMLS_DC)  /* {{{ */
+{
+    zval* zmetadata, *zlinks, *zindexes;
+    riak_binary *bin;
+
+    bin = riak_object_get_value(robject);
+    zend_update_property_stringl(riak_object_ce, zobject, "content", sizeof("content")-1,
+                                 (const char*)riak_binary_data(bin), riak_binary_len(bin) TSRMLS_CC);
+    if (riak_object_get_has_content_encoding(robject)) {
+        bin = riak_object_get_encoding(robject);
+        zend_update_property_stringl(riak_object_ce, zobject, "contentEncoding", sizeof("contentEncoding")-1,
+            (const char*)riak_binary_data(bin), riak_binary_len(bin) TSRMLS_CC);
+    }
+    if (riak_object_get_has_content_type(robject)) {
+        bin = riak_object_get_content_type(robject);
+        zend_update_property_stringl(riak_object_ce, zobject, "contentType", sizeof("contentType")-1,
+                                     (const char*)riak_binary_data(bin), riak_binary_len(bin) TSRMLS_CC);
+    }
+    if (riak_object_get_has_vtag(robject)) {
+        bin = riak_object_get_content_type(robject);
+        zend_update_property_stringl(riak_object_ce, zobject, "vtag", sizeof("vtag")-1,
+                                     (const char*)riak_binary_data(bin), riak_binary_len(bin) TSRMLS_CC);
+    }
+    if (riak_object_get_has_charset(robject)) {
+        bin = riak_object_get_charset(robject);
+        zend_update_property_stringl(riak_object_ce, zobject, "charset", sizeof("charset")-1,
+                                     (const char*)riak_binary_data(bin), riak_binary_len(bin) TSRMLS_CC);
+    }
+    if (riak_object_get_has_deleted(robject)) {
+        zend_update_property_bool(riak_object_ce, zobject, "isDeleted", sizeof("isDeleted")-1,
+                                  riak_object_get_deleted(robject) TSRMLS_CC);
+    } else {
+        zend_update_property_null(riak_object_ce, zobject, "isDeleted", sizeof("isDeleted")-1 TSRMLS_CC);
+    }
+    if (riak_object_get_has_last_mod(robject)) {
+        zend_update_property_long(riak_object_ce, zobject, "lastModified", sizeof("lastModified")-1, riak_object_get_last_mod(robject) TSRMLS_CC);
+    } else {
+        zend_update_property_null(riak_object_ce, zobject, "lastModified", sizeof("lastModified")-1 TSRMLS_CC);
+    }
+    if (riak_object_get_has_last_mod_usecs(robject)) {
+        zend_update_property_long(riak_object_ce, zobject, "lastModifiedUSecs", sizeof("lastModifiedUSecs")-1,
+                                  riak_object_get_last_mod_usecs(robject) TSRMLS_CC);
+    } else {
+        zend_update_property_null(riak_object_ce, zobject, "lastModifiedUSecs", sizeof("lastModifiedUSecs")-1 TSRMLS_CC);
+    }
+    /*
+     *
+     *zmetadata = assoc_array_from_riack_pairs(content->usermetas, content->usermeta_count TSRMLS_CC);
+    zend_update_property(riak_object_ce, object, "metadata", sizeof("metadata")-1, zmetadata TSRMLS_CC);
+    zval_ptr_dtor(&zmetadata);
+riak_int32_t   riak_object_get_n_usermeta(riak_object *obj);
+riak_pair    **riak_object_get_usermeta(riak_object *obj);
+
+    zindexes = assoc_array_from_riack_pairs(content->indexes, content->index_count TSRMLS_CC);
+    zend_update_property(riak_object_ce, object, "indexes", sizeof("indexes")-1, zindexes TSRMLS_CC);
+    zval_ptr_dtor(&zindexes);
+riak_int32_t   riak_object_get_n_indexes(riak_object *obj);
+riak_pair    **riak_object_get_indexes(riak_object *obj);
+
+    zlinks = links_from_content(content TSRMLS_CC);
+    zend_update_property(riak_object_ce, object, "links", sizeof("links")-1, zlinks TSRMLS_CC);
+    zval_ptr_dtor(&zlinks);
+
+riak_int32_t   riak_object_get_n_links(riak_object *obj);
+riak_link    **riak_object_get_links(riak_object *obj);
+*/
+}
+/* }}} */
+
 /* Set object properties from returned content */
 // void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC) /* {{{ */
 // {
 /*
     zval* zmetadata, *zlinks, *zindexes;
-
-    zend_update_property_stringl(riak_object_ce, object, "content", sizeof("content")-1,
-		(const char*)content->data, content->data_len TSRMLS_CC);
-	zend_update_property_stringl(riak_object_ce, object, "contentEncoding", sizeof("contentEncoding")-1, 
-		(const char*)content->content_encoding.value, content->content_encoding.len TSRMLS_CC);
-	zend_update_property_stringl(riak_object_ce, object, "contentType", sizeof("contentType")-1, 
-		(const char*)content->content_type.value, content->content_type.len TSRMLS_CC);
-    if (content->vtag.len > 0 && content->vtag.value != NULL) {
-        zend_update_property_stringl(riak_object_ce, object, "vtag", sizeof("vtag")-1,
-            (const char*)content->vtag.value, content->vtag.len TSRMLS_CC);
-    }
-	zend_update_property_stringl(riak_object_ce, object, "charset", sizeof("charset")-1, content->charset.value, content->charset.len TSRMLS_CC);
-
-	if (content->deleted_present) {
-		zend_update_property_bool(riak_object_ce, object, "isDeleted", sizeof("isDeleted")-1, content->deleted TSRMLS_CC);
-	} else {
-		zend_update_property_null(riak_object_ce, object, "isDeleted", sizeof("isDeleted")-1 TSRMLS_CC);
-	}
-	if (content->last_modified_present) {
-		zend_update_property_long(riak_object_ce, object, "lastModified", sizeof("lastModified")-1, content->last_modified TSRMLS_CC);
-	} else {
-		zend_update_property_null(riak_object_ce, object, "lastModified", sizeof("lastModified")-1 TSRMLS_CC);
-	}
-	if (content->last_modified_usecs_present) {
-		zend_update_property_long(riak_object_ce, object, "lastModifiedUSecs", sizeof("lastModifiedUSecs")-1, content->last_modified_usecs TSRMLS_CC);
-	} else {
-		zend_update_property_null(riak_object_ce, object, "lastModifiedUSecs", sizeof("lastModifiedUSecs")-1 TSRMLS_CC);
-	}
     zmetadata = assoc_array_from_riack_pairs(content->usermetas, content->usermeta_count TSRMLS_CC);
     zend_update_property(riak_object_ce, object, "metadata", sizeof("metadata")-1, zmetadata TSRMLS_CC);
     zval_ptr_dtor(&zmetadata);
diff --git a/riak/object.h b/riak/object.h
index 4086457..30d276c 100644
--- a/riak/object.h
+++ b/riak/object.h
@@ -26,8 +26,7 @@ extern zend_class_entry *riak_object_ce;
 zval* create_object_object(const char* key TSRMLS_DC);
 
 void riak_object_init(TSRMLS_D);
-// TODO
-//  void set_object_from_riak_content(zval* object, struct RIACK_CONTENT* content TSRMLS_DC);
+void set_object_properties_from_riak_object(zval* zobject, riak_object* robject TSRMLS_DC);
 // TODO
 //  void set_riak_content_from_object(struct RIACK_CONTENT* content, zval* object, struct RIACK_CLIENT* client TSRMLS_DC);
 void riak_object_properties_set(riak_object *obj, zval* zobj, riak_context *ctx TSRMLS_DC);
diff --git a/riak/output/get_output.c b/riak/output/get_output.c
index a1ed98b..91960f0 100644
--- a/riak/output/get_output.c
+++ b/riak/output/get_output.c
@@ -36,17 +36,21 @@ void riak_output_get_output_init(TSRMLS_D)/* {{{ */
 }
 /* }}} */
 
-// zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC) /* {{{ */
-/*{
+zval *get_output_from_riack_get_response(riak_get_response* response, zval *zkey TSRMLS_DC) /* {{{ */
+{
     zval *zoutput;
     MAKE_STD_ZVAL(zoutput);
     object_init_ex(zoutput, riak_get_output_ce);
-    if (getobj->unchanged_present) {
-        zend_update_property_bool(riak_get_output_ce, zoutput, "unchanged", sizeof("unchanged")-1, getobj->unchanged TSRMLS_CC);
+    if (riak_get_get_has_unmodified(response)) {
+        zend_update_property_bool(riak_get_output_ce, zoutput, "unchanged", sizeof("unchanged")-1,
+                                  riak_get_get_unmodified(response) TSRMLS_CC);
     }
-    riak_set_output_properties(zoutput, zkey, &getobj->object TSRMLS_CC);
+    // TODO vclock & deleted
+    riak_int32_t n = riak_get_get_n_content(response);
+    riak_object **objs = riak_get_get_content(response);
+    riak_set_output_properties(zoutput, zkey, n, objs TSRMLS_CC);
     return zoutput;
-}*/
+}
 /* }}} */
 
 
diff --git a/riak/output/get_output.h b/riak/output/get_output.h
index a9329ba..59969c0 100644
--- a/riak/output/get_output.h
+++ b/riak/output/get_output.h
@@ -18,11 +18,12 @@
 #define RIAK_OUTPUT__GET_OUTPUT__H__
 
 #include "php_riak_internal.h"
+#include <messages/riak_get.h>
 
 extern zend_class_entry *riak_get_output_ce;
 
 void riak_output_get_output_init(TSRMLS_D);
-// zval *get_output_from_riack_get_object(struct RIACK_GET_OBJECT* getobj, zval* zkey TSRMLS_DC);
+zval *get_output_from_riack_get_response(riak_get_response* response, zval *zkey TSRMLS_DC);
 
 PHP_METHOD(Riak_Output_GetOutput, isUnchanged);
 
diff --git a/riak/output/output.c b/riak/output/output.c
index f6608c3..c4a09d1 100644
--- a/riak/output/output.c
+++ b/riak/output/output.c
@@ -56,15 +56,10 @@ void riak_output_init(TSRMLS_D)/* {{{ */
 }
 /* }}} */
 
-// void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC) /* {{{ */
-// {
-/*
-    size_t content_cnt, i;
+void riak_set_output_properties(zval* zoutput, zval *zkey, riak_int32_t cnt, riak_object **objects TSRMLS_DC)
+{
+    riak_int32_t i;
     zval *zobjectlist;
-    if (obj->vclock.len > 0) {
-        zend_update_property_stringl(riak_output_ce, zoutput, "vClock", sizeof("vClock")-1,
-                                     (char*)obj->vclock.clock, obj->vclock.len TSRMLS_CC);
-    }
     if (Z_TYPE_P(zkey) == IS_STRING) {
         zend_update_property_stringl(riak_output_ce, zoutput, "key", sizeof("key")-1,
                                      Z_STRVAL_P(zkey), Z_STRLEN_P(zkey) TSRMLS_CC);
@@ -72,25 +67,25 @@ void riak_output_init(TSRMLS_D)/* {{{ */
     MAKE_STD_ZVAL(zobjectlist);
     object_init_ex(zobjectlist, riak_output_object_list_ce);
     RIAK_CALL_METHOD(Riak_Object_List, __construct, NULL, zobjectlist);
-    content_cnt = obj->content_count;
-    for (i=0; i<content_cnt; ++i) {
-        // Create a new Riak\Object for each content
+    for (i=0; i<cnt; ++i) {
         zval *zobject, *zoffset;
         MAKE_STD_ZVAL(zobject);
         object_init_ex(zobject, riak_object_ce);
-
+        // Create a new Riak\Object for each object
         if (Z_TYPE_P(zkey) != IS_NULL) {
             RIAK_CALL_METHOD1(RiakObject, __construct, NULL, zobject, zkey);
         } else {
             RIAK_CALL_METHOD(RiakObject, __construct, NULL, zobject);
         }
-
+        // TODO vClock
+        /*
         if (obj->vclock.len > 0) {
             zend_update_property_stringl(riak_object_ce, zobject, "vClock", sizeof("vClock")-1,
                                      (char*)obj->vclock.clock, obj->vclock.len TSRMLS_CC);
         }
+        */
 
-        set_object_from_riak_content(zobject, &obj->content[i] TSRMLS_CC);
+        set_object_properties_from_riak_object(zobject, objects[i] TSRMLS_DC);
 
         MAKE_STD_ZVAL(zoffset);
         ZVAL_LONG(zoffset, i);
@@ -100,9 +95,7 @@ void riak_output_init(TSRMLS_D)/* {{{ */
     }
     zend_update_property(riak_output_ce, zoutput, "objectList", sizeof("objectList")-1, zobjectlist TSRMLS_CC);
     zval_ptr_dtor(&zobjectlist);
-    */
-// }
-/* }}} */
+}
 
 /*************************************************************
 * Implementation: Riak\Output\Output
diff --git a/riak/output/output.h b/riak/output/output.h
index ee55099..b4cbd38 100644
--- a/riak/output/output.h
+++ b/riak/output/output.h
@@ -24,6 +24,7 @@ extern zend_class_entry *riak_output_ce;
 void riak_output_init(TSRMLS_D);
 
 // void riak_set_output_properties(zval* zoutput, zval* zkey, struct RIACK_OBJECT* obj TSRMLS_DC);
+void riak_set_output_properties(zval* zoutput, zval *zkey, riak_int32_t cnt, riak_object **objects TSRMLS_DC);
 
 PHP_METHOD(Riak_Output_Output, getObjectList);
 PHP_METHOD(Riak_Output_Output, getVClock);

From eda989f46705c325ef724138c3f5110829ced484 Mon Sep 17 00:00:00 2001
From: Kaspar Pedersen <kaspar@inka-apps.com>
Date: Fri, 24 Jan 2014 22:49:36 +0100
Subject: [PATCH 22/22] More get

---
 riak/bucket.c        | 41 ++++++++++++++++++++++-------------------
 riak/output/output.c |  3 +--
 2 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/riak/bucket.c b/riak/bucket.c
index 408c04b..4cd932f 100644
--- a/riak/bucket.c
+++ b/riak/bucket.c
@@ -48,15 +48,15 @@ riak_connection *get_riak_connection(zval *zbucket TSRMLS_DC);
 
 zend_class_entry *riak_bucket_ce;
 
-#define RIAK_REQ_PROP_SET_LONG(CLASS_ALIAS, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
+#define RIAK_REQ_PROP_SET_LONG(CLASS_ALIAS, OP, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
     RIAK_CALL_METHOD(CLASS_ALIAS, GETTER, &ZV, zinput); \
     if (Z_TYPE(ZV) == IS_LONG) { \
-        riak_delete_options_set_##TARGET_NAME(TARGET_STRUCT, Z_LVAL(ZV)); }
+        riak_##OP##_options_set_##TARGET_NAME(TARGET_STRUCT, Z_LVAL(ZV)); }
 
-#define RIAK_REQ_PROP_SET_BOOL(CLASS_ALIAS, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
+#define RIAK_REQ_PROP_SET_BOOL(CLASS_ALIAS, OP, ZV, GETTER, TARGET_STRUCT, TARGET_NAME) \
     RIAK_CALL_METHOD(CLASS_ALIAS, GETTER, &ZV, zinput); \
-    if (Z_TYPE(ZV) == IS_BOOL && Z_BVAL(ZV)) { riak_delete_options_set_##TARGET_NAME(TARGET_STRUCT, Z_BVAL(ZV)); }
-
+    if (Z_TYPE(ZV) == IS_BOOL && Z_BVAL(ZV)) { riak_##OP##_options_set_##TARGET_NAME(TARGET_STRUCT, Z_BVAL(ZV)); }
+/*
 #define RIAK_GET_STR_COPY_TO(CLASSNAME, GETTER, ZOBJ, TARGET, TARGET_LEN) { \
     zval *z_getter_res; MAKE_STD_ZVAL(z_getter_res); \
     RIAK_CALL_METHOD(CLASSNAME, GETTER, z_getter_res, ZOBJ); \
@@ -64,7 +64,7 @@ zend_class_entry *riak_bucket_ce;
         RMALLOCCOPY(connection->client, TARGET, TARGET_LEN, Z_STRVAL_P(z_getter_res), Z_STRLEN_P(z_getter_res)); \
     } \
     zval_ptr_dtor(&z_getter_res); }
-
+*/
 ZEND_BEGIN_ARG_INFO_EX(arginfo_bucket_ctor, 0, ZEND_RETURN_VALUE, 2)
     ZEND_ARG_INFO(0, client)
     ZEND_ARG_INFO(0, name)
@@ -820,9 +820,9 @@ PHP_METHOD(RiakBucket, delete)
         RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getPR, props.pr);
         RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getRW, props.rw);
         */
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getW, opts, w);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getDW, opts, dw);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, zget, getPW, opts, pw);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, delete, zget, getW, opts, w);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, delete, zget, getDW, opts, dw);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_DeleteInput, delete, zget, getPW, opts, pw);
 
         RIAK_CALL_METHOD(Riak_Input_DeleteInput, getVClock, &zget, zinput);
         if (Z_TYPE(zget) == IS_STRING) {
@@ -915,8 +915,9 @@ PHP_METHOD(RiakBucket, put)
         //
     }
 
-    riak_put_options_free(ctx->config, &opts);
-    riak_object_free(ctx->config, &obj);
+    // Why do these fail?, they should not...
+//    riak_put_options_free(ctx->config, &opts);
+//    riak_object_free(ctx->config, &obj);
     riak_put_response_free(ctx->config, &response);
     riak_binary_free(ctx->config, &bucket);
     riak_binary_free(ctx->config, &key);
@@ -948,7 +949,7 @@ PHP_METHOD(RiakBucket, get)
     riak_get_response *response;
     riak_get_options *opts;
     riak_binary *bucket, *key;
-    zval *zinput;
+    zval *zinput, *zkey;
     char *skey;
     int skeylen;
 
@@ -957,16 +958,18 @@ PHP_METHOD(RiakBucket, get)
         zend_throw_exception(riak_badarguments_exception_ce, "Bad or missing argument", 500 TSRMLS_CC);
         return;
     }
+    MAKE_STD_ZVAL(zkey);
+    ZVAL_STRINGL(zkey, skey, skeylen, 1);
     ctx = get_riak_context(getThis() TSRMLS_CC);
     bucket = binary_bucket_name_new(ctx, getThis() TSRMLS_CC);
     key = riak_binary_new(ctx->config, (riak_uint32_t)skeylen, (riak_uint8_t*)key);
     opts = riak_get_options_new(ctx->config);
     if (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT) {
-/*
         zval zget;
-        RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, zget, getReturnHead, props.head);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_GetInput, zget, getR, props.r);
-        RIAK_REQ_PROP_SET_LONG(Riak_Input_GetInput, zget, getPR, props.pr);
+        RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, get, zget, getReturnHead, opts, head);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_GetInput, get, zget, getR, opts, r);
+        RIAK_REQ_PROP_SET_LONG(Riak_Input_GetInput, get, zget, getPR, opts, pr);
+        /*
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, zget, getBasicQuorum, props.basic_quorum);
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, zget, getNotFoundOk, props.notfound_ok);
         RIAK_REQ_PROP_SET_BOOL(Riak_Input_GetInput, zget, getReturnDeletedVClock, props.deletedvclock);
@@ -981,6 +984,7 @@ PHP_METHOD(RiakBucket, get)
     result = riak_get(ctx->connection, bucket, key, opts, &response);
     if (result == ERIAK_OK) {
         zval *zout, *zResolver;
+        zout = get_output_from_riack_get_response(response, zkey TSRMLS_CC);
         //zout      = get_output_from_riack_get_object(&getResult, zkey TSRMLS_CC);
         //zResolver = (zinput != NULL && Z_TYPE_P(zinput) == IS_OBJECT)
         //    ? zend_read_property(riak_get_resolver_input_ce, zinput, "conflictResolver", sizeof("conflictResolver")-1, 1 TSRMLS_CC)
@@ -988,11 +992,9 @@ PHP_METHOD(RiakBucket, get)
         /*
         if (Z_TYPE_P(zResolver) == IS_OBJECT) {
             zend_update_property(riak_output_ce, zout, "conflictResolver", sizeof("conflictResolver")-1, zResolver TSRMLS_CC);
-        }
+        }*/
         zend_update_property(riak_output_ce, zout, "bucket", sizeof("bucket")-1, getThis() TSRMLS_CC);
         RETVAL_ZVAL(zout, 0, 1);
-        riack_free_get_object(connection->client, &getResult);
-*/
     } else {
         ctx->needs_reconnect = 1;
         // Hack warning, same as above
@@ -1007,6 +1009,7 @@ PHP_METHOD(RiakBucket, get)
     riak_get_options_free(ctx->config, &opts);
     riak_binary_free(ctx->config, &bucket);
     riak_binary_free(ctx->config, &key);
+    zval_ptr_dtor(&zkey);
 }
 /* }}} */
 
diff --git a/riak/output/output.c b/riak/output/output.c
index c4a09d1..ee39ec5 100644
--- a/riak/output/output.c
+++ b/riak/output/output.c
@@ -84,8 +84,7 @@ void riak_set_output_properties(zval* zoutput, zval *zkey, riak_int32_t cnt, ria
                                      (char*)obj->vclock.clock, obj->vclock.len TSRMLS_CC);
         }
         */
-
-        set_object_properties_from_riak_object(zobject, objects[i] TSRMLS_DC);
+        set_object_properties_from_riak_object(zobject, objects[i] TSRMLS_CC);
 
         MAKE_STD_ZVAL(zoffset);
         ZVAL_LONG(zoffset, i);
