diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-20 15:09:15 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-20 15:09:15 +0200 |
commit | 381f8efd1096001f34810414c1054a00a64179c8 (patch) | |
tree | a8bd92117dfd7352bfae3e06cacf57287312036a | |
parent | 4d4a27969705b8ff14c139795228d2f05e017b87 (diff) |
set autopurge limit to 200 and purge per feed rather than per user or per all items, fix #98
-rw-r--r-- | businesslayer/itembusinesslayer.php | 10 | ||||
-rw-r--r-- | db/itemmapper.php | 41 | ||||
-rw-r--r-- | dependencyinjection/dicontainer.php | 2 | ||||
-rw-r--r-- | tests/unit/businesslayer/ItemBusinessLayerTest.php | 13 | ||||
-rw-r--r-- | tests/unit/db/ItemMapperTest.php | 81 |
5 files changed, 95 insertions, 52 deletions
diff --git a/businesslayer/itembusinesslayer.php b/businesslayer/itembusinesslayer.php index fd01506a6..d97108431 100644 --- a/businesslayer/itembusinesslayer.php +++ b/businesslayer/itembusinesslayer.php @@ -128,15 +128,7 @@ class ItemBusinessLayer extends BusinessLayer { * old, the id is taken */ public function autoPurgeOld(){ - $readAndNotStarred = - $this->mapper->getReadOlderThanThreshold($this->autoPurgeCount); - - // delete entries with a lower id than last item - if($this->autoPurgeCount > 0 - && isset($readAndNotStarred[$this->autoPurgeCount-1])){ - $this->mapper->deleteReadOlderThanId( - $readAndNotStarred[$this->autoPurgeCount-1]->getId()); - } + $this->mapper->deleteReadOlderThanThreshold($this->autoPurgeCount); } diff --git a/db/itemmapper.php b/db/itemmapper.php index 326d58a2d..225cfb9b9 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -216,25 +216,38 @@ class ItemMapper extends Mapper implements IMapper { } - public function getReadOlderThanThreshold($threshold){ - - // we want items that are not starred and not unread + /** + * Delete all items for feeds that have over $threshold unread and not + * starred items + */ + public function deleteReadOlderThanThreshold($threshold){ $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'SELECT * FROM `*PREFIX*news_items` ' . - 'WHERE NOT ((`status` & ?) > 0)'; + $sql = 'SELECT COUNT(*) `size`, `feed_id` ' . + 'FROM `*PREFIX*news_items` ' . + 'AND NOT ((`status` & ?) > 0) ' . + 'GROUP BY `feed_id` ' . + 'HAVING COUNT(*) > ?'; + $params = array($status, $threshold); + $result = $this->execute($sql, $params); + + while($row = $result->fetchRow()) { + + $limit = $threshold - $row['size']; + + if($limit > 0) { + $params = array($status, $row['feed_id']); + + $sql = 'DELETE FROM `*PREFIX*news_items` `items` ' . + 'WHERE NOT ((`status` & ?) > 0) ' . + 'AND `feed_id` = ? ' . + 'ORDER BY `items`.`id` ASC'; - $params = array($status); - return $this->findAllRows($sql, $params, $threshold); + $this->execute($sql, $params, $limit); + } + } } - public function deleteReadOlderThanId($id){ - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' . - 'AND NOT ((`status` & ?) > 0)'; - $params = array($id, $status); - $this->execute($sql, $params); - } } diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index 2fc894a70..10dd56578 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -62,7 +62,7 @@ class DIContainer extends BaseContainer { /** * Configuration values */ - $this['autoPurgeCount'] = 1000; + $this['autoPurgeCount'] = 200; $this['simplePieCacheDirectory'] = __DIR__ . '/../cache/simplepie/'; $this['simplePieCacheDuration'] = 1000; // seconds diff --git a/tests/unit/businesslayer/ItemBusinessLayerTest.php b/tests/unit/businesslayer/ItemBusinessLayerTest.php index 9bff8acfc..e3ae280a6 100644 --- a/tests/unit/businesslayer/ItemBusinessLayerTest.php +++ b/tests/unit/businesslayer/ItemBusinessLayerTest.php @@ -249,18 +249,9 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { public function testAutoPurgeOldWillPurgeOld(){ - $item = new Item(); - $item->setId(3); - $unread = array( - new Item(), $item - ); - $this->mapper->expects($this->once()) - ->method('getReadOlderThanThreshold') - ->with($this->equalTo($this->threshold)) - ->will($this->returnValue($unread)); $this->mapper->expects($this->once()) - ->method('deleteReadOlderThanId') - ->with($this->equalTo($item->getId())); + ->method('deleteReadOlderThanThreshold') + ->with($this->equalTo($this->threshold)); $result = $this->itemBusinessLayer->autoPurgeOld(); diff --git a/tests/unit/db/ItemMapperTest.php b/tests/unit/db/ItemMapperTest.php index ed47f175c..f46533c58 100644 --- a/tests/unit/db/ItemMapperTest.php +++ b/tests/unit/db/ItemMapperTest.php @@ -280,31 +280,78 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } - public function testGetReadOlderThanThreshold(){ + public function testDeleteReadOlderThanThresholdDoesNotDeleteBelowThreshold(){ $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'SELECT * FROM `*PREFIX*news_items` ' . - 'WHERE NOT ((`status` & ?) > 0)'; + $sql = 'SELECT COUNT(*) `size`, `feed_id` ' . + 'FROM `*PREFIX*news_items` ' . + 'AND NOT ((`status` & ?) > 0) ' . + 'GROUP BY `feed_id` ' . + 'HAVING COUNT(*) > ?'; + $threshold = 10; - $feed = new Feed(); - $feed->setId(30); - $rows = array(array('id' => 30)); - $params = array($status); + $rows = array(array('feed_id' => 30, 'size' => 11)); + $params = array($status, $threshold); $this->setMapperResult($sql, $params, $rows); - $result = $this->mapper->getReadOlderThanThreshold($threshold); - - $this->assertEquals($feed->getId(), $result[0]->getId()); + $this->mapper->deleteReadOlderThanThreshold($threshold); } - public function testDeleteReadOlderThanId(){ - $id = 10; + public function testDeleteReadOlderThanThreshold(){ + $threshold = 10; $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' . - 'AND NOT ((`status` & ?) > 0)'; - $params = array($id, $status); - $this->setMapperResult($sql, $params); - $this->mapper->deleteReadOlderThanId($id); + $sql1 = 'SELECT COUNT(*) `size`, `feed_id` ' . + 'FROM `*PREFIX*news_items` ' . + 'AND NOT ((`status` & ?) > 0) ' . + 'GROUP BY `feed_id` ' . + 'HAVING COUNT(*) > ?'; + $params1 = array($status, $threshold); + + + $row = array('feed_id' => 30, 'size' => 9); + + $sql2 = 'DELETE FROM `*PREFIX*news_items` `items` ' . + 'WHERE NOT ((`status` & ?) > 0) ' . + 'AND `feed_id` = ? ' . + 'ORDER BY `items`.`id` ASC'; + $params2 = array($status, 30); + + + $pdoResult = $this->getMock('Result', + array('fetchRow')); + + $pdoResult->expects($this->at(0)) + ->method('fetchRow') + ->will($this->returnValue($row)); + $pdoResult->expects($this->at(1)) + ->method('fetchRow') + ->will($this->returnValue(false)); + + $query = $this->getMock('Query', + array('execute')); + $query->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($params1)) + ->will($this->returnValue($pdoResult)); + + $this->api->expects($this->at(0)) + ->method('prepareQuery') + ->with($this->equalTo($sql1)) + ->will(($this->returnValue($query))); + + $query2 = $this->getMock('Query', + array('execute')); + $query2->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($params2)); + + $this->api->expects($this->at(1)) + ->method('prepareQuery') + ->with($this->equalTo($sql2)) + ->will(($this->returnValue($query2))); + + $result = $this->mapper->deleteReadOlderThanThreshold($threshold); } + }
\ No newline at end of file |