| 1 | ## Copyright (C) Graham Barr | ||
| 2 | ## vim: ts=8:sw=2:expandtab:shiftround | ||
| 3 | |||
| 4 | ## WARNING!!!! | ||
| 5 | ## WARNING!!!! This code is still very much a work in progress, | ||
| 6 | ## WARNING!!!! do not depend on anything not changing | ||
| 7 | ## WARNING!!!! | ||
| 8 | |||
| 9 | package AnyEvent::MongoDB::Request; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | |||
| 13 | has message => ( | ||
| 14 | isa => 'ScalarRef', | ||
| 15 | is => 'ro', | ||
| 16 | required => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has cb => (is => 'ro'); | ||
| 20 | has ns => (is => 'ro', required => 1, isa => 'Str'); | ||
| 21 | has max => (is => 'ro'); | ||
| 22 | has prefetch => (is => 'ro'); | ||
| 23 | has timeout => (is => 'ro'); | ||
| 24 | has limit => (is => 'ro'); | ||
| 25 | |||
| 26 | has ids => ( # Only used for op_insert | ||
| 27 | isa => 'ArrayRef', | ||
| 28 | traits => ['Array'], | ||
| 29 | lazy_build => 1, | ||
| 30 | handles => { ## | ||
| 31 | ids => 'elements', | ||
| 32 | }, | ||
| 33 | ); | ||
| 34 | sub _build_ids { [] } | ||
| 35 | |||
| 36 | has request_id => ( | ||
| 37 | is => 'ro', | ||
| 38 | writer => '_set_request_id', | ||
| 39 | ); | ||
| 40 | |||
| 41 | has at => ( | ||
| 42 | traits => ['Number'], | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Num', | ||
| 45 | default => 0, | ||
| 46 | init_arg => undef, | ||
| 47 | handles => { ## | ||
| 48 | received => 'add', | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has cancelled => ( | ||
| 53 | traits => ['Bool'], | ||
| 54 | is => 'ro', | ||
| 55 | isa => 'Bool', | ||
| 56 | init_arg => undef, | ||
| 57 | default => 0, | ||
| 58 | handles => { ## | ||
| 59 | cancel => 'set', | ||
| 60 | }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | |||
| 64 | sub op_query { | ||
| 65 | my ($self, $opt) = @_; | ||
| 66 | |||
| 67 | my $flags = ($opt->{tailable} ? 1 << 1 : 0) ## | ||
| 68 | | ($opt->{slave_okay} ? 1 << 2 : 0) ## | ||
| 69 | | ($opt->{immortal} ? 1 << 4 : 0); | ||
| 70 | |||
| 71 | my $limit = $opt->{limit} || 0; | ||
| 72 | my $page = $opt->{page} || 100; | ||
| 73 | |||
| 74 | $page = $limit if $limit and $limit < $page; | ||
| 75 | |||
| 76 | my ($query, $info) = MongoDB::write_query( | ||
| 77 | $opt->{ns}, ## | ||
| 78 | $flags, ## | ||
| 79 | $opt->{skip} || 0, | ||
| 80 | $page, | ||
| 81 | $opt->{query} || {}, | ||
| 82 | $opt->{fields} || {} | ||
| 83 | ); | ||
| 84 | |||
| 85 | # TODO: $opt->{all} - fetch all documents before calling cb, just once | ||
| 86 | $info->{exhaust} = $opt->{exhaust}; # TODO | ||
| 87 | $info->{prefetch} = defined $opt->{prefetch} ? $opt->{prefetch} : 1; | ||
| 88 | $info->{cb} = $opt->{cb}; | ||
| 89 | $info->{max} = abs($limit); | ||
| 90 | $info->{timeout} = $opt->{timeout}; | ||
| 91 | |||
| 92 | # MongoDB returns an alias to the scalar in $info hash, | ||
| 93 | # which means it changes the next time $MongoDB::Cursor::_request_id | ||
| 94 | # is incremented. but we want a copy | ||
| 95 | $info->{request_id} = 0 + delete $info->{request_id}; | ||
| 96 | $info->{message} = \$query; | ||
| 97 | |||
| 98 | $self->new($info); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub op_update { | ||
| 103 | my ($self, $opt) = @_; | ||
| 104 | |||
| 105 | my $flags = ($opt->{upsert} ? 1 : 0) | ($opt->{multiple} ? 1 << 1 : 0); | ||
| 106 | |||
| 107 | my ($update) = MongoDB::write_update( ## | ||
| 108 | $opt->{ns}, | ||
| 109 | $opt->{query} || {}, | ||
| 110 | $opt->{update} || {}, | ||
| 111 | $flags, | ||
| 112 | ); | ||
| 113 | |||
| 114 | $self->new({ %$opt, message => \$update }); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub op_insert { | ||
| 118 | my ($self, $opt) = @_; | ||
| 119 | |||
| 120 | my ($insert, $ids) = MongoDB::write_insert($opt->{ns}, $opt->{documents}); | ||
| 121 | |||
| 122 | $self->new({ %$opt, message => \$insert, ids => $ids }); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub op_delete { | ||
| 126 | my ($self, $opt) = @_; | ||
| 127 | |||
| 128 | my ($delete) = MongoDB::write_remove($opt->{ns}, $opt->{query}, $opt->{just_one} || 0); | ||
| 129 | |||
| 130 | $self->new({%$opt, message => \$delete }); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub op_get_more { | ||
| 134 | my ($self, $request_id, $cursor_id, $page) = @_; | ||
| 135 | |||
| 136 | my $next_request_id = ++$MongoDB::Cursor::_request_id; | ||
| 137 | my $message = pack( | ||
| 138 | "V V V V V Z* V a8", | ||
| 139 | 0, $next_request_id, $request_id, 2005, # OP_GET_MORE | ||
| 140 | 0, $self->ns, $page, $cursor_id | ||
| 141 | ); | ||
| 142 | substr($message, 0, 4) = pack("V", length($message)); | ||
| 143 | ${$self->message} = $message; | ||
| 144 | $self->_set_request_id($next_request_id); | ||
| 145 | |||
| 146 | return $self; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub BUILDARGS { | ||
| 150 | my $class = shift; | ||
| 151 | my $args = $class->SUPER::BUILDARGS(@_); | ||
| 152 | |||
| 153 | if ($args->{safe}) { | ||
| 154 | my $last_error = Tie::IxHash->new( | ||
| 155 | getlasterror => 1, | ||
| 156 | w => int($args->{w} || 1), | ||
| 157 | wtimeout => int($args->{wtimeout} || 1000), | ||
| 158 | ); | ||
| 159 | (my $db = $args->{ns}) =~ s/\..*//; | ||
| 160 | my ($query, $info) = MongoDB::write_query($db . '.$cmd', 0, 0, -1, $last_error); | ||
| 161 | @{$args}{keys %$info} = values %$info; | ||
| 162 | |||
| 163 | ${$args->{message}} .= $query; | ||
| 164 | } | ||
| 165 | |||
| 166 | return $args; | ||
| 167 | } | ||
| 168 | |||
| 169 | __PACKAGE__->meta->make_immutable; |