summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/CachingEntityLookup.php
blob: a94e28a8d82ac9f284241d6167bfddf463929191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php

namespace SMW\SQLStore\EntityStore;

use Onoi\BlobStore\BlobStore;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\EntityLookup;
use SMW\HashBuilder;
use SMW\Localizer;
use SMW\RequestOptions;
use SMW\SemanticData;
use SMW\SQLStore\Lookup\RedirectTargetLookup;
use SMWDataItem as DataItem;

/**
 * Intermediary (fast) access to serialized blob values to avoid DB access on
 * objects that are static until it is altered. An intermediary object will
 * invalidate itself on any delete, update, change, or move operation.
 *
 * Each subject (including its subobjects) will be stored as individual blob with
 * each operation belonging to that subject extending its blob to be able
 * to discard the entire entity at once.
 *
 * Each operation request will either fill the cache or return the result from
 * the cache until the subject is changed and the whole container is being
 * flushed.
 *
 * The class could be decorator but due to the nature of the current Store design
 * it is called from within each method. The class is not for public use and it
 * is expected to be accessed only by a Store operation.
 *
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class CachingEntityLookup implements EntityLookup {

	/**
	 * Update this version number when the serialization format
	 * changes.
	 */
	const VERSION = '1.1';

	/**
	 * @var EntityLookup
	 */
	private $entityLookup;

	/**
	 * @var RedirectTargetLookup
	 */
	private $redirectTargetLookup;

	/**
	 * @var BlobStore
	 */
	private $blobStore;

	/**
	 * @var integer
	 */
	private $lookupFeatures = 0;

	/**
	 * @since 2.3
	 *
	 * @param EntityLookup $entityLookup
	 * @param RedirectTargetLookup $redirectTargetLookup
	 * @param BlobStore $blobStore
	 */
	public function __construct( EntityLookup $entityLookup, RedirectTargetLookup $redirectTargetLookup, BlobStore $blobStore ) {
		$this->entityLookup = $entityLookup;
		$this->redirectTargetLookup = $redirectTargetLookup;
		$this->blobStore = $blobStore;
	}

	/**
	 * @since 2.3
	 *
	 * @param integer $lookupFeatures
	 */
	public function setLookupFeatures( $lookupFeatures ) {
		$this->lookupFeatures = $lookupFeatures;
	}

	/**
	 * @since 2.3
	 *
	 * @param integer $lookupFeatures
	 *
	 * @return boolean
	 */
	public function isEnabledFeature( $lookupFeatures ) {
		return $this->lookupFeatures === ( $this->lookupFeatures | $lookupFeatures );
	}

	/**
	 * @see EntityLookup::getSemanticData
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getSemanticData( DIWikiPage $subject, $filter = false ) {

		if ( !$this->blobStore->canUse() || !$this->isEnabledFeature( SMW_VL_SD ) ) {
			return $this->entityLookup->getSemanticData( $subject, $filter );
		}

		// Use a separate container otherwise large serializations of subobjects
		// will decrease performance when combined with other lists
		$sid = $this->getHashFrom(
			$subject,
			$subject->getSubobjectName()
		);

		$container = $this->blobStore->read( $sid );

		// Make sure that when switching user languages, user labels etc.
		// are appropriately generated
		$userLang = Localizer::getInstance()->getUserLanguage()->getCode();

		$sdid = HashBuilder::createFromContent(
			[
				(array)$filter,
				self::VERSION
			],
			'sd:'. $userLang . ':'
		);

		if ( $container->has( $sdid ) ) {
			return $container->get( $sdid );
		}

		$semanticData = $this->entityLookup->getSemanticData(
			$subject,
			$filter
		);

		$semanticData->setOption(
			SemanticData::OPT_LAST_MODIFIED,
			wfTimestamp( TS_UNIX )
		);

		$container->set( $sdid, $semanticData );

		$this->blobStore->save(
			$container
		);

		$this->appendToList( $sid, $subject );

		return $semanticData;
	}

	/**
	 * @see EntityLookup::getProperties
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getProperties( DIWikiPage $subject, RequestOptions $requestOptions = null ) {

		if ( !$this->blobStore->canUse() || !$this->isEnabledFeature( SMW_VL_PL ) ) {
			return $this->entityLookup->getProperties( $subject, $requestOptions );
		}

		$container = $this->blobStore->read(
			$this->getHashFrom( $subject )
		);

		$plid = HashBuilder::createFromContent(
			[
				$subject->getSubobjectName(),
				(array)$requestOptions,
				self::VERSION
			],
			'pl:'
		);

		if ( $container->has( $plid ) ) {
			return $this->resolveRedirectTargets( $container->get( $plid ) );
		}

		$result = $this->entityLookup->getProperties(
			$subject,
			$requestOptions
		);

		$container->set( $plid, $result );

		$this->blobStore->save(
			$container
		);

		return $result;
	}

	/**
	 * @see EntityLookup::getPropertyValues
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getPropertyValues( DIWikiPage $subject = null, DIProperty $property, RequestOptions $requestOptions = null ) {

		// The cache is not used for $subject === null (means all values for
		// the given property are returned)
		if ( $subject === null || !$this->blobStore->canUse() || !$this->isEnabledFeature( SMW_VL_PV ) ) {
			return $this->entityLookup->getPropertyValues( $subject, $property, $requestOptions );
		}

		// Too many subobjects in one list can kill the performance therefore split
		// the container by subobject
		$sid = $this->getHashFrom(
			$subject,
			$subject->getSubobjectName()
		);

		$container = $this->blobStore->read( $sid );

		$pvid = HashBuilder::createFromContent(
			[
				$property->getKey(),
				$property->isInverse(),
				(array)$requestOptions,
				self::VERSION
			],
			'pv:'
		);

		if ( $container->has( $pvid ) ) {
			return $this->resolveRedirectTargets( $container->get( $pvid ) );
		}

		$result = $this->entityLookup->getPropertyValues(
			$subject,
			$property,
			$requestOptions
		);

		// Returns a possible iterator, avoid "Serialization of 'Closure' is not allowed"
		if ( $result instanceof \Iterator ) {
			$result = iterator_to_array( $result );
		}

		$container->set( $pvid, $result );

		$this->blobStore->save(
			$container
		);

		$this->appendToList( $sid, $subject );

		return $result;
	}

	/**
	 * @see EntityLookup::getPropertySubjects
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getPropertySubjects( DIProperty $property, DataItem $dataItem = null, RequestOptions $requestOptions = null ) {

		// The cache is not used for $dataItem === null (means all values for
		// the given property are returned)
		if ( $dataItem === null || !$dataItem instanceof DIWikiPage || !$this->blobStore->canUse() || !$this->isEnabledFeature( SMW_VL_PS ) ) {
			return $this->entityLookup->getPropertySubjects( $property, $dataItem, $requestOptions );
		}

		// Added as linked list as we keep the container ttl different from
		// that of the main container
		$sid = $this->getHashFrom(
			$dataItem,
			'ps'
		);

		$container = $this->blobStore->read( $sid );

		$psid = HashBuilder::createFromContent(
			[
				$property->getKey(),
				$property->isInverse(),
				(array)$requestOptions,
				self::VERSION
			],
			'ps:'
		);

		if ( $container->has( $psid ) ) {
			return $container->get( $psid );
		}

		$result = $this->entityLookup->getPropertySubjects(
			$property,
			$dataItem,
			$requestOptions
		);

		// Returns an iterator, avoid "Serialization of 'Closure' is not allowed"
		if ( $result instanceof \Iterator ) {
			$result = iterator_to_array( $result );
		}

		$container->set( $psid, $result );

		// We set a short lifetime (5 min) in order to cache repeated requests but
		// avoiding a complex invalidation during a subject update otherwise all
		// properties of a container would require scanning and removal
		$container->setExpiryInSeconds( 60 * 5 );

		$this->blobStore->save(
			$container
		);

		$this->appendToList( $sid, $dataItem );

		return $result;
	}

	/**
	 * @see EntityLookup::getAllPropertySubjects
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getAllPropertySubjects( DIProperty $property, RequestOptions $requestOptions = null  ) {
		return $this->entityLookup->getAllPropertySubjects( $property, $requestOptions );
	}

	/**
	 * @see EntityLookup::getInProperties
	 *
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getInProperties( DataItem $object, RequestOptions $requestOptions = null ) {
		return $this->entityLookup->getInProperties( $object, $requestOptions );
	}

	/**
	 * Remove a cache item that appears during an alteration action (update,
	 * change, delete) to ensure that we always have the correct set of matches.
	 *
	 * @since 2.3
	 */
	public function invalidateCache() {

		$args = func_get_args();
		$subject = array_shift( $args );

		if ( !$this->blobStore->canUse() || !$subject instanceof DIWikiPage ) {
			return null;
		}

		// Remove a redirect target subject directly
		$redirects = $this->getSemanticData( $subject )->getPropertyValues(
			new DIProperty( '_REDI' )
		);

		foreach ( $redirects as $redirectTarget ) {
			$this->blobStore->delete( $this->getHashFrom(
				$redirectTarget
			) );
		}

		$sid = $this->getHashFrom( $subject );

		// Remove all linked objects
		$container = $this->blobStore->read( $sid );

		if ( $container->has( 'list' ) ) {
			foreach ( array_keys( $container->get( 'list' ) ) as $id ) {
				$this->blobStore->delete( $id );
			}
		}

		$this->blobStore->delete( $sid );
	}

	/**
	 * Ensures that new redirects are resolved while a value is still kept
	 * in cache (internally it uses getPropertyValues hence we don't loose
	 * much as objects are reused during the lookup).
	 */
	private function resolveRedirectTargets( array $results ) {

		$dataItems =  [];

		foreach ( $results as $dataItem ) {
			$dataItems[] = $this->redirectTargetLookup->findRedirectTarget( $dataItem );
		}

		return $dataItems;
	}

	/**
	 * The subobject is attached to a root subject therefore using the root as
	 * identifier to allow it to be invalidated at once with all other subobjects
	 * that relate to a subject
	 */
	private function getHashFrom( DIWikiPage $subject, $suffix = '' ) {
		return md5( HashBuilder::createHashIdFromSegments(
			$subject->getDBkey(),
			$subject->getNamespace(),
			$subject->getInterwiki()
		) . $suffix );
	}

	private function appendToList( $id, $subject ) {

		// Store the id with the main subject
		$container = $this->blobStore->read(
			$this->getHashFrom( $subject )
		);

		// Use the id as key to avoid unnecessary duplicate entries when
		// employing append
		$container->append(
			'list',
			[ $id => true ]
		);

		$this->blobStore->save(
			$container
		);
	}

}