Skip to content

Commit

Permalink
let the share backend get the node cacheentry to save queries
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Nov 17, 2016
1 parent 065753f commit 2f03fca
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 27 deletions.
3 changes: 3 additions & 0 deletions apps/files_sharing/lib/MountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ private function buildSuperShares(array $allShares, \OCP\IUser $user) {
$share->setTarget($superShare->getTarget());
$this->shareManager->moveShare($share, $user->getUID());
}
if (!is_null($share->getNodeCacheEntry())) {
$superShare->setNodeCacheEntry($share->getNodeCacheEntry());
}
}

$superShare->setPermissions($permissions);
Expand Down
32 changes: 24 additions & 8 deletions apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedSto
*/
private $logger;

private $options;

public function __construct($arguments) {
$this->ownerView = $arguments['ownerView'];
$this->logger = \OC::$server->getLogger();
Expand All @@ -86,6 +88,20 @@ public function __construct($arguments) {
]);
}

/**
* @return ICacheEntry
*/
private function getSourceRootInfo() {
if (is_null($this->sourceRootInfo)) {
if (is_null($this->superShare->getNodeCacheEntry())) {
$this->sourceRootInfo = $this->storage->getCache()->get($this->rootPath);
} else {
$this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
}
}
return $this->sourceRootInfo;
}

private function init() {
if ($this->initialized) {
return;
Expand All @@ -95,7 +111,6 @@ private function init() {
Filesystem::initMountPoints($this->superShare->getShareOwner());
$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
list($this->storage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
$this->sourceRootInfo = $this->storage->getCache()->get($this->rootPath);
} catch (NotFoundException $e) {
$this->storage = new FailedStorage(['exception' => $e]);
$this->rootPath = '';
Expand All @@ -110,6 +125,9 @@ private function init() {
* @inheritdoc
*/
public function instanceOfStorage($class) {
if ($class === '\OC\Files\Storage\Common') {
return true;
}
if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
return false;
}
Expand All @@ -124,8 +142,7 @@ public function getShareId() {
}

private function isValid() {
$this->init();
return $this->sourceRootInfo && ($this->sourceRootInfo->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
}

/**
Expand Down Expand Up @@ -314,14 +331,10 @@ public function getCache($path = '', $storage = null) {
if ($this->cache) {
return $this->cache;
}
$this->init();
if (is_null($this->storage) || $this->storage instanceof FailedStorage) {
return new FailedCache(false);
}
if (!$storage) {
$storage = $this;
}
$this->cache = new \OCA\Files_Sharing\Cache($storage, $this->storage, $this->sourceRootInfo);
$this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare);
return $this->cache;
}

Expand Down Expand Up @@ -449,4 +462,7 @@ public function file_put_contents($path, $data) {
return parent::file_put_contents($path, $data);
}

public function setMountOptions(array $options) {
$this->mountOptions = $options;
}
}
44 changes: 28 additions & 16 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,37 @@ public function get($file) {
}
return $data;
} else {
//fix types
$data['fileid'] = (int)$data['fileid'];
$data['parent'] = (int)$data['parent'];
$data['size'] = 0 + $data['size'];
$data['mtime'] = (int)$data['mtime'];
$data['storage_mtime'] = (int)$data['storage_mtime'];
$data['encryptedVersion'] = (int)$data['encrypted'];
$data['encrypted'] = (bool)$data['encrypted'];
$data['storage'] = $this->storageId;
$data['mimetype'] = $this->mimetypeLoader->getMimetypeById($data['mimetype']);
$data['mimepart'] = $this->mimetypeLoader->getMimetypeById($data['mimepart']);
if ($data['storage_mtime'] == 0) {
$data['storage_mtime'] = $data['mtime'];
}
$data['permissions'] = (int)$data['permissions'];
return new CacheEntry($data);
return self::cacheEntryFromData($data, $this->storageId, $this->mimetypeLoader);
}
}

/**
* Create a CacheEntry from database row
*
* @param array $data
* @param string $storageId
* @param IMimeTypeLoader $mimetypeLoader
* @return CacheEntry
*/
public static function cacheEntryFromData($data, $storageId, IMimeTypeLoader $mimetypeLoader) {
//fix types
$data['fileid'] = (int)$data['fileid'];
$data['parent'] = (int)$data['parent'];
$data['size'] = 0 + $data['size'];
$data['mtime'] = (int)$data['mtime'];
$data['storage_mtime'] = (int)$data['storage_mtime'];
$data['encryptedVersion'] = (int)$data['encrypted'];
$data['encrypted'] = (bool)$data['encrypted'];
$data['storage'] = $storageId;
$data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']);
$data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']);
if ($data['storage_mtime'] == 0) {
$data['storage_mtime'] = $data['mtime'];
}
$data['permissions'] = (int)$data['permissions'];
return new CacheEntry($data);
}

/**
* get the metadata of all files stored in $folder
*
Expand Down
21 changes: 18 additions & 3 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
namespace OC\Share20;

use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheEntry;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Share\IShareProvider;
Expand Down Expand Up @@ -571,7 +573,7 @@ public function getShareById($id, $recipientId = null) {
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
));

$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
Expand Down Expand Up @@ -656,7 +658,11 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
//Get shares directly with this user
$qb = $this->dbConn->getQueryBuilder();
$qb->select('s.*', 'f.fileid', 'f.path')
$qb->select('s.*',
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
)
->selectAlias('st.id', 'storage_string_id')
->from('share', 's')
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
Expand Down Expand Up @@ -798,7 +804,7 @@ public function getShareByToken($token) {

return $share;
}

/**
* Create a share object from an database row
*
Expand Down Expand Up @@ -838,6 +844,15 @@ private function createShare($data) {
$share->setExpirationDate($expiration);
}

if (isset($data['f_permissions'])) {
$entryData = $data;
$entryData['permissions'] = $entryData['f_permissions'];
$entryData['parent'] = $entryData['f_parent'];;
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
$entryData['storage_string_id'],
\OC::$server->getMimeTypeLoader()));
}

$share->setProviderId($this->identifier());

return $share;
Expand Down
18 changes: 18 additions & 0 deletions lib/private/Share20/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OC\Share20;

use OCP\Files\Cache\ICacheEntry;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
Expand Down Expand Up @@ -72,6 +73,9 @@ class Share implements \OCP\Share\IShare {
/** @var IUserManager */
private $userManager;

/** @var ICacheEntry|null */
private $nodeCacheEntry;

public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
Expand Down Expand Up @@ -418,4 +422,18 @@ public function setMailSend($mailSend) {
public function getMailSend() {
return $this->mailSend;
}

/**
* @inheritdoc
*/
public function setNodeCacheEntry(ICacheEntry $entry) {
$this->nodeCacheEntry = $entry;
}

/**
* @inheritdoc
*/
public function getNodeCacheEntry() {
return $this->nodeCacheEntry;
}
}
17 changes: 17 additions & 0 deletions lib/public/Share/IShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace OCP\Share;

use OCP\Files\Cache\ICacheEntry;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
Expand Down Expand Up @@ -324,4 +325,20 @@ public function setMailSend($mailSend);
* @since 9.0.0
*/
public function getMailSend();

/**
* Set the cache entry for the shared node
*
* @param ICacheEntry $entry
* @since 11.0.0
*/
public function setNodeCacheEntry(ICacheEntry $entry);

/**
* Get the cache entry for the shared node
*
* @return null|ICacheEntry
* @since 11.0.0
*/
public function getNodeCacheEntry();
}

0 comments on commit 2f03fca

Please sign in to comment.