Catmandu-Store-MongoDB

 view release on metacpan or  search on metacpan

lib/Catmandu/Store/MongoDB/Bag.pm  view on Meta::CPAN

    $self->store->database->get_collection($self->name);
}

sub _options {
    my ($self, $opts) = @_;
    $opts //= {};
    $opts->{session} = $self->store->session if $self->store->has_session;
    $opts;
}

sub _cursor {
    my ($self, $filter, $opts) = @_;
    $self->collection->find($filter // {}, $self->_options($opts));
}

sub generator {
    my ($self) = @_;
    sub {
        state $cursor = do {
            my $c = $self->_cursor;
            $c->immortal(1);
            $c;
        };
        $cursor->next;
    };
}

sub to_array {
    my ($self) = @_;
    my @all = $self->_cursor->all;
    \@all;
}

sub each {
    my ($self, $sub) = @_;
    my $cursor = $self->_cursor;
    my $n      = 0;
    while (my $data = $cursor->next) {
        $sub->($data);
        $n++;
    }
    $n;
}

sub count {
    my ($self) = @_;
    if ($self->store->estimate_count) {
        return $self->collection->estimated_document_count();

lib/Catmandu/Store/MongoDB/Bag.pm  view on Meta::CPAN

# $bag->select('foo' => 'bar')
# $bag->select('foo' => /bar/)
# $bag->select('foo' => ['bar', 'baz'])
around select => sub {
    my ($orig, $self, $arg1, $arg2) = @_;
    if (is_string($arg1)) {
        if (is_value($arg2) || is_regex_ref($arg2)) {
            return Catmandu::Iterator->new(
                sub {
                    sub {
                        state $cursor = $self->_cursor({$arg1 => $arg2});
                        $cursor->next;
                    }
                }
            );
        }
        if (is_array_ref($arg2)) {
            return Catmandu::Iterator->new(
                sub {
                    sub {
                        state $cursor
                            = $self->_cursor({$arg1 => {'$in' => $arg2}});
                        $cursor->next;
                    }
                }
            );
        }
    }
    $self->$orig($arg1, $arg2);
};

# efficiently handle:
# $bag->reject('foo' => 'bar')
# $bag->reject('foo' => ['bar', 'baz'])
around reject => sub {
    my ($orig, $self, $arg1, $arg2) = @_;
    if (is_string($arg1)) {
        if (is_value($arg2)) {
            return Catmandu::Iterator->new(
                sub {
                    sub {
                        state $cursor
                            = $self->_cursor({$arg1 => {'$ne' => $arg2}});
                        $cursor->next;
                    }
                }
            );
        }
        if (is_array_ref($arg2)) {
            return Catmandu::Iterator->new(
                sub {
                    sub {
                        state $cursor
                            = $self->_cursor({$arg1 => {'$nin' => $arg2}});
                        $cursor->next;
                    }
                }
            );
        }
    }
    $self->$orig($arg1, $arg2);
};

sub pluck {
    my ($self, $key) = @_;
    Catmandu::Iterator->new(
        sub {
            sub {
                state $cursor
                    = $self->_cursor({}, {projection => {$key => 1}});
                ($cursor->next || return)->{$key};
            }
        }
    );
}

sub get {
    my ($self, $id) = @_;
    $self->collection->find_one({_id => $id}, {}, $self->_options);
}

lib/Catmandu/Store/MongoDB/Bag.pm  view on Meta::CPAN

    my $limit  = $args{limit};
    my $bag    = $args{reify};
    my $fields = $args{fields};

    # limit 0 == all in mongodb
    my $orig_limit = $limit;
    if ($orig_limit == 0) {
        $limit = 1;
    }

    my $cursor = $self->_cursor($query)->skip($start)->limit($limit);
    if ($bag) {    # only retrieve _id
        $cursor->fields({});
    }
    elsif ($fields) {    # only retrieve specified fields
        $cursor->fields($fields);
    }

    if (my $sort = $args{sort}) {
        $cursor->sort($sort);
    }

    my @hits = $cursor->all;

    if ($orig_limit == 0) {
        @hits = ();
    }

    if ($bag) {
        @hits = map {$bag->get($_->{_id})} @hits;
    }

    my $total;

lib/Catmandu/Store/MongoDB/Searcher.pm  view on Meta::CPAN

has query => (is => 'ro', required => 1);
has start => (is => 'ro', required => 1);
has limit => (is => 'ro', required => 1);
has total => (is => 'ro');
has sort  => (is => 'ro');
has fields => (is => 'ro');

sub generator {
    my ($self) = @_;
    sub {
        state $cursor = do {
            my $c = $self->bag->_cursor($self->query);
            $c->fields($self->fields) if defined $self->fields;

            # limit is unused because the perl driver doesn't expose batchSize
            $c->limit($self->total) if defined $self->total;
            $c->sort($self->sort)   if defined $self->sort;
            $c->immortal(1);
            $c;
        };
        $cursor->next;
    };
}

sub slice {    # TODO constrain total?
    my ($self, $start, $total) = @_;
    $start //= 0;
    $self->new(
        bag    => $self->bag,
        query  => $self->query,
        start  => $self->start + $start,



( run in 0.237 second using v1.01-cache-2.11-cpan-4d50c553e7e )