Tuesday, February 28, 2012

Book Review: Just for Fun

Just for Fun: The Story of an Accidental Revolutionary

A while ago, my colleague Wouter recommended this book titled Just for Fun – The Story of an Accidental Revolutionary written by Linus Torvalds. The author needs no further introduction as he’s well known as the initiator of the Linux kernel and the most excellent Git revision control system. I immediately ordered myself a copy of this book and finished reading it last weekend. I must admit that this was quite a fascinating read. The writing style is very entertaining and just plain witty at times.

The book consists of three major parts. The first part, titled “Birth of a Nerd”, describes the early life of the author in his home country Finland, how he got introduced to computers by his grandfather, etc. … .

The second part, titled “Birth of an Operating System”, is probably the most interesting part for us geeks. Here Linus describes how he started hacking on software while he was at university. At first he started working on a terminal emulator for Minix in order to learn about the x386 CPU instruction set for the PC he bought. This terminal emulator later grown into the first version of the Linux kernel after he implemented more and more Unix system functions. He further elaborates how these humble beginnings further evolved into the Linux operating system as we know it today. Truly an amazing story!

The last part of the book, titled “King of the ball”, discusses some varying topics like intellectual property, why open-source software makes sense and even the meaning of life. This section contains some very interesting perspectives regarding these topics.  

In conclusion, I very much liked reading this book. It’s a fast and easy read and you don’t even have to be a geek to make sense of this great story. This book is definitely obligatory reading for everyone who’s even slightly involved with open-source software.

Two thumbs up!!

Wednesday, February 22, 2012

Book Review: The Unix Programming Environment

book cover

A couple of weeks ago I finished reading The Unix Programming Environment, written by Brian W. Kernighan and Rob Pike. The main reason that I picked up this classic book was to learn more about the fundamentals of Unix. I must say that this book is an excellent guide to start learning more about this widespread operating system. 

Ever since I started my programming career, I had this on and off relationship with Linux. There were times that I wanted to turn my back on Windows entirely and make the jump. Then there were also times that I couldn’t care less about Linux. But when I started playing with CouchDB and especially Node.js, this fascination for the other side has been growing steadily. In fact, I kind of made a small jump already. All of the code that I’ve developed during my spare time for the past 14 months have been hacked together on a Linux VM.   

I don’t want you to fall into the impression that Windows is a bad operating system either, quite on the contrary. I’ve been doing professional software development on Windows for more than 12 years now and I still believe that it’s a rock solid operating system. Why am I not  solely sticking to software development on Windows then? Well, for the simple reason that I’ve always had this nagging feeling that I’ve been missing out by not learning equally as much about Linux as I learned the past 12 years about Windows. So I kind of suspect that I’ll be running Windows in a VM on a Linux host in the not too distant future. I’m quite fascinated and determined to find my way in this mostly unknown environment.

Anyway, back to the book. There are basically two major parts in this book. The first part deals with the basics, the file system, the shell and an introduction to sed & awk. The second part discusses more advanced topics like shell programming, programming standard I/O and Unix system calls which I skipped entirely (for now). By completely focusing on the first part, I learned quite some stuff and also relearned a few things from the past.

The only thing that bothered me about this book is that its quite boring. The book was originally published in 1984. Perhaps authors of technical books from back then were not supposed to have an entertaining writing style. I’m not sure. I was probably too busy playing with Legos at the time to notice ;-). 

I now realize that I have quite a long journey ahead of me. Good thing that I started this path sooner than later.

Until next time.

PS: I recently switched from Shelfari to GoodReads and I’m quite liking it so far. Just let me know if you have some interesting books to share.

Tuesday, February 14, 2012

View Collation in CouchDB

A couple of weeks ago I learned about a very neat feature of CouchDB called view collations. Basically, view collations enables us to make joins between documents. Let’s look at a simple example here.

Suppose we have a customer-order model where a customer can have one or more orders. The data of the documents for our example looks like this:

{
   "_id": "807023fe77d927e1c222adfe4a0007ba",
   "_rev": "2-2eae48beb08acc72ed2ab64ccfbcb1c7",
   "type": "customer",
   "firstName": "Homer",
   "lastName": "Simpson"
}

{
   "_id": "807023fe77d927e1c222adfe4a0013c2",
   "_rev": "2-9fc81f3b250c514fbd7e1d5f4a44816b",
   "type": "order",
   "amount": 34,
   "customer": "807023fe77d927e1c222adfe4a0007ba"
}

{
   "_id": "807023fe77d927e1c222adfe4a001b53",
   "_rev": "2-9dbf739124353363d53d1ba6671aac4e",
   "type": "order",
   "amount": 58,
   "customer": "807023fe77d927e1c222adfe4a0007ba"
}

{
   "_id": "807023fe77d927e1c222adfe4a002516",
   "_rev": "1-5cb6d3729618eb7db0f9285237fd8308",
   "firstName": "Ned",
   "lastName": "Flanders",
   "type": "customer"
}

{
   "_id": "807023fe77d927e1c222adfe4a002a4f",
   "_rev": "1-cc5eb0d8c6db0f698c53f9078d16db3b",
   "type": "order",
   "amount": 75,
   "customer": "807023fe77d927e1c222adfe4a002516"
}

Here we have two customers with their respective orders. The first customer has two orders and the second customer has only one order. Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document. 

The final goal is that we want to retrieve all customers with their corresponding orders using a single view. In order to accomplish this we have to make use of complex keys in our map function:

function(document) {
  if(document.type == 'customer') {
    emit([document._id, 0], document);
  }
  else if(document.type == 'order') {
    emit([document.customer, 1], document);
  } 
}

The key is composed of the ID of a customer and an  arbitrary number used for sorting (collating). For this number we’ve used 0 for customer documents and  1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:

image

Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.

We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the startkey/endkey query options in order to get a subset of the view rows:

?startkey=["807023fe77d927e1c222adfe4a0007ba"]&endkey=["807023fe77d927e1c222adfe4a0007ba", 2]

Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice. 

Until next time