~bzr-pqm/bzr/bzr.dev

1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
1
#! /usr/bin/python
2
3
# Copyright (C) 2005 Canonical Ltd
4
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
1393.1.19 by Martin Pool
- add WeaveStore.__iter__, __contains__ and copy_multi()
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
1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
24
import os
1224 by Martin Pool
- new method WeaveStore.get_weave_or_empty
25
import errno
1223 by Martin Pool
- store inventories in weave
26
27
from bzrlib.weavefile import read_weave, write_weave_v5
28
from bzrlib.weave import Weave
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
29
from bzrlib.store import Store
1223 by Martin Pool
- store inventories in weave
30
from bzrlib.atomicfile import AtomicFile
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
31
from bzrlib.errors import NoSuchFile
1393.1.19 by Martin Pool
- add WeaveStore.__iter__, __contains__ and copy_multi()
32
from bzrlib.trace import mutter
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
33
34
35
36
37
class WeaveStore(Store):
1352 by Martin Pool
- store control weaves in .bzr/, not mixed in with file weaves
38
    """Collection of several weave files in a directory.
39
40
    This has some shortcuts for reading and writing them.
41
    """
1393.1.19 by Martin Pool
- add WeaveStore.__iter__, __contains__ and copy_multi()
42
    FILE_SUFFIX = '.weave'
43
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
44
    def __init__(self, transport):
45
        self._transport = transport
1363 by Martin Pool
- add quick-and-dirty cache for weaves to speed check command
46
        self._cache = {}
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
47
	self.enable_cache = False
1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
48
49
1223 by Martin Pool
- store inventories in weave
50
    def filename(self, file_id):
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
51
        """Return the path relative to the transport root."""
1393.1.19 by Martin Pool
- add WeaveStore.__iter__, __contains__ and copy_multi()
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))
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
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)
1223 by Martin Pool
- store inventories in weave
70
1224 by Martin Pool
- new method WeaveStore.get_weave_or_empty
71
1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
72
    def get_weave(self, file_id):
1373 by Martin Pool
- use cache for weaves for check only
73
        if self.enable_cache:
1363 by Martin Pool
- add quick-and-dirty cache for weaves to speed check command
74
            if file_id in self._cache:
75
                return self._cache[file_id]
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
76
        w = read_weave(self._get(file_id))
1373 by Martin Pool
- use cache for weaves for check only
77
        if self.enable_cache:
1363 by Martin Pool
- add quick-and-dirty cache for weaves to speed check command
78
            self._cache[file_id] = w
79
        return w
1262 by Martin Pool
- fetch should also copy ancestry records
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))
1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
88
    
1223 by Martin Pool
- store inventories in weave
89
1224 by Martin Pool
- new method WeaveStore.get_weave_or_empty
90
    def get_weave_or_empty(self, file_id):
91
        """Return a weave, or an empty one if it doesn't exist.""" 
92
        try:
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
93
            inf = self._get(file_id)
94
        except NoSuchFile:
95
            return Weave(weave_name=file_id)
1224 by Martin Pool
- new method WeaveStore.get_weave_or_empty
96
        else:
97
            return read_weave(inf)
98
    
99
1223 by Martin Pool
- store inventories in weave
100
    def put_weave(self, file_id, weave):
101
        """Write back a modified weave"""
1373 by Martin Pool
- use cache for weaves for check only
102
        if self.enable_cache:
1363 by Martin Pool
- add quick-and-dirty cache for weaves to speed check command
103
            self._cache[file_id] = weave
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
104
105
        sio = StringIO()
106
        write_weave_v5(weave, sio)
107
        sio.seek(0)
108
109
        self._put(file_id, sio)
1223 by Martin Pool
- store inventories in weave
110
111
112
    def add_text(self, file_id, rev_id, new_lines, parents):
1224 by Martin Pool
- new method WeaveStore.get_weave_or_empty
113
        w = self.get_weave_or_empty(file_id)
1223 by Martin Pool
- store inventories in weave
114
        parent_idxs = map(w.lookup, parents)
115
        w.add(rev_id, parent_idxs, new_lines)
116
        self.put_weave(file_id, w)
117
        
1092.2.22 by Robert Collins
text_version and name_version unification looking reasonable
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)
1223 by Martin Pool
- store inventories in weave
123
     
1393.1.19 by Martin Pool
- add WeaveStore.__iter__, __contains__ and copy_multi()
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))