-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize join for sharedWith by moving logic to DB
- Loading branch information
Showing
10 changed files
with
513 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* @author phisch <[email protected]> | ||
* @author Roeland Jago Douma <[email protected]> | ||
* @author Vincent Petry <[email protected]> | ||
* @author Piotr Mrowczynski <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
|
@@ -792,6 +793,124 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) { | |
return $shares; | ||
} | ||
|
||
/** | ||
* Performs DB call to obtain all shared with specific groups or user for specific node predicate (if any) | ||
* It also segregates results in chunks of result(array) batches e.g. { { array, array, ...}[100], { array, array, array }[3] }[2] | ||
* | ||
* @param IGroup[] $sharedWithGroup | ||
* @param string $userId | ||
* @param Node|null $node | ||
* @return array $chunkedResults | ||
*/ | ||
private function getChunkedAllSharedWith($sharedWithGroup, $userId, $node) { | ||
$sharedWithGroupChunks = array_chunk($sharedWithGroup, 100); | ||
|
||
// Check how many group chunks do we need | ||
$sharedWithGroupChunksNo = count($sharedWithGroupChunks); | ||
|
||
// Check that if there are no groups for users, we still query for a shared with user | ||
$chunkNoRequired = $sharedWithGroupChunksNo > 0 ? $sharedWithGroupChunksNo : 1; | ||
|
||
for ($chunkNo = 0; $chunkNo < $chunkNoRequired; $chunkNo++) { | ||
// Query for user/group shares | ||
$qb = $this->dbConn->getQueryBuilder(); | ||
$qb->select('s.*', 'f.fileid', 'f.path') | ||
->selectAlias('st.id', 'storage_string_id') | ||
->from('share', 's') | ||
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) | ||
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) | ||
->orderBy('s.id'); | ||
|
||
// Apply predicate on item_type | ||
$qb->where($qb->expr()->orX( | ||
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')), | ||
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) | ||
)); | ||
|
||
// Apply predicate on node if provided | ||
if ($node !== null) { | ||
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); | ||
} | ||
|
||
// Handle chunking logic | ||
if ($chunkNo === 0 && $sharedWithGroupChunksNo > 0) { | ||
// There are groups to be checked and this is first chunk | ||
// In the first chunk, check for shared with user along with groups | ||
$groups = $sharedWithGroupChunks[$chunkNo]; | ||
$qb->andWhere($qb->expr()->orX( | ||
$qb->expr()->andX( | ||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), | ||
$qb->expr()->in('share_with', $qb->createNamedParameter( | ||
$groups, | ||
IQueryBuilder::PARAM_STR_ARRAY | ||
)) | ||
), | ||
$qb->expr()->andX( | ||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), | ||
$qb->expr()->eq('share_with', $qb->createNamedParameter($userId)) | ||
) | ||
)); | ||
} else if ($chunkNo > 0 && $sharedWithGroupChunksNo > 0) { | ||
// There are groups to be checked and this is consecutive chunk | ||
$groups = $sharedWithGroupChunks[$chunkNo]; | ||
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))); | ||
$qb->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( | ||
$groups, | ||
IQueryBuilder::PARAM_STR_ARRAY | ||
)) | ||
); | ||
} else { | ||
// There are no groups to be checked | ||
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); | ||
$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); | ||
} | ||
|
||
$cursor = $qb->execute(); | ||
$chunkedResults[] = $cursor->fetchAll(); | ||
$cursor->closeCursor(); | ||
} | ||
return $chunkedResults; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getAllSharedWith($userId, $node) { | ||
// Create array of sharedWith objects (target user -> $userId or group of which user is a member | ||
$user = $this->userManager->get($userId); | ||
$allGroups = $this->groupManager->getUserGroups($user, 'sharing'); | ||
|
||
/** @var Share[] $resolvedShares */ | ||
$resolvedShares = []; | ||
$groupShares = []; | ||
|
||
// Check if user is member of some groups and chunk them | ||
$sharedWithGroup = array_map(function(IGroup $group) { return $group->getGID(); }, $allGroups); | ||
|
||
$chunkedResults = $this->getChunkedAllSharedWith($sharedWithGroup, $userId, $node); | ||
|
||
foreach($chunkedResults as $resultBatch) { | ||
foreach($resultBatch as $data) { | ||
if ($this->isAccessibleResult($data)) { | ||
$share = $this->createShare($data); | ||
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP){ | ||
$groupShares[] = $share; | ||
} else { | ||
$resolvedShares[] = $share; | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Resolve all group shares to user specific shares | ||
if (!empty($groupShares)) { | ||
$resolvedGroupShares = $this->resolveGroupShares($groupShares, $userId); | ||
$resolvedShares = array_merge($resolvedShares, $resolvedGroupShares); | ||
} | ||
|
||
return $resolvedShares; | ||
} | ||
|
||
/** | ||
* Get a share by token | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.