[redland-dev] Problem with repeat queries
Dave Beckett
dave.beckett at bristol.ac.uk
Mon Sep 6 19:55:12 BST 2004
On Mon, 6 Sep 2004 18:12:09 +0100
Richard Newman <r.newman at reading.ac.uk> wrote:
> Hi,
> I'm having a little confusion implementing refreshing on queries
> (i.e. running them again). I think I've narrowed it down to an example
>
> case (see below). This is running against 0.9.18 on Mac OS X 10.3.5,
> latest GCC.
>
> Essentially, running a query a second time returns NULL, even
> immediately after the first's success, or before using the results,
> and regardless of whether the results have been freed. Also,
> librdf_new_query_from_query throws up an error:
> -----
> librdf fatal - rdf_query.c:254:librdf_new_query_from_query: fatal
> error: clone not implemented for query factor
> Abort trap
Yeah I confirm I get that exact crash on linux too, which makes it
easier for me to debug.
> -----
> (the 'y' on 'factory' is in the code, but not printed!)
Incidently that's a bug caused by a bug in a work-around of another
bug / feature on OSX with respect snprintf; another story.
>
> So it seems that to do repeat queries it is necessary to construct
> a
> new query object from strings each time.
Yes, it seems so.
> My questions, then:
> 1. Have I found a bug?
In a sense. Copying queries is not yet implemented as the message
tries to show.
> 2. If not, could you please advise how best to work around this
> behaviour? What am I missing?
A work around without actually implementing that copying is to
just make a new query object. See below.
> 3. Alternatively, is there a way to 'refresh' a query result object?
> (IIRC it does keep track of its query...?)
No. Querying is lazily evaluated as you ask for the next result. So
if you change the graph or want to start again, make a new query.
It might be that re-executing the same query might not be working, I
can't recall testing that myself. So for now, just free the current
query results, query and make a new one.
> 3. Bonus question: is there any news on implementing optional query
> parameters, in RDQL or otherwise? They'd be mighty handy!
It's on the list of things to do. Early on :) I've got the
algorithm worked out, it's merely a matter of coding now!
>
> Thanks in advance for any help offered.
>
> Kind regards,
> Richard Newman
> School of Systems Engineering
> University of Reading
<snip/>
Thanks for the good example.
In summary, the modified program below works and leaks no memory - I
checked.
Dave
----------------------------------------------------------------------
#include <redland.h>
#include <stdio.h>
#define QUERY "SELECT ?first WHERE \
(?first, rdf:type, rss:item) USING \
rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>, \
rss FOR <http://purl.org/rss/1.0/item>"
int
main(int argc, char **argv) {
librdf_statement *s = NULL;
librdf_world *world = NULL;
librdf_storage *storage = NULL;
librdf_model *model = NULL;
librdf_query *query = NULL;
librdf_query_results *results = NULL;
// Setup.
world = librdf_new_world();
librdf_world_open(world);
librdf_init_statement(world);
storage = librdf_new_storage(world, "memory", "Red", NULL);
model = librdf_new_model(world, storage, NULL);
// Add some statements.
s = librdf_new_statement_from_nodes(world,
librdf_new_node_from_uri_string(world,
"http://example.com/item1"),
librdf_new_node_from_uri_string(world,
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
librdf_new_node_from_uri_string(world,
"http://purl.org/rss/1.0/item"));
librdf_model_add_statement(model, s);
librdf_free_statement(s);
// Query.
query = librdf_new_query(world, "rdql", NULL, QUERY);
if (query) {
// EXECUTE FIRST TIME.
results = librdf_model_query_execute(model, query);
}
if (results) {
printf("Results returned.\n");
librdf_free_query_results(results);
librdf_free_query(query);
query = librdf_new_query(world, "rdql", NULL, QUERY);
if(query) {
results = librdf_model_query_execute(model, query);
results ? printf("Success!\n") : printf("Failure!\n");
}
}
librdf_free_query_results(results);
librdf_free_query(query);
librdf_free_model(model);
librdf_free_storage(storage);
librdf_free_world(world);
return 0;
}
More information about the redland-dev
mailing list