1313use OC \Preview \Db \Preview ;
1414use OC \Preview \Db \PreviewMapper ;
1515use OC \Preview \Storage \StorageFactory ;
16+ use OCP \DB \QueryBuilder \IQueryBuilder ;
1617use OCP \IDBConnection ;
1718
1819class PreviewService {
@@ -34,36 +35,52 @@ public function deletePreview(Preview $preview): void {
3435 * @return \Generator<array{storageId: int, fileIds: int[]}>
3536 */
3637 public function getAvailableFileIds (): \Generator {
37- $ maxQb = $ this ->connection ->getQueryBuilder ();
38- $ maxQb ->select ($ maxQb ->func ()->max ('id ' ))
39- ->from ($ this ->previewMapper ->getTableName ())
40- ->groupBy ('file_id ' );
41-
42- $ qb = $ this ->connection ->getQueryBuilder ();
43- $ qb ->select ('file_id ' , 'storage_id ' )
44- ->from ($ this ->previewMapper ->getTableName ())
45- ->where ($ qb ->expr ()->in ('id ' , $ qb ->createFunction ($ maxQb ->getSQL ())));
46-
47- $ result = $ qb ->executeQuery ();
48-
49- $ lastStorageId = -1 ;
50- /** @var int[] $fileIds */
51- $ fileIds = [];
52-
53- // Previews next to each others in the database are likely in the same storage, so group them
54- while ($ row = $ result ->fetch ()) {
55- if ($ lastStorageId !== (int )$ row ['storage_id ' ]) {
56- if ($ lastStorageId !== -1 ) {
57- yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
58- $ fileIds = [];
38+ $ lastId = null ;
39+ while (true ) {
40+ $ maxQb = $ this ->connection ->getQueryBuilder ();
41+ $ maxQb ->select ($ maxQb ->func ()->max ('id ' ))
42+ ->from ($ this ->previewMapper ->getTableName ())
43+ ->groupBy ('file_id ' )
44+ ->setMaxResults (IQueryBuilder::MAX_IN_PARAMS );
45+
46+ $ qb = $ this ->connection ->getQueryBuilder ();
47+
48+ if ($ lastId !== null ) {
49+ $ maxQb ->andWhere ($ maxQb ->expr ()->gt ('id ' , $ qb ->createNamedParameter ($ lastId )));
50+ }
51+
52+ $ qb ->select ('id ' , 'file_id ' , 'storage_id ' )
53+ ->from ($ this ->previewMapper ->getTableName ())
54+ ->where ($ qb ->expr ()->in ('id ' , $ qb ->createFunction ($ maxQb ->getSQL ())));
55+
56+ $ result = $ qb ->executeQuery ();
57+
58+ $ lastStorageId = -1 ;
59+ /** @var int[] $fileIds */
60+ $ fileIds = [];
61+
62+ $ found = false ;
63+ // Previews next to each others in the database are likely in the same storage, so group them
64+ while ($ row = $ result ->fetch ()) {
65+ $ found = true ;
66+ if ($ lastStorageId !== (int )$ row ['storage_id ' ]) {
67+ if ($ lastStorageId !== -1 ) {
68+ yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
69+ $ fileIds = [];
70+ }
71+ $ lastStorageId = (int )$ row ['storage_id ' ];
5972 }
60- $ lastStorageId = (int )$ row ['storage_id ' ];
73+ $ fileIds [] = (int )$ row ['file_id ' ];
74+ $ lastId = $ row ['id ' ];
75+ }
76+
77+ if (count ($ fileIds ) > 0 ) {
78+ yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
6179 }
62- $ fileIds [] = (int )$ row ['file_id ' ];
63- }
6480
65- if (count ($ fileIds ) > 0 ) {
66- yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
81+ if (!$ found ) {
82+ break ;
83+ }
6784 }
6885 }
6986
0 commit comments