Initial import into git.
[galago.git] / cpp / galago / appsrc / galagod.cpp
blob327fcfc2a35ebad3f5fa05c9a0e827c6356d44dc
2 //
3 // galagod
4 //
5 // 27 February 2007 -- tds
6 //
8 #define GALAGOD_DEFAULT_QUERY_RESULTS (20)
9 #define GALAGOD_MAXIMUM_QUERY_RESULTS (1<<20)
12 // print_xml_results
15 static void print_xml_results( const std::vector<indri::api::ScoredExtentResult>& results,
16 Retrieval& retrieval,
17 FCGX_Request& request )
19 FCGX_FPrintF( request.out, "[\n" );
21 for( int i=0; i<results.size(); i++ ) {
22 std::string document = retrieval.getDocument(results[i].document);
23 bool more = (results.size() - i > 1);
25 FCGX_FPrintF( request.out, "\t{ \"score\" : %f, \"document\" : %s }%s\n", results[i].score,
26 document.c_str(),
27 more ? "," : "" );
30 FCGX_FPrintF( request.out, "]\n" );
34 // print_json_results
35 //
37 static void print_json_results( const std::vector<indri::api::ScoredExtentResult>& results,
38 Retrieval& retrieval,
39 FCGX_Request& request )
41 FCGX_FPrintF( request.out, "<results>\n" );
43 for( int i=0; i<results.size(); i++ ) {
44 std::string document = retrieval.getDocument(results[i].document);
46 FCGX_FPrintF( request.out, "\t<result>\n" );
47 FCGX_FPrintF( request.out, "\t\t<score>%f</score>\n", results[i].score );
48 FCGX_FPrintF( request.out, "\t\t<document>%s</document>\n", document.c_str() );
49 FCGX_FPrintF( request.out, "\t</result>\n" );
52 FCGX_FPrintF( request.out, "</results>\n" );
56 // unescape_string
59 static std::string unescape_string( const std::string& input ) {
60 std::stringstream output;
62 for( int i=0; i<input.size(); i++ ) {
63 if( input[i] == '+' ) {
64 output << ' ';
65 } else if( input[i] == '%' ) {
66 output << unescape_hex( input, i+1 );
67 i += 2;
68 } else {
69 output << input[i];
73 return output.str();
77 // parse_query_string
80 static std::map<std::string, std::string> parse_query_string( const std::string& input ) {
81 int location = 0;
82 std::map<std::string, std::string> result;
84 while (true) {
85 std::string::size_type equals = input.find( '=', location );
86 std::string::size_type ampersand = input.find( '&', equals );
88 if( equals == std::string::npos )
89 break;
91 std::string key = input.substr( location, equals-location-1 );
92 std::string value;
94 if( ampersand != std::string::npos ) {
95 value = input.substr( equals+1 );
96 } else {
97 value = input.substr( equals+1, ampersand-equals-1 );
100 value = unescape_string( value );
101 result[key] = value;
103 if( ampersand == std::string::npos )
104 break;
107 return result;
111 // fcgi_daemon
114 static void fcgi_daemon( Retrieval& retrieval, indri::api::Parameters& parameters ) {
115 FCGX_Request request;
116 FCGX_InitRequest(&request, 0, 0);
118 while (true) {
119 int rc = FCGX_Accept_r( &request );
121 if( rc < 0 )
122 break;
124 const char* requestString = FCGX_GetParam( "QUERY_STRING", request.envp );
126 // parse the query string
127 std::map<std::string, std::string> parsed = parse_query_string( requestString );
129 // get the count parameter
130 int count = GALAGOD_DEFAULT_QUERY_RESULTS;
132 if( parsed.find( "count" ) != parsed.end() ) {
133 count = string_to_i64( parsed["count"] );
134 count = std::min( count, GALAGOD_MAXIMUM_QUERY_RESULTS;
137 // get the query
138 std::string queryText;
140 if( parsed.find( "query" ) != parsed.end() ) {
141 queryText = parsed["query"];
144 // run the query
145 std::vector<indri::api::ScoredExtentResult> results;
146 results = retrieval.runQuery( queryText, requested );
148 // print the results
149 if( parsed.find( "json" ) != parsed.end() ) {
150 print_json_results( results, retrieval, request );
151 } else {
152 print_xml_results( results, retrieval, request );
154 FCGX_Finish_r(&request);
159 // main
162 int main( int argc, char** argv ) {
163 try {
164 // read in parameters
165 indri::api::Parameters::instance().loadCommandLine( argc, argv );
166 indri::api::Parameters& parameters = indri::api::Parameters::instance();
168 // open index
169 Retrieval retrieval;
170 std::string indexPath = parameters["index"];
171 retrieval.openRead( indexPath );
173 fcgi_daemon( retrieval, parameters );
174 } catch( lemur::api::Exception& e ) {
175 LEMUR_ABORT(e);
178 return 0;