|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Drivers\MongoDB; |
|
4
|
|
|
|
|
5
|
|
|
use MongoDB\Collection; |
|
|
|
|
|
|
6
|
|
|
use MongoDB\Model\IndexInfo; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Index |
|
9
|
|
|
{ |
|
10
|
|
|
public static function getIndexes(Collection $collection) |
|
11
|
|
|
{ |
|
12
|
|
|
$listIndexes = $collection->listIndexes(); |
|
13
|
|
|
$indexes = []; |
|
14
|
|
|
foreach ($listIndexes as $index) { |
|
15
|
|
|
$indexes[] = [ |
|
16
|
|
|
"name" => $index->getName(), |
|
17
|
|
|
"oldName" => $index->getName(), |
|
18
|
|
|
"columns" => array_keys($index->getKey()), |
|
19
|
|
|
"type" => static::getType($index), |
|
20
|
|
|
"isPrimary" => false, |
|
21
|
|
|
"isUnique" => $index->isUnique(), |
|
22
|
|
|
"isComposite" => (count($index->getKey()) > 1) ? true : false, |
|
23
|
|
|
"flags" => [], |
|
24
|
|
|
"options" => [], |
|
25
|
|
|
"namespace" => $index->getNamespace(), |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $indexes; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function getType(IndexInfo $index) |
|
33
|
|
|
{ |
|
34
|
|
|
$type = ""; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$type = static::getCommonType($index); |
|
37
|
|
|
|
|
38
|
|
|
if ($index->isUnique() && !$index->isSparse() && !static::checkDescending($index)) { |
|
39
|
|
|
$type = "UNIQUE"; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($index->isUnique() && !$index->isSparse() && static::checkDescending($index)) { |
|
43
|
|
|
$type = "UNIQUE_DESC"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($index->isSparse() && !static::checkDescending($index)) { |
|
47
|
|
|
$type = "SPARSE"; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($index->isSparse() && $index->isUnique() && !static::checkDescending($index)) { |
|
51
|
|
|
$type = "SPARSE_UNIQUE"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($index->isSparse() && $index->isUnique() && static::checkDescending($index)) { |
|
55
|
|
|
$type = "SPARSE_UNIQUE_DESC"; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($index->isSparse() && static::checkDescending($index)) { |
|
59
|
|
|
$type = "SPARSE_DESC"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $type; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected static function getCommonType(IndexInfo $index) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($index->isText()) { |
|
68
|
|
|
return "TEXT"; |
|
69
|
|
|
} else if ($index->is2dSphere()) { |
|
70
|
|
|
return "2DSPARSE"; |
|
71
|
|
|
} else if ($index->isTtl()) { |
|
72
|
|
|
return "TTL"; |
|
73
|
|
|
} else if ($index->isGeoHaystack()) { |
|
74
|
|
|
return "GEOHAYSTACK"; |
|
75
|
|
|
} else { |
|
76
|
|
|
$name = $index->getName(); |
|
77
|
|
|
$partials = explode("_", $name); |
|
78
|
|
|
$type = end($partials); |
|
79
|
|
|
if ($type == 'asc') { |
|
80
|
|
|
return "ASC"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($type == 'index') { |
|
84
|
|
|
return "INDEX"; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($type == 'desc') { |
|
88
|
|
|
return "DESC"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return ""; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected static function checkDescending($index) |
|
96
|
|
|
{ |
|
97
|
|
|
$keys = $index->getKey(); |
|
98
|
|
|
|
|
99
|
|
|
foreach ($keys as $key => $value) { |
|
100
|
|
|
if ($value == -1) { |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function setIndexes(Collection $collection, $indexes) |
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
$collection->dropIndexes(); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($indexes as $index) { |
|
114
|
|
|
$columns = $index['columns']; |
|
115
|
|
|
$name = $index['name']; |
|
|
|
|
|
|
116
|
|
|
$type = $index['type']; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($columns as $column) { |
|
119
|
|
|
if ($column == '_id') { |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
switch ($type) { |
|
124
|
|
|
|
|
125
|
|
|
case 'TEXT': |
|
126
|
|
|
$indexType = 'text'; |
|
127
|
|
|
break; |
|
128
|
|
|
case 'INDEX': |
|
129
|
|
|
$indexType = 1; |
|
130
|
|
|
break; |
|
131
|
|
|
case 'UNIQUE': |
|
132
|
|
|
$indexType = 1; |
|
133
|
|
|
$options['unique'] = true; |
|
134
|
|
|
break; |
|
135
|
|
|
case 'UNIQUE_DESC': |
|
136
|
|
|
$indexType = -1; |
|
137
|
|
|
$options['unique'] = true; |
|
138
|
|
|
break; |
|
139
|
|
|
case 'TTL': |
|
140
|
|
|
$indexType = 1; |
|
141
|
|
|
$options['expireAfterSeconds'] = 3600; |
|
142
|
|
|
break; |
|
143
|
|
|
case 'SPARSE': |
|
144
|
|
|
$indexType = 1; |
|
145
|
|
|
$options['sparse'] = true; |
|
146
|
|
|
break; |
|
147
|
|
|
case 'SPARSE_DESC': |
|
148
|
|
|
$indexType = -1; |
|
149
|
|
|
$options['sparse'] = true; |
|
150
|
|
|
break; |
|
151
|
|
|
case 'SPARSE_UNIQUE': |
|
152
|
|
|
$indexType = 1; |
|
153
|
|
|
$options['sparse'] = true; |
|
154
|
|
|
$options['unique'] = true; |
|
|
|
|
|
|
155
|
|
|
case 'SPARSE_UNIQUE_DESC': |
|
156
|
|
|
$indexType = -1; |
|
157
|
|
|
$options['sparse'] = true; |
|
158
|
|
|
$options['unique'] = true; |
|
159
|
|
|
break; |
|
160
|
|
|
case 'ASC': |
|
161
|
|
|
$indexType = 1; |
|
162
|
|
|
break; |
|
163
|
|
|
case 'DESC': |
|
164
|
|
|
$indexType = -1; |
|
165
|
|
|
break; |
|
166
|
|
|
default: |
|
167
|
|
|
$indexType = 1; |
|
168
|
|
|
break; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type); |
|
172
|
|
|
|
|
173
|
|
|
$options['ns'] = $collection->getNamespace(); |
|
174
|
|
|
|
|
175
|
|
|
$collection->createIndex([$column => $indexType], $options); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths