[polari/wip/fmuellner/tracker: 6/15] logger: Add convenience wrapper for tracker queries (WIP)



commit 1d0ab0709fd3bef1c7134793700560e55accd9a1
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Dec 24 05:02:35 2015 +0100

    logger: Add convenience wrapper for tracker queries (WIP)
    
    As telepathy-logger is unsuitable for anything more advanced than
    retrieving logs sequentially (at least with acceptable performance),
    we want to replace it with tracker eventually. To prepare for that,
    add our own LogManager singleton that wraps a tracker connection and
    provides a convenient API to get result objects from a SPARQL query.

 src/logger.js   | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.js     |   3 +-
 src/meson.build |   1 +
 3 files changed, 106 insertions(+), 1 deletion(-)
---
diff --git a/src/logger.js b/src/logger.js
new file mode 100644
index 0000000..37135c9
--- /dev/null
+++ b/src/logger.js
@@ -0,0 +1,103 @@
+const Gio = imports.gi.Gio;
+const Lang = imports.lang;
+const Polari = imports.gi.Polari;
+const Tracker = imports.gi.Tracker;
+
+var GenericQuery = new Lang.Class({
+    Name: 'GenericQuery',
+
+    _init: function() {
+        this._connection = Polari.util_get_tracker_connection();
+        this._results = [];
+        this._task = null;
+    },
+
+    run: function(sparql, cancellable, callback) {
+        this._task = Gio.Task.new(this._connection, cancellable, Lang.bind(this,
+            function(o, res) {
+                let success = false;
+                try {
+                    success = this._task.propagate_boolean();
+                } catch(e) {
+                    log("Failed to run query: "  + e.message);
+                }
+
+                callback(success ? this._results : []);
+                this._task = null;
+            }));
+
+        this._connection.query_async(sparql, cancellable, Lang.bind(this,
+            function(c, res) {
+                let cursor;
+                try {
+                    cursor = this._connection.query_finish(res);
+                } catch(e) {
+                    this._task.return_error(e);
+                    return;
+                }
+
+                cursor.next_async(cancellable,
+                                  Lang.bind(this, this._onCursorNext));
+            }));
+    },
+
+    _onCursorNext: function(cursor, res) {
+        let valid = false;
+        try {
+            valid = cursor.next_finish(res);
+        } catch(e) {
+            this._task.return_error(e);
+        }
+
+        if (valid) {
+            this._pushResult(cursor);
+            cursor.next_async(this._task.get_cancellable(),
+                              Lang.bind(this, this._onCursorNext));
+        } else {
+            cursor.close();
+            if (!this._task.had_error())
+                this._task.return_boolean(true);
+        }
+    },
+
+    _getColumnsValue: function(cursor, col) {
+        switch(cursor.get_value_type(col)) {
+            case Tracker.SparqlValueType.STRING:
+                return cursor.get_string(col)[0];
+            case Tracker.SparqlValueType.INTEGER:
+                return cursor.get_integer(col);
+            case Tracker.SparqlValueType.DOUBLE:
+                return cursor.get_double(col);
+            case Tracker.SparqlValueType.BOOLEAN:
+                return cursor.get_boolean(col);
+            case Tracker.SparqlValueType.DATETIME:
+                return Date.parse(cursor.get_string(col)[0]) / 1000;
+            case Tracker.SparqlValueType.BLANK_NODE:
+            case Tracker.SparqlValueType.UNBOUND:
+                return null;
+            default:
+                throw new Error("Unhandled result type");
+        }
+    },
+
+    _getValue: function(cursor) {
+        let nCols = cursor.get_n_columns();
+        if (nCols == 1)
+            return this._getColumnsValue(cursor, 0);
+
+        let value = {};
+        for (let i = 0; i < nCols; i++) {
+            let name = cursor.get_variable_name(i);
+            value[name] = this._getColumnsValue(cursor, i);
+        }
+        return value;
+    },
+
+    _pushResult: function(cursor) {
+        try {
+            this._results.push(this._getValue(cursor));
+        } catch(e) {
+            log("Error fetching result: " + e.toString());
+        }
+    }
+});
diff --git a/src/main.js b/src/main.js
index d23beda..d92dd16 100755
--- a/src/main.js
+++ b/src/main.js
@@ -9,7 +9,8 @@ pkg.require({ 'GdkPixbuf': '2.0',
               'Secret': '1',
               'Soup': '2.4',
               'TelepathyGLib': '0.12',
-              'TelepathyLogger': '0.2' });
+              'TelepathyLogger': '0.2',
+              'Tracker': '2.0' });
 pkg.requireSymbol('Gio', '2.0', 'Application.send_notification');
 pkg.requireSymbol('GLib', '2.0', 'log_variant');
 pkg.requireSymbol('Gspell', '1', 'Entry');
diff --git a/src/meson.build b/src/meson.build
index 1313b77..6ed0b0e 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -33,6 +33,7 @@ js_sources = [
   'initialSetup.js',
   'ircParser.js',
   'joinDialog.js',
+  'logger.js',
   'main.js',
   'mainWindow.js',
   'networksManager.js',


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