COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/MergeOutputIndex.cpp
Go to the documentation of this file.
00001 #include <coopy/MergeOutputIndex.h>
00002 #include <coopy/Dbg.h>
00003 
00004 #include <coopy/EfficientMap.h>
00005 #include <coopy/Stringer.h>
00006 #include <coopy/IndexSniffer.h>
00007 
00008 using namespace coopy::store;
00009 using namespace coopy::cmp;
00010 using namespace std;
00011 
00012 #define HELPER(x) (*((efficient_map<std::string,int> *)(x)))
00013 
00014 MergeOutputIndex::MergeOutputIndex() {
00015   implementation = new efficient_map<std::string,int>;
00016   COOPY_ASSERT(implementation);
00017   pending_row = true;
00018   sheet_set = false;
00019 }
00020 
00021 MergeOutputIndex::~MergeOutputIndex() {
00022   delete &HELPER(implementation);
00023   implementation = NULL;
00024 }
00025 
00026 bool MergeOutputIndex::mergeStart() {
00027   pending_row = true;
00028   sheet_set = false;
00029   return true;
00030 }
00031 
00032 bool MergeOutputIndex::setSheet(const char *name) {
00033   this->name = name;
00034 
00035   sheet_set = true;
00036   efficient_map<string,int>& seen = HELPER(implementation);
00037   seen.clear();
00038   links_column.clear();
00039   links.clear();
00040   pending_row = true;
00041 
00042   links_column_schema = SimpleSheetSchema();
00043   links_column_schema.setSheetName((string(name) + "_columns").c_str());
00044   if (flags.has_pivot) {
00045     links_column_schema.addColumn("p_offset",ColumnType("INTEGER"));
00046   }
00047   links_column_schema.addColumn("l_name",ColumnType("TEXT"));
00048   links_column_schema.addColumn("r_name",ColumnType("TEXT"));
00049   links_column_schema.addColumn("l_offset",ColumnType("INTEGER"));
00050   links_column_schema.addColumn("r_offset",ColumnType("INTEGER"));
00051 
00052   return true;
00053 }
00054 
00055 
00056 bool MergeOutputIndex::mergeDone() {
00057   return true;
00058 }
00059 
00060 
00061 bool MergeOutputIndex::mergeAllDone() {
00062   return true;
00063 }
00064 
00065 
00066 static SheetCell link_cell(int x) {
00067   if (x>=0) return SheetCell(x);
00068   return SheetCell();
00069 }
00070 
00071 static SheetCell link_cell(PolySheet& sheet, int x, int y) {
00072   if (y<0) {
00073     return SheetCell();
00074   }
00075   return sheet.cellSummary(x,y);
00076 }
00077 
00078 bool MergeOutputIndex::declareLink(const LinkDeclare& decl) {
00079   std::string mode = decl.column?"column":"row";
00080   dbg_printf("LINK %s %d %d %d // %s %s %s\n",
00081              mode.c_str(),
00082              decl.rc_id_pivot,
00083              decl.rc_id_local,
00084              decl.rc_id_remote,
00085              decl.rc_str_pivot.c_str(),
00086              decl.rc_str_local.c_str(),
00087              decl.rc_str_remote.c_str());
00088 
00089   if (!sheet_set) {
00090     setSheet("sheet");
00091   }
00092 
00093   if (pending_row && !decl.column) {
00094     pending_row = false;
00095     pivot = decl.pivot;
00096     //printf("WORKING ON A %d x %d sheet: %s\n", pivot.width(), pivot.height(), pivot.toString().c_str());
00097     local = decl.local;
00098     remote = decl.remote;
00099     if (!pivot.isValid()) {
00100       fprintf(stderr,"Table not provided\n");
00101       exit(1);
00102     }
00103     
00104     pivot.mustHaveSchema();
00105     local.mustHaveSchema();
00106     remote.mustHaveSchema();
00107     NameSniffer npivot(pivot,flags);
00108     NameSniffer nlocal(local,flags);
00109     NameSniffer nremote(remote,flags);
00110     IndexSniffer xpivot(pivot,flags,npivot);
00111     IndexSniffer xlocal(local,flags,nlocal);
00112     IndexSniffer xremote(remote,flags,nremote);
00113     ipivot = xpivot.suggestIndexes();
00114     ilocal = xlocal.suggestIndexes();
00115     iremote = xremote.suggestIndexes();
00116     //printf("%d %d %d\n", ipivot.size(), ilocal.size(), iremote.size());
00117 
00118     SimpleSheetSchema ss;
00119     ss.setSheetName(name.c_str());
00120     if (flags.has_pivot) {
00121       for (int i=0; i<(int)ipivot.size(); i++) {
00122         if (ipivot[i]) {
00123           ss.addColumn((string("p_")+npivot.suggestColumnName(i)).c_str(),
00124                        npivot.suggestColumnType(i));
00125         }
00126       }
00127     }
00128     {
00129       for (int i=0; i<(int)ilocal.size(); i++) {
00130         if (ilocal[i]) {
00131           ss.addColumn((string("l_")+nlocal.suggestColumnName(i)).c_str(),
00132                        nlocal.suggestColumnType(i));
00133         }
00134       }
00135     }
00136     {
00137       for (int i=0; i<(int)iremote.size(); i++) {
00138         if (iremote[i]) {
00139           ss.addColumn((string("r_")+nremote.suggestColumnName(i)).c_str(),
00140                        nremote.suggestColumnType(i));
00141         }
00142       }
00143     }
00144     if (getOutputBook()!=NULL) {
00145       links = getOutputBook()->provideSheet(ss);
00146     }
00147     
00148     if (!links.isValid()) {
00149       fprintf(stderr,"* Could not generate links sheet\n");
00150       exit(1);
00151       return false;
00152     }
00153     links.deleteData();
00154   }
00155   
00156   if (decl.column) {
00157     if (!links_column.isValid()) {
00158       if (getOutputBook()!=NULL) {
00159         links_column = getOutputBook()->provideSheet(links_column_schema);
00160       }
00161       
00162       if (!links_column.isValid()) {
00163         fprintf(stderr,"* Could not generate links schema sheet\n");
00164         exit(1);
00165         return false;
00166       }
00167       links_column.deleteData();
00168     }
00169   }
00170 
00171   std::string frame = "none";
00172   switch (decl.mode) {
00173   case LINK_DECLARE_MERGE:
00174     frame = "merge";
00175     break;
00176   case LINK_DECLARE_LOCAL:
00177     frame = "local";
00178     break;
00179   case LINK_DECLARE_REMOTE:
00180     frame = "remote";
00181     break;
00182   }
00183 
00184   string s = mode + "_" +
00185     stringer_encoder(decl.rc_id_pivot) + "_" +
00186     stringer_encoder(decl.rc_id_local) + "_" +
00187     stringer_encoder(decl.rc_id_remote) + "_" +
00188     stringer_encoder(decl.rc_deleted);
00189 
00190   efficient_map<string,int>& seen = HELPER(implementation);
00191   if (seen.find(s)==seen.end()) {
00192     seen[s] = 1;
00193     if (decl.column) {
00194       Poly<SheetRow> pRow = links_column.insertRow();
00195       SheetRow& row = *pRow;
00196       int at = 0;
00197       if (flags.has_pivot) {
00198         row.setCell(at,link_cell(decl.rc_id_pivot)); at++;
00199       }
00200       //row.setCell(at,SheetCell(decl.rc_deleted?1:0)); at++;
00201       row.setCell(at,SheetCell(decl.rc_str_local,false)); at++;
00202       row.setCell(at,SheetCell(decl.rc_str_remote,false)); at++;
00203       row.setCell(at,link_cell(decl.rc_id_local)); at++;
00204       row.setCell(at,link_cell(decl.rc_id_remote)); at++;
00205       row.flush();
00206     } else {
00207       Poly<SheetRow> pRow = links.insertRow();
00208       SheetRow& row = *pRow;
00209       int at = 0;
00210       if (flags.has_pivot) {
00211         for (int i=0; i<(int)ipivot.size(); i++) {
00212           if (ipivot[i]) {
00213             row.setCell(at,link_cell(pivot,i,decl.rc_id_pivot)); at++;
00214           }
00215         }
00216       }
00217       {
00218         for (int i=0; i<(int)ilocal.size(); i++) {
00219           if (ilocal[i]) {
00220             row.setCell(at,link_cell(local,i,decl.rc_id_local)); at++;
00221           }
00222         }
00223       }
00224       {
00225         for (int i=0; i<(int)iremote.size(); i++) {
00226           if (iremote[i]) {
00227             row.setCell(at,link_cell(remote,i,decl.rc_id_remote)); at++;
00228           }
00229         }
00230       }
00231         /*
00232         // not yet ready for showing multi-column keys etc
00233         row.setCell(at,link_cell(decl.rc_id_pivot)); at++;
00234         row.setCell(at,link_cell(decl.rc_id_local)); at++;
00235         row.setCell(at,link_cell(decl.rc_id_remote)); at++;
00236         */
00237       //row.setCell(at,SheetCell(decl.rc_deleted?1:0)); at++;
00238       row.flush();
00239     }
00240   }
00241   return true;
00242 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines