~bzr-pqm/bzr/bzr.dev

2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
1
# Copyright (C) 2007 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
16
6379.6.3 by Jelmer Vernooij
Use absolute_import.
17
from __future__ import absolute_import
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
18
19
from bzrlib import errors
20
21
22
def _load_data_py(kndx, fp):
23
    """Read in a knit index."""
24
    cache = kndx._cache
25
    history = kndx._history
26
27
    kndx.check_header(fp)
28
    # readlines reads the whole file at once:
29
    # bad for transports like http, good for local disk
30
    # we save 60 ms doing this one change (
31
    # from calling readline each time to calling
32
    # readlines once.
33
    # probably what we want for nice behaviour on
34
    # http is a incremental readlines that yields, or
35
    # a check for local vs non local indexes,
36
    history_top = len(history) - 1
37
    for line in fp.readlines():
38
        rec = line.split()
39
        if len(rec) < 5 or rec[-1] != ':':
40
            # corrupt line.
41
            # FIXME: in the future we should determine if its a
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
42
            # short write - and ignore it
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
43
            # or a different failure, and raise. RBC 20060407
44
            continue
45
46
        try:
47
            parents = []
48
            for value in rec[4:-1]:
49
                if value[0] == '.':
50
                    # uncompressed reference
51
                    parent_id = value[1:]
52
                else:
53
                    parent_id = history[int(value)]
54
                parents.append(parent_id)
55
        except (IndexError, ValueError), e:
56
            # The parent could not be decoded to get its parent row. This
57
            # at a minimum will cause this row to have wrong parents, or
58
            # even to apply a delta to the wrong base and decode
59
            # incorrectly. its therefore not usable, and because we have
60
            # encountered a situation where a new knit index had this
61
            # corrupt we can't asssume that no other rows referring to the
62
            # index of this record actually mean the subsequent uncorrupt
63
            # one, so we error.
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
64
            raise errors.KnitCorrupt(kndx._filename, "line %r: %s" % (rec, e))
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
65
66
        version_id, options, pos, size = rec[:4]
67
        version_id = version_id
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
68
        try:
69
            pos = int(pos)
70
        except ValueError, e:
71
            raise errors.KnitCorrupt(kndx._filename,
72
                                     "invalid position on line %r: %s"
73
                                     % (rec, e))
74
        try:
75
            size = int(size)
76
        except ValueError, e:
77
            raise errors.KnitCorrupt(kndx._filename,
78
                                     "invalid size on line %r: %s"
79
                                     % (rec, e))
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
80
81
        # See kndx._cache_version
82
        # only want the _history index to reference the 1st
83
        # index entry for version_id
84
        if version_id not in cache:
85
            history_top += 1
86
            index = history_top
87
            history.append(version_id)
88
        else:
89
            index = cache[version_id][5]
90
        cache[version_id] = (version_id,
91
                             options.split(','),
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
92
                             pos,
93
                             size,
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
94
                             tuple(parents),
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
95
                             index)
96
        # end kndx._cache_version