Changeset 527

Show
Ignore:
Timestamp:
05/02/07 20:46:56 (2 years ago)
Author:
hachi
Message:

Now that we have working constants, we should use them. Also making this module stand-alone (doesn't inheret from the original class) because we do things just a little differently over here.

Files:

Legend:

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

    r304 r527  
    44use strict; 
    55use warnings; 
     6# We don't want to inherit from this, because our constants may be different. 
     7# use base 'Cache::Memcached::GetParser'; 
    68use Carp; 
    7  
    8 require Exporter; 
     9use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN ); 
    910use AutoLoader; 
    1011 
    11 our @ISA = qw(Exporter Cache::Memcached::GetParser)
     12our $VERSION = '0.01'
    1213 
    13 # Items to export into callers namespace by default. Note: do not export 
    14 # names by default without a very good reason. Use EXPORT_OK instead. 
    15 # Do not simply export all your public functions/methods/constants. 
     14require XSLoader; 
     15XSLoader::load('Cache::Memcached::GetParserXS', $VERSION); 
    1616 
    17 # This allows declaration       use Cache::Memcached::GetParserXS ':all'; 
    18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK 
    19 # will save memory. 
    20 our %EXPORT_TAGS = ( 'all' => [ qw( 
     17sub DEST; 
     18sub NSLEN; 
     19sub ON_ITEM; 
     20sub BUF; 
     21sub STATE; 
     22sub OFFSET; 
     23sub FLAGS; 
     24sub KEY; 
     25sub FINISHED; 
    2126 
    22 ) ] ); 
     27sub new { 
     28    my ($class, $dest, $nslen, $on_item) = @_; 
    2329 
    24 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); 
     30    my $self = bless [], (ref $class || $class); 
    2531 
    26 our @EXPORT = qw( 
     32    $self->[DEST]     = $dest; 
     33    $self->[NSLEN]    = $nslen; 
     34    $self->[ON_ITEM]  = $on_item; 
     35    $self->[BUF]      = ''; 
     36    $self->[STATE]    = 0; 
     37    $self->[OFFSET]   = 0; 
     38    $self->[FLAGS]    = undef; 
     39    $self->[KEY]      = undef; 
     40    $self->[FINISHED] = {}; 
    2741 
    28 ); 
     42    return $self 
     43
    2944 
    30 our $VERSION = '0.01'; 
     45sub current_key { 
     46    return $_[0][KEY]; 
     47
     48 
     49# returns 1 on success, -1 on failure, and 0 if still working. 
     50sub parse_from_sock { 
     51    my ($self, $sock) = @_; 
     52    my $res; 
     53 
     54    # where are we reading into? 
     55    if ($self->[STATE]) { # reading value into $ret 
     56        my $ret = $self->[DEST]; 
     57        $res = sysread($sock, $ret->{$self->[KEY]}, 
     58                       $self->[STATE] - $self->[OFFSET], 
     59                       $self->[OFFSET]); 
     60 
     61        return 0 
     62            if !defined($res) and $!==EWOULDBLOCK; 
     63 
     64        if ($res == 0) { # catches 0=conn closed or undef=error 
     65            $self->[ON_ITEM] = undef; 
     66            return -1; 
     67        } 
     68 
     69        $self->[OFFSET] += $res; 
     70        if ($self->[OFFSET] == $self->[STATE]) { # finished reading 
     71            $self->[OFFSET] = 0; 
     72            $self->[STATE]  = 0; 
     73            # wait for another VALUE line or END... 
     74        } 
     75        return 0; # still working, haven't got to end yet 
     76    } 
     77 
     78    # we're reading a single line. 
     79    # first, read whatever's there, but be satisfied with 2048 bytes 
     80    $res = sysread($sock, $self->[BUF], 
     81                   128*1024, $self->[OFFSET]); 
     82    return 0 
     83        if !defined($res) and $!==EWOULDBLOCK; 
     84    if ($res == 0) { 
     85        $self->[ON_ITEM] = undef; 
     86        return -1; 
     87    } 
     88 
     89    $self->[OFFSET] += $res; 
     90 
     91    my $answer = $self->parse_buffer; 
     92 
     93    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        } 
     100    } 
     101 
     102    return $answer; 
     103
     104 
     105sub DESTROY {} # Empty definition, so AUTOLOAD doesn't catch it 
     106 
     107# sub parse_buffer is defined in XS 
    31108 
    32109sub AUTOLOAD { 
     
    52129    goto &$AUTOLOAD; 
    53130} 
    54  
    55 require XSLoader; 
    56 XSLoader::load('Cache::Memcached::GetParserXS', $VERSION); 
    57  
    58 # Preloaded methods go here. 
    59  
    60 # Autoload methods go after =cut, and are processed by the autosplit program. 
    61131 
    621321;