summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/CachedPropertyValuesPrefetcher.php
blob: 873a887fd64e917e4747b609ba820d772c574011 (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
<?php

namespace SMW;

use Onoi\BlobStore\BlobStore;
use SMWQuery as Query;

/**
 * This class should be accessed via ApplicationFactory::getCachedPropertyValuesPrefetcher
 * to ensure a singleton instance.
 *
 * The purpose of this class is to give fragmented access to frequent (hence
 * cacheable) property values to ensure that the store is only used for when a
 * match can not be found and so freeing up the capacities that can equally be
 * served from a persistent cache instance.
 *
 * It is expected that as soon as the "on.before.semanticdata.update.complete"
 * event has been emitted that matchable cache entries are purged for the
 * subject in question.
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class CachedPropertyValuesPrefetcher {

	/**
	 * @var string
	 */
	const VERSION = '0.4.1';

	/**
	 * Namespace occupied by the BlobStore
	 */
	const CACHE_NAMESPACE = 'smw:pvp:store';

	/**
	 * @var Store
	 */
	private $store;

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

	/**
	 * @var boolean
	 */
	private $disableCache = false;

	/**
	 * @since 2.4
	 *
	 * @param Store $store
	 * @param BlobStore $blobStore
	 */
	public function __construct( Store $store, BlobStore $blobStore ) {
		$this->store = $store;
		$this->blobStore = $blobStore;
	}

	/**
	 * @since 2.4
	 */
	public function resetCacheBy( DIWikiPage $subject ) {
		$this->blobStore->delete( $this->getRootHashFrom( $subject ) );
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $disableCache
	 */
	public function disableCache( $disableCache ) {
		$this->disableCache = (bool)$disableCache;
	}

	/**
	 * @since 2.4
	 *
	 * @param DIWikiPage $subject
	 * @param DIProperty $property
	 * @param RequestOptions|null $requestOptions
	 *
	 * @return array
	 */
	public function getPropertyValues( DIWikiPage $subject, DIProperty $property, RequestOptions $requestOptions = null ) {

		// Items are collected as part of the subject hash so that any request is
		// stored with that entity identifier allowing it to be evicted entirely
		// when the subject is changed.
		//
		// The key on the other hand represent an individual request identifier
		// that is stored as part of the overall cache item but making distinct
		// requests possible, yet is fetched as part of the overall subject to
		// minimize cache fragmentation and a better eviction strategy.
		$key = $property->getKey() .
			':' . $subject->getSubobjectName() .
			':' . ( $requestOptions !== null ? md5( $requestOptions->getHash() ) : null );

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

		if ( $this->disableCache === false && $container->has( $key ) ) {
			return $container->get( $key );
		}

		$dataItems = $this->store->getPropertyValues(
			$subject,
			$property,
			$requestOptions
		);

		$container->set( $key, $dataItems );

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

		return $dataItems;
	}

	/**
	 * @since 2.4
	 *
	 * @param Query $query
	 *
	 * @return array
	 */
	public function queryPropertyValuesFor( Query $query ) {
		return $this->store->getQueryResult( $query )->getResults();
	}

	/**
	 * @since 2.4
	 *
	 * @return BlobStore
	 */
	public function getBlobStore() {
		return $this->blobStore;
	}

	/**
	 * @since 2.4
	 *
	 * @return Store
	 */
	public function getStore() {
		return $this->store;
	}

	/**
	 * @since 2.4
	 *
	 * @param DIWikiPage $subject
	 *
	 * @return string
	 */
	public function getRootHashFrom( DIWikiPage $subject ) {
		return md5( $subject->asBase()->getHash() . self::VERSION );
	}

	/**
	 * @since 2.4
	 *
	 * @param string $hash
	 *
	 * @return string
	 */
	public function createHashFromString( $hash ) {
		return md5( $hash . self::VERSION );
	}

}