[rygel] core: Smarter and cleaner MediaContainer.search



commit abdecf858186d87ab9dd3a4318734924d07846e6
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Nov 11 00:14:51 2009 +0200

    core: Smarter and cleaner MediaContainer.search
    
    Smarter and cleaner default search implementation.

 src/rygel/rygel-media-container.vala |   57 +++++++++++++++++++++++++---------
 1 files changed, 42 insertions(+), 15 deletions(-)
---
diff --git a/src/rygel/rygel-media-container.vala b/src/rygel/rygel-media-container.vala
index 568dc10..a0c0de7 100644
--- a/src/rygel/rygel-media-container.vala
+++ b/src/rygel/rygel-media-container.vala
@@ -141,22 +141,8 @@ public abstract class Rygel.MediaContainer : MediaObject {
             limit = 0; // No limits on searches
         }
 
+        // First add relavant children
         foreach (var child in children) {
-            if (child is MediaContainer) {
-                // First search inside the child container
-                var container = child as MediaContainer;
-                uint tmp;
-
-                var child_result = yield container.search (expression,
-                                                           0,
-                                                           limit,
-                                                           out tmp,
-                                                           cancellable);
-
-                result.add_all (child_result);
-            }
-
-            // Then check if child itself satisfies search criteria
             if (expression == null || expression.satisfied_by (child)) {
                 result.add (child);
             }
@@ -166,6 +152,17 @@ public abstract class Rygel.MediaContainer : MediaObject {
             }
         }
 
+        if (limit == 0 || result.size < limit) {
+            // Then search in the children
+            var child_limit = (limit == 0)? 0: limit - result.size;
+
+            var child_results = yield this.search_in_children (expression,
+                                                               children,
+                                                               child_limit,
+                                                               cancellable);
+            result.add_all (child_results);
+        }
+
         // See if we need to slice the results
         if (result.size > 0 && limit > 0) {
             uint start;
@@ -191,6 +188,36 @@ public abstract class Rygel.MediaContainer : MediaObject {
         }
     }
 
+    private async Gee.List<MediaObject> search_in_children (
+                                        SearchExpression      expression,
+                                        Gee.List<MediaObject> children,
+                                        uint                  limit,
+                                        Cancellable?          cancellable)
+                                        throws Error {
+        var result = new ArrayList<MediaObject> ();
+
+        foreach (var child in children) {
+            if (child is MediaContainer) {
+                var container = child as MediaContainer;
+                uint tmp;
+
+                var child_result = yield container.search (expression,
+                                                           0,
+                                                           limit,
+                                                           out tmp,
+                                                           cancellable);
+
+                result.add_all (child_result);
+            }
+
+            if (limit > 0 && result.size >= limit) {
+                break;
+            }
+        }
+
+        return result;
+    }
+
     /**
      * Method to be be called each time this container is updated (metadata
      * changes for this container, items under it gets removed/added or their



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]