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 no longer undergoing tests in a secret laboratory somewhere in Wakanda and 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 or 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 until recently, drawn many blanks in attempting to create a viable solution in plain C.
Writing the extension in C++ — via one of either PHP-CPP or C-interoperable PHP Virtual Machine bindings is the path I have chosen to take. 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 C++— 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.
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.
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 as it is a paltry five-method composition, but, there exists room for farther enhancement. All criticisms and opinions are welcome as I continue to improve the library presently accessible on GitHub.