Completed
Push — add/sync-rest-2 ( 1b25af...658618 )
by
unknown
09:31
created

Jetpack_Sync_Queue_Buffer::get_item_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * A buffer of items from the queue that can be checked out
5
 */
6
class Jetpack_Sync_Queue_Buffer {
7
	public $id;
8
	public $items_with_ids;
9
10
	public function __construct( $items_with_ids ) {
11
		$this->id             = uniqid();
12
		$this->items_with_ids = $items_with_ids;
13
	}
14
15
	public function get_items() {
16
		return array_map( array( $this, 'get_item_value' ), $this->items_with_ids );
17
	}
18
19
	public function get_item_ids() {
20
		return array_map( array( $this, 'get_item_id' ), $this->items_with_ids );
21
	}
22
23
	private function get_item_value( $item ) {
24
		return $item->value;
25
	}
26
27
	private function get_item_id( $item ) {
28
		return $item->id;
29
	}
30
}
31
32
/**
33
 * A persistent queue that can be flushed in increments of N items,
34
 * and which blocks reads until checked-out buffers are checked in or
35
 * closed. This uses raw SQL for two reasons: speed, and not triggering
36
 * tons of added_option callbacks.
37
 */
38
class Jetpack_Sync_Queue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
39
	public $id;
40
	private $checkout_size;
41
	private $row_iterator;
42
43
	function __construct( $id, $checkout_size = 10 ) {
44
		$this->id            = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL
45
		$this->checkout_size = $checkout_size;
46
		$this->row_iterator  = 0;
47
	}
48
49
	function add( $item ) {
50
		global $wpdb;
51
		$added = false;
52
		// this basically tries to add the option until enough time has elapsed that
53
		// it has a unique (microtime-based) option key
54
		while ( ! $added ) {
55
			$rows_added = $wpdb->query( $wpdb->prepare(
56
				"INSERT INTO $wpdb->options (option_name, option_value) VALUES (%s, %s)",
57
				$this->get_next_data_row_option_name(),
58
				serialize( $item )
59
			) );
60
			$added      = ( $rows_added !== 0 );
61
		}
62
	}
63
64
	// Attempts to insert all the items in a single SQL query. May be subject to query size limits!
65
	function add_all( $items ) {
66
		global $wpdb;
67
		$base_option_name = $this->get_next_data_row_option_name();
68
69
		$query = "INSERT INTO $wpdb->options (option_name, option_value) VALUES ";
70
71
		$rows = array();
72
73
		for ( $i = 0; $i < count( $items ); $i += 1 ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
74
			$option_name  = esc_sql( $base_option_name . '-' . $i );
75
			$option_value = esc_sql( serialize( $items[ $i ] ) );
76
			$rows[]       = "('$option_name', '$option_value')";
77
		}
78
79
		$rows_added = $wpdb->query( $query . join( ',', $rows ) );
80
81
		if ( $rows_added !== count( $items ) ) {
82
			return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" );
83
		}
84
	}
85
86
	// Peek at the front-most item on the queue without checking it out
87
	function peek( $count = 1 ) {
88
		$items = $this->fetch_items( $count );
89
		if ( $items ) {
90
			return array_map( function ( $item ) {
91
				return $item->value;
92
			}, $items );
93
		}
94
95
		return array();
96
	}
97
98
	function reset() {
99
		global $wpdb;
100
		$this->delete_checkout_id();
101
		$wpdb->query( $wpdb->prepare(
102
			"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%"
103
		) );
104
	}
105
106
	function size() {
107
		global $wpdb;
108
109
		return $wpdb->get_var( $wpdb->prepare(
110
			"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%"
111
		) );
112
	}
113
114
	// we use this peculiar implementation because it's much faster than count(*)
115
	function has_any_items() {
116
		global $wpdb;
117
		$value = $wpdb->get_var( $wpdb->prepare(
118
			"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jetpack_sync_queue_{$this->id}-%"
119
		) );
120
121
		return ( $value === "1" );
122
	}
123
124
	function checkout() {
125
		if ( $this->get_checkout_id() ) {
126
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
127
		}
128
129
		$items = $this->fetch_items( $this->checkout_size );
130
131
		if ( count( $items ) === 0 ) {
132
			return false;
133
		}
134
135
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $this->checkout_size ) );
136
137
		$result = $this->set_checkout_id( $buffer->id );
138
139
		if ( ! $result || is_wp_error( $result ) ) {
140
			error_log( "badness setting checkout ID (this should not happen)" );
141
142
			return $result;
143
		}
144
145
		return $buffer;
146
	}
147
148
	function checkin( $buffer ) {
149
		$is_valid = $this->validate_checkout( $buffer );
150
151
		if ( is_wp_error( $is_valid ) ) {
152
			return $is_valid;
153
		}
154
155
		$this->delete_checkout_id();
156
157
		return true;
158
	}
159
160
	function close( $buffer ) {
161
		$is_valid = $this->validate_checkout( $buffer );
162
163
		if ( is_wp_error( $is_valid ) ) {
164
			return $is_valid;
165
		}
166
167
		$this->delete_checkout_id();
168
169
		global $wpdb;
170
171
		// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause
172
		$ids_to_remove = $buffer->get_item_ids();
173
		if ( count( $ids_to_remove ) > 0 ) {
174
			$sql   = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')';
175
			$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) );
176
			$wpdb->query( $query );
177
		}
178
179
		return true;
180
	}
181
182
	function flush_all() {
183
		$items = array_map( function ( $item ) {
184
			return $item->value;
185
		}, $this->fetch_items() );
186
		$this->reset();
187
188
		return $items;
189
	}
190
191
	function get_all() {
192
		return $this->fetch_items();
193
	}
194
195
	function set_checkout_size( $new_size ) {
196
		$this->checkout_size = $new_size;
197
	}
198
199
	private function get_checkout_id() {
200
		return get_option( $this->get_checkout_option_name(), false );
201
	}
202
203
	private function set_checkout_id( $checkout_id ) {
204
		$added = add_option( $this->get_checkout_option_name(), $checkout_id, null, true ); // this one we should autoload
205
		if ( ! $added ) {
206
			return new WP_Error( 'buffer_mismatch', 'Another buffer is already checked out: ' . $this->get_checkout_id() );
207
		} else {
208
			return true;
209
		}
210
	}
211
212
	private function delete_checkout_id() {
213
		delete_option( $this->get_checkout_option_name() );
214
	}
215
216
	private function get_checkout_option_name() {
217
		return "jetpack_sync_queue_{$this->id}-checkout";
218
	}
219
220
	private function get_next_data_row_option_name() {
221
		// this option is specifically chosen to, as much as possible, preserve time order
222
		// and minimise the possibility of collisions between multiple processes working 
223
		// at the same time
224
		// TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
225
		// @see: http://php.net/manual/en/function.microtime.php
226
		$timestamp = sprintf( '%.6f', microtime( true ) );
227
228
		// row iterator is used to avoid collisions where we're writing data waaay fast in a single process
229
		if ( $this->row_iterator === PHP_INT_MAX ) {
230
			$this->row_iterator = 0;
231
		} else {
232
			$this->row_iterator += 1;
233
		}
234
235
		return 'jetpack_sync_queue_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator;
236
	}
237
238
	private function fetch_items( $limit = null ) {
239
		global $wpdb;
240
241
		if ( $limit ) {
242
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jetpack_sync_queue_{$this->id}-%", $limit );
243
		} else {
244
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jetpack_sync_queue_{$this->id}-%" );
245
		}
246
247
		$items = $wpdb->get_results( $query_sql );
248
		foreach ( $items as $item ) {
249
			$item->value = maybe_unserialize( $item->value );
250
		}
251
252
		return $items;
253
	}
254
255
	private function validate_checkout( $buffer ) {
256
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
257
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
258
		}
259
260
		$checkout_id = $this->get_checkout_id();
261
262
		if ( ! $checkout_id ) {
263
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
264
		}
265
266
		if ( $checkout_id != $buffer->id ) {
267
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
268
		}
269
270
		return true;
271
	}
272
}
273