Chicken Scheme and CouchDB

But Why?

I’ve recently been playing around with Chicken Scheme, leaving poor Clojure all high and dry (minus some visits to the newly started DFW-Clojure User Group). After building a service in C at work, I’ve caught the high-performance bug and Clojure hasn’t particularly been helpful in that regard. I’ve also found Chicken Scheme to be very simple with a more straightforward syntax than Clojure. I’ll still use Clojure, and am using it on a web-app side-project, but client side I’m really digging Chicken.

I’ve also been using MongoDB, but (OMG!) there’s no driver currently for MongoDB in Chicken Scheme. Until I have the chops to write my own version, I decided I might as well learn a similar DB: enter Couch. In my naive view, it’s very similar to Mongo – it’s a document-oriented database with JSON (or variation thereof) as its representative data structure. Even cooler, it’s already been ported to BlackBerry 10. This gives me some really good ideas for new projects.

The Setup

I’m currently running Linux Mint, a “fork” of Ubuntu, so this setup guide follows what was necessary for me to get up and going with Chicken Scheme and CouchDB.

Chicken Scheme

There are two options. The easy way:

sudo apt-get install chicken-bin.

This installs everything for you with no issue, however it’s an older version (as seems to be a common issue with the Ubuntu repositories). At the time of writing it’s at 4.7.0. You can also install Chicken by compiling from source. Download the tarball from call-cc.org, untar, and follow the INSTALL instructions. Easy!

CouchDB Egg

Chicken Scheme has an extension system which packages and distributes “eggs.” An egg consists of Scheme sources plus some meta information such as build scripts and egg dependencies. You can install an egg using the chicken-install utility that comes with Chicken.

chicken-install -sudo couchdb

This will download the couchdb egg along with all eggs in the dependency tree. The utility then begins compiling and installing all eggs along the way. If you don’t do a lot of development, chances are likely that you will run into system dependencies that are unmet. Chicken Scheme compiles to C, and as such many eggs have dependencies on C libraries.

The one I hit with couchdb in particular was openssl. These system lib dependencies can be a little tricky to identify. Two tips:

  • See which egg is failing, then find it in the egg index and look through it’s notes which usually indicate dependencies
  • Read the error message and decipher from there.

For example, my first attempt at installing the couchdb egg failed due to missing openssl header files. The install utility was attempting to compile the openssl egg, which was a dependency of the http-client egg, which is a dependency of the couchdb egg.

A few searches for openssl and I satisfied the dependency with the following command:

sudo apt-get install libssl-dev

With that successfully in place, another call to install the couchdb egg succeeded.

Playing Around

I didn’t have time to compile and install a CouchDB server on my machine, so to the cloud I went. I signed up for an account at cloudant.com which provides a free-for-small-databases CouchDB compatible database. After signing up, I imported the example database aptly named “crud” and began toying around.

Booting up csi, the Chicken Scheme interpreter, I called (use couchdb) and set out to play. The first hurdle: making a connection. The current documentation says to use something along the lines of (define couch (make-connection "databasename" "http://localhost:123")).

That failed miserably.

No matter what I tried, the command (get-server-info couch) would fail with a connection refused. (connection-uri couch) showed that the URI struct was still pointing at localhost. Luckily, the source for Chicken eggs are available, even for CouchDB. The make-connection function is created by the (defstruct connection ...) expression. I noticed that the server attribute of connection struct is a uri-referencenot a string. Reading through the defstruct docs I figured out the following worked – woot.

(import uri-common)
(define cloudant-url server: (uri-reference "https://username:password@username.cloudant.com"))
(define cloudant (make-connection server: cloudant-url database: "crud"))
(get-server-info cloudant)

This returns JSON data in Scheme structure:

#(("couchdb" . "Welcome")
  ("version" . "1.0.2")
  ("cloudant_build" . "768"))

Since I’m new to Scheme, getting the data out was a bit odd, and I’m sure completely inefficient.

(define server-info (vector->list (get-server-info cloudant)))
(alist-ref "version" server-info equal?)

That about sums it up. Hopefully I figure more stuff out and post some updates. Feel free to comment if I’m reading the JSON structure in a completely asinine way. The vector->list doesn’t seem right.

Update

The couchdb egg documentation has been updated to cover most of this. I’ve also noticed a generally handy function that is exported by the couchdb module, json-ref, that makes it simple to grab top-level JSON items like “version” above.

(define server-version (json-ref 'version (get-server-info cloudant)))
About these ads

2 Comments on “Chicken Scheme and CouchDB”

  1. dergutemoritz says:

    Hi David,

    as the original author of the couchdb egg (back in 2009 when I had just started becoming Chickeneer myself) I feel obliged to respond. I just fixed up the documentation to cover the latest version of the API in all examples. Thanks for noticing and glad you didn’t jump off board right away :-) Unfortunately the couchdb egg suffers from using the json egg which–as you rightfully noticed–has this strange way of representing objects as vectors of pairs. Back in the day this was the only json egg we had so it’s what I chose. Unfortunately I am not actively using the couchdb egg at the moment so I don’t feel too inclined to rework it (the last-error mechanism is also something that should be changed, for example). If you feel like adopting it, I’m happy to help, though. Let me know!

    Moritz

    • David Sosby says:

      Moritz, thanks for responding! I’m delighted to use the egg. I’m still learning the ways of Chicken and Scheme in general, and this was just a documentation of my naive attempts. I did not even realize I could edit the documentation myself to handle these notes – though next time I will know. I plan to use Couch and Chicken in my current project and if I get ideas and changes I will surely let you know.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.