~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavestore.py

  • Committer: Martin Pool
  • Date: 2005-09-12 09:50:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050912095044-6acfdb5611729987
- no tests in bzrlib.fetch anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
 
# XXX: Some consideration of the problems that might occur if there are
20
 
# files whose id differs only in case.  That should probably be forbidden.
21
 
 
22
 
 
23
 
from cStringIO import StringIO
 
19
# Author: Martin Pool <mbp@canonical.com>
 
20
 
 
21
 
24
22
import os
25
 
import errno
26
 
 
27
 
from bzrlib.weavefile import read_weave, write_weave_v5
28
 
from bzrlib.weave import Weave
29
 
from bzrlib.store import Store
30
 
from bzrlib.atomicfile import AtomicFile
31
 
from bzrlib.errors import NoSuchFile
32
 
from bzrlib.trace import mutter
33
 
 
34
 
 
35
 
 
36
 
 
37
 
class WeaveStore(Store):
38
 
    """Collection of several weave files in a directory.
39
 
 
40
 
    This has some shortcuts for reading and writing them.
41
 
    """
42
 
    FILE_SUFFIX = '.weave'
43
 
 
44
 
    def __init__(self, transport):
45
 
        self._transport = transport
46
 
        self._cache = {}
47
 
        self.enable_cache = False
48
 
 
49
 
 
50
 
    def filename(self, file_id):
51
 
        """Return the path relative to the transport root."""
52
 
        return file_id + WeaveStore.FILE_SUFFIX
53
 
 
54
 
    def __iter__(self):
55
 
        l = len(WeaveStore.FILE_SUFFIX)
56
 
        for f in self._transport.list_dir('.'):
57
 
            if f.endswith(WeaveStore.FILE_SUFFIX):
58
 
                f = f[:-l]
59
 
                yield f
60
 
 
61
 
    def __contains__(self, fileid):
62
 
        """"""
63
 
        return self._transport.has(self.filename(fileid))
64
 
 
65
 
    def _get(self, file_id):
66
 
        return self._transport.get(self.filename(file_id))
67
 
 
68
 
    def _put(self, file_id, f):
69
 
        return self._transport.put(self.filename(file_id), f)
 
23
import bzrlib.weavefile
 
24
 
 
25
 
 
26
class WeaveStore(object):
 
27
    """Collection of several weave files."""
 
28
    def __init__(self, dir):
 
29
        self._dir = dir
70
30
 
71
31
 
72
32
    def get_weave(self, file_id):
73
 
        if self.enable_cache:
74
 
            if file_id in self._cache:
75
 
                return self._cache[file_id]
76
 
        w = read_weave(self._get(file_id))
77
 
        if self.enable_cache:
78
 
            self._cache[file_id] = w
79
 
        return w
80
 
 
81
 
 
82
 
    def get_lines(self, file_id, rev_id):
83
 
        """Return text from a particular version of a weave.
84
 
 
85
 
        Returned as a list of lines."""
86
 
        w = self.get_weave(file_id)
87
 
        return w.get(w.lookup(rev_id))
88
 
    
89
 
 
90
 
    def get_weave_or_empty(self, file_id):
91
 
        """Return a weave, or an empty one if it doesn't exist.""" 
92
 
        try:
93
 
            inf = self._get(file_id)
94
 
        except NoSuchFile:
95
 
            return Weave(weave_name=file_id)
96
 
        else:
97
 
            return read_weave(inf)
98
 
    
99
 
 
100
 
    def put_weave(self, file_id, weave):
101
 
        """Write back a modified weave"""
102
 
        if self.enable_cache:
103
 
            self._cache[file_id] = weave
104
 
 
105
 
        sio = StringIO()
106
 
        write_weave_v5(weave, sio)
107
 
        sio.seek(0)
108
 
 
109
 
        self._put(file_id, sio)
110
 
 
111
 
 
112
 
    def add_text(self, file_id, rev_id, new_lines, parents):
113
 
        w = self.get_weave_or_empty(file_id)
114
 
        parent_idxs = map(w.lookup, parents)
115
 
        w.add(rev_id, parent_idxs, new_lines)
116
 
        self.put_weave(file_id, w)
117
 
        
118
 
    def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents):
119
 
        w = self.get_weave_or_empty(file_id)
120
 
        parent_idxs = map(w.lookup, parents)
121
 
        w.add_identical(old_rev_id, new_rev_id, parent_idxs)
122
 
        self.put_weave(file_id, w)
123
 
     
124
 
    def copy_multi(self, from_store, file_ids):
125
 
        assert isinstance(from_store, WeaveStore)
126
 
        for f in file_ids:
127
 
            mutter("copy weave {%s} into %s", f, self)
128
 
            self._put(f, from_store._get(f))
 
33
        path = self._dir + os.sep + file_id + '.weave'
 
34
        return bzrlib.weavefile.read_weave(file(path, 'rb'))
 
35