1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
1
# Copyright (C) 2007-2010 Canonical Ltd
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
646
647
del self.combined_index._indices[:]
647
648
self.add_callback = None
649
def remove_index(self, index, pack):
650
def remove_index(self, index):
650
651
"""Remove index from the indices used to answer queries.
652
653
:param index: An index from the pack parameter.
653
:param pack: A Pack instance.
655
655
del self.index_to_pack[index]
656
656
self.combined_index._indices.remove(index)
1117
1117
iterator is a tuple with:
1118
1118
index, readv_vector, node_vector. readv_vector is a list ready to
1119
1119
hand to the transport readv method, and node_vector is a list of
1120
(key, eol_flag, references) for the the node retrieved by the
1120
(key, eol_flag, references) for the node retrieved by the
1121
1121
matching readv_vector.
1123
1123
# group by pack so we do one readv per pack
1542
1545
self._remove_pack_from_memory(pack)
1543
1546
# record the newly available packs and stop advertising the old
1545
result = self._save_pack_names(clear_obsolete_packs=True)
1546
# Move the old packs out of the way now they are no longer referenced.
1547
for revision_count, packs in pack_operations:
1548
self._obsolete_packs(packs)
1548
to_be_obsoleted = []
1549
for _, packs in pack_operations:
1550
to_be_obsoleted.extend(packs)
1551
result = self._save_pack_names(clear_obsolete_packs=True,
1552
obsolete_packs=to_be_obsoleted)
1551
1555
def _flush_new_pack(self):
1785
1789
:param return: None.
1787
1791
for pack in packs:
1788
pack.pack_transport.rename(pack.file_name(),
1789
'../obsolete_packs/' + pack.file_name())
1793
pack.pack_transport.rename(pack.file_name(),
1794
'../obsolete_packs/' + pack.file_name())
1795
except (errors.PathError, errors.TransportError), e:
1796
# TODO: Should these be warnings or mutters?
1797
mutter("couldn't rename obsolete pack, skipping it:\n%s"
1790
1799
# TODO: Probably needs to know all possible indices for this pack
1791
1800
# - or maybe list the directory and move all indices matching this
1792
1801
# name whether we recognize it or not?
1794
1803
if self.chk_index is not None:
1795
1804
suffixes.append('.cix')
1796
1805
for suffix in suffixes:
1797
self._index_transport.rename(pack.name + suffix,
1798
'../obsolete_packs/' + pack.name + suffix)
1807
self._index_transport.rename(pack.name + suffix,
1808
'../obsolete_packs/' + pack.name + suffix)
1809
except (errors.PathError, errors.TransportError), e:
1810
mutter("couldn't rename obsolete index, skipping it:\n%s"
1800
1813
def pack_distribution(self, total_revisions):
1801
1814
"""Generate a list of the number of revisions to put in each pack.
1827
1840
self._remove_pack_indices(pack)
1828
1841
self.packs.remove(pack)
1830
def _remove_pack_indices(self, pack):
1831
"""Remove the indices for pack from the aggregated indices."""
1832
self.revision_index.remove_index(pack.revision_index, pack)
1833
self.inventory_index.remove_index(pack.inventory_index, pack)
1834
self.text_index.remove_index(pack.text_index, pack)
1835
self.signature_index.remove_index(pack.signature_index, pack)
1836
if self.chk_index is not None:
1837
self.chk_index.remove_index(pack.chk_index, pack)
1843
def _remove_pack_indices(self, pack, ignore_missing=False):
1844
"""Remove the indices for pack from the aggregated indices.
1846
:param ignore_missing: Suppress KeyErrors from calling remove_index.
1848
for index_type in Pack.index_definitions.keys():
1849
attr_name = index_type + '_index'
1850
aggregate_index = getattr(self, attr_name)
1851
if aggregate_index is not None:
1852
pack_index = getattr(pack, attr_name)
1854
aggregate_index.remove_index(pack_index)
1839
1860
def reset(self):
1840
1861
"""Clear all cached data."""
1873
1894
disk_nodes = set()
1874
1895
for index, key, value in self._iter_disk_pack_index():
1875
1896
disk_nodes.add((key, value))
1897
orig_disk_nodes = set(disk_nodes)
1877
1899
# do a two-way diff against our original content
1878
1900
current_nodes = set()
1891
1913
disk_nodes.difference_update(deleted_nodes)
1892
1914
disk_nodes.update(new_nodes)
1894
return disk_nodes, deleted_nodes, new_nodes
1916
return disk_nodes, deleted_nodes, new_nodes, orig_disk_nodes
1896
1918
def _syncronize_pack_names_from_disk_nodes(self, disk_nodes):
1897
1919
"""Given the correct set of pack files, update our saved info.
1937
1959
added.append(name)
1938
1960
return removed, added, modified
1940
def _save_pack_names(self, clear_obsolete_packs=False):
1962
def _save_pack_names(self, clear_obsolete_packs=False, obsolete_packs=None):
1941
1963
"""Save the list of packs.
1943
1965
This will take out the mutex around the pack names list for the
1948
1970
:param clear_obsolete_packs: If True, clear out the contents of the
1949
1971
obsolete_packs directory.
1972
:param obsolete_packs: Packs that are obsolete once the new pack-names
1973
file has been written.
1950
1974
:return: A list of the names saved that were not previously on disk.
1976
already_obsolete = []
1952
1977
self.lock_names()
1954
1979
builder = self._index_builder_class()
1955
disk_nodes, deleted_nodes, new_nodes = self._diff_pack_names()
1980
(disk_nodes, deleted_nodes, new_nodes,
1981
orig_disk_nodes) = self._diff_pack_names()
1956
1982
# TODO: handle same-name, index-size-changes here -
1957
1983
# e.g. use the value from disk, not ours, *unless* we're the one
1960
1986
builder.add_node(key, value)
1961
1987
self.transport.put_file('pack-names', builder.finish(),
1962
1988
mode=self.repo.bzrdir._get_file_mode())
1963
# move the baseline forward
1964
1989
self._packs_at_load = disk_nodes
1965
1990
if clear_obsolete_packs:
1966
self._clear_obsolete_packs()
1993
to_preserve = set([o.name for o in obsolete_packs])
1994
already_obsolete = self._clear_obsolete_packs(to_preserve)
1968
1996
self._unlock_names()
1969
1997
# synchronise the memory packs list with what we just wrote:
1970
1998
self._syncronize_pack_names_from_disk_nodes(disk_nodes)
2000
# TODO: We could add one more condition here. "if o.name not in
2001
# orig_disk_nodes and o != the new_pack we haven't written to
2002
# disk yet. However, the new pack object is not easily
2003
# accessible here (it would have to be passed through the
2004
# autopacking code, etc.)
2005
obsolete_packs = [o for o in obsolete_packs
2006
if o.name not in already_obsolete]
2007
self._obsolete_packs(obsolete_packs)
1971
2008
return [new_node[0][0] for new_node in new_nodes]
1973
2010
def reload_pack_names(self):
1990
2027
# out the new value.
1991
disk_nodes, _, _ = self._diff_pack_names()
1992
self._packs_at_load = disk_nodes
2028
(disk_nodes, deleted_nodes, new_nodes,
2029
orig_disk_nodes) = self._diff_pack_names()
2030
# _packs_at_load is meant to be the explicit list of names in
2031
# 'pack-names' at then start. As such, it should not contain any
2032
# pending names that haven't been written out yet.
2033
self._packs_at_load = orig_disk_nodes
1993
2034
(removed, added,
1994
2035
modified) = self._syncronize_pack_names_from_disk_nodes(disk_nodes)
1995
2036
if removed or added or modified:
2005
2046
raise errors.RetryAutopack(self.repo, False, sys.exc_info())
2007
def _clear_obsolete_packs(self):
2048
def _clear_obsolete_packs(self, preserve=None):
2008
2049
"""Delete everything from the obsolete-packs directory.
2051
:return: A list of pack identifiers (the filename without '.pack') that
2052
were found in obsolete_packs.
2010
2055
obsolete_pack_transport = self.transport.clone('obsolete_packs')
2056
if preserve is None:
2011
2058
for filename in obsolete_pack_transport.list_dir('.'):
2059
name, ext = osutils.splitext(filename)
2062
if name in preserve:
2013
2065
obsolete_pack_transport.delete(filename)
2014
2066
except (errors.PathError, errors.TransportError), e:
2015
warning("couldn't delete obsolete pack, skipping it:\n%s" % (e,))
2067
warning("couldn't delete obsolete pack, skipping it:\n%s"
2017
2071
def _start_write_group(self):
2018
2072
# Do not permit preparation for writing if we're not in a 'write lock'.
2045
2099
# FIXME: just drop the transient index.
2046
2100
# forget what names there are
2047
2101
if self._new_pack is not None:
2049
self._new_pack.abort()
2051
# XXX: If we aborted while in the middle of finishing the write
2052
# group, _remove_pack_indices can fail because the indexes are
2053
# already gone. If they're not there we shouldn't fail in this
2054
# case. -- mbp 20081113
2055
self._remove_pack_indices(self._new_pack)
2056
self._new_pack = None
2102
operation = cleanup.OperationWithCleanups(self._new_pack.abort)
2103
operation.add_cleanup(setattr, self, '_new_pack', None)
2104
# If we aborted while in the middle of finishing the write
2105
# group, _remove_pack_indices could fail because the indexes are
2106
# already gone. But they're not there we shouldn't fail in this
2107
# case, so we pass ignore_missing=True.
2108
operation.add_cleanup(self._remove_pack_indices, self._new_pack,
2109
ignore_missing=True)
2110
operation.run_simple()
2057
2111
for resumed_pack in self._resumed_packs:
2059
resumed_pack.abort()
2061
# See comment in previous finally block.
2063
self._remove_pack_indices(resumed_pack)
2112
operation = cleanup.OperationWithCleanups(resumed_pack.abort)
2113
# See comment in previous finally block.
2114
operation.add_cleanup(self._remove_pack_indices, resumed_pack,
2115
ignore_missing=True)
2116
operation.run_simple()
2066
2117
del self._resumed_packs[:]
2068
2119
def _remove_resumed_pack_indices(self):
2234
2285
self._reconcile_fixes_text_parents = True
2235
2286
self._reconcile_backsup_inventory = False
2237
def _warn_if_deprecated(self):
2288
def _warn_if_deprecated(self, branch=None):
2238
2289
# This class isn't deprecated, but one sub-format is
2239
2290
if isinstance(self._format, RepositoryFormatKnitPack5RichRootBroken):
2240
from bzrlib import repository
2241
if repository._deprecation_warning_done:
2243
repository._deprecation_warning_done = True
2244
warning("Format %s for %s is deprecated - please use"
2245
" 'bzr upgrade --1.6.1-rich-root'"
2246
% (self._format, self.bzrdir.transport.base))
2291
super(KnitPackRepository, self)._warn_if_deprecated(branch)
2248
2293
def _abort_write_group(self):
2249
2294
self.revisions._index._key_dependencies.clear()