Return a class from cache operations instead of an array

This commit is contained in:
Robin Appelman 2015-12-02 14:59:13 +01:00
parent 5d0451b848
commit 6d321f5f6b
11 changed files with 63 additions and 86 deletions

View File

@ -33,6 +33,7 @@
namespace OC\Files\Cache;
use OC\User\NoUserException;
use OCP\Files\Cache\ICacheEntry;
use OCP\Share_Backend_Collection;
/**
@ -98,8 +99,8 @@ class Shared_Cache extends Cache {
/**
* get the stored metadata of a file or folder
*
* @param string $file
* @return array|false
* @param string|int $file
* @return ICacheEntry|false
*/
public function get($file) {
$mimetypeLoader = \OC::$server->getMimeTypeLoader();
@ -161,7 +162,7 @@ class Shared_Cache extends Cache {
* get the metadata of all files stored in $folder
*
* @param string $folderId
* @return array|false
* @return ICacheEntry[]|false
*/
public function getFolderContentsById($folderId) {
$cache = $this->getSourceCache('');
@ -281,7 +282,7 @@ class Shared_Cache extends Cache {
* search for files matching $pattern
*
* @param string $pattern
* @return array of file data
* @return ICacheEntry[] of file data
*/
public function search($pattern) {
@ -320,7 +321,7 @@ class Shared_Cache extends Cache {
* search for files by mimetype
*
* @param string $mimetype
* @return array
* @return ICacheEntry[]
*/
public function searchByMime($mimetype) {
$mimepart = null;
@ -373,7 +374,7 @@ class Shared_Cache extends Cache {
*
* @param string|int $tag tag to search for
* @param string $userId owner of the tags
* @return array file data
* @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
// TODO: inject this

View File

@ -34,6 +34,7 @@
namespace OC\Files\Cache;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use \OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;
@ -48,11 +49,6 @@ 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 {
const NOT_FOUND = 0;
const PARTIAL = 1; //only partial data available, file not cached in the database
const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
const COMPLETE = 3;
/**
* @var array partial data for the cache
*/
@ -106,28 +102,8 @@ class Cache implements ICache {
/**
* get the stored metadata of a file or folder
*
* the returned cache entry contains at least the following values:
* [
* 'fileid' => int, the numeric id of a file (see getId)
* 'storage' => int, the numeric id of the storage the file is stored on
* 'path' => string, the path of the file within the storage ('foo/bar.txt')
* 'name' => string, the basename of a file ('bar.txt)
* 'mimetype' => string, the full mimetype of the file ('text/plain')
* 'mimepart' => string, the first half of the mimetype ('text')
* 'size' => int, the size of the file or folder in bytes
* 'mtime' => int, the last modified date of the file as unix timestamp as shown in the ui
* 'storage_mtime' => int, the last modified date of the file as unix timestamp as stored on the storage
* Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
* This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
* 'etag' => string, the etag for the file
* An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
* Etag for folders change whenever a file in the folder has changed
* 'permissions' int, the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
* \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
* ]
*
* @param string | int $file either the path of a file or folder or the file id for a file or folder
* @return array|false the cache entry as array of false if the file is not found in the cache
* @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
*/
public function get($file) {
if (is_string($file) or $file == '') {
@ -157,6 +133,7 @@ class Cache implements ICache {
if (isset($this->partial[$file])) {
$data = $this->partial[$file];
}
return $data;
} else {
//fix types
$data['fileid'] = (int)$data['fileid'];
@ -172,16 +149,15 @@ class Cache implements ICache {
$data['storage_mtime'] = $data['mtime'];
}
$data['permissions'] = (int)$data['permissions'];
return new CacheEntry($data);
}
return $data;
}
/**
* get the metadata of all files stored in $folder
*
* @param string $folder
* @return array
* @return ICacheEntry[]
*/
public function getFolderContents($folder) {
$fileId = $this->getId($folder);
@ -192,7 +168,7 @@ class Cache implements ICache {
* get the metadata of all files stored in $folder
*
* @param int $fileId the file id of the folder
* @return array
* @return ICacheEntry[]
*/
public function getFolderContentsById($fileId) {
if ($fileId > -1) {
@ -212,7 +188,9 @@ class Cache implements ICache {
$file['storage_mtime'] = (int)$file['storage_mtime'];
$file['size'] = 0 + $file['size'];
}
return $files;
return array_map(function (array $data) {
return new CacheEntry($data);
}, $files);
} else {
return array();
}
@ -393,7 +371,7 @@ class Cache implements ICache {
return -1;
} else {
$parent = $this->getParentPath($file);
return (int) $this->getId($parent);
return (int)$this->getId($parent);
}
}
@ -482,12 +460,12 @@ class Cache implements ICache {
/**
* Move a file or folder in the cache
*
* @param \OC\Files\Cache\Cache $sourceCache
* @param \OCP\Files\Cache\ICache $sourceCache
* @param string $sourcePath
* @param string $targetPath
* @throws \OC\DatabaseException
*/
public function moveFromCache(Cache $sourceCache, $sourcePath, $targetPath) {
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
// normalize source and target
$sourcePath = $this->normalize($sourcePath);
$targetPath = $this->normalize($targetPath);
@ -572,7 +550,7 @@ class Cache implements ICache {
* search for files matching $pattern
*
* @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
* @return array an array of cache entries where the name matches the search pattern
* @return ICacheEntry[] an array of cache entries where the name matches the search pattern
*/
public function search($pattern) {
// normalize pattern
@ -595,7 +573,9 @@ class Cache implements ICache {
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
$files[] = $row;
}
return $files;
return array_map(function(array $data) {
return new CacheEntry($data);
}, $files);
}
/**
@ -603,7 +583,7 @@ class Cache implements ICache {
*
* @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
* where it will search for all mimetypes in the group ('image/*')
* @return array an array of cache entries where the mimetype matches the search
* @return ICacheEntry[] an array of cache entries where the mimetype matches the search
*/
public function searchByMime($mimetype) {
if (strpos($mimetype, '/')) {
@ -621,7 +601,9 @@ class Cache implements ICache {
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
$files[] = $row;
}
return $files;
return array_map(function (array $data) {
return new CacheEntry($data);
}, $files);
}
/**
@ -631,7 +613,7 @@ class Cache implements ICache {
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array file data
* @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
@ -666,7 +648,9 @@ class Cache implements ICache {
while ($row = $result->fetch()) {
$files[] = $row;
}
return $files;
return array_map(function (array $data) {
return new CacheEntry($data);
}, $files);
}
/**

View File

@ -107,4 +107,8 @@ class CacheEntry implements ICacheEntry, \ArrayAccess {
public function isEncrypted() {
return isset($this->data['encrypted']) && $this->data['encrypted'];
}
public function getData() {
return $this->data;
}
}

View File

@ -26,6 +26,8 @@
namespace OC\Files\Cache;
use OCP\Files\Cache\ICacheEntry;
class HomeCache extends Cache {
/**
* get the size of a folder and set it in the cache
@ -67,7 +69,7 @@ class HomeCache extends Cache {
/**
* @param string $path
* @return array
* @return ICacheEntry
*/
public function get($path) {
$data = parent::get($path);

View File

@ -160,6 +160,7 @@ class Scanner extends BasicEmitter {
$data['parent'] = $parentId;
}
if (is_null($cacheData)) {
/** @var CacheEntry $cacheData */
$cacheData = $this->cache->get($file);
}
if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) {
@ -182,7 +183,7 @@ class Scanner extends BasicEmitter {
}
}
// Only update metadata that has changed
$newData = array_diff_assoc($data, $cacheData);
$newData = array_diff_assoc($data, $cacheData->getData());
} else {
$newData = $data;
$fileId = -1;

View File

@ -25,6 +25,7 @@
namespace OC\Files\Cache\Wrapper;
use OC\Files\Cache\Cache;
use OCP\Files\Cache\ICacheEntry;
class CacheWrapper extends Cache {
/**
@ -42,8 +43,8 @@ class CacheWrapper extends Cache {
/**
* Make it easy for wrappers to modify every returned cache entry
*
* @param array $entry
* @return array
* @param ICacheEntry $entry
* @return ICacheEntry
*/
protected function formatCacheEntry($entry) {
return $entry;
@ -53,7 +54,7 @@ class CacheWrapper extends Cache {
* get the stored metadata of a file or folder
*
* @param string /int $file
* @return array|false
* @return ICacheEntry|false
*/
public function get($file) {
$result = $this->cache->get($file);
@ -67,7 +68,7 @@ class CacheWrapper extends Cache {
* get the metadata of all files stored in $folder
*
* @param string $folder
* @return array
* @return ICacheEntry[]
*/
public function getFolderContents($folder) {
// cant do a simple $this->cache->.... call here since getFolderContentsById needs to be called on this
@ -178,7 +179,7 @@ class CacheWrapper extends Cache {
* search for files matching $pattern
*
* @param string $pattern
* @return array an array of file data
* @return ICacheEntry[] an array of file data
*/
public function search($pattern) {
$results = $this->cache->search($pattern);
@ -189,7 +190,7 @@ class CacheWrapper extends Cache {
* search for files by mimetype
*
* @param string $mimetype
* @return array
* @return ICacheEntry[]
*/
public function searchByMime($mimetype) {
$results = $this->cache->searchByMime($mimetype);
@ -201,7 +202,7 @@ class CacheWrapper extends Cache {
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array file data
* @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
$results = $this->cache->searchByTag($tag, $userId);

View File

@ -29,6 +29,7 @@
namespace OC\Files;
use OCP\Files\Cache\ICacheEntry;
use OCP\IUser;
class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
@ -71,7 +72,7 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
* @param string|boolean $path
* @param Storage\Storage $storage
* @param string $internalPath
* @param array $data
* @param array|ICacheEntry $data
* @param \OCP\Files\Mount\IMountPoint $mount
* @param \OCP\IUser|null $owner
*/

View File

@ -28,6 +28,7 @@ namespace OC\Files\Storage\Wrapper;
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
use OC\Encryption\Update;
use OC\Encryption\Util;
use OC\Files\Cache\CacheEntry;
use OC\Files\Filesystem;
use OC\Files\Mount\Manager;
use OC\Files\Storage\LocalTempFileTrait;
@ -123,13 +124,14 @@ class Encryption extends Wrapper {
public function filesize($path) {
$fullPath = $this->getFullPath($path);
/** @var CacheEntry $info */
$info = $this->getCache()->get($path);
if (isset($this->unencryptedSize[$fullPath])) {
$size = $this->unencryptedSize[$fullPath];
// update file cache
$info['encrypted'] = true;
$info['size'] = $size;
$this->getCache()->put($path, $info);
$this->getCache()->put($path, $info->getData());
return $size;
}

View File

@ -47,6 +47,7 @@ use OC\Files\Cache\Updater;
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
use OC\User\User;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileNameTooLongException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
@ -1274,7 +1275,7 @@ class View {
if ($storage) {
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
if (!is_array($data)) {
if (!$data instanceof ICacheEntry) {
return false;
}
@ -1334,7 +1335,7 @@ class View {
$data = $this->getCacheEntry($storage, $internalPath, $directory);
if (!is_array($data) || !isset($data['fileid'])) {
if (!$data instanceof ICacheEntry || !isset($data['fileid'])) {
return [];
}
@ -1345,7 +1346,7 @@ class View {
/**
* @var \OC\Files\FileInfo[] $files
*/
$files = array_map(function (array $content) use ($path, $storage, $mount, $sharingDisabled) {
$files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
if ($sharingDisabled) {
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}

View File

@ -47,28 +47,8 @@ interface ICache {
/**
* get the stored metadata of a file or folder
*
* the returned cache entry contains at least the following values:
* [
* 'fileid' => int, the numeric id of a file (see getId)
* 'storage' => int, the numeric id of the storage the file is stored on
* 'path' => string, the path of the file within the storage ('foo/bar.txt')
* 'name' => string, the basename of a file ('bar.txt)
* 'mimetype' => string, the full mimetype of the file ('text/plain')
* 'mimepart' => string, the first half of the mimetype ('text')
* 'size' => int, the size of the file or folder in bytes
* 'mtime' => int, the last modified date of the file as unix timestamp as shown in the ui
* 'storage_mtime' => int, the last modified date of the file as unix timestamp as stored on the storage
* Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
* This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
* 'etag' => string, the etag for the file
* An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
* Etag for folders change whenever a file in the folder has changed
* 'permissions' int, the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
* \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
* ]
*
* @param string | int $file either the path of a file or folder or the file id for a file or folder
* @return array|false the cache entry as array of 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
*/
public function get($file);
@ -76,7 +56,7 @@ interface ICache {
* get the metadata of all files stored in $folder
*
* @param string $folder
* @return array
* @return ICacheEntry[]
*/
public function getFolderContents($folder);
@ -84,7 +64,7 @@ interface ICache {
* get the metadata of all files stored in $folder
*
* @param int $fileId the file id of the folder
* @return array
* @return ICacheEntry[]
*/
public function getFolderContentsById($fileId);
@ -185,7 +165,7 @@ interface ICache {
* search for files matching $pattern
*
* @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
* @return array an array of cache entries where the name matches the search pattern
* @return ICacheEntry[] an array of cache entries where the name matches the search pattern
*/
public function search($pattern);
@ -194,7 +174,7 @@ interface ICache {
*
* @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
* where it will search for all mimetypes in the group ('image/*')
* @return array an array of cache entries where the mimetype matches the search
* @return ICacheEntry[] an array of cache entries where the mimetype matches the search
*/
public function searchByMime($mimetype);
@ -205,7 +185,7 @@ interface ICache {
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array file data
* @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId);