Index: trunk/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 12960)
+++ trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 12961)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: Server.pm,v 1.22 2006-12-12 00:02:53 jhoblitt Exp $
+# $Id: Server.pm,v 1.23 2007-04-23 20:45:08 jhoblitt Exp $
 
 package Nebulous::Server;
@@ -13,10 +13,11 @@
 
 use DBI;
-use Nebulous::Util qw( :standard );
+use Log::Log4perl;
 use Nebulous::Server::Config;
 use Nebulous::Server::Log;
 use Nebulous::Server::SQL;
-use Log::Log4perl;
-use Params::Validate qw( validate_pos SCALAR );
+use Nebulous::Util qw( :standard );
+use Params::Validate qw( validate_pos SCALAR SCALARREF );
+use URI::file;
 
 __PACKAGE__->mk_accessors(qw( log sql config ));
@@ -159,9 +160,6 @@
     $log->debug( "entered - @_" );
 
-    # TODO do some intelligent volume allocated 
-    # add hooks to make this decision based on avaiable space
     $volume = $self->_get_storage_volume( $volume );
 
-    my $uri;
     my $object_id;
     my $ins_id;
@@ -210,13 +208,11 @@
     }
 
-    $uri = "$volume/$key.$ins_id";
+    my $uri = URI::file->new("$volume/$key.$ins_id");
 
     $log->debug( "generated uri $uri");
 
-    my $path = _get_file_path( $uri );
-
     # TODO add some stuff here to retry if unsucessful
     eval {
-        _create_empty_file( $path );
+        _create_empty_file($uri->file);
     };
     if ( $@ ) {
@@ -237,5 +233,5 @@
     $log->debug( "leaving" );
 
-    return $uri;
+    return "$uri";
 }
 
@@ -297,6 +293,6 @@
 
         {
-            $uri = "$volume/$key.$ins_id";
-            $log->debug( "generated uri $uri");
+            $uri = URI::file->new("$volume/$key.$ins_id");
+            $log->debug("generated uri $uri");
 
             my $query = $db->prepare_cached( $sql->update_instance_uri );
@@ -311,6 +307,5 @@
 
     eval {
-        $path = _get_file_path( $uri );
-        $path = _create_empty_file( $path );
+        _create_empty_file($uri->file);
     };
     if ( $@ ) {
@@ -323,5 +318,5 @@
     $log->debug( "leaving" );
 
-    return $uri;
+    return "$uri";
 }
 
@@ -620,5 +615,5 @@
     my ( $uri ) = validate_pos( @_,
         {
-            type => SCALAR,
+            type => SCALAR|SCALARREF,
         },
     );
@@ -731,4 +726,6 @@
 }
 
+#  select *, total - used as free, (used / total) * 100 as perused from mount;
+
 sub _get_storage_volume {
     my $self = shift;
@@ -751,8 +748,8 @@
         if ( $name ) {
             $query = $db->prepare_cached( $sql->get_storage_volume_byname );
-            $rows = $query->execute( $name );
+            $rows = $query->execute(0.95, $name);
         } else {
             $query = $db->prepare_cached( $sql->get_storage_volume );
-            $rows = $query->execute;
+            $rows = $query->execute(0.95);
         }
 
@@ -762,5 +759,5 @@
         }
 
-        $volume = $query->fetchrow_hashref->{ 'uri' };
+        $volume = $query->fetchrow_hashref->{ 'mountpoint' };
 
         $query->finish;
Index: trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm
===================================================================
--- trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 12960)
+++ trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 12961)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: SQL.pm,v 1.23 2007-02-23 01:23:45 jhoblitt Exp $
+# $Id: SQL.pm,v 1.24 2007-04-23 20:45:08 jhoblitt Exp $
 
 package Nebulous::Server::SQL;
@@ -115,11 +115,30 @@
     },
     get_storage_volume_byname   => qq{
-        SELECT uri FROM volume WHERE name = ? ORDER BY rand() LIMIT 1 
+        SELECT
+            mountpoint,
+            total - used as free
+        FROM volume
+        JOIN mount      -- join limits us to currently mounted volumes
+            ON volume.path = mount.mountpoint
+        WHERE
+            used / total < ?
+            AND name = ?
+        ORDER BY free DESC
+        LIMIT 1
     },
     get_storage_volume          => qq{
-        SELECT uri FROM volume ORDER BY rand() LIMIT 1
+        SELECT
+            mountpoint,
+            total - used as free
+        FROM volume
+        JOIN mount      -- join limits us to currently mounted volumes
+            ON volume.path = mount.mountpoint
+        WHERE
+            used / total < ?
+        ORDER BY free DESC
+        LIMIT 1
     },
     new_volume          => qq{
-        INSERT INTO volume (name, uri)
+        INSERT INTO volume (name, path)
         VALUES (?, ?)
     },
@@ -130,5 +149,5 @@
     },
     get_volume_by_name => qq{
-        SELECT vol_id, name, uri
+        SELECT vol_id, name, path
         FROM volume
         WHERE name = ?
@@ -156,4 +175,5 @@
 DROP TABLE IF EXISTS lock_record;
 DROP TABLE IF EXISTS volume;
+DROP TABLE IF EXISTS mount;
 DROP TABLE IF EXISTS class;
 DROP TABLE IF EXISTS log
