database-independence: it has begun.

Brad Fitzpatrick brad at danga.com
Fri Dec 29 00:42:07 UTC 2006


Mogilers,

After I removed all the old APIs and made the mogilefsd file small and
beautiful, I moved onto the oft-requested database independence
project.

Currently code is like:

    my $dbh = Mgd::get_dbh();
    $dbh->selectrow_array("SELECT MYSQL_SPECIFIC_CRAP() FROM foo WHERE blah blah",
                          undef, $param, $another_param, $etc);

I want to kill all that.  There are 50 places like that (down from 54
earlier!):

   $ grep -r get_dbh mogilefsd lib/ | grep -v text-base  | wc -l
   50

The new way is:

    my $sto = Mgd::get_store();
    my $data = $sto->some_method_on_the_store_class(@params) # %params, etc

Where the $sto is a per-process singleton.  This is Mgd::get_store():

  my ($store, $store_pid);
  sub get_store {
      return $store if $store && $store_pid == $$;
      $store_pid = $$;
      return $store = MogileFS::Store->new;
  }

Where the MogileFS::Store constructor does:

  sub new {
      my ($class) = @_;
      my $dsn = MogileFS->config('db_dsn');
      my $subclass;
      if ($dsn =~ /^DBI:mysql:/i) {
          $subclass = "MogileFS::Store::MySQL";
      }  else {
          die "Unknown database type: $dsn";
      }
      return bless {
          dsn    => MogileFS->config('db_dsn'),
          user   => MogileFS->config('db_user'),
          pass   => MogileFS->config('db_pass'),
      }, $subclass;
  }

Yes, currently just MySQL.  Volunteers for Oracle, Postgres, SQLite?
(well, SQLite would kinda suck as you couldn't have multiple trackers
then....)

But then the MySQL class is currently:

  package MogileFS::Store::MySQL;
  use strict;
  use warnings;
  use base 'MogileFS::Store';

  1;

Yes, that's it.

Because all the shared methods not requiring trickery are in the base
class.  So maintaining a new database store should be simple.

Anybody want to help with those 50 remaining get_dbh() callers?

- Brad







More information about the mogilefs mailing list