/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3735.26.1 by John Arbash Meinel
Write a pyrex extension for computing search keys.
1
# Copyright (C) 2009 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Python implementation of _search_key functions, etc."""
18
19
import zlib
20
import struct
21
22
23
def _crc32(bit):
24
    # Depending on python version and platform, zlib.crc32 will return either a
25
    # signed (<= 2.5 >= 3.0) or an unsigned (2.5, 2.6).
26
    # http://docs.python.org/library/zlib.html recommends using a mask to force
27
    # an unsigned value to ensure the same numeric value (unsigned) is obtained
28
    # across all python versions and platforms.
29
    # Note: However, on 32-bit platforms this causes an upcast to PyLong, which
30
    #       are generally slower than PyInts. However, if performance becomes
31
    #       critical, we should probably write the whole thing as an extension
32
    #       anyway.
33
    #       Though we really don't need that 32nd bit of accuracy. (even 2**24
34
    #       is probably enough node fan out for realistic trees.)
35
    return zlib.crc32(bit)&0xFFFFFFFF
36
37
38
def _search_key_16(key):
39
    """Map the key tuple into a search key string which has 16-way fan out."""
40
    return '\x00'.join(['%08X' % _crc32(bit) for bit in key])
41
42
43
def _search_key_255(key):
44
    """Map the key tuple into a search key string which has 255-way fan out.
45
46
    We use 255-way because '\n' is used as a delimiter, and causes problems
47
    while parsing.
48
    """
49
    bytes = '\x00'.join([struct.pack('>L', _crc32(bit)) for bit in key])
50
    return bytes.replace('\n', '_')
51
52