aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-11-08 21:12:40 +0100
committerRobin Appelman <icewind@owncloud.com>2012-11-08 21:12:40 +0100
commitfedff3eafe73a3eafbc1bc7af364297e8135d086 (patch)
tree1139b1556b7ad7769e7e977f422dddf55f436d70
parent5a173b901f90dba3465847cde4477be9994a1341 (diff)
downloadnextcloud-server-fedff3eafe73a3eafbc1bc7af364297e8135d086.tar.gz
nextcloud-server-fedff3eafe73a3eafbc1bc7af364297e8135d086.zip
add cache updater
-rw-r--r--lib/files/cache/updater.php71
-rw-r--r--tests/lib/files/cache/updater.php116
2 files changed, 187 insertions, 0 deletions
diff --git a/lib/files/cache/updater.php b/lib/files/cache/updater.php
new file mode 100644
index 00000000000..fb9783023e8
--- /dev/null
+++ b/lib/files/cache/updater.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Copyright (c) 2012 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\Cache;
+
+/**
+ * listen to filesystem hooks and change the cache accordingly
+ */
+class Updater {
+
+ /**
+ * resolve a path to a storage and internal path
+ *
+ * @param string $path
+ * @return array consisting of the storage and the internal path
+ */
+ static public function resolvePath($path) {
+ $view = \OC\Files\Filesystem::getView();
+ return $view->resolvePath($path);
+ }
+
+ static public function writeUpdate($path) {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath
+ */
+ list($storage, $internalPath) = self::resolvePath($path);
+ $cache = new Cache($storage);
+ $scanner = new Scanner($storage);
+ $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
+ $cache->correctFolderSize($internalPath);
+ }
+
+ static public function deleteUpdate($path) {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath
+ */
+ list($storage, $internalPath) = self::resolvePath($path);
+ $cache = new Cache($storage);
+ $cache->remove($internalPath);
+ $cache->correctFolderSize($internalPath);
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function writeHook($params) {
+ self::writeUpdate($params['path']);
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function renameHook($params) {
+ self::deleteUpdate($params['oldpath']);
+ self::writeUpdate($params['newpath']);
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function deleteHook($params) {
+ self::deleteUpdate($params['path']);
+ }
+}
diff --git a/tests/lib/files/cache/updater.php b/tests/lib/files/cache/updater.php
new file mode 100644
index 00000000000..8059418dc16
--- /dev/null
+++ b/tests/lib/files/cache/updater.php
@@ -0,0 +1,116 @@
+<?php
+/**
+ * Copyright (c) 2012 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 Test\Files\Cache;
+
+use \OC\Files\Filesystem as Filesystem;
+
+class Updater extends \PHPUnit_Framework_TestCase {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ */
+ private $storage;
+
+ /**
+ * @var \OC\Files\Cache\Scanner $scanner
+ */
+ private $scanner;
+
+ /**
+ * @var \OC\Files\Cache\Cache $cache
+ */
+ private $cache;
+
+ private static $user;
+
+ public function setUp() {
+ $this->storage = new \OC\Files\Storage\Temporary(array());
+ $textData = "dummy file data\n";
+ $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
+ $this->storage->mkdir('folder');
+ $this->storage->file_put_contents('foo.txt', $textData);
+ $this->storage->file_put_contents('foo.png', $imgData);
+ $this->storage->file_put_contents('folder/bar.txt', $textData);
+ $this->storage->file_put_contents('folder/bar2.txt', $textData);
+
+ $this->scanner = $this->storage->getScanner();
+ $this->scanner->scan('');
+ $this->cache = $this->storage->getCache();
+
+ if (!self::$user) {
+ if (!\OC\Files\Filesystem::getView()) {
+ self::$user = uniqid();
+ \OC\Files\Filesystem::init('/' . self::$user . '/files');
+ } else {
+ self::$user = \OC_User::getUser();
+ }
+ }
+
+ Filesystem::clearMounts();
+ Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
+
+ \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
+ \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
+ \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
+
+ }
+
+ public function tearDown() {
+ $this->cache->clear();
+ Filesystem::tearDown();
+ }
+
+ public function testWrite() {
+ $textSize = strlen("dummy file data\n");
+ $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
+
+ Filesystem::file_put_contents('foo.txt', 'asd');
+ $cachedData = $this->cache->get('foo.txt');
+ $this->assertEquals(3, $cachedData['size']);
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
+
+ $this->assertFalse($this->cache->inCache('bar.txt'));
+ Filesystem::file_put_contents('bar.txt', 'asd');
+ $this->assertTrue($this->cache->inCache('bar.txt'));
+ $cachedData = $this->cache->get('bar.txt');
+ $this->assertEquals(3, $cachedData['size']);
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
+ }
+
+ public function testDelete() {
+ $textSize = strlen("dummy file data\n");
+ $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
+
+ $this->assertTrue($this->cache->inCache('foo.txt'));
+ Filesystem::unlink('foo.txt', 'asd');
+ $this->assertFalse($this->cache->inCache('foo.txt'));
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
+ }
+
+ public function testRename() {
+ $textSize = strlen("dummy file data\n");
+ $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
+
+ $this->assertTrue($this->cache->inCache('foo.txt'));
+ $this->assertFalse($this->cache->inCache('bar.txt'));
+ Filesystem::rename('foo.txt', 'bar.txt');
+ $this->assertFalse($this->cache->inCache('foo.txt'));
+ $this->assertTrue($this->cache->inCache('bar.txt'));
+ $cachedData = $this->cache->get('');
+ $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
+ }
+}