this repo has no description
fork

Configure Feed

Select the types of activity you want to include in your feed.

addition of 'safe' and 'use_oid' flags

+42 -14
+42 -14
lib/CHI/Driver/MongoDB.pm
··· 10 10 11 11 extends 'CHI::Driver'; 12 12 13 - has 'conn' => ( is => 'ro', isa => 'MongoDB::Connection' ); 14 - has 'db' => ( is => 'ro', isa => 'MongoDB::Database' ); 15 - has 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' ); 13 + has 'conn' => ( is => 'ro', isa => 'MongoDB::Connection' ); 14 + has 'db' => ( is => 'ro', isa => 'MongoDB::Database' ); 15 + has 'collection' => ( is => 'ro', isa => 'MongoDB::Collection' ); 16 + has 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' ); 17 + has 'safe' => ( is => 'rw', isa => 'Bool', default => 0 ); 18 + has 'use_oid' => ( is => 'rw', isa => 'Bool', default => 0 ); 16 19 17 20 __PACKAGE__->meta->make_immutable(); 18 21 19 22 sub BUILD { 20 23 my ( $self, $args ) = @_; 21 - 22 - return if $self->{db}; 23 24 24 25 if ( $self->{conn} && $self->{db_name} ) { 25 26 $self->{db} = $self->{conn}->get_database( $self->{db_name} ); 26 27 } 27 - else { 28 + elsif ( !$self->{db} ) { 28 29 croak 'No Database Set'; 29 30 } 30 31 32 + $self->{collection} = $self->db->get_collection( $self->namespace() ); 33 + 31 34 return; 32 35 } 33 36 ··· 38 41 39 42 sub fetch { 40 43 my ( $self, $key ) = @_; 41 - my $results = 42 - $self->_collection->find_one( { _id => $key }, { data => 1 } ); 44 + $key = 45 + ( $self->{use_oid} ) 46 + ? MongoDB::OID->new( value => unpack( "H*", $key ) ) 47 + : $key; 48 + 49 + my $results = $self->collection->find_one( { _id => $key }, { data => 1 } ); 43 50 return ($results) ? $results->{data} : undef; 44 51 } 45 52 46 53 sub store { 47 54 my ( $self, $key, $data ) = @_; 48 - $self->_collection->save( { _id => $key, data => $data }, { safe => 1 } ); 55 + $key = 56 + ( $self->{use_oid} ) 57 + ? MongoDB::OID->new( value => unpack( "H*", $key ) ) 58 + : $key; 59 + 60 + $self->collection->save( { _id => $key, data => $data }, 61 + { safe => $self->{safe} } ); 49 62 return; 50 63 } 51 64 52 65 sub remove { 53 66 my ( $self, $key ) = @_; 54 - $self->_collection->remove( { _id => $key }, { safe => 1 } ); 67 + $key = 68 + ( $self->{use_oid} ) 69 + ? MongoDB::OID->new( value => unpack( "H*", $key ) ) 70 + : $key; 71 + 72 + $self->collection->remove( { _id => $key }, { safe => $self->{safe} } ); 55 73 return; 56 74 } 57 75 58 76 sub clear { 59 - shift->_collection->drop; 77 + shift->collection->drop; 60 78 return; 61 79 } 62 80 63 81 sub get_keys { 64 - return map { $_->{_id} } shift->_collection->find( {}, { _id => 1 } )->all; 82 + return ( !shift->{use_oid} ) 83 + ? map { $_->{_id} } shift->collection->find( {}, { _id => 1 } )->all 84 + : croak 'Cannot get keys when converting keys to OID'; 65 85 } 66 86 67 87 sub get_namespaces { 68 - return shift->db->collection_names( { safe => 1 } ); 88 + return shift->db->collection_names(); 69 89 } 70 90 71 91 1; ··· 120 140 121 141 =item db_name 122 142 123 - Option database name to use in conjunction with the conn 143 + Optional database name to use in conjunction with the conn 144 + 145 + =item safe 146 + 147 + Optional flag to confirm insertion/removal. This will slow down writes significantly. 148 + 149 + =item use_oid 150 + 151 + Optional flag to convert key to OID. This speeds up gets, slows retrieval, and removes the ability to get_keys. 124 152 125 153 =back 126 154