]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/magicfolderdb.py
Refactor is_new_file.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / magicfolderdb.py
index 187941b85ff0734228e01ee930a77b841eee294c..0db1857c26bda82dc2859cab0bbd5889931879c7 100644 (file)
@@ -1,5 +1,6 @@
 
 import sys
+from collections import namedtuple
 
 from allmydata.util.dbutil import get_db, DBError
 
@@ -16,12 +17,12 @@ CREATE TABLE local_files
  path                VARCHAR(1024) PRIMARY KEY, -- UTF-8 filename relative to local magic folder dir
  -- note that size is before mtime and ctime here, but after in function parameters
  size                INTEGER,                   -- ST_SIZE, or NULL if the file has been deleted
- mtime               REAL,                      -- ST_MTIME
- ctime               REAL,                      -- ST_CTIME
+ mtime               NUMBER,                      -- ST_MTIME
+ ctime               NUMBER,                      -- ST_CTIME
  version             INTEGER,
  last_uploaded_uri   VARCHAR(256) UNIQUE,       -- URI:CHK:...
  last_downloaded_uri VARCHAR(256) UNIQUE,       -- URI:CHK:...
- last_downloaded_timestamp REAL
+ last_downloaded_timestamp TIMESTAMP
 );
 """
 
@@ -42,6 +43,7 @@ def get_magicfolderdb(dbfile, stderr=sys.stderr,
         print >>stderr, e
         return None
 
+PathEntry = namedtuple('PathEntry', 'size mtime ctime version last_uploaded_uri last_downloaded_uri last_downloaded_timestamp')
 
 class MagicFolderDB(object):
     VERSION = 1
@@ -51,20 +53,25 @@ class MagicFolderDB(object):
         self.connection = connection
         self.cursor = connection.cursor()
 
-    def check_file_db_exists(self, path):
-        """I will tell you if a given file has an entry in my database or not
-        by returning True or False.
+    def get_db_entry(self, relpath_u):
+        """
+        Retrieve the entry in the database for a given path, or return None
+        if there is no such entry.
         """
         c = self.cursor
-        c.execute("SELECT size,mtime,ctime"
+        c.execute("SELECT size, mtime, ctime, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp"
                   " FROM local_files"
                   " WHERE path=?",
-                  (path,))
+                  (relpath_u,))
         row = self.cursor.fetchone()
         if not row:
-            return False
+            return None
         else:
-            return True
+            (size, mtime, ctime, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp) = row
+            return PathEntry(size=size, mtime=mtime, ctime=ctime, version=version,
+                             last_uploaded_uri=last_uploaded_uri,
+                             last_downloaded_uri=last_downloaded_uri,
+                             last_downloaded_timestamp=last_downloaded_timestamp)
 
     def get_all_relpaths(self):
         """
@@ -75,65 +82,17 @@ class MagicFolderDB(object):
         rows = self.cursor.fetchall()
         return set([r[0] for r in rows])
 
-    def get_last_downloaded_uri(self, relpath_u):
-        """
-        Return the last downloaded uri recorded in the magic folder db.
-        If none are found then return None.
-        """
-        c = self.cursor
-        c.execute("SELECT last_downloaded_uri"
-                  " FROM local_files"
-                  " WHERE path=?",
-                  (relpath_u,))
-        row = self.cursor.fetchone()
-        if not row:
-            return None
-        else:
-            return row[0]
-
-    def get_local_file_version(self, relpath_u):
-        """
-        Return the version of a local file tracked by our magic folder db.
-        If no db entry is found then return None.
-        """
-        c = self.cursor
-        c.execute("SELECT version"
-                  " FROM local_files"
-                  " WHERE path=?",
-                  (relpath_u,))
-        row = self.cursor.fetchone()
-        if not row:
-            return None
-        else:
-            return row[0]
-
-    def did_upload_version(self, filecap, relpath_u, version, pathinfo):
-        print "did_upload_version(%r, %r, %r, %r)" % (filecap, relpath_u, version, pathinfo)
+    def did_upload_version(self, relpath_u, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp, pathinfo):
+        print "%r.did_upload_version(%r, %r, %r, %r, %r, %r)" % (self, relpath_u, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp, pathinfo)
         try:
             print "insert"
-            self.cursor.execute("INSERT INTO local_files VALUES (?,?,?,?,?,?)",
-                                (relpath_u, pathinfo.size, pathinfo.mtime, pathinfo.ctime, version, filecap, pathinfo.mtime))
+            self.cursor.execute("INSERT INTO local_files VALUES (?,?,?,?,?,?,?,?)",
+                                (relpath_u, pathinfo.size, pathinfo.mtime, pathinfo.ctime, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp))
         except (self.sqlite_module.IntegrityError, self.sqlite_module.OperationalError):
             print "err... update"
             self.cursor.execute("UPDATE local_files"
-                                " SET size=?, mtime=?, ctime=?, version=?, last_downloaded_uri=?, last_downloaded_timestamp=?"
+                                " SET size=?, mtime=?, ctime=?, version=?, last_uploaded_uri=?, last_downloaded_uri=?, last_downloaded_timestamp=?"
                                 " WHERE path=?",
-                                (pathinfo.size, pathinfo.mtime, pathinfo.ctime, version, filecap, pathinfo.mtime, relpath_u))
+                                (pathinfo.size, pathinfo.mtime, pathinfo.ctime, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp, relpath_u))
         self.connection.commit()
-        print "commited"
-
-    def is_new_file(self, pathinfo, relpath_u):
-        """
-        Returns true if the file's current pathinfo (size, mtime, and ctime) has
-        changed from the pathinfo previously stored in the db.
-        """
-        #print "is_new_file(%r, %r)" % (pathinfo, relpath_u)
-        c = self.cursor
-        c.execute("SELECT size, mtime, ctime"
-                  " FROM local_files"
-                  " WHERE path=?",
-                  (relpath_u,))
-        row = self.cursor.fetchone()
-        if not row:
-            return True
-        return (pathinfo.size, pathinfo.mtime, pathinfo.ctime) != row
+        print "committed"