Mercurial > hg
comparison mercurial/exchange.py @ 30187:3e86261bf110
exchange: refactor APIs to obtain bundle data (API)
Currently, exchange.getbundle() returns either a cg1unpacker or a
util.chunkbuffer (in the case of bundle2). This is kinda OK, as
both expose a .read() to consumers. However, localpeer.getbundle()
has code inferring what the response type is based on arguments and
converts the util.chunkbuffer returned in the bundle2 case to a
bundle2.unbundle20 instance. This is a sign that the API for
exchange.getbundle() is not ideal because it doesn't consistently
return an "unbundler" instance.
In addition, unbundlers mask the fact that there is an underlying
generator of changegroup data. In both cg1 and bundle2, this generator
is being fed into a util.chunkbuffer so it can be re-exposed as a
file object.
util.chunkbuffer is a nice abstraction. However, it should only be
used "at the edges." This is because keeping data as a generator is
more efficient than converting it to a chunkbuffer, especially if we
convert that chunkbuffer back to a generator (as is the case in some
code paths currently).
This patch refactors exchange.getbundle() into
exchange.getbundlechunks(). The new API returns an iterator of chunks
instead of a file-like object.
Callers of exchange.getbundle() have been updated to use the new API.
There is a minor change of behavior in test-getbundle.t. This is
because `hg debuggetbundle` isn't defining bundlecaps. As a result,
a cg1 data stream and unpacker is being produced. This is getting fed
into a new bundle20 instance via bundle2.writebundle(), which uses
a backchannel mechanism between changegroup generation to add the
"nbchanges" part parameter. I never liked this backchannel mechanism
and I plan to remove it someday. `hg bundle` still produces the
"nbchanges" part parameter, so there should be no user-visible
change of behavior. I consider this "regression" a bug in
`hg debuggetbundle`. And that bug is captured by an existing
"TODO" in the code to use bundle2 capabilities.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 10:38:52 -0700 |
parents | a76d5ba7ac43 |
children | 318a24b52eeb |
comparison
equal
deleted
inserted
replaced
30186:f7ed5af31242 | 30187:3e86261bf110 |
---|---|
1530 def bundle2requested(bundlecaps): | 1530 def bundle2requested(bundlecaps): |
1531 if bundlecaps is not None: | 1531 if bundlecaps is not None: |
1532 return any(cap.startswith('HG2') for cap in bundlecaps) | 1532 return any(cap.startswith('HG2') for cap in bundlecaps) |
1533 return False | 1533 return False |
1534 | 1534 |
1535 def getbundle(repo, source, heads=None, common=None, bundlecaps=None, | 1535 def getbundlechunks(repo, source, heads=None, common=None, bundlecaps=None, |
1536 **kwargs): | 1536 **kwargs): |
1537 """return a full bundle (with potentially multiple kind of parts) | 1537 """Return chunks constituting a bundle's raw data. |
1538 | 1538 |
1539 Could be a bundle HG10 or a bundle HG20 depending on bundlecaps | 1539 Could be a bundle HG10 or a bundle HG20 depending on bundlecaps |
1540 passed. For now, the bundle can contain only changegroup, but this will | 1540 passed. |
1541 changes when more part type will be available for bundle2. | 1541 |
1542 | 1542 Returns an iterator over raw chunks (of varying sizes). |
1543 This is different from changegroup.getchangegroup that only returns an HG10 | |
1544 changegroup bundle. They may eventually get reunited in the future when we | |
1545 have a clearer idea of the API we what to query different data. | |
1546 | |
1547 The implementation is at a very early stage and will get massive rework | |
1548 when the API of bundle is refined. | |
1549 """ | 1543 """ |
1550 usebundle2 = bundle2requested(bundlecaps) | 1544 usebundle2 = bundle2requested(bundlecaps) |
1551 # bundle10 case | 1545 # bundle10 case |
1552 if not usebundle2: | 1546 if not usebundle2: |
1553 if bundlecaps and not kwargs.get('cg', True): | 1547 if bundlecaps and not kwargs.get('cg', True): |
1555 | 1549 |
1556 if kwargs: | 1550 if kwargs: |
1557 raise ValueError(_('unsupported getbundle arguments: %s') | 1551 raise ValueError(_('unsupported getbundle arguments: %s') |
1558 % ', '.join(sorted(kwargs.keys()))) | 1552 % ', '.join(sorted(kwargs.keys()))) |
1559 outgoing = _computeoutgoing(repo, heads, common) | 1553 outgoing = _computeoutgoing(repo, heads, common) |
1560 return changegroup.getchangegroup(repo, source, outgoing, | 1554 bundler = changegroup.getbundler('01', repo, bundlecaps) |
1561 bundlecaps=bundlecaps) | 1555 return changegroup.getsubsetraw(repo, outgoing, bundler, source) |
1562 | 1556 |
1563 # bundle20 case | 1557 # bundle20 case |
1564 b2caps = {} | 1558 b2caps = {} |
1565 for bcaps in bundlecaps: | 1559 for bcaps in bundlecaps: |
1566 if bcaps.startswith('bundle2='): | 1560 if bcaps.startswith('bundle2='): |
1574 for name in getbundle2partsorder: | 1568 for name in getbundle2partsorder: |
1575 func = getbundle2partsmapping[name] | 1569 func = getbundle2partsmapping[name] |
1576 func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps, | 1570 func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps, |
1577 **kwargs) | 1571 **kwargs) |
1578 | 1572 |
1579 return util.chunkbuffer(bundler.getchunks()) | 1573 return bundler.getchunks() |
1580 | 1574 |
1581 @getbundle2partsgenerator('changegroup') | 1575 @getbundle2partsgenerator('changegroup') |
1582 def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None, | 1576 def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None, |
1583 b2caps=None, heads=None, common=None, **kwargs): | 1577 b2caps=None, heads=None, common=None, **kwargs): |
1584 """add a changegroup part to the requested bundle""" | 1578 """add a changegroup part to the requested bundle""" |