Skip to content

Instantly share code, notes, and snippets.

@rtravis
Created October 30, 2015 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rtravis/d5a6b1b15972a8ce6d87 to your computer and use it in GitHub Desktop.
Save rtravis/d5a6b1b15972a8ce6d87 to your computer and use it in GitHub Desktop.
replicate cleanup issue
Compile:
g++ -std=c++0x -I/usr/include/raptor2 -I/usr/include/rasqal -O0 -g3 -Wall -c -fmessage-length=0 -o "main.o" "../main.cpp"
Link:
g++ -o "RedlandStorage" ... main.o ... obj files ... -lsqlite3 -lrdf -lpthread
Run:
./RedlandStorage -q query.rq
#include "librdf.sqlite/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 = "loader.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());
}
SELECT ?s ?p
WHERE {
?s ?p "101118"^^<http://www.w3.org/2001/XMLSchema#string> .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment