File: WriteConcern.pm

package info (click to toggle)
libmongodb-perl 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,292 kB
  • sloc: perl: 14,421; python: 299; makefile: 20; sh: 11
file content (229 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#  Copyright 2014 - present MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

use strict;
use warnings;
package MongoDB::WriteConcern;

# ABSTRACT: Encapsulate and validate a write concern

use version;
our $VERSION = 'v2.2.2';

use Moo;
use MongoDB::Error;
use MongoDB::_Types qw(
    Boolish
);
use Types::Standard qw(
    ArrayRef
    Int
    Str
    Maybe
);
use Scalar::Util qw/looks_like_number/;
use boolean;
use namespace::clean -except => 'meta';

#pod =attr w
#pod
#pod Specifies the desired acknowledgement level. If not set, the
#pod server default will be used, which is usually "1".
#pod
#pod =cut

has w => (
    is        => 'ro',
    isa       => Maybe[Str],
);

#pod =attr wtimeout
#pod
#pod Specifies how long to wait for the write concern to be satisfied (in
#pod milliseconds).  Defaults to 1000. If you set this to undef, it could block
#pod indefinitely (or until socket timeout is reached).
#pod
#pod =cut

has wtimeout => (
    is      => 'ro',
    isa     => Maybe[Int],
    default => 1000,
);

#pod =attr j
#pod
#pod The j option confirms that the mongod instance has written the data to the
#pod on-disk journal.  Defaults to false.
#pod
#pod B<Note>: specifying a write concern that set j to a true value may result in an
#pod error with a mongod or mongos running with --nojournal option now errors.
#pod
#pod =cut

has j => (
    is        => 'ro',
    isa       => Boolish,
);

has _is_acknowledged => (
    is      => 'lazy',
    isa     => Boolish,
    reader  => 'is_acknowledged',
    builder => '_build_is_acknowledged',
);

has _as_args => (
    is      => 'lazy',
    isa     => ArrayRef,
    reader  => 'as_args',
    builder => '_build_as_args',
);

sub _build_is_acknowledged {
    my ($self) = @_;
    return !!( $self->j || $self->_w_is_acknowledged );
}

sub _build_as_args {
    my ($self) = @_;

    my $wc = {
        ( defined( $self->w )        ? ( w        => $self->w )            : () ),
        ( defined( $self->wtimeout ) ? ( wtimeout => 0+ $self->wtimeout )  : () ),
        ( defined( $self->j )        ? ( j        => boolean( $self->j ) ) : () ),
    };

    return ( keys %$wc ? [writeConcern => $wc] : [] );
}

sub BUILD {
    my ($self) = @_;
    if ( ! $self->_w_is_valid ) {
        MongoDB::UsageError->throw("can't use write concern w=" . $self->w );
    }

    if ( ! $self->_w_is_acknowledged && $self->j ) {
        MongoDB::UsageError->throw("can't use write concern w=0 with j=" . $self->j );
    }

    # cant use nonnegnum earlier in type as errors explode with wrong class
    if ( defined($self->wtimeout) && $self->wtimeout < 0 ) {
        MongoDB::UsageError->throw("wtimeout must be non negative");
    }
    return;
}

sub _w_is_valid {
  my ($self) = @_;
  return 1 if !defined $self->w;
  return looks_like_number( $self->w ) ? $self->w >= 0 : length $self->w;
}

sub _w_is_acknowledged {
    my ($self) = @_;
    return 1 if !defined $self->w;
    return looks_like_number( $self->w ) ? $self->w > 0 : length $self->w;
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

MongoDB::WriteConcern - Encapsulate and validate a write concern

=head1 VERSION

version v2.2.2

=head1 SYNOPSIS

    $rp = MongoDB::WriteConcern->new(); # w:1, wtimeout: 1000

    $rp = MongoDB::WriteConcern->new(
        w        => 'majority',
        wtimeout => 10000, # milliseconds
    );

=head1 DESCRIPTION

A write concern describes the guarantee that MongoDB provides when reporting on
the success of a write operation.

For core documentation on read preference see
L<http://docs.mongodb.org/manual/core/read-preference/>.

=head1 ATTRIBUTES

=head2 w

Specifies the desired acknowledgement level. If not set, the
server default will be used, which is usually "1".

=head2 wtimeout

Specifies how long to wait for the write concern to be satisfied (in
milliseconds).  Defaults to 1000. If you set this to undef, it could block
indefinitely (or until socket timeout is reached).

=head2 j

The j option confirms that the mongod instance has written the data to the
on-disk journal.  Defaults to false.

B<Note>: specifying a write concern that set j to a true value may result in an
error with a mongod or mongos running with --nojournal option now errors.

=head1 AUTHORS

=over 4

=item *

David Golden <david@mongodb.com>

=item *

Rassi <rassi@mongodb.com>

=item *

Mike Friedman <friedo@friedo.com>

=item *

Kristina Chodorow <k.chodorow@gmail.com>

=item *

Florian Ragwitz <rafl@debian.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2020 by MongoDB, Inc.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut