keys, _sock_result_bulk
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 21 Mar 2009 23:23:37 +0000 (23:23 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 21 Mar 2009 23:23:37 +0000 (23:23 +0000)
git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/Redis@12 447b33ff-793d-4489-8442-9bea7d161be5

lib/Redis.pm
t/01-Redis.t

index 039fade..f0b21a2 100644 (file)
@@ -58,6 +58,18 @@ sub _sock_result {
        return $result;
 }
 
+sub _sock_result_bulk {
+       my $len = <$sock>;
+       warn "# len: ",dump($len);
+       return undef if $len eq "nil\r\n";
+       my $v;
+       read($sock, $v, $len) || die $!;
+       warn "# v: ",dump($v);
+       my $crlf;
+       read($sock, $crlf, 2); # skip cr/lf
+       return $v;
+}
+
 =head1 Connection Handling
 
 =head2 quit
@@ -108,15 +120,7 @@ sub set {
 sub get {
        my ( $self, $k ) = @_;
        print $sock "GET $k\r\n";
-       my $len = <$sock>;
-#      warn "# len: ",dump($len);
-       return undef if $len eq "nil\r\n";
-       my $v;
-       read($sock, $v, $len) || die $!;
-#      warn "# v: ",dump($v);
-       my $crlf;
-       read($sock, $crlf, 2); # skip cr/lf
-       return $v;
+       _sock_result_bulk();
 }
 
 =head2 incr
@@ -187,10 +191,24 @@ sub del {
 
 sub type {
        my ( $self, $key ) = @_;
-       print $sock "type $key\r\n";
+       print $sock "TYPE $key\r\n";
        _sock_result();
 }
 
+=head1 Commands operating on the key space
+
+=head2 keys
+
+  my @keys = $r->keys( '*glob_pattern*' );
+
+=cut
+
+sub keys {
+       my ( $self, $glob ) = @_;
+       print $sock "KEYS $glob\r\n";
+       return split(/\s/, _sock_result_bulk());
+}
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
index ff41882..7dad9f7 100755 (executable)
@@ -3,7 +3,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 44;
+use Test::More tests => 48;
 
 use lib 'lib';
 
@@ -37,10 +37,14 @@ my $key_next = 3;
 
 ok( $o->set('key-left' => $key_next), 'key-left' );
 
+my @keys;
+
 foreach ( 0 .. $key_next ) {
-       ok(     $o->set( "key-$_" => $_ ),           "set key-$_" );
-       ok(  $o->exists( "key-$_"       ),           "exists key-$_" );
-       cmp_ok( $o->get( "key-$_"       ), 'eq', $_, "get key-$_" );
+       my $key = 'key-' . $_;
+       push @keys, $key;
+       ok(     $o->set( $key => $_ ),           "set $key" );
+       ok(  $o->exists( $key       ),           "exists $key" );
+       cmp_ok( $o->get( $key       ), 'eq', $_, "get $key" );
        cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' );
        cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' );
 }
@@ -54,8 +58,12 @@ foreach ( 1 .. 3 ) {
        cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
 }
 
-ok( $o->del('key-next' ), 'del' );
+ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
+ok( ! $o->del('non-existing' ), 'del non-existing' );
 
 cmp_ok( $o->type('foo'), 'eq', 'string', 'type' );
 
+cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
+is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' );
+
 ok( $o->quit, 'quit' );