From f961573843b1b5283f32b1cbadac26d3421684bd Mon Sep 17 00:00:00 2001 From: Nilesh Chakraborty Date: Sun, 16 Jun 2019 20:58:59 +0200 Subject: [PATCH] Can show data loading progress, can block cout/cerr debug messages from cpp library --- include/hdt_document.hpp | 9 ++++++--- src/hdt.cpp | 6 ++++-- src/hdt_document.cpp | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/include/hdt_document.hpp b/include/hdt_document.hpp index 3d61822..de41227 100644 --- a/include/hdt_document.hpp +++ b/include/hdt_document.hpp @@ -31,8 +31,9 @@ class HDTDocument { private: std::string hdt_file; hdt::HDT *hdt; + bool debug; hdt::QueryProcessor *processor; - HDTDocument(std::string file, bool map, bool indexed); + HDTDocument(std::string file, bool map, bool indexed, bool progress, bool debug); public: /*! @@ -87,9 +88,11 @@ class HDTDocument { * @param file - Path to the HDT file * @param map - True maps the HDT file (faster), False loads everything in memory * @param indexed - True if the HDT must be loaded with indexes, False otherwise + * @param progress - True to show progress on stdout, False otherwise + * @param debug - True to show cout debug messages from hdt-cpp, False otherwise */ - static HDTDocument create(std::string file, bool map, bool indexed) { - return HDTDocument(file, map, indexed); + static HDTDocument create(std::string file, bool map, bool indexed, bool progress, bool debug) { + return HDTDocument(file, map, indexed, progress, debug); } /*! diff --git a/src/hdt.cpp b/src/hdt.cpp index cc044e0..b6336fa 100644 --- a/src/hdt.cpp +++ b/src/hdt.cpp @@ -80,8 +80,10 @@ PYBIND11_MODULE(hdt, m) { py::class_(m, "HDTDocument", HDT_DOCUMENT_CLASS_DOC) .def(py::init(&HDTDocument::create), py::arg("file"), - py::arg("map") = false, - py::arg("indexed") = true) + py::arg("map") = true, + py::arg("indexed") = true, + py::arg("progress") = false, + py::arg("debug") = false) .def_property_readonly("file_path", &HDTDocument::getFilePath, HDT_DOCUMENT_GETFILEPATH_DOC) .def_property_readonly("total_triples", &HDTDocument::getNbTriples, diff --git a/src/hdt_document.cpp b/src/hdt_document.cpp index 89ef947..546effd 100644 --- a/src/hdt_document.cpp +++ b/src/hdt_document.cpp @@ -52,21 +52,27 @@ inline bool file_exists(const std::string &name) { * @param file - Path to HDT file to load * @param map - True maps the HDT file (faster), False loads everything in memory * @param indexed - True if the HDT must be loaded with indexes, False otherwise + * @param progress - True to show loading progress on stdout, False otherwise + * @param debug - True to show debug messages from hdt-cpp, False otherwise */ -HDTDocument::HDTDocument(std::string file, bool map, bool indexed) { +HDTDocument::HDTDocument(std::string file, bool map, bool indexed, bool progress, bool debug) + :debug(debug) + { hdt_file = file; if (!file_exists(file)) { throw std::runtime_error("Cannot open HDT file '" + file + "': Not Found!"); } + StdoutProgressListener* listener = progress ? nullptr : new StdoutProgressListener(); + if(!map && indexed) { - hdt = HDTManager::loadIndexedHDT(file.c_str()); + hdt = HDTManager::loadIndexedHDT(file.c_str(), listener); } else if(!map && !indexed) { - hdt = HDTManager::loadHDT(file.c_str()); + hdt = HDTManager::loadHDT(file.c_str(), listener); } else if(map && indexed){ - hdt = HDTManager::mapIndexedHDT(file.c_str()); + hdt = HDTManager::mapIndexedHDT(file.c_str(), listener); } else { - hdt = HDTManager::mapHDT(file.c_str()); + hdt = HDTManager::mapHDT(file.c_str(), listener); } processor = new QueryProcessor(hdt); } @@ -288,6 +294,19 @@ unsigned int HDTDocument::convertTerm(std::string term, IdentifierPosition pos) * @return A JoinIterator* used to evaluated the join. */ JoinIterator * HDTDocument::searchJoin(std::vector patterns) { + std::streambuf *cout_original = nullptr; + std::streambuf *cerr_original = nullptr; + std::ofstream *null_out = nullptr; + + if(!debug) { + cout_original = std::cout.rdbuf(); + cerr_original = std::cerr.rdbuf(); + + null_out = new std::ofstream("/dev/null"); + std::cout.rdbuf(null_out->rdbuf()); // redirect 'cout' to /dev/null + std::cerr.rdbuf(null_out->rdbuf()); // redirect 'cerr' to /dev/null + } + set vars {}; vector joinPatterns {}; std::string subj, pred, obj; @@ -311,5 +330,12 @@ JoinIterator * HDTDocument::searchJoin(std::vector patterns) { } VarBindingString *iterator = processor->searchJoin(joinPatterns, vars); + + if(!debug) { + std::cout.rdbuf(cout_original); + std::cerr.rdbuf(cerr_original); + null_out->close(); + } + return new JoinIterator(iterator); }