/** * librdf_stream_write: * @stream: the stream object * @iostr: the iostream to write to * * Write a stream of triples to an iostream in a debug format. * * This prints the remaining statements of the stream to the given * #raptor_iostream in a debug format. * * Note that after this method is called the stream will be empty so * that librdf_stream_end() will always be true and * librdf_stream_next() will always return NULL. The only useful * operation is to dispose of the stream with the * librdf_free_stream() destructor. * * This method is for debugging and the format of the output should * not be relied on. In particular, when contexts are used the * result may be 4 nodes. * * Return value: non-0 on failure **/ int librdf_stream_write(librdf_stream *stream, raptor_iostream *iostr) { LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(stream, librdf_stream, 1); LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(iostr, raptor_iostream, 1); while(!librdf_stream_end(stream)) { librdf_statement* statement; librdf_node* context_node; statement = librdf_stream_get_object(stream); if(!statement) break; raptor_iostream_counted_string_write(" ", 2, iostr); if(librdf_statement_write(statement, iostr)) return 1; context_node = librdf_stream_get_context2(stream); if(context_node) { raptor_iostream_counted_string_write(" with context ", 14, iostr); librdf_node_write(context_node, iostr); } raptor_iostream_counted_string_write(". \n", 3, iostr); librdf_stream_next(stream); } return 0; }
/** * librdf_stream_print: * @stream: the stream object * @fh: the FILE stream to print to * * Print the stream. * * This prints the remaining statements of the stream to the given * file handle. Note that after this method is called the stream * will be empty so that librdf_stream_end() will always be true * and librdf_stream_next() will always return NULL. The only * useful operation is to dispose of the stream with the * librdf_free_stream() destructor. * * This method is for debugging and the format of the output should * not be relied on. * * @Deprecated: Use librdf_stream_write() to write to * #raptor_iostream which can be made to write to a string. Use a * #librdf_serializer to write proper syntax formats. * **/ void librdf_stream_print(librdf_stream *stream, FILE *fh) { raptor_iostream *iostr; if(!stream) return; iostr = raptor_new_iostream_to_file_handle(stream->world->raptor_world_ptr, fh); if(!iostr) return; while(!librdf_stream_end(stream)) { librdf_statement* statement = librdf_stream_get_object(stream); librdf_node* context_node = librdf_stream_get_context2(stream); if(!statement) break; fputs(" ", fh); librdf_statement_write(statement, iostr); if(context_node) { fputs(" with context ", fh); librdf_node_print(context_node, fh); } fputs("\n", fh); librdf_stream_next(stream); } raptor_free_iostream(iostr); }
static int librdf_storage_cassandra_add_statements(librdf_storage* storage, librdf_stream* statement_stream) { librdf_storage_cassandra_instance* context; context = (librdf_storage_cassandra_instance*)storage->instance; CassBatch* batch = 0; const int batch_size = 10; int rows = 0; for(; !librdf_stream_end(statement_stream); librdf_stream_next(statement_stream)) { librdf_statement* statement; librdf_node* context_node; statement = librdf_stream_get_object(statement_stream); context_node = librdf_stream_get_context2(statement_stream); if(!statement) { break; } char* s; char* p; char* o; char* c; statement_helper(storage, statement, context_node, &s, &p, &o, &c); if (batch == 0) batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED); char* query = "INSERT INTO rdf.spo (s, p, o) VALUES (?, ?, ?);"; CassStatement* stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); query = "INSERT INTO rdf.pos (s, p, o) VALUES (?, ?, ?);"; stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); query = "INSERT INTO rdf.osp (s, p, o) VALUES (?, ?, ?);"; stmt = cass_statement_new(query, 3); cass_statement_bind_string(stmt, 0, s); cass_statement_bind_string(stmt, 1, p); cass_statement_bind_string(stmt, 2, o); cass_batch_add_statement(batch, stmt); cass_statement_free(stmt); free(s); free(p); free(o); if (++rows > batch_size) { CassFuture* future = cass_session_execute_batch(context->session, batch); cass_batch_free(batch); 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 -1; } cass_future_free(future); batch = 0; } } if (batch) { CassFuture* future = cass_session_execute_batch(context->session, batch); cass_batch_free(batch); 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 -1; } cass_future_free(future); } return 0; }
/** * librdf_stream_get_context: * @stream: the #librdf_stream object * * Get the context of the current object on the stream. * * This method returns a SHARED pointer to the current context node object * which should be copied by the caller to preserve it if the stream * is moved on librdf_stream_next or if it should last after the * stream is closed. * * @Deprecated: Use librdf_stream_get_context2() which returns a #librdf_node * * Return value: The context node (can be NULL) or NULL if the stream has finished. **/ void* librdf_stream_get_context(librdf_stream* stream) { return librdf_stream_get_context2(stream); }
static rasqal_triple_parts rasqal_redland_bind_match(struct rasqal_triples_match_s* rtm, void *user_data, rasqal_variable* bindings[4], rasqal_triple_parts parts) { rasqal_redland_triples_match_context* rtmc=(rasqal_redland_triples_match_context*)rtm->user_data; rasqal_literal* l; librdf_statement* statement; rasqal_triple_parts result=(rasqal_triple_parts)0; librdf_world *world = rtmc->stream->world; statement=librdf_stream_get_object(rtmc->stream); if(!statement) return (rasqal_triple_parts)0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1(" matched statement "); librdf_statement_print(statement, stderr); fputc('\n', stderr); #endif /* set 1 or 2 variable values from the fields of statement */ if(bindings[0] && (parts & RASQAL_TRIPLE_SUBJECT)) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding subject to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_subject(statement)); rasqal_variable_set_value(bindings[0], l); result= RASQAL_TRIPLE_SUBJECT; } if(bindings[1] && (parts & RASQAL_TRIPLE_PREDICATE)) { if(bindings[0] == bindings[1]) { /* check matching(?x, ?x, ...) / subject=predicate */ if(!librdf_node_equals(librdf_statement_get_subject(statement), librdf_statement_get_predicate(statement))) return (rasqal_triple_parts)0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and predicate values match\n"); #endif } else { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding predicate to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_predicate(statement)); rasqal_variable_set_value(bindings[1], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_PREDICATE); } } if(bindings[2] && (parts & RASQAL_TRIPLE_OBJECT)) { int bind=1; if(bindings[0] == bindings[2]) { /* check matching (?x, ..., ?x) / subject=object */ if(!librdf_node_equals(librdf_statement_get_subject(statement), librdf_statement_get_object(statement))) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and object values match\n"); #endif } if((bindings[1] == bindings[2]) && !(bindings[0] == bindings[1])) { /* check matching (..., ?x, ?x) / predicate=object * Don't do this if matching (?x, ?x, ...) / subject=predicate * was already done since that would mean the match was (?x, ?x, ?x) */ if(!librdf_node_equals(librdf_statement_get_predicate(statement), librdf_statement_get_object(statement))) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("predicate and object values match\n"); #endif } if(bind) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding object to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_object(statement)); rasqal_variable_set_value(bindings[2], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_OBJECT); } } /* Contexts */ if(bindings[3] && (parts & RASQAL_TRIPLE_ORIGIN)) { int bind=1; librdf_node* context_node = librdf_stream_get_context2(rtmc->stream); if(bindings[0] == bindings[3]) { /* check matching (?x, ..., ...) in context ?x */ if(!librdf_node_equals(librdf_statement_get_subject(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and context values match\n"); #endif } if(bindings[1] == bindings[3]) { /* check matching (..., ?x, ...) in context ?x */ if(!librdf_node_equals(librdf_statement_get_predicate(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("predicate and context values match\n"); #endif } if(bindings[2] == bindings[3]) { /* check matching (..., ..., ?x) in context ?x */ if(!librdf_node_equals(librdf_statement_get_object(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("object and context values match\n"); #endif } if(bind) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding origin to variable\n"); #endif if(context_node) l = redland_node_to_rasqal_literal(world, context_node); else l=NULL; rasqal_variable_set_value(bindings[3], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_ORIGIN); } } return result; }