Active Record

One of my favorite design patterns is the Active Record pattern. For those of you who haven’t heard of it, this pattern abstracts database access to a very simplistic level. I first starting playing with my own implementations a couple years ago, but it wasn’t until I used Ruby on Rails that I first saw a fully functional implementation.

At Webmail.us, I have decided to use my side-project hours to work on my own version of the active record pattern. It has been a slow going process, since I rarely find free hours to work on it, but I have finally created a workable version. But, before we get to the inner workings of my implementation, I need to give credit where credit is due.

Rails Active Record:

Since the first active record pattern I encountered was the Rails implementation, I decided to start there with my first PHP implementation. It wasn’t long before I realized the PHP vs. Ruby differences were going to be a serious problem.

I wanted the find functions to be static, because an object shouldn’t have to populate itself after it has been instantiated. Unfortunately, in PHP, static methods aren’t capable of polymorphism (which makes sense since $this isn’t applicable). Therefore, overloading them becomes impossible.

CakePHP solved this by making all the find functions non-static, but I didn’t like this solution either, because it doesn’t make sense for the previously stated reasons. Then I found …..

Zend DB:

The Zend framework, while still very new, has some powerful features, namely the DB section. When I first saw this implementation of the active record pattern, I knew this was the starting point for my own version.

Zend DB has three main classes, Zend_Db_Table, Zend_Db_Table_Row, and Zend_Db_Table_Rowset. This design is much more flexible, and makes more sense to me than Rails’ and CakePHP’s implementations. Each database object is abstracted to its own classes, aka tables, rows, and rowsets (or arrays of rows).

At first, I almost decided to use this implementation and forgo the long process of creating my own. But, it fell short of my requirements for a few different reasons. The biggest problem was that the Row and RowSet class names are hard-coded in the Table class. This means overloading them is impossible:

1
2
3
4
5
return new Zend_Db_Table_Row(array(
    'db'    => $this->_db,
    'table' => $this,
    'data'  => $this->_fetch('Row', $where, $order, 1),
));

It also uses prepared statements, which I have never used and don’t really have a desire to. They are definitely slower than straight MySQL calls and I don’t have the mysqli extension installed so that is out (Although I should probably work on getting that extension installed).


Even though Rails, CakePHP, and Zend DB didn’t work out for me, they played in important role in leading me towards my current solution. In the future, I will discuss in more depth my own implementation and keep everyone updated with how it is working out.

SSHFS

I recently discovered SSHFS, or SSH filesytem, and I haven’t used Samba or NFS for my Linux sharing needs since. It has all the features of Samba and NFS, without all the headaches. To install on Ubuntu, just:

apt-get install sshfs
usermod -a -G fuse username

and to mount a filesystem:

sshfs username@remote_machine:remote_dir local_dir

You may be wondering why I have become such an advocate for SSHFS overnight. Here are the biggest benefits I see:

  • Encryption
  • Reliability
  • User mounting without any root privileges
  • No Gnome keyring to manage Samba password (I hate Gnome keyring)
  • NO SERVER SETUP

Unfortunately, there are a few downsides to using this remote filesystem utility. Supposedly SSHFS is not as fast as Samba or NFS although I can’t really tell any differences. Also, to mount a windows directory, the windows machine must have OpenSSH for Windows installed, which is just a subset of Cygwin.

If you are planning on using Subversion over SSHFS, make sure you connect using the “-o workaround=rename” option because of a POSIX vs. SFTP problem discussed on the FUSE wiki. I don’t recommend using svn over SSHFS because of how I/O intense it is though. Since svn has to read so many files when performing operations, it has proven quite slow in my experience. I read a post saying “-o direct_io” and “-C” options can improve performance, but I didn’t really see any differences. That could be because the working versions I have are around 75 MB each.

I encourage anyone to try it out, you won’t be disappointed.

First Post

Well, like so many others around me, I have decided to start a blog for a few reasons. First, I feel left out especially after I heard my friend Sola, who is quite computer illiterate, had a blog. Second, I have a problem with forgetting things, especially work/programming related. With a blog, I can easily document my thoughts and search through them once my brain has long forgotten. Lastly, I hope that a few people may enjoy reading my rants, thoughts, and anything else I decide to write about.

In this blog, some of the topics I hope to write about will relate to:

Since the last time I took an English class was 2001, I will try not to bore anyone with long posts about topics that are meaningless to anyone but me. That is why any input as to what you, the reader, would like to hear me write about is greatly appreciated.

So, without making you wait any longer, here is the end of my first post.