// GenericRepositoryOperator.cxx #include "dataset_catalog/GenericRepositoryOperator.h" #include #include #include using std::string; using std::cout; using std::endl; using dset::GenericRepository; using dset::GenericRepositoryOperator; typedef GenericRepository::IdList IdList; typedef GenericRepositoryOperator::ObjList ObjList; //********************************************************************** // Local definitions. //********************************************************************** namespace { // Convert plain text to XML text. // < --> < // > --> > // & --> & // Return the mapping of special characters between text and XML. typedef std::pair XmlRepPair; typedef std::vector XmlRepMap; typedef XmlRepMap::size_type RepIndex; const XmlRepMap& xmlreps() { static XmlRepMap reps; if ( reps.size() == 0 ) { reps.push_back(XmlRepPair("<", "<")); } return reps; } //********************************************************************** // Convert a string from text to XML. void text_to_xml(string& str) { const XmlRepMap& reps = xmlreps(); RepIndex irep = reps.size(); while ( irep-- > 0 ) { string oldrep = reps[irep].first; string newrep = reps[irep].second; string::size_type ipos = 0; while ( ipos < str.size() ) { ipos = str.find(oldrep, ipos); if ( ipos == string::npos ) break; str.replace(ipos, oldrep.size(), newrep); ipos += newrep.size(); } } } } // end unnamed namespace //********************************************************************** // Member functions. //********************************************************************** // Constructor. GenericRepositoryOperator:: GenericRepositoryOperator(const GenericRepository &rep1, GenericRepository &rep2 ) : m_src(rep1), m_dst(rep2) { } //********************************************************************** // Destructor. GenericRepositoryOperator::~GenericRepositoryOperator() { } //********************************************************************** // Type check. bool GenericRepositoryOperator::is_valid() const { // Check if the 2 repositories are valid // Also check if they hold the same kind of objects if( m_src.is_valid() && m_dst.is_valid() && (m_src.repository_type() == m_dst.repository_type()) ) return true; return false; } //********************************************************************** // Copy source repository to the destination repository. // Returns 0 on success. int GenericRepositoryOperator::merge() { IdList lst = m_src.get_ids(); assert( !lst.empty() ); // Retrieve each element whose id is in the list. // Copy that element to the other repository // only if it is not already present. for(unsigned int i = 0; i < lst.size(); ++i) { if(m_dst.has(lst[i])) continue; else { string ele = m_src.get(lst[i]); text_to_xml(ele); assert( m_dst.insert(lst[i], ele) == 0 ); } } return 0; } //********************************************************************** // Return the list of IDs of inconsistent duplicates. IdList GenericRepositoryOperator::inconsistent_duplicate_ids() { IdList lst1 = m_src.get_ids(); IdList lst; string s1, s2; // Check if each of the ids in one repository is in the other. // Check if they point to the same object in the repository. for(IdList::const_iterator it = lst1.begin(); it != lst1.end(); ++it) { if(m_dst.has(*it)) { s1 = m_src.get(*it); s2 = m_dst.get(*it); if(s1 != s2) lst.push_back(*it); } } return lst; } //********************************************************************** // Return the inconsistent duplicates. ObjList GenericRepositoryOperator::inconsistent_duplicates() { IdList lst1 = m_src.get_ids(); ObjList lst; string s1, s2; // Check if each of the ids in one repository is in the other. // Check if they point to the same object in the repository. for(IdList::const_iterator it = lst1.begin(); it != lst1.end(); ++it) { if(m_dst.has(*it)) { s1 = m_src.get(*it); s2 = m_dst.get(*it); if(s1 != s2) lst.push_back(s1); } } return lst; } //********************************************************************** // Return the 2 repositories. void GenericRepositoryOperator:: result(GenericRepository &rep1, GenericRepository &rep2) { rep1 = m_src; rep2 = m_dst; } //**********************************************************************