Rev 6: Use sqlite cache in home directory for faster access. in file:///data/jelmer/bzr-global-log/trunk/

Jelmer Vernooij jelmer at samba.org
Tue Nov 20 18:22:46 GMT 2007


At file:///data/jelmer/bzr-global-log/trunk/

------------------------------------------------------------
revno: 6
revision-id:jelmer at samba.org-20071105033740-91rpwjvbgfixmk2u
parent: jelmer at samba.org-20071105025326-lsj3pct1eepgmqng
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: trunk
timestamp: Mon 2007-11-05 04:37:40 +0100
message:
  Use sqlite cache in home directory for faster access.
modified:
  __init__.py                    __init__.py-20071029004222-avdgq0mc7n1q6q8f-1
  cache.py                       db.py-20071105012118-v51dlgcjf12dagnc-1
=== modified file '__init__.py'
--- a/__init__.py	2007-11-05 02:53:26 +0000
+++ b/__init__.py	2007-11-05 03:37:40 +0000
@@ -14,14 +14,19 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from bzrlib import ui
+from bzrlib import osutils, ui
 from bzrlib.branch import Branch
+from bzrlib.config import config_dir
 from bzrlib.commands import Command, register_command
 from bzrlib.option import ListOption, Option
 from bzrlib.log import (log_formatter, log_formatter_registry, show_log, 
                         LineLogFormatter, LogRevision)
 
 
+def get_cache_db_path():
+    return osutils.pathjoin(config_dir(), "global-log.db")
+
+
 class cmd_global_log(Command):
     """Log global revisions."""
 
@@ -42,7 +47,7 @@
             location_list = ["."]
 
         if cache:
-            repository = RevisionCachingRepository()
+            repository = RevisionCachingRepository(get_cache_db_path())
         else:
             repository = SimpleStackingRepository()
 

=== modified file 'cache.py'
--- a/cache.py	2007-11-05 02:53:26 +0000
+++ b/cache.py	2007-11-05 03:37:40 +0000
@@ -17,6 +17,7 @@
 import bzrlib
 from bzrlib.errors import NoSuchRevision
 from bzrlib.repository import Repository
+from bzrlib.revision import Revision
 from bzrlib.trace import warning
 
 from stack import SimpleStackingRepository
@@ -46,39 +47,69 @@
 
 class RevisionCache:
 
-    def __init__(self, location=":memory:"):
+    def __init__(self, location=None):
+        if location is None:
+            location = ":memory:"
         self.db = sqlite3.connect(location)
         self.db.executescript("""
-            CREATE TABLE revision (revid text, author text, committer text, 
+            CREATE TABLE IF NOT EXISTS revision (revid text, committer text, 
                                    timestamp integer, timezone integer, 
                                    message text);
-            CREATE TABLE revision_location (revid text, branch text);
-            CREATE TABLE revision_parent (child_revid text, parent_revid text);
-            CREATE TABLE revision_property (revid text, name text, value text);
+            CREATE UNIQUE INDEX IF NOT EXISTS idx_revision_revid ON revision (revid);
+            CREATE INDEX IF NOT EXISTS idx_timestamp ON revision (timestamp);
+            CREATE TABLE IF NOT EXISTS revision_location (revid text, repository text);
+            CREATE UNIQUE INDEX IF NOT EXISTS idx_revision_location ON revision_location (revid, repository);
+            CREATE TABLE IF NOT EXISTS revision_parent (child_revid text, parent_revid text);
+            CREATE UNIQUE INDEX IF NOT EXISTS idx_revparent ON revision_parent (child_revid, parent_revid);
+            CREATE TABLE IF NOT EXISTS revision_property (revid text, name text, value text);
+            CREATE UNIQUE INDEX IF NOT EXISTS idx_revprop_name ON revision_property (revid, name);
         """)
 
     def get_parents(self, revid):
         raise NotImplementedError(self.get_parents)
 
     def add_revision_location(self, revid, repository_location):
-        pass
+        self.db.execute("REPLACE INTO revision_location(revid, repository) VALUES (?, ?)", (revid, repository_location))
+        self.db.commit()
 
     def add_revision(self, rev):
-        pass
+        self.db.execute("""INSERT INTO revision (revid, committer, 
+                                               timestamp, timezone, message)
+                         VALUES (?, ?, ?, ?, ?)""",
+                         (rev.revision_id, rev.committer,
+                          rev.timestamp, rev.timezone, rev.message))
+        for name, value in rev.properties.items():
+            self.db.execute("INSERT INTO revision_property (revid, name, value) VALUES (?, ?, ?)", (rev.revision_id, name, value))
+        for p in rev.parent_ids:
+            self.db.execute("INSERT INTO revision_parent (child_revid, parent_revid) VALUES (?, ?)", (rev.revision_id, p))
+        self.db.commit()
 
     def get_revision(self, revid):
-        raise NoSuchRevision(revid, self)
-
-    def get_log_revision(self, rev):
-        raise NotImplementedError(self.get_log_revision)
+        row = self.db.execute("SELECT committer, timestamp, timezone, message FROM revision WHERE revid=?", (revid,)).fetchone()
+        if row is None:
+            raise NoSuchRevision(revid, self)
+        rev = Revision(revid)
+        rev.committer = row[0]
+        rev.timestamp = int(row[1])
+        rev.timezone = int(row[2])
+        rev.message = row[3]
+        rev.properties = dict(self.db.execute("SELECT name, value FROM revision_property WHERE revid=?", (revid,)).fetchall())
+        rev.parent_ids = map(lambda x: x[0], self.db.execute("SELECT parent_revid FROM revision_parent WHERE child_revid = ?", (revid,)).fetchall())
+        return rev
+
+    def get_revision_locations(self, revid):
+        return map(lambda x: x[0], self.db.execute("SELECT repository FROM revision_location WHERE revid=?", (revid,)).fetchall())
+
+    def __del__(self):
+        self.db.close()
 
 
 class RevisionCachingRepository(SimpleStackingRepository):
     """Repository implementation that uses a revision cache if possible."""
 
-    def __init__(self):
+    def __init__(self, cache_location=None):
         super(RevisionCachingRepository, self).__init__()
-        self.cache = RevisionCache()
+        self.cache = RevisionCache(cache_location)
 
     def _get_revisions(self, revids):
         for revid in revids:




More information about the bazaar-commits mailing list