[polari/wip/fmuellner/tracker: 14/22] logger: Support running queries in chunks



commit 6e40314894cee699b6f010c1ac59935483ad0a33
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 e5b72be..5424cff 100644
--- a/src/logger.js
+++ b/src/logger.js
@@ -4,14 +4,18 @@ const Polari = imports.gi.Polari;
 const Tracker = imports.gi.Tracker;
 
 var GenericQuery  = class {
-    constructor() {
+    constructor(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(sparql, cancellable, callback) {
-        this._task = Gio.Task.new(this._connection, cancellable, (o, res) => {
+    _createTask(cancellable, callback) {
+        return Gio.Task.new(this._connection, cancellable, (o, res) => {
             let success = false;
             try {
                 success = this._task.propagate_boolean();
@@ -22,6 +26,10 @@ var GenericQuery  = class {
             callback(success ? this._results : []);
             this._task = null;
         });
+    }
+
+    run(sparql, cancellable, callback) {
+        this._task = this._createTask(cancellable, callback);
 
         this._connection.query_async(sparql, cancellable, (c, res) => {
             let cursor;
@@ -32,11 +40,28 @@ var GenericQuery  = class {
                 return;
             }
 
+            this._cursor = cursor;
             cursor.next_async(cancellable,
                               Lang.bind(this, this._onCursorNext));
         });
     }
 
+    next(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() {
+        return this._closed;
+    }
+
     _onCursorNext(cursor, res) {
         let valid = false;
         try {
@@ -47,12 +72,19 @@ var GenericQuery  = 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]