Merge pull request #22506 from owncloud/node-get-from-cache

Query the cache when checking if a node exists
This commit is contained in:
Thomas Müller 2016-03-23 13:08:17 +01:00
commit ea07a428f4
6 changed files with 131 additions and 66 deletions

View File

@ -86,11 +86,12 @@ class Test_Trashbin extends \Test\TestCase {
} }
public static function tearDownAfterClass() { public static function tearDownAfterClass() {
// cleanup test user // cleanup test user
$user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1); $user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1);
if ($user !== null) { $user->delete(); } if ($user !== null) {
$user->delete();
}
\OC::$server->getConfig()->setSystemValue('trashbin_retention_obligation', self::$rememberRetentionObligation); \OC::$server->getConfig()->setSystemValue('trashbin_retention_obligation', self::$rememberRetentionObligation);
@ -109,6 +110,18 @@ class Test_Trashbin extends \Test\TestCase {
parent::setUp(); parent::setUp();
\OC::$server->getAppManager()->enableApp('files_trashbin'); \OC::$server->getAppManager()->enableApp('files_trashbin');
$config = \OC::$server->getConfig();
$mockConfig = $this->getMock('\OCP\IConfig');
$mockConfig->expects($this->any())
->method('getSystemValue')
->will($this->returnCallback(function ($key, $default) use ($config) {
if ($key === 'filesystem_check_changes') {
return \OC\Files\Cache\Watcher::CHECK_ONCE;
} else {
return $config->getSystemValue($key, $default);
}
}));
$this->overwriteService('AllConfig', $mockConfig);
$this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
$this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin'; $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin';
@ -117,6 +130,7 @@ class Test_Trashbin extends \Test\TestCase {
} }
protected function tearDown() { protected function tearDown() {
$this->restoreService('AllConfig');
// disable trashbin to be able to properly clean up // disable trashbin to be able to properly clean up
\OC::$server->getAppManager()->disableApp('files_trashbin'); \OC::$server->getAppManager()->disableApp('files_trashbin');
@ -138,8 +152,8 @@ class Test_Trashbin extends \Test\TestCase {
public function testExpireOldFiles() { public function testExpireOldFiles() {
$currentTime = time(); $currentTime = time();
$expireAt = $currentTime - 2*24*60*60; $expireAt = $currentTime - 2 * 24 * 60 * 60;
$expiredDate = $currentTime - 3*24*60*60; $expiredDate = $currentTime - 3 * 24 * 60 * 60;
// create some files // create some files
\OC\Files\Filesystem::file_put_contents('file1.txt', 'file1'); \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
@ -187,7 +201,7 @@ class Test_Trashbin extends \Test\TestCase {
$currentTime = time(); $currentTime = time();
$folder = "trashTest-" . $currentTime . '/'; $folder = "trashTest-" . $currentTime . '/';
$expiredDate = $currentTime - 3*24*60*60; $expiredDate = $currentTime - 3 * 24 * 60 * 60;
// create some files // create some files
\OC\Files\Filesystem::mkdir($folder); \OC\Files\Filesystem::mkdir($folder);
@ -250,6 +264,7 @@ class Test_Trashbin extends \Test\TestCase {
/** /**
* verify that the array contains the expected results * verify that the array contains the expected results
*
* @param OCP\Files\FileInfo[] $result * @param OCP\Files\FileInfo[] $result
* @param string[] $expected * @param string[] $expected
*/ */
@ -265,7 +280,7 @@ class Test_Trashbin extends \Test\TestCase {
} }
if (!$found) { if (!$found) {
// if we didn't found the expected file, something went wrong // if we didn't found the expected file, something went wrong
$this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin"); $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin");
} }
} }
} }
@ -281,7 +296,7 @@ class Test_Trashbin extends \Test\TestCase {
// modify every second file // modify every second file
$counter = ($counter + 1) % 2; $counter = ($counter + 1) % 2;
if ($counter === 1) { if ($counter === 1) {
$source = $trashRoot . '/files/' . $file['name'].'.d'.$file['mtime']; $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime'];
$target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate);
$this->rootView->rename($source, $target); $this->rootView->rename($source, $target);
$file['mtime'] = $expireDate; $file['mtime'] = $expireDate;
@ -445,7 +460,7 @@ class Test_Trashbin extends \Test\TestCase {
$trashedFile = $filesInTrash[0]; $trashedFile = $filesInTrash[0];
$this->assertTrue( $this->assertTrue(
OCA\Files_Trashbin\Trashbin::restore( OCA\Files_Trashbin\Trashbin::restore(
'folder.d' . $trashedFile->getMtime() . '/file1.txt', 'folder.d' . $trashedFile->getMtime() . '/file1.txt',
'file1.txt', 'file1.txt',
$trashedFile->getMtime() $trashedFile->getMtime()
@ -639,7 +654,7 @@ class Test_Trashbin extends \Test\TestCase {
if ($create) { if ($create) {
try { try {
\OC::$server->getUserManager()->createUser($user, $user); \OC::$server->getUserManager()->createUser($user, $user);
} catch(\Exception $e) { // catch username is already being used from previous aborted runs } catch (\Exception $e) { // catch username is already being used from previous aborted runs
} }
} }

View File

@ -74,6 +74,19 @@ class Test_Files_Versioning extends \Test\TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$config = \OC::$server->getConfig();
$mockConfig = $this->getMock('\OCP\IConfig');
$mockConfig->expects($this->any())
->method('getSystemValue')
->will($this->returnCallback(function ($key, $default) use ($config) {
if ($key === 'filesystem_check_changes') {
return \OC\Files\Cache\Watcher::CHECK_ONCE;
} else {
return $config->getSystemValue($key, $default);
}
}));
$this->overwriteService('AllConfig', $mockConfig);
// clear hooks // clear hooks
\OC_Hook::clear(); \OC_Hook::clear();
\OC::registerShareHooks(); \OC::registerShareHooks();
@ -87,6 +100,8 @@ class Test_Files_Versioning extends \Test\TestCase {
} }
protected function tearDown() { protected function tearDown() {
$this->restoreService('AllConfig');
if ($this->rootView) { if ($this->rootView) {
$this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/'); $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/');
$this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/'); $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/');

View File

@ -90,19 +90,19 @@ class Folder extends Node implements \OCP\Files\Folder {
/** /**
* @param string $path * @param string $path
* @param array $info * @param FileInfo $info
* @return File|Folder * @return File|Folder
*/ */
protected function createNode($path, $info = array()) { protected function createNode($path, FileInfo $info = null) {
if (!isset($info['mimetype'])) { if (is_null($info)) {
$isDir = $this->view->is_dir($path); $isDir = $this->view->is_dir($path);
} else { } else {
$isDir = $info['mimetype'] === 'httpd/unix-directory'; $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
} }
if ($isDir) { if ($isDir) {
return new Folder($this->root, $this->view, $path); return new Folder($this->root, $this->view, $path, $info);
} else { } else {
return new File($this->root, $this->view, $path); return new File($this->root, $this->view, $path, $info);
} }
} }
@ -211,10 +211,9 @@ class Folder extends Node implements \OCP\Files\Folder {
private function searchCommon($method, $args) { private function searchCommon($method, $args) {
$files = array(); $files = array();
$rootLength = strlen($this->path); $rootLength = strlen($this->path);
/** $mount = $this->root->getMount($this->path);
* @var \OC\Files\Storage\Storage $storage $storage = $mount->getStorage();
*/ $internalPath = $mount->getInternalPath($this->path);
list($storage, $internalPath) = $this->view->resolvePath($this->path);
$internalPath = rtrim($internalPath, '/'); $internalPath = rtrim($internalPath, '/');
if ($internalPath !== '') { if ($internalPath !== '') {
$internalPath = $internalPath . '/'; $internalPath = $internalPath . '/';
@ -229,7 +228,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$result['internalPath'] = $result['path']; $result['internalPath'] = $result['path'];
$result['path'] = substr($result['path'], $internalRootLength); $result['path'] = substr($result['path'], $internalRootLength);
$result['storage'] = $storage; $result['storage'] = $storage;
$files[] = $result; $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
} }
} }
@ -245,17 +244,14 @@ class Folder extends Node implements \OCP\Files\Folder {
$result['internalPath'] = $result['path']; $result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path']; $result['path'] = $relativeMountPoint . $result['path'];
$result['storage'] = $storage; $result['storage'] = $storage;
$files[] = $result; $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
} }
} }
} }
$result = array(); return array_map(function(FileInfo $file) {
foreach ($files as $file) { return $this->createNode($file->getPath(), $file);
$result[] = $this->createNode($this->normalizePath($this->path . '/' . $file['path']), $file); }, $files);
}
return $result;
} }
/** /**

View File

@ -176,8 +176,9 @@ class Root extends Folder implements IRootFolder {
$path = $this->normalizePath($path); $path = $this->normalizePath($path);
if ($this->isValidPath($path)) { if ($this->isValidPath($path)) {
$fullPath = $this->getFullPath($path); $fullPath = $this->getFullPath($path);
if ($this->view->file_exists($fullPath)) { $fileInfo = $this->view->getFileInfo($fullPath);
return $this->createNode($fullPath); if ($fileInfo) {
return $this->createNode($fullPath, $fileInfo);
} else { } else {
throw new NotFoundException($path); throw new NotFoundException($path);
} }
@ -336,18 +337,18 @@ class Root extends Folder implements IRootFolder {
$dir = '/' . $userId; $dir = '/' . $userId;
$folder = null; $folder = null;
if (!$this->nodeExists($dir)) { try {
$folder = $this->newFolder($dir);
} else {
$folder = $this->get($dir); $folder = $this->get($dir);
} catch (NotFoundException $e) {
$folder = $this->newFolder($dir);
} }
$dir = '/files'; $dir = '/files';
if (!$folder->nodeExists($dir)) { try {
$folder = $folder->get($dir);
} catch (NotFoundException $e) {
$folder = $folder->newFolder($dir); $folder = $folder->newFolder($dir);
\OC_Util::copySkeleton($userId, $folder); \OC_Util::copySkeleton($userId, $folder);
} else {
$folder = $folder->get($dir);
} }
return $folder; return $folder;

View File

@ -376,6 +376,14 @@ class Folder extends \Test\TestCase {
->method('getCache') ->method('getCache')
->will($this->returnValue($cache)); ->will($this->returnValue($cache));
$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));
$cache->expects($this->once()) $cache->expects($this->once())
->method('search') ->method('search')
->with('%qw%') ->with('%qw%')
@ -388,9 +396,10 @@ class Folder extends \Test\TestCase {
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$view->expects($this->once()) $root->expects($this->once())
->method('resolvePath') ->method('getMount')
->will($this->returnValue(array($storage, 'foo'))); ->with('/bar/foo')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->search('qw'); $result = $node->search('qw');
@ -404,13 +413,21 @@ class Folder extends \Test\TestCase {
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/ */
$view = $this->getMock('\OC\Files\View'); $view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn'), array($manager, $view, $this->user)); $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
$root->expects($this->any()) $root->expects($this->any())
->method('getUser') ->method('getUser')
->will($this->returnValue($this->user)); ->will($this->returnValue($this->user));
$storage = $this->getMock('\OC\Files\Storage\Storage'); $storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('files'));
$storage->expects($this->once()) $storage->expects($this->once())
->method('getCache') ->method('getCache')
->will($this->returnValue($cache)); ->will($this->returnValue($cache));
@ -428,9 +445,10 @@ class Folder extends \Test\TestCase {
->with('') ->with('')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$view->expects($this->once()) $root->expects($this->once())
->method('resolvePath') ->method('getMount')
->will($this->returnValue(array($storage, 'files'))); ->with('')
->will($this->returnValue($mount));
$result = $root->search('qw'); $result = $root->search('qw');
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
@ -450,6 +468,14 @@ class Folder extends \Test\TestCase {
$storage = $this->getMock('\OC\Files\Storage\Storage'); $storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue(''));
$storage->expects($this->once()) $storage->expects($this->once())
->method('getCache') ->method('getCache')
->will($this->returnValue($cache)); ->will($this->returnValue($cache));
@ -466,9 +492,10 @@ class Folder extends \Test\TestCase {
->with('/bar') ->with('/bar')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$view->expects($this->once()) $root->expects($this->once())
->method('resolvePath') ->method('getMount')
->will($this->returnValue(array($storage, ''))); ->with('/bar')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar'); $node = new \OC\Files\Node\Folder($root, $view, '/bar');
$result = $node->search('qw'); $result = $node->search('qw');
@ -489,6 +516,14 @@ class Folder extends \Test\TestCase {
$storage = $this->getMock('\OC\Files\Storage\Storage'); $storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));
$storage->expects($this->once()) $storage->expects($this->once())
->method('getCache') ->method('getCache')
->will($this->returnValue($cache)); ->will($this->returnValue($cache));
@ -505,9 +540,10 @@ class Folder extends \Test\TestCase {
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$view->expects($this->once()) $root->expects($this->once())
->method('resolvePath') ->method('getMount')
->will($this->returnValue(array($storage, 'foo'))); ->with('/bar/foo')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->searchByTag('tag1', 'user1'); $result = $node->searchByTag('tag1', 'user1');
@ -531,6 +567,14 @@ class Folder extends \Test\TestCase {
$subStorage = $this->getMock('\OC\Files\Storage\Storage'); $subStorage = $this->getMock('\OC\Files\Storage\Storage');
$subMount = $this->getMock('\OC\Files\Mount\MountPoint', array(), array(null, '')); $subMount = $this->getMock('\OC\Files\Mount\MountPoint', array(), array(null, ''));
$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));
$subMount->expects($this->once()) $subMount->expects($this->once())
->method('getStorage') ->method('getStorage')
->will($this->returnValue($subStorage)); ->will($this->returnValue($subStorage));
@ -566,9 +610,10 @@ class Folder extends \Test\TestCase {
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array($subMount))); ->will($this->returnValue(array($subMount)));
$view->expects($this->once()) $root->expects($this->once())
->method('resolvePath') ->method('getMount')
->will($this->returnValue(array($storage, 'foo'))); ->with('/bar/foo')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
@ -601,8 +646,8 @@ class Folder extends \Test\TestCase {
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$view->expects($this->once()) $view->expects($this->once())
->method('file_exists') ->method('getFileInfo')
->will($this->returnValue(true)); ->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'text/plain'], null)));
$storage->expects($this->once()) $storage->expects($this->once())
->method('getCache') ->method('getCache')
@ -683,8 +728,8 @@ class Folder extends \Test\TestCase {
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$view->expects($this->any()) $view->expects($this->any())
->method('file_exists') ->method('getFileInfo')
->will($this->returnValue(true)); ->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'plain'], null)));
$storage->expects($this->any()) $storage->expects($this->any())
->method('getCache') ->method('getCache')

View File

@ -12,6 +12,9 @@ use OC\Files\FileInfo;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OC\Files\Mount\Manager; use OC\Files\Mount\Manager;
/**
* @group DB
*/
class Root extends \Test\TestCase { class Root extends \Test\TestCase {
private $user; private $user;
@ -41,16 +44,6 @@ class Root extends \Test\TestCase {
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain')))); ->will($this->returnValue($this->getFileInfo(array('fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain'))));
$view->expects($this->once())
->method('is_dir')
->with('/bar/foo')
->will($this->returnValue(false));
$view->expects($this->once())
->method('file_exists')
->with('/bar/foo')
->will($this->returnValue(true));
$root->mount($storage, ''); $root->mount($storage, '');
$node = $root->get('/bar/foo'); $node = $root->get('/bar/foo');
$this->assertEquals(10, $node->getId()); $this->assertEquals(10, $node->getId());
@ -73,7 +66,7 @@ class Root extends \Test\TestCase {
$root = new \OC\Files\Node\Root($manager, $view, $this->user); $root = new \OC\Files\Node\Root($manager, $view, $this->user);
$view->expects($this->once()) $view->expects($this->once())
->method('file_exists') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(false)); ->will($this->returnValue(false));