static int librdf_storage_file_remove_statement(librdf_storage* storage, librdf_statement* statement) { librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance; context->changed=1; return librdf_model_remove_statement(context->model, statement); }
/* * call-seq: * graph.remove( statement ) -> array * * Removes one or more statements from the graph that match the specified +statement+ * (either a Redleaf::Statement or a valid triple in an Array) and returns any that * were removed. * * Any +nil+ values in the statement will match any value. * * # Set a new home page for the Redleaf project, preserving the old one * # as the 'old_homepage' * stmt = graph.remove([ :Redleaf, DOAP[:homepage], nil ]) * stmt.predicate = DOAP[:old_homepage] * graph.append( stmt ) * graph.append([ :Redleaf, DOAP[:homepage], * URL.parse('http://deveiate.org/projects/Redleaf') ]) */ static VALUE rleaf_redleaf_graph_remove( VALUE self, VALUE statement ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_statement *search_statement, *stmt; librdf_stream *stream; int count = 0; VALUE rval = rb_ary_new(); rleaf_log_with_context( self, "debug", "removing statements matching %s", RSTRING_PTR(rb_inspect(statement)) ); search_statement = rleaf_value_to_librdf_statement( statement ); stream = librdf_model_find_statements( ptr->model, search_statement ); if ( !stream ) { librdf_free_statement( search_statement ); rb_raise( rleaf_eRedleafError, "could not create a stream when removing %s from %s", RSTRING_PTR(rb_inspect(statement)), RSTRING_PTR(rb_inspect(self)) ); } while ( ! librdf_stream_end(stream) ) { if ( (stmt = librdf_stream_get_object( stream )) == NULL ) break; count++; rb_ary_push( rval, rleaf_librdf_statement_to_value(stmt) ); if ( librdf_model_remove_statement(ptr->model, stmt) != 0 ) { librdf_free_stream( stream ); librdf_free_statement( search_statement ); rb_raise( rleaf_eRedleafError, "failed to remove statement from model" ); } librdf_stream_next( stream ); } rleaf_log_with_context( self, "debug", "removed %d statements", count ); librdf_free_stream( stream ); librdf_free_statement( search_statement ); return rval; }
bool RdfStorePrivate::Remove(RdfTriple triple) { librdf_statement* statement = RdfTripleToStatement(triple); if (!CheckComplete(statement)) { librdf_free_statement(statement); return false; } // Store does not contain statement if (!Contains(triple)) return true; if (librdf_model_remove_statement(m_Model, statement) != 0) { librdf_free_statement(statement); return false; } else { librdf_free_statement(statement); return true; } return false; }
int main(int argc, char** argv) { try { /*********************************************************************/ /* Initialise */ /*********************************************************************/ std::cout << "** Initialise" << std::endl; librdf_world* world = librdf_new_world(); if (world == 0) throw std::runtime_error("Didn't get world"); librdf_storage* storage = librdf_new_storage(world, STORE, STORE_NAME, "new='yes'"); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* model = librdf_new_model(world, storage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); /*********************************************************************/ /* Create in-memory model */ /*********************************************************************/ std::cout << "** Create in-memory model" << std::endl; librdf_storage* mstorage = librdf_new_storage(world, "memory", 0, 0); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* mmodel = librdf_new_model(world, mstorage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); for(int i = 0; i < 10; i++) { char sbuf[256]; char obuf[256]; sprintf(sbuf, "http://gaffer.test/number#%d", i); sprintf(obuf, "http://gaffer.test/number#%d", i+1); librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) sbuf); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) "http://gaffer.test/number#is_before"); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char*) obuf); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(mmodel, st); librdf_free_statement(st); } /*********************************************************************/ /* Size */ /*********************************************************************/ std::cout << "** Model size is " << librdf_model_size(model) << std::endl; /*********************************************************************/ /* Add statement */ /*********************************************************************/ std::cout << "** Add statement" << std::endl; const char* fred = "http://gaffer.test/#fred"; const char* is_a = "http://gaffer.test/#is_a"; const char* cat = "http://gaffer.test/#cat"; librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) fred); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) is_a); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char *) cat); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(model, st); /*********************************************************************/ /* Add memory model statements */ /*********************************************************************/ std::cout << "** Add statements" << std::endl; librdf_stream* strm = librdf_model_as_stream(mmodel); if (strm == 0) throw std::runtime_error("Couldn't get memory stream"); int ret = librdf_model_add_statements(model, strm); if (ret != 0) throw std::runtime_error("Couldn't add_statements"); librdf_free_stream(strm); librdf_free_model(mmodel); librdf_free_storage(mstorage); /*********************************************************************/ /* Run queries */ /*********************************************************************/ run_query(world, model, query_string8); run_query(world, model, query_string1); run_query(world, model, query_string2); run_query(world, model, query_string3); run_query(world, model, query_string4); run_query(world, model, query_string5); run_query(world, model, query_string6); run_query2(world, model, query_string7); /*********************************************************************/ /* Remove statement */ /*********************************************************************/ std::cout << "** Remove statements" << std::endl; librdf_model_remove_statement(model, st); /*********************************************************************/ /* Serialise */ /*********************************************************************/ std::cout << "** Serialise" << std::endl; strm = librdf_model_as_stream(model); if (strm == 0) throw std::runtime_error("Couldn't get model as stream"); librdf_serializer* srl = librdf_new_serializer(world, "ntriples", 0, 0); if (srl == 0) throw std::runtime_error("Couldn't create serialiser"); size_t len; unsigned char* out = librdf_serializer_serialize_stream_to_counted_string(srl, 0, strm, &len); std::cout << "--------------------------------------------------------" << std::endl; write(1, out, len); std::cout << "--------------------------------------------------------" << std::endl; free(out); librdf_free_stream(strm); librdf_free_serializer(srl); /*********************************************************************/ /* Cleanup */ /*********************************************************************/ librdf_free_statement(st); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } }