/
ezdbgarbagecollector.php
418 lines (349 loc) · 16 KB
/
ezdbgarbagecollector.php
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* File containing the eZDBGarbageCollector class.
*
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package kernel
*/
/*!
\class eZDBGrbageCollector ezdbgarbagecollector.php
\brief Handles garbage collection in the database.
Cleans up garbage (leaks) in the database which can be left behind by
bugs or crashes in the system.
Garbage collection is divided into several static functions which performs
cleanup of one or more tables. What is common for all functions are the
\a $maxTime and \a $sleepTime parameters which controls how long a GC
operation can go on and how long it should sleep between batch operations.
\code
eZDBGarbageCollector::collectBaskets();
// or
// Perform GC for 5 minutes with 2 second sleep intervals
eZDBGarbageCollector::collectBaskets( 5*60, 2 );
\endcode
*/
class eZDBGarbageCollector
{
/*!
Controls the default value for how many items are cleaned in one batch operation.
*/
const ITEM_LIMIT = 3000;
/*!
\static
Removes all baskets which are missing a session entry.
This usually means the basket was not cleaned up when the session was removed.
\param $maxTime The maximum number of seconds the collector should run.
If set to \c false or \c true it will continue until all garbage is collected,
\c false means to try the fastest way possible (may use lots of resources).
\param $sleepTime Controls how many seconds it will sleep between each batch of cleanup,
setting it to \c false means that it should never sleep.
\param $limit Controls how many items are cleaned in one batch operation,
a value of \c false means to use default value.
\note The function will most likely use a little more time than \a $maxTime so
don't depend on it to be accurate.
\note This will also remove the product collection the basket is using.
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
function collectBaskets( $maxTime = false, $sleepTime = false, $limit = false )
{
$db = eZDB::instance();
if ( $maxTime === false and $db->hasRequiredServerVersion( '4.0', 'mysql' ) )
{
$sql = "DELETE FROM ezbasket, ezproductcollection, ezproductcollection_item, ezproductcollection_item_opt
USING ezsession
RIGHT JOIN ezbasket
ON ezsession.session_key = ezbasket.session_id
LEFT JOIN ezproductcollection
ON ezbasket.productcollection_id = ezproductcollection.id
LEFT JOIN ezproductcollection_item
ON ezproductcollection.id = ezproductcollection_item.productcollection_id
LEFT JOIN ezproductcollection_item_opt
ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id
WHERE ezsession.session_key IS NULL";
$db->query( $sql );
return;
}
// Find all baskets which are lacking a session (db leaks)
if ( $db->hasRequiredServerVersion( '8', 'oracle' ) )
{
$sql = "SELECT id, productcollection_id
FROM ezsession, ezbasket
WHERE ezbasket.session_id = ezsession.session_key (+) AND
ezsession.session_key IS NULL";
}
else
{
$sql = "SELECT id, productcollection_id
FROM ezsession
RIGHT JOIN ezbasket
ON ezbasket.session_id = ezsession.session_key
WHERE ezsession.session_key IS NULL";
}
if ( $limit === false )
$limit = eZDBGarbageCollector::ITEM_LIMIT;
$end = false;
if ( is_numeric( $maxTime ) )
$end = time() + $maxTime;
do
{
$rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
if ( count( $rows ) == 0 )
break;
$productCollectionIDList = array();
$idList = array();
foreach ( $rows as $row )
{
$idList[] = (int)$row['id'];
$productCollectionIDList[] = (int)$row['productcollection_id'];
}
eZProductCollection::cleanupList( $productCollectionIDList );
$ids = implode( ', ', $idList );
$db->query( "DELETE FROM ezbasket WHERE id IN ( $ids )" );
// Stop when we used up our time
if ( $end !== false and time() > $end )
break;
// Sleep a little if required, reduces load on server
if ( $sleepTime !== false )
sleep( $sleepTime );
} while ( true );
}
/*!
Removes entries from ezproductcollection which is missing relations to other tables.
\param $maxTime The maximum number of seconds the collector should run.
If set to \c false or \c true it will continue until all garbage is collected,
\c false means to try the fastest way possible (may use lots of resources).
\param $sleepTime Controls how many seconds it will sleep between each batch of cleanup,
setting it to \c false means that it should never sleep.
\param $limit Controls how many items are cleaned in one batch operation,
a value of \c false means to use default value.
\note The function will most likely use a little more time than \a $maxTime so
don't depend on it to be accurate.
\note The current code hardcodes the related tables.
\warning This code may be very slow on large sites.
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
function collectProductCollections( $maxTime = false, $sleepTime = false, $limit = false )
{
$db = eZDB::instance();
// Create a temporary table for filling in collection ids
// that are in use
if ( $db->databaseName() == 'mysql' )
$db->query( "DROP TABLE IF EXISTS ezproductcollection_used" );
$db->query( "CREATE TABLE ezproductcollection_used ( id int )" );
// Fill in collections used by orders
$db->query( "INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezorder" );
// Fill in collections used by wish lists
$db->query( "INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezwishlist" );
// Fill in collections used by baskets
$db->query( "INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezbasket" );
// Create the index for faster selects
$db->query( "CREATE INDEX ezproductcollection_used_id on ezproductcollection_used( id )" );
if ( $maxTime === false and $db->hasRequiredServerVersion( '4.0', 'mysql' ) )
{
// Delete entries which are not in ezproductcollection_used
$db->query( "DELETE FROM ezproductcollection, ezproductcollection_item, ezproductcollection_item_opt
USING ezproductcollection_used
RIGHT JOIN ezproductcollection
ON ezproductcollection_used.id = ezproductcollection.id
LEFT JOIN ezproductcollection_item
ON ezproductcollection.id = ezproductcollection_item.productcollection_id
LEFT JOIN ezproductcollection_item_opt
ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id
WHERE ezproductcollection_used.id IS NULL" );
$db->query( $sql );
// Remove the temporary table
$db->query( "DROP TABLE ezproductcollection_used" );
return;
}
// Find all product collections which are not in use
if ( $db->hasRequiredServerVersion( '8', 'oracle' ) )
{
$sql = "SELECT ezproductcollection.id
FROM ezproductcollection_used, ezproductcollection
WHERE ezproductcollection_used.id = ezproductcollection.id (+) AND
ezproductcollection_used.id IS NULL";
}
else
{
$sql = "SELECT ezproductcollection.id
FROM ezproductcollection_used
RIGHT JOIN ezproductcollection
ON ezproductcollection_used.id = ezproductcollection.id
WHERE ezproductcollection_used.id IS NULL";
}
if ( $limit === false )
$limit = eZDBGarbageCollector::ITEM_LIMIT;
$end = false;
if ( is_numeric( $maxTime ) )
$end = time() + $maxTime;
do
{
$rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
if ( count( $rows ) == 0 )
break;
$idList = array();
foreach ( $rows as $row )
{
$idList[] = (int)$row['id'];
}
eZProductCollectionItem::cleanupList( $idList );
$ids = implode( ', ', $idList );
$db->query( "DELETE FROM ezproductcollection WHERE id IN ( $ids )" );
$db->query( "DELETE FROM ezproductcollection_used WHERE id IN ( $ids )" );
// Stop when we used up our time
if ( $end !== false and time() > $end )
break;
// Sleep a little if required, reduces load on server
if ( $sleepTime !== false )
sleep( $sleepTime );
} while ( true );
// Remove the temporary table
$db->query( "DROP TABLE ezproductcollection_used" );
}
/*!
\static
Removes all product collection items which are missing a product collection.
\param $maxTime The maximum number of seconds the collector should run.
If set to \c false or \c true it will continue until all garbage is collected,
\c false means to try the fastest way possible (may use lots of resources).
\param $sleepTime Controls how many seconds it will sleep between each batch of cleanup,
setting it to \c false means that it should never sleep.
\param $limit Controls how many items are cleaned in one batch operation,
a value of \c false means to use default value.
\note The function will most likely use a little more time than \a $maxTime so
don't depend on it to be accurate.
\note This will also remove the product collection item option the item is using.
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
function collectProductCollectionItems( $maxTime = false, $sleepTime = false, $limit = false )
{
$db = eZDB::instance();
if ( $maxTime === false and $db->hasRequiredServerVersion( '4.0', 'mysql' ) )
{
$sql = "DELETE FROM ezproductcollection_item, ezproductcollection_item_opt
USING ezproductcollection
LEFT JOIN ezproductcollection_item
ON ezproductcollection.id = ezproductcollection_item.productcollection_id
LEFT JOIN ezproductcollection_item_opt
ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id
WHERE ezproductcollection.id IS NULL";
$db->query( $sql );
return;
}
// Find all items which are lacking a collection (db leaks)
if ( $db->hasRequiredServerVersion( '8', 'oracle' ) )
{
$sql = "SELECT ezproductcollection_item.productcollection_id
FROM ezproductcollection, ezproductcollection_item
WHERE ezproductcollection.id = ezproductcollection_item.productcollection_id (+) AND
ezproductcollection.id IS NULL";
}
else
{
$sql = "SELECT ezproductcollection_item.productcollection_id
FROM ezproductcollection
RIGHT JOIN ezproductcollection_item
ON ezproductcollection.id = ezproductcollection_item.productcollection_id
WHERE ezproductcollection.id IS NULL";
}
if ( $limit === false )
$limit = eZDBGarbageCollector::ITEM_LIMIT;
$end = false;
if ( is_numeric( $maxTime ) )
$end = time() + $maxTime;
do
{
$rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
if ( count( $rows ) == 0 )
break;
$idList = array();
foreach ( $rows as $row )
{
$idList[] = (int)$row['productcollection_id'];
}
eZProductCollectionItemOption::cleanupList( $idList );
$ids = implode( ', ', $idList );
$db->query( "DELETE FROM ezproductcollection_item WHERE productcollection_id IN ( $ids )" );
// Stop when we used up our time
if ( $end !== false and time() > $end )
break;
// Sleep a little if required, reduces load on server
if ( $sleepTime !== false )
sleep( $sleepTime );
} while ( true );
}
/*!
\static
Removes all product collection item options which are missing a product collection item.
\param $maxTime The maximum number of seconds the collector should run.
If set to \c false or \c true it will continue until all garbage is collected,
\c false means to try the fastest way possible (may use lots of resources).
\param $sleepTime Controls how many seconds it will sleep between each batch of cleanup,
setting it to \c false means that it should never sleep.
\param $limit Controls how many items are cleaned in one batch operation,
a value of \c false means to use default value.
\note The function will most likely use a little more time than \a $maxTime so
don't depend on it to be accurate.
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
function collectProductCollectionItemOptions( $maxTime = false, $sleepTime = false, $limit = false )
{
$db = eZDB::instance();
if ( $maxTime === false and $db->hasRequiredServerVersion( '4.0', 'mysql' ) )
{
$sql = "DELETE FROM ezproductcollection_item_opt
USING ezproductcollection_item
LEFT JOIN ezproductcollection_item_opt
ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id
WHERE ezproductcollection_item.id IS NULL";
$db->query( $sql );
return;
}
// Find all items which are lacking a collection (db leaks)
if ( $db->hasRequiredServerVersion( '8', 'oracle' ) )
{
$sql = "SELECT ezproductcollection_item_opt.item_id
FROM ezproductcollection_item, ezproductcollection_item_opt
WHERE ezproductcollection_item.id = ezproductcollection_item_opt.item_id (+) AND
ezproductcollection_item.id IS NULL";
}
else
{
$sql = "SELECT ezproductcollection_item_opt.item_id
FROM ezproductcollection_item
RIGHT JOIN ezproductcollection_item_opt
ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id
WHERE ezproductcollection_item.id IS NULL";
}
if ( $limit === false )
$limit = eZDBGarbageCollector::ITEM_LIMIT;
$end = false;
if ( is_numeric( $maxTime ) )
$end = time() + $maxTime;
do
{
$rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
if ( count( $rows ) == 0 )
break;
$idList = array();
foreach ( $rows as $row )
{
$idList[] = (int)$row['item_id'];
}
$ids = implode( ', ', $idList );
$db->query( "DELETE FROM ezproductcollection_item_opt WHERE item_id IN ( $ids )" );
// Stop when we used up our time
if ( $end !== false and time() > $end )
break;
// Sleep a little if required, reduces load on server
if ( $sleepTime !== false )
sleep( $sleepTime );
} while ( true );
}
}
?>