this repo has no description

addition of 'safe' and 'use_oid' flags

Changed files
+42 -14
lib
CHI
Driver
+42 -14
lib/CHI/Driver/MongoDB.pm
···
extends 'CHI::Driver';
-
has 'conn' => ( is => 'ro', isa => 'MongoDB::Connection' );
-
has 'db' => ( is => 'ro', isa => 'MongoDB::Database' );
-
has 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' );
+
has 'conn' => ( is => 'ro', isa => 'MongoDB::Connection' );
+
has 'db' => ( is => 'ro', isa => 'MongoDB::Database' );
+
has 'collection' => ( is => 'ro', isa => 'MongoDB::Collection' );
+
has 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' );
+
has 'safe' => ( is => 'rw', isa => 'Bool', default => 0 );
+
has 'use_oid' => ( is => 'rw', isa => 'Bool', default => 0 );
__PACKAGE__->meta->make_immutable();
sub BUILD {
my ( $self, $args ) = @_;
-
-
return if $self->{db};
if ( $self->{conn} && $self->{db_name} ) {
$self->{db} = $self->{conn}->get_database( $self->{db_name} );
}
-
else {
+
elsif ( !$self->{db} ) {
croak 'No Database Set';
}
+
$self->{collection} = $self->db->get_collection( $self->namespace() );
+
return;
}
···
sub fetch {
my ( $self, $key ) = @_;
-
my $results =
-
$self->_collection->find_one( { _id => $key }, { data => 1 } );
+
$key =
+
( $self->{use_oid} )
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
+
: $key;
+
+
my $results = $self->collection->find_one( { _id => $key }, { data => 1 } );
return ($results) ? $results->{data} : undef;
}
sub store {
my ( $self, $key, $data ) = @_;
-
$self->_collection->save( { _id => $key, data => $data }, { safe => 1 } );
+
$key =
+
( $self->{use_oid} )
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
+
: $key;
+
+
$self->collection->save( { _id => $key, data => $data },
+
{ safe => $self->{safe} } );
return;
}
sub remove {
my ( $self, $key ) = @_;
-
$self->_collection->remove( { _id => $key }, { safe => 1 } );
+
$key =
+
( $self->{use_oid} )
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
+
: $key;
+
+
$self->collection->remove( { _id => $key }, { safe => $self->{safe} } );
return;
}
sub clear {
-
shift->_collection->drop;
+
shift->collection->drop;
return;
}
sub get_keys {
-
return map { $_->{_id} } shift->_collection->find( {}, { _id => 1 } )->all;
+
return ( !shift->{use_oid} )
+
? map { $_->{_id} } shift->collection->find( {}, { _id => 1 } )->all
+
: croak 'Cannot get keys when converting keys to OID';
}
sub get_namespaces {
-
return shift->db->collection_names( { safe => 1 } );
+
return shift->db->collection_names();
}
1;
···
=item db_name
-
Option database name to use in conjunction with the conn
+
Optional database name to use in conjunction with the conn
+
+
=item safe
+
+
Optional flag to confirm insertion/removal. This will slow down writes significantly.
+
+
=item use_oid
+
+
Optional flag to convert key to OID. This speeds up gets, slows retrieval, and removes the ability to get_keys.
=back