summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--businesslayer/itembusinesslayer.php10
-rw-r--r--db/itemmapper.php41
-rw-r--r--dependencyinjection/dicontainer.php2
-rw-r--r--tests/unit/businesslayer/ItemBusinessLayerTest.php13
-rw-r--r--tests/unit/db/ItemMapperTest.php81
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