void iterate(HDT *hdt, char *query, ostream &out, bool measure) { TripleString tripleString; tripleString.read(query); const char *subj = tripleString.getSubject().c_str(); const char *pred = tripleString.getPredicate().c_str(); const char *obj = tripleString.getObject().c_str(); if(strcmp(subj, "?")==0) { subj=""; } if(strcmp(pred, "?")==0) { pred=""; } if(strcmp(obj, "?")==0) { obj=""; } #if 0 cout << "Subject: |" << subj <<"|"<< endl; cout << "Predicate: |" << pred <<"|"<< endl; cout << "Object: |" << obj << "|"<<endl; #endif try { IteratorTripleString *it = hdt->search(subj, pred, obj); StopWatch st; unsigned int numTriples=0; while(it->hasNext() && interruptSignal==0) { TripleString *ts = it->next(); if(!measure) out << *ts << endl; numTriples++; } cout << numTriples << " results in " << st << endl; delete it; interruptSignal=0; // Interrupt caught, enable again. } catch (char *e) { cerr << e << endl; } }
int main(int argc, char **argv) { int c; char *inputFile=NULL, *insertFile=NULL, *removeFile=NULL, *outputFile=NULL; char *insertSubject=NULL, *insertPredicate=NULL, *insertObject=NULL; char *removeSubject=NULL, *removePredicate=NULL, *removeObject=NULL; bool insertSingle = false; bool removeSingle = false; bool insertMultiple = false; bool removeMultiple = false; while ((c = getopt(argc, argv, "hO:i:r:I:R:")) != -1) { switch (c) { case 'h': help(); break; case 'O': outputFile = optarg; break; case 'i': insertSingle = true; insertSubject = optarg; insertPredicate = argv[optind++]; insertObject = argv[optind++]; break; case 'r': removeSingle = true; removeSubject = optarg; removePredicate = argv[optind++]; removeObject = argv[optind++]; break; case 'I': insertMultiple = true; insertFile = optarg; break; case 'R': removeMultiple = true; removeFile = optarg; break; default: cout << "ERROR: Unknown option" << endl; help(); return 1; } } if (argc - optind < 2) { cout << "ERROR: You must supply an input and output HDT File" << endl << endl; help(); return 1; } inputFile = argv[optind]; outputFile = argv[optind+1]; if (strcmp(inputFile,outputFile)==0){ cerr<< "ERROR: input and output files must me different" << endl <<endl; return 1; } try { // LOAD HDT *hdt = HDTManager::mapHDT(inputFile); // Replace header Header *head = hdt->getHeader(); if (insertSingle) { TripleString ti(insertSubject, insertPredicate, insertObject); head->insert(ti); } if (removeSingle) { TripleString ti(removeSubject, removePredicate, removeObject); head->remove(ti); } if (insertMultiple) { string line; std::ifstream infile(insertFile); while (getline(infile, line)) { TripleString ti; ti.read(line); head->insert(ti); } } if (removeMultiple) { string line; std::ifstream infile(removeFile); while (getline(infile, line)) { TripleString ti; ti.read(line); head->remove(ti); } } // SAVE hdt->saveToHDT(outputFile); delete hdt; } catch (std::exception& e) { cerr << "ERROR: " << e.what() << endl; return 1; } }