···
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 );
__PACKAGE__->meta->make_immutable();
my ( $self, $args ) = @_;
22
-
return if $self->{db};
if ( $self->{conn} && $self->{db_name} ) {
$self->{db} = $self->{conn}->get_database( $self->{db_name} );
28
+
elsif ( !$self->{db} ) {
32
+
$self->{collection} = $self->db->get_collection( $self->namespace() );
···
42
-
$self->_collection->find_one( { _id => $key }, { data => 1 } );
45
+
( $self->{use_oid} )
46
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
49
+
my $results = $self->collection->find_one( { _id => $key }, { data => 1 } );
return ($results) ? $results->{data} : undef;
my ( $self, $key, $data ) = @_;
48
-
$self->_collection->save( { _id => $key, data => $data }, { safe => 1 } );
56
+
( $self->{use_oid} )
57
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
60
+
$self->collection->save( { _id => $key, data => $data },
61
+
{ safe => $self->{safe} } );
54
-
$self->_collection->remove( { _id => $key }, { safe => 1 } );
68
+
( $self->{use_oid} )
69
+
? MongoDB::OID->new( value => unpack( "H*", $key ) )
72
+
$self->collection->remove( { _id => $key }, { safe => $self->{safe} } );
59
-
shift->_collection->drop;
77
+
shift->collection->drop;
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';
68
-
return shift->db->collection_names( { safe => 1 } );
88
+
return shift->db->collection_names();
···
123
-
Option database name to use in conjunction with the conn
143
+
Optional database name to use in conjunction with the conn
147
+
Optional flag to confirm insertion/removal. This will slow down writes significantly.
151
+
Optional flag to convert key to OID. This speeds up gets, slows retrieval, and removes the ability to get_keys.