use C++14 optional as wildcard in Ostrich SPARQL
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/76ed0613 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/76ed0613 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/76ed0613 Branch: refs/heads/develop Commit: 76ed0613164085e0dc807c0a1e78b3a87ef9277f Parents: 3828f65 Author: Sebastian Schaffert <[email protected]> Authored: Mon Aug 22 21:53:40 2016 +0200 Committer: Sebastian Schaffert <[email protected]> Committed: Mon Aug 22 21:53:40 2016 +0200 ---------------------------------------------------------------------- libraries/ostrich/backend/CMakeLists.txt | 2 +- .../backend/persistence/leveldb_sparql.cc | 22 ++++++----- .../backend/persistence/leveldb_sparql.h | 10 ++++- .../ostrich/backend/sparql/rasqal_adapter.cc | 40 ++++++++++---------- .../ostrich/backend/sparql/rasqal_adapter.h | 10 +++-- libraries/ostrich/backend/test/SparqlTest.cc | 26 +++++++------ 6 files changed, 60 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/CMakeLists.txt b/libraries/ostrich/backend/CMakeLists.txt index 50efd24..87dfc7f 100644 --- a/libraries/ostrich/backend/CMakeLists.txt +++ b/libraries/ostrich/backend/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) project(Marmotta) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -g") set(PROTOBUF_IMPORT_DIRS "${CMAKE_SOURCE_DIR}/model") set(USE_TCMALLOC TRUE) http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/persistence/leveldb_sparql.cc ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/persistence/leveldb_sparql.cc b/libraries/ostrich/backend/persistence/leveldb_sparql.cc index a71dc46..f0983b7 100644 --- a/libraries/ostrich/backend/persistence/leveldb_sparql.cc +++ b/libraries/ostrich/backend/persistence/leveldb_sparql.cc @@ -35,19 +35,20 @@ class WrapProtoStatementIterator : public util::ConvertingIterator<rdf::proto::S bool LevelDBTripleSource::HasStatement( - const rdf::Resource *s, const rdf::URI *p, const rdf::Value *o, const rdf::Resource *c) { + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) { rdf::proto::Statement pattern; - if (s != nullptr) { + if (s) { *pattern.mutable_subject() = s->getMessage(); } - if (p != nullptr) { + if (p) { *pattern.mutable_predicate() = p->getMessage(); } - if (o != nullptr) { + if (o) { *pattern.mutable_object() = o->getMessage(); } - if (c != nullptr) { + if (c) { *pattern.mutable_context() = c->getMessage(); } @@ -61,19 +62,20 @@ bool LevelDBTripleSource::HasStatement( } std::unique_ptr<sparql::StatementIterator> LevelDBTripleSource::GetStatements( - const rdf::Resource *s, const rdf::URI *p, const rdf::Value *o, const rdf::Resource *c) { + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) { rdf::proto::Statement pattern; - if (s != nullptr) { + if (s) { *pattern.mutable_subject() = s->getMessage(); } - if (p != nullptr) { + if (p) { *pattern.mutable_predicate() = p->getMessage(); } - if (o != nullptr) { + if (o) { *pattern.mutable_object() = o->getMessage(); } - if (c != nullptr) { + if (c) { *pattern.mutable_context() = c->getMessage(); } http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/persistence/leveldb_sparql.h ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/persistence/leveldb_sparql.h b/libraries/ostrich/backend/persistence/leveldb_sparql.h index 9d8e989..7b103a4 100644 --- a/libraries/ostrich/backend/persistence/leveldb_sparql.h +++ b/libraries/ostrich/backend/persistence/leveldb_sparql.h @@ -25,6 +25,8 @@ namespace marmotta { namespace persistence { namespace sparql { +using std::experimental::optional; + /** * A SPARQL triple source using a LevelDBPersistence to access data. */ @@ -34,10 +36,14 @@ class LevelDBTripleSource : public ::marmotta::sparql::TripleSource { LevelDBTripleSource(LevelDBPersistence *persistence) : persistence(persistence) { } - bool HasStatement(const rdf::Resource *s, const rdf::URI *p, const rdf::Value *o, const rdf::Resource *c) override; + bool HasStatement( + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) override; std::unique_ptr<::marmotta::sparql::StatementIterator> - GetStatements(const rdf::Resource *s, const rdf::URI *p, const rdf::Value *o, const rdf::Resource *c) override; + GetStatements( + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) override; private: // A pointer to the persistence instance wrapped by this triple source. http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/sparql/rasqal_adapter.cc ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/sparql/rasqal_adapter.cc b/libraries/ostrich/backend/sparql/rasqal_adapter.cc index 1f6055b..9081e0d 100644 --- a/libraries/ostrich/backend/sparql/rasqal_adapter.cc +++ b/libraries/ostrich/backend/sparql/rasqal_adapter.cc @@ -124,52 +124,52 @@ int init_triples_match( SparqlService *self = (SparqlService *) *(void**)user_data; - std::unique_ptr<rdf::Resource> s = nullptr; - std::unique_ptr<rdf::URI> p = nullptr; - std::unique_ptr<rdf::Value> o = nullptr; - std::unique_ptr<rdf::Resource> c = nullptr; + optional<rdf::Resource> s; + optional<rdf::URI> p; + optional<rdf::Value> o; + optional<rdf::Resource> c; rasqal_variable* var; if ((var=rasqal_literal_as_variable(t->subject))) { m->bindings[0] = var; if (var->value) { - s.reset(new rdf::Resource(rasqal::ConvertResource(var->value))); + s = rasqal::ConvertResource(var->value); } } else { - s.reset(new rdf::Resource(rasqal::ConvertResource(t->subject))); + s = rasqal::ConvertResource(t->subject); } if ((var=rasqal_literal_as_variable(t->predicate))) { m->bindings[1] = var; if (var->value) { - p.reset(new rdf::URI(rasqal::ConvertURI(var->value))); + p = rasqal::ConvertURI(var->value); } } else { - p.reset(new rdf::URI(rasqal::ConvertURI(t->predicate))); + p = rasqal::ConvertURI(t->predicate); } if ((var=rasqal_literal_as_variable(t->object))) { m->bindings[2] = var; if (var->value) { - o.reset(new rdf::Value(rasqal::ConvertValue(var->value))); + o = rasqal::ConvertValue(var->value); } } else { - o.reset(new rdf::Value(rasqal::ConvertValue(t->object))); + o = rasqal::ConvertValue(t->object); } if(t->origin) { if ((var=rasqal_literal_as_variable(t->origin))) { m->bindings[3] = var; if (var->value) { - c.reset(new rdf::Resource(rasqal::ConvertResource(var->value))); + c = rasqal::ConvertResource(var->value); } } else { - c.reset(new rdf::Resource(rasqal::ConvertResource(t->origin))); + c = rasqal::ConvertResource(t->origin); } } // Store C++ iterator in user_data and take ownership - auto it = self->Source().GetStatements(s.get(), p.get(), o.get(), c.get()); + auto it = self->Source().GetStatements(s, p, o, c); rtm->user_data = it.release(); rtm->bind_match = bind_match; @@ -185,18 +185,16 @@ int triple_present( struct rasqal_triples_source_s *rts, void *user_data, rasqal_triple *t) { DLOG(INFO) << "Check triple"; - auto s = rasqal::ConvertResource(t->subject); - auto p = rasqal::ConvertURI(t->predicate); - auto o = rasqal::ConvertValue(t->object); + optional<rdf::Resource> s = rasqal::ConvertResource(t->subject); + optional<rdf::URI> p = rasqal::ConvertURI(t->predicate); + optional<rdf::Value> o = rasqal::ConvertValue(t->object); + optional<rdf::Resource> c; SparqlService *self = (SparqlService *) *(void**)user_data; if ((t->flags & RASQAL_TRIPLE_ORIGIN) != 0) { - auto c = rasqal::ConvertResource(t->origin); - - return self->Source().HasStatement(&s, &p, &o, &c); - } else { - return self->Source().HasStatement(&s, &p, &o, nullptr); + c = rasqal::ConvertResource(t->origin); } + return self->Source().HasStatement(s, p, o, c); } void free_triples_source(void *user_data) { http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/sparql/rasqal_adapter.h ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/sparql/rasqal_adapter.h b/libraries/ostrich/backend/sparql/rasqal_adapter.h index 68f7b69..451b16d 100644 --- a/libraries/ostrich/backend/sparql/rasqal_adapter.h +++ b/libraries/ostrich/backend/sparql/rasqal_adapter.h @@ -20,6 +20,7 @@ #include <map> #include <memory> +#include <experimental/optional> #include <rasqal/rasqal.h> #include "model/rdf_model.h" @@ -29,6 +30,7 @@ namespace marmotta { namespace sparql { using StatementIterator = util::CloseableIterator<rdf::Statement>; +using std::experimental::optional; /** * An abstract superclass for more easily interfacing from the C++ Marmotta model @@ -42,8 +44,8 @@ class TripleSource { * Parameters with nullptr value are interpreted as wildcards. */ virtual bool HasStatement( - const rdf::Resource* s, const rdf::URI* p, const rdf::Value* o, - const rdf::Resource* c) = 0; + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) = 0; /** * Return an iterator over statements matching the given subject, predicate, @@ -52,8 +54,8 @@ class TripleSource { * Parameters with nullptr value are interpreted as wildcards. */ virtual std::unique_ptr<StatementIterator> GetStatements( - const rdf::Resource* s, const rdf::URI* p, const rdf::Value* o, - const rdf::Resource* c) = 0; + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) = 0; }; class SparqlException : public std::exception { http://git-wip-us.apache.org/repos/asf/marmotta/blob/76ed0613/libraries/ostrich/backend/test/SparqlTest.cc ---------------------------------------------------------------------- diff --git a/libraries/ostrich/backend/test/SparqlTest.cc b/libraries/ostrich/backend/test/SparqlTest.cc index a45a282..75d00ca 100644 --- a/libraries/ostrich/backend/test/SparqlTest.cc +++ b/libraries/ostrich/backend/test/SparqlTest.cc @@ -13,7 +13,7 @@ namespace { const rdf::URI base_uri("http://example.com/"); - +using std::experimental::optional; using MockStatementIterator = util::CollectionIterator<rdf::Statement>; class MockTripleSource : public TripleSource { @@ -22,19 +22,20 @@ class MockTripleSource : public TripleSource { MockTripleSource(std::vector<rdf::Statement> statements) : statements(statements) { } - bool HasStatement(const rdf::Resource *s, const rdf::URI *p, const rdf::Value *o, const rdf::Resource *c) override { + bool HasStatement(const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) override { for (const auto& stmt : statements) { bool match = true; - if (s != nullptr && *s != stmt.getSubject()) { + if (s && *s != stmt.getSubject()) { match = false; } - if (p != nullptr && *p != stmt.getPredicate()) { + if (p && *p != stmt.getPredicate()) { match = false; } - if (o != nullptr && *o != stmt.getObject()) { + if (o && *o != stmt.getObject()) { match = false; } - if (c != nullptr && *c != stmt.getContext()) { + if (c && *c != stmt.getContext()) { match = false; } if (!match) { @@ -44,21 +45,22 @@ class MockTripleSource : public TripleSource { return false; } - std::unique_ptr<StatementIterator> GetStatements(const rdf::Resource *s, const rdf::URI *p, - const rdf::Value *o, const rdf::Resource *c) override { + std::unique_ptr<StatementIterator> GetStatements( + const optional<rdf::Resource>& s, const optional<rdf::URI>& p, + const optional<rdf::Value>& o, const optional<rdf::Resource>& c) override { std::vector<rdf::Statement> results; for (const auto& stmt : statements) { bool match = true; - if (s != nullptr && *s != stmt.getSubject()) { + if (s && *s != stmt.getSubject()) { match = false; } - if (p != nullptr && *p != stmt.getPredicate()) { + if (p && *p != stmt.getPredicate()) { match = false; } - if (o != nullptr && *o != stmt.getObject()) { + if (o && *o != stmt.getObject()) { match = false; } - if (c != nullptr && *c != stmt.getContext()) { + if (c && *c != stmt.getContext()) { match = false; } if (match) {
