file_storage_manager.h

00001 /*
00002  *  Copyright (C) 2005 M.J. Zaki <zaki@cs.rpi.edu> Rensselaer Polytechnic Institute
00003  *  Written by salems@cs.rpi.edu
00004  *  Modifications:
00005  *           added file name passing -- Zaki 7/25/06
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; either version 2
00010  *  of the License, or (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License along
00018  *  with this program; if not, write to the Free Software Foundation, Inc.,
00019  *  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 #ifndef FILE_STORAGE_MANAGER_H
00022 #define FILE_STORAGE_MANAGER_H
00023 
00024 #include<string>
00025 #include<memory>
00026 #include <ext/hash_map>
00027 #include "pat_fam.h"
00028 #include "pat_support.h"
00029 #include "generic_classes.h"
00030 #include "helper_funs.h"
00031 #include "time_tracker.h"
00032 #include "file_cache_manager.h"
00033 
00034 using namespace std;
00035 
00036 #define FILENAME "__DMTL_file_cache.dat"
00037 
00044 template<class PAT, template <typename> class ALLOC, class VAT >
00045 class storage_manager<PAT, VAT, ALLOC, file_storage > {
00046   
00047   typedef typename PAT::CC_STORAGE_TYPE C_ST;
00048   typedef typename PAT::CC_COMPARISON_FUNC C_CF;
00049   
00050 public:
00051     
00052   typedef int key_type;
00053   // typename TYPE T;
00054   typedef VAT data_type;
00055   typedef VAT VATT;
00056   // This way you have designed your cache, so you can
00057   // define cache manager for your caching system.
00058   // Let us use a basic manager.
00059   
00060   typedef cache_manager<key_type, data_type> cache_type;
00061   
00062   typedef typename cache_type::size_type size_type;
00063   typedef pattern_support<typename PAT::MINE_PROPS> PAT_SUP;
00064   
00065   storage_manager(){}
00066   
00067   storage_manager(typename cache_type::size_type C=1,const char * filename=FILENAME){
00068     Cache = new cache_type(C, filename);
00069     manager_count++;   
00070   }
00071   
00072   //Destructor
00073   ~storage_manager(){
00074     if(manager_count==1){
00075       delete Cache;
00076     }
00077     manager_count--;
00078   }
00079   
00082   size_type mem_entries() const {
00083     return Cache->mem_entries();
00084   } // no_entries
00085   
00088   size_type file_entries() const {
00089     return  Cache->file_entries();
00090   } // no_entries
00091   
00092   void print_stats() const{
00093     Cache->print_stats();
00094   }
00095   
00096   bool find(PAT* const& p) const{   
00097     return Cache->find_vat(p->pat_id());
00098   }//end find()
00099   
00100   
00101   void delete_vat(PAT* const& p){
00102     Cache->delete_vat(p->pat_id());
00103   }
00104   
00105   void lock_vat(PAT* const& p) {
00106     Cache->lock_vat(p->pat_id());
00107   } //lock_vat
00108 
00109   void unlock_vat(PAT* const& p) {
00110     Cache->unlock_vat(p->pat_id());
00111   } //unlock_vat
00112 
00113   // get Vat* from the cache
00114   VAT* get_vat(PAT* const& p) const {
00115     VAT *value=Cache->get_vat_point(p->pat_id());
00116     return value;
00117   } //get_using_pattern
00118 
00119   // get Vat* from the cache-level 1
00120   VAT* get_vat(const key_type & id) const{
00121     VAT *value=Cache->get_vat_point_levelone(id);
00122     return value;
00123   } //get_using_ID
00124 
00128   VAT** intersect(PAT* const& p1, PAT* const& p2, PAT_SUP ** cand_sups,PAT ** cand_pats, bool is_l2) {
00129     // Get the vats.
00130     VAT* v1, *v2;
00131     v1 = Cache->get_vat_point(p1->pat_id());
00132     static int p1old=0;
00133     int p1new= p1->pat_id();
00134     if(p1new!=p1old){
00135       Cache->lock_vat(p1new);
00136       Cache->unlock_vat(p1old);      
00137       p1old=p1new;
00138     }
00139     
00140     v2 = Cache->get_vat_point(p2->pat_id());
00141         
00142     if(v1 == 0 || v2 == 0) {
00143       cout<< "file_storage_manager: vat not found for pattern = " << p1->pat_id() << endl;
00144       return NULL;
00145     }
00146     // TODO: Lock v1 and v2. 
00147     return VAT::intersection(v1, v2, cand_sups, cand_pats,is_l2);
00148   }
00149   
00150   //adding vat to the cache--not in level 1
00151   bool add_vat(const PAT* const& p, VAT* v){
00152     bool ret;
00153     ret=Cache->insert(p->pat_id(),v);
00154     return ret;
00155   }//end add_vat()
00156   
00157   //adding vat to the cache--not in level 1
00158   void write_file(const key_type & id,VAT* v){
00159     Cache->write_file(id,*v);
00160   }//end add_vat()
00161   
00162   //adding vat to the cache--level 1
00163   bool my_add_vat(const key_type & id, VAT* v){
00164     bool ret;
00165     ret=Cache->insert_levelone(id,v);
00166     //delete v;
00167     return ret;
00168   }//end add_vat()
00169   
00174   void initialize_vat(std::map<int, int> & mapcount) {
00175     std::map<int, int>::iterator it;
00176     for (it = mapcount.begin(); it != mapcount.end(); ++it) {
00177       VAT* ivat=new VAT();
00178       ivat->resize(it->second);  
00179       my_add_vat(it->first,ivat);
00180     }
00181   } //end initialize_vat
00182 
00183 private:
00184   cache_type* Cache;
00185   
00186   static int manager_count; // Ensures that we do not delete the file twice.
00187 }; // class StorageManager
00188 
00189 template<class PAT, template <typename> class ALLOC, class VAT >
00190 int storage_manager<PAT, VAT, ALLOC, file_storage >::manager_count=0;
00191 
00192 #endif

Generated on Wed Jul 26 14:01:08 2006 for DMTL by  doxygen 1.4.7