|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\Services\Explain; |
|
4
|
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Models\Query; |
|
6
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
|
7
|
|
|
use MongoDB\Driver\Command; |
|
8
|
|
|
use MongoDB\Driver\Cursor; |
|
9
|
|
|
|
|
10
|
|
|
class ExplainQueryService |
|
11
|
|
|
{ |
|
12
|
|
|
const VERBOSITY_QUERY_PLANNER = 'queryPlanner'; |
|
13
|
|
|
const VERBOSITY_EXECUTION_STATS = 'executionStats'; |
|
14
|
|
|
const VERBOSITY_ALL_PLAN_EXECUTION = 'allPlansExecution'; |
|
15
|
|
|
|
|
16
|
|
|
public static $acceptedMethods= [ |
|
17
|
|
|
'count', |
|
18
|
|
|
'distinct', |
|
19
|
|
|
'find', |
|
20
|
|
|
'findOne', |
|
21
|
|
|
'findOneAndUpdate', |
|
22
|
|
|
'findOneAndDelete', |
|
23
|
|
|
'deleteOne', |
|
24
|
|
|
'deleteMany', |
|
25
|
|
|
'aggregate', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ClientRegistry */ |
|
29
|
|
|
private $clientRegistry; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructs a explain command. |
|
33
|
|
|
* |
|
34
|
|
|
* Supported options: |
|
35
|
|
|
* verbosity : queryPlanner | executionStats Mode | allPlansExecution (default) |
|
36
|
|
|
* The explain command provides information on the execution of the following commands: |
|
37
|
|
|
* count, distinct, group, find, findAndModify, delete, and update. |
|
38
|
|
|
* |
|
39
|
|
|
* @param ClientRegistry $clientRegistry |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function __construct(ClientRegistry $clientRegistry) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->clientRegistry = $clientRegistry; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Execute the operation. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Query $query |
|
50
|
|
|
* @param string $verbosity |
|
51
|
|
|
* |
|
52
|
|
|
* @return Cursor |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function execute(Query $query, string $verbosity = self::VERBOSITY_ALL_PLAN_EXECUTION): Cursor |
|
55
|
|
|
{ |
|
56
|
1 |
|
if (!in_array($query->getMethod(), self::$acceptedMethods)) { |
|
57
|
|
|
throw new \InvalidArgumentException( |
|
58
|
|
|
'Cannot explain the method \''.$query->getMethod().'\'. Allowed methods: '. implode(', ',self::$acceptedMethods) |
|
59
|
|
|
); |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
1 |
|
$manager = $this->clientRegistry->getClient($query->getClient())->__debugInfo()['manager']; |
|
63
|
|
|
|
|
64
|
|
|
return $manager |
|
65
|
1 |
|
->executeCommand( |
|
66
|
1 |
|
$query->getDatabase(), |
|
67
|
1 |
|
new Command(ExplainCommandBuilder::createCommandArgs($query, $verbosity)) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|