store function.
PersistentStorage backend;
backend.storeObject(patch1);
now, lets reimplement retrieve of objects, which is moving some code I
have on test2.cc to the backend class.
/** Dtor */
virtual ~Backend();
virtual void doTest() = 0;
- virtual bool isDatabaseInitialized() = 0;
- virtual void initDatabaseForFirstTime() = 0;
- virtual void storePatch( Patch::Ptr p ) = 0;
- virtual std::list<Patch::Ptr> installedPatches() = 0;
-
+
+ /**
+ * is the storage backend initialized
+ */
+ virtual bool isBackendInitialized() = 0;
+ /**
+ * initialize the storage backend
+ */
+ virtual void initBackend() = 0;
+
+ /**
+ * Stores a Resolvable in the active backend.
+ */
+ virtual void storeObject( Resolvable::Ptr resolvable ) = 0;
+ /**
+ * Deletes a Resolvable from the active backend.
+ */
+ virtual void deleteObject( Resolvable::Ptr resolvable ) = 0;
+
+ /**
+ * Query for installed Resolvables.
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects() = 0;
+ /**
+ * Query for installed Resolvables of a certain kind
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind) = 0;
+ /**
+ * Query for installed Resolvables of a certain kind by name
+ * \a partial_match allows for text search.
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match = false) = 0;
+
private:
/** Pointer to implementation */
class Private;
}
void
-PersistentStorage::storePatch( Patch::Ptr p )
+PersistentStorage::storeObject( Resolvable::Ptr resolvable )
{
- d->backend->storePatch(p);
+ d->backend->storeObject(resolvable);
}
-std::list<Patch::Ptr>
-PersistentStorage::installedPatches()
+void
+PersistentStorage::deleteObject( Resolvable::Ptr resolvable )
+{
+
+}
+
+
+std::list<Resolvable::Ptr>
+PersistentStorage::storedObjects()
+{
+ return d->backend->storedObjects();
+}
+
+std::list<Resolvable::Ptr>
+PersistentStorage::storedObjects(const Resolvable::Kind kind)
+{
+ //list<Resolvable::Ptr>::iterator it;
+ //it = find(nums.begin(), nums.end(), 3); // Search the list.
+ return d->backend->storedObjects(kind);
+}
+
+std::list<Resolvable::Ptr>
+PersistentStorage::storedObjects(const Resolvable::Kind kind, const std::string & name, bool partial_match)
{
- return d->backend->installedPatches();
+ return d->backend->storedObjects(kind, name, partial_match);
}
/******************************************************************
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- namespace storage
- { /////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- //
- // CLASS NAME : PersistentStorage
- //
- /** */
- class PersistentStorage : public base::ReferenceCounted, private base::NonCopyable
- {
- friend std::ostream & operator<<( std::ostream & str, const PersistentStorage & obj );
- typedef intrusive_ptr<PersistentStorage> Ptr;
- typedef intrusive_ptr<const PersistentStorage> constPtr;
- public:
- /** Default ctor */
- PersistentStorage();
- /** Dtor */
- ~PersistentStorage();
- void doTest();
- void storePatch( Patch::Ptr p );
- std::list<Patch::Ptr> installedPatches();
-
- private:
- class Private;
- Private *d;
- };
- ///////////////////////////////////////////////////////////////////
- /** \relates PersistentStorage Stream output */
- std::ostream & operator<<( std::ostream & str, const PersistentStorage & obj );
+ ///////////////////////////////////////////////////////////////////
+ namespace storage
+ { /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : PersistentStorage
+ //
+ /** */
+ class PersistentStorage : public base::ReferenceCounted, private base::NonCopyable
+ {
+ friend std::ostream & operator<<( std::ostream & str, const PersistentStorage & obj );
+ typedef intrusive_ptr<PersistentStorage> Ptr;
+ typedef intrusive_ptr<const PersistentStorage> constPtr;
+ public:
+ /** Default ctor */
+ PersistentStorage();
+ /** Dtor */
+ ~PersistentStorage();
+ void doTest();
- /////////////////////////////////////////////////////////////////
- } // namespace devel.dmacvicar
- ///////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////
+ public:
+ /**
+ * Stores a Resolvable in the active backend.
+ */
+ void storeObject( Resolvable::Ptr resolvable );
+ /**
+ * Deletes a Resolvable from the active backend.
+ */
+ void deleteObject( Resolvable::Ptr resolvable );
+ /**
+ * Query for installed Resolvables.
+ */
+ std::list<Resolvable::Ptr> storedObjects();
+ /**
+ * Query for installed Resolvables of a certain kind.
+ */
+ std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind kind);
+ /**
+ * Query for installed Resolvables of a certain kind by name
+ * \a partial_match allows for text search.
+ */
+ std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind kind, const std::string & name, bool partial_match = false);
+
+ private:
+ class Private;
+ Private *d;
+ };
+ ///////////////////////////////////////////////////////////////////
+ /** \relates PersistentStorage Stream output */
+ std::ostream & operator<<( std::ostream & str, const PersistentStorage & obj );
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace devel.dmacvicar
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
} // namespace devel
///////////////////////////////////////////////////////////////////
#endif // DEVEL_DEVEL_DMACVICAR_PERSISTENTSTORAGE_H
XMLFilesBackend::XMLFilesBackend()
{
// check if the db exists
- if (!isDatabaseInitialized())
+ if (!isBackendInitialized())
{
DBG << "Database not initialized" << std::endl;
- initDatabaseForFirstTime();
- if (!isDatabaseInitialized())
+ initBackend();
+ // should be initialized now...
+ if (!isBackendInitialized())
DBG << "Error, cant init database" << std::endl;
else
DBG << "Database initialized" << std::endl;
}
}
+
bool
-XMLFilesBackend::isDatabaseInitialized()
+XMLFilesBackend::isBackendInitialized()
{
return exists( ZYPP_DB_DIR );
}
void
-XMLFilesBackend::initDatabaseForFirstTime()
+XMLFilesBackend::initBackend()
{
// FIXME duncan * handle exceptions
DBG << "Creating directory structure..." << std::endl;
create_directory( path(ZYPP_DB_DIR) / path("products") );
}
-std::string
-XMLFilesBackend::randomFileName() const
+void
+XMLFilesBackend::storeObject( Resolvable::Ptr resolvable )
{
- FILE *fp;
- char puffer[49];
- if ( (fp = popen("openssl rand -base64 48", "r")) == 0)
- {
- DBG << "mierda!" << std::endl;
- //ZYPP_THROW("put some message here");
- }
- fscanf(fp, "%s", &puffer);
- pclose(fp);
- return "blah";
+ std::string xml = castedToXML(resolvable);
+ std::string filename;
+ DBG << std::endl << xml << std::endl;
+ std::ofstream file;
+ // FIXME replace with path class of boost
+ filename += std::string(ZYPP_DB_DIR);
+ filename += "/";
+ filename += typeToString(resolvable, true);
+ filename += "/";
+ filename += resolvable->name();
+ DBG << filename << std::endl;
+ file.open(filename.c_str());
+ file << xml;
+ file.close();
}
-std::string XMLFilesBackend::fileNameForPatch( Patch::Ptr patch ) const
+void
+XMLFilesBackend::deleteObject( Resolvable::Ptr resolvable )
+{}
+
+std::list<Resolvable::Ptr>
+XMLFilesBackend::storedObjects()
{
- return patch->id();
+ return std::list<Resolvable::Ptr>();
}
-std::list<Patch::Ptr>
-XMLFilesBackend::installedPatches()
+std::list<Resolvable::Ptr>
+XMLFilesBackend::storedObjects(const Resolvable::Kind)
{
- return std::list<Patch::Ptr>();
+ return storedObjects();
}
-void
-XMLFilesBackend::storePatch( Patch::Ptr p )
+std::list<Resolvable::Ptr>
+XMLFilesBackend::storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match)
{
- std::string xml = toXML(p);
- DBG << std::endl << xml << std::endl;
- std::ofstream file;
- // FIXME replace with path class of boost
- file.open( (std::string(ZYPP_DB_DIR) + std::string("/patches/") + fileNameForPatch(p)).c_str() );
- file << xml;
- file.close();
+ return storedObjects();
}
-void
-XMLFilesBackend::insertTest()
+std::string
+XMLFilesBackend::randomFileName() const
{
-
+ FILE *fp;
+ char puffer[49];
+ if ( (fp = popen("openssl rand -base64 48", "r")) == 0)
+ {
+ DBG << "mierda!" << std::endl;
+ //ZYPP_THROW("put some message here");
+ }
+ fscanf(fp, "%s", &puffer);
+ pclose(fp);
+ return "blah";
}
///////////////////////////////////////////////////////////////////
XMLFilesBackend();
/** Dtor */
~XMLFilesBackend();
- void doTest();
- bool isDatabaseInitialized();
- void initDatabaseForFirstTime();
+ virtual void doTest();
- void insertTest();
+ /**
+ * is the storage backend initialized
+ */
+ virtual bool isBackendInitialized();
+ /**
+ * initialize the storage backend
+ */
+ virtual void initBackend();
+
+ /**
+ * Stores a Resolvable in the active backend.
+ */
+ virtual void storeObject( Resolvable::Ptr resolvable ) ;
+ /**
+ * Deletes a Resolvable from the active backend.
+ */
+ virtual void deleteObject( Resolvable::Ptr resolvable );
+ /**
+ * Deletes a Resolvable from the active backend.
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects();
+ /**
+ * Query for installed Resolvables of a certain kind
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind);
+ /**
+ * Query for installed Resolvables of a certain kind by name
+ * \a partial_match allows for text search.
+ */
+ virtual std::list<Resolvable::Ptr> storedObjects(const Resolvable::Kind, const std::string & name, bool partial_match = false);
+
+ protected:
std::string randomFileName() const;
- std::string fileNameForPatch( Patch::Ptr patch ) const;
- void storePatch( Patch::Ptr p );
- std::list<Patch::Ptr> installedPatches();
+
private:
class Private;
Private *d;
return out.str();
}
+
+
template<> // or constPtr?
std::string toXML( Message::Ptr obj )
{
return out.str();
}
+// or constPtr?
+std::string castedToXML( Resolvable::Ptr resolvable )
+{
+ stringstream out;
+ if ( isKind<Package>(resolvable) )
+ out << toXML(asKind<Package>(resolvable)) << std::endl;
+ if ( isKind<Patch>(resolvable) )
+ out << toXML(asKind<Patch>(resolvable)) << std::endl;
+ if ( isKind<Message>(resolvable) )
+ out << toXML(asKind<Message>(resolvable)) << std::endl;
+ if ( isKind<Script>(resolvable) )
+ out << toXML(asKind<Script>(resolvable)) << std::endl;
+ return out.str();
+}
+
+// or constPtr?
+std::string typeToString( Resolvable::Ptr resolvable, bool plural )
+{
+ stringstream out;
+ if ( isKind<Package>(resolvable) )
+ return plural ? "packages" : "package";
+ if ( isKind<Patch>(resolvable) )
+ return plural ? "patches" : "patch";
+ if ( isKind<Message>(resolvable) )
+ return plural ? "messages" : "message";
+ if ( isKind<Script>(resolvable) )
+ return plural ? "scripts" : "script";
+}
+
template<> // or constPtr?
std::string toXML( Patch::Ptr obj )
{
out << " <atoms>" << std::endl;
// I have a better idea to avoid the cast here (Michaels code in his tmp/)
Resolvable::Ptr one_atom = *it;
- if ( isKind<Package>(one_atom) )
- out << toXML(asKind<Package>(one_atom)) << std::endl;
- if ( isKind<Patch>(one_atom) )
- out << toXML(asKind<Patch>(one_atom)) << std::endl;
- if ( isKind<Message>(one_atom) )
- out << toXML(asKind<Message>(one_atom)) << std::endl;
- if ( isKind<Script>(one_atom) )
- out << toXML(asKind<Script>(one_atom)) << std::endl;
+ out << castedToXML(one_atom) << std::endl;
out << " </atoms>" << std::endl;
}
out << "</patch>" << std::endl;
template<> // or constPtr?
std::string toXML( const Dependencies dep );
+/**
+ * Serialize Resolvable properties
+ * NOTE: This wont serialize child classes properties
+ * Use castedToXML for that.
+ */
template<> // or constPtr?
std::string toXML( Resolvable::Ptr obj );
+/**
+ * Serialize properties based in the specific kind of the Resolvable
+ */
+std::string castedToXML( Resolvable::Ptr ret );
+
+/**
+ * lack of instrospection sucks
+ */
+std::string typeToString( Resolvable::Ptr resolvable, bool plural = false );
+
template<> // or constPtr?
std::string toXML( Package::Ptr obj );
throw *iter.errorStatus();
PersistentStorage backend;
- backend.storePatch(patch1);
+ backend.storeObject(patch1);
// test xml 2 object
std::string xml = toXML(patch1);