our
$VERSION
=
'v0.999.999.4'
;
requires
qw/query _set_query query_flags read_preference/
;
sub
_apply_read_prefs {
my
(
$self
,
$link
,
$topology_type
) =
@_
;
$topology_type
||=
"<undef>"
;
my
$read_pref
=
$self
->read_preference;
if
(
$topology_type
eq
'Single'
) {
if
(
$link
->server &&
$link
->server->type eq
'Mongos'
) {
$self
->_apply_mongos_read_prefs(
$read_pref
);
}
else
{
$self
->query_flags->{slave_ok} = 1;
}
}
elsif
(
$topology_type
eq any(
qw/ReplicaSetNoPrimary ReplicaSetWithPrimary/
) ) {
if
( !
$read_pref
||
$read_pref
->mode eq
'primary'
) {
$self
->query_flags->{slave_ok} = 0;
}
else
{
$self
->query_flags->{slave_ok} = 1;
}
}
elsif
(
$topology_type
eq
'Sharded'
) {
$self
->_apply_mongos_read_prefs(
$read_pref
);
}
else
{
MongoDB::InternalError->throw(
"can't query topology type '$topology_type'"
);
}
return
;
}
sub
_apply_mongos_read_prefs {
my
(
$self
,
$read_pref
) =
@_
;
my
$mode
=
$read_pref
?
$read_pref
->mode :
'primary'
;
my
$need_read_pref
;
if
(
$mode
eq
'primary'
) {
$self
->query_flags->{slave_ok} = 0;
}
elsif
(
$mode
eq any(
qw/secondary primaryPreferred nearest/
) ) {
$self
->query_flags->{slave_ok} = 1;
$need_read_pref
= 1;
}
elsif
(
$mode
eq
'secondaryPreferred'
) {
$self
->query_flags->{slave_ok} = 1;
$need_read_pref
= 1
unless
$read_pref
->has_empty_tag_sets;
}
else
{
MongoDB::InternalError->throw(
"invalid read preference mode '$mode'"
);
}
if
(
$need_read_pref
) {
if
( !
$self
->query->FETCH(
'$query'
) ) {
$self
->_set_query( Tie::IxHash->new(
'$query'
=>
$self
->query ) );
}
$self
->query->Push(
'$readPreference'
=>
$read_pref
->for_mongos );
}
return
;
}
1;