| 1 | # This file was part of Redis, licensed under: | ||||||||||
| 2 | # | ||||||||||
| 3 | # The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 4 | # | ||||||||||
| 5 | # Copyright (c) 2015 by Pedro Melo, Damien Krotkine. | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | package Tie::Redis::Candy::Array; | ||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: tie Perl arrays to Redis lists - the candy way | ||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Carp qw(croak confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use CBOR::XS qw(encode_cbor decode_cbor); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use base 'Tie::Array'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 2 | my $undef = encode_cbor(undef); | |||||||||
| 18 | |||||||||||
| 19 | 2 | our $VERSION = '1.001'; # VERSION | |||||||||
| 20 | |||||||||||
| 21 | 2 | ▶ | sub TIEARRAY { | ||||||||
|
Subroutine
TIEARRAY: called CC=2 SCAR=7.0
| |||||||||||
| 22 | 2 | my ( $class, $redis, $listname ) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | croak "not a Redis instance: $redis" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | unless ref($redis) =~ m{^(?:Test::Mock::)?Redis$}; | ||||||||||
| 26 | |||||||||||
| 27 | 2 | my $self = { | |||||||||
| 28 | list => $listname, | ||||||||||
| 29 | redis => $redis, | ||||||||||
| 30 | }; | ||||||||||
| 31 | |||||||||||
| 32 | 2 | return bless( $self, $class ); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 51 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||
| 36 | 51 | my ( $self, $index ) = @_; | |||||||||
| 37 | 51 | my $data = $self->{redis}->lindex( $self->{list}, $index ); | |||||||||
| 38 | 51 | ▶ | return unless defined $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 50 | decode_cbor($data); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 67 | ▶ | sub FETCHSIZE { | ||||||||
|
Subroutine
FETCHSIZE: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 67 | my ($self) = @_; | |||||||||
| 44 | 67 | $self->{redis}->llen( $self->{list} ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 2 | my ( $self, $index, $value ) = @_; | |||||||||
| 49 | 2 | $self->{redis}->lset( $self->{list}, $index, encode_cbor($value) ); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub STORESIZE { | ||||||||
|
Subroutine
STORESIZE: not called CC=1 SCAR=6.9
| |||||||||||
| 53 | 0 | my ( $self, $count ) = @_; | |||||||||
| 54 | 0 | $self->{redis}->ltrim( $self->{list}, 0, $count ); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | my ($self) = @_; | |||||||||
| 59 | 1 | $self->{redis}->del( $self->{list} ); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 5 | ▶ | sub PUSH { | ||||||||
|
Subroutine
PUSH: called CC=2 SCAR=6.9
| |||||||||||
| 63 | 5 | my ( $self, @values ) = @_; | |||||||||
| 64 | 5 | $self->{redis}->rpush( $self->{list}, encode_cbor($_) ) for @values; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | ▶ | sub POP { | ||||||||
|
Subroutine
POP: called CC=2 SCAR=7.1
| |||||||||||
| 68 | 1 | my ($self) = @_; | |||||||||
| 69 | 1 | ▶ | map { defined($_) ? decode_cbor($_) : undef } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | $self->{redis}->rpop( $self->{list} ); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 1 | ▶ | sub SHIFT { | ||||||||
|
Subroutine
SHIFT: called CC=2 SCAR=7.1
| |||||||||||
| 74 | 1 | my ($self) = @_; | |||||||||
| 75 | 1 | ▶ | map { defined($_) ? decode_cbor($_) : undef } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 1 | $self->{redis}->lpop( $self->{list} ); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | ▶ | sub UNSHIFT { | ||||||||
|
Subroutine
UNSHIFT: called CC=2 SCAR=6.9
| |||||||||||
| 80 | 1 | my ( $self, @values ) = @_; | |||||||||
| 81 | 1 | $self->{redis}->lpush( $self->{list}, encode_cbor($_) ) for reverse @values; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub SPLICE { | ||||||||
|
Subroutine
SPLICE: not called CC=1 SCAR=6.9
| |||||||||||
| 85 | 0 | confess 'UNIMPLEMENTED'; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub EXTEND { | ||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 2 | 1; | |||||||||
| 95 | |||||||||||
| 96 | __END__ | ||||||||||