summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/CachingSemanticDataLookup.php
blob: 3826ed82111f7496925c8c4b5a13e5a483dab88b (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
<?php

namespace SMW\SQLStore\EntityStore;

use Onoi\Cache\Cache;
use Onoi\Cache\NullCache;
use Psr\Log\LoggerAwareTrait;
use RuntimeException;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\RequestOptions;
use SMW\SemanticData;
use SMW\SQLStore\PropertyTableDefinition;
use SMWDataItem as DataItem;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class CachingSemanticDataLookup {

	use LoggerAwareTrait;

	/**
	 * @var SemanticDataLookup
	 */
	private $semanticDataLookup;

	/**
	 * @var Cache
	 */
	private $cache;

	/**
	 * Cache for SemanticData dataItems, indexed by SMW ID.
	 *
	 * @var array
	 */
	private static $data = [];

	/**
	 * Like SMWSQLStore3::data, but containing flags indicating
	 * completeness of the SemanticData objs.
	 *
	 * @var array
	 */
	private static $state = [];

	/**
	 * >0 while getSemanticData runs, used to prevent nested calls from clearing
	 * the cache while another call runs and is about to fill it with data
	 *
	 * @var int
	 */
	private static $lookupCount = 0;

	/**
	 * @since 3.0
	 *
	 * @param SemanticDataLookup $semanticDataLookup
	 * @param Cache|null $cache
	 */
	public function __construct( SemanticDataLookup $semanticDataLookup, Cache $cache = null ) {
		$this->semanticDataLookup = $semanticDataLookup;
		$this->cache = $cache;

		if ( $this->cache === null ) {
			$this->cache = new NullCache();
		}
	}

	/**
	 * @since 3.0
	 */
	public function lockCache() {
		self::$lookupCount++;
	}

	/**
	 * @since 3.0
	 */
	public function unlockCache() {
		self::$lookupCount--;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $id
	 */
	public function invalidateCache( $id ) {
		unset( self::$data[$id] );
		unset( self::$state[$id] );
	}

	/**
	 * @since 3.0
	 */
	public static function clear() {
		self::$data = [];
		self::$state = [];
		self::$lookupCount = 0;
	}

	/**
	 * Helper method to make sure there is a cache entry for the data about
	 * the given subject with the given ID.
	 *
	 * @todo The management of this cache should be revisited.
	 *
	 * @since 3.0
	 *
	 * @param int $id
	 * @param DIWikiPage $subject
	 */
	public function initLookupCache( $id, DIWikiPage $subject ) {

		// *** Prepare the cache ***//
		if ( !isset( self::$data[$id] ) ) {
			self::$data[$id] = $this->semanticDataLookup->newStubSemanticData( $subject );
			self::$state[$id] = [];
		}

		// Issue #622
		// If a redirect was cached preceding this request and points to the same
		// subject id ensure that in all cases the requested subject matches with
		// the selected DB id
		if ( self::$data[$id]->getSubject()->getHash() !== $subject->getHash() ) {
			self::$data[$id] = $this->semanticDataLookup->newStubSemanticData( $subject );
			self::$state[$id] = [];
		}

		// It is not so easy to find the sweet spot between cache size and
		// performance gains (both memory and time), The value of 20 was chosen
		// by profiling runtimes for large inline queries and heavily annotated
		// pages. However, things might have changed in the meantime ...
		if ( ( count( self::$data ) > 20 ) && ( self::$lookupCount == 1 ) ) {
			self::$data = [ $id => self::$data[$id] ];
			self::$state = [ $id => self::$state[$id] ];
		}
	}

	/**
	 * Set the semantic data lookup cache to hold exactly the given value for the
	 * given ID.
	 *
	 * @since 3.0
	 *
	 * @param integer $id
	 * @param SemanticData $semanticData
	 */
	public function setLookupCache( $id, SemanticData $semanticData ) {

		self::$data[$id] = $this->semanticDataLookup->newStubSemanticData(
			$semanticData
		);

		self::$state[$id] = $this->semanticDataLookup->getTableUsageInfo(
			$semanticData
		);
	}

	/**
	 * Helper method to make sure there is a cache entry for the data about
	 * the given subject with the given ID.
	 *
	 * @since 3.0
	 *
	 * @param int $id
	 * @param DIWikiPage $subject
	 */
	public function getSemanticDataById( $id ) {

		if ( !isset( self::$data[$id] ) ) {
			throw new RuntimeException( 'Data are not initialized.' );
		}

		return self::$data[$id];
	}

	/**
	 * @since 3.0
	 *
	 * @param PropertyTableDefinition $propertyTableDef
	 * @param DIProperty $property
	 * @param RequestOptions|null $requestOptions
	 *
	 * @return RequestOptions|null
	 */
	public function newRequestOptions( PropertyTableDefinition $propertyTableDef, DIProperty $property, RequestOptions $requestOptions = null ) {
		return $this->semanticDataLookup->newRequestOptions( $propertyTableDef, $property, $requestOptions );
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $id
	 * @param DataItem $dataItem
	 * @param PropertyTableDefinition $propertyTableDef
	 * @param RequestOptions $requestOptions
	 *
	 * @return RequestOptions|null
	 */
	public function fetchSemanticData( $id, DataItem $dataItem = null, PropertyTableDefinition $propertyTableDef, RequestOptions $requestOptions = null ) {
		return $this->semanticDataLookup->fetchSemanticData( $id, $dataItem, $propertyTableDef, $requestOptions );
	}

	/**
	 * Fetch and cache the data about one subject for one particular table
	 *
	 * @param integer $id
	 * @param DIWikiPage $subject
	 * @param PropertyTableDefinition $propertyTableDef
	 * @param RequestOptions|null $requestOptions
	 *
	 * @return SemanticData
	 */
	public function getSemanticDataFromTable( $id, DataItem $dataItem = null, PropertyTableDefinition $propertyTableDef, RequestOptions $requestOptions = null ) {

		// Avoid the cache when a request is constrainted
		if ( $requestOptions !== null || !$dataItem instanceof DIWikiPage ) {
			return $this->semanticDataLookup->getSemanticData( $id, $dataItem, $propertyTableDef, $requestOptions );
		}

		return $this->fetchFromCache( $id, $dataItem, $propertyTableDef );
	}

	/**
	 * @since 3.0
	 *
	 * @param DIWikiPage $subject
	 *
	 * @return StubSemanticData
	 */
	public function newStubSemanticData( DIWikiPage $subject ) {
		return $this->semanticDataLookup->newStubSemanticData( $subject );
	}

	private function fetchFromCache( $id, DataItem $dataItem = null, PropertyTableDefinition $propertyTableDef ) {

		// Do not clear the cache when called recursively.
		$this->lockCache();
		$this->initLookupCache( $id, $dataItem );

		// @see also setLookupCache
		$name = $propertyTableDef->getName();

		if ( isset( self::$state[$id][$name] ) ) {
			$this->unlockCache();
			return self::$data[$id];
		}

		$data = $this->semanticDataLookup->fetchSemanticData(
			$id,
			$dataItem,
			$propertyTableDef
		);

		foreach ( $data as $d ) {
			self::$data[$id]->addPropertyStubValue( reset( $d ), end( $d ) );
		}

		self::$state[$id][$name] = true;

		$this->unlockCache();

		return self::$data[$id];
	}

}