Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Store file id in extended attributes where possible #14546

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/private/files/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getMimetypeId($mime) {
try {
$connection = \OC_DB::getConnection();
$connection->insertIfNotExist('*PREFIX*mimetypes', [
'mimetype' => $mime,
'mimetype' => $mime,
]);
$this->loadMimetypes();
} catch (\Doctrine\DBAL\DBALException $e) {
Expand Down Expand Up @@ -236,10 +236,11 @@ public function getFolderContentsById($fileId) {
*
* @param string $file
* @param array $data
*
* @param bool | int $fileId
* @return int file id
* @throws \OC\DatabaseException
*/
public function put($file, array $data) {
public function put($file, array $data, $fileId = false) {
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
Expand Down Expand Up @@ -267,6 +268,10 @@ public function put($file, array $data) {
list($queryParts, $params) = $this->buildParts($data);
$queryParts[] = '`storage`';
$params[] = $this->getNumericStorageId();
if ($fileId) {
$queryParts[] = 'fileid';
$params[] = $fileId;
}
$valuesPlaceholder = array_fill(0, count($queryParts), '?');

$sql = 'INSERT INTO `*PREFIX*filecache` (' . implode(', ', $queryParts) . ')'
Expand Down
17 changes: 16 additions & 1 deletion lib/private/files/cache/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,27 @@ protected function removeFromCache($path) {
protected function addToCache($path, $data, $fileId = -1) {
\OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data));
$this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data));
if ($this->storage->instanceOfStorage('OCP\Files\Storage\IAttribute') and $fileId === -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean you need to add getAttribute and setAttribute to the default storage wrapper, to make sure they are forwarded to the underlying storage ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there are magic methods to handle that

$oldId = (int)$this->storage->getAttribute($path, 'oc-file-id');
if ($oldId and $oldId !== $fileId) {
\OC::$server->getLogger()->warning('Found "new" file with existing file id: ' . $path);
if ($this->cache->getPathById($oldId)) { // check if the file is moved externally or deleted from the cache
$fileId = $oldId;
}
}
} else {
$oldId = false;
}
if ($this->cacheActive) {
if ($fileId !== -1) {
$this->cache->update($fileId, $data);
return $fileId;
} else {
return $this->cache->put($path, $data);
$fileId = $this->cache->put($path, $data, $oldId);
if ($this->storage->instanceOfStorage('OCP\Files\Storage\IAttribute')) {
$this->storage->setAttribute($path, 'oc-file-id', $fileId);
}
return $fileId;
}
} else {
return -1;
Expand Down
32 changes: 31 additions & 1 deletion lib/private/files/storage/local.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
namespace OC\Files\Storage;

use OCP\Files\Storage\IAttribute;

if (\OC_Util::runningOnWindows()) {
class Local extends MappedLocal {

Expand All @@ -43,7 +45,7 @@ class Local extends MappedLocal {
/**
* for local filestore, we only have to map the paths
*/
class Local extends \OC\Files\Storage\Common {
class Local extends \OC\Files\Storage\Common implements IAttribute {
protected $datadir;

public function __construct($arguments) {
Expand Down Expand Up @@ -339,5 +341,33 @@ public function getETag($path) {
return parent::getETag($path);
}
}

/**
* Set an attribute on a file
*
* @param string $path
* @param string $name
* @param string $value
*/
public function setAttribute($path, $name, $value) {
if (function_exists('xattr_set')) {
xattr_set($this->getSourcePath($path), $name, $value);
}
}

/**
* Get an attribute from a file
*
* @param string $path
* @param string $name
* @return string | bool
*/
public function getAttribute($path, $name) {
if (function_exists('xattr_get')) {
return xattr_get($this->getSourcePath($path), $name);
} else {
return false;
}
}
}
}
41 changes: 41 additions & 0 deletions lib/public/files/storage/iattribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
*
* @copyright Copyright (c) 2015, 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 OCP\Files\Storage;

interface IAttribute {
/**
* Set an attribute on a file
*
* @param string $path
* @param string $name
* @param string $value
*/
public function setAttribute($path, $name, $value);

/**
* Get an attribute from a file
*
* @param string $path
* @param string $name
* @return string | bool
*/
public function getAttribute($path, $name);
}