[tracker/wip/sam/resource: 46/54] tracker-extract: Use TrackerResource for cmdline output



commit c005e9914fcea99b239e6bef0c77943fc08e9699
Author: Sam Thursfield <sam afuera me uk>
Date:   Fri Apr 8 15:51:24 2016 +0100

    tracker-extract: Use TrackerResource for cmdline output
    
    This means we can now serialize to Turtle as well as to SPARQL. Turtle
    is a lot simpler to convert to other formats than SPARQL. It's pretty
    easy to add further options for serialization too, for example JSON-LD.

 src/libtracker-common/tracker-enums.h |    5 +++
 src/tracker-extract/tracker-extract.c |   61 +++++++++++++++++++-------------
 src/tracker-extract/tracker-main.c    |    3 ++
 3 files changed, 44 insertions(+), 25 deletions(-)
---
diff --git a/src/libtracker-common/tracker-enums.h b/src/libtracker-common/tracker-enums.h
index 3fd43d9..2be97c1 100644
--- a/src/libtracker-common/tracker-enums.h
+++ b/src/libtracker-common/tracker-enums.h
@@ -35,6 +35,11 @@ typedef enum {
        TRACKER_SCHED_IDLE_NEVER,
 } TrackerSchedIdle;
 
+typedef enum {
+       TRACKER_SERIALIZATION_FORMAT_SPARQL,
+       TRACKER_SERIALIZATION_FORMAT_TURTLE,
+} TrackerSerializationFormat;
+
 G_END_DECLS
 
 #endif /* __TRACKER_ENUMS_H__ */
diff --git a/src/tracker-extract/tracker-extract.c b/src/tracker-extract/tracker-extract.c
index a184078..1a92796 100644
--- a/src/tracker-extract/tracker-extract.c
+++ b/src/tracker-extract/tracker-extract.c
@@ -796,42 +796,53 @@ tracker_extract_get_metadata_by_cmdline (TrackerExtract *object,
        while (task->cur_func) {
                if (!filter_module (object, task->cur_module) &&
                    get_file_metadata (task, &info)) {
-                       const gchar *preupdate_str, *postupdate_str, *statements_str, *where;
-                       TrackerSparqlBuilder *builder;
+                       TrackerResource *resource = tracker_extract_info_get_resource (info);
+                       GError *error = NULL;
+
+                       if (resource == NULL)
+                               break;
 
                        no_data_or_modules = FALSE;
-                       preupdate_str = statements_str = postupdate_str = NULL;
 
-                       builder = tracker_extract_info_get_metadata_builder (info);
+                       if (output_format == TRACKER_SERIALIZATION_FORMAT_SPARQL) {
+                               TrackerSparqlBuilder *builder;
+                               const char *text;
 
-                       if (tracker_sparql_builder_get_length (builder) > 0) {
-                               statements_str = tracker_sparql_builder_get_result (builder);
-                       }
+                               builder = tracker_sparql_builder_new_update ();
 
-                       builder = tracker_extract_info_get_preupdate_builder (info);
+                               /* If this was going into the tracker-store we'd generate a unique ID
+                                * here, so that the data persisted across file renames.
+                                */
+                               tracker_resource_set_identifier (resource, uri);
 
-                       if (tracker_sparql_builder_get_length (builder) > 0) {
-                               preupdate_str = tracker_sparql_builder_get_result (builder);
-                       }
+                               tracker_resource_generate_sparql_update (resource,
+                                                                        builder,
+                                                                        
tracker_namespace_manager_get_default (),
+                                                                        NULL,
+                                                                        &error);
 
-                       builder = tracker_extract_info_get_postupdate_builder (info);
+                               text = tracker_sparql_builder_get_result (builder);
 
-                       if (tracker_sparql_builder_get_length (builder) > 0) {
-                               postupdate_str = tracker_sparql_builder_get_result (builder);
-                       }
+                               g_print ("%s\n", text);
+                       } else if (output_format == TRACKER_SERIALIZATION_FORMAT_TURTLE) {
+                               char *turtle;
 
-                       where = tracker_extract_info_get_where_clause (info);
+                               /* If this was going into the tracker-store we'd generate a unique ID
+                                * here, so that the data persisted across file renames.
+                                */
+                               tracker_resource_set_identifier (resource, uri);
 
-                       g_print ("\n");
+                               turtle = tracker_resource_print_turtle (resource,
+                                                                       tracker_namespace_manager_get_default 
());
 
-                       g_print ("SPARQL pre-update:\n--\n%s--\n\n",
-                                preupdate_str ? preupdate_str : "");
-                       g_print ("SPARQL item:\n--\n%s--\n\n",
-                                statements_str ? statements_str : "");
-                       g_print ("SPARQL where clause:\n--\n%s--\n\n",
-                                where ? where : "");
-                       g_print ("SPARQL post-update:\n--\n%s--\n\n",
-                                postupdate_str ? postupdate_str : "");
+                               if (turtle) {
+                                       g_print ("%s\n", turtle);
+                                       g_free (turtle);
+                               } else {
+                                       g_printerr ("%s\n", error->message);
+                                       g_error_free (error);
+                               }
+                       }
 
                        tracker_extract_info_unref (info);
                        break;
diff --git a/src/tracker-extract/tracker-main.c b/src/tracker-extract/tracker-main.c
index 2c8ecd8..cc02fdd 100644
--- a/src/tracker-extract/tracker-main.c
+++ b/src/tracker-extract/tracker-main.c
@@ -94,6 +94,9 @@ static GOptionEntry entries[] = {
          G_OPTION_ARG_STRING, &force_module,
          N_("Force a module to be used for extraction (e.g. \"foo\" for \"foo.so\")"),
          N_("MODULE") },
+       { "output-format", 'o', 0, G_OPTION_ARG_STRING, &output_format_name,
+         N_("Output results format: 'sparql', or 'turtle'"),
+         N_("FORMAT") },
        { "version", 'V', 0,
          G_OPTION_ARG_NONE, &version,
          N_("Displays version information"),


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