aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-09-02 15:10:44 +0200
committerArthurHoaro <arthur@hoa.ro>2017-09-02 15:10:44 +0200
commit9ec0a61156192484ca90a8dc88b7c23b26129755 (patch)
treea3b6690d3a0dde8ea858f3b8c27a9454e997d005
parent96a1c79456b27892b9221707803f29585565b9dc (diff)
downloadShaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.tar.gz
Shaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.tar.zst
Shaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.zip
Performances: reorder links when they're written instead of read
relates to #891
-rw-r--r--application/LinkDB.php17
-rw-r--r--application/Updater.php8
-rw-r--r--tests/LinkFilterTest.php13
-rw-r--r--tests/utils/ReferenceLinkDB.php23
4 files changed, 51 insertions, 10 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 22c1f0ab..eace625e 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -289,13 +289,15 @@ You use the community supported version of the original Shaarli project, by Seba
289 return; 289 return;
290 } 290 }
291 291
292 $this->urls = [];
293 $this->ids = [];
292 $this->links = FileUtils::readFlatDB($this->datastore, []); 294 $this->links = FileUtils::readFlatDB($this->datastore, []);
293 295
294 $toremove = array(); 296 $toremove = array();
295 foreach ($this->links as $key => &$link) { 297 foreach ($this->links as $key => &$link) {
296 if (! $this->loggedIn && $link['private'] != 0) { 298 if (! $this->loggedIn && $link['private'] != 0) {
297 // Transition for not upgraded databases. 299 // Transition for not upgraded databases.
298 $toremove[] = $key; 300 unset($this->links[$key]);
299 continue; 301 continue;
300 } 302 }
301 303
@@ -329,14 +331,10 @@ You use the community supported version of the original Shaarli project, by Seba
329 } 331 }
330 $link['shorturl'] = smallHash($link['linkdate']); 332 $link['shorturl'] = smallHash($link['linkdate']);
331 } 333 }
332 }
333 334
334 // If user is not logged in, filter private links. 335 $this->urls[$link['url']] = $key;
335 foreach ($toremove as $offset) { 336 $this->ids[$link['id']] = $key;
336 unset($this->links[$offset]);
337 } 337 }
338
339 $this->reorder();
340 } 338 }
341 339
342 /** 340 /**
@@ -346,6 +344,7 @@ You use the community supported version of the original Shaarli project, by Seba
346 */ 344 */
347 private function write() 345 private function write()
348 { 346 {
347 $this->reorder();
349 FileUtils::writeFlatDB($this->datastore, $this->links); 348 FileUtils::writeFlatDB($this->datastore, $this->links);
350 } 349 }
351 350
@@ -528,8 +527,8 @@ You use the community supported version of the original Shaarli project, by Seba
528 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; 527 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
529 }); 528 });
530 529
531 $this->urls = array(); 530 $this->urls = [];
532 $this->ids = array(); 531 $this->ids = [];
533 foreach ($this->links as $key => $link) { 532 foreach ($this->links as $key => $link) {
534 $this->urls[$link['url']] = $key; 533 $this->urls[$link['url']] = $key;
535 $this->ids[$link['id']] = $key; 534 $this->ids[$link['id']] = $key;
diff --git a/application/Updater.php b/application/Updater.php
index 40a15906..0702158a 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -436,6 +436,14 @@ class Updater
436 } 436 }
437 return true; 437 return true;
438 } 438 }
439
440 /**
441 * Save the datastore -> the link order is now applied when links are saved.
442 */
443 public function updateMethodReorderDatastore()
444 {
445 $this->linkDB->save($this->conf->get('resource.page_cache'));
446 }
439} 447}
440 448
441/** 449/**
diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php
index d796d3a3..9cd6dbd4 100644
--- a/tests/LinkFilterTest.php
+++ b/tests/LinkFilterTest.php
@@ -8,6 +8,10 @@ require_once 'application/LinkFilter.php';
8class LinkFilterTest extends PHPUnit_Framework_TestCase 8class LinkFilterTest extends PHPUnit_Framework_TestCase
9{ 9{
10 /** 10 /**
11 * @var string Test datastore path.
12 */
13 protected static $testDatastore = 'sandbox/datastore.php';
14 /**
11 * @var LinkFilter instance. 15 * @var LinkFilter instance.
12 */ 16 */
13 protected static $linkFilter; 17 protected static $linkFilter;
@@ -18,12 +22,19 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
18 protected static $refDB; 22 protected static $refDB;
19 23
20 /** 24 /**
25 * @var LinkDB instance
26 */
27 protected static $linkDB;
28
29 /**
21 * Instanciate linkFilter with ReferenceLinkDB data. 30 * Instanciate linkFilter with ReferenceLinkDB data.
22 */ 31 */
23 public static function setUpBeforeClass() 32 public static function setUpBeforeClass()
24 { 33 {
25 self::$refDB = new ReferenceLinkDB(); 34 self::$refDB = new ReferenceLinkDB();
26 self::$linkFilter = new LinkFilter(self::$refDB->getLinks()); 35 self::$refDB->write(self::$testDatastore);
36 self::$linkDB = new LinkDB(self::$testDatastore, true, false);
37 self::$linkFilter = new LinkFilter(self::$linkDB);
27 } 38 }
28 39
29 /** 40 /**
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index f09eebc1..e887aa78 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -141,6 +141,7 @@ class ReferenceLinkDB
141 */ 141 */
142 public function write($filename) 142 public function write($filename)
143 { 143 {
144 $this->reorder();
144 file_put_contents( 145 file_put_contents(
145 $filename, 146 $filename,
146 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' 147 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
@@ -148,6 +149,27 @@ class ReferenceLinkDB
148 } 149 }
149 150
150 /** 151 /**
152 * Reorder links by creation date (newest first).
153 *
154 * Also update the urls and ids mapping arrays.
155 *
156 * @param string $order ASC|DESC
157 */
158 public function reorder($order = 'DESC')
159 {
160 // backward compatibility: ignore reorder if the the `created` field doesn't exist
161 if (! isset(array_values($this->_links)[0]['created'])) {
162 return;
163 }
164
165 $order = $order === 'ASC' ? -1 : 1;
166 // Reorder array by dates.
167 usort($this->_links, function($a, $b) use ($order) {
168 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
169 });
170 }
171
172 /**
151 * Returns the number of links in the reference data 173 * Returns the number of links in the reference data
152 */ 174 */
153 public function countLinks() 175 public function countLinks()
@@ -187,6 +209,7 @@ class ReferenceLinkDB
187 209
188 public function getLinks() 210 public function getLinks()
189 { 211 {
212 $this->reorder();
190 return $this->_links; 213 return $this->_links;
191 } 214 }
192 215