/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/btree_index.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from __future__ import absolute_import
21
21
 
22
 
import cStringIO
23
 
 
24
 
from bzrlib.lazy_import import lazy_import
 
22
from .lazy_import import lazy_import
25
23
lazy_import(globals(), """
26
24
import bisect
27
25
import math
29
27
import zlib
30
28
""")
31
29
 
32
 
from bzrlib import (
 
30
from . import (
33
31
    chunk_writer,
34
32
    debug,
35
33
    errors,
41
39
    trace,
42
40
    transport,
43
41
    )
44
 
from bzrlib.index import _OPTION_NODE_REFS, _OPTION_KEY_ELEMENTS, _OPTION_LEN
 
42
from .index import _OPTION_NODE_REFS, _OPTION_KEY_ELEMENTS, _OPTION_LEN
 
43
from .sixish import (
 
44
    BytesIO,
 
45
    )
45
46
 
46
47
 
47
48
_BTSIGNATURE = "B+Tree Graph Index 2\n"
74
75
    def finish_node(self, pad=True):
75
76
        byte_lines, _, padding = self.writer.finish()
76
77
        if self.nodes == 0:
77
 
            self.spool = cStringIO.StringIO()
 
78
            self.spool = BytesIO()
78
79
            # padded note:
79
80
            self.spool.write("\x00" * _RESERVED_HEADER_BYTES)
80
81
        elif self.nodes == 1:
397
398
                                    self.reference_lists)
398
399
            self._add_key(string_key, line, rows, allow_optimize=allow_optimize)
399
400
        for row in reversed(rows):
400
 
            pad = (type(row) != _LeafBuilderRow)
 
401
            pad = (not isinstance(row, _LeafBuilderRow))
401
402
            row.finish_node(pad=pad)
402
403
        lines = [_BTSIGNATURE]
403
404
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
408
409
        if row_lengths and row_lengths[-1] > 1:
409
410
            result = tempfile.NamedTemporaryFile(prefix='bzr-index-')
410
411
        else:
411
 
            result = cStringIO.StringIO()
 
412
            result = BytesIO()
412
413
        result.writelines(lines)
413
414
        position = sum(map(len, lines))
414
415
        root_row = True
430
431
            position = 0 # Only the root row actually has an offset
431
432
            copied_len = osutils.pumpfile(row.spool, result)
432
433
            if copied_len != (row.nodes - 1) * _PAGE_SIZE:
433
 
                if type(row) != _LeafBuilderRow:
 
434
                if not isinstance(row, _LeafBuilderRow):
434
435
                    raise AssertionError("Incorrect amount of data copied"
435
436
                        " expected: %d, got: %d"
436
437
                        % ((row.nodes - 1) * _PAGE_SIZE,
575
576
                    key_dict = dicts.pop(-1)
576
577
                    # can't be empty or would not exist
577
578
                    item, value = key_dict.iteritems().next()
578
 
                    if type(value) == dict:
 
579
                    if isinstance(value, dict):
579
580
                        # push keys
580
581
                        dicts.extend(key_dict.itervalues())
581
582
                    else:
635
636
 
636
637
    def all_items(self):
637
638
        """Return a sorted list of (key, (value, refs)) items"""
638
 
        items = self.items()
639
 
        items.sort()
 
639
        items = sorted(self.items())
640
640
        return items
641
641
 
642
642
    def all_keys(self):
643
643
        """Return a sorted list of all keys."""
644
 
        keys = self.keys()
645
 
        keys.sort()
 
644
        keys = sorted(self.keys())
646
645
        return keys
647
646
 
648
647
 
717
716
    def __eq__(self, other):
718
717
        """Equal when self and other were created with the same parameters."""
719
718
        return (
720
 
            type(self) == type(other) and
 
719
            isinstance(self, type(other)) and
721
720
            self._transport == other._transport and
722
721
            self._name == other._name and
723
722
            self._size == other._size)
1360
1359
            self._get_root_node()
1361
1360
        # TODO: only access nodes that can satisfy the prefixes we are looking
1362
1361
        # for. For now, to meet API usage (as this function is not used by
1363
 
        # current bzrlib) just suck the entire index and iterate in memory.
 
1362
        # current breezy) just suck the entire index and iterate in memory.
1364
1363
        nodes = {}
1365
1364
        if self.node_ref_lists:
1366
1365
            if self._key_length == 1:
1429
1428
                    key_dict = dicts.pop(-1)
1430
1429
                    # can't be empty or would not exist
1431
1430
                    item, value = key_dict.iteritems().next()
1432
 
                    if type(value) == dict:
 
1431
                    if isinstance(value, dict):
1433
1432
                        # push keys
1434
1433
                        dicts.extend(key_dict.itervalues())
1435
1434
                    else:
1601
1600
_gcchk_factory = _LeafNode
1602
1601
 
1603
1602
try:
1604
 
    from bzrlib import _btree_serializer_pyx as _btree_serializer
 
1603
    from breezy import _btree_serializer_pyx as _btree_serializer
1605
1604
    _gcchk_factory = _btree_serializer._parse_into_chk
1606
 
except ImportError, e:
 
1605
except ImportError as e:
1607
1606
    osutils.failed_to_load_extension(e)
1608
 
    from bzrlib import _btree_serializer_py as _btree_serializer
 
1607
    from breezy import _btree_serializer_py as _btree_serializer