[gnome-documents] all: separate the item count in a separate query



commit 36f891e11bc87060c3427d95314f71953784bfab
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Aug 30 19:39:49 2011 -0400

    all: separate the item count in a separate query
    
    Separating this in a separate query allows to get rid of a lot of
    argument shuffling.

 src/categories.js        |    8 +++---
 src/offsetController.js  |   30 ++++++++++++++++++---
 src/query.js             |   67 ++++++++++++++++++++++------------------------
 src/sources.js           |   16 +++++-----
 src/trackerController.js |   11 +++-----
 5 files changed, 74 insertions(+), 58 deletions(-)
---
diff --git a/src/categories.js b/src/categories.js
index 7cb35a0..035dca5 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -48,10 +48,10 @@ Category.prototype = {
         return '{ }';
     },
 
-    getFilter: function(subject) {
+    getFilter: function() {
         // require to be not local
         if (this.id == 'shared')
-            return Global.queryBuilder.buildFilterNotLocal(subject);
+            return Global.queryBuilder.buildFilterNotLocal();
 
         return '(true)';
     }
@@ -96,8 +96,8 @@ CategoryManager.prototype = {
         return this.activeCategory.getWhere();
     },
 
-    getActiveCategoryFilter: function(subject) {
-        return this.activeCategory.getFilter(subject);
+    getActiveCategoryFilter: function() {
+        return this.activeCategory.getFilter();
     },
 
     getCategories: function() {
diff --git a/src/offsetController.js b/src/offsetController.js
index b6fa2a8..10068a4 100644
--- a/src/offsetController.js
+++ b/src/offsetController.js
@@ -19,8 +19,11 @@
  *
  */
 
+const Lang = imports.lang;
 const Signals = imports.signals;
 
+const Global = imports.global;
+
 const _OFFSET_STEP = 50;
 
 function OffsetController() {
@@ -40,9 +43,28 @@ OffsetController.prototype = {
     },
 
     // to be called by the model
-    setItemCount: function(itemCount) {
-        this._itemCount = itemCount;
-        this.emit('item-count-changed', this._itemCount);
+    resetItemCount: function() {
+        Global.connection.query_async
+            (Global.queryBuilder.buildCountQuery(), null, Lang.bind(this,
+                function(object, res) {
+                    let cursor = null;
+                    try {
+                        cursor = object.query_finish(res);
+                    } catch (e) {
+                        log('Unable to execute count query: ' + e.toString());
+                        return;
+                    }
+
+                    cursor.next_async(null, Lang.bind(this,
+                        function(object, res) {
+                            let valid = object.next_finish(res);
+                            if (!valid)
+                                return;
+
+                            this._itemCount = cursor.get_integer(0);
+                            this.emit('item-count-changed', this._itemCount);
+                        }));
+                }));
     },
 
     // to be called by the model
@@ -62,4 +84,4 @@ OffsetController.prototype = {
         return this._offset;
     }
 };
-Signals.addSignalMethods(OffsetController.prototype);
\ No newline at end of file
+Signals.addSignalMethods(OffsetController.prototype);
diff --git a/src/query.js b/src/query.js
index cc0389f..493e667 100644
--- a/src/query.js
+++ b/src/query.js
@@ -34,8 +34,7 @@ const QueryColumns = {
     TYPE: 6,
     RESOURCE_URN: 7,
     FAVORITE: 8,
-    SHARED: 9,
-    TOTAL_COUNT: 10 // only in global query
+    SHARED: 9
 };
 
 function QueryBuilder() {
@@ -46,7 +45,7 @@ QueryBuilder.prototype = {
     _init: function() {
     },
 
-    buildFilterLocal: function(subject) {
+    buildFilterLocal: function() {
         let path;
         let desktopURI;
         let documentsURI;
@@ -64,60 +63,49 @@ QueryBuilder.prototype = {
             documentsURI = '';
 
         let filter =
-            ('((fn:starts-with (nie:url(%s), "%s")) || ' +
-             '(fn:starts-with (nie:url(%s), "%s")))').format(subject, desktopURI,
-                                                             subject, documentsURI);
+            ('((fn:starts-with (nie:url(?urn), "%s")) || ' +
+             '(fn:starts-with (nie:url(?urn), "%s")))').format(desktopURI, documentsURI);
 
         return filter;
     },
 
-    buildFilterNotLocal: function(subject) {
+    buildFilterNotLocal: function() {
         let filter =
-            ('(fn:contains(rdf:type(%s), \"RemoteDataObject\"))').format(subject);
+            '(fn:contains(rdf:type(?urn), \"RemoteDataObject\"))';
 
         return filter;
     },
 
-    _buildFilterSearch: function(subject) {
+    _buildFilterSearch: function() {
         let filter =
             ('fn:contains ' +
-             '(fn:lower-case (tracker:coalesce(nie:title(%s), nfo:fileName(%s))), ' +
-             '"%s")').format(subject, subject, Global.filterController.getFilter());
+             '(fn:lower-case (tracker:coalesce(nie:title(?urn), nfo:fileName(?urn))), ' +
+             '"%s")').format(Global.filterController.getFilter());
 
         return filter;
     },
 
-    _buildFilterString: function(subject) {
+    _buildFilterString: function() {
         let sparql = 'FILTER (';
 
-        sparql += '(' + this._buildFilterSearch(subject) + ')';
+        sparql += '(' + this._buildFilterSearch() + ')';
         sparql += ' && ';
-        sparql += '(' + Global.sourceManager.getActiveSourceFilter(subject) + ')';
+        sparql += '(' + Global.sourceManager.getActiveSourceFilter() + ')';
         sparql += ' && ';
-        sparql += '(' + Global.categoryManager.getActiveCategoryFilter(subject) + ')';
+        sparql += '(' + Global.categoryManager.getActiveCategoryFilter() + ')';
 
         sparql += ')';
 
         return sparql;
     },
 
-    _buildTypeFilter: function(subject) {
+    _buildTypeFilter: function() {
         let sparql =
-            ('{ %s a nfo:PaginatedTextDocument } ' +
-             'UNION ' +
-             '{ %s a nfo:Spreadsheet } ' +
-             'UNION ' +
-             '{ %s a nfo:Presentation } ').format(subject, subject, subject);
-
-        return sparql;
-    },
-
-    _buildTotalCounter: function() {
-        let sparql =
-            '(SELECT DISTINCT COUNT(?doc) WHERE { ' +
-            this._buildTypeFilter('?doc') +
-            this._buildFilterString('?doc') +
-            '}) ';
+            '{ ?urn a nfo:PaginatedTextDocument } ' +
+            'UNION ' +
+            '{ ?urn a nfo:Spreadsheet } ' +
+            'UNION ' +
+            '{ ?urn a nfo:Presentation } ';
 
         return sparql;
     },
@@ -136,12 +124,11 @@ QueryBuilder.prototype = {
 
         if (global) {
             globalSparql =
-                (this._buildTotalCounter() + // totalCount
-                 'WHERE { ' +
-                 this._buildTypeFilter('?urn') +
+                ('WHERE { ' +
+                 this._buildTypeFilter() +
                  this._buildOptional() +
                  Global.categoryManager.getActiveCategoryWhere() +
-                 this._buildFilterString('?urn') +
+                 this._buildFilterString() +
                  ' } ' +
                  'ORDER BY DESC (?mtime)' +
                  'LIMIT %d OFFSET %d').format(Global.offsetController.getOffsetStep(),
@@ -171,5 +158,15 @@ QueryBuilder.prototype = {
 
     buildGlobalQuery: function() {
         return this._buildQueryInternal(true);
+    },
+
+    buildCountQuery: function() {
+        let sparql =
+            'SELECT DISTINCT COUNT(?urn) WHERE { ' +
+            this._buildTypeFilter() +
+            this._buildFilterString() +
+            '}';
+
+        return sparql;
     }
 };
diff --git a/src/sources.js b/src/sources.js
index ec04feb..7eba511 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -73,19 +73,19 @@ Source.prototype = {
         }
     },
 
-    getFilter: function(subject) {
+    getFilter: function() {
         if (this.id == 'local')
-            return Global.queryBuilder.buildFilterLocal(subject);
+            return Global.queryBuilder.buildFilterLocal();
 
         if (this.id == 'all')
-            return Global.queryBuilder.buildFilterLocal(subject) + ' || ' + Global.queryBuilder.buildFilterNotLocal(subject);
+            return Global.queryBuilder.buildFilterLocal() + ' || ' + Global.queryBuilder.buildFilterNotLocal();
 
-        return this._buildFilterResource(subject);
+        return this._buildFilterResource();
     },
 
-    _buildFilterResource: function(subject) {
+    _buildFilterResource: function() {
         let filter =
-            ('(nie:dataSource(%s) = "%s")').format(subject, this.resourceUrn);
+            ('(nie:dataSource(?urn) = "%s")').format(this.resourceUrn);
 
         return filter;
     }
@@ -170,8 +170,8 @@ SourceManager.prototype = {
         return this.activeSource.id;
     },
 
-    getActiveSourceFilter: function(subject) {
-        return this.activeSource.getFilter(subject);
+    getActiveSourceFilter: function() {
+        return this.activeSource.getFilter();
     },
 
     getSourceByUrn: function(resourceUrn) {
diff --git a/src/trackerController.js b/src/trackerController.js
index 4785bff..7e803b0 100644
--- a/src/trackerController.js
+++ b/src/trackerController.js
@@ -88,7 +88,7 @@ TrackerController.prototype = {
             Global.errorHandler.addQueryError(exception);
     },
 
-    _onCursorNext: function(cursor, res, addCount) {
+    _onCursorNext: function(cursor, res) {
         try {
             let valid = cursor.next_finish(res);
 
@@ -102,17 +102,14 @@ TrackerController.prototype = {
             return;
         }
 
-        if (addCount)
-            this._offsetController.setItemCount(cursor.get_integer(Query.QueryColumns.TOTAL_COUNT));
-
         Global.documentManager.addDocument(cursor);
-        cursor.next_async(null, Lang.bind(this, this._onCursorNext, false));
+        cursor.next_async(null, Lang.bind(this, this._onCursorNext));
     },
 
     _onQueryExecuted: function(object, res) {
         try {
             let cursor = object.query_finish(res);
-            cursor.next_async(null, Lang.bind(this, this._onCursorNext, true));
+            cursor.next_async(null, Lang.bind(this, this._onCursorNext));
         } catch (e) {
             this._onQueryFinished(e);
         }
@@ -126,7 +123,7 @@ TrackerController.prototype = {
     _refresh: function() {
         Global.selectionController.freezeSelection(true);
         Global.documentManager.clear();
-        this._offsetController.setItemCount(0);
+        this._offsetController.resetItemCount();
 
         this._performCurrentQuery();
     },



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