Skip to content

Commit

Permalink
Can show data loading progress, can block cout/cerr debug messages fr…
Browse files Browse the repository at this point in the history
…om cpp library
  • Loading branch information
nilesh-c committed Jun 16, 2019
1 parent dfd82f0 commit f961573
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
9 changes: 6 additions & 3 deletions include/hdt_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/*!
Expand Down Expand Up @@ -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);
}

/*!
Expand Down
6 changes: 4 additions & 2 deletions src/hdt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ PYBIND11_MODULE(hdt, m) {

py::class_<HDTDocument>(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,
Expand Down
36 changes: 31 additions & 5 deletions src/hdt_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<triple> 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<string> vars {};
vector<TripleString> joinPatterns {};
std::string subj, pred, obj;
Expand All @@ -311,5 +330,12 @@ JoinIterator * HDTDocument::searchJoin(std::vector<triple> 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);
}

0 comments on commit f961573

Please sign in to comment.