浏览代码

Merge pull request #6037 from nextcloud/ezaquarii/implement-query-pagination-in-uploads-storage-manager

Add SQL query pagination in UploadStorageManager
Tobias Kaminsky 5 年之前
父节点
当前提交
6163badbb7
共有 1 个文件被更改,包括 70 次插入24 次删除
  1. 70 24
      src/main/java/com/owncloud/android/datamodel/UploadsStorageManager.java

+ 70 - 24
src/main/java/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -39,7 +39,10 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.UploadFileOperation;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
+import java.util.Locale;
 import java.util.Observable;
 
 import androidx.annotation.Nullable;
@@ -284,35 +287,78 @@ public class UploadsStorageManager extends Observable {
     }
 
     private OCUpload[] getUploads(@Nullable String selection, @Nullable String... selectionArgs) {
-        OCUpload[] list;
+        ArrayList<OCUpload> uploads = new ArrayList<>();
+        final long pageSize = 100;
+        long page = 0;
+        long rowsRead;
+        long rowsTotal = 0;
+        long lastRowID = -1;
+
+        do {
+            String pageSelection = selection;
+            String[] pageSelectionArgs = selectionArgs;
+            if (page > 0 && lastRowID >= 0) {
+                if (selection != null) {
+                    pageSelection = "(" + selection + ") AND _id < ?";
+                } else {
+                    pageSelection = "_id < ?";
+                }
+                if (selectionArgs != null) {
+                    pageSelectionArgs = Arrays.copyOf(selectionArgs, selectionArgs.length + 1);
+                } else {
+                    pageSelectionArgs = new String[1];
+                }
+                pageSelectionArgs[pageSelectionArgs.length - 1] = String.valueOf(lastRowID);
+                Log_OC.d(TAG, String.format(Locale.ENGLISH, "QUERY: %s ROWID: %d", pageSelection, lastRowID));
+            } else {
+                Log_OC.d(TAG, String.format(Locale.ENGLISH, "QUERY: %s ROWID: %d", selection, lastRowID));
+            }
+            rowsRead = 0;
 
-        Cursor c = getDB().query(
+            Cursor c = getDB().query(
                 ProviderTableMeta.CONTENT_URI_UPLOADS,
                 null,
-                selection,
-                selectionArgs,
-                null
-        );
-
-        if (c != null) {
-            list = new OCUpload[c.getCount()];
-
-            if (c.moveToFirst()) {
-                do {
-                    OCUpload upload = createOCUploadFromCursor(c);
-                    if (upload == null) {
-                        Log_OC.e(TAG, "OCUpload could not be created from cursor");
-                    } else {
-                        list[c.getPosition()] = upload;
-                    }
-                } while (c.moveToNext() && !c.isAfterLast());
+                pageSelection,
+                pageSelectionArgs,
+                String.format(Locale.ENGLISH, "_id DESC LIMIT %d", pageSize)
+                                    );
+
+            if (c != null) {
+                if (c.moveToFirst()) {
+                    do {
+                        rowsRead++;
+                        rowsTotal++;
+                        lastRowID = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
+                        OCUpload upload = createOCUploadFromCursor(c);
+                        if (upload == null) {
+                            Log_OC.e(TAG, "OCUpload could not be created from cursor");
+                        } else {
+                            uploads.add(upload);
+                        }
+                    } while (c.moveToNext() && !c.isAfterLast());
+                }
+                c.close();
+                Log_OC.v(TAG, String.format(Locale.ENGLISH,
+                                            "getUploads() got %d rows from page %d, %d rows total so far, last ID %d",
+                                            rowsRead,
+                                            page,
+                                            rowsTotal,
+                                            lastRowID
+                                           ));
+                page += 1;
+            } else {
+                break;
             }
-            c.close();
-        } else {
-            list = new OCUpload[0];
-        }
+        } while (rowsRead > 0);
+
+        Log_OC.v(TAG, String.format(Locale.ENGLISH,
+                                    "getUploads() returning %d (%d) rows after reading %d pages",
+                                    rowsTotal,
+                                    uploads.size(),
+                                    page
+                                   ));
 
-        return list;
+        return uploads.toArray(new OCUpload[0]);
     }
 
     private OCUpload createOCUploadFromCursor(Cursor c) {