]> source.dussan.org Git - nextcloud-server.git/commitdiff
Get all shares as iterable
authorRoeland Jago Douma <roeland@famdouma.nl>
Tue, 29 Oct 2019 15:56:06 +0000 (16:56 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Wed, 30 Oct 2019 08:45:27 +0000 (09:45 +0100)
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>
apps/federatedfilesharing/lib/FederatedShareProvider.php
apps/sharebymail/lib/ShareByMailProvider.php
lib/private/Share20/DefaultShareProvider.php
lib/private/Share20/Manager.php
lib/public/Share/IManager.php
lib/public/Share/IShareProvider.php

index 67089cba9d37cdd07af6baf392dc2c0821d03613..4d93dec18f7f307eb5c59a0e3fd8c5a23c3cce23 100644 (file)
@@ -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();
+       }
 }
index 9bfe5e733f49fe8369c2a2c6f35a534b384e7692..903df175e9cc63c3719867b0c6174a1dc9cf6fac 100644 (file)
@@ -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();
+       }
 }
index aea50dfcdb6e28e1d8a2ebbfb177c69f32f5226f..05b3094e6a20133186ca234c8895fe3fde10dcb7 100644 (file)
@@ -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();
+       }
 }
index 2e8e6f9a3af73a622da390eb1b2777b706afbc51..df537062e4ab1470f7267ca5fd2f9eeb10d193a2 100644 (file)
@@ -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();
+               }
+       }
 }
index 302be5233273c43270db4e1dc4b7535e95ed1429..0c47eb730fd98265f5c2aa99117b4396e92fcaa6 100644 (file)
@@ -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;
+
 }
index 6731bf8882b293af3162518d6658f0aa54fbf5fd..da29a7b98aa1e78eb87c94f949270985b001fea1 100644 (file)
@@ -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;
 }