Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use limit instead of since when listing recent files
  • Loading branch information
icewind1991 committed Jul 22, 2016
commit 81e103074ea6ce9e7035734bc527ab582dbca89f
3 changes: 1 addition & 2 deletions apps/files/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public function getFilesByTag($tagName) {
* @return DataResponse
*/
public function getRecentFiles() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test for this? 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the todo 😄

$since = time() - (60 * 60 * 24 * 7);//1 week
$nodes = $this->userFolder->getRecent($since);
$nodes = $this->userFolder->getRecent(100);
$files = $this->formatNodes($nodes);
return new DataResponse(['files' => $files]);
}
Expand Down
49 changes: 7 additions & 42 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ public function getNonExistingName($name) {
}

/**
* @param int $since
* @param int $limit
* @param int $offset
* @return \OCP\Files\Node[]
*/
public function getRecent($since) {
public function getRecent($limit, $offset = 0) {
$mimetypeLoader = \OC::$server->getMimeTypeLoader();
$mounts = $this->root->getMountsIn($this->path);
$mounts[] = $this->getMountPoint();
Expand All @@ -387,55 +388,19 @@ public function getRecent($since) {
$query = $builder
->select('f.*')
->from('filecache', 'f')
->where($builder->expr()->gt('f.storage_mtime', $builder->createNamedParameter($since, IQueryBuilder::PARAM_INT)))
->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($builder->expr()->orX(
// handle non empty folders separate
$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
$builder->expr()->eq('f.size', new Literal(0))
))
->orderBy('f.mtime', 'DESC');
->orderBy('f.mtime', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset);

$result = $query->execute()->fetchAll();

// select folders with their mtime being the mtime of the oldest file in the folder
// this way we still show new folders but dont bumb the folder every time a file in it is changed
$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = $builder
->select('p.fileid', 'p.storage', 'p.mimetype', 'p.mimepart', 'p.size', 'p.path', 'p.etag', 'f1.storage_mtime', 'f1.mtime', 'p.permissions')
->from('filecache', 'f1')
->leftJoin('f1', 'filecache', 'f2', $builder->expr()->andX( // find the f1 with lowest mtime in the folder
$builder->expr()->eq('f1.parent', 'f2.parent'),
$builder->expr()->gt('f1.storage_mtime', 'f2.storage_mtime')
))
->innerJoin('f1', 'filecache', 'p', $builder->expr()->eq('f1.parent', 'p.fileid'))
->where($builder->expr()->isNull('f2.fileid'))
->andWhere($builder->expr()->gt('f1.storage_mtime', $builder->createNamedParameter($since, IQueryBuilder::PARAM_INT)))
->andWhere($builder->expr()->in('f1.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($builder->expr()->neq('f1.size', new Literal(0)))
->orderBy('f1.storage_mtime', 'DESC');

$folderResults = $query->execute()->fetchAll();

$found = []; // we sometimes get duplicate folders
$folderResults = array_filter($folderResults, function ($item) use (&$found) {
$isFound = isset($found[$item['fileid']]);
$found[$item['fileid']] = true;
return !$isFound;
});

$result = array_merge($folderResults, $result);

usort($result, function ($a, $b) use ($folderMimetype) {
$diff = $b['mtime'] - $a['mtime'];
if ($diff === 0) {
return $a['mimetype'] === $folderMimetype ? -1 : 1;
} else {
return $diff;
}
});

$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
$mount = $mountMap[$entry['storage']];
$entry['internalPath'] = $entry['path'];
$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/LazyRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public function unlock($type) {
/**
* @inheritDoc
*/
public function getRecent($type) {
public function getRecent($limit, $offset = 0) {
return $this->__call(__FUNCTION__, func_get_args());
}
}
5 changes: 3 additions & 2 deletions lib/public/Files/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ public function isCreatable();
public function getNonExistingName($name);

/**
* @param int $since
* @param int $limit
* @param int $offset
* @return \OCP\Files\Node[]
* @since 9.1.0
*/
public function getRecent($since);
public function getRecent($limit, $offset = 0);
}
13 changes: 6 additions & 7 deletions tests/lib/Files/Node/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function testRecent() {
'mimetype' => 'text/plain',
'size' => 3
]);
$cache->put('bar/foo/toold.txt', [
$id3 = $cache->put('bar/foo/older.txt', [
'storage_mtime' => $baseTime - 600,
'mtime' => $baseTime - 600,
'mimetype' => 'text/plain',
Expand All @@ -846,11 +846,11 @@ public function testRecent() {
$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);


$nodes = $node->getRecent($baseTime - 500);
$nodes = $node->getRecent(5);
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
$this->assertEquals([$id1, $id2], $ids);
$this->assertEquals([$id1, $id2, $id3], $ids);
}

public function testRecentFolder() {
Expand Down Expand Up @@ -900,14 +900,13 @@ public function testRecentFolder() {
$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);


$nodes = $node->getRecent($baseTime - 500);
$nodes = $node->getRecent(5);
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
$this->assertEquals([$id2, $id1, $id3], $ids);// sort folders before files with the same mtime, folders get the lowest child mtime
$this->assertEquals([$id2, $id3], $ids);
$this->assertEquals($baseTime, $nodes[0]->getMTime());
$this->assertEquals($baseTime - 100, $nodes[1]->getMTime());
$this->assertEquals($baseTime - 100, $nodes[2]->getMTime());
}

public function testRecentJail() {
Expand Down Expand Up @@ -952,7 +951,7 @@ public function testRecentJail() {

$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);

$nodes = $node->getRecent($baseTime - 500);
$nodes = $node->getRecent(5);
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
Expand Down