29
29
from bisect import bisect_right
30
from cStringIO import StringIO
34
from breezy.lazy_import import lazy_import
33
from .lazy_import import lazy_import
35
34
lazy_import(globals(), """
36
35
from breezy import (
331
333
lines.append("%s\x00%s\x00%s\x00%s\n" % (string_key, absent,
332
334
'\t'.join(flattened_references), value))
333
335
lines.append('\n')
334
result = StringIO(''.join(lines))
336
result = BytesIO(''.join(lines))
335
337
if expected_bytes and len(result.getvalue()) != expected_bytes:
336
338
raise errors.BzrError('Failed index creation. Internal error:'
337
339
' mismatched output length and expected length: %d %d' %
432
434
def __eq__(self, other):
433
435
"""Equal when self and other were created with the same parameters."""
435
type(self) == type(other) and
437
isinstance(self, type(other)) and
436
438
self._transport == other._transport and
437
439
self._name == other._name and
438
440
self._size == other._size)
460
462
if self._base_offset != 0:
461
463
# This is wasteful, but it is better than dealing with
462
464
# adjusting all the offsets, etc.
463
stream = StringIO(stream.read()[self._base_offset:])
465
stream = BytesIO(stream.read()[self._base_offset:])
464
466
self._read_prefix(stream)
465
467
self._expected_elements = 3 + self._key_length
749
751
key_dict = dicts.pop(-1)
750
752
# can't be empty or would not exist
751
753
item, value = key_dict.iteritems().next()
752
if type(value) == dict:
754
if isinstance(value, dict):
754
756
dicts.extend(key_dict.itervalues())
1228
1230
# We read the whole range, most likely because the
1229
1231
# Transport upcast our readv ranges into one long request
1230
1232
# for enough total data to grab the whole index.
1231
self._buffer_all(StringIO(data))
1233
self._buffer_all(BytesIO(data))
1233
1235
if self._bisect_nodes is None:
1234
1236
# this must be the start
1310
1312
found_parents[key] = parents
1311
1313
return found_parents
1313
has_key = _has_key_from_parent_map
1315
__contains__ = _has_key_from_parent_map
1315
1317
def insert_index(self, pos, index, name=None):
1316
1318
"""Insert a new index in the list of indices to query.
1344
1346
seen_keys.add(node[1])
1346
except errors.NoSuchFile:
1347
self._reload_or_raise()
1348
except errors.NoSuchFile as e:
1349
if not self._try_reload(e):
1349
1352
def iter_entries(self, keys):
1350
1353
"""Iterate over keys within the index.
1373
1376
hit_indices.append(index)
1375
except errors.NoSuchFile:
1376
self._reload_or_raise()
1378
except errors.NoSuchFile as e:
1379
if not self._try_reload(e):
1377
1381
self._move_to_front(hit_indices)
1379
1383
def iter_entries_prefix(self, keys):
1415
1419
hit_indices.append(index)
1417
except errors.NoSuchFile:
1418
self._reload_or_raise()
1421
except errors.NoSuchFile as e:
1422
if not self._try_reload(e):
1419
1424
self._move_to_front(hit_indices)
1421
1426
def _move_to_front(self, hit_indices):
1561
1566
return sum((index.key_count() for index in self._indices), 0)
1562
except errors.NoSuchFile:
1563
self._reload_or_raise()
1567
except errors.NoSuchFile as e:
1568
if not self._try_reload(e):
1565
1571
missing_keys = _missing_keys_from_parent_map
1567
def _reload_or_raise(self):
1573
def _try_reload(self, error):
1568
1574
"""We just got a NoSuchFile exception.
1570
1576
Try to reload the indices, if it fails, just raise the current
1573
1579
if self._reload_func is None:
1575
exc_type, exc_value, exc_traceback = sys.exc_info()
1576
trace.mutter('Trying to reload after getting exception: %s',
1581
trace.mutter('Trying to reload after getting exception: %s', error)
1578
1582
if not self._reload_func():
1579
1583
# We tried to reload, but nothing changed, so we fail anyway
1580
1584
trace.mutter('_reload_func indicated nothing has changed.'
1581
1585
' Raising original exception.')
1582
raise exc_type, exc_value, exc_traceback
1584
1589
def set_sibling_indices(self, sibling_combined_graph_indices):
1585
1590
"""Set the CombinedGraphIndex objects to reorder after reordering self.
1593
1598
for index in self._indices:
1594
1599
index.validate()
1596
except errors.NoSuchFile:
1597
self._reload_or_raise()
1601
except errors.NoSuchFile as e:
1602
if not self._try_reload(e):
1600
1606
class InMemoryGraphIndex(GraphIndexBuilder):
1720
1726
key_dict = dicts.pop(-1)
1721
1727
# can't be empty or would not exist
1722
1728
item, value = key_dict.iteritems().next()
1723
if type(value) == dict:
1729
if isinstance(value, dict):
1725
1731
dicts.extend(key_dict.itervalues())