Implemented the generic interface and implemented the generic Resolvable
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 13 Jan 2006 16:46:56 +0000 (16:46 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 13 Jan 2006 16:46:56 +0000 (16:46 +0000)
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.

devel/devel.dmacvicar/Backend.h
devel/devel.dmacvicar/PersistentStorage.cc
devel/devel.dmacvicar/PersistentStorage.h
devel/devel.dmacvicar/XMLFilesBackend.cc
devel/devel.dmacvicar/XMLFilesBackend.h
devel/devel.dmacvicar/serialize.cc
devel/devel.dmacvicar/serialize.h
devel/devel.dmacvicar/test2.cc

index f6146a7166a3cb772a06eacd44ef3c2348921586..093d84341f3b7349442724085b03081013be9e3e 100644 (file)
@@ -38,11 +38,39 @@ namespace zypp
                        /** 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;
index 261f25c1adf790a49021e05738bd4746637348f8..6a53e026033174a3d9fab96dd6c27da9bf5a1543 100644 (file)
@@ -73,15 +73,36 @@ void PersistentStorage::doTest()
 }
 
 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);
 }
 
 /******************************************************************
index 0973c6a8aa17e773d1f02ca40d187e2ca62edb10..7865b807ce5ca42c02f180fa8312f2f4a1b38c0d 100644 (file)
 ///////////////////////////////////////////////////////////////////
 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
index fb7e6ad4fb81254784d0e7a3ee95456195630478..b824f6b78b1e792a051b704ad5df7f47a14b4a7f 100644 (file)
@@ -61,11 +61,12 @@ class XMLFilesBackend::Private
 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;
@@ -76,14 +77,15 @@ XMLFilesBackend::XMLFilesBackend()
        }
 }
 
+
 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;
@@ -93,48 +95,60 @@ XMLFilesBackend::initDatabaseForFirstTime()
   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";
 }
 
 ///////////////////////////////////////////////////////////////////
index 2e63ea0304ba71d8fc6ad1854b4cc21072e91fbf..bc84ad5bfee9d23b151c6e0780f8e93cf566acb1 100644 (file)
@@ -40,15 +40,42 @@ public:
   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;
index 32ab3b89ca44baaceeb844b68f6fdc721836eeb6..16ca46dd08f7197b68c36ba9701b719a56a523fe 100644 (file)
@@ -162,6 +162,8 @@ std::string toXML( Script::Ptr obj )
   return out.str();
 }
 
+
+
 template<> // or constPtr?
 std::string toXML( Message::Ptr obj )
 {
@@ -174,6 +176,35 @@ 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 )
 {
@@ -188,14 +219,7 @@ 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;
index 597f7995c083267e4ef073f46fbbf954c22f8ecb..bcd6a0bbf110c04a386e73e48dd002e278296183 100644 (file)
@@ -59,9 +59,24 @@ std::string toXML( const CapSet caps );
 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 );
 
index e61d267d156377ff95847fc1635cba25fcd4c56a..c84c1ec08e122e78cb0b5a7c3e3402135bbd564d 100644 (file)
@@ -73,7 +73,7 @@ int main()
                throw *iter.errorStatus();
 
   PersistentStorage backend;
-  backend.storePatch(patch1);
+  backend.storeObject(patch1);
 
   // test xml 2 object
   std::string xml = toXML(patch1);