[tracker/wip/carlosg/domain-ontologies: 13/116] libtracker-data: Make TrackerTurtleReader take GFiles



commit 3b1032143e6ddce347636fe26e4368b48465d375
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon May 29 18:59:32 2017 +0200

    libtracker-data: Make TrackerTurtleReader take GFiles
    
    Instead of file paths. This is so we can load ontology files
    from resource:// uris. All callers have been changed accordingly.

 docs/tools/ttl_loader.c                        |   15 ++++++++++---
 src/libtracker-data/tracker-data-manager.c     |   15 +++++++++----
 src/libtracker-data/tracker-turtle-reader.vala |   25 +++++++++++++++++++----
 tests/libtracker-data/tracker-backup-test.c    |    4 ++-
 tests/libtracker-data/tracker-ontology-test.c  |    5 +++-
 tests/libtracker-data/tracker-sparql-test.c    |    4 ++-
 utils/ontology/data-validator.c                |   10 +++++++-
 utils/ontology/ontology-validator.c            |    6 +++-
 8 files changed, 63 insertions(+), 21 deletions(-)
---
diff --git a/docs/tools/ttl_loader.c b/docs/tools/ttl_loader.c
index 97e072a..92a518b 100644
--- a/docs/tools/ttl_loader.c
+++ b/docs/tools/ttl_loader.c
@@ -360,8 +360,10 @@ ttl_loader_load_ontology (const gchar *ttl_file)
        if (ttl_file) {
                TrackerTurtleReader *reader;
                GError *error = NULL;
+               GFile *file = g_file_new_for_path (ttl_file);
 
-               reader = tracker_turtle_reader_new (ttl_file, NULL);
+               reader = tracker_turtle_reader_new (file, NULL);
+               g_object_unref (file);
 
                while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
                        load_in_memory (ontology,
@@ -446,9 +448,12 @@ ttl_loader_load_ontology_dir (const gchar *ttl_dir)
                TrackerTurtleReader *reader;
                GError *error = NULL;
                gchar *ttl_file;
+               GFile *file;
 
                ttl_file = g_build_filename (ttl_dir, f->data, NULL);
-               reader = tracker_turtle_reader_new (ttl_file, NULL);
+               file = g_file_new_for_path (ttl_file);
+               reader = tracker_turtle_reader_new (file, NULL);
+               g_object_unref (file);
                g_free (ttl_file);
 
                while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
@@ -476,11 +481,13 @@ ttl_loader_load_description (const gchar *filename)
        OntologyDescription *desc;
        TrackerTurtleReader *reader;
        GError *error = NULL;
+       GFile *file;
 
        desc = ttl_model_description_new ();
 
-
-       reader = tracker_turtle_reader_new (filename, NULL);
+       file = g_file_new_for_path (filename);
+       reader = tracker_turtle_reader_new (file, NULL);
+       g_object_unref (file);
 
        while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
                load_description (desc,
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index a8260a1..9de9f6a 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -1720,8 +1720,10 @@ load_ontology_file_from_path (const gchar        *ontology_path,
 {
        TrackerTurtleReader *reader;
        GError              *ttl_error = NULL;
+       GFile               *file = g_file_new_for_path (ontology_path);
 
-       reader = tracker_turtle_reader_new (ontology_path, &ttl_error);
+       reader = tracker_turtle_reader_new (file, &ttl_error);
+       g_object_unref (file);
 
        if (ttl_error) {
                g_propagate_error (error, ttl_error);
@@ -1769,8 +1771,10 @@ get_ontology_from_path (const gchar *ontology_path)
        GError *error = NULL;
        GHashTable *ontology_uris;
        TrackerOntology *ret = NULL;
+       GFile *file = g_file_new_for_path (ontology_path);
 
-       reader = tracker_turtle_reader_new (ontology_path, &error);
+       reader = tracker_turtle_reader_new (file, &error);
+       g_object_unref (file);
 
        if (error) {
                g_critical ("Turtle parse error: %s", error->message);
@@ -1984,11 +1988,12 @@ import_ontology_path (const gchar *ontology_path,
                       gboolean in_update,
                       gboolean ignore_nao_last_modified)
 {
-       GError          *error = NULL;
-
+       GError *error = NULL;
        TrackerTurtleReader* reader;
+       GFile *file = g_file_new_for_path (ontology_path);
 
-       reader = tracker_turtle_reader_new (ontology_path, &error);
+       reader = tracker_turtle_reader_new (file, &error);
+       g_object_unref (file);
 
        if (error != NULL) {
                g_critical ("%s", error->message);
diff --git a/src/libtracker-data/tracker-turtle-reader.vala b/src/libtracker-data/tracker-turtle-reader.vala
index 8acb082..8475db7 100644
--- a/src/libtracker-data/tracker-turtle-reader.vala
+++ b/src/libtracker-data/tracker-turtle-reader.vala
@@ -28,6 +28,7 @@ public class Tracker.TurtleReader : Object {
        int size;
 
        const int BUFFER_SIZE = 32;
+       const int MAX_ONTOLOGY_SIZE = 2 * 1024 * 1024;
 
        struct TokenInfo {
                public SparqlTokenType type;
@@ -63,10 +64,24 @@ public class Tracker.TurtleReader : Object {
        uchar[] base_uuid;
 
        MappedFile? mapped_file;
+       uchar[]? buffer;
 
-       public TurtleReader (string path) throws FileError {
-               mapped_file = new MappedFile (path, false);
-               scanner = new SparqlScanner (mapped_file.get_contents (), mapped_file.get_length ());
+       public TurtleReader (File file) throws Error, FileError {
+               if (file.is_native ()) {
+                       mapped_file = new MappedFile (file.get_path(), false);
+                       scanner = new SparqlScanner (mapped_file.get_contents (), mapped_file.get_length ());
+               } else {
+                       size_t len;
+                       var istream = file.read (null);
+                       buffer = new uchar[MAX_ONTOLOGY_SIZE];
+                       istream.read_all (buffer, out len, null);
+
+                       // Not good if we read a portion of the ontology
+                       if (len == MAX_ONTOLOGY_SIZE)
+                               throw new IOError.FAILED ("Ontology file too large");
+
+                       scanner = new SparqlScanner (buffer, len);
+               }
 
                base_uuid = new uchar[16];
                uuid_generate (base_uuid);
@@ -365,11 +380,11 @@ public class Tracker.TurtleReader : Object {
                }
        }
 
-       public static void load (string path) throws FileError, Sparql.Error, DateError, DBInterfaceError {
+       public static void load (File file) throws Error, FileError, Sparql.Error, DateError, 
DBInterfaceError {
                try {
                        Data.begin_transaction ();
 
-                       var reader = new TurtleReader (path);
+                       var reader = new TurtleReader (file);
                        while (reader.next ()) {
                                if (reader.object_is_uri) {
                                        Data.insert_statement_with_uri (reader.graph, reader.subject, 
reader.predicate, reader.object);
diff --git a/tests/libtracker-data/tracker-backup-test.c b/tests/libtracker-data/tracker-backup-test.c
index 310754d..88e1856 100644
--- a/tests/libtracker-data/tracker-backup-test.c
+++ b/tests/libtracker-data/tracker-backup-test.c
@@ -134,8 +134,10 @@ test_backup_and_restore_helper (gboolean journal)
        /* load data set */
        data_filename = g_strconcat (data_prefix, ".data", NULL);
        if (g_file_test (data_filename, G_FILE_TEST_IS_REGULAR)) {
-               tracker_turtle_reader_load (data_filename, &error);
+               GFile *file = g_file_new_for_path (data_filename);
+               tracker_turtle_reader_load (file, &error);
                g_assert_no_error (error);
+               g_object_unref (file);
        } else {
                g_assert_not_reached ();
        }
diff --git a/tests/libtracker-data/tracker-ontology-test.c b/tests/libtracker-data/tracker-ontology-test.c
index 4ad2956..c9d87f3 100644
--- a/tests/libtracker-data/tracker-ontology-test.c
+++ b/tests/libtracker-data/tracker-ontology-test.c
@@ -233,6 +233,7 @@ test_query (TestInfo      *test_info,
        gchar *query_filename;
        gchar *results_filename;
        gchar *prefix, *data_prefix, *test_prefix;
+       GFile *file;
 
        prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", NULL);
        data_prefix = g_build_filename (prefix, test_info->data, NULL);
@@ -257,8 +258,10 @@ test_query (TestInfo      *test_info,
 
        /* load data set */
        data_filename = g_strconcat (data_prefix, ".ttl", NULL);
-       tracker_turtle_reader_load (data_filename, &error);
+       file = g_file_new_for_path (data_filename);
+       tracker_turtle_reader_load (file, &error);
        g_assert_no_error (error);
+       g_object_unref (file);
 
        query_filename = g_strconcat (test_prefix, ".rq", NULL);
        results_filename = g_strconcat (test_prefix, ".out", NULL);
diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c
index 1647635..3218cba 100644
--- a/tests/libtracker-data/tracker-sparql-test.c
+++ b/tests/libtracker-data/tracker-sparql-test.c
@@ -280,8 +280,10 @@ test_sparql_query (TestInfo      *test_info,
        /* load data set */
        data_filename = g_strconcat (data_prefix, ".ttl", NULL);
        if (g_file_test (data_filename, G_FILE_TEST_IS_REGULAR)) {
-               tracker_turtle_reader_load (data_filename, &error);
+               GFile *file = g_file_new_for_path (data_filename);
+               tracker_turtle_reader_load (file, &error);
                g_assert_no_error (error);
+               g_object_unref (file);
        } else {
                /* no .ttl available, assume .rq with SPARQL Update */
                gchar *data;
diff --git a/utils/ontology/data-validator.c b/utils/ontology/data-validator.c
index d80cdb5..38dbd90 100644
--- a/utils/ontology/data-validator.c
+++ b/utils/ontology/data-validator.c
@@ -120,6 +120,7 @@ load_ontology_files (const gchar *services_dir)
        while (conf_file) {
                TrackerTurtleReader *reader;
                GError *error = NULL;
+               GFile *file;
 
                if (!g_str_has_suffix (conf_file, "ontology")) {
                        conf_file = g_dir_read_name (services);
@@ -127,8 +128,10 @@ load_ontology_files (const gchar *services_dir)
                }
 
                fullpath = g_build_filename (dir_uri, conf_file, NULL);
+               file = g_file_new_for_path (fullpath);
 
-               reader = tracker_turtle_reader_new (fullpath, NULL);
+               reader = tracker_turtle_reader_new (file, NULL);
+               g_object_unref (file);
 
                while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
                        turtle_load_ontology (tracker_turtle_reader_get_subject (reader),
@@ -162,6 +165,7 @@ main (gint argc, gchar **argv)
        GOptionContext *context;
        TrackerTurtleReader *reader;
        GError *error = NULL;
+       GFile *file;
 
        /* Translators: this messagge will apper immediately after the  */
        /* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>     */
@@ -189,7 +193,9 @@ main (gint argc, gchar **argv)
        //"/home/ivan/devel/codethink/tracker-ssh/data/services"
        load_ontology_files (ontology_dir);
 
-       reader = tracker_turtle_reader_new (ttl_file, NULL);
+       file = g_file_new_for_commandline_arg (ttl_file);
+       reader = tracker_turtle_reader_new (file, NULL);
+       g_object_unref (file);
 
        while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
                turtle_statement_handler (tracker_turtle_reader_get_subject (reader),
diff --git a/utils/ontology/ontology-validator.c b/utils/ontology/ontology-validator.c
index 7aa79ef..8d93433 100644
--- a/utils/ontology/ontology-validator.c
+++ b/utils/ontology/ontology-validator.c
@@ -176,12 +176,13 @@ turtle_load_ontology (const gchar *turtle_subject,
 }
 
 static void
-process_file (const gchar *ttl_file)
+process_file (const gchar *ttl_path)
 {
        TrackerTurtleReader *reader;
        GError *error = NULL;
+       GFile *ttl_file = g_file_new_for_path (ttl_path);
 
-       g_print ("Processing %s\n", ttl_file);
+       g_print ("Processing %s\n", ttl_path);
 
        reader = tracker_turtle_reader_new (ttl_file, NULL);
 
@@ -192,6 +193,7 @@ process_file (const gchar *ttl_file)
        }
 
        g_object_unref (reader);
+       g_object_unref (ttl_file);
 
        if (error) {
                g_message ("Turtle parse error: %s", error->message);


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