mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
Get all shares as iterable
Sometimes we need all shares or rather a specific subset of shares but creating dedicated functions is a pain. This just returns an iterable object for all shares so we can loop over them without allocating all the memory on the system. It should not be used by any user called code. But in an occ command or background job it is fine IMO. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
380563fd53
commit
8085ca4cc4
6 changed files with 104 additions and 0 deletions
|
|
@ -1100,4 +1100,31 @@ class FederatedShareProvider implements IShareProvider {
|
|||
|
||||
return ['remote' => $remote];
|
||||
}
|
||||
|
||||
public function getAllShares(): iterable {
|
||||
$qb = $this->dbConnection->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from('share')
|
||||
->where(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE)),
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE_GROUP))
|
||||
)
|
||||
);
|
||||
|
||||
$cursor = $qb->execute();
|
||||
while($data = $cursor->fetch()) {
|
||||
try {
|
||||
$share = $this->createShareObject($data);
|
||||
} catch (InvalidShare $e) {
|
||||
continue;
|
||||
} catch (ShareNotFound $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $share;
|
||||
}
|
||||
$cursor->closeCursor();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1166,4 +1166,29 @@ class ShareByMailProvider implements IShareProvider {
|
|||
return ['public' => $mail];
|
||||
}
|
||||
|
||||
public function getAllShares(): iterable {
|
||||
$qb = $this->dbConnection->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from('share')
|
||||
->where(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_EMAIL))
|
||||
)
|
||||
);
|
||||
|
||||
$cursor = $qb->execute();
|
||||
while($data = $cursor->fetch()) {
|
||||
try {
|
||||
$share = $this->createShareObject($data);
|
||||
} catch (InvalidShare $e) {
|
||||
continue;
|
||||
} catch (ShareNotFound $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $share;
|
||||
}
|
||||
$cursor->closeCursor();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1382,4 +1382,30 @@ class DefaultShareProvider implements IShareProvider {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public function getAllShares(): iterable {
|
||||
$qb = $this->dbConn->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from('share')
|
||||
->where(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
|
||||
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
|
||||
)
|
||||
);
|
||||
|
||||
$cursor = $qb->execute();
|
||||
while($data = $cursor->fetch()) {
|
||||
try {
|
||||
$share = $this->createShare($data);
|
||||
} catch (InvalidShare $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $share;
|
||||
}
|
||||
$cursor->closeCursor();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1671,4 +1671,11 @@ class Manager implements IManager {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function getAllShares(): iterable {
|
||||
$providers = $this->factory->getAllProviders();
|
||||
|
||||
foreach ($providers as $provider) {
|
||||
yield from $provider->getAllShares();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -385,4 +385,15 @@ interface IManager {
|
|||
*/
|
||||
public function shareProviderExists($shareType);
|
||||
|
||||
/**
|
||||
* @Internal
|
||||
*
|
||||
* Get all the shares as iterable to reduce memory overhead
|
||||
* Note, since this opens up database cursors the iterable should
|
||||
* be fully itterated.
|
||||
*
|
||||
* @return iterable
|
||||
*/
|
||||
public function getAllShares(): iterable;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -217,4 +217,12 @@ interface IShareProvider {
|
|||
* @since 12
|
||||
*/
|
||||
public function getAccessList($nodes, $currentAccess);
|
||||
|
||||
/**
|
||||
* Get all the shares in this provider returned as iterable to reduce memory
|
||||
* overhead
|
||||
*
|
||||
* @return iterable
|
||||
*/
|
||||
public function getAllShares(): iterable;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue