aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/ElggVolatileMetadataCache.php
blob: 4acda7cee5e6ad5ab34fd4f40dfca113e5264f44 (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
<?php
/**
 * ElggVolatileMetadataCache
 * In memory cache of known metadata values stored by entity.
 *
 * @package    Elgg.Core
 * @subpackage Cache
 *
 * @access private
 */
class ElggVolatileMetadataCache {

	/**
	 * The cached values (or null for known to be empty). If the portion of the cache
	 * is synchronized, missing values are assumed to indicate that values do not
	 * exist in storage, otherwise, we don't know what's there.
	 *
	 * @var array
	 */
	protected $values = array();

	/**
	 * Does the cache know that it contains all names fetch-able from storage?
	 * The keys are entity GUIDs and either the value exists (true) or it's not set.
	 *
	 * @var array
	 */
	protected $isSynchronized = array();

	/**
	 * @var null|bool
	 */
	protected $ignoreAccess = null;

	/**
	 * Cache metadata for an entity
	 * 
	 * @param int   $entity_guid The GUID of the entity
	 * @param array $values      The metadata values to cache
	 * @return void
	 */
	public function saveAll($entity_guid, array $values) {
		if (!$this->getIgnoreAccess()) {
			$this->values[$entity_guid] = $values;
			$this->isSynchronized[$entity_guid] = true;
		}
	}

	/**
	 * Get the metadata for an entity
	 * 
	 * @param int $entity_guid The GUID of the entity
	 * @return array
	 */
	public function loadAll($entity_guid) {
		if (isset($this->values[$entity_guid])) {
			return $this->values[$entity_guid];
		} else {
			return array();
		}
	}

	/**
	 * Declare that there may be fetch-able metadata names in storage that this
	 * cache doesn't know about
	 *
	 * @param int $entity_guid The GUID of the entity
	 * @return void
	 */
	public function markOutOfSync($entity_guid) {
		unset($this->isSynchronized[$entity_guid]);
	}

	/**
	 * Have all the metadata for this entity been cached?
	 * 
	 * @param int $entity_guid The GUID of the entity
	 * @return bool
	 */
	public function isSynchronized($entity_guid) {
		return isset($this->isSynchronized[$entity_guid]);
	}

	/**
	 * Cache a piece of metadata
	 * 
	 * @param int                   $entity_guid    The GUID of the entity
	 * @param string                $name           The metadata name
	 * @param array|int|string|null $value          The metadata value. null means it is 
	 *                                              known that there is no fetch-able 
	 *                                              metadata under this name
	 * @param bool                  $allow_multiple Can the metadata be an array
	 * @return void
	 */
	public function save($entity_guid, $name, $value, $allow_multiple = false) {
		if ($this->getIgnoreAccess()) {
			// we don't know if what gets saves here will be available to user once
			// access control returns, hence it's best to forget :/
			$this->markUnknown($entity_guid, $name);
		} else {
			if ($allow_multiple) {
				if ($this->isKnown($entity_guid, $name)) {
					$existing = $this->load($entity_guid, $name);
					if ($existing !== null) {
						$existing = (array) $existing;
						$existing[] = $value;
						$value = $existing;
					}
				} else {
					// we don't know whether there are unknown values, so it's
					// safest to leave that assumption
					$this->markUnknown($entity_guid, $name);
					return;
				}
			}
			$this->values[$entity_guid][$name] = $value;
		}
	}

	/**
	 * Warning: You should always call isKnown() beforehand to verify that this
	 * function's return value should be trusted (otherwise a null return value
	 * is ambiguous).
	 *
	 * @param int    $entity_guid The GUID of the entity
	 * @param string $name        The metadata name
	 * @return array|string|int|null null = value does not exist
	 */
	public function load($entity_guid, $name) {
		if (isset($this->values[$entity_guid]) && array_key_exists($name, $this->values[$entity_guid])) {
			return $this->values[$entity_guid][$name];
		} else {
			return null;
		}
	}

	/**
	 * Forget about this metadata entry. We don't want to try to guess what the
	 * next fetch from storage will return
	 *
	 * @param int    $entity_guid The GUID of the entity
	 * @param string $name        The metadata name
	 * @return void
	 */
	public function markUnknown($entity_guid, $name) {
		unset($this->values[$entity_guid][$name]);
		$this->markOutOfSync($entity_guid);
	}

	/**
	 * If true, load() will return an accurate value for this name
	 *
	 * @param int    $entity_guid The GUID of the entity
	 * @param string $name        The metadata name
	 * @return bool
	 */
	public function isKnown($entity_guid, $name) {
		if (isset($this->isSynchronized[$entity_guid])) {
			return true;
		} else {
			return (isset($this->values[$entity_guid]) && array_key_exists($name, $this->values[$entity_guid]));
		}

	}

	/**
	 * Declare that metadata under this name is known to be not fetch-able from storage
	 *
	 * @param int    $entity_guid The GUID of the entity
	 * @param string $name        The metadata name
	 * @return array
	 */
	public function markEmpty($entity_guid, $name) {
		$this->values[$entity_guid][$name] = null;
	}

	/**
	 * Forget about all metadata for an entity
	 *
	 * @param int $entity_guid The GUID of the entity
	 * @return void
	 */
	public function clear($entity_guid) {
		$this->values[$entity_guid] = array();
		$this->markOutOfSync($entity_guid);
	}

	/**
	 * Clear entire cache and mark all entities as out of sync
	 * 
	 * @return void
	 */
	public function flush() {
		$this->values = array();
		$this->isSynchronized = array();
	}

	/**
	 * Use this value instead of calling elgg_get_ignore_access(). By default that
	 * function will be called.
	 *
	 * This setting makes this component a little more loosely-coupled.
	 *
	 * @param bool $ignore Whether to ignore access or not
	 * @return void
	 */
	public function setIgnoreAccess($ignore) {
		$this->ignoreAccess = (bool) $ignore;
	}

	/**
	 * Tell the cache to call elgg_get_ignore_access() to determing access status.
	 * 
	 * @return void
	 */
	public function unsetIgnoreAccess() {
		$this->ignoreAccess = null;
	}

	/**
	 * Get the ignore access value
	 * 
	 * @return bool
	 */
	protected function getIgnoreAccess() {
		if (null === $this->ignoreAccess) {
			return elgg_get_ignore_access();
		} else {
			return $this->ignoreAccess;
		}
	}

	/**
	 * Invalidate based on options passed to the global *_metadata functions
	 *
	 * @param string $action  Action performed on metadata. "delete", "disable", or "enable"
	 * @param array  $options Options passed to elgg_(delete|disable|enable)_metadata
	 *                         "guid" if given, invalidation will be limited to this entity
	 *                         "metadata_name" if given, invalidation will be limited to metadata with this name
	 * @return void
	 */
	public function invalidateByOptions($action, array $options) {
		// remove as little as possible, optimizing for common cases
		if (empty($options['guid'])) {
			// safest to clear everything unless we want to make this even more complex :(
			$this->flush();
		} else {
			if (empty($options['metadata_name'])) {
				// safest to clear the whole entity
				$this->clear($options['guid']);
			} else {
				switch ($action) {
					case 'delete':
						$this->markEmpty($options['guid'], $options['metadata_name']);
						break;
					default:
						$this->markUnknown($options['guid'], $options['metadata_name']);
				}
			}
		}
	}

	/**
	 * Populate the cache from a set of entities
	 * 
	 * @param int|array $guids Array of or single GUIDs
	 * @return void
	 */
	public function populateFromEntities($guids) {
		if (empty($guids)) {
			return;
		}
		if (!is_array($guids)) {
			$guids = array($guids);
		}
		$guids = array_unique($guids);

		// could be useful at some point in future
		//$guids = $this->filterMetadataHeavyEntities($guids);

		$db_prefix = elgg_get_config('dbprefix');
		$options = array(
			'guids' => $guids,
			'limit' => 0,
			'callback' => false,
			'joins' => array(
				"JOIN {$db_prefix}metastrings v ON n_table.value_id = v.id",
				"JOIN {$db_prefix}metastrings n ON n_table.name_id = n.id",
			),
			'selects' => array('n.string AS name', 'v.string AS value'),
			'order_by' => 'n_table.entity_guid, n_table.time_created ASC',

			// @todo don't know why this is necessary
			'wheres' => array(get_access_sql_suffix('n_table')),
		);
		$data = elgg_get_metadata($options);

		// build up metadata for each entity, save when GUID changes (or data ends)
		$last_guid = null;
		$metadata = array();
		$last_row_idx = count($data) - 1;
		foreach ($data as $i => $row) {
			$name = $row->name;
			$value = ($row->value_type === 'text') ? $row->value : (int) $row->value;
			$guid = $row->entity_guid;
			if ($guid !== $last_guid) {
				if ($last_guid) {
					$this->saveAll($last_guid, $metadata);
				}
				$metadata = array();
			}
			if (isset($metadata[$name])) {
				$metadata[$name] = (array) $metadata[$name];
				$metadata[$name][] = $value;
			} else {
				$metadata[$name] = $value;
			}
			if (($i == $last_row_idx)) {
				$this->saveAll($guid, $metadata);
			}
			$last_guid = $guid;
		}
	}

	/**
	 * Filter out entities whose concatenated metadata values (INTs casted as string)
	 * exceed a threshold in characters. This could be used to avoid overpopulating the
	 * cache if RAM usage becomes an issue.
	 *
	 * @param array $guids GUIDs of entities to examine
	 * @param int   $limit Limit in characters of all metadata (with ints casted to strings)
	 * @return array
	 */
	public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
		$db_prefix = elgg_get_config('dbprefix');

		$options = array(
			'guids' => $guids,
			'limit' => 0,
			'callback' => false,
			'joins' => "JOIN {$db_prefix}metastrings v ON n_table.value_id = v.id",
			'selects' => array('SUM(LENGTH(v.string)) AS bytes'),
			'order_by' => 'n_table.entity_guid, n_table.time_created ASC',
			'group_by' => 'n_table.entity_guid',
		);
		$data = elgg_get_metadata($options);
		// don't cache if metadata for entity is over 10MB (or rolled INT)
		foreach ($data as $row) {
			if ($row->bytes > $limit || $row->bytes < 0) {
				array_splice($guids, array_search($row->entity_guid, $guids), 1);
			}
		}
		return $guids;
	}
}