Show
Ignore:
Timestamp:
05/02/07 21:57:25 (2 years ago)
Author:
bradfitz
Message:

cleanup, cleanup, cleanup, cleanup.

and some tests.

and use the new interface in Cache::Memcached 1.21 to do less closure
calls, and pass in instead a whole map of {$key => $flags, ...} to
finalize subref.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/client-xs-20070328/Cache-Memcached-GetParserXS

    • Property svn:ignore set to
      const-xs.inc
      const-c.inc
      GetParserXS.c
      *.diff
      notes.txt
      *blib
      *.bs
      Makefile
      Makefile.old


  • branches/client-xs-20070328/Cache-Memcached-GetParserXS/GetParserXS.xs

    r528 r529  
    262262} 
    263263 
    264 int parse_from_sock_xx (SV* selfref, SV* sock, int sockfd) { 
    265   int res; 
    266   AV* self = (AV*) SvRV(selfref); 
    267   HV* ret = get_dest(self); 
    268   IV state = get_state(self); 
    269  
    270   if (state) { 
    271     // We're reading into a key, get the SV for the key 
    272     SV* key = get_key_sv(self); 
    273     SV* offset = get_offset_sv(self); 
    274     STRLEN keylen; 
    275     char *keyptr = SvPV(key, keylen); 
    276  
    277     STRLEN buflen; 
    278     char* buf; 
    279  
    280     // Then we try to get the value 
    281     SV** valptr = hv_fetch(ret, keyptr, keylen, 0); 
    282     if (valptr) { 
    283       // Got a real value, must be an SV 
    284       buf = SvPV(*valptr, buflen); 
    285     } else { 
    286       // If we get null back, it's because the entry didn't exist, vivify it. 
    287       New(0, buf, state, char); 
    288       *valptr = newSVpvn(buf, state); 
    289     } 
    290  
    291     if (buflen < SvIV(offset) + state) { 
    292       // Buffer is too short, Renew it to the proper length 
    293       Renew(buf, SvIV(offset) + state, char); 
    294     } 
    295  
    296     // Finally, read the data into the buffer 
    297     res = read(sockfd, (buf + SvIV(offset)), (state - SvIV(offset))); 
    298  
    299     if (res < 0 && errno == EWOULDBLOCK) 
    300       return 0; 
    301  
    302     if (res == 0) { 
    303       clear_on_item(self); 
    304       return -1; 
    305     } 
    306  
    307     sv_setiv(offset, (SvIV(offset) + res)); 
    308  
    309     if (SvIV(offset) == state) { 
    310        
    311     } 
    312   } 
    313  
    314   printf("fileno = %d\n", sockfd); 
    315  
    316   printf("got = %x, state = %d\n", ret, state); 
    317   return -1; 
    318 } 
    319  
    320   /* 
    321     # where are we reading into? 
    322     if ($self->[STATE]) { # reading value into $ret 
    323         $res = sysread($sock, $ret->{$self->[KEY]}, 
    324                        $self->[STATE] - $self->[OFFSET], 
    325                        $self->[OFFSET]); 
    326  
    327         return 0 
    328             if !defined($res) and $!==EWOULDBLOCK; 
    329  
    330         if ($res == 0) { # catches 0=conn closed or undef=error 
    331             $self->[ON_ITEM] = undef; 
    332             return -1; 
    333         } 
    334  
    335         $self->[OFFSET] += $res; 
    336         if ($self->[OFFSET] == $self->[STATE]) { # finished reading 
    337             $self->[ON_ITEM]->($self->[KEY], $self->[FLAGS]); 
    338             $self->[OFFSET] = 0; 
    339             $self->[STATE]  = 0; 
    340             # wait for another VALUE line or END... 
    341         } 
    342         return 0; # still working, haven't got to end yet 
    343     } 
    344  
    345     # we're reading a single line. 
    346     # first, read whatever's there, but be satisfied with 2048 bytes 
    347     $res = sysread($sock, $self->[BUF], 
    348                    2048, $self->[OFFSET]); 
    349     return 0 
    350         if !defined($res) and $!==EWOULDBLOCK; 
    351     if ($res == 0) { 
    352         $self->[ON_ITEM] = undef; 
    353         return -1; 
    354     } 
    355  
    356     $self->[OFFSET] += $res; 
    357  
    358   */ 
    359  
    360264MODULE = Cache::Memcached::GetParserXS      PACKAGE = Cache::Memcached::GetParserXS 
    361265 
    362266INCLUDE: const-xs.inc 
    363  
    364 int 
    365 parse_from_sock_xx ( self, sock, sockfd ) 
    366     SV *self 
    367     SV *sock 
    368     int sockfd 
    369267 
    370268int 
  • branches/client-xs-20070328/Cache-Memcached-GetParserXS/Makefile.PL

    r526 r529  
    55WriteMakefile( 
    66              OPTIMIZE => '-g', 
    7     NAME              => 'Cache::Memcached::GetParserXS', 
    8     VERSION_FROM      => 'lib/Cache/Memcached/GetParserXS.pm', # finds $VERSION 
    9     PREREQ_PM         => {}, # e.g., Module::Name => 1.1 
    10     ($] >= 5.005 ?     ## Add these new keywords supported since 5.005 
    11       (ABSTRACT_FROM  => 'lib/Cache/Memcached/GetParserXS.pm', # retrieve abstract from module 
    12        AUTHOR         => 'LiveJournal user <lj@>') : ()), 
    13     LIBS              => [''], # e.g., '-lm' 
    14     DEFINE            => '', # e.g., '-DHAVE_SOMETHING' 
    15     INC               => '-I.', # e.g., '-I. -I/usr/include/other' 
    16         # Un-comment this if you add C files to link with later: 
    17     # OBJECT            => '$(O_FILES)', # link all the C files too 
    18 ); 
     7              NAME              => 'Cache::Memcached::GetParserXS', 
     8              VERSION_FROM      => 'lib/Cache/Memcached/GetParserXS.pm', # finds $VERSION 
     9              PREREQ_PM         => { 
     10                  'Cache::Memcached' => 1.21, 
     11              }, # e.g., Module::Name => 1.1 
     12              ABSTRACT_FROM  => 'lib/Cache/Memcached/GetParserXS.pm', # retrieve abstract from module 
     13              AUTHOR         => 'LiveJournal user <lj@>', 
     14              LIBS              => [''], # e.g., '-lm' 
     15              DEFINE            => '', # e.g., '-DHAVE_SOMETHING' 
     16              INC               => '-I.', # e.g., '-I. -I/usr/include/other' 
     17              # Un-comment this if you add C files to link with later: 
     18              # OBJECT            => '$(O_FILES)', # link all the C files too 
     19              ); 
     20 
    1921if  (eval {require ExtUtils::Constant; 1}) { 
    2022  # If you edit these definitions to change the constants used by this module, 
  • branches/client-xs-20070328/Cache-Memcached-GetParserXS/lib/Cache/Memcached/GetParserXS.pm

    r528 r529  
    99use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN ); 
    1010use AutoLoader; 
     11use Cache::Memcached 1.21; 
    1112 
    1213our $VERSION = '0.01'; 
     
    4546sub current_key { 
    4647    return $_[0][KEY]; 
     48} 
     49 
     50sub t_parse_buf { 
     51    my ($self, $buf) = @_; 
     52    # force buf into \r\n format 
     53    $buf =~ s/\n/\r\n/g; 
     54    $buf =~ s/\r\r/\r/g; 
     55 
     56    $self->[BUF] .= $buf; 
     57    $self->[OFFSET] += length $buf; 
     58    my $rv = $self->parse_buffer; 
     59    if ($rv > 0) { 
     60        $self->[ON_ITEM]->($self->[FINISHED]); 
     61        $self->[ON_ITEM] = undef; 
     62    } 
     63    return $rv; 
    4764} 
    4865 
     
    92109 
    93110    if ($answer > 0) { 
    94         my $finished = $self->[FINISHED]; 
    95         my $finalize = $self->[ON_ITEM]; 
    96  
    97         while (my ($key, $flags) = each %$finished) { 
    98             $finalize->($key, $flags); 
    99         } 
     111        $self->[ON_ITEM]->($self->[FINISHED]); 
    100112        $self->[ON_ITEM] = undef; 
    101113    } 
  • branches/client-xs-20070328/Cache-Memcached-GetParserXS/t/Cache-Memcached-GetParserXS.t

    r524 r529  
    1 use Test::More tests => 2; 
     1#!/usr/bi/perl 
     2 
     3use Test::More tests => 6; 
    24BEGIN { use_ok('Cache::Memcached::GetParserXS') }; 
     5use Data::Dumper; 
    36 
    4 my $parser = Cache::Memcached::GetParserXS->new({}, 0, sub {}); 
    5 ok($parser, "Parser object was created"); 
     7my $fin; 
     8my $p = new_parser(); 
     9ok($p, "Parser object was created"); 
     10 
     11# simple case 
     12$p->t_parse_buf("VALUE foo 0 3 
     13bar 
     14END 
     15"); 
     16is_deeply($fin, { foo => 0 }, "got foo"); 
     17 
     18# in chunks... 
     19$p = new_parser(); 
     20$p->t_parse_buf("VALUE foo 0 3 
     21bar 
     22VALUE bar 1 3 
     23baz 
     24"); 
     25is($fin, undef, "nothing yet"); 
     26$p->t_parse_buf("END"); 
     27is($fin, undef, "nothing yet"); 
     28$p->t_parse_buf("\n"); 
     29is_deeply($fin, { foo => 0, bar => 1 }, "got 'em"); 
     30 
     31 
     32sub new_parser { 
     33    $fin = undef; 
     34    Cache::Memcached::GetParserXS->new({}, 0, sub { $fin = $_[0] }); 
     35