| 1 | use v5.10; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Meerkat::Cursor; | ||
| 6 | # ABSTRACT: Wrap MongoDB::Cursor to inflate data to objects | ||
| 7 | |||
| 8 | our $VERSION = '0.016'; | ||
| 9 | |||
| 10 | # Dependencies | ||
| 11 | use Moose 2; | ||
| 12 | use Try::Tiny::Retry 0.002 qw/:all/; | ||
| 13 | |||
| 14 | #pod =attr cursor (required) | ||
| 15 | #pod | ||
| 16 | #pod A L<MongoDB::Cursor> object | ||
| 17 | #pod | ||
| 18 | #pod =cut | ||
| 19 | |||
| 20 | has cursor => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'MongoDB::Cursor', | ||
| 23 | required => 1, | ||
| 24 | handles => [ | ||
| 25 | qw( fields sort limit tailable skip snapshot hint ), | ||
| 26 | qw( explain count reset has_next next info all immortal), | ||
| 27 | ], | ||
| 28 | ); | ||
| 29 | |||
| 30 | #pod =attr collection (required) | ||
| 31 | #pod | ||
| 32 | #pod A L<Meerkat::Collection> used for inflating results. | ||
| 33 | #pod | ||
| 34 | #pod =cut | ||
| 35 | |||
| 36 | has collection => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Meerkat::Collection', | ||
| 39 | required => 1, | ||
| 40 | ); | ||
| 41 | |||
| 42 | around 'next' => sub { | ||
| 43 | my $orig = shift; | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $data = | ||
| 47 | retry { $self->$orig }, | ||
| 48 | retry_if { /not connected/ }, | ||
| 49 | delay_exp { 5, 1e6 }, | ||
| 50 | on_retry { $self->mongo_clear_caches }; | ||
| 51 | |||
| 52 | if ($data) { | ||
| 53 | return $self->collection->thaw_object($data); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | |||
| 63 | # vim: ts=4 sts=4 sw=4 et: | ||
| 64 | |||
| 65 | __END__ |