##// END OF EJS Templates
pvec: add an explicit type hint to help pytype...
Augie Fackler -
r43780:8e89b6e1 default
parent child Browse files
Show More
@@ -1,227 +1,228 b''
1 1 # pvec.py - probabilistic vector clocks for Mercurial
2 2 #
3 3 # Copyright 2012 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 '''
9 9 A "pvec" is a changeset property based on the theory of vector clocks
10 10 that can be compared to discover relatedness without consulting a
11 11 graph. This can be useful for tasks like determining how a
12 12 disconnected patch relates to a repository.
13 13
14 14 Currently a pvec consist of 448 bits, of which 24 are 'depth' and the
15 15 remainder are a bit vector. It is represented as a 70-character base85
16 16 string.
17 17
18 18 Construction:
19 19
20 20 - a root changeset has a depth of 0 and a bit vector based on its hash
21 21 - a normal commit has a changeset where depth is increased by one and
22 22 one bit vector bit is flipped based on its hash
23 23 - a merge changeset pvec is constructed by copying changes from one pvec into
24 24 the other to balance its depth
25 25
26 26 Properties:
27 27
28 28 - for linear changes, difference in depth is always <= hamming distance
29 29 - otherwise, changes are probably divergent
30 30 - when hamming distance is < 200, we can reliably detect when pvecs are near
31 31
32 32 Issues:
33 33
34 34 - hamming distance ceases to work over distances of ~ 200
35 35 - detecting divergence is less accurate when the common ancestor is very close
36 36 to either revision or total distance is high
37 37 - this could probably be improved by modeling the relation between
38 38 delta and hdist
39 39
40 40 Uses:
41 41
42 42 - a patch pvec can be used to locate the nearest available common ancestor for
43 43 resolving conflicts
44 44 - ordering of patches can be established without a DAG
45 45 - two head pvecs can be compared to determine whether push/pull/merge is needed
46 46 and approximately how many changesets are involved
47 47 - can be used to find a heuristic divergence measure between changesets on
48 48 different branches
49 49 '''
50 50
51 51 from __future__ import absolute_import, division
52 52
53 53 from .node import nullrev
54 54 from . import (
55 55 pycompat,
56 56 util,
57 57 )
58 58
59 59 _size = 448 # 70 chars b85-encoded
60 60 _bytes = _size // 8
61 61 _depthbits = 24
62 62 _depthbytes = _depthbits // 8
63 63 _vecbytes = _bytes - _depthbytes
64 64 _vecbits = _vecbytes * 8
65 65 _radius = (_vecbits - 30) // 2 # high probability vectors are related
66 66
67 67
68 68 def _bin(bs):
69 69 '''convert a bytestring to a long'''
70 70 v = 0
71 71 for b in bs:
72 72 v = v * 256 + ord(b)
73 73 return v
74 74
75 75
76 76 def _str(v, l):
77 # type: (int, int) -> bytes
77 78 bs = b""
78 79 for p in pycompat.xrange(l):
79 80 bs = pycompat.bytechr(v & 255) + bs
80 81 v >>= 8
81 82 return bs
82 83
83 84
84 85 def _split(b):
85 86 '''depth and bitvec'''
86 87 return _bin(b[:_depthbytes]), _bin(b[_depthbytes:])
87 88
88 89
89 90 def _join(depth, bitvec):
90 91 return _str(depth, _depthbytes) + _str(bitvec, _vecbytes)
91 92
92 93
93 94 def _hweight(x):
94 95 c = 0
95 96 while x:
96 97 if x & 1:
97 98 c += 1
98 99 x >>= 1
99 100 return c
100 101
101 102
102 103 _htab = [_hweight(x) for x in pycompat.xrange(256)]
103 104
104 105
105 106 def _hamming(a, b):
106 107 '''find the hamming distance between two longs'''
107 108 d = a ^ b
108 109 c = 0
109 110 while d:
110 111 c += _htab[d & 0xFF]
111 112 d >>= 8
112 113 return c
113 114
114 115
115 116 def _mergevec(x, y, c):
116 117 # Ideally, this function would be x ^ y ^ ancestor, but finding
117 118 # ancestors is a nuisance. So instead we find the minimal number
118 119 # of changes to balance the depth and hamming distance
119 120
120 121 d1, v1 = x
121 122 d2, v2 = y
122 123 if d1 < d2:
123 124 d1, d2, v1, v2 = d2, d1, v2, v1
124 125
125 126 hdist = _hamming(v1, v2)
126 127 ddist = d1 - d2
127 128 v = v1
128 129 m = v1 ^ v2 # mask of different bits
129 130 i = 1
130 131
131 132 if hdist > ddist:
132 133 # if delta = 10 and hdist = 100, then we need to go up 55 steps
133 134 # to the ancestor and down 45
134 135 changes = (hdist - ddist + 1) // 2
135 136 else:
136 137 # must make at least one change
137 138 changes = 1
138 139 depth = d1 + changes
139 140
140 141 # copy changes from v2
141 142 if m:
142 143 while changes:
143 144 if m & i:
144 145 v ^= i
145 146 changes -= 1
146 147 i <<= 1
147 148 else:
148 149 v = _flipbit(v, c)
149 150
150 151 return depth, v
151 152
152 153
153 154 def _flipbit(v, node):
154 155 # converting bit strings to longs is slow
155 156 bit = (hash(node) & 0xFFFFFFFF) % _vecbits
156 157 return v ^ (1 << bit)
157 158
158 159
159 160 def ctxpvec(ctx):
160 161 '''construct a pvec for ctx while filling in the cache'''
161 162 r = ctx.repo()
162 163 if not util.safehasattr(r, "_pveccache"):
163 164 r._pveccache = {}
164 165 pvc = r._pveccache
165 166 if ctx.rev() not in pvc:
166 167 cl = r.changelog
167 168 for n in pycompat.xrange(ctx.rev() + 1):
168 169 if n not in pvc:
169 170 node = cl.node(n)
170 171 p1, p2 = cl.parentrevs(n)
171 172 if p1 == nullrev:
172 173 # start with a 'random' vector at root
173 174 pvc[n] = (0, _bin((node * 3)[:_vecbytes]))
174 175 elif p2 == nullrev:
175 176 d, v = pvc[p1]
176 177 pvc[n] = (d + 1, _flipbit(v, node))
177 178 else:
178 179 pvc[n] = _mergevec(pvc[p1], pvc[p2], node)
179 180 bs = _join(*pvc[ctx.rev()])
180 181 return pvec(util.b85encode(bs))
181 182
182 183
183 184 class pvec(object):
184 185 def __init__(self, hashorctx):
185 186 if isinstance(hashorctx, str):
186 187 self._bs = hashorctx
187 188 self._depth, self._vec = _split(util.b85decode(hashorctx))
188 189 else:
189 190 self._vec = ctxpvec(hashorctx)
190 191
191 192 def __str__(self):
192 193 return self._bs
193 194
194 195 def __eq__(self, b):
195 196 return self._vec == b._vec and self._depth == b._depth
196 197
197 198 def __lt__(self, b):
198 199 delta = b._depth - self._depth
199 200 if delta < 0:
200 201 return False # always correct
201 202 if _hamming(self._vec, b._vec) > delta:
202 203 return False
203 204 return True
204 205
205 206 def __gt__(self, b):
206 207 return b < self
207 208
208 209 def __or__(self, b):
209 210 delta = abs(b._depth - self._depth)
210 211 if _hamming(self._vec, b._vec) <= delta:
211 212 return False
212 213 return True
213 214
214 215 def __sub__(self, b):
215 216 if self | b:
216 217 raise ValueError(b"concurrent pvecs")
217 218 return self._depth - b._depth
218 219
219 220 def distance(self, b):
220 221 d = abs(b._depth - self._depth)
221 222 h = _hamming(self._vec, b._vec)
222 223 return max(d, h)
223 224
224 225 def near(self, b):
225 226 dist = abs(b.depth - self._depth)
226 227 if dist > _radius or _hamming(self._vec, b._vec) > _radius:
227 228 return False
General Comments 0
You need to be logged in to leave comments. Login now