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



commit efc826b2b3a6f84c1a01528160752b9297073adf
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/org.gnome.Polari.src.gresource.xml |    1 +
 2 files changed, 104 insertions(+), 0 deletions(-)
---
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/org.gnome.Polari.src.gresource.xml b/src/org.gnome.Polari.src.gresource.xml
index a526f25..f50a2ab 100644
--- a/src/org.gnome.Polari.src.gresource.xml
+++ b/src/org.gnome.Polari.src.gresource.xml
@@ -9,6 +9,7 @@
     <file>entryArea.js</file>
     <file>ircParser.js</file>
     <file>joinDialog.js</file>
+    <file>logger.js</file>
     <file>main.js</file>
     <file>mainWindow.js</file>
     <file>networksManager.js</file>


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