-
-
Save mro/1d173e9195d088288e81 to your computer and use it in GitHub Desktop.
replicate cleanup issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on https://gist.github.com/rtravis/d5a6b1b15972a8ce6d87 | |
// | |
// Compile: | |
// $ gcc -std=c99 -D DEBUG=1 -I /usr/include/raptor2 -I /usr/include/rasqal -c -o rdf_storage_sqlite_mro.o ../rdf_storage_sqlite_mro.c | |
// $ g++ -std=c++0x -I/usr/include/raptor2 -I/usr/include/rasqal -O0 -g3 -Wall -c -fmessage-length=0 -o RedlandStorage.o RedlandStorage.cc | |
// | |
// Link: | |
// $ g++ -o RedlandStorage RedlandStorage.o rdf_storage_sqlite_mro.o -lsqlite3 -lrdf | |
// | |
// Run: | |
// ./RedlandStorage -q RedlandStorage.rq | |
// | |
#include "../rdf_storage_sqlite_mro.h" | |
#include <memory> | |
#include <unistd.h> | |
#include <iostream> | |
#include <sstream> | |
#include <fstream> | |
#include <string.h> | |
using namespace std; | |
static shared_ptr <librdf_world> make_rdf_world() | |
{ | |
shared_ptr <librdf_world> world(librdf_new_world(), | |
&librdf_free_world); | |
librdf_world_open( world.get() ); | |
// register SQLite storage factory | |
librdf_init_storage_sqlite_mro( world.get() ); | |
return world; | |
} | |
static shared_ptr <librdf_storage> make_rdf_storage(librdf_world *world, const char *file_path, bool is_new = false, bool use_contexts = true) | |
{ | |
char options[64]; | |
snprintf(options, sizeof(options), | |
"new='%s', contexts='%s', synchronous='off'", | |
is_new ? "yes" : "no", use_contexts ? "yes" : "no"); | |
shared_ptr <librdf_storage> store(librdf_new_storage(world, LIBRDF_STORAGE_SQLITE_MRO, file_path, options), | |
&librdf_free_storage); | |
return store; | |
} | |
static shared_ptr <librdf_model> make_rdf_model(librdf_world *world, librdf_storage *store) | |
{ | |
shared_ptr <librdf_model> model(librdf_new_model(world, store, NULL), &librdf_free_model); | |
return model; | |
} | |
static void run_query(librdf_world *world, librdf_model *model, const char *query_string, const char *lang = "sparql") | |
{ | |
shared_ptr <librdf_query> rdf_query(librdf_new_query(world, lang, nullptr, (const unsigned char *)query_string, nullptr), | |
&librdf_free_query); | |
librdf_query_results *res = librdf_query_execute(rdf_query.get(), model); | |
if( !res ) { | |
return; | |
} | |
librdf_query_results_to_file_handle2(res, stdout, "csv", nullptr, nullptr, nullptr); | |
librdf_free_query_results(res); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
string queryFile; | |
for( int i = 0; i < argc; ++i ) { | |
if( strcmp(argv[i], "-q") == 0 && (i + 1) < argc ) { | |
// query file | |
queryFile = argv[i + 1]; | |
i++; | |
} | |
} | |
if( queryFile.empty() ) { | |
cout << "Usage: " << argv[0] << " [-q <sparql_query_file> ]\n"; | |
return 1; | |
} | |
shared_ptr <librdf_world> world = make_rdf_world(); | |
if( !world ) { | |
return 1; | |
} | |
// TODO: set database file name here ! | |
const char *db_file = "RedlandStorage.sqlite"; | |
bool is_new = (access(db_file, F_OK) < 0); | |
shared_ptr <librdf_storage> store = make_rdf_storage(world.get(), db_file, is_new); | |
if( !store ) { | |
return 1; | |
} | |
shared_ptr <librdf_model> model = make_rdf_model( world.get(), store.get() ); | |
if( !model ) { | |
return 1; | |
} | |
string query; | |
if( !queryFile.empty() ) { | |
ifstream ifs( queryFile.c_str() ); | |
stringstream ss; | |
ss << ifs.rdbuf(); | |
query = ss.str(); | |
} | |
run_query( world.get(), model.get(), query.c_str() ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/rtravis/d5a6b1b15972a8ce6d87 | |
SELECT ?s ?p | |
WHERE { | |
?s ?p "101118"^^<http://www.w3.org/2001/XMLSchema#string> . | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cd "$(dirname "$0")" | |
# sudo apt-get install gcc librdf0-dev raptor2-utils | |
# sudo apt-get install librdf-storage-sqlite | |
# sudo apt-get install valgrind | |
set -x | |
# wget --no-check-certificate https://www.sqlite.org/2015/sqlite-amalgamation-3090100.zip | |
# gcc -g3 -O0 -Wall -std=c99 -c -o sqlite3.o sqlite-amalgamation-3090100/sqlite3.c | |
gcc -g3 -O0 -std=c99 -D DEBUG=1 -I /usr/include/raptor2 -I /usr/include/rasqal -c -o rdf_storage_sqlite_mro.o ../rdf_storage_sqlite_mro.c | |
g++ -g3 -O0 -Wall -std=c++0x -I/usr/include/raptor2 -I/usr/include/rasqal -c -fmessage-length=0 -o RedlandStorage.o RedlandStorage.cc | |
g++ -g3 -o RedlandStorage RedlandStorage.o rdf_storage_sqlite_mro.o -lrdf -lsqlite3 | |
rm RedlandStorage.sqlite | |
valgrind --leak-check=full --show-reachable=yes ./RedlandStorage -q RedlandStorage.rq | |
# ./RedlandStorage -q RedlandStorage.rq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment