aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-01-15 22:33:06 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-01-15 22:33:06 +0100
commit0ebcc2d4f9582c652c6e37ff6e9024e44fc453cf (patch)
tree711dde9cffac4daf347ba0f690beddb7ed7d1c11
parent2426729ad455122424b0b813eb5eecb6e39e64d1 (diff)
parent0a3e0a1665f2c1faffd94ba0beb3dd2d6b0a465a (diff)
downloadnextcloud-server-0ebcc2d4f9582c652c6e37ff6e9024e44fc453cf.tar.gz
nextcloud-server-0ebcc2d4f9582c652c6e37ff6e9024e44fc453cf.zip
Merge pull request #13394 from owncloud/cache-remove-folder
Remove children from the cache in one query
-rw-r--r--lib/private/files/cache/cache.php60
-rw-r--r--tests/lib/files/cache/cache.php19
2 files changed, 55 insertions, 24 deletions
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php
index bf44cf48aa7..8d0681ba935 100644
--- a/lib/private/files/cache/cache.php
+++ b/lib/private/files/cache/cache.php
@@ -73,12 +73,11 @@ class Cache {
}
if (!isset(self::$mimetypeIds[$mime])) {
- try{
+ try {
$result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime));
self::$mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
self::$mimetypes[self::$mimetypeIds[$mime]] = $mime;
- }
- catch (\Doctrine\DBAL\DBALException $e){
+ } catch (\Doctrine\DBAL\DBALException $e) {
\OC_Log::write('core', 'Exception during mimetype insertion: ' . $e->getmessage(), \OC_Log::DEBUG);
return -1;
}
@@ -95,20 +94,20 @@ class Cache {
return isset(self::$mimetypes[$id]) ? self::$mimetypes[$id] : null;
}
- public function loadMimetypes(){
- $result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array());
- if ($result) {
- while ($row = $result->fetchRow()) {
- self::$mimetypeIds[$row['mimetype']] = $row['id'];
- self::$mimetypes[$row['id']] = $row['mimetype'];
- }
+ public function loadMimetypes() {
+ $result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array());
+ if ($result) {
+ while ($row = $result->fetchRow()) {
+ self::$mimetypeIds[$row['mimetype']] = $row['id'];
+ self::$mimetypes[$row['id']] = $row['mimetype'];
}
+ }
}
/**
* get the stored metadata of a file or folder
*
- * @param string/int $file
+ * @param string /int $file
* @return array|false
*/
public function get($file) {
@@ -135,7 +134,7 @@ class Cache {
}
//merge partial data
- if (!$data and is_string($file)) {
+ if (!$data and is_string($file)) {
if (isset($this->partial[$file])) {
$data = $this->partial[$file];
}
@@ -146,7 +145,7 @@ class Cache {
$data['mtime'] = (int)$data['mtime'];
$data['storage_mtime'] = (int)$data['storage_mtime'];
$data['encrypted'] = (bool)$data['encrypted'];
- $data['unencrypted_size'] = 0 + $data['unencrypted_size'];
+ $data['unencrypted_size'] = 0 + $data['unencrypted_size'];
$data['storage'] = $this->storageId;
$data['mimetype'] = $this->getMimetype($data['mimetype']);
$data['mimepart'] = $this->getMimetype($data['mimepart']);
@@ -181,7 +180,7 @@ class Cache {
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
`storage_mtime`, `encrypted`, `unencrypted_size`, `etag`, `permissions`
FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
- $result = \OC_DB::executeAudited($sql,array($fileId));
+ $result = \OC_DB::executeAudited($sql, array($fileId));
$files = $result->fetchAll();
foreach ($files as &$file) {
$file['mimetype'] = $this->getMimetype($file['mimetype']);
@@ -255,12 +254,12 @@ class Cache {
*/
public function update($id, array $data) {
- if(isset($data['path'])) {
+ if (isset($data['path'])) {
// normalize path
$data['path'] = $this->normalize($data['path']);
}
- if(isset($data['name'])) {
+ if (isset($data['name'])) {
// normalize path
$data['name'] = $this->normalize($data['name']);
}
@@ -365,14 +364,26 @@ class Cache {
*/
public function remove($file) {
$entry = $this->get($file);
+ $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
+ \OC_DB::executeAudited($sql, array($entry['fileid']));
if ($entry['mimetype'] === 'httpd/unix-directory') {
- $children = $this->getFolderContents($file);
- foreach ($children as $child) {
- $this->remove($child['path']);
- }
+ $this->removeChildren($entry);
}
+ }
- $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
+ private function getSubFolders($entry) {
+ $children = $this->getFolderContentsById($entry['fileid']);
+ return array_filter($children, function ($child) {
+ return $child['mimetype'] === 'httpd/unix-directory';
+ });
+ }
+
+ private function removeChildren($entry) {
+ $subFolders = $this->getSubFolders($entry);
+ foreach ($subFolders as $folder) {
+ $this->removeChildren($folder);
+ }
+ $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
\OC_DB::executeAudited($sql, array($entry['fileid']));
}
@@ -520,12 +531,12 @@ class Cache {
'`*PREFIX*vcategory_to_object` `tagmap`, ' .
'`*PREFIX*vcategory` `tag` ' .
// JOIN filecache to vcategory_to_object
- 'WHERE `file`.`fileid` = `tagmap`.`objid` '.
+ 'WHERE `file`.`fileid` = `tagmap`.`objid` ' .
// JOIN vcategory_to_object to vcategory
'AND `tagmap`.`type` = `tag`.`type` ' .
'AND `tagmap`.`categoryid` = `tag`.`id` ' .
// conditions
- 'AND `file`.`storage` = ? '.
+ 'AND `file`.`storage` = ? ' .
'AND `tag`.`type` = \'files\' ' .
'AND `tag`.`uid` = ? ';
if (is_int($tag)) {
@@ -638,7 +649,7 @@ class Cache {
*/
public function getIncomplete() {
$query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache`'
- . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC',1);
+ . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
$result = \OC_DB::executeAudited($query, array($this->getNumericStorageId()));
if ($row = $result->fetchRow()) {
return $row['path'];
@@ -694,6 +705,7 @@ class Cache {
/**
* normalize the given path
+ *
* @param string $path
* @return string
*/
diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php
index 6df98ee531d..15bcff24f36 100644
--- a/tests/lib/files/cache/cache.php
+++ b/tests/lib/files/cache/cache.php
@@ -143,6 +143,25 @@ class Cache extends \Test\TestCase {
$this->assertFalse($this->cache->inCache($folder.'/bar'));
}
+ public function testRemoveRecursive() {
+ $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
+ $fileData = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'text/plain');
+ $folders = ['folder', 'folder/subfolder', 'folder/sub2', 'folder/sub2/sub3'];
+ $files = ['folder/foo.txt', 'folder/bar.txt', 'folder/subfolder/asd.txt', 'folder/sub2/qwerty.txt', 'folder/sub2/sub3/foo.txt'];
+
+ foreach($folders as $folder){
+ $this->cache->put($folder, $folderData);
+ }
+ foreach ($files as $file) {
+ $this->cache->put($file, $fileData);
+ }
+
+ $this->cache->remove('folder');
+ foreach ($files as $file) {
+ $this->assertFalse($this->cache->inCache($file));
+ }
+ }
+
public function folderDataProvider() {
return array(