Merge pull request #4131 from owncloud/scanner-split

Remove scanner logic from ajax file and place it in it's own class
remotes/origin/stable6
Bart Visscher 2013-07-21 11:32:11 +07:00
commit 37a731bcad
5 changed files with 140 additions and 49 deletions

@ -16,72 +16,56 @@ if (isset($_GET['users'])) {
} }
$eventSource = new OC_EventSource(); $eventSource = new OC_EventSource();
ScanListener::$eventSource = $eventSource; $listener = new ScanListener($eventSource);
ScanListener::$view = \OC\Files\Filesystem::getView();
OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_folder', 'ScanListener', 'folder');
OC_Hook::connect('\OC\Files\Cache\Scanner', 'scan_file', 'ScanListener', 'file');
foreach ($users as $user) { foreach ($users as $user) {
$eventSource->send('user', $user); $eventSource->send('user', $user);
OC_Util::tearDownFS(); $scanner = new \OC\Files\Utils\Scanner($user);
OC_Util::setupFS($user); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file'));
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', array($listener, 'folder'));
$absolutePath = \OC\Files\Filesystem::getView()->getAbsolutePath($dir); if ($force) {
$scanner->scan($dir);
$mountPoints = \OC\Files\Filesystem::getMountPoints($absolutePath); } else {
$mountPoints[] = \OC\Files\Filesystem::getMountPoint($absolutePath); $scanner->backgroundScan($dir);
$mountPoints = array_reverse($mountPoints); //start with the mount point of $dir
foreach ($mountPoints as $mountPoint) {
$storage = \OC\Files\Filesystem::getStorage($mountPoint);
if ($storage) {
ScanListener::$mountPoints[$storage->getId()] = $mountPoint;
$scanner = $storage->getScanner();
if ($force) {
$scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG);
} else {
$scanner->backgroundScan();
}
}
} }
} }
$eventSource->send('done', ScanListener::$fileCount); $eventSource->send('done', $listener->getCount());
$eventSource->close(); $eventSource->close();
class ScanListener { class ScanListener {
static public $fileCount = 0; private $fileCount = 0;
static public $lastCount = 0; private $lastCount = 0;
/** /**
* @var \OC\Files\View $view * @var \OC_EventSource event source to pass events to
*/ */
static public $view; private $eventSource;
/** /**
* @var array $mountPoints map storage ids to mountpoints * @param \OC_EventSource $eventSource
*/ */
static public $mountPoints = array(); public function __construct($eventSource) {
$this->eventSource = $eventSource;
}
/** /**
* @var \OC_EventSource event source to pass events to * @param string $path
*/ */
static public $eventSource; public function folder($path) {
$this->eventSource->send('folder', $path);
static function folder($params) {
$internalPath = $params['path'];
$mountPoint = self::$mountPoints[$params['storage']];
$path = self::$view->getRelativePath($mountPoint . $internalPath);
self::$eventSource->send('folder', $path);
} }
static function file() { public function file() {
self::$fileCount++; $this->fileCount++;
if (self::$fileCount > self::$lastCount + 20) { //send a count update every 20 files if ($this->fileCount > $this->lastCount + 20) { //send a count update every 20 files
self::$lastCount = self::$fileCount; $this->lastCount = $this->fileCount;
self::$eventSource->send('count', self::$fileCount); $this->eventSource->send('count', $this->fileCount);
} }
} }
public function getCount() {
return $this->fileCount;
}
} }

@ -53,7 +53,7 @@ class OC_EventSource{
/** /**
* send a message to the client * send a message to the client
* @param string $type * @param string $type
* @param object $data * @param mixed $data
* *
* if only one parameter is given, a typeless message will be send with that parameter as data * if only one parameter is given, a typeless message will be send with that parameter as data
*/ */

@ -9,8 +9,18 @@
namespace OC\Files\Cache; namespace OC\Files\Cache;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Hooks\BasicEmitter;
class Scanner { /**
* Class Scanner
*
* Hooks available in scope \OC\Files\Cache\Scanner:
* - scanFile(string $path, string $storageId)
* - scanFolder(string $path, string $storageId)
*
* @package OC\Files\Cache
*/
class Scanner extends BasicEmitter {
/** /**
* @var \OC\Files\Storage\Storage $storage * @var \OC\Files\Storage\Storage $storage
*/ */
@ -71,6 +81,7 @@ class Scanner {
if (!self::isPartialFile($file) if (!self::isPartialFile($file)
and !Filesystem::isFileBlacklisted($file) and !Filesystem::isFileBlacklisted($file)
) { ) {
$this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId));
\OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId));
$data = $this->getData($file); $data = $this->getData($file);
if ($data) { if ($data) {
@ -134,7 +145,7 @@ class Scanner {
if ($reuse === -1) { if ($reuse === -1) {
$reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0; $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0;
} }
\OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_folder', array('path' => $path, 'storage' => $this->storageId)); $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId));
$size = 0; $size = 0;
$childQueue = array(); $childQueue = array();
$existingChildren = array(); $existingChildren = array();

@ -148,13 +148,20 @@ class Filesystem {
*/ */
private static $loader; private static $loader;
public static function getLoader(){ public static function getLoader() {
if (!self::$loader) { if (!self::$loader) {
self::$loader = new Loader(); self::$loader = new Loader();
} }
return self::$loader; return self::$loader;
} }
public static function getMountManager() {
if (!self::$mounts) {
\OC_Util::setupFS();
}
return self::$mounts;
}
/** /**
* get the mountpoint of the storage object for a path * get the mountpoint of the storage object for a path
* ( note: because a storage is not always mounted inside the fakeroot, the * ( note: because a storage is not always mounted inside the fakeroot, the

@ -0,0 +1,89 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Utils;
use OC\Hooks\BasicEmitter;
use OC\Files\Filesystem;
/**
* Class Scanner
*
* Hooks available in scope \OC\Utils\Scanner
* - scanFile(string $absolutePath)
* - scanFolder(string $absolutePath)
*
* @package OC\Files\Utils
*/
class Scanner extends BasicEmitter {
/**
* @var string $user
*/
private $user;
/**
* @param string $user
*/
public function __construct($user) {
$this->user = $user;
}
/**
* get all storages for $dir
*
* @param string $dir
* @return \OC\Files\Mount\Mount[]
*/
protected function getMounts($dir) {
//TODO: move to the node based fileapi once that's done
\OC_Util::tearDownFS();
\OC_Util::setupFS($this->user);
$absolutePath = Filesystem::getView()->getAbsolutePath($dir);
$mountManager = Filesystem::getMountManager();
$mounts = $mountManager->findIn($absolutePath);
$mounts[] = $mountManager->find($absolutePath);
$mounts = array_reverse($mounts); //start with the mount of $dir
return $mounts;
}
/**
* attach listeners to the scanner
*
* @param \OC\Files\Mount\Mount $mount
*/
protected function attachListener($mount) {
$scanner = $mount->getStorage()->getScanner();
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount) {
$this->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
});
$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount) {
$this->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
});
}
public function backgroundScan($dir) {
$mounts = $this->getMounts($dir);
foreach ($mounts as $mount) {
$scanner = $mount->getStorage()->getScanner();
$this->attachListener($mount);
$scanner->backgroundScan();
}
}
public function scan($dir) {
$mounts = $this->getMounts($dir);
foreach ($mounts as $mount) {
$scanner = $mount->getStorage()->getScanner();
$this->attachListener($mount);
$scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG);
}
}
}