Merge pull request #21956 from owncloud/cross-cache-move

Add fallback moveFromCache implementation
This commit is contained in:
Thomas Müller 2016-01-29 17:03:16 +01:00
commit 67bf225fbe
6 changed files with 157 additions and 28 deletions

View File

@ -49,6 +49,10 @@ use OCP\IDBConnection;
* - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
*/
class Cache implements ICache {
use MoveFromCacheTrait {
MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
}
/**
* @var array partial data for the cache
*/
@ -466,39 +470,42 @@ class Cache implements ICache {
* @throws \OC\DatabaseException
*/
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
// normalize source and target
$sourcePath = $this->normalize($sourcePath);
$targetPath = $this->normalize($targetPath);
if ($sourceCache instanceof Cache) {
// normalize source and target
$sourcePath = $this->normalize($sourcePath);
$targetPath = $this->normalize($targetPath);
$sourceData = $sourceCache->get($sourcePath);
$sourceId = $sourceData['fileid'];
$newParentId = $this->getParentId($targetPath);
$sourceData = $sourceCache->get($sourcePath);
$sourceId = $sourceData['fileid'];
$newParentId = $this->getParentId($targetPath);
list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);
list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);
// sql for final update
$moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';
// sql for final update
$moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//find all child entries
$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
$result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
$childEntries = $result->fetchAll();
$sourceLength = strlen($sourcePath);
$this->connection->beginTransaction();
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//find all child entries
$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
$result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
$childEntries = $result->fetchAll();
$sourceLength = strlen($sourcePath);
$this->connection->beginTransaction();
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
foreach ($childEntries as $child) {
$newTargetPath = $targetPath . substr($child['path'], $sourceLength);
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
foreach ($childEntries as $child) {
$newTargetPath = $targetPath . substr($child['path'], $sourceLength);
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
}
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->connection->commit();
} else {
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
}
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->connection->commit();
} else {
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
}
}
/**

View File

@ -0,0 +1,87 @@
<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\Cache;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
/**
* Fallback implementation for moveFromCache
*/
trait MoveFromCacheTrait {
/**
* store meta data for a file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
* @throws \RuntimeException
*/
abstract public function put($file, array $data);
/**
* Move a file or folder in the cache
*
* @param \OCP\Files\Cache\ICache $sourceCache
* @param string $sourcePath
* @param string $targetPath
*/
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
$sourceEntry = $sourceCache->get($sourcePath);
$this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
$sourceCache->remove($sourcePath);
}
/**
* Copy a file or folder in the cache
*
* @param \OCP\Files\Cache\ICache $sourceCache
* @param ICacheEntry $sourceEntry
* @param string $targetPath
*/
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath) {
$this->put($targetPath, $this->cacheEntryToArray($sourceEntry));
if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
$folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
foreach ($folderContent as $subEntry) {
$subTargetPath = $targetPath . '/' . $subEntry->getName();
$this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
}
}
}
private function cacheEntryToArray(ICacheEntry $entry) {
return [
'size' => $entry->getSize(),
'mtime' => $entry->getMTime(),
'storage_mtime' => $entry->getStorageMTime(),
'mimetype' => $entry->getMimeType(),
'mimepart' => $entry->getMimePart(),
'etag' => $entry->getEtag(),
'permissions' => $entry->getPermissions(),
'encrypted' => $entry->isEncrypted()
];
}
}

View File

@ -51,7 +51,7 @@ interface ICache {
* get the stored metadata of a file or folder
*
* @param string | int $file either the path of a file or folder or the file id for a file or folder
* @return ICacheEntry[]|false the cache entry or false if the file is not found in the cache
* @return ICacheEntry|false the cache entry or false if the file is not found in the cache
* @since 9.0.0
*/
public function get($file);
@ -148,6 +148,8 @@ interface ICache {
/**
* Move a file or folder in the cache
*
* Note that this should make sure the entries are removed from the source cache
*
* @param \OCP\Files\Cache\ICache $sourceCache
* @param string $sourcePath
* @param string $targetPath

View File

@ -27,6 +27,8 @@ namespace OCP\Files\Cache;
* @since 9.0.0
*/
interface ICacheEntry {
const DIRECTORY_MIMETYPE = 'httpd/unix-directory';
/**
* Get the numeric id of a file
*

View File

@ -641,8 +641,8 @@ class Cache extends \Test\TestCase {
$this->cache->put($name, $folderData);
$this->cache->put('other', $folderData);
$childs = ['asd', 'bar', 'foo', 'sub/folder'];
$this->cache->put($name . '/sub/folder', $folderData);
$this->cache->put('other/sub/folder', $folderData);
$this->cache->put($name . '/sub', $folderData);
$this->cache->put('other/sub', $folderData);
foreach ($childs as $child) {
$this->cache->put($name . '/' . $child, $data);
$this->cache->put('other/' . $child, $data);

View File

@ -0,0 +1,31 @@
<?php
/**
* Copyright (c) 2016 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\Cache\MoveFromCacheTrait;
class FallBackCrossCacheMoveCache extends \OC\Files\Cache\Cache {
use MoveFromCacheTrait;
}
/**
* Class Cache
*
* @group DB
*/
class MoveFromCacheTraitTest extends Cache {
protected function setUp() {
parent::setUp();
$this->storage = new \OC\Files\Storage\Temporary(array());
$this->storage2 = new \OC\Files\Storage\Temporary(array());
$this->cache = new FallBackCrossCacheMoveCache($this->storage);
$this->cache2 = new FallBackCrossCacheMoveCache($this->storage2);
}
}