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 Exception; |
15
|
|
|
use MongoDB\Collection; |
16
|
|
|
use MongoDB\Database; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Statistics |
22
|
|
|
{ |
23
|
|
|
const COUNT = 'count'; |
24
|
|
|
const SIZE = 'size'; |
25
|
|
|
const AVG_OBJ_SIZE = 'avgObjSize'; |
26
|
|
|
const STORAGE_SIZE = 'storageSize'; |
27
|
|
|
const NUM_EXTENTS = 'numExtents'; |
28
|
|
|
const NINDEXES = 'nindexes'; |
29
|
|
|
const LAST_EXTENT_SIZE = 'lastExtentSize'; |
30
|
|
|
const PADDING_FACTOR = 'paddingFactor'; |
31
|
|
|
const TOTAL_INDEX_SIZE = 'totalIndexSize'; |
32
|
|
|
|
33
|
|
|
public static $metrics = [ |
34
|
|
|
self::COUNT, |
35
|
|
|
self::SIZE, |
36
|
|
|
self::AVG_OBJ_SIZE, |
37
|
|
|
self::STORAGE_SIZE, |
38
|
|
|
self::NUM_EXTENTS, |
39
|
|
|
self::NINDEXES, |
40
|
|
|
self::LAST_EXTENT_SIZE, |
41
|
|
|
self::PADDING_FACTOR, |
42
|
|
|
self::TOTAL_INDEX_SIZE, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \MongoDB\Collection |
47
|
|
|
*/ |
48
|
|
|
private $collection; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $before = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $after = []; |
59
|
|
|
|
60
|
2 |
|
public function setDatabase(Database $database) |
61
|
|
|
{ |
62
|
2 |
|
$this->database = $database; |
|
|
|
|
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \MongoDB\Collection |
67
|
|
|
*/ |
68
|
3 |
|
public function setCollection(Collection $collection) |
69
|
|
|
{ |
70
|
3 |
|
$this->collection = $collection; |
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \MongoDB\Collection |
75
|
|
|
*/ |
76
|
1 |
|
public function getCollection() |
77
|
|
|
{ |
78
|
1 |
|
return $this->collection; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function updateBefore() |
82
|
|
|
{ |
83
|
|
|
$data = $this->getCollectionStats(); |
84
|
|
|
foreach ($data as $key => $value) { |
85
|
|
|
if (in_array($key, static::$metrics)) { |
86
|
|
|
$this->before[$key] = $value; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getBefore() |
95
|
|
|
{ |
96
|
|
|
return $this->before; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function updateAfter() |
100
|
|
|
{ |
101
|
|
|
$data = $this->getCollectionStats(); |
102
|
|
|
foreach ($data as $key => $value) { |
103
|
|
|
if (in_array($key, static::$metrics)) { |
104
|
|
|
$this->after[$key] = $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getAfter() |
113
|
|
|
{ |
114
|
|
|
return $this->after; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
* |
120
|
|
|
* @throws \Exception |
121
|
|
|
*/ |
122
|
2 |
|
protected function getCollectionStats() |
123
|
|
|
{ |
124
|
2 |
|
$name = $this->collection->getCollectionName(); |
125
|
|
|
|
126
|
2 |
|
if (!$data = $this->database->command(['collStats' => $name])) { |
127
|
1 |
|
$message = sprintf( |
128
|
1 |
|
'Statistics not found for collection %s', |
129
|
1 |
|
$name |
130
|
|
|
); |
131
|
1 |
|
throw new Exception($message); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $data; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|