
    6hH                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSK	J
r
JrJr  SS jrSS
 jr " S S5      r\r\R"                  " SS\S	S9r   SS jrg)a;  
Algorithm works in the following way. You have two repository: local and
remote. They both contains a DAG of changelists.

The goal of the discovery protocol is to find one set of node *common*,
the set of nodes shared by local and remote.

One of the issue with the original protocol was latency, it could
potentially require lots of roundtrips to discover that the local repo was a
subset of remote (which is a very common case, you usually have few changes
compared to upstream, while upstream probably had lots of development).

The new protocol only requires one interface for the remote repo: `known()`,
which given a set of changelists tells you if they are present in the DAG.

The algorithm then works as follow:

 - We will be using three sets, `common`, `missing`, `unknown`. Originally
 all nodes are in `unknown`.
 - Take a sample from `unknown`, call `remote.known(sample)`
   - For each node that remote knows, move it and all its ancestors to `common`
   - For each node that remote doesn't know, move it and all its descendants
   to `missing`
 - Iterate until `unknown` is empty

There are a couple optimizations, first is instead of starting with a random
sample of missing, start by sending all heads, in the case where the local
repo is a subset, you computed the answer in one round trip.

Then you can do something similar to the bisecting strategy used when
finding faulty changesets. Instead of random samples, you can try picking
nodes that will maximize the number of nodes that will be
classified with it (since all ancestors or descendants will be marked as well).
    )annotationsN   )_)nullrev)errorpolicyutilc                   0 n[         R                  " U5      n[        5       nSnU(       a  UR                  5       n	X;   a  M  UR	                  U	S5      n
X:  a  US-  nX:X  a(  UR                  U	5        U(       a  [        U5      U:  a  gUR                  U	5        U" U	5       HB  nU[        :w  d  M  U (       a  X;   d  M  UR	                  XS-   5        UR                  U5        MD     U(       a  M  gg)a3  update an existing sample to match the expected size

The sample is updated with revs exponentially distant from each head of the
<revs> set. (H~1, H~2, H~4, H~8, etc).

If a target size is specified, the sampling will stop once this size is
reached. Otherwise sampling will happen until roots of the <revs> set are
reached.

:revs:  set of revs we want to discover (if None, assume the whole dag)
:heads: set of DAG head revs
:sample: a sample to update
:parentfn: a callable to resolve parents for a revision
:quicksamplesize: optional target size of the sampler      N)	collectionsdequesetpopleft
setdefaultaddlenr   append)revsheadssampleparentfnquicksamplesizedistvisitseenfactorcurrdps               8/usr/lib/python3/dist-packages/mercurial/setdiscovery.py_updatesampler!   9   s     De$E5DF
}}<OOD!$:aKF;JJtCK?$B$AG|TQYq5)Q   %    Tc                    [        U 5      U::  a  U $ [        U 5      n U(       a  [        [        R                  " X5      5      $ U R                  5         [        U SU 5      $ )zreturn a random subset of sample of at most desiredlen item.

If randomize is False, though, a deterministic subset is returned.
This is meant for integration tests.
N)r   listr   randomr   sort)r   
desiredlen	randomizes      r    _limitsampler)   _   sP     6{j &\F6==455
KKMvkz"##r"   c                  t    \ rS rSrSrSS jrS rS rS rS r	S r
\S	 5       rS
 rS rS rS rS rS rSrg)partialdiscoveryn   aF  an object representing ongoing discovery

Feed with data from the remote repository, this object keep track of the
current set of changeset in various states:

- common:    revs also known remotely
- undecided: revs we don't have information on yet
- missing:   revs missing remotely
(all tracked revisions are known locally)
c                    Xl         X l        UR                  R                  5       U l        S U l        [        5       U l        S U l        X0l	        X@l
        g N)_repo_targetheads	changelogincrementalmissingrevs_common
_undecidedr   missing_childrenmap_respectsizer(   )selfrepotargetheadsrespectsizer(   s        r    __init__partialdiscovery.__init__z   sD    
'~~<<>u '"r"   c                    U R                   R                  U5        U R                  b&  U R                   R                  U R                  5        gg)zregister nodes known as commonN)r3   addbasesr4   removeancestorsfrom)r8   commonss     r    
addcommonspartialdiscovery.addcommons   s9    g&??&LL,,T__= 'r"   c                    U R                   R                  SXR                  5      nU(       a7  U R                  R	                  U5        U R                  R                  U5        gg)zregister some nodes as missings   %ld::%ldN)r/   r   	undecidedr5   updatedifference_update)r8   missings
newmissings      r    addmissingspartialdiscovery.addmissings   sF    ZZ__[(NNK
LL
+NN,,Z8 r"   c                    [        5       n[        5       nU H0  u  pEU(       a  UR                  U5        M  UR                  U5        M2     U(       a  U R                  U5        U(       a  U R                  U5        gg)z*consume an iterable of (rev, known) tuplesN)r   r   rB   rJ   )r8   r   commonr5   revknowns         r    addinfopartialdiscovery.addinfo   s]    % JC

3C 	 !
 OOF#W% r"   c                6    U R                   R                  5       $ )z6return True is we have any clue about the remote state)r3   hasbasesr8   s    r    hasinfopartialdiscovery.hasinfo   s    ||$$&&r"   c                N    U R                   SL=(       a    U R                   (       + $ )z1True if all the necessary data have been gatheredN)r4   rT   s    r    
iscompletepartialdiscovery.iscomplete   s    d*B4??/BBr"   c                    U R                   b  U R                   $ [        U R                  R                  U R                  5      5      U l         U R                   $ r.   )r4   r   r3   missingancestorsr0   rT   s    r    rE   partialdiscovery.undecided   sA    ??&??"dll;;D<M<MNOr"   c                0    S[        U R                  5      0$ )NrE   )r   rE   rT   s    r    statspartialdiscovery.stats   s    T^^,
 	
r"   c                6    U R                   R                  5       $ )z!the heads of the known common set)r3   
basesheadsrT   s    r    commonheadspartialdiscovery.commonheads   s     ||&&((r"   c                h   ^ U R                   R                  R                  R                  mU4S jnU$ )Nc                   > T" U 5      SS $ )N       )rgetrevs    r    
getparents3partialdiscovery._parentsgetter.<locals>.getparents   s    !9Qq>!r"   )r/   r1   index__getitem__)r8   rk   rj   s     @r    _parentsgetterpartialdiscovery._parentsgetter   s+    %%++77	" r"   c                Z   U R                   b  U R                   R                  $ 0 =U l         nU R                  5       nU R                  n[	        U5       HI  n/ X'   U" U5       H6  nU[
        :X  a  M  UR                  U5      nUc  M%  UR                  U5        M8     MK     UR                  $ r.   )r6   rn   ro   rE   sortedr   getr   )r8   children
parentrevsr   rN   prevcs          r    _childrengetter partialdiscovery._childrengetter   s    (
 $$000 (*)H((*
~~$<C HM"37?LL&=HHSM (	   ###r"   c                   U R                   n[        U5      U::  a  [        U5      $ [        U R                  R                  SU5      5      n[        U5      U:  a  [        XBU R                  S9$ [        SXU R                  5       US9  U$ )ztakes a quick sample of size <size>

It is meant for initial sampling and focuses on querying heads and close
ancestors of heads.

:headrevs: set of head revisions in local DAG to consider
:size: the maximum size of the sample
   heads(%ld)r(   N)r   )
rE   r   r$   r   r/   r   r)   r(   r!   ro   )r8   headrevssizer   r   s        r    takequicksample partialdiscovery.takequicksample   sz     ~~t9:TZZ__]D9:v;$GG(D$7$7$94	
 r"   c           	        U R                   n[        U5      U::  a  [        U5      $ U R                  n[	        UR                  SU5      5      nU R                  5       nUR                  5       n[        X7XV5        [	        UR                  SU5      5      nU R                  5       n	[        X8XY5        U(       d   eU R                  (       d(  [        U[        [        U5      [        U5      5      5      n[        XRU R                  S9n[        U5      U:  aw  U[        U5      -
  n
[        X5-
  5      nU R                  (       a'  UR                  [         R"                  " X5      5        U$ UR%                  5         UR                  US U
 5        U$ )Nr{   s
   roots(%ld)r|   )rE   r   r$   r/   r   r   ro   copyr!   rx   r7   maxminr)   r(   rF   r%   r   r&   )r8   r}   r~   r   r9   r   ru   	revsheads	revsrootschildrenrevsmoretakefroms               r    takefullsamplepartialdiscovery.takefullsample   s9   ~~t9:zzTYY}d34((*
 KKM	dv: 		-67	++-dv<v  tSYY@ADfdnnEv;#f+%DDM*H~~fmmH;<  huo.r"   )r6   r3   r/   r7   r0   r4   r5   r(   NT)__name__
__module____qualname____firstlineno____doc__r<   rB   rJ   rP   rU   rX   propertyrE   r^   rb   ro   rx   r   r   __static_attributes__rh   r"   r    r+   r+   n   sX    	#>9&'C  

)$:*r"   r+   	discoveryPartialDiscovery)memberdefaultpyo3c                   [        U R                  SS5      5      nUb  SUS'   [        R                  " 5       nSnUR                  n	U	R
                  n
U	R                  nUb  U Vs/ s H
  o" U5      PM     nnO+U	R                  5        Vs/ s H  o[        :w  d  M  UPM     nnU R                  SS5      nU R                  SS5      nU R                  SS5      nU(       Ga"  UR                  (       a  [        UU5      n[        U5      nOUnU R                  S	5        US
-  nUR                  5        nUR!                  S0 5      nUb  US==   [#        U5      -  ss'   UR!                  SSU Vs/ s H  nU
" U5      PM     sn05      nSSS5        WR%                  5       WR%                  5       nnUb  S
US'   U	R'                  5       [        :X  a1  UU	R(                  /:w  a  U	R(                  /SU4$ U	R(                  /S/ 4$ O;UR                  5        nUR!                  S0 5      nSSS5        WR%                  5       nU R+                  [-        S5      5        / nU H-  nUU	R(                  :X  a  M   UR/                  U" U5      5        M/     U(       a  [#        U5      [#        U5      :X  a  U R                  S5        USU4$ [#        W5      [#        U5      :X  aG  [5        W5      (       a7  U R7                  [-        S5      5        U Vs/ s H  nU
" U5      PM     nnUSU4$ UR8                  R                  nU" SS5      nU=(       a    UR                  (       + nU" SS5      nU=(       d    UR                  (       + nU R                  SS5      n U	R:                  R<                  (       a  [>        n!O[@        n!U!" XUU S9n"U(       a,  U"RC                  U5        U"RE                  [G        WW5      5        U(       + n#U RI                  [-        S5      [-        S5      S9n$U"RK                  5       (       Gd  U#(       d  U"RM                  5       (       aW  U#(       a  U R7                  [-        S5      5        OU R                  S5        U"RN                  n%Un&U(       a  [Q        UU-  5      nOU R                  S5        U"RR                  n%Un&U%" UU&5      nUS
-  nU$RU                  U5        U"RW                  5       n'U R                  SUU'S   [#        U5      4-  5        [        U5      nUR                  5        nUb  US==   [#        U5      -  ss'   UR!                  SSU Vs/ s H  nU
" U5      PM     sn05      R%                  5       nSSS5        Sn#U"RE                  [G        UW5      5        U"RK                  5       (       d  GM  U"RY                  5       n([        R                  " 5       U-
  n)U$R[                  5         U R                  S UU)4-  5        S!n*[]        U(5      []        U5      -
  n+U R_                  S"U*[#        U(5      [#        U+5      UU)5        Ub  XS'   U((       da  UU	R(                  /:w  aP  U(       a  [0        R`                  " [-        S#5      5      eU Rc                  [-        S$5      5        U	R(                  1SU4$ UU	R(                  /:g  n,U( Vs1 s H  nU
" U5      iM     n(nU(U,U4$ s  snf s  snf s  snf ! , (       d  f       GNx= f! , (       d  f       GN= f! [0        R2                   a     GM  f = fs  snf s  snf ! , (       d  f       GN= fs  snf )%zReturn a tuple (common, anyincoming, remoteheads) used to identify
missing nodes from or in remote.

The audit argument is an optional dictionnary that a caller can pass. it
will be updated with extra data about the discovery, this is useful for
debug.
s   devels   discovery.grow-sample.rateNr   s   total-queriess   discovery.exchange-headss   discovery.sample-size.initials   discovery.sample-sizes   query 1; heads
r   s   headss   knowns   nodess   total-roundtripsTFs   searching for changes
s   all remote heads known locally
s$   all local changesets known remotely
s   discovery.grow-samples   discovery.grow-sample.dynamics   discovery.randomizer|   s	   searchings   queries)units   sampling from both directions
s   taking initial sample
s   taking quick initial sample
s2   query %i; still undecided: %i, sample size is: %i
rE   s   %d total queries in %.4fs
sD   found %d common and %d unknown server heads, %d roundtrips in %.4fs
s	   discoverys   repository is unrelateds!   warning: repository is unrelated
)2floatconfigr	   timerr1   noderN   r}   r   
configbool	configintlimitedargumentsr)   r$   debugcommandexecutorcallcommandr   resulttiprevnullidstatusr   r   r   LookupErrorallnoteuirm   rust_ext_compatr+   pure_partialdiscoveryrB   rP   zipmakeprogressrX   rU   r   intr   rF   r^   rb   completer   logAbortwarn)-r   localremoteabortwhenunrelatedancestorsofauditsamplegrowthstart
roundtripsclclnodeclrevnownheadsrN   initial_head_exchangeinitialsamplesizefullsamplesizer   efheadsri   fknownsrvheadhashesyesnoknownsrvheadsr   ownheadhashesr   grow_sampledynamic_samplehard_limit_sampler(   pddiscofullprogress
samplefunc
targetsizer^   r   elapsedmsgr5   anyincomings-                                                r    findcommonheadsr     s     8-JKLL"#JJLEJ	BWWFFFE&12kE!Hk2#%;;=C=C7NC=CMM(4OPX/OP\\(,DENh ""!(,=>F&\FF
$%a
##%]]8R0F &'3v;6']]&9&Qvay&9F	 &  &}}u)*E%&99;'!+		{D-77II;r)) " ##%]]8R0F & IIa*+,M299	  t-  }]!33HH89 %66v;#h-'CJJGGA>?@0891VAYM9 $55
 $$JX'?@K=f&=&="=K*JKN+Fv/F/FGh(>?I	xx"u 1YGE 	'c&%()$$DqQz]CH  5==??<=>34--J'J!$^l%B!C HH56..J*JHj1a

#
B5-s6{;<	
 f##% &'3v;6'MM&9&Qvay&9
 fh  & c&%()Q   T  FjjlU"GHH+z7.CCD	%  &kC..GFF<c&k3w<WM%/!"m		{2++a :;<<GGA;<=YYK
 	
  BII;.K!'(AfQiF(;--k 3CR : &%, &%     		 :z : &%P )sx   &[-[2[24=[<1[7[<\4\ 
\<2+]]/]]7[<<
\
\ \98\9]
])r   r   )TNN)r   
__future__r   r   r%   i18nr   r   r    r   r   r	   r!   r)   r+   r   
importrustr   rh   r"   r    <module>r      ss   !F #     # L$c cL ) $$*4D4  
r.r"   