Patchwork [1,of,8] bundle2: add an integer id to part

login
register
mail settings
Submitter Pierre-Yves David
Date April 11, 2014, 10:05 p.m.
Message ID <e5ac0c58e81ac8fc28a8.1397253958@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/4284/
State Accepted
Headers show

Comments

Pierre-Yves David - April 11, 2014, 10:05 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1396336037 25200
#      Tue Apr 01 00:07:17 2014 -0700
# Node ID e5ac0c58e81ac8fc28a8a69ae0bc96b33acde5f4
# Parent  2778616de7ce64e92c1e6611b3493734be586f35
bundle2: add an integer id to part

For sending response to a pushed bundled, we need to link reply to initially
pushed parts. We introduce a part id for this purpose. This is a 32bits unique
integer stored in the header.

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -85,10 +85,13 @@  Binary format is as follow
 
     :typesize: (one byte)
 
     :typename: alphanumerical part name
 
+    :partid: A 32bits integer (unique in the bundle) that can be used to refer
+             to this part.
+
     :parameters:
 
         Part's parameter may have arbitraty content, the binary structure is::
 
             <mandatory-count><advisory-count><param-sizes><param-data>
@@ -152,10 +155,11 @@  from i18n import _
 _magicstring = 'HG20'
 
 _fstreamparamsize = '>H'
 _fpartheadersize = '>H'
 _fparttypesize = '>B'
+_fpartid = '>I'
 _fpayloadsize = '>I'
 _fpartparamcount = '>BB'
 
 def _makefpartparamsizes(nbparams):
     """return a struct format to read part parameter sizes
@@ -317,10 +321,12 @@  class bundle20(object):
 
     def addpart(self, part):
         """add a new part to the bundle2 container
 
         Parts contains the actuall applicative payload."""
+        assert part.id is None
+        part.id = len(self._parts) # very cheap counter
         self._parts.append(part)
 
     def getchunks(self):
         self.ui.debug('start emission of %s stream\n' % _magicstring)
         yield _magicstring
@@ -447,10 +453,12 @@  class unbundle20(object):
             return _unpack(format, data)
 
         typesize = unpackheader(_fparttypesize)[0]
         parttype = fromheader(typesize)
         self.ui.debug('part type: "%s"\n' % parttype)
+        partid = unpackheader(_fpartid)[0]
+        self.ui.debug('part id: "%s"\n' % partid)
         ## reading parameters
         # param count
         mancount, advcount = unpackheader(_fpartparamcount)
         self.ui.debug('part parameters: %i\n' % (mancount + advcount))
         # param size
@@ -476,10 +484,11 @@  class unbundle20(object):
             payload.append(self._readexact(payloadsize))
             payloadsize = self._unpack(_fpayloadsize)[0]
             self.ui.debug('payload chunk size: %i\n' % payloadsize)
         payload = ''.join(payload)
         current = part(parttype, manparams, advparams, data=payload)
+        current.id = partid
         return current
 
 
 class part(object):
     """A bundle2 part contains application level payload
@@ -488,20 +497,21 @@  class part(object):
     handler.
     """
 
     def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
                  data=''):
+        self.id = None
         self.type = parttype
         self.data = data
         self.mandatoryparams = mandatoryparams
         self.advisoryparams = advisoryparams
 
     def getchunks(self):
         #### header
         ## parttype
         header = [_pack(_fparttypesize, len(self.type)),
-                  self.type,
+                  self.type, _pack(_fpartid, self.id),
                  ]
         ## parameters
         # count
         manpar = self.mandatoryparams
         advpar = self.advisoryparams
diff --git a/tests/test-bundle2.t b/tests/test-bundle2.t
--- a/tests/test-bundle2.t
+++ b/tests/test-bundle2.t
@@ -319,15 +319,15 @@  Test part
   bundle part: "test:song"
   bundle part: "test:math"
   end of bundle
 
   $ cat ../parts.hg2
-  HG20\x00\x00\x00\r (esc)
-  test:empty\x00\x00\x00\x00\x00\x00\x00\r (esc)
-  test:empty\x00\x00\x00\x00\x00\x00\x00\x0c	test:song\x00\x00\x00\x00\x00\xb2Patali Dirapata, Cromda Cromda Ripalo, Pata Pata, Ko Ko Ko (esc)
+  HG20\x00\x00\x00\x11 (esc)
+  test:empty\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11 (esc)
+  test:empty\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x10	test:song\x00\x00\x00\x02\x00\x00\x00\x00\x00\xb2Patali Dirapata, Cromda Cromda Ripalo, Pata Pata, Ko Ko Ko (esc)
   Bokoro Dipoulito, Rondi Rondi Pepino, Pata Pata, Ko Ko Ko
-  Emana Karassoli, Loucra Loucra Ponponto, Pata Pata, Ko Ko Ko.\x00\x00\x00\x00\x00'	test:math\x02\x01\x02\x04\x01\x04\x07\x03pi3.14e2.72cookingraw\x00\x00\x00\x0242\x00\x00\x00\x00\x00\x00 (no-eol) (esc)
+  Emana Karassoli, Loucra Loucra Ponponto, Pata Pata, Ko Ko Ko.\x00\x00\x00\x00\x00+	test:math\x00\x00\x00\x03\x02\x01\x02\x04\x01\x04\x07\x03pi3.14e2.72cookingraw\x00\x00\x00\x0242\x00\x00\x00\x00\x00\x00 (no-eol) (esc)
 
 
   $ hg statbundle2 < ../parts.hg2
   options count: 0
   parts count:   4
@@ -351,25 +351,29 @@  Test part
   $ hg statbundle2 --debug < ../parts.hg2
   start processing of HG20 stream
   reading bundle2 stream parameters
   options count: 0
   start extraction of bundle2 parts
-  part header size: 13
+  part header size: 17
   part type: "test:empty"
+  part id: "0"
   part parameters: 0
   payload chunk size: 0
-  part header size: 13
+  part header size: 17
   part type: "test:empty"
+  part id: "1"
   part parameters: 0
   payload chunk size: 0
-  part header size: 12
+  part header size: 16
   part type: "test:song"
+  part id: "2"
   part parameters: 0
   payload chunk size: 178
   payload chunk size: 0
-  part header size: 39
+  part header size: 43
   part type: "test:math"
+  part id: "3"
   part parameters: 3
   payload chunk size: 2
   payload chunk size: 0
   part header size: 0
   end of bundle2 stream
@@ -398,32 +402,36 @@  Process the bundle
 
   $ hg unbundle2 --debug < ../parts.hg2
   start processing of HG20 stream
   reading bundle2 stream parameters
   start extraction of bundle2 parts
-  part header size: 13
+  part header size: 17
   part type: "test:empty"
+  part id: "0"
   part parameters: 0
   payload chunk size: 0
   ignoring unknown advisory part 'test:empty'
-  part header size: 13
+  part header size: 17
   part type: "test:empty"
+  part id: "1"
   part parameters: 0
   payload chunk size: 0
   ignoring unknown advisory part 'test:empty'
-  part header size: 12
+  part header size: 16
   part type: "test:song"
+  part id: "2"
   part parameters: 0
   payload chunk size: 178
   payload chunk size: 0
   found a handler for part 'test:song'
   The choir starts singing:
       Patali Dirapata, Cromda Cromda Ripalo, Pata Pata, Ko Ko Ko
       Bokoro Dipoulito, Rondi Rondi Pepino, Pata Pata, Ko Ko Ko
       Emana Karassoli, Loucra Loucra Ponponto, Pata Pata, Ko Ko Ko.
-  part header size: 39
+  part header size: 43
   part type: "test:math"
+  part id: "3"
   part parameters: 3
   payload chunk size: 2
   payload chunk size: 0
   ignoring unknown advisory part 'test:math'
   part header size: 0
@@ -530,11 +538,11 @@  Support for changegroup
   start of parts
   bundle part: "changegroup"
   end of bundle
 
   $ cat ../rev.hg2
-  HG20\x00\x00\x00\x0e\x0bchangegroup\x00\x00\x00\x00\x06\x19HG10UN\x00\x00\x00\xa42\xafv\x86\xd4\x03\xcfE\xb5\xd9_-p\xce\xbe\xa5\x87\xac\x80j_\xdd\xd9\x89W\xc8\xa5JMCm\xfe\x1d\xa9\xd8\x7f!\xa1\xb9{\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x002\xafv\x86\xd4\x03\xcfE\xb5\xd9_-p\xce\xbe\xa5\x87\xac\x80j\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00)6e1f4c47ecb533ffd0c8e52cdc88afb6cd39e20c (esc)
+  HG20\x00\x00\x00\x12\x0bchangegroup\x00\x00\x00\x00\x00\x00\x00\x00\x06\x19HG10UN\x00\x00\x00\xa42\xafv\x86\xd4\x03\xcfE\xb5\xd9_-p\xce\xbe\xa5\x87\xac\x80j_\xdd\xd9\x89W\xc8\xa5JMCm\xfe\x1d\xa9\xd8\x7f!\xa1\xb9{\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x002\xafv\x86\xd4\x03\xcfE\xb5\xd9_-p\xce\xbe\xa5\x87\xac\x80j\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00)6e1f4c47ecb533ffd0c8e52cdc88afb6cd39e20c (esc)
   \x00\x00\x00f\x00\x00\x00h\x00\x00\x00\x02D (esc)
   \x00\x00\x00i\x00\x00\x00j\x00\x00\x00\x01D\x00\x00\x00\xa4\x95 \xee\xa7\x81\xbc\xca\x16\xc1\xe1Z\xcc\x0b\xa1C5\xa0\xe8\xe5\xba\xcd\x01\x0b\x8c\xd9\x98\xf3\x98\x1aZ\x81\x15\xf9O\x8d\xa4\xabP`\x89\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x95 \xee\xa7\x81\xbc\xca\x16\xc1\xe1Z\xcc\x0b\xa1C5\xa0\xe8\xe5\xba\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00)4dece9c826f69490507b98c6383a3009b295837d (esc)
   \x00\x00\x00f\x00\x00\x00h\x00\x00\x00\x02E (esc)
   \x00\x00\x00i\x00\x00\x00j\x00\x00\x00\x01E\x00\x00\x00\xa2\xee\xa17Fy\x9a\x9e\x0b\xfd\x88\xf2\x9d<.\x9d\xc98\x9fRO$\xb68|\x8c\x8c\xae7\x17\x88\x80\xf3\xfa\x95\xde\xd3\xcb\x1c\xf7\x85\x95 \xee\xa7\x81\xbc\xca\x16\xc1\xe1Z\xcc\x0b\xa1C5\xa0\xe8\xe5\xba\xee\xa17Fy\x9a\x9e\x0b\xfd\x88\xf2\x9d<.\x9d\xc98\x9fRO\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00)365b93d57fdf4814e2b5911d6bacff2b12014441 (esc)
   \x00\x00\x00f\x00\x00\x00h\x00\x00\x00\x00\x00\x00\x00i\x00\x00\x00j\x00\x00\x00\x01G\x00\x00\x00\xa4\x02\xdeB\x19n\xbe\xe4.\xf2\x84\xb6x (esc)