1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\MongoDB; |
4
|
|
|
|
5
|
|
|
use Doctrine\MongoDB\Database; |
6
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser; |
7
|
|
|
use Wikibase\EntityStore\EntityDocumentSaver; |
8
|
|
|
use Wikibase\EntityStore\EntityStore; |
9
|
|
|
use Wikibase\EntityStore\EntityStoreOptions; |
10
|
|
|
use Wikibase\EntityStore\Internal\DispatchingEntityIdForQueryLookup; |
11
|
|
|
use Wikibase\EntityStore\Internal\DispatchingEntityIdForTermLookup; |
12
|
|
|
use Wikibase\EntityStore\Internal\DispatchingEntityLookup; |
13
|
|
|
use Wikibase\EntityStore\Internal\EntitySerializationFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @licence GPLv2+ |
17
|
|
|
* @author Thomas Pellissier Tanon |
18
|
|
|
* |
19
|
|
|
* @todo add indexes if all languages are supported |
20
|
|
|
*/ |
21
|
|
|
class MongoDBEntityStore extends EntityStore { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Option to set the time limit for query operations in milliseconds. |
25
|
|
|
*/ |
26
|
|
|
const OPTION_QUERY_TIME_LIMIT = 'mongodb-query-time-limit'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Database |
30
|
|
|
*/ |
31
|
|
|
private $database; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DispatchingEntityLookup |
35
|
|
|
*/ |
36
|
|
|
private $entityLookup; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DispatchingEntityIdForTermLookup |
40
|
|
|
*/ |
41
|
|
|
private $entityForTermLookup; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DispatchingEntityIdForQueryLookup |
45
|
|
|
*/ |
46
|
|
|
private $entityForQueryLookup; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var EntityDocumentSaver |
50
|
|
|
*/ |
51
|
|
|
private $entitySaver; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Database $database |
55
|
|
|
* @param EntityStoreOptions $options |
56
|
|
|
*/ |
57
|
10 |
|
public function __construct( Database $database, EntityStoreOptions $options = null ) { |
58
|
10 |
|
$this->database = $database; |
59
|
|
|
|
60
|
10 |
|
parent::__construct( $options ); |
61
|
10 |
|
$this->defaultOption( self::OPTION_QUERY_TIME_LIMIT, null ); |
62
|
|
|
|
63
|
10 |
|
$entityDatabase = $this->newEntityDatabase( $database ); |
64
|
10 |
|
$this->entityLookup = new DispatchingEntityLookup( $entityDatabase ); |
65
|
10 |
|
$this->entityForTermLookup = new DispatchingEntityIdForTermLookup( $this->newEntityIdForTermLookup( $database ) ); |
66
|
10 |
|
$this->entityForQueryLookup = new DispatchingEntityIdForQueryLookup( $this->newEntityIdForQueryLookup( $database ) ); |
67
|
10 |
|
$this->entitySaver = $entityDatabase; |
68
|
10 |
|
} |
69
|
|
|
|
70
|
10 |
|
private function newEntityDatabase( Database $database ) { |
71
|
10 |
|
return new MongoDBEntityDatabase( $database, $this->newDocumentBuilder() ); |
72
|
|
|
} |
73
|
|
|
|
74
|
10 |
|
private function newEntityIdForTermLookup( Database $database ) { |
75
|
10 |
|
return new MongoDBEntityIdForTermLookup( $database, $this->newDocumentBuilder() ); |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
private function newEntityIdForQueryLookup( Database $database ) { |
79
|
10 |
|
return new MongoDBEntityIdForQueryLookup( |
80
|
10 |
|
$database, |
81
|
10 |
|
$this->newDocumentBuilder(), |
82
|
10 |
|
$this->getOption( self::OPTION_QUERY_TIME_LIMIT ) |
83
|
10 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
private function newDocumentBuilder() { |
87
|
10 |
|
$serialization = new EntitySerializationFactory(); |
88
|
10 |
|
return new MongoDBDocumentBuilder( |
89
|
10 |
|
$serialization->newEntitySerializer(), |
90
|
10 |
|
$serialization->newEntityDeserializer(), |
91
|
10 |
|
new BasicEntityIdParser(), |
92
|
10 |
|
$this->getOptions() |
93
|
10 |
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @see EntityStore::getEntityLookup |
98
|
|
|
*/ |
99
|
1 |
|
public function getEntityLookup() { |
100
|
1 |
|
return $this->entityLookup; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @see EntityStore::getEntityDocumentLookup |
105
|
|
|
*/ |
106
|
2 |
|
public function getEntityDocumentLookup() { |
107
|
2 |
|
return $this->entityLookup; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @see EntityStore::getItemLookup |
112
|
|
|
*/ |
113
|
2 |
|
public function getItemLookup() { |
114
|
2 |
|
return $this->entityLookup; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see EntityStore::getPropertyLookup |
119
|
|
|
*/ |
120
|
2 |
|
public function getPropertyLookup() { |
121
|
2 |
|
return $this->entityLookup; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @see EntityStore::getEntityDocumentSaver |
126
|
|
|
*/ |
127
|
2 |
|
public function getEntityDocumentSaver() { |
128
|
2 |
|
return $this->entitySaver; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @see EntityStore::getItemIdForTermLookup |
133
|
|
|
*/ |
134
|
2 |
|
public function getItemIdForTermLookup() { |
135
|
2 |
|
return $this->entityForTermLookup; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @see EntityStore::getPropertyIdForTermLookup |
140
|
|
|
*/ |
141
|
2 |
|
public function getPropertyIdForTermLookup() { |
142
|
2 |
|
return $this->entityForTermLookup; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @see EntityStore::getItemIdForQueryLookup |
147
|
|
|
*/ |
148
|
2 |
|
public function getItemIdForQueryLookup() { |
149
|
2 |
|
return $this->entityForQueryLookup; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @see EntityStore::getPropertyIdForQueryLookup |
154
|
|
|
*/ |
155
|
2 |
|
public function getPropertyIdForQueryLookup() { |
156
|
2 |
|
return $this->entityForQueryLookup; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @see EntityStore::setupStore |
161
|
|
|
*/ |
162
|
1 |
|
public function setupStore() { |
163
|
1 |
|
foreach( MongoDBDocumentBuilder::$SUPPORTED_ENTITY_TYPES as $type ) { |
164
|
1 |
|
$this->database->command( [ |
165
|
1 |
|
'collMod' => $type, |
166
|
|
|
'usePowerOf2Sizes' => true |
167
|
1 |
|
] ); |
168
|
1 |
|
} |
169
|
1 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @see EntityStore::setupIndexes |
173
|
|
|
*/ |
174
|
1 |
|
public function setupIndexes() { |
175
|
1 |
|
$this->setupTermIndexes(); |
176
|
1 |
|
$this->setupClaimsIndexes(); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
private function setupTermIndexes() { |
180
|
1 |
|
$languagesOption = $this->getOption( EntityStore::OPTION_LANGUAGES ); |
181
|
|
|
|
182
|
1 |
|
if( $languagesOption === null ) { |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
foreach( $languagesOption as $language ) { |
187
|
1 |
|
$key = 'sterms.' . $language; |
188
|
|
|
|
189
|
1 |
|
foreach( MongoDBDocumentBuilder::$SUPPORTED_ENTITY_TYPES as $type ) { |
190
|
1 |
|
$this->database->selectCollection( $type )->ensureIndex( |
191
|
1 |
|
[ $key => 1 ], |
192
|
1 |
|
[ 'sparse' => true, 'socketTimeoutMS' => -1 ] |
193
|
1 |
|
); |
194
|
1 |
|
} |
195
|
1 |
|
} |
196
|
1 |
|
} |
197
|
|
|
|
198
|
1 |
|
private function setupClaimsIndexes() { |
199
|
1 |
|
foreach( MongoDBDocumentBuilder::$SUPPORTED_ENTITY_TYPES as $entityType ) { |
200
|
1 |
|
$collection = $this->database->selectCollection( $entityType ); |
201
|
|
|
|
202
|
1 |
|
foreach( MongoDBDocumentBuilder::$SUPPORTED_DATAVALUE_TYPES as $dataValueType ) { |
203
|
1 |
|
$key = 'sclaims.' . $dataValueType; |
204
|
1 |
|
$collection->ensureIndex( |
205
|
1 |
|
[ $key => 1 ], |
206
|
1 |
|
[ 'sparse' => true, 'socketTimeoutMS' => -1 ] |
207
|
1 |
|
); |
208
|
1 |
|
} |
209
|
1 |
|
} |
210
|
1 |
|
} |
211
|
|
|
} |
212
|
|
|
|