[PATCH 19 of 21 V2] speedy: turn index create functions into update functions

Tomasz Kleczek tkleczek at fb.com
Thu Dec 13 20:52:31 CST 2012


# HG changeset patch
# User Tomasz Kleczek <tkleczek at fb.com>
# Date 1355291043 28800
# Node ID 0d2191815306cddea7852060925cc5f3def44ed0
# Parent  1c7e6abfe5949c7134ba498e6ae5ae3b74031997
speedy: turn index create functions into update functions

This is a step in making indices persistent.

Until now, the index could only be created from scratch. This patch
converts all functions that were creating indices into the functions
that are used to update them.

Each such function now takes a current index and a list of 'new' changes
and returns a list of index entries that should be updated.

diff --git a/hgext/speedy/index.py b/hgext/speedy/index.py
--- a/hgext/speedy/index.py
+++ b/hgext/speedy/index.py
@@ -8,34 +8,39 @@
 Indices are data structures in the form of key-value mappings used
 by the server to speed up computations.
 
-Each index has a corresponding maker function that takes as parameter
-an iterable with change contexs from which the index is to be created
-and returns the index in a form of a dict.
+Each index has a corresponding updater function that:
+    * Takes as parameters a current index and an iterable with new
+      change contexs.
+    * Computes the entries that have to be updated/inserted to reflect
+      the changes in the new chagesets.
+    * Returns a list of (key, value) pairs of such entries.
 """
 
-def makeuserchgs(ctxs):
-    """Return the `userchgs` index.
+def userchgsentries(index, ctxs):
+    """Return new entries for the `userchgs` index.
 
     `userchgs` is keyed by username, with each value being a list
         of changes commited by that user.
     """
     newentries = {}
     for ctx in ctxs:
-        newentries.setdefault(ctx.user(), []).append(ctx.node())
-    return newentries
+        user = ctx.user()
+        if user not in newentries:
+            newentries[user] = index.get(user, [])
+        newentries[user].append(ctx.node())
+    return list(newentries.iteritems())
 
 
-def makechgdate(ctxs):
-    """Return the `chgdate` index.
+def chgdateentries(index, ctxs):
+    """Return new entries for the `chgdate` index.
 
     `chgdate` is keyed by change id, with each value being a commit date
         of that change.
     """
-    return dict([(ctx.node(), ctx.date()[0]) for ctx in ctxs])
+    return [(ctx.node(), ctx.date()[0]) for ctx in ctxs]
 
-
-def makefilechgs(ctxs):
-    """Return the `filechgs` index.
+def filechgsentries(index, ctxs):
+    """Return new entries for the `filechgs` index.
 
     `filechgs` is keyed by paths (files or directories), with each value
         being a list of commits touching this path (for direcories it is
@@ -51,15 +56,17 @@
             for i in range(len(dirs)):
                 paths.add('/'.join(dirs[0:i+1]))
         for path in paths:
-            filechgs.setdefault(path, []).append(ctx.node())
-    return filechgs
+            if path not in filechgs:
+                filechgs[path] = index.get(path, [])
+            filechgs[path].append(ctx.node())
+    return list(filechgs.iteritems())
 
-def makefiles(ctxs):
-    """Return the `files` index.
+def filesentries(index, ctxs):
+    """Return new entries for the `files` index.
 
     `files` is keyed by file name, with each value being an empty string
     """
     files = set()
     for ctx in ctxs:
         files.update(ctx.files())
-    return dict([(fn, '') for fn in files])
+    return [(fn, '') for fn in files]
diff --git a/hgext/speedy/server.py b/hgext/speedy/server.py
--- a/hgext/speedy/server.py
+++ b/hgext/speedy/server.py
@@ -122,16 +122,16 @@
         return self._literalpath(matchingfiles)
 
 indicecfg = {
-    'userchgs': index.makeuserchgs,
-    'chgdate': index.makechgdate,
-    'filechgs': index.makefilechgs,
-    'files': index.makefiles,
+    'userchgs': index.userchgsentries,
+    'chgdate': index.chgdateentries,
+    'filechgs': index.filechgsentries,
+    'files': index.filesentries,
 }
 
 def makeserver(repo):
     """Return an initialized metaserver instance."""
     ctxs = [repo[r] for r in xrange(0, len(repo))]
-    indices = dict([(name, create(ctxs)) for name, create in
+    indices = dict([(name, dict(newentries({}, ctxs))) for name, newentries in
         indicecfg.iteritems()])
     return metaserver(repo, indices)
 


More information about the Mercurial-devel mailing list