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
4de66e39
Commit
4de66e39
authored
Apr 24, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-63: Index creation methods
parent
e950daec
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
141 additions
and
13 deletions
+141
-13
Collection.php
src/Collection.php
+116
-13
Exception.php
src/Exception/Exception.php
+7
-0
InvalidArgumentException.php
src/Exception/InvalidArgumentException.php
+7
-0
UnexpectedTypeException.php
src/Exception/UnexpectedTypeException.php
+11
-0
No files found.
src/Collection.php
View file @
4de66e39
...
...
@@ -10,9 +10,11 @@ use MongoDB\Driver\Query;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIteratorIterator
;
use
InvalidArgumentException
;
use
stdClass
;
class
Collection
{
...
...
@@ -248,34 +250,84 @@ class Collection
}
/**
* Create a single index
in
the collection.
* Create a single index
for
the collection.
*
* @see http://docs.mongodb.org/manual/reference/command/createIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
* @param array|object $keys
* @param array $options
* @see Collection::createIndexes()
* @param array|object $key Document containing fields mapped to values,
* which denote order or an index type
* @param array $options Index options
* @return string The name of the created index
*/
public
function
createIndex
(
$key
s
,
array
$options
=
array
())
public
function
createIndex
(
$key
,
array
$options
=
array
())
{
// TODO
return
current
(
$this
->
createIndexes
(
array
(
array
(
'key'
=>
$key
)
+
$options
)));
}
/**
* Create multiple indexes in the collection.
* Create one or more indexes for the collection.
*
* Each element in the $indexes array must have a "key" document, which
* contains fields mapped to an order or type. Other options may follow.
* For example:
*
* $indexes = [
* // Create a unique index on the "username" field
* [ 'key' => [ 'username' => 1 ], 'unique' => true ],
* // Create a 2dsphere index on the "loc" field with a custom name
* [ 'key' => [ 'loc' => '2dsphere' ], 'name' => 'geo' ],
* ];
*
* TODO: decide if $models should be an array of associative arrays, using
* createIndex()'s parameter names as keys, or tuples, using parameters in
* order (e.g. [keys, options]).
* If the "name" option is unspecified, a name will be generated from the
* "key" document.
*
* @see http://docs.mongodb.org/manual/reference/command/createIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
* @param array $
model
s
* @param array $
indexes List of index specification
s
* @return string[] The names of the created indexes
* @throws InvalidArgumentException if an index specification does not
* contain a "key" document
* @throws UnexpectedTypeException if an index specification is not an array
* or a "key" document is not an array or
* object
*/
public
function
createIndexes
(
array
$
model
s
)
public
function
createIndexes
(
array
$
indexe
s
)
{
// TODO
foreach
(
$indexes
as
&
$index
)
{
if
(
!
is_array
(
$index
))
{
throw
new
UnexpectedTypeException
(
$index
,
'array'
);
}
if
(
!
isset
(
$index
[
'key'
]))
{
throw
new
InvalidArgumentException
(
'Required "key" document is missing from index specification'
);
}
if
(
!
is_array
(
$index
[
'key'
])
&&
!
is_object
(
$index
[
'key'
]))
{
throw
new
UnexpectedTypeException
(
$index
[
'key'
],
'array or object'
);
}
$index
[
'key'
]
=
(
object
)
$index
[
'key'
];
$index
[
'ns'
]
=
$this
->
ns
;
if
(
!
isset
(
$index
[
'name'
]))
{
$index
[
'name'
]
=
$this
->
generateIndexName
(
$index
[
'key'
]);
}
}
$readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$server
=
$this
->
manager
->
selectServer
(
$readPreference
);
$serverInfo
=
$server
->
getInfo
();
$maxWireVersion
=
isset
(
$serverInfo
[
'maxWireVersion'
])
?
$serverInfo
[
'maxWireVersion'
]
:
0
;
if
(
$maxWireVersion
>=
2
)
{
$this
->
createIndexesCommand
(
$server
,
$indexes
);
}
else
{
$this
->
createIndexesLegacy
(
$server
,
$indexes
);
}
return
array_map
(
function
(
array
$index
)
{
return
$index
[
'name'
];
},
$indexes
);
}
/**
...
...
@@ -1162,6 +1214,57 @@ class Collection
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
}
/**
* Create one or more indexes for the collection using the createIndexes
* command.
*
* @param Server $server
* @param array $indexes
*/
private
function
createIndexesCommand
(
Server
$server
,
array
$indexes
)
{
$command
=
new
Command
(
array
(
'createIndexes'
=>
$this
->
collname
,
'indexes'
=>
$indexes
,
));
$server
->
executeCommand
(
$this
->
dbname
,
$command
);
}
/**
* Create one or more indexes for the collection by inserting into the
* "system.indexes" collection (MongoDB <2.6).
*
* @param Server $server
* @param array $indexes
*/
private
function
createIndexesLegacy
(
Server
$server
,
array
$indexes
)
{
$bulk
=
new
BulkWrite
(
true
);
foreach
(
$indexes
as
$index
)
{
$bulk
->
insert
(
$index
);
}
$server
->
executeBulkWrite
(
$this
->
dbname
.
'.system.indexes'
,
$bulk
);
}
/**
* Generates an index name from its key specification.
*
* @param object $key
* @return string
*/
private
function
generateIndexName
(
stdClass
$key
)
{
$name
=
''
;
foreach
(
$key
as
$field
=>
$type
)
{
$name
.=
(
$name
!=
''
?
'_'
:
''
)
.
$field
.
'_'
.
$type
;
}
return
$name
;
}
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
...
...
src/Exception/Exception.php
0 → 100644
View file @
4de66e39
<?php
namespace
MongoDB\Exception
;
interface
Exception
{
}
src/Exception/InvalidArgumentException.php
0 → 100644
View file @
4de66e39
<?php
namespace
MongoDB\Exception
;
class
InvalidArgumentException
extends
\InvalidArgumentException
implements
Exception
{
}
src/Exception/UnexpectedTypeException.php
0 → 100644
View file @
4de66e39
<?php
namespace
MongoDB\Exception
;
class
UnexpectedTypeException
extends
InvalidArgumentException
{
public
function
__construct
(
$value
,
$expectedType
)
{
parent
::
__construct
(
sprintf
(
'Expected argument of type "%s", "%s" given'
,
$expectedType
,
is_object
(
$value
)
?
get_class
(
$value
)
:
gettype
(
$value
)));
}
}
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