Index: /branches/neb_distrib_20081210/Nebulous-Server/MANIFEST
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/MANIFEST	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/MANIFEST	(revision 20989)
@@ -35,4 +35,5 @@
 t/00_distribution.t 
 t/01_load.t
+t/02_config.t
 t/02_server_setup.t
 t/03_server_create_object.t
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm	(revision 20989)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004-2008  Joshua Hoblitt
 #
-# $Id: Server.pm,v 1.93 2008-10-13 20:41:17 jhoblitt Exp $
+# $Id: Server.pm,v 1.93.6.1 2008-12-14 22:52:37 eugene Exp $
 
 package Nebulous::Server;
@@ -9,5 +9,5 @@
 no warnings qw( uninitialized );
 
-our $VERSION = '0.15';
+our $VERSION = '0.16';
 
 use base qw( Class::Accessor::Fast );
@@ -19,8 +19,8 @@
 use File::Spec;
 use Log::Log4perl;
+use Nebulous::Keys qw( parse_neb_key parse_neb_volume );
 use Nebulous::Server::Config;
 use Nebulous::Server::Log;
 use Nebulous::Server::SQL;
-use Nebulous::Keys qw( parse_neb_key parse_neb_volume );
 use Params::Validate qw( validate_pos SCALAR SCALARREF UNDEF );
 use URI::file;
@@ -36,5 +36,13 @@
 
     # let Nebulous::Server::Config validate our params
-    my $config = Nebulous::Server::Config->init( @_ );
+    my $config = Nebulous::Server::Config->new( @_ );
+
+    return $class->new_from_config($config);
+}
+
+
+sub new_from_config
+{
+    my ($class, $config) = @_;
 
     # log4perl is not avaliable until we call init()
@@ -51,21 +59,12 @@
     $self->config($config);
 
-    # ask for the db handle as a means of validating the database parameters
-    $self->db;
-
     $log->debug( "leaving" );
-    
+
     return $self;
 }
 
-
 sub db
 {
-    my $self = shift;
-
-    if (@_) {
-        $self->{db} = $_[0];
-        return $self;
-    }
+    my ($self, $key) = @_;
 
     my $log     = $self->log;
@@ -73,11 +72,26 @@
     my $config  = $self->config;
 
+    my $db_index = 0;
+    if (defined $key) {
+        # hash the key to select the correct database instance
+        # only use the first 8 hex chars... have to be careful to avoid an int
+        # overflow here
+        $db_index = unpack("h8", sha1_hex("$key")) % $config->n_db;
+    }
+
+    # lookup to see if we have a stored dbh for this database
+    my $dbh = $self->{dbs}[$db_index];
     # if the dbh is still alive, return it
-    if (defined $self->{db} and $self->{db}->ping) {
+    if (defined $dbh and $dbh->ping) {
         $log->debug("db handle is still alive");
-        return $self->{db};
+        return $dbh;
     }
     # otherwise create a new connection
     $log->debug("db handle is dead/unopened");
+
+    # lookup database info
+    my $db_config = $config->db($db_index);
+    die "can't find database configuration info for database # $db_index"
+        unless $db_config;
 
     # if we're running under mod_perl & Apache::DBI is loaded we want to
@@ -86,10 +100,9 @@
     # processes and the database might have gone away on us.  Apache::DBI will
     # take care of getting a valid dbh back.
-    my $db;
-    eval {
-        $db = DBI->connect_cached(
-            $config->dsn,
-            $config->dbuser,
-            $config->dbpasswd,
+    eval {
+        $dbh = DBI->connect_cached(
+            $db_config->dsn,
+            $db_config->dbuser,
+            $db_config->dbpasswd,
             {
                 RaiseError => 1,
@@ -99,18 +112,18 @@
         );
 
-        $db->do( $sql->set_transaction_model );
-        $log->debug( "connected to database: ", sub { $db->data_sources; } );
-        $db->commit;
+        $dbh->do( $sql->set_transaction_model );
+        $log->debug( "connected to database: ", sub { $dbh->data_sources; } );
+        $dbh->commit;
         $log->debug("commit");
     };
     if ( $@ ) {
-        $db->rollback if $db;
+        $dbh->rollback if $dbh;
         $log->debug("rollback");
         $log->logdie( "database error: $@" );
     }
 
-    $self->{db} = $db;
-
-    return $db;
+    $self->{dbs}[$db_index] = $dbh;
+
+    return $dbh;
 }
 
@@ -139,5 +152,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug( "entered - @_" );
@@ -150,5 +163,5 @@
     # to check it after parsing the key
     if (defined $vol_name
-        and not $self->_is_valid_volume_name($key->volume)) {
+        and not $self->_is_valid_volume_name($key, $key->volume)) {
         if ($key->soft_volume) {
             $log->warn( "$vol_name is not a known volume name" );
@@ -160,5 +173,5 @@
         
     my ($vol_id, $vol_host, $vol_path, $vol_xattr)
-        = $self->_get_storage_volume($vol_name, $key->soft_volume);
+        = $self->_get_storage_volume($key, $vol_name, $key->soft_volume);
 
     my $uri;
@@ -261,5 +274,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -296,91 +309,157 @@
 
 
-sub swap_objects
-{
-    my $self = shift;
-
-    my ($key1, $key2) = validate_pos(@_,
-        {
-            type        => SCALAR,
-            callbacks   => {
-                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
-            },
-        },
-        {
-            type        => SCALAR,
-            callbacks   => {
-                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
-            },
-        },
-    );
-
-    my $log = $self->log;
-    my $sql = $self->sql;
-    my $db  =$self->db;
-
-    $log->debug("entered - @_");
-
-    # ignore volumes
-    $key1 = parse_neb_key($key1);
-    $key2 = parse_neb_key($key2);
-
-    # order of operations for the swap:
-    # key1 -> key1.swap
-    # key2 -> key1
-    # key1.swap -> key2
-
-    eval {
-        {
-            # key1 -> key1.swap
-            my $query = $db->prepare_cached($sql->rename_object); 
-            # this SQL statment takes the new key name as the first param
-            my $rows = $query->execute($key1->path . ".swap", $key1->path);
-
-            # if we affected more then one row something very bad has happened.
-            unless ($rows == 1) {
-                $query->finish;
-                $log->logdie("affected row count is $rows instead of 1");
-            }
-        }
-
-        {
-            # key2 -> key1
-            my $query = $db->prepare_cached($sql->rename_object); 
-            # this SQL statment takes the new key name as the first param
-            my $rows = $query->execute($key1->path, $key2->path);
-
-            # if we affected more then one row something very bad has happened.
-            unless ($rows == 1) {
-                $query->finish;
-                $log->logdie("affected row count is $rows instead of 1");
-            }
-        }
-
-        {
-            # key1.swap -> key2
-            my $query = $db->prepare_cached($sql->rename_object); 
-            # this SQL statment takes the new key name as the first param
-            my $rows = $query->execute($key2->path, $key1->path . ".swap");
-
-            # if we affected more then one row something very bad has happened.
-            unless ($rows == 1) {
-                $query->finish;
-                $log->logdie("affected row count is $rows instead of 1");
-            }
-        }
-
-        $db->commit;
-        $log->debug("commit");
-    };
-    if ($@) {
-        $db->rollback;
-        $log->debug("rollback");
-        $log->logdie("database error: $@");
-    }
-
-    $log->debug("leaving");
-
-    return 1;
-}
+# sub swap_objects
+# {
+#     my $self = shift;
+# 
+#     my ($key1, $key2) = validate_pos(@_,
+#         {
+#             type        => SCALAR,
+#             callbacks   => {
+#                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
+#             },
+#         },
+#         {
+#             type        => SCALAR,
+#             callbacks   => {
+#                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
+#             },
+#         },
+#     );
+# 
+#     my $log  = $self->log;
+#     my $sql  = $self->sql;
+#     my $dbh1 = $self->db($key1);
+#     my $dbh2 = $self->db($key2);
+# 
+#     $log->debug("entered - @_");
+# 
+#     # ignore volumes
+#     $key1 = parse_neb_key($key1);
+#     $key2 = parse_neb_key($key2);
+# 
+#     # order of operations for the swap with a single db is:
+#     # key1 -> key1.swap
+#     # key2 -> key1
+#     # key1.swap -> key2
+# 
+#     # XXX this cmp will only work if ->db() returns the same exact (cached) dbh
+#     if ($dbh1 == $dbh2) {
+#         my $dbh = $dbh1;
+#         eval {
+#             {
+#                 # key1 -> key1.swap
+#                 my $query = $dbh->prepare_cached($sql->rename_object); 
+#                 # this SQL statment takes the new key name as the first param
+#                 my $rows = $query->execute($key1->path . ".swap", $key1->path);
+# 
+#                 # if we affected more then one row something very bad has happened.
+#                 unless ($rows == 1) {
+#                     $query->finish;
+#                     $log->logdie("affected row count is $rows instead of 1");
+#                 }
+#             }
+# 
+#             {
+#                 # key2 -> key1
+#                 my $query = $dbh->prepare_cached($sql->rename_object); 
+#                 # this SQL statment takes the new key name as the first param
+#                 my $rows = $query->execute($key1->path, $key2->path);
+# 
+#                 # if we affected more then one row something very bad has happened.
+#                 unless ($rows == 1) {
+#                     $query->finish;
+#                     $log->logdie("affected row count is $rows instead of 1");
+#                 }
+#             }
+# 
+#             {
+#                 # key1.swap -> key2
+#                 my $query = $dbh->prepare_cached($sql->rename_object); 
+#                 # this SQL statment takes the new key name as the first param
+#                 my $rows = $query->execute($key2->path, $key1->path . ".swap");
+# 
+#                 # if we affected more then one row something very bad has happened.
+#                 unless ($rows == 1) {
+#                     $query->finish;
+#                     $log->logdie("affected row count is $rows instead of 1");
+#                 }
+#             }
+# 
+#             $dbn->commit;
+#             $log->debug("commit");
+#         };
+#         if ($@) {
+#             $dbh->rollback;
+#             $log->debug("rollback");
+#             $log->logdie("database error: $@");
+#         }
+#     }
+# 
+#     # order of operations for the swap between two dbs is:
+#     # key1 start transaction
+#     # key1 -> read all instances
+#     # key1 -> remove all instances
+#     # key2 start transaction
+#     # key2 -> read all instances
+#     # key2 -> remove all instances
+#     # key1 -> insert key 2 instances
+#     # key2 -> insert key 1 instances
+#     # key1,2 commit
+# 
+#     eval {
+#         {
+#             # key1 -> read all instances
+#             my $query = $dbh->prepare_cached($sql->rename_object); 
+#             # this SQL statment takes the new key name as the first param
+#             my $rows = $query->execute($key1->path . ".swap", $key1->path);
+# 
+#             # if we affected more then one row something very bad has happened.
+#             unless ($rows == 1) {
+#                 $query->finish;
+#                 $log->logdie("affected row count is $rows instead of 1");
+#             }
+#         }
+# 
+#         {
+#             # key2 -> key1
+#             my $query = $db->prepare_cached($sql->rename_object); 
+#             # this SQL statment takes the new key name as the first param
+#             my $rows = $query->execute($key1->path, $key2->path);
+# 
+#             # if we affected more then one row something very bad has happened.
+#             unless ($rows == 1) {
+#                 $query->finish;
+#                 $log->logdie("affected row count is $rows instead of 1");
+#             }
+#         }
+# 
+#         {
+#             # key1.swap -> key2
+#             my $query = $db->prepare_cached($sql->rename_object); 
+#             # this SQL statment takes the new key name as the first param
+#             my $rows = $query->execute($key2->path, $key1->path . ".swap");
+# 
+#             # if we affected more then one row something very bad has happened.
+#             unless ($rows == 1) {
+#                 $query->finish;
+#                 $log->logdie("affected row count is $rows instead of 1");
+#             }
+#         }
+# 
+#         $db->commit;
+#         $log->debug("commit");
+#     };
+#         if ($@) {
+#             $db->rollback;
+#             $log->debug("rollback");
+#             $log->logdie("database error: $@");
+#         }
+# 
+# 
+#     $log->debug("leaving");
+# 
+#     return 1;
+# }
 
 
@@ -420,5 +499,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -429,5 +508,5 @@
 
     if (defined $vol_name
-        and not $self->_is_valid_volume_name($key->volume)) {
+        and not $self->_is_valid_volume_name($key, $key->volume)) {
         if ($key->soft_volume) {
             $log->warn( "$vol_name is not a known volume name" );
@@ -441,5 +520,5 @@
     if (defined $vol_name) {
         ($vol_id, $vol_host, $vol_path, $vol_xattr)
-            = $self->_get_storage_volume($vol_name);
+            = $self->_get_storage_volume($key, $vol_name);
     } else {
         ($vol_id, $vol_host, $vol_path, $vol_xattr)
@@ -534,5 +613,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug( "entered - @_" );
@@ -637,5 +716,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug( "entered - @_" );
@@ -749,5 +828,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -816,5 +895,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -869,5 +948,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -912,5 +991,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -947,7 +1026,9 @@
 sub find_objects
 {
-    my $self = shift;
-
-    my ( $pattern ) = validate_pos( @_,
+    # XXX: this will only search one db
+
+    my $self = shift;
+
+    my ($pattern) = validate_pos( @_,
         {
             type        => SCALAR,
@@ -958,5 +1039,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db;
 
     $log->debug( "entered - @_" );
@@ -1016,5 +1097,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -1027,5 +1108,5 @@
     # to check it after parsing the key
     if (defined $vol_name
-        and not $self->_is_valid_volume_name($key->volume)) {
+        and not $self->_is_valid_volume_name($key, $key->volume)) {
         if ($key->soft_volume) {
             $log->warn( "$vol_name is not a known volume name" );
@@ -1087,5 +1168,11 @@
     my $self = shift;
 
-    my ( $uri ) = validate_pos( @_,
+    my ($key, $uri) = validate_pos( @_,
+        {
+            type        => SCALAR,
+            callbacks   => {
+                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
+            },
+        },
         {
             type => SCALAR|SCALARREF,
@@ -1095,5 +1182,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug( "entered - @_" );
@@ -1182,5 +1269,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $log->debug("entered - @_");
@@ -1211,4 +1298,5 @@
 sub mounts
 {
+    # XXX: this will only pull the mounts from one db
     my $self = shift;
 
@@ -1217,5 +1305,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db;
 
     $log->debug("entered - @_");
@@ -1246,13 +1334,13 @@
     my $self = shift;
 
-    my $log = $self->log;
-    my $sql = $self->sql;
-    my $db  = $self->db;
+    my ($key, $name, $soft_volume) = @_;
+
+    my $log = $self->log;
+    my $sql = $self->sql;
+    my $db  = $self->db($key);
 
     no warnings qw( uninitialized );
     $log->debug( "entered - @_" );
     use warnings;
-
-    my ($name, $soft_volume) = @_;
 
     my ($vol_id, $vol_host, $vol_path, $xattr);
@@ -1271,5 +1359,5 @@
                 # find it, fall back to any volume
                 if ($soft_volume) {
-                    ($vol_id, $vol_host, $vol_path, $xattr) = $self->_get_storage_volume;
+                    ($vol_id, $vol_host, $vol_path, $xattr) = $self->_get_storage_volume($key);
                     return; # this just returns out of the eval not from the subroutine
                 }
@@ -1311,7 +1399,9 @@
     my $self = shift;
 
-    my $log = $self->log;
-    my $sql = $self->sql;
-    my $db  =$self->db;
+    my $key = shift;
+
+    my $log = $self->log;
+    my $sql = $self->sql;
+    my $db  = $self->db($key);
 
     no warnings qw( uninitialized );
@@ -1319,5 +1409,4 @@
     use warnings;
 
-    my $key = shift;
 
     $key = parse_neb_key($key);
@@ -1361,5 +1450,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+    my $db  = $self->db($key);
 
     $key = parse_neb_key($key);
@@ -1388,9 +1477,9 @@
 sub _is_valid_volume_name
 {
-    my ($self, $vol_name) = @_;
-
-    my $log = $self->log;
-    my $sql = $self->sql;
-    my $db  =$self->db;
+    my ($self, $key, $vol_name) = @_;
+
+    my $log = $self->log;
+    my $sql = $self->sql;
+    my $db  = $self->db($key);
 
     my $volume_info = parse_neb_volume($vol_name);
@@ -1429,5 +1518,5 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  = $self->db;
+    my $db  = $self->db($key);
 
     my $uri;
@@ -1558,11 +1647,11 @@
     my $log = $self->log;
     my $sql = $self->sql;
-    my $db  =$self->db;
+#    my $db  = $self->db;
 
     $log->debug( "entered" );
 
-    $self->db->disconnect;        
-
-    $log->debug( "disconnected from database: ", sub { $db->data_sources; } );
+#    $self->db->disconnect;        
+
+#    $log->debug( "disconnected from database: ", sub { $db->data_sources; } );
 
     $log->debug( "leaving" );
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm	(revision 20989)
@@ -1,5 +1,5 @@
 # Copyright (C) 2005  Joshua Hoblitt
 #
-# $Id: Config.pm,v 1.3 2008-03-20 21:10:57 jhoblitt Exp $
+# $Id: Config.pm,v 1.3.22.1 2008-12-14 22:52:37 eugene Exp $
 
 package Nebulous::Server::Config;
@@ -8,10 +8,10 @@
 use warnings FATAL => qw( all );
 
-our $VERSION = 0.02;
+our $VERSION = 0.03;
 
 use base qw( Class::Accessor::Fast );
 
 use Log::Log4perl qw( :levels );
-use Params::Validate qw( validate SCALAR );
+use Params::Validate qw( validate validate_pos SCALAR );
 
 our %LEVELS = (
@@ -25,8 +25,15 @@
 );
 
-my $new_validate = {
+my $db_validate = {
+    dbindex       => {
+        type => SCALAR,
+        regex => qr/^\d+$/,
+    },
     dsn         => { type => SCALAR },
     dbuser      => { type => SCALAR },
     dbpasswd    => { type => SCALAR },
+};
+
+my $new_validate = {
     log_level   => {
         type        => SCALAR,
@@ -39,9 +46,14 @@
         },
     },
+    dsn         => { type => SCALAR, optional => 1 },
+    dbuser      => { type => SCALAR, optional => 1 },
+    dbpasswd    => { type => SCALAR, optional => 1 },
 };
 
 __PACKAGE__->mk_ro_accessors( keys %$new_validate );
 
-sub init {
+
+sub new
+{
     my $class = shift;
 
@@ -49,13 +61,74 @@
 
     # normalize log levels to lower-case
-    $p{ log_level } = lc $p{ log_level };
-
-    my $self = \%p;
+    my $self ={ log_level => lc $p{ log_level } };
 
     bless $self, $class || ref $class;
+
+    my @dbs;
+    $self->{dbs} = \@dbs;
+
+    if (defined $p{dsn} or defined $p{dbuser} or defined $p{dbpasswd}) {
+        $self->add_db(
+            dbindex     => 0,
+            dsn         => $p{dsn},
+            dbuser      => $p{dbuser},
+            dbpasswd    => $p{dbpasswd},
+        );
+    }
 
     return $self;
 }
 
+
+sub add_db
+{
+    my $self = shift;
+    
+    my %p = validate( @_, $db_validate );
+
+    my $config_db = Nebulous::Server::Config::DB->new({
+        dsn         => $p{dsn},
+        dbuser      => $p{dbuser},
+        dbpasswd    => $p{dbpasswd},
+    });
+
+    $self->{dbs}->[$p{dbindex}] = $config_db;
+
+    return $self;
+}
+
+
+sub db 
+{
+    my $self = shift;
+
+    my ($db_index) = validate_pos( @_, { type => SCALAR, optional => 1, });
+
+    # default to 0
+    $db_index ||= 0;
+
+    return $self->{dbs}->[$db_index];
+}
+
+
+sub n_db 
+{
+    my $self = shift;
+
+    return scalar @{ $self->{dbs} };
+}
+
+
+package Nebulous::Server::Config::DB;
+
+use strict;
+use warnings FATAL => qw( all );
+
+our $VERSION = 0.01;
+
+use base qw( Class::Accessor::Fast );
+
+__PACKAGE__->mk_ro_accessors(qw( dsn dbuser dbpasswd )); 
+
 1;
 
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Log.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Log.pm	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Log.pm	(revision 20989)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: Log.pm,v 1.7 2008-05-07 00:02:10 jhoblitt Exp $
+# $Id: Log.pm,v 1.7.22.1 2008-12-14 22:52:37 eugene Exp $
 
 package Nebulous::Server::Log;
@@ -16,7 +16,7 @@
     my ($self, $config) = @_;
 
-    my $dsn         = $config->dsn;
-    my $dbuser      = $config->dbuser;
-    my $dbpasswd    = $config->dbpasswd;
+#    my $dsn         = $config->db->dsn;
+#    my $dbuser      = $config->db->dbuser;
+#    my $dbpasswd    = $config->db->dbpasswd;
 
     my $conf = <<END;
@@ -30,9 +30,9 @@
 #   date | hostname | priority | method/sub - message\n
 
-    log4perl.appender.SQLLOG            = Log::Log4perl::Appender::DBI
-    log4perl.appender.SQLLOG.datasource = $dsn
-    log4perl.appender.SQLLOG.username   = $dbuser
-    log4perl.appender.SQLLOG.password   = $dbpasswd
-    log4perl.appender.SQLLOG.sql        = \
+#    log4perl.appender.SQLLOG            = Log::Log4perl::Appender::DBI
+#    log4perl.appender.SQLLOG.sql        = \
+#    log4perl.appender.SQLLOG.datasource = 
+#    log4perl.appender.SQLLOG.username   = 
+#    log4perl.appender.SQLLOG.password   = 
     INSERT INTO log (timestamp, hostname, level, sub, message) \
     VALUES          (%d,        %H,       %p,    %M,  %m)
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SOAP.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SOAP.pm	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SOAP.pm	(revision 20989)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: SOAP.pm,v 1.4 2007-05-02 00:42:52 jhoblitt Exp $
+# $Id: SOAP.pm,v 1.4.32.1 2008-12-14 22:52:37 eugene Exp $
 
 package Nebulous::Server::SOAP;
@@ -17,8 +17,10 @@
 our $AUTOLOAD;
 
-our @args;
+our $config;
 our $neb;
 
-sub new_on_init {
+
+sub new_on_init
+{
     my $self = shift;
 
@@ -27,5 +29,5 @@
     require Apache2::ServerUtil;
 
-    @args = @_;
+    $config = shift;
 
     my $s = Apache2::ServerUtil->server;
@@ -35,13 +37,17 @@
 }
 
-sub init {
+
+sub init
+{
     my $self = shift;
 
-    $neb = Nebulous::Server->new(@args);        
+    $neb = Nebulous::Server->new_from_config($config);        
 
     return Apache2::Const::OK;
 }
 
-sub AUTOLOAD {
+
+sub AUTOLOAD
+{
     my $self = shift;
 
@@ -76,3 +82,4 @@
 }
 
+
 1;
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 20989)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: SQL.pm,v 1.75 2008-10-15 20:45:51 jhoblitt Exp $
+# $Id: SQL.pm,v 1.75.6.1 2008-12-14 22:52:37 eugene Exp $
 
 package Nebulous::Server::SQL;
@@ -8,5 +8,5 @@
 use warnings FATAL => qw( all );
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 use base qw( Class::Accessor::Fast );
@@ -47,4 +47,8 @@
         (so_id, vol_id, uri)
         VALUES (?, ?, 'error')
+    },
+    get_all_instances   => qq{
+        SELECT * FROM INSTANCE
+        WHERE so_id = ?
     },
     get_object          => qq{
@@ -324,28 +328,26 @@
         HAVING available_instances < instances OR instances < copies
     },
-    find_objects_with_extra_instances => qq{
-        SELECT
-            storage_object.so_id,
-            ext_id,
+    find_objects_with_extra_instances_by_xattr => qq{
+        SELECT
+            so.so_id,
+            so.ext_id,
             count(ins_id) as instances,
-            volume.name as volume_name,
-            volume.host as volume_host,
-            count(mymountedvol.vol_id) as available_instances,
-            count(mymountedvol.vol_id) > 0 as recoverable,
-            storage_object_xattr.value as copies
-        FROM storage_object
-        JOIN instance
-            USING(so_id)
-        JOIN volume
-            USING(vol_id)
-        LEFT JOIN storage_object_xattr
-            ON storage_object.so_id = storage_object_xattr.so_id
-        JOIN mymountedvol
+            mv.name as volume_name,
+            mv.host as volume_host,
+            count(mv.vol_id) as available_instances,
+            xattr.value as copies
+        FROM storage_object AS so
+        JOIN storage_object_xattr as xattr
+            ON so.so_id = xattr.so_id
+            AND xattr.name = 'user.copies'
+        JOIN instance AS i
+            ON so.so_id = i.so_id
+        JOIN mountedvol AS mv
             USING(vol_id)
         WHERE
-            mymountedvol.available = 1
-            AND storage_object_xattr.name = 'user.copies'
+            mv.available = 1
         GROUP BY so_id
         HAVING available_instances > copies
+        limit 5;
     },
     get_mounted_volumes => qq{
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/02_config.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/02_config.t	(revision 20989)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/02_config.t	(revision 20989)
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+
+# Copryight (C) 2004-2005  Joshua Hoblitt
+#
+# $Id: 02_config.t,v 1.1.2.2 2008-12-14 22:52:37 eugene Exp $
+
+use strict;
+use warnings;
+
+use Test::More tests => 21;
+
+use lib qw( ./t ./lib );
+
+use Nebulous::Server::Config;
+
+isa_ok(Nebulous::Server::Config->new(), "Nebulous::Server::Config");
+
+{
+    my $config = Nebulous::Server::Config->new;
+
+    ok($config->add_db(
+        dbindex     => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    ), "add a db");
+
+    is($config->n_db, 1, "number of dbs");
+}
+
+
+{
+    my $config = Nebulous::Server::Config->new;
+
+    ok($config->add_db(
+        dbindex     => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    ), "add a db");
+
+    ok($config->add_db(
+        dbindex     => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    ), "add a db");
+
+    is($config->n_db, 1, "number of dbs");
+}
+
+{
+    my $config = Nebulous::Server::Config->new;
+
+    ok($config->add_db(
+        dbindex     => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    ), "add a db");
+
+    ok($config->add_db(
+        dbindex     => 1,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    ), "add a db");
+
+    is($config->n_db, 2, "number of dbs");
+}
+
+{
+    my $config = Nebulous::Server::Config->new;
+
+    $config->add_db(
+        dbindex     => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    => "boo",
+    );
+
+    $config->add_db(
+        dbindex     => 1,
+        dsn         => "DBI:mysql:database=foobar:host=localhost2",
+        dbuser      => "baz2",
+        dbpasswd    => "boo2",
+    );
+
+    # default should be 0
+    my $config_db = $config->db();
+
+    isa_ok($config_db, "Nebulous::Server::Config::DB");
+    is($config_db->dsn, "DBI:mysql:database=foobar:host=localhost", "dsn");
+    is($config_db->dbuser, "baz", "dbuser");
+    is($config_db->dbpasswd, "boo", "dbpasswd");
+
+    my $config_db0 = $config->db(0);
+
+    isa_ok($config_db0, "Nebulous::Server::Config::DB");
+    is($config_db0->dsn, "DBI:mysql:database=foobar:host=localhost", "dsn");
+    is($config_db0->dbuser, "baz", "dbuser");
+    is($config_db0->dbpasswd, "boo", "dbpasswd");
+
+    my $config_db1 = $config->db(1);
+
+    isa_ok($config_db1, "Nebulous::Server::Config::DB");
+    is($config_db1->dsn, "DBI:mysql:database=foobar:host=localhost2", "dsn");
+    is($config_db1->dbuser, "baz2", "dbuser");
+    is($config_db1->dbpasswd, "boo2", "dbpasswd");
+}
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t	(revision 20989)
@@ -3,10 +3,10 @@
 # Copryight (C) 2004-2005  Joshua Hoblitt
 #
-# $Id: 02_server_setup.t,v 1.6 2008-02-02 01:51:29 jhoblitt Exp $
+# $Id: 02_server_setup.t,v 1.6.22.1 2008-12-14 22:52:37 eugene Exp $
 
 use strict;
 use warnings;
 
-use Test::More tests => 2;
+use Test::More tests => 6;
 
 use lib qw( ./t ./lib );
@@ -17,23 +17,49 @@
 Test::Nebulous->setup;
 
-isa_ok(
-    Nebulous::Server->new(
-        dsn         => $NEB_DB,
-        dbuser      => $NEB_USER,
-        dbpasswd    => $NEB_PASS,
-    ),
-    "Nebulous::Server"
-);
+isa_ok(Nebulous::Server->new(), "Nebulous::Server");
 
 Test::Nebulous->setup;
 
-eval {
-    Nebulous::Server->new(
+# ->new()
+{
+    my $neb = Nebulous::Server->new( log_level => 'off' );
+
+    ok($neb, "set log level");
+}
+
+Test::Nebulous->setup;
+
+{
+    my $neb = Nebulous::Server->new(
         dsn         => "DBI:mysql:database=foobar:host=localhost",
         dbuser      => "baz",
         dbpasswd    =>"boo",
     );
-};
-like( $@, qr/DBI connect.*? failed/, "bad dsn/user/pass" );
+
+    ok($neb, "set database");
+}
+
+Test::Nebulous->setup;
+
+# add dbs after ->new()
+{
+    my $neb = Nebulous::Server->new;
+
+    ok($neb->config->add_db(
+        dbindex    => 0,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    =>"boo",
+    ), "add db");
+    
+    ok($neb->config->add_db(
+        dbindex    => 1,
+        dsn         => "DBI:mysql:database=foobar:host=localhost",
+        dbuser      => "baz",
+        dbpasswd    =>"boo",
+    ), "add dbs");
+
+    is($neb->config->n_db, 2, "n dbs")
+}
 
 Test::Nebulous->cleanup;
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/08_server_delete_instance.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/08_server_delete_instance.t	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/08_server_delete_instance.t	(revision 20989)
@@ -3,5 +3,5 @@
 # Copryight (C) 2004-2005  Joshua Hoblitt
 #
-# $Id: 08_server_delete_instance.t,v 1.10 2008-03-20 21:10:14 jhoblitt Exp $
+# $Id: 08_server_delete_instance.t,v 1.10.22.1 2008-12-14 22:52:37 eugene Exp $
 
 use strict;
@@ -24,7 +24,8 @@
 
 {
-    my $uri = $neb->create_object("foo");
+    my $key = "foo";
+    my $uri = $neb->create_object($key);
 
-    ok($neb->delete_instance($uri), "delete instance");
+    ok($neb->delete_instance($key, $uri), "delete instance");
 }
 
@@ -32,17 +33,18 @@
 
 {
-    my $uri1 = $neb->create_object("foo");
-    my $uri2 = $neb->replicate_object("foo");
+    my $key = "foo";
+    my $uri1 = $neb->create_object($key);
+    my $uri2 = $neb->replicate_object($key);
 
-    ok($neb->delete_instance($uri1), "delete instance");
+    ok($neb->delete_instance($key, $uri1), "delete instance");
 
-    my $locations = $neb->find_instances("foo");
+    my $locations = $neb->find_instances($key);
 
     is($locations->[0], $uri2, "instance remains");
 
-    ok($neb->delete_instance( $uri2 ), "delete instance");
+    ok($neb->delete_instance($key, $uri2), "delete instance");
 
     eval {
-        $neb->find_instances("foo");
+        $neb->find_instances($key);
     };
     like($@, qr/is valid object key/, "storage object was deleted");
@@ -52,5 +54,8 @@
 
 eval {
-    $neb->delete_instance("file:/foo");
+    my $key = "foo";
+    my $uri1 = $neb->create_object($key);
+
+    $neb->delete_instance($key, "file:/foo");
 };
 like($@, qr/no instance is associated with uri/, "uri does not exist");
@@ -61,12 +66,15 @@
     $neb->delete_instance();
 };
-like($@, qr/1 was expected/, "no params");
+like($@, qr/2 were expected/, "no params");
 
 Test::Nebulous->setup;
 
 eval {
-    $neb->delete_instance("foo", 2);
+    my $key = "foo";
+    my $uri1 = $neb->create_object($key);
+
+    $neb->delete_instance("foo", 2, 3);
 };
-like($@, qr/1 was expected/, "too many params");
+like($@, qr/2 were expected/, "too many params");
 
 Test::Nebulous->cleanup;
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/10_server_is_valid_volume_name.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/10_server_is_valid_volume_name.t	(revision 20988)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/10_server_is_valid_volume_name.t	(revision 20989)
@@ -3,5 +3,5 @@
 # Copryight (C) 2004-2005  Joshua Hoblitt
 #
-# $Id: 10_server_is_valid_volume_name.t,v 1.5 2008-03-20 21:10:14 jhoblitt Exp $
+# $Id: 10_server_is_valid_volume_name.t,v 1.5.22.1 2008-12-14 22:52:37 eugene Exp $
 
 use strict;
@@ -23,13 +23,13 @@
 Test::Nebulous->setup;
 
-ok($neb->_is_valid_volume_name('node01'), "valid volume name");
+ok($neb->_is_valid_volume_name('foo', 'node01'), "valid volume name");
 
 Test::Nebulous->setup;
 
-ok($neb->_is_valid_volume_name('node02'), "valid volume name");
+ok($neb->_is_valid_volume_name('foo', 'node02'), "valid volume name");
 
 Test::Nebulous->setup;
 
-is($neb->_is_valid_volume_name('node99'), undef, "invalid volume name");
+is($neb->_is_valid_volume_name('foo', 'node99'), undef, "invalid volume name");
 
 Test::Nebulous->cleanup;
