1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
5
|
|
|
* |
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations\Collection; |
13
|
|
|
|
14
|
|
|
use \MongoDB\Collection; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Statistics |
21
|
|
|
{ |
22
|
|
|
const COUNT = 'count'; |
23
|
|
|
const SIZE = 'size'; |
24
|
|
|
const AVG_OBJ_SIZE = 'avgObjSize'; |
25
|
|
|
const STORAGE_SIZE = 'storageSize'; |
26
|
|
|
const NUM_EXTENTS = 'numExtents'; |
27
|
|
|
const NINDEXES = 'nindexes'; |
28
|
|
|
const LAST_EXTENT_SIZE = 'lastExtentSize'; |
29
|
|
|
const PADDING_FACTOR = 'paddingFactor'; |
30
|
|
|
const TOTAL_INDEX_SIZE = 'totalIndexSize'; |
31
|
|
|
|
32
|
|
|
public static $metrics = [ |
33
|
|
|
self::COUNT, |
34
|
|
|
self::SIZE, |
35
|
|
|
self::AVG_OBJ_SIZE, |
36
|
|
|
self::STORAGE_SIZE, |
37
|
|
|
self::NUM_EXTENTS, |
38
|
|
|
self::NINDEXES, |
39
|
|
|
self::LAST_EXTENT_SIZE, |
40
|
|
|
self::PADDING_FACTOR, |
41
|
|
|
self::TOTAL_INDEX_SIZE, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \MongoDB\Collection |
46
|
|
|
*/ |
47
|
|
|
private $collection; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $before = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $after = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \MongoDB\Collection |
61
|
|
|
*/ |
62
|
3 |
|
public function setCollection(Collection $collection) |
63
|
|
|
{ |
64
|
3 |
|
$this->collection = $collection; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return \MongoDB\Collection |
69
|
|
|
*/ |
70
|
1 |
|
public function getCollection() |
71
|
|
|
{ |
72
|
1 |
|
return $this->collection; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function updateBefore() |
76
|
|
|
{ |
77
|
|
|
$data = $this->getCollectionStats(); |
78
|
|
|
foreach ($data as $key => $value) { |
79
|
|
|
if (in_array($key, static::$metrics)) { |
80
|
|
|
$this->before[$key] = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getBefore() |
89
|
|
|
{ |
90
|
|
|
return $this->before; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function updateAfter() |
94
|
|
|
{ |
95
|
|
|
$data = $this->getCollectionStats(); |
96
|
|
|
foreach ($data as $key => $value) { |
97
|
|
|
if (in_array($key, static::$metrics)) { |
98
|
|
|
$this->after[$key] = $value; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getAfter() |
107
|
|
|
{ |
108
|
|
|
return $this->after; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
* |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
|
|
protected function getCollectionStats() |
117
|
|
|
{ |
118
|
|
|
$database = $this->collection->getDatabase(); |
|
|
|
|
119
|
|
|
$name = $this->collection->getName(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
if (!$data = $database->command(['collStats' => $name])) { |
122
|
|
|
$message = sprintf( |
123
|
|
|
'Statistics not found for collection %s', |
124
|
|
|
$name |
125
|
|
|
); |
126
|
|
|
throw new Exception($message); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (isset($data['errmsg'])) { |
130
|
|
|
throw new Exception($data['errmsg']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $data; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.