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
Index: /trunk/Nebulous/lib/Nebulous/Client.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Client.pm	(revision 12960)
+++ /trunk/Nebulous/lib/Nebulous/Client.pm	(revision 12961)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: Client.pm,v 1.23 2005-12-03 03:19:43 jhoblitt Exp $
+# $Id: Client.pm,v 1.24 2007-04-23 20:45:08 jhoblitt Exp $
 
 package Nebulous::Client;
@@ -123,19 +123,14 @@
     }
 
-    my $uri = $response->result;
-
-    $log->debug( "server allocated $uri" );
-
-    my $filename;
-    eval {
-        $filename = _get_file_path( $uri );
-    };
-    $log->logdie( $@ ) if $@;
-
+    my $res = $response->result;
+    $log->debug( "server respone $res" );
+
+    my $uri = URI->new($res);
+    my $filename = $uri->file;
     $log->debug( "local filename is $filename" );
 
     $log->debug( "leaving" );
 
-    return $filename;
+    return $uri;
 }
 
@@ -176,7 +171,10 @@
     }
 
-    my $uri = $response->result;
-
-    $log->debug( "server allocated $uri" );
+    my $res = $response->result;
+    $log->debug( "server response $res" );
+
+    my $uri = URI->new($res);
+    my $filename = $uri->file;
+    $log->debug( "local filename is $filename" );
 
     my $fh;
@@ -215,4 +213,11 @@
     }
 
+    my $res = $response->result;
+    $log->debug( "server respone $res" );
+
+    my $uri = URI->new($res);
+    my $filename = $uri->file;
+    $log->debug( "local filename is $filename" );
+
     my $fh  = $self->open( $key, 'read' );
     unless ( $fh ) {
@@ -222,8 +227,4 @@
         return undef;
     }
-
-    $log->debug( "server allocated new instance" );
-
-    my $uri = $response->result;
 
     my $new_fh;
Index: /trunk/Nebulous/lib/Nebulous/Server.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Server.pm	(revision 12960)
+++ /trunk/Nebulous/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/lib/Nebulous/Server/SQL.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Server/SQL.pm	(revision 12960)
+++ /trunk/Nebulous/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
Index: /trunk/Nebulous/lib/Nebulous/Util.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Util.pm	(revision 12960)
+++ /trunk/Nebulous/lib/Nebulous/Util.pm	(revision 12961)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: Util.pm,v 1.3 2005-06-30 02:35:06 jhoblitt Exp $
+# $Id: Util.pm,v 1.4 2007-04-23 20:45:08 jhoblitt Exp $
 
 package Nebulous::Util;
@@ -14,4 +14,5 @@
 use Log::Log4perl qw( :levels );
 use URI;
+use URI::file;
 
 my @symbols = qw(
@@ -71,6 +72,7 @@
     my ( $uri , $flags ) = @_;
 
-    my $path = _get_file_path( $uri );
-    my $fh = _get_filehandle( $path, $flags );
+    $uri = URI->new("$uri");
+
+    my $fh = _get_filehandle($uri->file, $flags);
 
     return $fh;
