diff options
author | David Robillard <d@drobilla.net> | 2010-11-07 06:35:31 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2010-11-07 06:35:31 +0000 |
commit | 2e52630e15faf3cfbf06583fa66c82dcd172627f (patch) | |
tree | a3862dafca2bcd8f92d5d701643723755f8ece9a | |
parent | 38416afb8e2d7fe432e0f8610a458e2429d17ece (diff) | |
download | lilv-2e52630e15faf3cfbf06583fa66c82dcd172627f.tar.gz lilv-2e52630e15faf3cfbf06583fa66c82dcd172627f.tar.bz2 lilv-2e52630e15faf3cfbf06583fa66c82dcd172627f.zip |
Use librdf_model_find_statements instead of SPARQL for (most) simple 1-variable queries.
git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@2667 a436a847-0d15-0410-975c-d299462d15a1
-rw-r--r-- | src/plugin.c | 98 | ||||
-rw-r--r-- | src/query.c | 12 | ||||
-rw-r--r-- | src/slv2_internal.h | 9 | ||||
-rw-r--r-- | src/world.c | 4 |
4 files changed, 72 insertions, 51 deletions
diff --git a/src/plugin.c b/src/plugin.c index 6cd637c..2cf30d6 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -258,32 +258,27 @@ slv2_plugin_get_bundle_uri(SLV2Plugin p) SLV2Value slv2_plugin_get_library_uri(SLV2Plugin p) { - assert(p); slv2_plugin_load_if_necessary(p); if (!p->binary_uri) { - const unsigned char* query = (const unsigned char*) - "PREFIX : <http://lv2plug.in/ns/lv2core#>\n" - "SELECT ?binary WHERE { <> :binary ?binary . }"; - - librdf_query* q = librdf_new_query(p->world->world, "sparql", - NULL, query, slv2_value_as_librdf_uri(p->plugin_uri)); - - librdf_query_results* results = librdf_query_execute(q, p->rdf); - - if (!librdf_query_results_finished(results)) { - librdf_node* binary_node = librdf_query_results_get_binding_value(results, 0); - librdf_uri* binary_uri = librdf_node_get_uri(binary_node); + // <plugin> lv2:binary ?binary + librdf_stream* results = slv2_plugin_find_statements( + p, + librdf_new_node_from_uri(p->world->world, p->plugin_uri->val.uri_val), + librdf_new_node_from_node(p->world->lv2_binary_node), + NULL); + while (!librdf_stream_end(results)) { + librdf_statement* s = librdf_stream_get_object(results); + librdf_node* binary_node = librdf_statement_get_object(s); + librdf_uri* binary_uri = librdf_node_get_uri(binary_node); if (binary_uri) { - SLV2Value binary = slv2_value_new_librdf_uri(p->world, binary_uri); - p->binary_uri = binary; + p->binary_uri = slv2_value_new_librdf_uri(p->world, binary_uri); + break; } - librdf_free_node(binary_node); + librdf_stream_next(results); } - - librdf_free_query_results(results); - librdf_free_query(q); + librdf_free_stream(results); } return p->binary_uri; } @@ -301,20 +296,19 @@ slv2_plugin_get_class(SLV2Plugin p) { slv2_plugin_load_if_necessary(p); if (!p->plugin_class) { - const unsigned char* query = (const unsigned char*) - "SELECT DISTINCT ?class WHERE { <> a ?class }"; - - librdf_query* q = librdf_new_query(p->world->world, "sparql", - NULL, query, slv2_value_as_librdf_uri(p->plugin_uri)); - - librdf_query_results* results = librdf_query_execute(q, p->rdf); - - while (!librdf_query_results_finished(results)) { - librdf_node* class_node = librdf_query_results_get_binding_value(results, 0); - librdf_uri* class_uri = librdf_node_get_uri(class_node); + // <plugin> a ?class + librdf_stream* results = slv2_plugin_find_statements( + p, + librdf_new_node_from_uri(p->world->world, p->plugin_uri->val.uri_val), + librdf_new_node_from_node(p->world->rdf_a_node), + NULL); + while (!librdf_stream_end(results)) { + librdf_statement* s = librdf_stream_get_object(results); + librdf_node* class_node = librdf_new_node_from_node(librdf_statement_get_object(s)); + librdf_uri* class_uri = librdf_node_get_uri(class_node); if (!class_uri) { - librdf_query_results_next(results); + librdf_stream_next(results); continue; } @@ -335,16 +329,14 @@ slv2_plugin_get_class(SLV2Plugin p) } slv2_value_free(class); - librdf_query_results_next(results); + librdf_stream_next(results); } if (p->plugin_class == NULL) p->plugin_class = p->world->lv2_plugin_class; - librdf_free_query_results(results); - librdf_free_query(q); + librdf_free_stream(results); } - return p->plugin_class; } @@ -434,19 +426,31 @@ SLV2Values slv2_plugin_get_value(SLV2Plugin p, SLV2Value predicate) { - char* query = NULL; + // <plugin> <predicate> ?value + librdf_stream* results = slv2_plugin_find_statements( + p, + librdf_new_node_from_uri(p->world->world, p->plugin_uri->val.uri_val), + librdf_new_node_from_uri(p->world->world, predicate->val.uri_val), + NULL); - /* Hack around broken RASQAL, full URI predicates don't work :/ */ - query = slv2_strjoin( - "PREFIX slv2predicate: <", slv2_value_as_string(predicate), ">\n", - "SELECT DISTINCT ?value WHERE {\n" - "<> slv2predicate: ?value .\n" - "}\n", NULL); + if (librdf_stream_end(results)) { + librdf_free_stream(results); + return NULL; + } - SLV2Values result = slv2_plugin_query_variable(p, query, 0); + SLV2Values result = slv2_values_new(); + while (!librdf_stream_end(results)) { + librdf_statement* s = librdf_stream_get_object(results); + librdf_node* value_node = librdf_statement_get_object(s); - free(query); + SLV2Value value = slv2_value_new_librdf_node(p->world, value_node); + if (value) + raptor_sequence_push(result, value); + librdf_stream_next(results); + } + + librdf_free_stream(results); return result; } @@ -498,14 +502,10 @@ slv2_plugin_get_value_for_subject(SLV2Plugin p, return NULL; } - char* query = NULL; - - - char* subject_token = slv2_value_get_turtle_token(subject); /* Hack around broken RASQAL, full URI predicates don't work :/ */ - query = slv2_strjoin( + char* query = slv2_strjoin( "PREFIX slv2predicate: <", slv2_value_as_string(predicate), ">\n", "SELECT DISTINCT ?value WHERE {\n", subject_token, " slv2predicate: ?value .\n" diff --git a/src/query.c b/src/query.c index 58e5f80..615eeaf 100644 --- a/src/query.c +++ b/src/query.c @@ -205,3 +205,15 @@ slv2_plugin_query_count(SLV2Plugin plugin, return ret; } +librdf_stream* +slv2_plugin_find_statements(SLV2Plugin plugin, + librdf_node* subject, + librdf_node* predicate, + librdf_node* object) +{ + librdf_statement* q = librdf_new_statement_from_nodes( + plugin->world->world, subject, predicate, object); + librdf_stream* results = librdf_model_find_statements(plugin->rdf, q); + librdf_free_statement(q); + return results; +} diff --git a/src/slv2_internal.h b/src/slv2_internal.h index 8feafdd..1075703 100644 --- a/src/slv2_internal.h +++ b/src/slv2_internal.h @@ -80,9 +80,13 @@ void slv2_plugin_load_if_necessary(SLV2Plugin p); void slv2_plugin_load_ports_if_necessary(SLV2Plugin p); void slv2_plugin_free(SLV2Plugin plugin); void slv2_plugin_get_port_float_values(SLV2Plugin p, - const char* qname, - float* values); + const char* qname, + float* values); +librdf_stream* slv2_plugin_find_statements(SLV2Plugin plugin, + librdf_node* subject, + librdf_node* predicate, + librdf_node* object); /* ********* Plugins ********* */ @@ -147,6 +151,7 @@ struct _SLV2World { SLV2Plugins plugins; librdf_node* lv2_specification_node; librdf_node* lv2_plugin_node; + librdf_node* lv2_binary_node; librdf_node* rdf_a_node; librdf_node* xsd_integer_node; librdf_node* xsd_decimal_node; diff --git a/src/world.c b/src/world.c index 130dc94..b54ce65 100644 --- a/src/world.c +++ b/src/world.c @@ -61,6 +61,9 @@ slv2_world_new_internal(SLV2World world) world->lv2_plugin_node = librdf_new_node_from_uri_string(world->world, (const unsigned char*)"http://lv2plug.in/ns/lv2core#Plugin"); + world->lv2_binary_node = librdf_new_node_from_uri_string(world->world, + (const unsigned char*)"http://lv2plug.in/ns/lv2core#binary"); + world->rdf_a_node = librdf_new_node_from_uri_string(world->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); @@ -144,6 +147,7 @@ slv2_world_free(SLV2World world) librdf_free_node(world->lv2_specification_node); librdf_free_node(world->lv2_plugin_node); + librdf_free_node(world->lv2_binary_node); librdf_free_node(world->rdf_a_node); librdf_free_node(world->xsd_integer_node); librdf_free_node(world->xsd_decimal_node); |