3838#include < getopt.h>
3939#include < string.h>
4040#include < string>
41+ #include < cstdint>
4142#include < iostream>
4243#include < fstream>
4344#include " ../src/util/StopWatch.hpp"
@@ -57,12 +58,13 @@ void help() {
5758 cout << " \t -h\t\t\t This help" << endl;
5859 cout << " \t -q\t <query>\t\t Launch query and exit." << endl;
5960 cout << " \t -o\t <output>\t Save query output to file." << endl;
61+ cout << " \t -f\t <offset>\t Limit the result list starting after the offset." << endl;
6062 cout << " \t -m\t\t\t Do not show results, just measure query time." << endl;
61- cout << " \t -V\t Prints the HDT version number." << endl;
63+ cout << " \t -V\t\t\ t Prints the HDT version number." << endl;
6264 // cout << "\t-v\tVerbose output" << endl;
6365}
6466
65- void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
67+ void iterate(HDT *hdt, char *query, ostream &out, bool measure, uint32_t offset ) {
6668 TripleString tripleString;
6769 tripleString.read (query);
6870
@@ -89,6 +91,26 @@ void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
8991 IteratorTripleString *it = hdt->search (subj, pred, obj);
9092
9193 StopWatch st;
94+
95+ // Go to the right offset.
96+ if (it->canGoTo ()) {
97+ try {
98+ it->skip (offset);
99+ offset = 0 ;
100+ }
101+ catch (const runtime_error error) {
102+ /* invalid offset*/
103+ interruptSignal = 1 ;
104+ }
105+ }
106+ else {
107+ while (offset && it->hasNext ()) {
108+ it->next ();
109+ offset--;
110+ }
111+ }
112+
113+ // Get results.
92114 unsigned int numTriples=0 ;
93115 while (it->hasNext () && interruptSignal==0 ) {
94116 TripleString *ts = it->next ();
@@ -109,9 +131,11 @@ void iterate(HDT *hdt, char *query, ostream &out, bool measure) {
109131int main (int argc, char **argv) {
110132 int c;
111133 string query, inputFile, outputFile;
134+ stringstream sstream;
135+ uint32_t offset = 0 ;
112136 bool measure = false ;
113137
114- while ( (c = getopt (argc,argv," hq:o:m:V " ))!=-1 ) {
138+ while ( (c = getopt (argc,argv," hq:o:f:mV " ))!=-1 ) {
115139 switch (c) {
116140 case ' h' :
117141 help ();
@@ -122,6 +146,10 @@ int main(int argc, char **argv) {
122146 case ' o' :
123147 outputFile = optarg;
124148 break ;
149+ case ' f' :
150+ sstream << optarg;
151+ if (!(sstream >> offset)) offset=0 ;
152+ break ;
125153 case ' m' :
126154 measure = true ;
127155 break ;
@@ -160,7 +188,7 @@ int main(int argc, char **argv) {
160188
161189 if (query!=" " ) {
162190 // Supplied query, search and exit.
163- iterate(hdt, (char *)query.c_str (), *out, measure);
191+ iterate(hdt, (char *)query.c_str (), *out, measure, offset );
164192 } else {
165193 // No supplied query, show terminal.
166194 char line[1024 *10 ];
@@ -179,7 +207,7 @@ int main(int argc, char **argv) {
179207 continue ;
180208 }
181209
182- iterate(hdt, line, *out, measure);
210+ iterate(hdt, line, *out, measure, offset );
183211
184212 cerr << " >> " ;
185213 }
0 commit comments