static librdf_stream* librdf_storage_tstore_serialise(librdf_storage* storage) { librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)storage->instance; librdf_storage_tstore_serialise_stream_context* scontext; librdf_stream* stream; scontext=(librdf_storage_tstore_serialise_stream_context*)LIBRDF_CALLOC(librdf_storage_tstore_serialise_stream_context, 1, sizeof(librdf_storage_tstore_serialise_stream_context)); if(!scontext) return NULL; scontext->storage=storage; librdf_storage_add_reference(scontext->storage); scontext->result=rs_find_all_resources(context->rdfsql, 0, context->model); if(!scontext->result) /* empty */ scontext->triple=NULL; else scontext->triple=rs_next_triple(scontext->result); stream=librdf_new_stream(storage->world, (void*)scontext, &librdf_storage_tstore_serialise_end_of_stream, &librdf_storage_tstore_serialise_next_statement, &librdf_storage_tstore_serialise_get_statement, &librdf_storage_tstore_serialise_finished); if(!stream) { librdf_storage_tstore_serialise_finished((void*)scontext); return NULL; } return stream; }
static librdf_stream* librdf_query_rasqal_results_as_stream(librdf_query_results* query_results) { librdf_query *query=query_results->query; librdf_query_rasqal_context *context=(librdf_query_rasqal_context*)query->context; librdf_query_rasqal_stream_context* scontext; librdf_stream *stream; if(!context->results) return NULL; scontext = LIBRDF_CALLOC(librdf_query_rasqal_stream_context*, 1, sizeof(*scontext)); if(!scontext) return NULL; scontext->query=query; scontext->qcontext=context; librdf_query_rasqal_query_results_update_statement(scontext); stream=librdf_new_stream(query->world, (void*)scontext, &librdf_query_rasqal_query_results_end_of_stream, &librdf_query_rasqal_query_results_next_statement, &librdf_query_rasqal_query_results_get_statement, &librdf_query_rasqal_query_results_finished); if(!stream) { librdf_query_rasqal_query_results_finished((void*)scontext); return NULL; } return stream; }
/** * librdf_storage_tstore_find_statements: * @storage: the storage * @statement: the statement to match * * . * * Return a stream of statements matching the given statement (or * all statements if NULL). Parts (subject, predicate, object) of the * statement can be empty in which case any statement part will match that. * Uses #librdf_statement_match to do the matching. * * Return value: a #librdf_stream or NULL on failure **/ static librdf_stream* librdf_storage_tstore_find_statements(librdf_storage* storage, librdf_statement* statement) { librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)storage->instance; librdf_storage_tstore_find_stream_context* scontext; librdf_stream* stream; rs_triple* triple; rs_obj_type type; statement=librdf_new_statement_from_statement(statement); if(!statement) return NULL; scontext=(librdf_storage_tstore_find_stream_context*)LIBRDF_CALLOC(librdf_storage_tstore_find_stream_context, 1, sizeof(librdf_storage_tstore_find_stream_context)); if(!scontext) return NULL; scontext->storage=storage; librdf_storage_add_reference(scontext->storage); triple=librdf_storage_tstore_statement_as_rs_triple(statement); scontext->search_triple=triple; if(triple->object) type=(triple->literal ? ObjLiteral: ObjURI); else type=ObjAny; scontext->result=rs_find_triples(context->rdfsql, triple->subject, triple->predicate, triple->object, type, 0, context->model); if(!scontext->result) /* empty */ scontext->triple=NULL; else scontext->triple=rs_next_triple(scontext->result); stream=librdf_new_stream(storage->world, (void*)scontext, &librdf_storage_tstore_find_end_of_stream, &librdf_storage_tstore_find_next_statement, &librdf_storage_tstore_find_get_statement, &librdf_storage_tstore_find_finished); if(!stream) { librdf_storage_tstore_find_finished((void*)scontext); return NULL; } return stream; }
/** * librdf_new_stream_from_node_iterator: * @iterator: #librdf_iterator of #librdf_node objects * @statement: #librdf_statement prototype with one NULL node space * @field: node part of statement * * Constructor - create a new #librdf_stream from an iterator of nodes. * * Creates a new #librdf_stream using the passed in #librdf_iterator * which generates a series of #librdf_node objects. The resulting * nodes are then inserted into the given statement and returned. * The field attribute indicates which statement node is being generated. * * Return value: a new #librdf_stream object or NULL on failure **/ librdf_stream* librdf_new_stream_from_node_iterator(librdf_iterator* iterator, librdf_statement* statement, librdf_statement_part field) { librdf_stream_from_node_iterator_stream_context *scontext; librdf_stream *stream; scontext=(librdf_stream_from_node_iterator_stream_context*)LIBRDF_CALLOC(librdf_stream_from_node_iterator_stream_context, 1, sizeof(librdf_stream_from_node_iterator_stream_context)); if(!scontext) return NULL; /* copy the prototype statement */ statement=librdf_new_statement_from_statement(statement); if(!statement) { LIBRDF_FREE(librdf_stream_from_node_iterator_stream_context, scontext); return NULL; } scontext->iterator=iterator; scontext->current=statement; scontext->field=field; stream=librdf_new_stream(iterator->world, (void*)scontext, &librdf_stream_from_node_iterator_end_of_stream, &librdf_stream_from_node_iterator_next_statement, &librdf_stream_from_node_iterator_get_statement, &librdf_stream_from_node_iterator_finished); if(!stream) { librdf_stream_from_node_iterator_finished((void*)scontext); return NULL; } return stream; }
static librdf_stream* librdf_storage_trees_serialise_range(librdf_storage* storage, librdf_statement* range) { librdf_storage_trees_instance* context=(librdf_storage_trees_instance*)storage->instance; librdf_storage_trees_serialise_stream_context* scontext; librdf_stream* stream; int filter = 0; scontext=(librdf_storage_trees_serialise_stream_context*)LIBRDF_CALLOC(librdf_storage_trees_serialise_stream_context, 1, sizeof(librdf_storage_trees_serialise_stream_context)); if(!scontext) return NULL; scontext->iterator = NULL; /* ?s ?p ?o */ if (!range || (!range->subject && !range->predicate && !range->object)) { scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree, NULL, NULL); if (range) { librdf_free_statement(range); range=NULL; } /* s ?p o */ } else if (range->subject && !range->predicate && range->object) { if (context->index_sop) scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->sop_tree, range, librdf_storage_trees_avl_free); else filter=1; /* s _ _ */ } else if (range->subject) { scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree, range, librdf_storage_trees_avl_free); /* ?s _ o */ } else if (range->object) { if (context->index_ops) scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->ops_tree, range, librdf_storage_trees_avl_free); else filter=1; /* ?s p ?o */ } else { /* range->predicate != NULL */ if (context->index_pso) scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->pso_tree, range, librdf_storage_trees_avl_free); else filter=1; } /* If filter is set, we're missing the required index. * Iterate over the entire model and filter the stream. * (With a fully indexed store, this will never happen) */ if (filter) { scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree, range, librdf_storage_trees_avl_free); } #ifdef RDF_STORAGE_TREES_WITH_CONTEXTS scontext->context_node=NULL; #endif if(!scontext->iterator) { LIBRDF_FREE(librdf_storage_trees_serialise_stream_context, scontext); return librdf_new_empty_stream(storage->world); } scontext->storage=storage; librdf_storage_add_reference(scontext->storage); stream=librdf_new_stream(storage->world, (void*)scontext, &librdf_storage_trees_serialise_end_of_stream, &librdf_storage_trees_serialise_next_statement, &librdf_storage_trees_serialise_get_statement, &librdf_storage_trees_serialise_finished); if(!stream) { librdf_storage_trees_serialise_finished((void*)scontext); return NULL; } if(filter) { if(librdf_stream_add_map(stream, &librdf_stream_statement_find_map, NULL, (void*)range)) { /* error - stream_add_map failed */ librdf_free_stream(stream); stream=NULL; } } return stream; }
/** * librdf_storage_cassandra_find_statements: * @storage: the storage * @statement: the statement to match * * . * * Return a stream of statements matching the given statement (or * all statements if NULL). Parts (subject, predicate, object) of the * statement can be empty in which case any statement part will match that. * Uses #librdf_statement_match to do the matching. * * Return value: a #librdf_stream or NULL on failure **/ static librdf_stream* librdf_storage_cassandra_find_statements(librdf_storage* storage, librdf_statement* statement) { librdf_storage_cassandra_instance* context; cassandra_results_stream* scontext; librdf_stream* stream; char* s; char* p; char* o; char* c; context = (librdf_storage_cassandra_instance*)storage->instance; scontext = LIBRDF_CALLOC(cassandra_results_stream*, 1, sizeof(*scontext)); if(!scontext) return NULL; scontext->storage = storage; librdf_storage_add_reference(scontext->storage); scontext->cassandra_context = context; statement_helper(storage, statement, 0, &s, &p, &o, &c); #ifdef DEBUG fprintf(stderr, "Query: "); if (s) fprintf(stderr, "s=%s ", s); if (p) fprintf(stderr, "p=%s ", p); if (o) fprintf(stderr, "o=%s ", o); fprintf(stderr, "\n"); #endif typedef CassStatement* (*query_function)(const char* s, const char* p, const char* o); query_function functions[8] = { &cassandra_query_, /* ??? */ &cassandra_query_s, /* S?? */ &cassandra_query_p, /* ?P? */ &cassandra_query_sp, /* SP? */ &cassandra_query_o, /* ??O */ &cassandra_query_so, /* S?O */ &cassandra_query_po, /* ?PO */ &cassandra_query_spo /* SPO */ }; /* This creates an index into the function table, depending on input terms. */ int num = 0; if (o) num += 4; if (p) num += 2; if (s) num++; index_type tp; query_function fn = functions[num]; CassStatement* stmt = (*fn)(s, p, o); cass_statement_set_paging_size(stmt, 1000); if (s) free(s); if (p) free(p); if (o) free(o); CassFuture* future = cass_session_execute(context->session, stmt); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); const char* msg; size_t msg_len; cass_future_error_message(future, &msg, &msg_len); fprintf(stderr, "Cassandra: %*s\n", msg_len, msg); cass_future_free(future); return 0; } const CassResult* result = cass_future_get_result(future); cass_future_free(future); CassIterator* iter = cass_iterator_from_result(result); scontext->stmt = stmt; scontext->result = result; scontext->iter = iter; scontext->more_pages = cass_result_has_more_pages(result); scontext->at_end = !cass_iterator_next(scontext->iter); stream = librdf_new_stream(storage->world, (void*)scontext, &cassandra_results_stream_end_of_stream, &cassandra_results_stream_next_statement, &cassandra_results_stream_get_statement, &cassandra_results_stream_finished); if(!stream) { cassandra_results_stream_finished((void*)scontext); return NULL; } return stream; }