A Potential PHP Extension for CouchDB

If you have given some of my earlier blog posts enough heed to attempt to understand the amalgam of PHP-implementable ideas at their core, you might notice my passion for CouchDB. I find that CouchDB interactions are apt for a period defined by a web-service zeitgeist: the service’s REST API gracefully abstracts a bevy of arcane Erlang OTP offerings and is — generally — easy to integrate into modern web applications. Despite recently creating an asynchronous PHP wrapper, there is room for more optimization: enter the PHP extension, couchdb_ext.

Said software is still undergoing tests in a laboratory somewhere but offers many of the asynchronous fauxton client’s cogencies albeit in a blocking IO environment. The rationale behind building a PHP extension is offering more potency to software solutions that can, at times, be otherwise less performant as pure userland code manifestations. There exists more than one way to go about building PHP extensions: idiomatic C — the preferred approach, PHP-CPP, and Zephir are all valid solutions. I have, in recent months, combed through the incomplete PHP internals book and have drawn many blanks in attempting to create a viable solution in plain C.

PHP-CPP presents a C++ 11-compliant API and extension creation-apt tool that gracefully wraps around the Zend Engine. Simply put, C++ is, at least for me, a lot more riveting to write than C as the language is not only seminal to my career as a software development enthusiast but also one I love to write whenever PHP is not taking center stage in my life. The improvements to the language in the 11-spec which include, but are not limited to, the auto keyword, flexible template syntax, and neat closures, are all usable with PHP-CPP — and — in this case — with couchdb_ext.

The extension is libcurl-reliant. The reason behind this is in a premise established in the antecedent text — libcurl is a faster, more C/C++ affable 
solution which eliminates the need for unnecessary, performance inhibiting direct PHP function calls. The API is rather simple: a single constructor call, one akin to that in PHP’s PDO class for RDBMS access, is the ancestor of most couchdb_ext operations.

Printing five UUIDS with couchdb_ext

In the snippet above, there exist two checks: one for extension availability which is also ascertainable by typing php -m in a console of your choosing and another for CouchDB service availability. The code above generates five Universally Unique Identifiers (UUIDs) and prints them. The other methods I have been experimenting with are related to insertion, deletion, and updating records. The code below is a demonstration of a simple records retrieval action.

Printing all documents in a database with couchdb_ext

I attempted a record retrieval of over twenty-three thousand entries in a particular database stored on disk. The results indicate a lower memory consumption than PHP’s native CURL bindings: about five-twelfths of memory consumed by the latter.

In conclusion, the API as currently constructed is far from complete but, there exists room for farther enhancement: the door is still open for a true-blue C extension, and there are plans to support Windows users. All criticisms and opinions are welcome as I intend to release an improved library sometime later this month.