changeset 552:1ff30df0aeac multiprobeLSH

Improve the behaviour of the RDF example a little bit. Automatically create the database if it doesn't exist yet. Write a little explanatory comment about how we could be cleverer.
author mas01cr
date Fri, 13 Feb 2009 11:01:35 +0000
parents ecfba4208621
children fde17c61e797
files examples/runner-rdf/Makefile examples/runner-rdf/populate.c
diffstat 2 files changed, 72 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/examples/runner-rdf/Makefile	Fri Feb 13 11:01:32 2009 +0000
+++ b/examples/runner-rdf/Makefile	Fri Feb 13 11:01:35 2009 +0000
@@ -1,4 +1,7 @@
 all: populate
 
 populate: populate.c
-	gcc -o populate -std=c99 -lrdf -laudioDB -L../.. -I../.. -Wl,-rpath ../.. $<
\ No newline at end of file
+	gcc -o populate -std=c99 -lrdf -laudioDB -L../.. -I../.. -Wl,-rpath ../.. $<
+
+clean:
+	-rm populate keyplot.adb
--- a/examples/runner-rdf/populate.c	Fri Feb 13 11:01:32 2009 +0000
+++ b/examples/runner-rdf/populate.c	Fri Feb 13 11:01:35 2009 +0000
@@ -1,4 +1,6 @@
+#include <sys/types.h>
 #include <sys/stat.h>
+#include <unistd.h>
 #include <librdf.h>
 #include <fcntl.h>
 #include <string.h>
@@ -63,36 +65,72 @@
 }
 
 int main() {
-  librdf_world *world = librdf_new_world();
-  if(world == NULL) return 1;
+  librdf_world *world;
+  if (!(world = librdf_new_world())) 
+    goto librdf_error;
 
-  librdf_storage *storage = librdf_new_storage(world, "memory", NULL, NULL);
-  if(storage == NULL) return 1;
+  librdf_storage *storage;
+  if (!(storage = librdf_new_storage(world, "memory", NULL, NULL)))
+    goto librdf_error;
 
-  librdf_model *model = librdf_new_model(world, storage, NULL);
-  if(model == NULL) return 1;
+  librdf_model *model;
+  if (!(model = librdf_new_model(world, storage, NULL)))
+    goto librdf_error;
 
-  librdf_uri *uri = librdf_new_uri(world, "file:data/test.n3");
-  if(uri == NULL) return 1;
+  librdf_uri *uri;
+  if (!(uri = librdf_new_uri(world, "file:data/test.n3")))
+    goto librdf_error;
 
-  librdf_parser *parser = librdf_new_parser(world, "guess", NULL, NULL);
-  if(parser == NULL) return 1;
+  librdf_parser *parser;
+  if (!(parser = librdf_new_parser(world, "guess", NULL, NULL)))
+    goto librdf_error;
 
-  if(librdf_parser_parse_into_model(parser, uri, NULL, model)) return 1;
+  if(librdf_parser_parse_into_model(parser, uri, NULL, model))
+    goto librdf_error;
   
-  librdf_query *query = 
-    librdf_new_query(world, "sparql", NULL, qstring, NULL);
-  if(query == NULL) return 1;
+  librdf_query *query;
+  if (!(query = librdf_new_query(world, "sparql", NULL, qstring, NULL)))
+    goto librdf_error;
 
-  librdf_query_results *results = librdf_query_execute(query, model);
-  if(results == NULL) return 1;
-  if(!librdf_query_results_is_bindings(results)) return 1;
+  librdf_query_results *results;
+  if (!(results = librdf_query_execute(query, model)))
+    goto librdf_error;
 
-  adb_t *adb = audiodb_open("keyplot.adb", O_RDWR);
-  if(!adb) {
-    fprintf(stderr, "keyplot.adb not opened\n");
-    return 1;
+  if(!librdf_query_results_is_bindings(results)) 
+    goto librdf_error;
+
+  adb_t *adb; 
+  if(!(adb = audiodb_open("keyplot.adb", O_RDWR))) {
+    struct stat st;
+    if(!(stat("keyplot.adb", &st))) {
+      fprintf(stderr, "keyplot.adb not opened.\n");
+      return 1;
+    } else {
+      /* FIXME: if we are doing a proper SPARQL query over a
+       * potentially unbounded number of results, we could use
+       * librdf_query_results_get_count() to estimate how much space
+       * our database will need.
+
+       * If we're doing a SPARQL query over a number of RDF files,
+       * with an expected number of datasets per file of 1 (as might
+       * be produced by runner(?)) then obviously we can use that as
+       * an estimate of number of tracks instead.
+
+       * Specifying the data dimensionality should be easy from the
+       * semantics of the feature being inserted; it's not immediately
+       * obvious to me how to estimate the data size required, unless
+       * we maybe do a preliminary query to find out the total time
+       * (or similar) of all the tracks we're about to insert.
+
+       * (also NOTE: this audiodb_create() interface is scheduled for
+       * being made less inelegant.) */
+      if(!(adb = audiodb_create("keyplot.adb", 0, 0, 0))) {
+        fprintf(stderr, "failed to create keyplot.adb.\n");
+        return 1;
+      }
+    }
   }
+
   while(!librdf_query_results_finished(results)) {
     int count = librdf_query_results_get_bindings_count(results);
     adb_datum_t datum = {0};
@@ -110,12 +148,14 @@
         if(nelements % 25) return 4;
         datum.nvectors = nelements / 25;
       } else {
+        /* do something with the timeline (and other) information */
         printf("%s: %s\n", name, librdf_node_get_literal_value(node));
       }
       librdf_free_node(node);
     }
     if(audiodb_insert_datum(adb, &datum)) {
-      return 17;
+      fprintf(stderr, "failed to insert datum with key %s.\n", datum.key);
+      return 1;
     }
     librdf_query_results_next(results);
   }
@@ -127,4 +167,10 @@
   librdf_free_model(model);
   librdf_free_storage(storage);
   librdf_free_world(world);
+
+  return 0;
+
+ librdf_error:
+  fprintf(stderr, "Something went wrong in librdf.\n");
+  return 1;
 }