##// END OF EJS Templates
sidedata: adjust string for python3...
marmoute -
r43318:c348d829 default
parent child Browse files
Show More
@@ -1,96 +1,96 b''
1 # sidedata.py - Logic around store extra data alongside revlog revisions
1 # sidedata.py - Logic around store extra data alongside revlog revisions
2 #
2 #
3 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net)
3 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net)
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7 """core code for "sidedata" support
7 """core code for "sidedata" support
8
8
9 The "sidedata" are stored alongside the revision without actually being part of
9 The "sidedata" are stored alongside the revision without actually being part of
10 its content and not affecting its hash. It's main use cases is to cache
10 its content and not affecting its hash. It's main use cases is to cache
11 important information related to a changesets.
11 important information related to a changesets.
12
12
13 The current implementation is experimental and subject to changes. Do not rely
13 The current implementation is experimental and subject to changes. Do not rely
14 on it in production.
14 on it in production.
15
15
16 Sidedata are stored in the revlog itself, withing the revision rawtext. They
16 Sidedata are stored in the revlog itself, withing the revision rawtext. They
17 are inserted, removed from it using the flagprocessors mechanism. The following
17 are inserted, removed from it using the flagprocessors mechanism. The following
18 format is currently used::
18 format is currently used::
19
19
20 initial header:
20 initial header:
21 <number of sidedata; 2 bytes>
21 <number of sidedata; 2 bytes>
22 sidedata (repeated N times):
22 sidedata (repeated N times):
23 <sidedata-key; 2 bytes>
23 <sidedata-key; 2 bytes>
24 <sidedata-entry-length: 4 bytes>
24 <sidedata-entry-length: 4 bytes>
25 <sidedata-content-sha1-digest: 20 bytes>
25 <sidedata-content-sha1-digest: 20 bytes>
26 <sidedata-content; X bytes>
26 <sidedata-content; X bytes>
27 normal raw text:
27 normal raw text:
28 <all bytes remaining in the rawtext>
28 <all bytes remaining in the rawtext>
29
29
30 This is a simple and effective format. It should be enought to experiment with
30 This is a simple and effective format. It should be enought to experiment with
31 the concept.
31 the concept.
32 """
32 """
33
33
34 from __future__ import absolute_import
34 from __future__ import absolute_import
35
35
36 import hashlib
36 import hashlib
37 import struct
37 import struct
38
38
39 from .. import error
39 from .. import error
40
40
41 ## sidedata type constant
41 ## sidedata type constant
42 # reserve a block for testing purposes.
42 # reserve a block for testing purposes.
43 SD_TEST1 = 1
43 SD_TEST1 = 1
44 SD_TEST2 = 2
44 SD_TEST2 = 2
45 SD_TEST3 = 3
45 SD_TEST3 = 3
46 SD_TEST4 = 4
46 SD_TEST4 = 4
47 SD_TEST5 = 5
47 SD_TEST5 = 5
48 SD_TEST6 = 6
48 SD_TEST6 = 6
49 SD_TEST7 = 7
49 SD_TEST7 = 7
50
50
51 # internal format constant
51 # internal format constant
52 SIDEDATA_HEADER = struct.Struct('>H')
52 SIDEDATA_HEADER = struct.Struct(r'>H')
53 SIDEDATA_ENTRY = struct.Struct('>HL20s')
53 SIDEDATA_ENTRY = struct.Struct(r'>HL20s')
54
54
55 def sidedatawriteprocessor(rl, text, sidedata):
55 def sidedatawriteprocessor(rl, text, sidedata):
56 sidedata = list(sidedata.items())
56 sidedata = list(sidedata.items())
57 sidedata.sort()
57 sidedata.sort()
58 rawtext = [SIDEDATA_HEADER.pack(len(sidedata))]
58 rawtext = [SIDEDATA_HEADER.pack(len(sidedata))]
59 for key, value in sidedata:
59 for key, value in sidedata:
60 digest = hashlib.sha1(value).digest()
60 digest = hashlib.sha1(value).digest()
61 rawtext.append(SIDEDATA_ENTRY.pack(key, len(value), digest))
61 rawtext.append(SIDEDATA_ENTRY.pack(key, len(value), digest))
62 for key, value in sidedata:
62 for key, value in sidedata:
63 rawtext.append(value)
63 rawtext.append(value)
64 rawtext.append(bytes(text))
64 rawtext.append(bytes(text))
65 return ''.join(rawtext), False
65 return ''.join(rawtext), False
66
66
67 def sidedatareadprocessor(rl, text):
67 def sidedatareadprocessor(rl, text):
68 sidedata = {}
68 sidedata = {}
69 offset = 0
69 offset = 0
70 nbentry, = SIDEDATA_HEADER.unpack(text[:SIDEDATA_HEADER.size])
70 nbentry, = SIDEDATA_HEADER.unpack(text[:SIDEDATA_HEADER.size])
71 offset += SIDEDATA_HEADER.size
71 offset += SIDEDATA_HEADER.size
72 dataoffset = SIDEDATA_HEADER.size + (SIDEDATA_ENTRY.size * nbentry)
72 dataoffset = SIDEDATA_HEADER.size + (SIDEDATA_ENTRY.size * nbentry)
73 for i in range(nbentry):
73 for i in range(nbentry):
74 nextoffset = offset + SIDEDATA_ENTRY.size
74 nextoffset = offset + SIDEDATA_ENTRY.size
75 key, size, storeddigest = SIDEDATA_ENTRY.unpack(text[offset:nextoffset])
75 key, size, storeddigest = SIDEDATA_ENTRY.unpack(text[offset:nextoffset])
76 offset = nextoffset
76 offset = nextoffset
77 # read the data associated with that entry
77 # read the data associated with that entry
78 nextdataoffset = dataoffset + size
78 nextdataoffset = dataoffset + size
79 entrytext = text[dataoffset:nextdataoffset]
79 entrytext = text[dataoffset:nextdataoffset]
80 readdigest = hashlib.sha1(entrytext).digest()
80 readdigest = hashlib.sha1(entrytext).digest()
81 if storeddigest != readdigest:
81 if storeddigest != readdigest:
82 raise error.SidedataHashError(key, storeddigest, readdigest)
82 raise error.SidedataHashError(key, storeddigest, readdigest)
83 sidedata[key] = entrytext
83 sidedata[key] = entrytext
84 dataoffset = nextdataoffset
84 dataoffset = nextdataoffset
85 text = text[dataoffset:]
85 text = text[dataoffset:]
86 return text, True, sidedata
86 return text, True, sidedata
87
87
88 def sidedatarawprocessor(rl, text):
88 def sidedatarawprocessor(rl, text):
89 # side data modifies rawtext and prevent rawtext hash validation
89 # side data modifies rawtext and prevent rawtext hash validation
90 return False
90 return False
91
91
92 processors = (
92 processors = (
93 sidedatareadprocessor,
93 sidedatareadprocessor,
94 sidedatawriteprocessor,
94 sidedatawriteprocessor,
95 sidedatarawprocessor,
95 sidedatarawprocessor,
96 )
96 )
General Comments 0
You need to be logged in to leave comments. Login now