| 1 | package Voldemort::Store; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 0 | ▶ | use Voldemort::ProtoBuff::Connection; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Voldemort::Store | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The primary interface for interacting with a Voldemort store, synchronously or asynchromously. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | =head1 PROPERTIES | ||
| 22 | |||
| 23 | =head2 connection | ||
| 24 | |||
| 25 | Moose Voldemort::Connection property, required on instanciation. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | has 'connection' => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'Voldemort::Connection', | ||
| 32 | required => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | =head2 connection | ||
| 36 | |||
| 37 | Moose string property of the default store to use. If one is not provided, it must be provided on operations which manipulate data. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | has 'default_store' => ( | ||
| 42 | is => 'rw', | ||
| 43 | isa => 'Str' | ||
| 44 | ); | ||
| 45 | |||
| 46 | =head2 sync | ||
| 47 | |||
| 48 | Moose boolean property if the connection should be used synchronously or asynchronously. Changing this property mid-stride can cause confusion later getting results of operations. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | has 'sync' => ( | ||
| 53 | is => 'rw', | ||
| 54 | isa => 'Bool', | ||
| 55 | default => 1 | ||
| 56 | ); | ||
| 57 | |||
| 58 | has '_queue' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => 'ArrayRef', | ||
| 61 | default => sub { [] } | ||
| 62 | ); | ||
| 63 | |||
| 64 | sub _connect { | ||
| 65 | my $self = shift; | ||
| 66 | if ( !$self->connection()->is_connected() ) { | ||
| 67 | $self->connection()->connect(); | ||
| 68 | $self->connection()->is_connected(1); | ||
| 69 | } | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _handle_response { | ||
| 74 | my $self = shift; | ||
| 75 | my $writer = shift; | ||
| 76 | |||
| 77 | $self->connection()->flush(); | ||
| 78 | return $writer->read( $self->connection() ) if ( $self->sync() ); | ||
| 79 | unshift( @{ $self->_queue() }, $writer ); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 ready | ||
| 84 | |||
| 85 | Method which performs a check on the IO::Connection for data. Defaults to a 1 second wait if the number of seconds is not passed in. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub ready { | ||
| 90 | my $self = shift; | ||
| 91 | my $wait = shift || 1; | ||
| 92 | return $self->connection()->can_read($wait); | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 next_response | ||
| 96 | |||
| 97 | Method that retrieves the next response off the wire. In synchronous mode, this method is called for you. In asynchronous mode, you must call this yourself. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub next_response { | ||
| 102 | my $self = shift; | ||
| 103 | my $op = pop( @{ $self->_queue() } ); | ||
| 104 | my $connection = $self->connection(); | ||
| 105 | |||
| 106 | return $op->read($connection); | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 get/put/delete | ||
| 110 | |||
| 111 | Methods that perform operations on the store, named for their function. The first param of the method is a hash of the parameters. In asynchronous mode, these methods will not deserialize a result off the wire but instead queue them to be read by next_response. | ||
| 112 | |||
| 113 | =head3 store | ||
| 114 | |||
| 115 | The name of the store in voldemort to manipulate, defaults to default_store. | ||
| 116 | |||
| 117 | =head3 key | ||
| 118 | |||
| 119 | Key in which to operate. | ||
| 120 | |||
| 121 | =head3 node | ||
| 122 | |||
| 123 | For writes and deletes, which vectors are being modified. In scalar form or array ref, it is considered one writer. Passing in an array ref of [1,2] will create a single node vector of [1,2]. Then writing with a node of 1 will not modify the vector of [1,2]. | ||
| 124 | |||
| 125 | =head3 value | ||
| 126 | |||
| 127 | Key for puts, the value to store. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub delete { | ||
| 132 | my ( $self, %params ) = @_; | ||
| 133 | my $store = $params{'store'} || $self->default_store(); | ||
| 134 | |||
| 135 | $self->_connect(); | ||
| 136 | |||
| 137 | if ( $self->connection()->can_write(30) ) { | ||
| 138 | my $writer = $self->connection()->delete_handler(); | ||
| 139 | |||
| 140 | $writer->write( $self->connection(), $store, $params{'key'}, | ||
| 141 | $params{'node'} ); | ||
| 142 | return $self->_handle_response($writer); | ||
| 143 | } | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get { | ||
| 148 | my ( $self, %params ) = @_; | ||
| 149 | my $store = $params{'store'} || $self->default_store(); | ||
| 150 | |||
| 151 | $self->_connect(); | ||
| 152 | |||
| 153 | if ( $self->connection()->can_write(30) ) { | ||
| 154 | my $writer = $self->connection()->get_handler(); | ||
| 155 | |||
| 156 | $writer->write( $self->connection(), $store, $params{'key'} ); | ||
| 157 | return $self->_handle_response($writer); | ||
| 158 | } | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub put { | ||
| 163 | my ( $self, %params ) = @_; | ||
| 164 | my $store = $params{'store'} || $self->default_store(); | ||
| 165 | |||
| 166 | $self->_connect(); | ||
| 167 | |||
| 168 | if ( $self->connection()->can_write(30) ) { | ||
| 169 | my $writer = $self->connection()->put_handler(); | ||
| 170 | |||
| 171 | $writer->write( $self->connection(), $store, $params{'key'}, | ||
| 172 | $params{'value'}, $params{'node'} ); | ||
| 173 | |||
| 174 | return $self->_handle_response($writer); | ||
| 175 | } | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | __PACKAGE__->meta->make_immutable; | ||
| 180 | 1; | ||