Skip to content

Commit

Permalink
Merge pull request #147 from OpenTriply/add-offset-hdtSearch
Browse files Browse the repository at this point in the history
Add offset flag in hdtSearch
  • Loading branch information
LaurensRietveld authored Jan 5, 2018
2 parents 4dcf52b + a4378be commit 6f0bbf4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions libhdt/include/Iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ class IteratorTripleString {
}
virtual void goToStart() {
}
virtual bool canGoTo(){
return false;
}
virtual size_t estimatedNumResults() {
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion libhdt/src/hdt/TripleIDStringIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ TripleString *TripleIDStringIterator::previous() {
void TripleIDStringIterator::goToStart() {
iterator->goToStart();
}

bool TripleIDStringIterator::canGoTo() {
return iterator->canGoTo();
}
size_t TripleIDStringIterator::estimatedNumResults() {
return iterator->estimatedNumResults();
}
Expand Down
1 change: 1 addition & 0 deletions libhdt/src/hdt/TripleIDStringIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TripleIDStringIterator : public IteratorTripleString {
TripleString *next();
bool hasPrevious();
TripleString *previous();
bool canGoTo();
void goToStart();
size_t estimatedNumResults();
ResultEstimationType numResultEstimation();
Expand Down
9 changes: 7 additions & 2 deletions libhdt/src/triples/BitmapTriplesIterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,23 @@ void MiddleWaveletIterator::skip(unsigned int pos) {
//goTo(predicateOcurrence+pos);

int numJumps = 0;
unsigned int posLeft = pos;
while ((numJumps<pos)&&(posZ<maxZ)){
if((posZ+pos)>nextZ) {
if((posZ+posLeft)>nextZ) {
numJumps += (nextZ-posZ)+1; // count current jump
predicateOcurrence++; // jump to the next occurrence
posLeft = pos-numJumps; // set remaining offset
if (predicateOcurrence<=numOcurrences){
posY = predicateIndex->getAppearance(patY, predicateOcurrence);
posZ = prevZ = adjZ.find(posY);
nextZ = adjZ.last(posY);
}
else {
throw std::runtime_error("Cannot goTo on this pattern.");
}

} else {
posZ=(posZ+pos);
posZ=(posZ+posLeft);
numJumps=pos;
}
}
Expand Down
38 changes: 33 additions & 5 deletions libhdt/tools/hdtSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <getopt.h>
#include <string.h>
#include <string>
#include <cstdint>
#include <iostream>
#include <fstream>
#include "../src/util/StopWatch.hpp"
Expand All @@ -57,12 +58,13 @@ void help() {
cout << "\t-h\t\t\tThis help" << endl;
cout << "\t-q\t<query>\t\tLaunch query and exit." << endl;
cout << "\t-o\t<output>\tSave query output to file." << endl;
cout << "\t-f\t<offset>\tLimit the result list starting after the offset." << endl;
cout << "\t-m\t\t\tDo not show results, just measure query time." << endl;
cout << "\t-V\tPrints the HDT version number." << endl;
cout << "\t-V\t\t\tPrints the HDT version number." << endl;
//cout << "\t-v\tVerbose output" << endl;
}

void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
void iterate(HDT *hdt, char *query, ostream &out, bool measure, uint32_t offset) {
TripleString tripleString;
tripleString.read(query);

Expand All @@ -89,6 +91,26 @@ void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
IteratorTripleString *it = hdt->search(subj, pred, obj);

StopWatch st;

// Go to the right offset.
if(it->canGoTo()) {
try {
it->skip(offset);
offset = 0;
}
catch (const runtime_error error) {
/*invalid offset*/
interruptSignal = 1;
}
}
else {
while(offset && it->hasNext()) {
it->next();
offset--;
}
}

// Get results.
unsigned int numTriples=0;
while(it->hasNext() && interruptSignal==0) {
TripleString *ts = it->next();
Expand All @@ -109,9 +131,11 @@ void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
int main(int argc, char **argv) {
int c;
string query, inputFile, outputFile;
stringstream sstream;
uint32_t offset = 0;
bool measure = false;

while( (c = getopt(argc,argv,"hq:o:m:V"))!=-1) {
while( (c = getopt(argc,argv,"hq:o:f:mV"))!=-1) {
switch(c) {
case 'h':
help();
Expand All @@ -122,6 +146,10 @@ int main(int argc, char **argv) {
case 'o':
outputFile = optarg;
break;
case 'f':
sstream << optarg;
if(!(sstream >> offset)) offset=0;
break;
case 'm':
measure = true;
break;
Expand Down Expand Up @@ -160,7 +188,7 @@ int main(int argc, char **argv) {

if(query!="") {
// Supplied query, search and exit.
iterate(hdt, (char*)query.c_str(), *out, measure);
iterate(hdt, (char*)query.c_str(), *out, measure, offset);
} else {
// No supplied query, show terminal.
char line[1024*10];
Expand All @@ -179,7 +207,7 @@ int main(int argc, char **argv) {
continue;
}

iterate(hdt, line, *out, measure);
iterate(hdt, line, *out, measure, offset);

cerr << ">> ";
}
Expand Down

0 comments on commit 6f0bbf4

Please sign in to comment.