changeset 23083:ee45f5c2ffcc stable

setdiscovery: extract sample limitation in a `_limitsample` function We need to reuse this logic for the initial query. We extract it in a function to unsure sample limiting is applied consistently in all cases.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Mon, 27 Oct 2014 17:40:32 +0100
parents 0fc4686de1d7
children 3ef893520a85
files mercurial/setdiscovery.py
diffstat 1 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/setdiscovery.py	Fri Oct 24 17:24:46 2014 -0500
+++ b/mercurial/setdiscovery.py	Mon Oct 27 17:40:32 2014 +0100
@@ -105,14 +105,19 @@
     # update from roots
     _updatesample(dag.inverse(), nodes, sample, always)
     assert sample
-    if len(sample) > desiredlen:
-        sample = set(random.sample(sample, desiredlen))
-    elif len(sample) < desiredlen:
+    sample = _limitsample(sample, desiredlen)
+    if len(sample) < desiredlen:
         more = desiredlen - len(sample)
         sample.update(random.sample(list(nodes - sample - always), more))
     sample.update(always)
     return sample
 
+def _limitsample(sample, desiredlen):
+    """return a random subset of sample of at most desiredlen item"""
+    if len(sample) > desiredlen:
+        sample = set(random.sample(sample, desiredlen))
+    return sample
+
 def findcommonheads(ui, local, remote,
                     initialsamplesize=100,
                     fullsamplesize=200,