17.2.2. Filesystem Databases
Many people don't realize that in some cases, the
filesystem can serve perfectly well as a database. In fact, you are
probably using this kind of database every day on your PC—for
example, if you store your MP3 files categorized by genres, artists,
and albums. If we run:
panic% cd /data/mp3
panic% find .
We can see all the MP3 files that we have under
/data/mp3:
./Rock/Bjork/MTV Unplugged/01 - Human Behaviour.mp3
./Rock/Bjork/MTV Unplugged/02 - One Day.mp3
./Rock/Bjork/MTV Unplugged/03 - Come To Me.mp3
...
./Rock/Bjork/Europa/01 - Prologue.mp3
./Rock/Bjork/Europa/02 - Hunter.mp3
...
./Rock/Nirvana/MTV Unplugged/01 - About A Girl.mp3
./Rock/Nirvana/MTV Unplugged/02 - Come As You Are.mp3
...
./Jazz/Herbie Hancock/Head Hunters/01 - Chameleon.mp3
./Jazz/Herbie Hancock/Head Hunters/02 - Watermelon Man.mp3
Now if we want to query what artists we have in the Rock genre, we
just need to list the files in the Rock/
directory. Once we find out that Bjork is one of the artists in the
Rock category, we can do another enquiry to find out what Bjork
albums we have bought by listing the files under the
Rock/Bjork/ directory. Now if we want to see the
actual MP3 files from a particular album (e.g., MTV
Unplugged), we list the files under that directory.
What if we want to find all the albums that have
MTV in their names? We can use
ls to give us all the albums and MP3 files:
panic% ls -l ./*/*/*MTV*
Of course, filesystem manipulation can be done from your Perl program.
Let's look at another example. If you run a site
about rock groups, you might want to store images relating to
different groups. Using the filesystem as a database is a perfect
match. Chances are these images will be served to users via
<img> tags, so it makes perfect sense to use
the real path (DocumentRoot considerations aside)
to the image. For example:
<img src="/images/rock/ACDC/cover-front.gif" alt="ACDC" ...>
<img src="/images/rock/ACDC/cover-back.gif" alt="ACDC" ...>
In this example we treat ACDC as a record and
cover-front.gif and
cover-back.gif as fields. This database
implementation, just like the flat-file database, has no special
benefits under mod_perl, so we aren't going to
expand on the idea, but it's worth keeping in mind.