Skip to content
This repository has been archived by the owner on Feb 20, 2018. It is now read-only.

Commit

Permalink
preliminary add/delete code
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Nov 17, 2009
1 parent fef3214 commit f928a2a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
72 changes: 70 additions & 2 deletions lib/AnyEvent/Memcached/Protocol/Binary.pm
Expand Up @@ -215,7 +215,6 @@ sub _decode_header {
my $magic = $i1 >> 24;
my $opcode = ($i1 & 0x00ff0000) >> 16;
my $key_length = $i1 & 0x0000ffff;
warn "key length => $key_length";
my $extra_length = ($i2 & 0xff000000) >> 24;
my $data_type = ($i2 & 0x00ff0000) >> 8;
my $status = $i2 & 0x0000ffff;
Expand Down Expand Up @@ -248,6 +247,74 @@ sub _status_str {
return $strings{$status};
}

{
my $generator = sub {
my ($self, $cmd) = @_;
sub {
my ($key, $value, $exptime, $noreply, $cb) = @_;

my $handle = $self->get_handle_for( $key );
my $memcached = $self->memcached;

my ($write_data, $write_len, $flags, $expires) =
$self->prepare_value( $cmd, $value, $exptime );

my $extras = pack('N2', $flags, $expires);

$handle->push_write(
_encode_message(MEMD_ADD, $key, $extras, undef, undef,
# allow this to be set from outside
undef, # $cas
$write_data
)
);
$handle->push_read(chunk => HEADER_SIZE, sub {
my ($handle, $header) = @_;
my ($magic, $opcode, $key_length, $extra_length, $status, $data_type, $total_body_length, $opaque, $cas) = _decode_header($header);

if ($magic != RES_MAGIC) {
$cb->(undef, "Response magic is not of expected value");
$self->memcached->drain_queue;
return;
}

$cb->($status);
$self->memcached->drain_queue;
});
}
};

sub _build_add_cb {
my $self = shift;
return $generator->($self, "add");
}
}

sub _build_delete_cb {
my $self = shift;

return sub {
my ($key, $noreply, $cb) = @_;

my $handle = $self->get_handle_for($key);

$handle->push_write( _encode_message(MEMD_DELETE, $key) );
$handle->push_read(chunk => HEADER_SIZE, sub {
my ($handle, $header) = @_;
my ($magic, $opcode, $key_length, $extra_length, $status, $data_type, $total_body_length, $opaque, $cas) = _decode_header($header);

if ($magic != RES_MAGIC) {
$cb->(undef, "Response magic is not of expected value");
$self->memcached->drain_queue;
return;
}

$cb->($status);
$self->memcached->drain_queue;
});
}
}

sub _build_get_multi_cb {
my $self = shift;

Expand Down Expand Up @@ -290,7 +357,7 @@ sub _build_get_multi_cb {
}

if ($status != 0) {
$cb->(undef, "Error status");
$cb->(undef, _status_str($status));
$cv->end;
return;
}
Expand All @@ -311,6 +378,7 @@ sub _build_get_multi_cb {

$cv->end;
});

$handle->push_write( _noop() );
}
}
Expand Down
6 changes: 3 additions & 3 deletions t/101_commands_binary.t
Expand Up @@ -3,7 +3,7 @@ use lib "t/lib";
use AnyEvent::Memcached::Test;

my $memd = test_client() or exit;
plan tests => 220;
# plan tests => 220;

# count should be >= 4.
use constant count => 100;
Expand All @@ -16,8 +16,8 @@ $memd->meta->add_before_method_modifier($_ => sub { $cv->begin })
my $key = 'commands';
my @keys = map { "commands-$_" } (1..count);

#$memd->delete($key, sub { $cv->end });
#ok($memd->add($key, 'v1', sub { $cv->end }), 'Add');
$memd->delete($key, sub { $cv->end });
$memd->add($key, 'v1', sub { is($_[0], 0, 'Add'); $cv->end });

$memd->get($key, sub { is( $_[0], 'v1', 'Fetch'); $cv->end } );

Expand Down

0 comments on commit f928a2a

Please sign in to comment.