Never delete a path from the public table.
authorAntony Chazapis <chazapis@gmail.com>
Thu, 19 Jan 2012 12:52:56 +0000 (14:52 +0200)
committerAntony Chazapis <chazapis@gmail.com>
Thu, 19 Jan 2012 12:52:56 +0000 (14:52 +0200)
Fixes #1803

pithos/backends/lib/sqlalchemy/public.py
pithos/backends/lib/sqlite/public.py
pithos/backends/modular.py

index 9ebdae3..2fa5517 100644 (file)
@@ -32,8 +32,8 @@
 # or implied, of GRNET S.A.
 
 from dbworker import DBWorker
-from sqlalchemy import Table, Column, String, Integer, MetaData
-from sqlalchemy.sql import select
+from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData
+from sqlalchemy.sql import and_, select
 from sqlalchemy.schema import Index
 
 
@@ -45,30 +45,39 @@ class Public(DBWorker):
         metadata = MetaData()
         columns=[]
         columns.append(Column('public_id', Integer, primary_key=True))
-        columns.append(Column('path', String(2048)))
+        columns.append(Column('path', String(2048), nullable=False))
+        columns.append(Column('active', Boolean, nullable=False, default=True))
         self.public = Table('public', metadata, *columns, mysql_engine='InnoDB', sqlite_autoincrement=True)
         # place an index on path
         Index('idx_public_path', self.public.c.path, unique=True)
         metadata.create_all(self.engine)
     
     def public_set(self, path):
-        s = self.public.select()
+        s = select([self.public.c.public_id])
         s = s.where(self.public.c.path == path)
         r = self.conn.execute(s)
-        public = r.fetchall()
+        row = r.fetchone()
         r.close()
-        if len(public) == 0:
+        if row:
+            s = self.public.update().where(self.public.c.public_id == row[0])
+            s = s.values(active=True)
+        else:
             s = self.public.insert()
-            r = self.conn.execute(s, path = path)
-            r.close()
+            s = s.values(path=path, active=True)
+        r = self.conn.execute(s)
+        r.close()
     
     def public_unset(self, path):
-        s = self.public.delete().where(self.public.c.path == path)
+        s = self.public.update()
+        s = s.where(self.public.c.path == path)
+        s = s.values(active=False)
         r = self.conn.execute(s)
         r.close()
     
     def public_get(self, path):
-        s = select([self.public.c.public_id], self.public.c.path == path)
+        s = select([self.public.c.public_id])
+        s = s.where(and_(self.public.c.path == path,
+                                        self.public.c.active == True))
         r = self.conn.execute(s)
         row = r.fetchone()
         r.close()
@@ -77,7 +86,9 @@ class Public(DBWorker):
         return None
     
     def public_path(self, public):
-        s = select([self.public.c.path], self.public.c.public_id == public)
+        s = select([self.public.c.path])
+        s = s.where(and_(self.public.c.public_id == public,
+                                        self.public.c.active == True))
         r = self.conn.execute(s)
         row = r.fetchone()
         r.close()
index 23f00c1..fb67b15 100644 (file)
@@ -43,20 +43,23 @@ class Public(DBWorker):
         
         execute(""" create table if not exists public
                           ( public_id integer primary key autoincrement,
-                            path      text ) """)
+                            path      text not null,
+                            active    boolean not null default 1 ) """)
         execute(""" create unique index if not exists idx_public_path
                     on public(path) """)
     
     def public_set(self, path):
         q = "insert or ignore into public (path) values (?)"
         self.execute(q, (path,))
+        q = "update public set active = 1 where path = ?"
+        self.execute(q, (path,))
     
     def public_unset(self, path):
-        q = "delete from public where path = ?"
+        q = "update public set active = 0 where path = ?"
         self.execute(q, (path,))
     
     def public_get(self, path):
-        q = "select public_id from public where path = ?"
+        q = "select public_id from public where path = ? and active = 1"
         self.execute(q, (path,))
         row = self.fetchone()
         if row:
@@ -64,7 +67,7 @@ class Public(DBWorker):
         return None
     
     def public_path(self, public):
-        q = "select path from public where public_id = ?"
+        q = "select path from public where public_id = ? and active = 1"
         self.execute(q, (public,))
         row = self.fetchone()
         if row:
index e2c5d3a..62f1e09 100644 (file)
@@ -645,6 +645,7 @@ class ModularBackend(BaseBackend):
     @backend_method
     def get_uuid(self, user, uuid):
         """Return the (account, container, name) for the UUID given."""
+        
         logger.debug("get_uuid: %s", uuid)
         info = self.node.latest_uuid(uuid)
         if info is None:
@@ -657,6 +658,7 @@ class ModularBackend(BaseBackend):
     @backend_method
     def get_public(self, user, public):
         """Return the (account, container, name) for the public id given."""
+        
         logger.debug("get_public: %s", public)
         if public is None or public < ULTIMATE_ANSWER:
             raise NameError