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
6dc7d316
Commit
6dc7d316
authored
Jun 16, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract Collection::listIndexes() to an operation class
parent
99650bb3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
42 deletions
+110
-42
Collection.php
src/Collection.php
+6
-42
ListIndexes.php
src/Operation/ListIndexes.php
+104
-0
No files found.
src/Collection.php
View file @
6dc7d316
...
@@ -13,7 +13,6 @@ use MongoDB\Driver\WriteConcern;
...
@@ -13,7 +13,6 @@ use MongoDB\Driver\WriteConcern;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIteratorIterator
;
use
MongoDB\Model\IndexInput
;
use
MongoDB\Model\IndexInput
;
use
MongoDB\Operation\Aggregate
;
use
MongoDB\Operation\Aggregate
;
use
MongoDB\Operation\CreateIndexes
;
use
MongoDB\Operation\CreateIndexes
;
...
@@ -22,6 +21,7 @@ use MongoDB\Operation\Distinct;
...
@@ -22,6 +21,7 @@ use MongoDB\Operation\Distinct;
use
MongoDB\Operation\FindOneAndDelete
;
use
MongoDB\Operation\FindOneAndDelete
;
use
MongoDB\Operation\FindOneAndReplace
;
use
MongoDB\Operation\FindOneAndReplace
;
use
MongoDB\Operation\FindOneAndUpdate
;
use
MongoDB\Operation\FindOneAndUpdate
;
use
MongoDB\Operation\ListIndexes
;
use
Traversable
;
use
Traversable
;
class
Collection
class
Collection
...
@@ -728,18 +728,15 @@ class Collection
...
@@ -728,18 +728,15 @@ class Collection
/**
/**
* Returns information for all indexes for the collection.
* Returns information for all indexes for the collection.
*
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @see ListIndexes::__construct() for supported options
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
* @return IndexInfoIterator
* @return IndexInfoIterator
*/
*/
public
function
listIndexes
()
public
function
listIndexes
(
array
$options
=
array
()
)
{
{
$
readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$
operation
=
new
ListIndexes
(
$this
->
dbname
,
$this
->
collname
,
$options
);
$server
=
$this
->
manager
->
selectServer
(
$readPreference
);
$server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
)
);
return
(
FeatureDetection
::
isSupported
(
$server
,
FeatureDetection
::
API_LISTINDEXES_CMD
))
return
$operation
->
execute
(
$server
);
?
$this
->
listIndexesCommand
(
$server
)
:
$this
->
listIndexesLegacy
(
$server
);
}
}
/**
/**
...
@@ -904,37 +901,4 @@ class Collection
...
@@ -904,37 +901,4 @@ class Collection
$bulk
->
update
(
$filter
,
$update
,
$options
);
$bulk
->
update
(
$filter
,
$update
,
$options
);
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
}
}
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private
function
listIndexesCommand
(
Server
$server
)
{
$command
=
new
Command
(
array
(
'listIndexes'
=>
$this
->
collname
));
$cursor
=
$server
->
executeCommand
(
$this
->
dbname
,
$command
);
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
IndexInfoIteratorIterator
(
$cursor
);
}
/**
* Returns information for all indexes for this collection by querying the
* "system.indexes" collection (MongoDB <2.8).
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private
function
listIndexesLegacy
(
Server
$server
)
{
$query
=
new
Query
(
array
(
'ns'
=>
$this
->
ns
));
$cursor
=
$server
->
executeQuery
(
$this
->
dbname
.
'.system.indexes'
,
$query
);
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
IndexInfoIteratorIterator
(
$cursor
);
}
}
}
src/Operation/ListIndexes.php
0 → 100644
View file @
6dc7d316
<?php
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Query
;
use
MongoDB\Driver\Server
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIteratorIterator
;
/**
* Operation for the listIndexes command.
*
* @api
* @see MongoDB\Collection::listIndexes()
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
*/
class
ListIndexes
implements
Executable
{
private
static
$wireVersionForCommand
=
3
;
private
$databaseName
;
private
$collectionName
;
private
$options
;
/**
* Constructs a listIndexes command.
*
* Supported options:
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
array
())
{
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
new
InvalidArgumentTypeException
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return IndexInfoIterator
*/
public
function
execute
(
Server
$server
)
{
return
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCommand
)
?
$this
->
executeCommand
(
$server
)
:
$this
->
executeLegacy
(
$server
);
}
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private
function
executeCommand
(
Server
$server
)
{
$cmd
=
array
(
'listIndexes'
=>
$this
->
collectionName
);
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
$cmd
));
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
IndexInfoIteratorIterator
(
$cursor
);
}
/**
* Returns information for all indexes for this collection by querying the
* "system.indexes" collection (MongoDB <3.0).
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private
function
executeLegacy
(
Server
$server
)
{
$filter
=
array
(
'ns'
=>
$this
->
databaseName
.
'.'
.
$this
->
collectionName
);
$options
=
isset
(
$this
->
options
[
'maxTimeMS'
])
?
array
(
'modifiers'
=>
array
(
'$maxTimeMS'
=>
$this
->
options
[
'maxTimeMS'
]))
:
array
();
$cursor
=
$server
->
executeQuery
(
$this
->
databaseName
.
'.system.indexes'
,
new
Query
(
$filter
,
$options
));
$cursor
->
setTypeMap
(
array
(
'document'
=>
'array'
));
return
new
IndexInfoIteratorIterator
(
$cursor
);
}
}
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