Sunday, August 14, 2011

a lil bit wiser

The table had grown too big; adding new columns was not an option. I had no choice but to go bit wise.

Most often you store different attributes and their states in a single column. For instance status in a typical video work flow:
   Ingested -> Approved -> Transcoded
It is all good when it is sequential like that. What if you want to do approval and transcoding in parallel? That means you have to be able to store Approved and Transcoded, Approved but not Transcoded, Transcoded but not Approved.

One way of doing that is to add more columns is_approved, is_transcoded, is_uploaded etc. I wish I was using a NoSQL database like Mongo where adding a new column is no big deal.

Here is what I did:
UPLOADED = 1 #0th bit
APPROVED = 2 #1st bit
TRANSCODED = 4  #2nd bit

def is_approved() :
     return  status & APPROVED == APPROVED

def approve():
     status |= APPROVED

def disapprove():
      status &= ~APPROVED

My actual code is a bit more complex as I used more than 1 bit per attribute. In case of transcoding I used to 2 bits to specify - not started, in progress, success and failure.

No comments: