« lib/Mail/Decency/LogParser/Core/CacheWrite.pm
↑ Summary
lib/Mail/Decency/ContentFilter/ClamAV.pm »
| 1 | package Mail::Decency::Helper::Database::MongoDB; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'Mail::Decency::Helper::Database'; | ||
| 5 | use mro 'c3'; | ||
| 6 | |||
| 7 | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||
| 8 | |||
| 9 | use Data::Dumper; | ||
| 10 | use Tie::IxHash; | ||
| 11 | use MongoDB; | ||
| 12 | use Carp qw/ carp /; | ||
| 13 | use Time::HiRes qw/ usleep ualarm /; | ||
| 14 | |||
| 15 | has db => ( is => "ro", isa => "MongoDB::Database" ); | ||
| 16 | has host => ( is => "ro", isa => "Str", default => "127.0.0.1" ); | ||
| 17 | has port => ( is => "ro", isa => "Int", default => 27017 ); | ||
| 18 | has user => ( is => "ro", isa => "Str", predicate => 'use_auth' ); | ||
| 19 | has pass => ( is => "ro", isa => "Str" ); | ||
| 20 | has database => ( is => "ro", isa => "Str", default => "decency" ); | ||
| 21 | |||
| 22 | sub BUILD { | ||
| 23 | my ( $self ) = @_; | ||
| 24 | |||
| 25 | $self->connect; | ||
| 26 | |||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | =head2 connect | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub connect { | ||
| 36 | my ( $self ) = @_; | ||
| 37 | eval { | ||
| 38 | my %connect = ( | ||
| 39 | auto_reconnect => 1 | ||
| 40 | ); | ||
| 41 | if ( $self->host =~ /,/ ) { | ||
| 42 | my ( $left, $right ) = split( /\s*,\s*/, $self->host, 2 ); | ||
| 43 | my ( $pleft, $pright ) = split( /\s*,\s*/, $self->port, 2 ); | ||
| 44 | $pright ||= $pleft; | ||
| 45 | $connect{ host } = 'mongodb://'. join( ',', | ||
| 46 | join( ':', $left, $pleft ), | ||
| 47 | join( ':', $right, $pright ), | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | my $host = $self->host || 'localhost'; | ||
| 52 | my $port = $self->port || '27017'; | ||
| 53 | $connect{ host } = 'mongodb://'. join( ':', $host, $port ); | ||
| 54 | } | ||
| 55 | if ( $self->use_auth ) { | ||
| 56 | $connect{ username } = $self->user; | ||
| 57 | $connect{ password } = $self->pass if $self->pass; | ||
| 58 | $connect{ db_name } = $self->database; | ||
| 59 | } | ||
| 60 | $self->{ db } = MongoDB::Connection->new( %connect )->get_database( $self->database ); | ||
| 61 | }; | ||
| 62 | if ( $@ ) { | ||
| 63 | die "Connection to mongodb failed: $@\n:"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | =head2 stat_print | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub stat_print { | ||
| 73 | my ( $self ) = @_; | ||
| 74 | print "TODO\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | =head2 search | ||
| 80 | |||
| 81 | Search in database for a key | ||
| 82 | |||
| 83 | CAUTION: use with care. Always provide ALL search keys, not only one of a kind! | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub search { | ||
| 88 | my ( $self, $schema, $table, $search_ref ) = @_; | ||
| 89 | $self->read_lock; # accquire semaphore | ||
| 90 | $search_ref = $self->update_query( $search_ref ); | ||
| 91 | |||
| 92 | my @res = $self->_try_transaction( $schema => $table => find => [ $search_ref ] ); | ||
| 93 | |||
| 94 | $self->read_unlock; # release semaphore | ||
| 95 | return wantarray ? @res : \@res; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | =head2 search_read | ||
| 100 | |||
| 101 | Returns read handle and read method name for massive read actions | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub search_read { | ||
| 106 | my ( $self, $schema, $table, $search_ref ) = @_; | ||
| 107 | $search_ref = $self->update_query( $search_ref ); | ||
| 108 | my ( $handle ) = $self->_try_transaction( $schema => $table => query => [ $search_ref ] ); | ||
| 109 | return ( $handle, 'next' ); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | =head2 get | ||
| 114 | |||
| 115 | Search in database for a key | ||
| 116 | |||
| 117 | CAUTION: use with care. Always provide ALL search keys, not only one of a kind! | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub get { | ||
| 122 | my ( $self, $schema, $table, $search_ref ) = @_; | ||
| 123 | $self->read_lock; # accquire semaphore | ||
| 124 | $search_ref = $self->update_query( $search_ref ); | ||
| 125 | my ( $ref ) = $self->_try_transaction( $schema => $table => find_one => [ $search_ref ] ); | ||
| 126 | #print Dumper { q => $search_ref, res => $ref }; | ||
| 127 | $self->read_unlock; # release semaphore | ||
| 128 | return $self->parse_data( $ref ); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | =head2 set | ||
| 133 | |||
| 134 | Getter method for BerkeleyDB::* | ||
| 135 | |||
| 136 | CAUTION: use with care. Always provide ALL search keys, not only one of a kind! | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub set { | ||
| 141 | my ( $self, $schema, $table, $search_ref, $data_ref ) = @_; | ||
| 142 | $self->write_lock; # accquire semaphore | ||
| 143 | $search_ref = $self->update_query( $search_ref ); | ||
| 144 | $data_ref = $self->update_data( $data_ref ); | ||
| 145 | delete $data_ref->{ _id } if defined $data_ref->{ _id }; | ||
| 146 | |||
| 147 | my ( $res ) = $self->_try_transaction( $schema => $table => update => [ $search_ref, { %$search_ref, %$data_ref }, { upsert => 1 } ] ); | ||
| 148 | |||
| 149 | $self->write_unlock; # release semaphore | ||
| 150 | return $res; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | =head2 increment | ||
| 157 | |||
| 158 | Increment a sole value. Has to have "data" as key! | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | |||
| 163 | sub increment { | ||
| 164 | my ( $self, $schema, $table, $search_ref, $amount, $key ) = @_; | ||
| 165 | $key ||= 'data'; | ||
| 166 | $amount ||= 1; | ||
| 167 | $self->usr_lock; # accquire exclusive semaphore | ||
| 168 | |||
| 169 | my $ref = $self->get( $schema => $table => $search_ref ); | ||
| 170 | $ref ||= { $key => 0 }; | ||
| 171 | $ref->{ $key } += $amount; | ||
| 172 | $self->set( $schema => $table => $search_ref => $ref ); | ||
| 173 | |||
| 174 | $self->usr_unlock; # release exclusive semaphore | ||
| 175 | return $ref->{ $key }; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | =head2 remove | ||
| 181 | |||
| 182 | Delete an entry permenante | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub remove { | ||
| 187 | my ( $self, $schema, $table, $search_ref ) = @_; | ||
| 188 | $self->write_lock; # accquire semaphore | ||
| 189 | $search_ref = $self->update_query( $search_ref ); | ||
| 190 | my ( $res ) = $self->_try_transaction( $schema => $table => remove => [ $search_ref ] ); | ||
| 191 | $self->write_unlock; # release semaphore | ||
| 192 | return $res; | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | =head2 ping | ||
| 197 | |||
| 198 | Pings MongoDB Server, check wheter connect possible or not | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub ping { | ||
| 203 | my ( $self, $schema, $table ) = @_; | ||
| 204 | |||
| 205 | eval { | ||
| 206 | my $col = $self->db->get_collection( "${schema}_${table}" ); | ||
| 207 | }; | ||
| 208 | $self->logger->debug0( "Collection '${$schema}_${table}' not existing, yet.. no harm, should be created automatically. Response: $@" ) | ||
| 209 | if $@; | ||
| 210 | |||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | =head2 setup | ||
| 216 | |||
| 217 | Create database | ||
| 218 | |||
| 219 | setup indices | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub setup { | ||
| 224 | my ( $self, $schema, $table, $columns_ref, $execute ) = @_; | ||
| 225 | |||
| 226 | if ( $execute ) { | ||
| 227 | if ( defined $columns_ref->{ -unique } ) { | ||
| 228 | my $unique = Tie::IxHash->new( map { ( $_ => 1 ) } @{ $columns_ref->{ -unique } } ); | ||
| 229 | $self->db->get_collection( "${schema}_${table}" )->ensure_index( $unique, { unique => 1 } ); | ||
| 230 | } | ||
| 231 | |||
| 232 | if ( defined $columns_ref->{ -index } ) { | ||
| 233 | my $idx = Tie::IxHash->new( map { ( $_ => 1 ) } @{ $columns_ref->{ -index } } ); | ||
| 234 | $self->db->get_collection( "${schema}_${table}" )->ensure_index( $idx ); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | else { | ||
| 239 | print "-- MongoDB does no require create statements\n"; | ||
| 240 | } | ||
| 241 | |||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | =head2 update_query | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub update_query { | ||
| 251 | my ( $self, $ref ) = @_; | ||
| 252 | $ref = $self->next::method( $ref ); | ||
| 253 | |||
| 254 | my %op_match = ( | ||
| 255 | '>' => '$gt', | ||
| 256 | '<' => '$lt', | ||
| 257 | '>=' => '$gte', | ||
| 258 | '<=' => '$lte', | ||
| 259 | '!=' => '$ne', | ||
| 260 | ); | ||
| 261 | while( my ( $k, $v ) = each %$ref ) { | ||
| 262 | my $type = ref( $v ); | ||
| 263 | next unless $type; | ||
| 264 | if ( $type eq 'HASH' ) { | ||
| 265 | foreach my $op( keys %$v ) { | ||
| 266 | $v->{ $op_match{ $op } } = delete $v->{ $op } | ||
| 267 | if defined $op_match{ $op }; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | elsif ( $type eq 'ARRAY' ) { | ||
| 271 | $ref->{ $k } = { '$in' => delete $ref->{ $k } }; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | return $ref; | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | =head2 _try_transaction | ||
| 280 | |||
| 281 | Cause mongodb does not handle clean re-connections, this has to be implemented in code | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub _try_transaction { | ||
| 286 | my ( $self, $schema, $table, $method, $args_ref ) = @_; | ||
| 287 | |||
| 288 | my @res; | ||
| 289 | |||
| 290 | # if mongodb was restarted, this will throw an error | ||
| 291 | eval { | ||
| 292 | local $SIG{ ALRM } = sub { | ||
| 293 | #$self->logger->error( "MongoDB Connection lost, try reconnect" ); | ||
| 294 | die "Timeout\n"; | ||
| 295 | }; | ||
| 296 | ualarm( 1_000_000 ); | ||
| 297 | @res = $self->db->get_collection( "${schema}_${table}" )->$method( @$args_ref ); | ||
| 298 | alarm( 0 ); | ||
| 299 | }; | ||
| 300 | |||
| 301 | # handle disconnection event | ||
| 302 | if ( $@ && ( $@ =~ /not connected/ || $@ =~ /Timeout/ ) ) { | ||
| 303 | |||
| 304 | # try connect | ||
| 305 | eval { $self->connect; }; | ||
| 306 | |||
| 307 | # mongo db probably down: | ||
| 308 | carp "Cannot connect to MongoDB: $@" if $@; | ||
| 309 | |||
| 310 | # fetch again | ||
| 311 | @res = $self->db->get_collection( "${schema}_${table}" )->$method( @$args_ref ); | ||
| 312 | } | ||
| 313 | elsif ( $@ ) { | ||
| 314 | carp "Mongodb problem: $@"; | ||
| 315 | } | ||
| 316 | |||
| 317 | return @res; | ||
| 318 | } | ||
| 319 | |||
| 320 | =head1 AUTHOR | ||
| 321 | |||
| 322 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 323 | |||
| 324 | =head1 COPYRIGHT | ||
| 325 | |||
| 326 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 327 | |||
| 328 | =head1 LICENCSE | ||
| 329 | |||
| 330 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | 1; |