[polari/wip/fmuellner/tracker: 7/15] logger: Support running queries in chunks



commit b25bc51c97375d4e9be73814b02c6aaa3aaa5e6a
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Dec 29 18:14:52 2015 +0100

    logger: Support running queries in chunks
    
    It is not always desirable to retrieve the full result set at once;
    in particular for the history in the chat log that uses TplLogWalker
    currently, we want to maintain the behavior of only loading the most
    recent history initially and fetch additional logs on demand.
    To support this, extend the previously added GenericQuery class to
    specify an optional limit.

 src/logger.js | 42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)
---
diff --git a/src/logger.js b/src/logger.js
index 37135c9..86141ad 100644
--- a/src/logger.js
+++ b/src/logger.js
@@ -6,14 +6,18 @@ const Tracker = imports.gi.Tracker;
 var GenericQuery = new Lang.Class({
     Name: 'GenericQuery',
 
-    _init: function() {
+    _init: function(limit = -1) {
         this._connection = Polari.util_get_tracker_connection();
         this._results = [];
+        this._limit = limit;
+        this._count = 0;
+        this._closed = false;
+        this._cursor = null;
         this._task = null;
     },
 
-    run: function(sparql, cancellable, callback) {
-        this._task = Gio.Task.new(this._connection, cancellable, Lang.bind(this,
+    _createTask: function(cancellable, callback) {
+        return Gio.Task.new(this._connection, cancellable, Lang.bind(this,
             function(o, res) {
                 let success = false;
                 try {
@@ -25,6 +29,10 @@ var GenericQuery = new Lang.Class({
                 callback(success ? this._results : []);
                 this._task = null;
             }));
+    },
+
+    run: function(sparql, cancellable, callback) {
+        this._task = this._createTask(cancellable, callback);
 
         this._connection.query_async(sparql, cancellable, Lang.bind(this,
             function(c, res) {
@@ -36,11 +44,28 @@ var GenericQuery = new Lang.Class({
                     return;
                 }
 
+                this._cursor = cursor;
                 cursor.next_async(cancellable,
                                   Lang.bind(this, this._onCursorNext));
             }));
     },
 
+    next: function (limit, cancellable, callback) {
+        if (this._task)
+            return false;
+
+        this._results = [];
+        this._count = 0;
+        this._limit = limit;
+        this._task = this._createTask(cancellable, callback);
+        this._cursor.next_async(cancellable, Lang.bind(this, this._onCursorNext));
+        return true;
+    },
+
+    isClosed: function () {
+        return this._closed;
+    },
+
     _onCursorNext: function(cursor, res) {
         let valid = false;
         try {
@@ -51,12 +76,19 @@ var GenericQuery = new Lang.Class({
 
         if (valid) {
             this._pushResult(cursor);
-            cursor.next_async(this._task.get_cancellable(),
-                              Lang.bind(this, this._onCursorNext));
+            this._count++;
+
+            if (this._limit <= 0 || this._count < this._limit) {
+                cursor.next_async(this._task.get_cancellable(),
+                                  Lang.bind(this, this._onCursorNext));
+            } else {
+                this._task.return_boolean(true);
+            }
         } else {
             cursor.close();
             if (!this._task.had_error())
                 this._task.return_boolean(true);
+            this._closed = true;
         }
     },
 


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