/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/chk_map.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2017-06-02 11:26:27 UTC
  • mfrom: (6621.27.5 1089352-sni-support)
  • Revision ID: breezy.the.bot@gmail.com-20170602112627-jbvjcm9czx7gt3gb
Add SNI support.

Merged from https://code.launchpad.net/~jelmer/brz/sni-support/+merge/324979

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
37
37
 
38
38
"""
39
39
 
 
40
from __future__ import absolute_import
 
41
 
40
42
import heapq
41
43
import threading
42
44
 
43
 
from bzrlib import lazy_import
 
45
from . import lazy_import
44
46
lazy_import.lazy_import(globals(), """
45
 
from bzrlib import (
 
47
from breezy import (
46
48
    errors,
47
 
    versionedfile,
48
49
    )
49
50
""")
50
 
from bzrlib import (
 
51
from . import (
 
52
    errors,
51
53
    lru_cache,
52
54
    osutils,
53
55
    registry,
54
56
    static_tuple,
55
57
    trace,
56
58
    )
57
 
from bzrlib.static_tuple import StaticTuple
 
59
from .static_tuple import StaticTuple
58
60
 
59
61
# approx 4MB
60
62
# If each line is 50 bytes, and you have 255 internal pages, with 255-way fan
90
92
_INTERESTING_NEW_SIZE = 50
91
93
# If a ChildNode shrinks by more than this amount, we check for a remap
92
94
_INTERESTING_SHRINKAGE_LIMIT = 20
93
 
# If we delete more than this many nodes applying a delta, we check for a remap
94
 
_INTERESTING_DELETES_LIMIT = 5
95
95
 
96
96
 
97
97
def _search_key_plain(key):
135
135
            into the map; if old_key is not None, then the old mapping
136
136
            of old_key is removed.
137
137
        """
138
 
        delete_count = 0
 
138
        has_deletes = False
139
139
        # Check preconditions first.
140
140
        as_st = StaticTuple.from_sequence
141
 
        new_items = set([as_st(key) for (old, key, value) in delta
142
 
                         if key is not None and old is None])
 
141
        new_items = {as_st(key) for (old, key, value) in delta
 
142
                         if key is not None and old is None}
143
143
        existing_new = list(self.iteritems(key_filter=new_items))
144
144
        if existing_new:
145
145
            raise errors.InconsistentDeltaDelta(delta,
148
148
        for old, new, value in delta:
149
149
            if old is not None and old != new:
150
150
                self.unmap(old, check_remap=False)
151
 
                delete_count += 1
 
151
                has_deletes = True
152
152
        for old, new, value in delta:
153
153
            if new is not None:
154
154
                self.map(new, value)
155
 
        if delete_count > _INTERESTING_DELETES_LIMIT:
156
 
            trace.mutter("checking remap as %d deletions", delete_count)
 
155
        if has_deletes:
157
156
            self._check_remap()
158
157
        return self._save()
159
158
 
160
159
    def _ensure_root(self):
161
160
        """Ensure that the root node is an object not a key."""
162
 
        if type(self._root_node) is StaticTuple:
 
161
        if isinstance(self._root_node, StaticTuple):
163
162
            # Demand-load the root
164
163
            self._root_node = self._get_node(self._root_node)
165
164
 
173
172
        :param node: A tuple key or node object.
174
173
        :return: A node object.
175
174
        """
176
 
        if type(node) is StaticTuple:
 
175
        if isinstance(node, StaticTuple):
177
176
            bytes = self._read_bytes(node)
178
177
            return _deserialise(bytes, node,
179
178
                search_key_func=self._search_key_func)
210
209
                key_str = ' None'
211
210
        result.append('%s%r %s%s' % (indent, prefix, node.__class__.__name__,
212
211
                                     key_str))
213
 
        if type(node) is InternalNode:
 
212
        if isinstance(node, InternalNode):
214
213
            # Trigger all child nodes to get loaded
215
214
            list(node._iter_nodes(self._store))
216
215
            for prefix, sub in sorted(node._items.iteritems()):
244
243
        root_key = klass._create_directly(store, initial_value,
245
244
            maximum_size=maximum_size, key_width=key_width,
246
245
            search_key_func=search_key_func)
247
 
        if type(root_key) is not StaticTuple:
 
246
        if not isinstance(root_key, StaticTuple):
248
247
            raise AssertionError('we got a %s instead of a StaticTuple'
249
248
                                 % (type(root_key),))
250
249
        return root_key
332
331
        def process_node(node, path, a_map, pending):
333
332
            # take a node and expand it
334
333
            node = a_map._get_node(node)
335
 
            if type(node) == LeafNode:
 
334
            if isinstance(node, LeafNode):
336
335
                path = (node._key, path)
337
336
                for key, value in node._items.items():
338
337
                    # For a LeafNode, the key is a serialized_key, rather than
370
369
            # time?
371
370
            self_node = self._get_node(self_node)
372
371
            basis_node = basis._get_node(basis_node)
373
 
            if (type(self_node) == InternalNode
374
 
                and type(basis_node) == InternalNode):
 
372
            if (isinstance(self_node, InternalNode)
 
373
                and isinstance(basis_node, InternalNode)):
375
374
                # Matching internal nodes
376
375
                process_common_internal_nodes(self_node, basis_node)
377
 
            elif (type(self_node) == LeafNode
378
 
                  and type(basis_node) == LeafNode):
 
376
            elif (isinstance(self_node, LeafNode)
 
377
                  and isinstance(basis_node, LeafNode)):
379
378
                process_common_leaf_nodes(self_node, basis_node)
380
379
            else:
381
380
                process_node(self_node, self_path, self, self_pending)
390
389
            # A better implementation would probably have a reverse map
391
390
            # back to the children of a node, and jump straight to it when
392
391
            # a common node is detected, the proceed to remove the already
393
 
            # pending children. bzrlib.graph has a searcher module with a
 
392
            # pending children. breezy.graph has a searcher module with a
394
393
            # similar problem.
395
394
            while key_path is not None:
396
395
                key, key_path = key_path
520
519
 
521
520
    def key(self):
522
521
        """Return the key for this map."""
523
 
        if type(self._root_node) is StaticTuple:
 
522
        if isinstance(self._root_node, StaticTuple):
524
523
            return self._root_node
525
524
        else:
526
525
            return self._root_node._key
551
550
 
552
551
    def _node_key(self, node):
553
552
        """Get the key for a node whether it's a tuple or node."""
554
 
        if type(node) is tuple:
 
553
        if isinstance(node, tuple):
555
554
            node = StaticTuple.from_sequence(node)
556
 
        if type(node) is StaticTuple:
 
555
        if isinstance(node, StaticTuple):
557
556
            return node
558
557
        else:
559
558
            return node._key
562
561
        """remove key from the map."""
563
562
        key = StaticTuple.from_sequence(key)
564
563
        self._ensure_root()
565
 
        if type(self._root_node) is InternalNode:
 
564
        if isinstance(self._root_node, InternalNode):
566
565
            unmapped = self._root_node.unmap(self._store, key,
567
566
                check_remap=check_remap)
568
567
        else:
572
571
    def _check_remap(self):
573
572
        """Check if nodes can be collapsed."""
574
573
        self._ensure_root()
575
 
        if type(self._root_node) is InternalNode:
576
 
            self._root_node._check_remap(self._store)
 
574
        if isinstance(self._root_node, InternalNode):
 
575
            self._root_node = self._root_node._check_remap(self._store)
577
576
 
578
577
    def _save(self):
579
578
        """Save the map completely.
580
579
 
581
580
        :return: The key of the root node.
582
581
        """
583
 
        if type(self._root_node) is StaticTuple:
 
582
        if isinstance(self._root_node, StaticTuple):
584
583
            # Already saved.
585
584
            return self._root_node
586
585
        keys = list(self._root_node.serialise(self._store))
923
922
        bytes = ''.join(lines)
924
923
        if len(bytes) != self._current_size():
925
924
            raise AssertionError('Invalid _current_size')
926
 
        _get_cache().add(self._key, bytes)
 
925
        _get_cache()[self._key] = bytes
927
926
        return [self._key]
928
927
 
929
928
    def refs(self):
1196
1195
                    prefix, node_key_filter = keys[record.key]
1197
1196
                    node_and_filters.append((node, node_key_filter))
1198
1197
                    self._items[prefix] = node
1199
 
                    _get_cache().add(record.key, bytes)
 
1198
                    _get_cache()[record.key] = bytes
1200
1199
                for info in node_and_filters:
1201
1200
                    yield info
1202
1201
 
1229
1228
            # new child needed:
1230
1229
            child = self._new_child(search_key, LeafNode)
1231
1230
        old_len = len(child)
1232
 
        if type(child) is LeafNode:
 
1231
        if isinstance(child, LeafNode):
1233
1232
            old_size = child._current_size()
1234
1233
        else:
1235
1234
            old_size = None
1241
1240
            self._items[search_key] = child
1242
1241
            self._key = None
1243
1242
            new_node = self
1244
 
            if type(child) is LeafNode:
 
1243
            if isinstance(child, LeafNode):
1245
1244
                if old_size is None:
1246
1245
                    # The old node was an InternalNode which means it has now
1247
1246
                    # collapsed, so we need to check if it will chain to a
1294
1293
        :return: An iterable of the keys inserted by this operation.
1295
1294
        """
1296
1295
        for node in self._items.itervalues():
1297
 
            if type(node) is StaticTuple:
 
1296
            if isinstance(node, StaticTuple):
1298
1297
                # Never deserialised.
1299
1298
                continue
1300
1299
            if node._key is not None:
1311
1310
        lines.append('%s\n' % (self._search_prefix,))
1312
1311
        prefix_len = len(self._search_prefix)
1313
1312
        for prefix, node in sorted(self._items.items()):
1314
 
            if type(node) is StaticTuple:
 
1313
            if isinstance(node, StaticTuple):
1315
1314
                key = node[0]
1316
1315
            else:
1317
1316
                key = node._key[0]
1322
1321
            lines.append(serialised[prefix_len:])
1323
1322
        sha1, _, _ = store.add_lines((None,), (), lines)
1324
1323
        self._key = StaticTuple("sha1:" + sha1,).intern()
1325
 
        _get_cache().add(self._key, ''.join(lines))
 
1324
        _get_cache()[self._key] = ''.join(lines)
1326
1325
        yield self._key
1327
1326
 
1328
1327
    def _search_key(self, key):
1356
1355
            raise AssertionError("unserialised nodes have no refs.")
1357
1356
        refs = []
1358
1357
        for value in self._items.itervalues():
1359
 
            if type(value) is StaticTuple:
 
1358
            if isinstance(value, StaticTuple):
1360
1359
                refs.append(value)
1361
1360
            else:
1362
1361
                refs.append(value.key())
1372
1371
        return self._search_prefix
1373
1372
 
1374
1373
    def unmap(self, store, key, check_remap=True):
1375
 
        """Remove key from this node and it's children."""
 
1374
        """Remove key from this node and its children."""
1376
1375
        if not len(self._items):
1377
1376
            raise AssertionError("can't unmap in an empty InternalNode.")
1378
1377
        children = [node for node, _
1395
1394
        if len(self._items) == 1:
1396
1395
            # this node is no longer needed:
1397
1396
            return self._items.values()[0]
1398
 
        if type(unmapped) is InternalNode:
 
1397
        if isinstance(unmapped, InternalNode):
1399
1398
            return self
1400
1399
        if check_remap:
1401
1400
            return self._check_remap(store)
1441
1440
        #   c) With 255-way fan out, we don't want to read all 255 and destroy
1442
1441
        #      the page cache, just to determine that we really don't need it.
1443
1442
        for node, _ in self._iter_nodes(store, batch_size=16):
1444
 
            if type(node) is InternalNode:
 
1443
            if isinstance(node, InternalNode):
1445
1444
                # Without looking at any leaf nodes, we are sure
1446
1445
                return self
1447
1446
            for key, value in node._items.iteritems():
1526
1525
            bytes = record.get_bytes_as('fulltext')
1527
1526
            node = _deserialise(bytes, record.key,
1528
1527
                                search_key_func=self._search_key_func)
1529
 
            if type(node) is InternalNode:
 
1528
            if isinstance(node, InternalNode):
1530
1529
                # Note we don't have to do node.refs() because we know that
1531
1530
                # there are no children that have been pushed into this node
1532
1531
                # Note: Using as_st() here seemed to save 1.2MB, which would
1726
1725
 
1727
1726
 
1728
1727
try:
1729
 
    from bzrlib._chk_map_pyx import (
 
1728
    from breezy._chk_map_pyx import (
 
1729
        _bytes_to_text_key,
1730
1730
        _search_key_16,
1731
1731
        _search_key_255,
1732
1732
        _deserialise_leaf_node,
1733
1733
        _deserialise_internal_node,
1734
1734
        )
1735
 
except ImportError, e:
 
1735
except ImportError as e:
1736
1736
    osutils.failed_to_load_extension(e)
1737
 
    from bzrlib._chk_map_py import (
 
1737
    from breezy._chk_map_py import (
 
1738
        _bytes_to_text_key,
1738
1739
        _search_key_16,
1739
1740
        _search_key_255,
1740
1741
        _deserialise_leaf_node,
1750
1751
    This generally shouldn't be used in production code, but it can be helpful
1751
1752
    to debug problems.
1752
1753
    """
1753
 
    if type(key) is not StaticTuple:
 
1754
    if not isinstance(key, StaticTuple):
1754
1755
        raise TypeError('key %r is not StaticTuple but %s' % (key, type(key)))
1755
1756
    if len(key) != 1:
1756
1757
        raise ValueError('key %r should have length 1, not %d' % (key, len(key),))
1757
 
    if type(key[0]) is not str:
 
1758
    if not isinstance(key[0], str):
1758
1759
        raise TypeError('key %r should hold a str, not %r'
1759
1760
                        % (key, type(key[0])))
1760
1761
    if not key[0].startswith('sha1:'):