Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
2b1c1ec9
Unverified
Commit
2b1c1ec9
authored
Aug 01, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-357: Refactor EstimatedDocumentCount Operation to encapsulate a Count Operation
parent
a2609296
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
87 deletions
+9
-87
EstimatedDocumentCount.php
src/Operation/EstimatedDocumentCount.php
+9
-87
No files found.
src/Operation/EstimatedDocumentCount.php
View file @
2b1c1ec9
...
...
@@ -17,11 +17,7 @@
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Session
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnexpectedValueException
;
...
...
@@ -36,12 +32,10 @@ use MongoDB\Exception\UnsupportedException;
*/
class
EstimatedDocumentCount
implements
Executable
,
Explainable
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
$databaseName
;
private
$collectionName
;
private
$options
;
private
$count
;
/**
* Constructs a count command.
...
...
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
[])
{
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
ReadConcern
::
class
);
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
ReadPreference
::
class
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
Session
::
class
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
$options
[
'readConcern'
]
->
isDefault
())
{
unset
(
$options
[
'readConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
options
=
$options
;
$this
->
options
=
array_intersect_key
(
$options
,
[
'maxTimeMS'
=>
1
,
'readConcern'
=>
1
,
'readPreference'
=>
1
,
'session'
=>
1
]);
$this
->
count
=
$this
->
createCount
();
}
/**
...
...
@@ -106,73 +82,19 @@ class EstimatedDocumentCount implements Executable, Explainable
*/
public
function
execute
(
Server
$server
)
{
if
(
isset
(
$this
->
options
[
'collation'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCollation
))
{
throw
UnsupportedException
::
collationNotSupported
();
}
if
(
isset
(
$this
->
options
[
'readConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
throw
UnsupportedException
::
readConcernNotSupported
();
}
$inTransaction
=
isset
(
$this
->
options
[
'session'
])
&&
$this
->
options
[
'session'
]
->
isInTransaction
();
if
(
$inTransaction
&&
isset
(
$this
->
options
[
'readConcern'
]))
{
throw
UnsupportedException
::
readConcernNotSupportedInTransaction
();
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
// Older server versions may return a float
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
return
(
integer
)
$result
->
n
;
return
$this
->
count
->
execute
(
$server
);
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
c
reateCommandDocument
(
);
return
$this
->
c
ount
->
getCommandDocument
(
$server
);
}
/**
* Create the count command document.
*
* @return array
* @return Count
*/
private
function
createCo
mmandDocume
nt
()
private
function
createCo
u
nt
()
{
$cmd
=
[
'count'
=>
$this
->
collectionName
];
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
return
$cmd
;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'readConcern'
]))
{
$options
[
'readConcern'
]
=
$this
->
options
[
'readConcern'
];
}
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
options
[
'readPreference'
];
}
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
return
$options
;
return
new
Count
(
$this
->
databaseName
,
$this
->
collectionName
,
[],
$this
->
options
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment