CurlHandle::CurlHandle(CURL* handle) : mException(NULL) { memset(mErrorBuffer, 0, CURL_ERROR_SIZE + 1); mCurl = handle; runtimeAssert("Error when trying to curl_easy_init() a handle", mCurl != NULL); errorBuffer(mErrorBuffer); }
cURLpp::CurlHandle::CurlHandle() : mWriteFunctor(NULL) , mHeaderFunctor(NULL) , mReadFunctor(NULL) , mProgressFunctor(NULL) , mDebugFunctor(NULL) , mSslFunctor(NULL) ,mException(NULL) { mCurl = curl_easy_init(); runtimeAssert( "Error when trying to curl_easy_init() a handle", mCurl ); errorBuffer( mErrorBuffer ); }
//--------------------------------------------- // CAOActiveScheduler::Error() //--------------------------------------------- void CAOActiveScheduler::Error( TInt anError ) const { KAOMANAGER_LOGGER_WRITE_FORMAT("CAOActiveScheduler::Error() ERROR IN AO ACTIVE SCHEDULER error code: ", anError ); TPckgBuf<TInt> errorBuffer( anError ); //check that error is short enough value for //descriptor TInt error = KErrNone; // make sure thet AoServer pointer is set if ( iAoServer ) { TRAP(error, iAoServer->HandleClientCommandL( EServerAPIBaseCommandAoSchedulerError , errorBuffer ); ); }
bool CPixelShader::CreateFromDiskFile(const char* c_szFileName) { Destroy(); LPD3DXBUFFER lpd3dxShaderBuffer; LPD3DXBUFFER lpd3dxErrorBuffer; if (FAILED( D3DXAssembleShaderFromFile(c_szFileName, 0, NULL, &lpd3dxShaderBuffer, &lpd3dxErrorBuffer) )) return false; CDirect3DXBuffer shaderBuffer(lpd3dxShaderBuffer); CDirect3DXBuffer errorBuffer(lpd3dxErrorBuffer); if (FAILED(ms_lpd3dDevice->CreatePixelShader((DWORD*)shaderBuffer.GetPointer(), &m_handle))) return false; return true; }
int main( int argc, const char* argv[]) { int rt = 0; std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } strus::ProgramOptions opt; bool printUsageAndExit = false; try { opt = strus::ProgramOptions( argc, argv, 11, "h,help", "v,version", "license", "t,tokenizer:", "n,normalizer:", "m,module:", "M,moduledir:", "q,quot:", "p,plain", "R,resourcedir:", "T,trace:"); if (opt( "help")) printUsageAndExit = true; std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw strus::runtime_error(_TXT("failed to create module loader")); if (opt("moduledir")) { std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cerr << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus analyzer version ") << STRUS_ANALYZER_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 1) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } if (opt.nofargs() < 1) { std::cerr << _TXT("too few arguments") << std::endl; printUsageAndExit = true; rt = 2; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusAnalyze [options] <phrasepath>" << std::endl; std::cout << "<phrasepath> = " << _TXT("path to phrase to analyze ('-' for stdin)") << std::endl; std::cout << "description: " << _TXT("tokenizes and normalizes a text segment") << std::endl; std::cout << " " << _TXT("and prints the result to stdout.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-R|--resourcedir <DIR>" << std::endl; std::cout << " " << _TXT("Search resource files for analyzer first in <DIR>") << std::endl; std::cout << "-t|--tokenizer <CALL>" << std::endl; std::cout << " " << _TXT("Use the tokenizer <CALL> (default 'content')") << std::endl; std::cout << "-n|--normalizer <CALL>" << std::endl; std::cout << " " << _TXT("Use the normalizer <CALL> (default 'orig')") << std::endl; std::cout << "-q|--quot <STR>" << std::endl; std::cout << " " << _TXT("Use the string <STR> as quote for the result (default \"\'\")") << std::endl; std::cout << "-p|--plain" << std::endl; std::cout << " " << _TXT("Do not print position and define default quotes as empty") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } std::string resultQuot = "'"; bool resultPlain = false; if (opt( "plain")) { resultPlain = true; resultQuot.clear(); } if (opt( "quot")) { resultQuot = opt[ "quot"]; } std::string docpath = opt[0]; std::string tokenizer( "content"); if (opt( "tokenizer")) { tokenizer = opt[ "tokenizer"]; } std::string normalizer( "orig"); if (opt( "normalizer")) { normalizer = opt[ "normalizer"]; } // Set paths for locating resources: if (opt("resourcedir")) { std::vector<std::string> pathlist( opt.list("resourcedir")); std::vector<std::string>::const_iterator pi = pathlist.begin(), pe = pathlist.end(); for (; pi != pe; ++pi) { moduleLoader->addResourcePath( *pi); } } // Create root object for analyzer: std::auto_ptr<strus::AnalyzerObjectBuilderInterface> analyzerBuilder( moduleLoader->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw strus::runtime_error(_TXT("failed to create analyzer object builder")); // Create proxy objects if tracing enabled: { std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::AnalyzerObjectBuilderInterface* proxy = (*ti)->createProxy( analyzerBuilder.get()); analyzerBuilder.release(); analyzerBuilder.reset( proxy); } } // Create objects for analyzer: std::auto_ptr<strus::QueryAnalyzerInterface> analyzer( analyzerBuilder->createQueryAnalyzer()); if (!analyzer.get()) throw strus::runtime_error(_TXT("failed to create analyzer")); const strus::TextProcessorInterface* textproc = analyzerBuilder->getTextProcessor(); if (!textproc) throw strus::runtime_error(_TXT("failed to get text processor")); // Create phrase type (tokenizer and normalizer): std::string phraseType; if (!strus::loadQueryAnalyzerPhraseType( *analyzer, textproc, phraseType, "", normalizer, tokenizer, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load analyze phrase type")); } // Load the phrase: std::string phrase; if (docpath == "-") { unsigned int ec = strus::readStdin( phrase); if (ec) throw strus::runtime_error( _TXT( "error reading input from stdin (errno %u)"), ec); } else { unsigned int ec = strus::readFile( docpath, phrase); if (ec) throw strus::runtime_error( _TXT( "error reading input file '%s' (errno %u)"), docpath.c_str(), ec); } // Analyze the phrase and print the result: std::vector<strus::analyzer::Term> terms = analyzer->analyzePhrase( phraseType, phrase); std::sort( terms.begin(), terms.end(), TermOrder()); std::vector<strus::analyzer::Term>::const_iterator ti = terms.begin(), te = terms.end(); for (; ti != te; ++ti) { if (!resultPlain) { std::cout << ti->pos() << " "; } std::cout << resultQuot << ti->value() << resultQuot << std::endl; } if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("error in analyze phrase")); } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } return -1; }
void CGameServerState::dataPacketReceived(Net::CPaquete* packet) { // Obtenemos la id de la conexion por la que hemos recibido // el paquete (para identificar al cliente) Net::NetID playerNetId = packet->getConexion()->getId(); // Construimos un buffer para leer los datos que hemos recibido Net::CBuffer inBuffer( packet->getDataLength() ); inBuffer.write( packet->getData(), packet->getDataLength() ); inBuffer.reset(); // Desplazamos el puntero al principio para realizar la lectura // En primer lugar extraemos el tipo del mensaje que hemos recibido Net::NetMessageType msgType; inBuffer.read( &msgType, sizeof(msgType) ); switch(msgType) { case Net::PLAYER_INFO: { // Deserializamos el nombre del player std::string playerNick; inBuffer.deserialize(playerNick); //comprobamos si hay algún jugador con el mismo nombre en la partida, y si es así, //se lo cambiamos para que no haya problemas en el futuro if(_map->getEntityByName(playerNick)){ std::ostringstream convert; convert << playerNetId; std::string nameId = convert.str(); playerNick += nameId; } // Registramos al player en el gestor de jugadores _playersMgr->addPlayer(playerNetId, playerNick); // Enviamos la informacion de carga de mapa al cliente sendMapInfo(playerNetId); break; } case Net::MAP_LOADED: { Net::CBuffer gameSettingsBuffer; Net::NetMessageType msgType = Net::GAME_SETTINGS; gameSettingsBuffer.write(&msgType, sizeof(msgType)); gameSettingsBuffer.write(&_gameMode, sizeof(_gameMode)); gameSettingsBuffer.write(&_gameTime, sizeof(_gameTime)); gameSettingsBuffer.write(&_goalScore, sizeof(_goalScore)); _netMgr->sendTo( playerNetId, gameSettingsBuffer.getbuffer(), gameSettingsBuffer.getSize() ); break; } case Net::GAME_SETTINGS_LOADED: { // Una vez cargado el mapa, comienza la fase de carga de players. // El player que esta siendo conectado deberá cargar a todos los players que ya estaban online. sendWorldState(playerNetId); break; } case Net::WORLD_STATE_LOADED: { Net::NetMessageType startGameMsg = Net::START_GAME; _netMgr->sendTo(playerNetId, &startGameMsg, sizeof(startGameMsg)); break; } case Net::CLASS_SELECTED: { int race; inBuffer.deserialize(race); if(race == 5) { unsigned int nbSpectators = 0; for(auto it = _playersMgr->begin(); it != _playersMgr->end(); ++it) { std::pair<Logic::TEntityID, bool> id = _playersMgr->getPlayerId(playerNetId); if(id.second) { if(Logic::CServer::getSingletonPtr()->getMap()->getEntityByID(id.first)->getType() == "Spectator") ++nbSpectators; } } if(nbSpectators < _maxSpectators) { // Creamos una entidad espectador y la replicamos en el cliente createAndMirrorSpectator(playerNetId); } else { // Mandamos un mensaje de que no existen slots disponibles // para jugar Net::NetMessageType noSlotsMsg = Net::NO_FREE_SPECTATOR_SLOTS; Net::CBuffer errorBuffer( sizeof(noSlotsMsg) ); errorBuffer.write( &noSlotsMsg, sizeof(noSlotsMsg) ); _netMgr->sendTo(playerNetId, errorBuffer.getbuffer(), errorBuffer.getSize()); } } else { // Si quedan slots para conectarse como jugador permitimos que // se conecte, sino le mandamos un mensaje bool isChanging = false; if( _playersMgr->existsByNetId(playerNetId) ) { isChanging = _playersMgr->getPlayer(playerNetId).isSpawned(); } // Si estamos cambiando de clase, consideramos un jugador menos, ya que // nosotros si no contariamos unsigned nbPlayersPlaying = _playersMgr->getNumberOfPlayersSpawned(); if(isChanging) --nbPlayersPlaying; if( nbPlayersPlaying < _maxPlayers) { Logic::TeamFaction::Enum team; if(_autoBalanceTeams) { if(_playersMgr->blueTeamPlayers() < _playersMgr->redTeamPlayers()) { team = Logic::TeamFaction::eBLUE_TEAM; } else { team = Logic::TeamFaction::eRED_TEAM; } } else { if(_gameMode == GameMode::eTEAM_DEATHMATCH || _gameMode == GameMode::eCAPTURE_THE_FLAG) { inBuffer.read(&team, sizeof(team)); } else { team = Logic::TeamFaction::eNONE; } } // Creamos una entidad jugador con la clase que nos hayan dicho // y la replicamos en el cliente createAndMirrorPlayer(race, playerNetId, team); } else { // Mandamos un mensaje de que no existen slots disponibles // para jugar Net::NetMessageType noSlotsMsg = Net::NO_FREE_PLAYER_SLOTS; Net::CBuffer errorBuffer( sizeof(noSlotsMsg) ); errorBuffer.write( &noSlotsMsg, sizeof(noSlotsMsg) ); _netMgr->sendTo(playerNetId, errorBuffer.getbuffer(), errorBuffer.getSize()); } } break; } case Net::PING: { Net::NetMessageType ackMsg = Net::PING; clock_t time = clock(); Net::CBuffer ackBuffer(sizeof(ackMsg) + sizeof(time)); ackBuffer.write(&ackMsg, sizeof(ackMsg)); ackBuffer.write(&time, sizeof(time)); _netMgr->sendTo(playerNetId, ackBuffer.getbuffer(), ackBuffer.getSize()); break; } case Net::LOCAL_PLAYER_LOADED: { Logic::TEntityID id; inBuffer.deserialize(id); Logic::CEntity* player = _map->getEntityByID(id); player->activate(); player->start(); break; } } } // dataPacketReceived
int main( int argc, const char* argv[]) { std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } g_errorBuffer = errorBuffer.get(); bool doExit = false; int argi = 1; std::string logfile; std::vector<std::string> moduledirs; std::vector<std::string> modules; std::vector<std::string> resourcedirs; std::vector<std::string> tracecfglist; std::string storageconfig; bool doCreateIfNotExist = false; unsigned int nofThreads = 0; unsigned int port = 7181; //... default port try { // Parsing arguments: for (; argi < argc; ++argi) { if (0==std::strcmp( argv[argi], "-h") || 0==std::strcmp( argv[argi], "--help")) { printUsage(); doExit = true; } else if (0==std::strcmp( argv[argi], "-v") || 0==std::strcmp( argv[argi], "--version")) { std::cerr << "strusRpc version " << STRUS_RPC_VERSION_STRING << std::endl; doExit = true; } else if (0==std::strcmp( argv[argi], "-m") || 0==std::strcmp( argv[argi], "--module")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--module"); modules.push_back( argv[argi]); } else if (0==std::strcmp( argv[argi], "-M") || 0==std::strcmp( argv[argi], "--moduledir")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--moduledir"); moduledirs.push_back( argv[argi]); } else if (0==std::strcmp( argv[argi], "-R") || 0==std::strcmp( argv[argi], "--resourcedir")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--resourcedir"); resourcedirs.push_back( argv[argi]); } else if (0==std::strcmp( argv[argi], "-p") || 0==std::strcmp( argv[argi], "--port")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--port"); try { port = strus::utils::touint( argv[argi]); if (port == 0 || port > 65535) { throw strus::runtime_error( _TXT("value out of range")); } } catch (const std::runtime_error& err) { throw strus::runtime_error( _TXT("illegal argument for option %s: %s"), "--port", err.what()); } } else if (0==std::strcmp( argv[argi], "-s") || 0==std::strcmp( argv[argi], "--storage")) { if (!storageconfig.empty()) throw strus::runtime_error( _TXT("option %s or %s specified twice"), "--storage","--configfile"); ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--storage"); storageconfig.append( argv[argi]); if (storageconfig.empty()) throw strus::runtime_error(_TXT("option %s with empty argument"), "--storage"); } else if (0==std::strcmp( argv[argi], "-S") || 0==std::strcmp( argv[argi], "--configfile")) { if (!storageconfig.empty()) throw strus::runtime_error( _TXT("option %s or %s specified twice"), "--storage","--configfile"); ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--configfile"); int ec = strus::readFile( argv[argi], storageconfig); if (ec) { throw strus::runtime_error(_TXT("failed to read configuration file %s (file system error %u)"), argv[argi], ec); } std::string::iterator di = storageconfig.begin(), de = storageconfig.end(); for (; di != de; ++di) { if ((unsigned char)*di < 32) *di = ' '; } if (storageconfig.empty()) throw strus::runtime_error(_TXT("option %s with empty file"), "--configfile"); } else if (0==std::strcmp( argv[argi], "-c") || 0==std::strcmp( argv[argi], "--create")) { doCreateIfNotExist = true; } else if (0==std::strcmp( argv[argi], "-T") || 0==std::strcmp( argv[argi], "--trace")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument (trace configuration)"), "--trace"); tracecfglist.push_back( argv[argi]); } else if (0==std::strcmp( argv[argi], "-t") || 0==std::strcmp( argv[argi], "--threads")) { ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects number as argument"), "--threads"); try { nofThreads = strus::utils::touint( argv[argi]); } catch (const std::runtime_error& err) { throw strus::runtime_error(_TXT("illegal argument for option %s: %s"),"--threads", err.what()); } } else if (0==std::strcmp( argv[argi], "-l") || 0==std::strcmp( argv[argi], "--logfile")) { if (logfile.size()) throw strus::runtime_error(_TXT("duplicate definition of option %s"), "--logfile"); ++argi; if (argi == argc) throw strus::runtime_error(_TXT("option %s expects argument"), "--logfile"); logfile = argv[argi]; if (logfile.empty()) throw strus::runtime_error(_TXT("empty definition of option %s"), "--logfile"); } else if (argv[argi][0] == '-') { throw strus::runtime_error(_TXT("unknown option %s"), argv[argi]); } else { throw strus::runtime_error( _TXT("no arguments expected (only options)")); } } if (doExit) return 0; init_global_context( logfile.empty()?0:logfile.c_str()); g_errorBuffer->setLogFile( g_glbctx.logf); g_errorBuffer->setMaxNofThreads( strus_threadpool_size()+2); // Create the global context: std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( g_errorBuffer)); g_moduleLoader = moduleLoader.get(); if (!g_moduleLoader) throw strus::runtime_error( _TXT("failed to create module loader")); std::vector<std::string>::const_iterator di = moduledirs.begin(), de = moduledirs.end(); for (; di != de; ++di) { g_moduleLoader->addModulePath( *di); } moduleLoader->addSystemModulePath(); std::vector<std::string>::const_iterator mi = modules.begin(), me = modules.end(); for (; mi != me; ++mi) { g_moduleLoader->loadModule( *mi); } std::vector<std::string>::const_iterator pi = resourcedirs.begin(), pe = resourcedirs.end(); for (; pi != pe; ++pi) { g_moduleLoader->addResourcePath( *pi); } if (g_errorBuffer->hasError()) { throw strus::runtime_error(_TXT("error in initialization: %s"), g_errorBuffer->fetchError()); } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (!tracecfglist.empty()) { std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( g_moduleLoader, *ti, g_errorBuffer)); } } // Create objects: std::auto_ptr<strus::StorageObjectBuilderInterface> storageBuilder( g_moduleLoader->createStorageObjectBuilder()); if (!storageBuilder.get()) { throw strus::runtime_error( _TXT("failed to create storage builder")); } std::auto_ptr<strus::AnalyzerObjectBuilderInterface> analyzerBuilder( g_moduleLoader->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) { throw strus::runtime_error( _TXT("failed to create analyzer builder")); } // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::AnalyzerObjectBuilderInterface* aproxy = (*ti)->createProxy( analyzerBuilder.get()); analyzerBuilder.release(); analyzerBuilder.reset( aproxy); strus::StorageObjectBuilderInterface* sproxy = (*ti)->createProxy( storageBuilder.get()); storageBuilder.release(); storageBuilder.reset( sproxy); } std::auto_ptr<strus::StorageClientInterface> storageClient; g_storageObjectBuilder = storageBuilder.get(); g_analyzerObjectBuilder = analyzerBuilder.get(); if (!storageconfig.empty()) { if (doCreateIfNotExist) { createStorageIfNotExist( storageconfig); } storageClient.reset( strus::createStorageClient( g_storageObjectBuilder, g_errorBuffer, storageconfig)); if (!storageClient.get()) { throw strus::runtime_error( _TXT("failed to create storage client"), storageconfig.c_str()); } std::cerr << _TXT("strus RPC server is hosting storage ") << "'" << storageconfig << "'" << std::endl; g_storageClient = storageClient.get(); } // Start server: std::cerr << "strus RPC server listening on port " << port << std::endl; int err = strus_run_server( (unsigned short)(unsigned int)port, nofThreads, &g_glbctx); if (err) { throw strus::runtime_error( _TXT("server terminated with error (see logs)")); } std::cerr << _TXT("strus RPC server terminated") << std::endl; // Cleanup when done: done_global_context(); return 0; } catch (const std::exception& e) { const char* errormsg = g_errorBuffer?g_errorBuffer->fetchError():0; if (errormsg) { std::cerr << e.what() << ": " << errormsg << std::endl; } else { std::cerr << e.what() << std::endl; } } std::cerr << _TXT("strus RPC server terminated") << std::endl; done_global_context(); return -1; }
int main( int argc, const char* argv[]) { int rt = 0; strus::DebugTraceInterface* dbgtrace = strus::createDebugTrace_standard( 2); if (!dbgtrace) { std::cerr << _TXT("failed to create debug trace") << std::endl; return -1; } strus::local_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2, dbgtrace/*passed with ownership*/)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } try { bool printUsageAndExit = false; strus::ProgramOptions opt( errorBuffer.get(), argc, argv, 13, "h,help", "v,version", "license", "G,debug:", "t,tokenizer:", "n,normalizer:", "m,module:", "M,moduledir:", "q,quot:", "P,plain", "F,fileinput", "R,resourcedir:", "T,trace:"); if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("failed to parse program arguments")); } if (opt( "help")) printUsageAndExit = true; // Enable debugging selected with option 'debug': { std::vector<std::string> dbglist = opt.list( "debug"); std::vector<std::string>::const_iterator gi = dbglist.begin(), ge = dbglist.end(); for (; gi != ge; ++gi) { if (!dbgtrace->enable( *gi)) { throw strus::runtime_error(_TXT("failed to enable debug '%s'"), gi->c_str()); } } } strus::local_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw std::runtime_error( _TXT("failed to create module loader")); if (opt("moduledir")) { std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cout << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus analyzer version ") << STRUS_ANALYZER_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 1) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } if (opt.nofargs() < 1) { std::cerr << _TXT("too few arguments") << std::endl; printUsageAndExit = true; rt = 2; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusAnalyze [options] <phrase>" << std::endl; std::cout << "<phrase> = " << _TXT("path to phrase to analyze") << std::endl; std::cout << " " << _TXT("file or '-' for stdin if option -F is specified)") << std::endl; std::cout << "description: " << _TXT("tokenizes and normalizes a text segment") << std::endl; std::cout << " " << _TXT("and prints the result to stdout.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-G|--debug <COMP>" << std::endl; std::cout << " " << _TXT("Issue debug messages for component <COMP> to stderr") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-R|--resourcedir <DIR>" << std::endl; std::cout << " " << _TXT("Search resource files for analyzer first in <DIR>") << std::endl; std::cout << "-t|--tokenizer <CALL>" << std::endl; std::cout << " " << _TXT("Use the tokenizer <CALL> (default 'content')") << std::endl; std::cout << "-n|--normalizer <CALL>" << std::endl; std::cout << " " << _TXT("Use the normalizer <CALL> (default 'orig')") << std::endl; std::cout << "-q|--quot <STR>" << std::endl; std::cout << " " << _TXT("Use the string <STR> as quote for the result (default \"\'\")") << std::endl; std::cout << "-P|--plain" << std::endl; std::cout << " " << _TXT("Print results without quotes and without an end of line for each result") << std::endl; std::cout << "-F|--fileinput" << std::endl; std::cout << " " << _TXT("Interpret phrase argument as a file name containing the input") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } std::string resultQuot = "'"; bool resultPlain = false; if (opt( "plain")) { resultPlain = true; resultQuot.clear(); } if (opt( "quot")) { resultQuot = opt[ "quot"]; } std::string phrasestring = opt[0]; std::string tokenizer( "content"); if (opt( "tokenizer")) { tokenizer = opt[ "tokenizer"]; } std::string normalizer( "orig"); if (opt( "normalizer")) { normalizer = opt[ "normalizer"]; } // Set paths for locating resources: if (opt("resourcedir")) { std::vector<std::string> pathlist( opt.list("resourcedir")); std::vector<std::string>::const_iterator pi = pathlist.begin(), pe = pathlist.end(); for (; pi != pe; ++pi) { moduleLoader->addResourcePath( *pi); } } // Create objects for analyzer: strus::local_ptr<strus::RpcClientMessagingInterface> messaging; strus::local_ptr<strus::RpcClientInterface> rpcClient; strus::local_ptr<strus::AnalyzerObjectBuilderInterface> analyzerBuilder; if (opt("rpc")) { messaging.reset( strus::createRpcClientMessaging( opt[ "rpc"], errorBuffer.get())); if (!messaging.get()) throw std::runtime_error( _TXT("failed to create rpc client messaging")); rpcClient.reset( strus::createRpcClient( messaging.get(), errorBuffer.get())); if (!rpcClient.get()) throw std::runtime_error( _TXT("failed to create rpc client")); (void)messaging.release(); analyzerBuilder.reset( rpcClient->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw std::runtime_error( _TXT("failed to create rpc analyzer object builder")); } else { analyzerBuilder.reset( moduleLoader->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw std::runtime_error( _TXT("failed to create analyzer object builder")); } // Create proxy objects if tracing enabled: { std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::AnalyzerObjectBuilderInterface* proxy = (*ti)->createProxy( analyzerBuilder.get()); analyzerBuilder.release(); analyzerBuilder.reset( proxy); } } if (errorBuffer->hasError()) { throw std::runtime_error( _TXT("error in initialization")); } // Create objects for analyzer: strus::local_ptr<strus::QueryAnalyzerInstanceInterface> analyzer( analyzerBuilder->createQueryAnalyzer()); if (!analyzer.get()) throw std::runtime_error( _TXT("failed to create analyzer")); const strus::TextProcessorInterface* textproc = analyzerBuilder->getTextProcessor(); if (!textproc) throw std::runtime_error( _TXT("failed to get text processor")); std::string analyzerConfig = strus::string_format( "[Element]\nfeature = %s %s text", normalizer.c_str(), tokenizer.c_str()); // Create phrase type (tokenizer and normalizer): if (!strus::load_QueryAnalyzer_program_std( analyzer.get(), textproc, analyzerConfig, errorBuffer.get())) { throw strus::runtime_error( _TXT("failed to load query analyzer: %s"), errorBuffer->fetchError()); } // Load the phrase: bool queryIsFile = opt("fileinput"); if (queryIsFile) { int ec; std::string ps; if (phrasestring == "-") { ec = strus::readStdin( ps); if (ec) throw strus::runtime_error( _TXT("failed to read query from stdin (errno %u)"), ec); } else { ec = strus::readFile( phrasestring, ps); if (ec) throw strus::runtime_error(_TXT("failed to read query from file %s (errno %u)"), phrasestring.c_str(), ec); } phrasestring = ps; } // Analyze the phrase and print the result: strus::local_ptr<strus::QueryAnalyzerContextInterface> qryanactx( analyzer->createContext()); if (!qryanactx.get()) throw std::runtime_error( _TXT("failed to create query analyzer context")); qryanactx->putField( 1, "", phrasestring); strus::analyzer::QueryTermExpression qry = qryanactx->analyze(); if (errorBuffer->hasError()) throw std::runtime_error( _TXT("query analysis failed")); std::vector<strus::analyzer::QueryTerm> terms; std::vector<strus::analyzer::QueryTermExpression::Instruction>::const_iterator ii = qry.instructions().begin(), ie = qry.instructions().end(); for (int iidx=0; ii != ie; ++ii,++iidx) { if (ii->opCode() == strus::analyzer::QueryTermExpression::Instruction::Term) { const strus::analyzer::QueryTerm& term = qry.term( ii->idx()); if (resultPlain) { if (iidx) std::cout << " "; std::cout << term.value(); } else { std::cout << resultQuot << term.value() << resultQuot << std::endl; } } } if (errorBuffer->hasError()) { throw std::runtime_error( _TXT("error in analyze phrase")); } std::cerr << _TXT("done.") << std::endl; if (!dumpDebugTrace( dbgtrace, NULL/*filename ~ NULL = stderr*/)) { std::cerr << _TXT("failed to dump debug trace to file") << std::endl; } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; return -2; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } if (!dumpDebugTrace( dbgtrace, NULL/*filename ~ NULL = stderr*/)) { std::cerr << _TXT("failed to dump debug trace to file") << std::endl; } return -1; }
int main( int argc_, const char* argv_[]) { int rt = 0; std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } strus::ProgramOptions opt; bool printUsageAndExit = false; try { opt = strus::ProgramOptions( argc_, argv_, 14, "h,help", "v,version", "license", "Q,quiet", "u,user:"******"N,nofranks:", "I,firstrank:", "D,time", "m,module:", "M,moduledir:", "R,resourcedir:", "s,storage:", "r,rpc:", "T,trace:"); if (opt( "help")) printUsageAndExit = true; std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw strus::runtime_error(_TXT("failed to create module loader")); if (opt("moduledir")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--module", "--rpc"); std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cerr << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus analyzer version ") << STRUS_ANALYZER_VERSION_STRING << std::endl; std::cout << _TXT("Strus storage version ") << STRUS_STORAGE_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 3) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } if (opt.nofargs() < 3) { std::cerr << _TXT("too few arguments") << std::endl; printUsageAndExit = true; rt = 2; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusQuery [options] <anprg> <qeprg> <query>" << std::endl; std::cout << "<anprg> = " << _TXT("path of query analyzer program") << std::endl; std::cout << "<qeprg> = " << _TXT("path of query eval program") << std::endl; std::cout << "<query> = " << _TXT("path of query or '-' for stdin") << std::endl; std::cout << _TXT("description: Executes a query or a list of queries from a file.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-s|--storage <CONFIG>" << std::endl; std::cout << " " << _TXT("Define the storage configuration string as <CONFIG>") << std::endl; if (!opt("rpc")) { std::cout << _TXT(" <CONFIG> is a semicolon ';' separated list of assignments:") << std::endl; printStorageConfigOptions( std::cout, moduleLoader.get(), (opt("storage")?opt["storage"]:""), errorBuffer.get()); } std::cout << "-u|--user <NAME>" << std::endl; std::cout << " " << _TXT("Use user name <NAME> for the query") << std::endl; std::cout << "-N|--nofranks <N>" << std::endl; std::cout << " " << _TXT("Return maximum <N> ranks as query result") << std::endl; std::cout << "-I|--firstrank <N>" << std::endl; std::cout << " " << _TXT("Return the result starting with rank <N> as first rank") << std::endl; std::cout << "-Q|--quiet" << std::endl; std::cout << " " << _TXT("No output of results") << std::endl; std::cout << "-D|--time" << std::endl; std::cout << " " << _TXT("Do print duration of pure query evaluation") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-R|--resourcedir <DIR>" << std::endl; std::cout << " " << _TXT("Search resource files for analyzer first in <DIR>") << std::endl; std::cout << "-r|--rpc <ADDR>" << std::endl; std::cout << " " << _TXT("Execute the command on the RPC server specified by <ADDR>") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Parse arguments: bool quiet = opt( "quiet"); bool measureDuration = opt( "time"); std::string username; std::size_t nofRanks = 20; std::size_t firstRank = 0; std::string storagecfg; if (opt("user")) { username = opt[ "user"]; } if (opt("nofranks")) { nofRanks = opt.asUint( "nofranks"); } if (opt("firstrank")) { firstRank = opt.asUint( "firstrank"); } if (opt("storage")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); storagecfg = opt["storage"]; } std::string analyzerprg = opt[0]; std::string queryprg = opt[1]; std::string querypath = opt[2]; // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } // Set paths for locating resources: if (opt("resourcedir")) { std::vector<std::string> pathlist( opt.list("resourcedir")); std::vector<std::string>::const_iterator pi = pathlist.begin(), pe = pathlist.end(); for (; pi != pe; ++pi) { moduleLoader->addResourcePath( *pi); } } std::string resourcepath; if (0!=strus::getParentPath( analyzerprg, resourcepath)) { throw strus::runtime_error( _TXT("failed to evaluate resource path")); } if (!resourcepath.empty()) { moduleLoader->addResourcePath( resourcepath); } // Create objects for query evaluation: std::auto_ptr<strus::RpcClientMessagingInterface> messaging; std::auto_ptr<strus::RpcClientInterface> rpcClient; std::auto_ptr<strus::AnalyzerObjectBuilderInterface> analyzerBuilder; std::auto_ptr<strus::StorageObjectBuilderInterface> storageBuilder; if (opt("rpc")) { messaging.reset( strus::createRpcClientMessaging( opt[ "rpc"], errorBuffer.get())); if (!messaging.get()) throw strus::runtime_error(_TXT("failed to create rpc client messaging")); rpcClient.reset( strus::createRpcClient( messaging.get(), errorBuffer.get())); if (!rpcClient.get()) throw strus::runtime_error(_TXT("failed to create rpc client")); (void)messaging.release(); analyzerBuilder.reset( rpcClient->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw strus::runtime_error(_TXT("failed to create rpc analyzer object builder")); storageBuilder.reset( rpcClient->createStorageObjectBuilder()); if (!storageBuilder.get()) throw strus::runtime_error(_TXT("failed to create rpc storage object builder")); } else { analyzerBuilder.reset( moduleLoader->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw strus::runtime_error(_TXT("failed to create analyzer object builder")); storageBuilder.reset( moduleLoader->createStorageObjectBuilder()); if (!storageBuilder.get()) throw strus::runtime_error(_TXT("failed to create storage object builder")); } // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::AnalyzerObjectBuilderInterface* aproxy = (*ti)->createProxy( analyzerBuilder.get()); analyzerBuilder.release(); analyzerBuilder.reset( aproxy); strus::StorageObjectBuilderInterface* sproxy = (*ti)->createProxy( storageBuilder.get()); storageBuilder.release(); storageBuilder.reset( sproxy); } // Create objects: std::auto_ptr<strus::StorageClientInterface> storage( strus::createStorageClient( storageBuilder.get(), errorBuffer.get(), storagecfg)); if (!storage.get()) throw strus::runtime_error(_TXT("failed to create storage client")); strus::utils::ScopedPtr<strus::QueryAnalyzerInterface> analyzer( analyzerBuilder->createQueryAnalyzer()); if (!analyzer.get()) throw strus::runtime_error(_TXT("failed to create query analyzer")); strus::utils::ScopedPtr<strus::QueryEvalInterface> qeval( storageBuilder->createQueryEval()); if (!qeval.get()) throw strus::runtime_error(_TXT("failed to create query evaluation interface")); const strus::QueryProcessorInterface* qproc = storageBuilder->getQueryProcessor(); if (!qproc) throw strus::runtime_error(_TXT("failed to get query processor")); const strus::TextProcessorInterface* textproc = analyzerBuilder->getTextProcessor(); if (!textproc) throw strus::runtime_error(_TXT("failed to get text processor")); // Load query analyzer program: unsigned int ec; std::string analyzerProgramSource; ec = strus::readFile( analyzerprg, analyzerProgramSource); if (ec) throw strus::runtime_error(_TXT("failed to load analyzer program %s (errno %u)"), analyzerprg.c_str(), ec); if (!strus::loadQueryAnalyzerProgram( *analyzer, textproc, analyzerProgramSource, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query analyzer program")); } // Load query evaluation program: std::string qevalProgramSource; ec = strus::readFile( queryprg, qevalProgramSource); if (ec) throw strus::runtime_error(_TXT("failed to load query eval program %s (errno %u)"), queryprg.c_str(), ec); if (!strus::loadQueryEvalProgram( *qeval, qproc, qevalProgramSource, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query evaluation program")); } // Load query: std::string querystring; if (querypath == "-") { ec = strus::readStdin( querystring); if (ec) throw strus::runtime_error( _TXT("failed to read query string from stdin (errno %u)"), ec); } else { ec = strus::readFile( querypath, querystring); if (ec) throw strus::runtime_error(_TXT("failed to read query string from file %s (errno %u)"), querypath.c_str(), ec); } unsigned int nofQueries = 0; double startTime = 0.0; if (measureDuration) { startTime = getTimeStamp(); } std::string::const_iterator si = querystring.begin(), se = querystring.end(); std::string qs; while (strus::scanNextProgram( qs, si, se, errorBuffer.get())) { ++nofQueries; std::auto_ptr<strus::QueryInterface> query( qeval->createQuery( storage.get())); if (!query.get()) throw strus::runtime_error(_TXT("failed to create query object")); if (!strus::loadQuery( *query, analyzer.get(), qproc, qs, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query from source")); } query->setMaxNofRanks( nofRanks); query->setMinRank( firstRank); if (!username.empty()) { query->addUserName( username); } strus::QueryResult result = query->evaluate(); if (!quiet) { std::cout << strus::string_format( _TXT("evaluated till pass %u, got %u ranks (%u without restrictions applied):"), result.evaluationPass(), result.nofDocumentsRanked(), result.nofDocumentsVisited()) << std::endl; std::cout << strus::string_format( _TXT("ranked list (starting with rank %u, maximum %u results):"), firstRank, nofRanks) << std::endl; } std::vector<strus::ResultDocument>::const_iterator wi = result.ranks().begin(), we = result.ranks().end(); if (!quiet) { for (int widx=1; wi != we; ++wi,++widx) { std::cout << strus::string_format( _TXT( "[%u] %u score %f"), widx, wi->docno(), wi->weight()) << std::endl; std::vector<strus::SummaryElement>::const_iterator ai = wi->summaryElements().begin(), ae = wi->summaryElements().end(); for (; ai != ae; ++ai) { std::cout << "\t" << ai->name(); if (ai->index() >= 0) { std::cout << "[" << ai->index() << "]"; } std::cout << " = '" << ai->value() << "'"; std::cout << " " << ai->weight() << std::endl; } } } } if (measureDuration) { double endTime = getTimeStamp(); double duration = endTime - startTime; std::cerr << strus::string_format( _TXT("evaluated %u queries in %.4f seconds"), nofQueries, duration) << std::endl; } if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("unhandled error in command line query")); } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } return -1; }
spep::MetadataThread::RawMetadata spep::MetadataThread::getRawMetadata( std::string metadataURL ) { EVP_MD_CTX *hashContext = &(this->_hashContext); // Initialize the hash function for the new metadata. if ( ! EVP_DigestInit_ex( hashContext, this->_hashType, NULL ) ) { throw InvalidStateException( "Unable to initialize the digest function for metadata hashing" ); } RawMetadata data( hashContext ); CArray<char> errorBuffer( CURL_ERROR_SIZE ); std::memset( errorBuffer.get(), 0, CURL_ERROR_SIZE ); _localLogger.debug() << "Calling cURL to retrieve metadata from " << metadataURL; CURL *pCurlHandle = curl_easy_init(); // Set the URL for curl to retrieve from curl_easy_setopt(pCurlHandle, CURLOPT_URL, metadataURL.c_str()); // Give curl something to call with its data curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, spep::MetadataThread::curlCallback); curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, (void*)&data); // Buffer to output an error message if the call fails curl_easy_setopt(pCurlHandle, CURLOPT_ERRORBUFFER, errorBuffer.get()); // Don't give us any content on a HTTP >=400 response curl_easy_setopt(pCurlHandle, CURLOPT_FAILONERROR, 1); // Ignore signals curl_easy_setopt(pCurlHandle, CURLOPT_NOSIGNAL, 1L); // Debugging code curl_easy_setopt(pCurlHandle, CURLOPT_DEBUGFUNCTION, spep::MetadataThread::debugCallback); curl_easy_setopt(pCurlHandle, CURLOPT_DEBUGDATA, (void*)this); curl_easy_setopt(pCurlHandle, CURLOPT_VERBOSE, 1); // Set the CA bundle, if we were given one if( ! this->_caBundle.empty() ) { curl_easy_setopt( pCurlHandle, CURLOPT_CAINFO, this->_caBundle.c_str() ); } // Perform the retrieve operation. This will block until complete. CURLcode result = curl_easy_perform(pCurlHandle); curl_easy_cleanup(pCurlHandle); _localLogger.debug() << boost::lexical_cast<std::string>( result ); // If the request didn't succeed, handle the error condition. if (result != CURLE_OK) { _localLogger.error() << std::string("Metadata retrieve failed. Error message was: ") << errorBuffer.get(); data.failed = true; return data; } data.failed = false; // Finalise the hash unsigned int hashLength = 0; unsigned char hashValue[EVP_MAX_MD_SIZE]; EVP_DigestFinal_ex( hashContext, hashValue, &hashLength ); // .. and build it into a string to be returned in the object. AutoArray<char> hashChars( hashLength*2 + 1 ); for (unsigned int i=0; i<hashLength; ++i) { // Output each byte as 2 hex chars. snprintf( &(hashChars[2*i]), 3, "%02x", hashValue[i] ); } data.hashValue = std::string( hashChars.get(), hashLength*2 ); return data; }
int main( int argc, const char* argv[]) { int rt = 0; std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } strus::ProgramOptions opt; bool printUsageAndExit = false; try { opt = strus::ProgramOptions( argc, argv, 6, "h,help", "v,version", "license", "m,module:", "M,moduledir:", "T,trace:"); if (opt( "help")) { printUsageAndExit = true; } std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw strus::runtime_error(_TXT("error creating module loader")); if (opt("moduledir")) { std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cerr << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus storage version ") << STRUS_STORAGE_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() < 2) { std::cerr << _TXT("too few arguments") << std::endl; printUsageAndExit = true; rt = 1; } if (opt.nofargs() > 2) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 2; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusAlterMetaData [options] <config> <cmds>" << std::endl; std::cout << "<config> : " << _TXT("configuration string of the storage") << std::endl; std::cout << " " << _TXT("semicolon';' separated list of assignments:") << std::endl; printStorageConfigOptions( std::cout, moduleLoader.get(), (opt.nofargs()>=1?opt[0]:""), errorBuffer.get()); std::cout << "<cmds> : " << _TXT("semicolon separated list of commands:") << std::endl; std::cout << " alter <name> <newname> <newtype>" << std::endl; std::cout << " <name> :" << _TXT("name of the element to change") << std::endl; std::cout << " <newname> :" << _TXT("new name of the element") << std::endl; std::cout << " <newtype> :" << _TXT("new type (*) of the element") << std::endl; std::cout << " add <name> <type>" << std::endl; std::cout << " <name> :" << _TXT("name of the element to add") << std::endl; std::cout << " <type> :" << _TXT("type (*) of the element to add") << std::endl; std::cout << " delete <name>" << std::endl; std::cout << " <name> :" << _TXT("name of the element to remove") << std::endl; std::cout << " rename <name> <newname>" << std::endl; std::cout << " <name> :" << _TXT("name of the element to rename") << std::endl; std::cout << " <newname> :" << _TXT("new name of the element") << std::endl; std::cout << " clear <name>" << std::endl; std::cout << " <name> :" << _TXT("name of the element to clear all values") << std::endl; std::cout << "(*) :" << _TXT("type of an element is one of the following:") << std::endl; std::cout << " INT8 :" << _TXT("one byte signed integer value") << std::endl; std::cout << " UINT8 :" << _TXT("one byte unsigned integer value") << std::endl; std::cout << " INT16 :" << _TXT("two bytes signed integer value") << std::endl; std::cout << " UINT16 :" << _TXT("two bytes unsigned integer value") << std::endl; std::cout << " INT32 :" << _TXT("four bytes signed integer value") << std::endl; std::cout << " UINT32 :" << _TXT("four bytes unsigned integer value") << std::endl; std::cout << " FLOAT16 :" << _TXT("two bytes floating point value (IEEE 754 small)") << std::endl; std::cout << " FLOAT32 :" << _TXT("four bytes floating point value (IEEE 754 single)") << std::endl; std::cout << _TXT("description: Executes a list of alter the meta data table commands.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Parse arguments: std::string storagecfg = opt[0]; std::vector<AlterMetaDataCommand> cmds = parseCommands( opt[1]); // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } // Create objects for altering the meta data table: std::auto_ptr<strus::StorageObjectBuilderInterface> builder; std::auto_ptr<strus::StorageAlterMetaDataTableInterface> md; builder.reset( moduleLoader->createStorageObjectBuilder()); if (!builder.get()) throw strus::runtime_error(_TXT("failed to create storage object builder")); // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::StorageObjectBuilderInterface* builderproxy = (*ti)->createProxy( builder.get()); builder.release(); builder.reset( builderproxy); } md.reset( strus::createAlterMetaDataTable( builder.get(), errorBuffer.get(), storagecfg)); if (!md.get()) throw strus::runtime_error(_TXT("failed to create storage alter metadata table structure")); // Execute alter meta data table commands: std::vector<AlterMetaDataCommand>::const_iterator ci = cmds.begin(), ce = cmds.end(); for (; ci != ce; ++ci) { switch (ci->id()) { case AlterMetaDataCommand::Alter: md->alterElement( ci->name(), ci->newname(), ci->type()); break; case AlterMetaDataCommand::Add: md->addElement( ci->name(), ci->type()); break; case AlterMetaDataCommand::Delete: md->deleteElement( ci->name()); break; case AlterMetaDataCommand::Rename: md->renameElement( ci->name(), ci->newname()); break; case AlterMetaDataCommand::Clear: md->clearElement( ci->name()); break; } } std::cerr << _TXT("updating meta data table changes...") << std::endl; if (!md->commit()) throw strus::runtime_error(_TXT("alter meta data commit failed")); std::cerr << _TXT("done") << std::endl; if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("unhandled error in alter meta data")); } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } return -1; }
int main( int argc_, const char* argv_[]) { int rt = 0; strus::DebugTraceInterface* dbgtrace = strus::createDebugTrace_standard( 2); if (!dbgtrace) { std::cerr << _TXT("failed to create debug trace") << std::endl; return -1; } strus::local_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2, dbgtrace/*passed with ownership*/)); if (!errorBuffer.get()) { delete dbgtrace; std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } try { bool printUsageAndExit = false; bool withDebugInfo = false; strus::ProgramOptions opt( errorBuffer.get(), argc_, argv_, 18, "h,help", "v,version", "license", "G,debug:", "Q,quiet", "u,user:"******"N,nofranks:", "I,firstrank:", "F,fileinput", "D,time", "m,module:", "M,moduledir:", "R,resourcedir:", "s,storage:", "S,configfile:", "r,rpc:", "T,trace:", "V,verbose"); if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("failed to parse program arguments")); } if (opt( "help")) printUsageAndExit = true; // Enable debugging selected with option 'debug': { std::vector<std::string> dbglist = opt.list( "debug"); std::vector<std::string>::const_iterator gi = dbglist.begin(), ge = dbglist.end(); for (; gi != ge; ++gi) { if (*gi == "weighting") { withDebugInfo = true; } if (!dbgtrace->enable( *gi)) { throw strus::runtime_error(_TXT("failed to enable debug '%s'"), gi->c_str()); } } } strus::local_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw std::runtime_error( _TXT("failed to create module loader")); if (opt("moduledir")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--module", "--rpc"); std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cout << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus analyzer version ") << STRUS_ANALYZER_VERSION_STRING << std::endl; std::cout << _TXT("Strus storage version ") << STRUS_STORAGE_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 3) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } if (opt.nofargs() < 3) { std::cerr << _TXT("too few arguments") << std::endl; printUsageAndExit = true; rt = 2; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusQuery [options] <anprg> <qeprg> <query>" << std::endl; std::cout << "<anprg> = " << _TXT("path of query analyzer program") << std::endl; std::cout << "<qeprg> = " << _TXT("path of query eval program") << std::endl; std::cout << "<query> = " << _TXT("query string") << std::endl; std::cout << " " << _TXT("file or '-' for stdin if option -F is specified)") << std::endl; std::cout << _TXT("description: Executes a query or a list of queries from a file.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-s|--storage <CONFIG>" << std::endl; std::cout << " " << _TXT("Define the storage configuration string as <CONFIG>") << std::endl; if (!opt("rpc")) { std::cout << _TXT(" <CONFIG> is a semicolon ';' separated list of assignments:") << std::endl; printStorageConfigOptions( std::cout, moduleLoader.get(), (opt("storage")?opt["storage"]:""), errorBuffer.get()); } std::cout << "-S|--configfile <FILENAME>" << std::endl; std::cout << " " << _TXT("Define the storage configuration file as <FILENAME>") << std::endl; std::cout << " " << _TXT("<FILENAME> is a file containing the configuration string") << std::endl; std::cout << "-u|--user <NAME>" << std::endl; std::cout << " " << _TXT("Use user name <NAME> for the query") << std::endl; std::cout << "-N|--nofranks <N>" << std::endl; std::cout << " " << _TXT("Return maximum <N> ranks as query result") << std::endl; std::cout << "-I|--firstrank <N>" << std::endl; std::cout << " " << _TXT("Return the result starting with rank <N> as first rank") << std::endl; std::cout << "-Q|--quiet" << std::endl; std::cout << " " << _TXT("No output of results") << std::endl; std::cout << "-D|--time" << std::endl; std::cout << " " << _TXT("Do print duration of pure query evaluation") << std::endl; std::cout << "-F|--fileinput" << std::endl; std::cout << " " << _TXT("Interpret query argument as a file name containing the input") << std::endl; std::cout << "-G|--debug <COMP>" << std::endl; std::cout << " " << _TXT("Issue debug messages for component <COMP> to stderr") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-R|--resourcedir <DIR>" << std::endl; std::cout << " " << _TXT("Search resource files for analyzer first in <DIR>") << std::endl; std::cout << "-r|--rpc <ADDR>" << std::endl; std::cout << " " << _TXT("Execute the command on the RPC server specified by <ADDR>") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; std::cout << "-V|--verbose" << std::endl; std::cout << " " << _TXT("Verbose mode: Print some info like query analysis") << std::endl; return rt; } // Parse arguments: bool quiet = opt( "quiet"); bool doMeasureDuration = opt( "time"); bool verbose = opt("verbose"); std::string username; std::size_t nofRanks = 20; std::size_t firstRank = 0; std::string storagecfg; bool queryIsFile = opt("fileinput"); if (opt("user")) { username = opt[ "user"]; } if (opt("nofranks")) { nofRanks = opt.asUint( "nofranks"); } if (opt("firstrank")) { firstRank = opt.asUint( "firstrank"); } if (opt("configfile")) { if (opt("storage")) throw strus::runtime_error(_TXT("conflicting configuration options specified: '%s' and '%s'"), "--storage", "--configfile"); std::string configfile = opt[ "configfile"]; int ec = strus::readFile( configfile, storagecfg); if (ec) throw strus::runtime_error(_TXT("failed to read configuration file %s (errno %u)"), configfile.c_str(), ec); std::string::iterator di = storagecfg.begin(), de = storagecfg.end(); for (; di != de; ++di) { if ((unsigned char)*di < 32) *di = ' '; } } if (opt("storage")) { if (opt("configfile")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--storage", "--configfile"); storagecfg = opt["storage"]; } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } // Enable debugging selected with option 'debug': { std::vector<std::string> dbglist = opt.list( "debug"); std::vector<std::string>::const_iterator gi = dbglist.begin(), ge = dbglist.end(); for (; gi != ge; ++gi) { if (!dbgtrace->enable( *gi)) { throw strus::runtime_error(_TXT("failed to enable debug '%s'"), gi->c_str()); } } } // Set paths for locating resources: if (opt("resourcedir")) { std::vector<std::string> pathlist( opt.list("resourcedir")); std::vector<std::string>::const_iterator pi = pathlist.begin(), pe = pathlist.end(); for (; pi != pe; ++pi) { moduleLoader->addResourcePath( *pi); } } std::string analyzerprg = getFileArg( opt[0], moduleLoader.get()); std::string queryprg = getFileArg( opt[1], moduleLoader.get()); std::string querystring = opt[2]; if (errorBuffer->hasError()) { throw std::runtime_error( _TXT("error in initialization")); } // Create objects for query evaluation: strus::local_ptr<strus::RpcClientMessagingInterface> messaging; strus::local_ptr<strus::RpcClientInterface> rpcClient; strus::local_ptr<strus::AnalyzerObjectBuilderInterface> analyzerBuilder; strus::local_ptr<strus::StorageObjectBuilderInterface> storageBuilder; if (opt("rpc")) { messaging.reset( strus::createRpcClientMessaging( opt[ "rpc"], errorBuffer.get())); if (!messaging.get()) throw std::runtime_error( _TXT("failed to create rpc client messaging")); rpcClient.reset( strus::createRpcClient( messaging.get(), errorBuffer.get())); if (!rpcClient.get()) throw std::runtime_error( _TXT("failed to create rpc client")); (void)messaging.release(); analyzerBuilder.reset( rpcClient->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw std::runtime_error( _TXT("failed to create rpc analyzer object builder")); storageBuilder.reset( rpcClient->createStorageObjectBuilder()); if (!storageBuilder.get()) throw std::runtime_error( _TXT("failed to create rpc storage object builder")); } else { analyzerBuilder.reset( moduleLoader->createAnalyzerObjectBuilder()); if (!analyzerBuilder.get()) throw std::runtime_error( _TXT("failed to create analyzer object builder")); storageBuilder.reset( moduleLoader->createStorageObjectBuilder()); if (!storageBuilder.get()) throw std::runtime_error( _TXT("failed to create storage object builder")); } // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::AnalyzerObjectBuilderInterface* aproxy = (*ti)->createProxy( analyzerBuilder.get()); analyzerBuilder.release(); analyzerBuilder.reset( aproxy); strus::StorageObjectBuilderInterface* sproxy = (*ti)->createProxy( storageBuilder.get()); storageBuilder.release(); storageBuilder.reset( sproxy); } // Create objects: strus::local_ptr<strus::StorageClientInterface> storage( strus::createStorageClient( storageBuilder.get(), errorBuffer.get(), storagecfg)); if (!storage.get()) throw strus::runtime_error(_TXT("failed to create storage client: %s"), errorBuffer->fetchError()); strus::local_ptr<strus::QueryAnalyzerInstanceInterface> analyzer( analyzerBuilder->createQueryAnalyzer()); if (!analyzer.get()) throw strus::runtime_error(_TXT("failed to create query analyzer: %s"), errorBuffer->fetchError()); strus::local_ptr<strus::QueryEvalInterface> qeval( storageBuilder->createQueryEval()); if (!qeval.get()) throw strus::runtime_error(_TXT("failed to create query evaluation interface: %s"), errorBuffer->fetchError()); const strus::QueryProcessorInterface* qproc = storageBuilder->getQueryProcessor(); if (!qproc) throw strus::runtime_error(_TXT("failed to get query processor: %s"), errorBuffer->fetchError()); const strus::TextProcessorInterface* textproc = analyzerBuilder->getTextProcessor(); if (!textproc) throw strus::runtime_error(_TXT("failed to get text processor: %s"), errorBuffer->fetchError()); if (errorBuffer->hasError()) throw strus::runtime_error(_TXT("error in initialization: %s"), errorBuffer->fetchError()); // Load query analyzer program: if (!strus::load_QueryAnalyzer_programfile_std( analyzer.get(), textproc, analyzerprg, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query analyzer program: %s"), errorBuffer->fetchError()); } // Load query evaluation program: std::string qevalProgramSource; std::string queryprgpath = strus::isExplicitPath( queryprg) ? queryprg : textproc->getResourceFilePath( queryprg); int ec = strus::readFile( queryprgpath, qevalProgramSource); if (ec) throw strus::runtime_error(_TXT("failed to load query eval program %s (errno %u)"), queryprg.c_str(), ec); if (!strus::load_queryeval_program( *qeval, analyzer->queryTermTypes(), qproc, qevalProgramSource, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query evaluation program: %s"), errorBuffer->fetchError()); } // Load query: if (queryIsFile) { std::string qs; if (querystring == "-") { ec = strus::readStdin( qs); if (ec) throw strus::runtime_error( _TXT("failed to read query from stdin (errno %u)"), ec); } else { ec = strus::readFile( querystring, qs); if (ec) throw strus::runtime_error(_TXT("failed to read query from file %s (errno %u)"), querystring.c_str(), ec); } querystring = qs; } unsigned int nofQueries = 0; double startTime = 0.0; if (doMeasureDuration) { startTime = getTimeStamp(); } std::string::const_iterator si = querystring.begin(), se = querystring.end(); std::string qs; while (strus::scanNextProgram( qs, si, se, errorBuffer.get())) { ++nofQueries; strus::local_ptr<strus::QueryInterface> query( qeval->createQuery( storage.get())); if (!query.get()) throw strus::runtime_error(_TXT("failed to create query object: %s"), errorBuffer->fetchError()); std::string selectionFeatureSet; std::string weightingFeatureSet; std::vector<std::string> selfset = qeval->getSelectionFeatureSets(); if (!selfset.empty()) selectionFeatureSet = selfset[0]; std::vector<std::string> weightset = qeval->getWeightingFeatureSets(); if (!weightset.empty()) weightingFeatureSet = weightset[0]; if (!strus::loadQuery( *query, analyzer.get(), selectionFeatureSet, weightingFeatureSet, qproc, qs, errorBuffer.get())) { throw strus::runtime_error(_TXT("failed to load query from source: %s"), errorBuffer->fetchError()); } if (withDebugInfo) { query->setDebugMode( true); } query->setMaxNofRanks( nofRanks); query->setMinRank( firstRank); if (!username.empty()) { query->addAccess( username); } if (verbose) { std::cerr << "Query:" << std::endl; std::cerr << query->tostring() << std::endl; } strus::QueryResult result = query->evaluate(); if (!quiet) { std::cout << strus::string_format( _TXT("evaluated till pass %u, got %u ranks (%u without restrictions applied):"), result.evaluationPass(), result.nofRanked(), result.nofVisited()) << std::endl; std::cout << strus::string_format( _TXT("ranked list (starting with rank %u, maximum %u results):"), (unsigned int)firstRank, (unsigned int)nofRanks) << std::endl; } std::vector<strus::ResultDocument>::const_iterator wi = result.ranks().begin(), we = result.ranks().end(); if (!quiet) { for (int widx=1; wi != we; ++wi,++widx) { std::cout << strus::string_format( _TXT( "[%u] score %f"), widx, wi->weight()) << std::endl; std::vector<strus::SummaryElement>::const_iterator ai = wi->summaryElements().begin(), ae = wi->summaryElements().end(); for (; ai != ae; ++ai) { std::cout << "\t" << ai->name(); if (ai->index() >= 0) { std::cout << "[" << ai->index() << "]"; } std::cout << " = '" << ai->value() << "'"; std::cout << " " << ai->weight() << std::endl; } } } } if (doMeasureDuration) { double endTime = getTimeStamp(); double duration = endTime - startTime; std::cerr << strus::string_format( _TXT("evaluated %u queries in %.4f seconds"), nofQueries, duration) << std::endl; } if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("unhandled error in command line query: %s"), errorBuffer->fetchError()); } std::cerr << _TXT("done.") << std::endl; if (!dumpDebugTrace( dbgtrace, NULL/*filename ~ NULL = stderr*/)) { std::cerr << _TXT("failed to dump debug trace to file") << std::endl; } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; return -2; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } if (!dumpDebugTrace( dbgtrace, NULL/*filename ~ NULL = stderr*/)) { std::cerr << _TXT("failed to dump debug trace to file") << std::endl; } return -1; }
int main( int argc, const char* argv[]) { strus::local_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2, NULL/*debug trace interface*/)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } g_errorBuffer = errorBuffer.get(); try { bool doExit = false; int argi = 1; unsigned int transactionsize = 1000; std::pair<unsigned int,unsigned int> docnorange(0,0); std::string termtype; // Parsing arguments: for (; argi < argc; ++argi) { if (0==std::strcmp( argv[argi], "-h") || 0==std::strcmp( argv[argi], "--help")) { printUsage(); doExit = true; } else if (0==std::strcmp( argv[argi], "-v") || 0==std::strcmp( argv[argi], "--version")) { std::cerr << "strus storage version " << STRUS_STORAGE_VERSION_STRING << std::endl; doExit = true; } else if (0==std::strcmp( argv[argi], "-c") || 0==std::strcmp( argv[argi], "--commit")) { if (argi == argc || argv[argi+1][0] == '-') { throw strus::runtime_error( _TXT("no argument given to option %s"), "--commit"); } ++argi; transactionsize = parseNumber( argv[argi], "argument for option --commit"); } else if (0==std::strcmp( argv[argi], "-D") || 0==std::strcmp( argv[argi], "--docno")) { if (argi == argc || argv[argi+1][0] == '-') { throw strus::runtime_error( _TXT("no argument given to option %s"), "--docno"); } ++argi; docnorange = parseNumberRange( argv[argi], "argument for option --docno"); } else if (0==std::strcmp( argv[argi], "-T") || 0==std::strcmp( argv[argi], "--termtype")) { if (argi == argc || argv[argi+1][0] == '-') { throw strus::runtime_error( _TXT("no argument given to option %s"), "--termtype"); } ++argi; termtype = argv[ argi]; } else if (argv[argi][0] == '-') { throw strus::runtime_error(_TXT("unknown option %s"), argv[ argi]); } else { break; } } if (doExit) return 0; if (argc - argi < 3) throw strus::runtime_error( _TXT("too few arguments (given %u, required %u)"), argc - argi, 3); if (argc - argi > 3) throw strus::runtime_error( _TXT("too many arguments (given %u, required %u)"), argc - argi, 3); std::string dbconfig( argv[ argi+0]); std::string blocktype( argv[ argi+1]); unsigned int newblocksize = parseNumber( argv[argi+2], "positional argument 3"); strus::DatabaseInterface* dbi = strus::createDatabaseType_leveldb( "", g_errorBuffer); if (!dbi) throw strus::runtime_error( "%s", _TXT("could not create leveldb key/value store database handler")); if (g_errorBuffer->hasError()) throw std::runtime_error( _TXT("error in initialization")); resizeBlocks( dbi, dbconfig, blocktype, termtype, newblocksize, transactionsize, docnorange); // Check for reported error an terminate regularly: if (g_errorBuffer->hasError()) { throw strus::runtime_error( "%s", _TXT("error processing resize blocks")); } std::cerr << _TXT("done") << std::endl; return 0; } catch (const std::exception& e) { const char* errormsg = g_errorBuffer?g_errorBuffer->fetchError():0; if (errormsg) { std::cerr << e.what() << ": " << errormsg << std::endl; } else { std::cerr << e.what() << std::endl; } } std::cerr << _TXT("terminated") << std::endl; return -1; }
int main( int argc, const char* argv[]) { int rt = 0; std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } strus::ProgramOptions opt; bool printUsageAndExit = false; try { opt = strus::ProgramOptions( argc, argv, 9, "h,help", "v,version", "license", "m,module:", "M,moduledir:", "r,rpc:", "s,storage:", "e,exists", "T,trace:"); if (opt( "help")) printUsageAndExit = true; std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw strus::runtime_error( _TXT("error creating module loader")); if (opt("moduledir")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cerr << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus storage version ") << STRUS_STORAGE_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 0) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusCheckStorage [options]" << std::endl; std::cout << _TXT("description: Checks a storage for corrupt data.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-s|--storage <CONFIG>" << std::endl; std::cout << " " << _TXT("Define the storage configuration string as <CONFIG>") << std::endl; if (!opt("rpc")) { std::cout << _TXT(" <CONFIG> is a semicolon ';' separated list of assignments:") << std::endl; printStorageConfigOptions( std::cout, moduleLoader.get(), (opt("storage")?opt["storage"]:""), errorBuffer.get()); } std::cout << "-e|--exists" << std::endl; std::cout << " " << _TXT("Checks if the database of the storage exists and return 'yes'/'no'") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-r|--rpc <ADDR>" << std::endl; std::cout << " " << _TXT("Execute the commands on the RPC server specified by <ADDR>") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } std::string storagecfg; if (opt("storage")) { if (opt("rpc")) throw strus::runtime_error(_TXT("specified mutual exclusive options %s and %s"), "--moduledir", "--rpc"); storagecfg = opt["storage"]; } // Create objects to check storage: std::auto_ptr<strus::RpcClientMessagingInterface> messaging; std::auto_ptr<strus::RpcClientInterface> rpcClient; std::auto_ptr<strus::StorageObjectBuilderInterface> storageBuilder; if (opt("rpc")) { messaging.reset( strus::createRpcClientMessaging( opt[ "rpc"], errorBuffer.get())); if (!messaging.get()) throw strus::runtime_error( _TXT("error creating rpc client messaging")); rpcClient.reset( strus::createRpcClient( messaging.get(), errorBuffer.get())); if (!rpcClient.get()) throw strus::runtime_error( _TXT("error creating rpc client")); (void)messaging.release(); storageBuilder.reset( rpcClient->createStorageObjectBuilder()); if (!storageBuilder.get()) throw strus::runtime_error( _TXT("error creating rpc storage object builder")); } else { storageBuilder.reset( moduleLoader->createStorageObjectBuilder()); if (!storageBuilder.get()) throw strus::runtime_error( _TXT("error creating storage object builder")); } // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::StorageObjectBuilderInterface* sproxy = (*ti)->createProxy( storageBuilder.get()); storageBuilder.release(); storageBuilder.reset( sproxy); } if (opt("exists")) { const strus::DatabaseInterface* dbi = storageBuilder->getDatabase( storagecfg); if (!dbi) throw strus::runtime_error(_TXT("could not find key/value store database")); if (dbi->exists( storagecfg)) { std::cout << "yes" << std::endl; } else { std::cout << "no" << std::endl; } if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("unhandled error in analyze")); } } else { std::auto_ptr<strus::StorageClientInterface> storage( strus::createStorageClient( storageBuilder.get(), errorBuffer.get(), storagecfg)); if (!storage.get()) throw strus::runtime_error(_TXT("failed to create storage client")); if (!storage->checkStorage( std::cerr)) { if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("error in check storage")); } std::cerr << _TXT("check storage failed") << std::endl; } else { std::cerr << _TXT("done") << std::endl; } } return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } return -1; }
int main( int argc, const char* argv[]) { int rt = 0; std::auto_ptr<strus::ErrorBufferInterface> errorBuffer( strus::createErrorBuffer_standard( 0, 2)); if (!errorBuffer.get()) { std::cerr << _TXT("failed to create error buffer") << std::endl; return -1; } strus::ProgramOptions opt; bool printUsageAndExit = false; try { opt = strus::ProgramOptions( argc, argv, 8, "h,help", "v,version", "license", "m,module:", "M,moduledir:", "s,storage:", "S,configfile:", "T,trace:"); if (opt( "help")) printUsageAndExit = true; std::auto_ptr<strus::ModuleLoaderInterface> moduleLoader( strus::createModuleLoader( errorBuffer.get())); if (!moduleLoader.get()) throw strus::runtime_error(_TXT("failed to create module loader")); if (opt("moduledir")) { std::vector<std::string> modirlist( opt.list("moduledir")); std::vector<std::string>::const_iterator mi = modirlist.begin(), me = modirlist.end(); for (; mi != me; ++mi) { moduleLoader->addModulePath( *mi); } moduleLoader->addSystemModulePath(); } if (opt("module")) { std::vector<std::string> modlist( opt.list("module")); std::vector<std::string>::const_iterator mi = modlist.begin(), me = modlist.end(); for (; mi != me; ++mi) { if (!moduleLoader->loadModule( *mi)) { throw strus::runtime_error(_TXT("error failed to load module %s"), mi->c_str()); } } } if (opt("license")) { std::vector<std::string> licenses_3rdParty = moduleLoader->get3rdPartyLicenseTexts(); std::vector<std::string>::const_iterator ti = licenses_3rdParty.begin(), te = licenses_3rdParty.end(); if (ti != te) std::cout << _TXT("3rd party licenses:") << std::endl; for (; ti != te; ++ti) { std::cout << *ti << std::endl; } std::cerr << std::endl; if (!printUsageAndExit) return 0; } if (opt( "version")) { std::cout << _TXT("Strus utilities version ") << STRUS_UTILITIES_VERSION_STRING << std::endl; std::cout << _TXT("Strus module version ") << STRUS_MODULE_VERSION_STRING << std::endl; std::cout << _TXT("Strus rpc version ") << STRUS_RPC_VERSION_STRING << std::endl; std::cout << _TXT("Strus trace version ") << STRUS_TRACE_VERSION_STRING << std::endl; std::cout << _TXT("Strus storage version ") << STRUS_STORAGE_VERSION_STRING << std::endl; std::cout << _TXT("Strus base version ") << STRUS_BASE_VERSION_STRING << std::endl; std::vector<std::string> versions_3rdParty = moduleLoader->get3rdPartyVersionTexts(); std::vector<std::string>::const_iterator vi = versions_3rdParty.begin(), ve = versions_3rdParty.end(); if (vi != ve) std::cout << _TXT("3rd party versions:") << std::endl; for (; vi != ve; ++vi) { std::cout << *vi << std::endl; } if (!printUsageAndExit) return 0; } else if (!printUsageAndExit) { if (opt.nofargs() > 1) { std::cerr << _TXT("too many arguments") << std::endl; printUsageAndExit = true; rt = 1; } } std::string storagecfg; int nof_storagecfg = 0; if (opt("configfile")) { nof_storagecfg += 1; std::string configfile = opt[ "configfile"]; int ec = strus::readFile( configfile, storagecfg); if (ec) throw strus::runtime_error(_TXT("failed to read configuration file %s (errno %u)"), configfile.c_str(), ec); std::string::iterator di = storagecfg.begin(), de = storagecfg.end(); for (; di != de; ++di) { if ((unsigned char)*di < 32) *di = ' '; } } if (opt("storage")) { nof_storagecfg += 1; storagecfg = opt[ "storage"]; } if (opt.nofargs() == 1) { std::cerr << _TXT("warning: passing storage as first parameter instead of option -s (deprecated)") << std::endl; nof_storagecfg += 1; storagecfg = opt[0]; } if (nof_storagecfg > 1) { std::cerr << _TXT("conflicting configuration options specified: --storage and --configfile") << std::endl; rt = 10003; printUsageAndExit = true; } else if (!printUsageAndExit && nof_storagecfg == 0) { std::cerr << _TXT("missing configuration option: --storage or --configfile has to be defined") << std::endl; rt = 10004; printUsageAndExit = true; } if (printUsageAndExit) { std::cout << _TXT("usage:") << " strusCreate [options]" << std::endl; std::cout << _TXT("description: Creates a storage with its key value store database.") << std::endl; std::cout << _TXT("options:") << std::endl; std::cout << "-h|--help" << std::endl; std::cout << " " << _TXT("Print this usage and do nothing else") << std::endl; std::cout << "-v|--version" << std::endl; std::cout << " " << _TXT("Print the program version and do nothing else") << std::endl; std::cout << "--license" << std::endl; std::cout << " " << _TXT("Print 3rd party licences requiring reference") << std::endl; std::cout << "-m|--module <MOD>" << std::endl; std::cout << " " << _TXT("Load components from module <MOD>") << std::endl; std::cout << "-M|--moduledir <DIR>" << std::endl; std::cout << " " << _TXT("Search modules to load first in <DIR>") << std::endl; std::cout << "-s|--storage <CONFIG>" << std::endl; std::cout << " " << _TXT("Define the storage configuration string as <CONFIG>") << std::endl; std::cout << " " << _TXT("<CONFIG> is a semicolon ';' separated list of assignments:") << std::endl; printStorageConfigOptions( std::cout, moduleLoader.get(), storagecfg, errorBuffer.get()); std::cout << "-S|--configfile <FILENAME>" << std::endl; std::cout << " " << _TXT("Define the storage configuration file as <FILENAME>") << std::endl; std::cout << " " << _TXT("<FILENAME> is a file containing the configuration string") << std::endl; std::cout << "-T|--trace <CONFIG>" << std::endl; std::cout << " " << _TXT("Print method call traces configured with <CONFIG>") << std::endl; std::cout << " " << strus::string_format( _TXT("Example: %s"), "-T \"log=dump;file=stdout\"") << std::endl; return rt; } // Declare trace proxy objects: typedef strus::Reference<strus::TraceProxy> TraceReference; std::vector<TraceReference> trace; if (opt("trace")) { std::vector<std::string> tracecfglist( opt.list("trace")); std::vector<std::string>::const_iterator ti = tracecfglist.begin(), te = tracecfglist.end(); for (; ti != te; ++ti) { trace.push_back( new strus::TraceProxy( moduleLoader.get(), *ti, errorBuffer.get())); } } // Create root object: std::auto_ptr<strus::StorageObjectBuilderInterface> storageBuilder( moduleLoader->createStorageObjectBuilder()); if (!storageBuilder.get()) throw strus::runtime_error(_TXT("failed to create storage object builder")); // Create proxy objects if tracing enabled: std::vector<TraceReference>::const_iterator ti = trace.begin(), te = trace.end(); for (; ti != te; ++ti) { strus::StorageObjectBuilderInterface* sproxy = (*ti)->createProxy( storageBuilder.get()); storageBuilder.release(); storageBuilder.reset( sproxy); } // Create objects: std::string dbname; (void)strus::extractStringFromConfigString( dbname, storagecfg, "database", errorBuffer.get()); const strus::DatabaseInterface* dbi = storageBuilder->getDatabase( dbname); if (!dbi) throw strus::runtime_error(_TXT("failed to get database interface")); const strus::StorageInterface* sti = storageBuilder->getStorage(); if (!sti) throw strus::runtime_error(_TXT("failed to get storage interface")); if (!sti->createStorage( storagecfg, dbi)) { throw strus::runtime_error(_TXT("failed to create storage")); } if (errorBuffer->hasError()) { throw strus::runtime_error(_TXT("unhandled error in create storage")); } std::cerr << _TXT("storage successfully created.") << std::endl; return 0; } catch (const std::bad_alloc&) { std::cerr << _TXT("ERROR ") << _TXT("out of memory") << std::endl; } catch (const std::runtime_error& e) { const char* errormsg = errorBuffer->fetchError(); if (errormsg) { std::cerr << _TXT("ERROR ") << e.what() << ": " << errormsg << std::endl; } else { std::cerr << _TXT("ERROR ") << e.what() << std::endl; } } catch (const std::exception& e) { std::cerr << _TXT("EXCEPTION ") << e.what() << std::endl; } return -1; }
void CurlHandle::reset() { curl_easy_reset(mCurl); memset(mErrorBuffer, 0, CURL_ERROR_SIZE + 1); errorBuffer(mErrorBuffer); }