CouchDB introduction

A phenomenon of document-oriented databases is quite interesting - many software developers face problems there this kind of databases is an excellent choice, but these developers don't use them and reinvent the wheel using relational or object-oriented databases. Difficult to say why it happens - because of ignorance, fear of performance problems or desire to reinvent own wheel, but this situation widely spread over the world. Fortunately, the common sense is prevailing, and NoSQL movement prove it.

I have a serious experience with IBM Lotus Notes/Domino, and one of the most interesting features for me on first stages of its studying was saving application design in the documents. Thus the deployment of Lotus Notes database is incredibly easy - one just have to copy NSF-file to another location and it's ready for use (not always, but for trivial cases it's enough). Sometimes it can be a really useful feature, especially during prototyping, but not so many document-oriented databases have it. CouchDB is one of them.


What Apache CouchDB is:
  • A document database server, accessible via a RESTful JSON API.
  • Ad-hoc and schema-free with a flat address space.
  • Distributed, featuring robust, incremental replication with bi-directional conflict detection and management.
  • Queried and indexed in a MapReduce fashion using JavaScript..
  • Written in Erlang, a robust functional programming language ideal for building concurrent distributed systems.
CouchDB provides Web administration console (Futon), and in some way it can be compared with Lotus Notes Designer - it allows to operate documents, views, shows and lists, preview results, maintain databases. Of course, it is not so powerful, however it is suitable for trivial tasks, and in this introduction I'll show some aspects of it.

Installation of the CouchDB is covered in wiki: http://wiki.apache.org/couchdb/Installation. The installation can be checked by browsing to the local URL: http://localhost:5984/_utils/


Create a new database "db" using corresponding button:


Create a couple documents using "New Document" button:


Add fields title and content to these documents using "Add Field" button. Don't forget to click "Save Document" before navigating to the root.

After that let's create required view. Switch to temporary view:


Views are the method of aggregating and reporting on the documents in a database, and are built on-demand to aggregate, join and report on database documents. Views are built dynamically and don’t affect the underlying document, you can have as many different view representations of the same data as you like.

Let's create a simple view that just emit the rows with title and content fields from the documents:

function(doc) {
  emit(null, {Content: doc.content, Title: doc.title});
}


Test the view with the "Run" button:


Now it's time to save the view for further usage (to a design document that contain the application design):




After that the view is accessible as JSON-object (use RESTful rules, in this particular case URL is http://domain:port/database_name/_design/design_document_name/_view/view_name):


show and list functions convert documents and views, respectively, into non-JSON formats. The rows of each view are processed individually, which keeps long lists from becoming memory hogs. They are designed to be cacheable. CouchDB handles generating Etags for show and list responses.

Let's create a simple list function. It will generate a basic HTML document using the data from the rows provided by the view we previously created:

function(head, req) {
 start({'headers':{'Content-Type':'text/html;charset=UTF-8'}});
 send('<!DOCTYPE html><head><title>test</title></head><body>');
 var row;
 while(row = getRow()) {
   send('<h1>'+row.value['Title']+'</h1>'+
        '<h2>'+row.value['Content']+'</h2>');
 };
 send('</body>');
}

Add the field lists to the design document and create a sample list with the required function:



I would like to notice that there is a small glitch with the fields containing code - they don't work well with the carriage return symbol. I suggest to use Firefox for inserting code snippets, or just remove new lines symbols before inserting.

The finishing line - testing the results. URL for getting required HTML is http://domain:port/database_name/_design/design_document_name/_list/list_name/view_name:


That's all! There are many features left overboard, but I hope you have got an idea. Of course, CouchDB is not the most powerful document-oriented database, but it's free, have a serious potential and worth a try for your new software project especially if it's related to electronic workflow.

Comments

Popular posts from this blog

Web application framework comparison by memory consumption

Trac Ticket Workflow

Shellcode detection using libemu