CacheTypes.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include "zypp/base/Logger.h"
00014 #include "zypp/base/Exception.h"
00015 #include "zypp/cache/sqlite3x/sqlite3x.hpp"
00016 #include "zypp/CheckSum.h"
00017 #include "zypp/cache/CacheTypes.h"
00018 
00019 using namespace std;
00020 using namespace sqlite3x;
00021 
00023 namespace zypp
00024 { 
00025 
00026   namespace cache
00027   { 
00028 
00030     //
00031     //  CLASS NAME : CacheTypes
00032     //
00034 
00036     //
00037     //  METHOD NAME : CacheTypes::CacheTypes
00038     //  METHOD TYPE : Ctor
00039     //
00040     CacheTypes::CacheTypes( const Pathname &dbdir )
00041       : _dbdir(dbdir)
00042     {
00043       refreshCache();
00044     }
00045 
00046     void CacheTypes::refreshCache()
00047     {
00048       try
00049       {
00050         sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
00051         con.executenonquery("PRAGMA cache_size=8000;");
00052         con.executenonquery("BEGIN;");
00053       
00054         // get all types
00055         sqlite3_command select_types_cmd( con, "select id,class,name from types;");
00056         sqlite3_reader reader = select_types_cmd.executereader();
00057       
00058         while(reader.read())
00059         {
00060           data::RecordId id = reader.getint64(0);
00061           string klass = reader.getstring(1);
00062           string name = reader.getstring(2);
00063           if ( klass == "arch" )
00064             _arch_cache[id] = Arch(name);
00065           if ( klass == "rel" )
00066             _rel_cache[id] = Rel(name);
00067           if ( klass == "kind" )
00068             _kind_cache[id] = Resolvable::Kind(name);
00069           if ( klass == "deptype" )
00070             _deptype_cache[id] = name;
00071         }
00072         
00073         MIL << "archs: " << _arch_cache.size() << endl;
00074         MIL << "rel  : " << _rel_cache.size() << endl;
00075         MIL << "kind : " << _kind_cache.size() << endl;
00076         MIL << "deptype : " << _deptype_cache.size() << endl;
00077       }
00078       catch ( std::exception &e )
00079       {
00080         ZYPP_THROW(Exception("Error reading types"));
00081       }
00082     }
00083     
00084     Rel CacheTypes::relationFor( const data::RecordId &id )
00085     {
00086       Rel rel;
00087       std::map<data::RecordId, Rel>::const_iterator it;
00088       if ( (it = _rel_cache.find(id) ) != _rel_cache.end() )
00089         rel = it->second;
00090       else
00091         ZYPP_THROW(Exception("Inconsistent Rel"));
00092       
00093       return rel;
00094     
00095     }
00096 
00097     data::RecordId CacheTypes::idForRelation( const Rel &rel )
00098     {
00099       std::map<data::RecordId, Rel>::const_iterator it;
00100       for ( it = _rel_cache.begin(); it != _rel_cache.end(); ++it )
00101       {
00102         if ( rel == it->second )
00103           return it->first;
00104       }
00105       ZYPP_THROW(Exception("Inconsistent Rel"));   
00106     }
00107     
00108     Resolvable::Kind CacheTypes::kindFor( const data::RecordId &id )
00109     {
00110       Resolvable::Kind kind;
00111       std::map<data::RecordId, Resolvable::Kind>::const_iterator it;
00112       if ( (it = _kind_cache.find(id) ) != _kind_cache.end() )
00113         kind = it->second;
00114       else
00115         ZYPP_THROW(Exception("Inconsistent Kind"));
00116       
00117       return kind;
00118     }
00119 
00120     data::RecordId CacheTypes::idForKind( const Resolvable::Kind & kind )
00121     {
00122       std::map<data::RecordId, Resolvable::Kind>::const_iterator it;
00123       for ( it = _kind_cache.begin(); it != _kind_cache.end(); ++it )
00124       {
00125         if ( kind == it->second )
00126           return it->first;
00127       }
00128       ZYPP_THROW(Exception("Inconsistent Kind"));   
00129     }
00130     
00131     Dep CacheTypes::deptypeFor( const data::RecordId &id )
00132     {
00133       std::map<data::RecordId, string>::const_iterator it;
00134       if ( (it = _deptype_cache.find(id) ) != _deptype_cache.end() )
00135         return Dep(it->second);
00136       else
00137       {
00138         ERR << "deptype: " << id << endl;
00139         ZYPP_THROW(Exception("Inconsistent deptype"));
00140       }
00141     }
00142 
00143     data::RecordId CacheTypes::idForDeptype( const Dep & dep )
00144     {
00145       std::map<data::RecordId, string>::const_iterator it;
00146       for ( it = _deptype_cache.begin(); it != _deptype_cache.end(); ++it )
00147       {
00148         if ( dep.asString() == it->second )
00149           return it->first;
00150       }
00151       ZYPP_THROW(Exception("Inconsistent deptype"));   
00152     }
00153     
00154     Arch CacheTypes::archFor( const data::RecordId &id )
00155     {
00156       
00157       Arch arch;
00158       std::map<data::RecordId, Arch>::const_iterator it;
00159       if ( (it = _arch_cache.find(id) ) != _arch_cache.end() )
00160         arch = it->second;
00161       else
00162         ZYPP_THROW(Exception("Inconsistent Arch"));
00163       
00164       return arch;
00165     }
00166 
00167     data::RecordId CacheTypes::idForArch( const Arch & arch )
00168     {
00169       std::map<data::RecordId, Arch>::const_iterator it;
00170       for ( it = _arch_cache.begin(); it != _arch_cache.end(); ++it )
00171       {
00172         if ( arch == it->second )
00173           return it->first;
00174       }
00175       ZYPP_THROW(Exception("Inconsistent Arch"));   
00176     }
00177 
00178 
00179 
00181     //
00182     //  METHOD NAME : CacheTypes::~CacheTypes
00183     //  METHOD TYPE : Dtor
00184     //
00185     CacheTypes::~CacheTypes()
00186     {}
00187     
00188     /******************************************************************
00189     **
00190     **  FUNCTION NAME : operator<<
00191     **  FUNCTION TYPE : std::ostream &
00192     */
00193     std::ostream & operator<<( std::ostream & str, const CacheTypes & obj )
00194     {
00195       return str;
00196     }
00197 
00199   } // namespace cache
00202 } // namespace zypp

Generated on Tue Sep 25 19:23:00 2007 for libzypp by  doxygen 1.5.3