Completed
Push — add/sync-rest-2 ( 85c3b4...1b25af )
by
unknown
24:20 queued 15:11
created

Jetpack_Sync_Queue::set_checkout_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 6
nc 2
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( function( $item ) { return $item->value; }, $this->items_with_ids );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
17
	}
18
19
	public function get_item_ids() {
20
		return array_map( function( $item ) { return $item->id; }, $this->items_with_ids );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
21
	}
22
}
23
24
/**
25
 * A persistent queue that can be flushed in increments of N items,
26
 * and which blocks reads until checked-out buffers are checked in or
27
 * closed. This uses raw SQL for two reasons: speed, and not triggering 
28
 * tons of added_option callbacks.
29
 */
30
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...
31
	public $id;
32
	private $checkout_size;
33
	private $row_iterator;
34
35
	function __construct( $id, $checkout_size = 10 ) {
36
		$this->id = str_replace( '-', '_', $id); // necessary to ensure we don't have ID collisions in the SQL
37
		$this->checkout_size = $checkout_size;
38
		$this->row_iterator = 0;
39
	}
40
41
	function add( $item ) {
42
		global $wpdb;
43
		$added = false;
44
		// this basically tries to add the option until enough time has elapsed that
45
		// it has a unique (microtime-based) option key
46
		while(!$added) {
47
			$rows_added = $wpdb->query( $wpdb->prepare( 
48
				"INSERT INTO $wpdb->options (option_name, option_value) VALUES (%s, %s)", 
49
				$this->get_next_data_row_option_name(), 
50
				serialize($item)
51
			) );
52
			$added = ( $rows_added !== 0 );
53
		}
54
	}
55
56
	// Attempts to insert all the items in a single SQL query. May be subject to query size limits!
57
	function add_all( $items ) {
58
		global $wpdb;
59
		$base_option_name = $this->get_next_data_row_option_name();
60
61
		$query = "INSERT INTO $wpdb->options (option_name, option_value) VALUES ";
62
		
63
		$rows = array();
64
65
		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...
66
			$option_name = esc_sql( $base_option_name.'-'.$i );
67
			$option_value = esc_sql( serialize( $items[ $i ] ) );
68
			$rows[] = "('$option_name', '$option_value')";
69
		}
70
71
		$rows_added = $wpdb->query( $query . join( ',', $rows ) );
72
73
		if ( $rows_added !== count( $items ) ) {
74
			return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" );
75
		}
76
	}
77
78
	// Peek at the front-most item on the queue without checking it out
79
	function peek( $count = 1 ) {
80
		$items = $this->fetch_items( $count );
81
		if ( $items ) {
82
			return array_map( function( $item ) { return $item->value; }, $items );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
83
		} 
84
		return array();
85
	}
86
87
	function reset() {
88
		global $wpdb;
89
		$this->delete_checkout_id();
90
		$wpdb->query( $wpdb->prepare( 
91
			"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%" 
92
		) );
93
	}
94
95
	function size() {
96
		global $wpdb;
97
		return $wpdb->get_var( $wpdb->prepare( 
98
			"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%" 
99
		) );
100
	}
101
102
	// we use this peculiar implementation because it's much faster than count(*)
103
	function has_any_items() {
104
		global $wpdb;
105
		$value = $wpdb->get_var( $wpdb->prepare( 
106
			"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jetpack_sync_queue_{$this->id}-%" 
107
		) );
108
		return ( $value === "1" );
109
	}
110
111
	function checkout() {
112
		if ( $this->get_checkout_id() ) {
113
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
114
		}
115
116
		$items = $this->fetch_items( $this->checkout_size );
117
		
118
		if ( count( $items ) === 0 ) {
119
			return false;
120
		}
121
122
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $this->checkout_size ) );
123
		
124
		$result = $this->set_checkout_id( $buffer->id );
125
126
		if ( !$result || is_wp_error( $result ) ) {
127
			error_log("badness setting checkout ID (this should not happen)");
128
			return $result;
129
		}
130
		
131
		return $buffer;
132
	}
133
134
	function checkin( $buffer ) {
135
		$is_valid = $this->validate_checkout( $buffer );
136
137
		if ( is_wp_error( $is_valid ) ) {
138
			return $is_valid;
139
		}
140
141
		$this->delete_checkout_id();
142
143
		return true;
144
	}
145
146
	function close( $buffer ) {
147
		$is_valid = $this->validate_checkout( $buffer );
148
149
		if ( is_wp_error( $is_valid ) ) {
150
			return $is_valid;
151
		}
152
153
		$this->delete_checkout_id();
154
155
		global $wpdb;
156
157
		// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause
158
		$ids_to_remove = $buffer->get_item_ids();
159
		if ( count( $ids_to_remove ) > 0 ) {
160
			$sql = "DELETE FROM $wpdb->options WHERE option_name IN (".implode(', ', array_fill(0, count($ids_to_remove), '%s')).')';
161
			$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) );
162
			$wpdb->query( $query );
163
		}
164
165
		return true;
166
	}
167
168
	function flush_all() {
169
		$items = array_map( function( $item ) { return $item->value; }, $this->fetch_items() );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
170
		$this->reset();
171
		return $items;
172
	}
173
174
	function get_all() {
175
		return $this->fetch_items();
176
	}
177
178
	function set_checkout_size( $new_size ) {
179
		$this->checkout_size = $new_size;
180
	}
181
182
	private function get_checkout_id() {
183
		return get_option( $this->get_checkout_option_name(), false );
184
	}
185
186
	private function set_checkout_id( $checkout_id ) {
187
		$added = add_option( $this->get_checkout_option_name(), $checkout_id, null, true ); // this one we should autoload
188
		if ( ! $added )
189
			return new WP_Error( 'buffer_mismatch', 'Another buffer is already checked out: '.$this->get_checkout_id() );
190
		else
191
			return true;
192
	}
193
194
	private function delete_checkout_id() {
195
		delete_option( $this->get_checkout_option_name() );
196
	}
197
198
	private function get_checkout_option_name() {
199
		return "jetpack_sync_queue_{$this->id}-checkout";
200
	}
201
202
	private function get_next_data_row_option_name() {
203
		// this option is specifically chosen to, as much as possible, preserve time order
204
		// and minimise the possibility of collisions between multiple processes working 
205
		// at the same time
206
		// 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...
207
		// @see: http://php.net/manual/en/function.microtime.php
208
		$timestamp = sprintf( '%.6f', microtime(true) );
209
		
210
		// row iterator is used to avoid collisions where we're writing data waaay fast in a single process
211
		if ( $this->row_iterator === PHP_INT_MAX ) {
212
			$this->row_iterator = 0;
213
		} else {
214
			$this->row_iterator += 1;
215
		}
216
217
		return 'jetpack_sync_queue_'.$this->id.'-'.$timestamp.'-'.getmypid().'-'.$this->row_iterator;
218
	}
219
220
	private function fetch_items( $limit = null ) {
221
		global $wpdb;
222
223
		if ( $limit ) {
224
			$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 );
225
		} else {
226
			$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}-%" );
227
		}
228
229
		$items = $wpdb->get_results( $query_sql );
230
		foreach( $items as $item ) {
231
			$item->value = maybe_unserialize( $item->value );
232
		} 
233
234
		return $items;
235
	}
236
237
	private function validate_checkout( $buffer ) {
238
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
239
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
240
		}
241
242
		$checkout_id = $this->get_checkout_id();
243
244
		if ( !$checkout_id ) {
245
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
246
		}
247
248
		if ( $checkout_id != $buffer->id ) {
249
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
250
		}
251
252
		return true;
253
	}
254
}