src/dbapi/cache/admintool/dbapi_cache_admin.cpp

Go to the documentation of this file.
00001 /*  $Id: dbapi_cache_admin.cpp 112327 2007-10-16 14:44:27Z ivanovp $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Author: Anatoliy Kuznetsov
00027  *
00028  * File Description:  DBAPI BLOB cache administration tool
00029  *
00030  */
00031 
00032 
00033 #include <ncbi_pch.hpp>
00034 #include <corelib/ncbiapp.hpp>
00035 #include <corelib/ncbiargs.hpp>
00036 
00037 #include <dbapi/cache/dbapi_blob_cache.hpp>
00038 #include <dbapi/driver/drivers.hpp>
00039 #include <dbapi/error_codes.hpp>
00040 
00041 #include <common/test_assert.h>  /* This header must go last */
00042 
00043 
00044 #define NCBI_USE_ERRCODE_X   Dbapi_CacheAdmin
00045 
00046 USING_NCBI_SCOPE;
00047 
00048 
00049 /// DBAPI cache administration tool
00050 ///
00051 /// @internal
00052 ///
00053 class CDBAPI_CacheAdmin : public CNcbiApplication
00054 {
00055 public:
00056     void Init(void);
00057     int Run(void);
00058 
00059 protected:
00060     int Connect(const CArgs& args);
00061     unsigned GetTimeout(const CArgs& args);
00062 
00063 private:
00064     IDataSource*            m_Ds;
00065     auto_ptr<IConnection>   m_Conn;
00066     CDBAPI_Cache            m_Cache;
00067 };
00068 
00069 
00070 void CDBAPI_CacheAdmin::Init(void)
00071 {
00072     SetDiagPostLevel(eDiag_Warning);
00073     SetDiagPostFlag(eDPF_File);
00074     SetDiagPostFlag(eDPF_Line);
00075     auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
00076     arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
00077                               "DBAPI cache admin library");
00078 
00079     arg_desc->AddDefaultKey("s",
00080                              "server",
00081                              "Database server name (default: MSSQL10)",
00082                              CArgDescriptions::eString,
00083                              "MSSQL10");
00084 
00085     arg_desc->AddDefaultKey("d",
00086                              "database",
00087                              "Database name (default: NCBI_Cache)",
00088                              CArgDescriptions::eString,
00089                              "NCBI_Cache");
00090 
00091     arg_desc->AddDefaultKey("u",
00092                              "username",
00093                              "Login name (default: cwrite)",
00094                              CArgDescriptions::eString,
00095                              "cwrite");
00096 
00097     arg_desc->AddDefaultKey("p",
00098                             "password",
00099                             "Password",
00100                             CArgDescriptions::eString,
00101                             "allowed");
00102 
00103     arg_desc->AddFlag("m",
00104                       "Run cache maintenance (Timeout based BLOB removal).");
00105 
00106 
00107 
00108     arg_desc->AddDefaultKey("st",
00109                       "stimeout",
00110                       "BLOB expiration timeout in seconds",
00111                       CArgDescriptions::eInteger, 
00112                       "0");
00113 
00114     arg_desc->AddDefaultKey("mt",
00115                             "mtimeout",
00116                             "BLOB expiration timeout in minutes",
00117                             CArgDescriptions::eInteger,
00118                             "0");
00119 
00120     arg_desc->AddDefaultKey("ht",
00121                             "htimeout",
00122                             "BLOB expiration timeout in hours",
00123                             CArgDescriptions::eInteger,
00124                             "0");
00125 
00126     arg_desc->AddDefaultKey("dt",
00127                             "dtimeout",
00128                             "BLOB expiration timeout in days",
00129                             CArgDescriptions::eInteger,
00130                             "0");
00131 
00132     SetupArgDescriptions(arg_desc.release());
00133 }
00134 
00135 
00136 int CDBAPI_CacheAdmin::Connect(const CArgs& args)
00137 {
00138     CDriverManager &db_drv_man = CDriverManager::GetInstance();
00139     string drv_name;
00140     DBAPI_RegisterDriver_FTDS();
00141     drv_name = "ftds";
00142 
00143     IDataSource* ds = db_drv_man.CreateDs(drv_name);
00144 
00145     if (ds == 0) {
00146         ERR_POST_X(1, Error << "Cannot init driver: " << drv_name);
00147         return 1;
00148     }
00149 
00150     m_Conn.reset(ds->CreateConnection());
00151     if (m_Conn.get() == 0) {
00152         ERR_POST_X(2, "Cannot create connection. Driver: " << drv_name);
00153         return 1;
00154     }
00155 
00156     string server   = args["s"].AsString();
00157     string database = args["d"].AsString();
00158     string user     = args["u"].AsString();
00159     string passwd   = args["p"].AsString();
00160 
00161     m_Conn->Connect(user, passwd, server, database);
00162 
00163     m_Cache.Open(&*m_Conn);
00164 
00165     return 0;
00166 }
00167 
00168 unsigned CDBAPI_CacheAdmin::GetTimeout(const CArgs& args)
00169 {
00170     unsigned timeout = 24 * 60 * 60;
00171     unsigned sec  = args["st"].AsInteger();
00172     unsigned min  = args["mt"].AsInteger();
00173     unsigned hr   = args["ht"].AsInteger();
00174     unsigned days = args["dt"].AsInteger();
00175 
00176     unsigned tout = sec + (min * 60) + (hr * 60 * 60) + (days * 24 * 60 * 60);
00177 
00178     if (tout == 0)
00179         return timeout;
00180     return tout;
00181 }
00182 
00183 int CDBAPI_CacheAdmin::Run(void)
00184 {
00185     try {
00186         CArgs args = GetArgs();
00187 
00188         int rc = Connect(args);
00189         if (rc) {
00190             return rc;
00191         }
00192 
00193         if (args["m"]) {
00194             NcbiCout << "Running cache maintanance..." << NcbiEndl;
00195 
00196             unsigned time_out = GetTimeout(args);
00197             m_Cache.Purge(time_out);
00198         }
00199 
00200     } catch( CDB_Exception& dbe ) {
00201         ERR_POST_X(3, dbe.what() << dbe.GetMsg());
00202                  
00203     }
00204 
00205     return 0;
00206 }
00207 
00208 
00209 int main(int argc, const char* argv[])
00210 {
00211     return CDBAPI_CacheAdmin().AppMain(argc, argv, 0, eDS_Default, 0);
00212 }
00213 
00214 

Generated on Sun Dec 6 22:22:51 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:20:58 2009 by modify_doxy.py rev. 173732