Skip to content

Commit dbfceb7

Browse files
committed
Update FileStorage
Sort the datasets by modifiedtime (newest first), then load the files and stop when the maximum results are found, so when don't have to load a lot of older files.
1 parent 8b8fb7a commit dbfceb7

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/DebugBar/Storage/FileStorage.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,36 @@ public function get($id)
4949
*/
5050
public function find(array $filters = array(), $max = 20, $offset = 0)
5151
{
52-
$results = array();
52+
//Loop through all .json files and remember the modified time and id.
53+
$files = array();
5354
foreach (new \DirectoryIterator($this->dirname) as $file) {
54-
if (substr($file->getFilename(), 0, 1) !== '.') {
55-
$id = substr($file->getFilename(), 0, strpos($file->getFilename(), '.'));
56-
$data = $this->get($id);
57-
$meta = $data['__meta'];
58-
unset($data);
59-
if (array_keys(array_intersect($meta, $filters)) == array_keys($filters)) {
60-
$results[] = $meta;
61-
}
55+
if ($file->getExtension() == 'json') {
56+
$files[] = array(
57+
'time' => $file->getMTime(),
58+
'id' => $file->getBasename('.json')
59+
);
6260
}
6361
}
62+
63+
//Sort the files, newest first
64+
usort($files, function($a, $b) {
65+
return $a['time'] < $b['time'];
66+
});
67+
68+
//Load the metadata and filter the results.
69+
$results = array();
70+
foreach ($files as $file) {
71+
$data = $this->get($file['id']);
72+
$meta = $data['__meta'];
73+
unset($data);
74+
if (array_keys(array_intersect($meta, $filters)) == array_keys($filters)) {
75+
$results[] = $meta;
76+
}
77+
if(count($results) >= ($max + $offset)){
78+
break;
79+
}
80+
}
81+
6482
return array_slice($results, $offset, $max);
6583
}
6684

0 commit comments

Comments
 (0)