Index: /branches/neb_distrib_20081210/Nebulous-Server/Changes
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/Changes	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/Changes	(revision 23854)
@@ -14,4 +14,6 @@
     - create a pseduo directory structure on key creation
     - Nebulous::Key parsing and testing improvements
+    - change 'log_level' param to 'trace'
+    - refactor ->find_objects() functionality
       
 0.16
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server.pm	(revision 23854)
@@ -19,5 +19,5 @@
 use File::Path;
 use File::Spec;
-use Log::Log4perl;
+use Log::Log4perl qw( :levels );
 use Nebulous::Key qw( parse_neb_key parse_neb_volume );
 use Nebulous::Server::Config;
@@ -50,4 +50,5 @@
     Nebulous::Server::Log->init($config);
     my $log = Log::Log4perl::get_logger( "Nebulous::Server" );
+    $log->level($config->trace);
 
     my $sql = Nebulous::Server::SQL->new;
@@ -310,4 +311,10 @@
                 default     => undef,
             },
+            # return dir_id instead of parent_id
+            dir_id  => {
+                type        => BOOLEAN,
+                optional    => 1,
+                default     => undef,
+            },
         }
     );
@@ -318,4 +325,9 @@
     my $sql = $self->sql;
     my $db  = $self->db($key);
+
+    $log->debug( "entered - @_" );
+
+    # no path means '/', which has a dir_id & parent_id of 1
+    return 1 if $key->path eq '';
 
     # resolve parent directory
@@ -324,15 +336,22 @@
     # File::Spec->splitpath was causing ->splitdir to always an extra dir
     # named "" because of a trailing /
-    @dirs = File::Spec->splitdir(dirname($key->path));
+    unless ($p{dir_id}) {
+        @dirs = File::Spec->splitdir(dirname($key->path));
+    } else {
+        @dirs = File::Spec->splitdir($key->path);
+    }
     # dirname returns "." if there is no dir component to the path, we have
     # to filter this out
     @dirs = grep(!/^\.$/, @dirs);
 
+    $log->debug("looking for dirs - ", join(" : ", @dirs), "\n");
+
     # start at the root dir; '/' == 1
     my $parent_id = 1;
+    my $dir_id;
 TRANS: while (1) {
         eval {
             foreach my $dir (@dirs) {
-                my $dir_id;
+                $dir_id = undef;
                 {
                     my $query = $db->prepare_cached($sql->get_directory); 
@@ -340,4 +359,5 @@
                     if ($query->rows) {
                         $dir_id = $query->fetchrow_hashref->{'dir_id'};
+                        $log->debug("resolved $dir to dir_id: $dir_id");
                     }
                     $query->finish;
@@ -392,5 +412,7 @@
     }
 
-    return $parent_id;
+    $log->debug("leaving");
+
+    return $p{dir_id} ? $dir_id : $parent_id;
 }
 
@@ -1230,14 +1252,20 @@
     my $db  = $self->_db_for_index($index);
 
+    $log->debug( "entered - @_" );
+
     # first check to see if the key is an exact match
+    my @keys;
     eval {
+        $log->debug("trying for an exact key match with $key");
         my $query = $db->prepare_cached( $sql->find_object_by_ext_id );
-        $query->execute( $key );
+        $query->execute( $key->path );
         if ($query->rows) {
-            return [$query->fetchrow_hashref->{'ext_id'}];
-        }
-
-        my $parent_id = $self->_resolve_dir_parent_id(key => $key);
-
+            my $ext_id = $query->fetchrow_hashref->{'ext_id'};
+            $log->debug( "pattern has an exact match" );
+            push @keys, $ext_id;
+        } else {
+            $log->debug("no exact match for key");
+        }
+        $query->finish;
     };
     if ($@) {
@@ -1246,17 +1274,31 @@
     }
 
+    if (scalar @keys) {
+        # it was an exact match, so stop here
+        $log->debug("leaving");
+
+        return \@keys;
+    }
+
     # else, assume it's a directory
-
-    my @keys;
+    my $dir_id = $self->_resolve_dir_parent_id(key => $key, dir_id => 1);
+    unless (defined $dir_id) {
+        $log->logdie("pattern $key does not match any key or directory");
+    }
+
     eval {
-        my $query = $db->prepare_cached( $sql->find_object_by_ext_id );
-        $query->execute( $key );
+        $log->debug("trying for a directory match under dir: $dir_id");
+        my $query = $db->prepare_cached( $sql->find_object_by_dir_id );
+        $query->execute( $dir_id );
 
         while ( my $row = $query->fetchrow_hashref ) {
             my $key = $row->{ 'ext_id' };
             push @keys, $key if $key;
+            $log->debug( "matched $key" ) if $key;
         }
     };
     $log->logdie("database error: $@") if $@;
+
+    $log->debug( "leaving" );
 
     return \@keys;
@@ -1735,5 +1777,5 @@
         my $mode = [_retry(sub { stat($storage_path) } )]->[2] & 07777;
         unless ($mode == 0775) {
-            $log->error("$storage_path has the wrong permissions of: %04o", $mode);
+            $log->error("$storage_path has the wrong permissions of: %04x", $mode);
             _retry(sub { chmod(0775, $storage_path) }) or die "can't chmod $storage_path: $!";
         }
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/Config.pm	(revision 23854)
@@ -36,8 +36,8 @@
 
 my $new_validate = {
-    log_level   => {
+    trace       => {
         type        => SCALAR,
         optional    => 1,
-        default     => 'all',
+        default     => 'fatal',
         callbacks   => {
             'is valid level' => sub {
@@ -61,5 +61,5 @@
 
     # normalize log levels to lower-case
-    my $self ={ log_level => lc $p{ log_level } };
+    my $self = { trace => $LEVELS{lc($p{trace})} };
 
     bless $self, $class || ref $class;
Index: /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 23854)
@@ -314,4 +314,9 @@
         WHERE ext_id = ?
     },
+    find_object_by_dir_id => qq{
+        SELECT ext_id, ext_id_basename
+        FROM storage_object
+        WHERE dir_id = ?
+    },
     rename_object => qq{
         UPDATE storage_object
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/02_server_setup.t	(revision 23854)
@@ -23,5 +23,5 @@
 # ->new()
 {
-    my $neb = Nebulous::Server->new( log_level => 'off' );
+    my $neb = Nebulous::Server->new( trace => 'off' );
 
     ok($neb, "set log level");
Index: /branches/neb_distrib_20081210/Nebulous-Server/t/12_server_find_objects.t
===================================================================
--- /branches/neb_distrib_20081210/Nebulous-Server/t/12_server_find_objects.t	(revision 23853)
+++ /branches/neb_distrib_20081210/Nebulous-Server/t/12_server_find_objects.t	(revision 23854)
@@ -8,5 +8,5 @@
 use warnings FATAL => qw( all );
 
-use Test::More tests => 6;
+use Test::More tests => 15;
 
 use lib qw( ./t ./lib );
@@ -25,6 +25,5 @@
 # search for a regex of '' should match nothing
 eval {
-    # key
-    my $uri = $neb->create_object("foo");
+    $neb->create_object("foo");
 
     my $keys = $neb->find_objects();
@@ -35,6 +34,5 @@
 
 {
-    # key
-    my $uri = $neb->create_object("foo");
+    $neb->create_object("foo");
 
     my $keys = $neb->find_objects("foo");
@@ -48,6 +46,6 @@
 {
     # key
-    my $uri1 = $neb->create_object("foo");
-    my $uri2 = $neb->replicate_object("foo");
+    $neb->create_object("foo");
+    $neb->replicate_object("foo");
 
     my $keys = $neb->find_objects("foo");
@@ -55,4 +53,53 @@
     is(scalar @$keys, 1, 'number of keys found');
     is($keys->[0], "foo", "key name");
+}
+
+# test recursive dir searching
+Test::Nebulous->setup;
+
+{
+    $neb->create_object("a/foo");
+
+    my $keys = $neb->find_objects("a");
+
+    is(scalar @$keys, 1, 'number of keys found');
+    is($keys->[0], "a/foo", "key name");
+}
+
+Test::Nebulous->setup;
+
+{
+    $neb->create_object("a/foo");
+    $neb->create_object("a/bar");
+
+    my $keys = $neb->find_objects("a");
+
+    is(scalar @$keys, 2, 'number of keys found');
+    is($keys->[0], "a/foo", "key name");
+    is($keys->[1], "a/bar", "key name");
+}
+
+Test::Nebulous->setup;
+
+{
+    $neb->create_object("a/foo");
+    $neb->create_object("bar");
+
+    my $keys = $neb->find_objects("a");
+
+    is(scalar @$keys, 1, 'number of keys found');
+    is($keys->[0], "a/foo", "key name");
+}
+
+Test::Nebulous->setup;
+
+{
+    $neb->create_object("a/foo");
+    $neb->create_object("bar");
+
+    my $keys = $neb->find_objects("..");
+
+    is(scalar @$keys, 1, 'number of keys found');
+    is($keys->[0], "bar", "key name");
 }
 
