~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/weave.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-10-11 05:20:36 UTC
  • mfrom: (1442)
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1460.
  • Revision ID: aaron.bentley@utoronto.ca-20051011052036-23ff7bae64a29826
latest from robert collins

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
from bzrlib.weavefile import read_weave, write_weave_v5
28
28
from bzrlib.weave import Weave
29
 
from bzrlib.store import Store
 
29
from bzrlib.store import TransportStore, hash_prefix
30
30
from bzrlib.atomicfile import AtomicFile
31
 
from bzrlib.errors import NoSuchFile
 
31
from bzrlib.errors import NoSuchFile, FileExists
32
32
from bzrlib.trace import mutter
33
33
 
34
34
 
35
 
class WeaveStore(Store):
 
35
class WeaveStore(TransportStore):
36
36
    """Collection of several weave files in a directory.
37
37
 
38
38
    This has some shortcuts for reading and writing them.
39
39
    """
40
40
    FILE_SUFFIX = '.weave'
41
41
 
42
 
    def __init__(self, transport):
 
42
    def __init__(self, transport, prefixed=False, precious=False):
43
43
        self._transport = transport
 
44
        self._prefixed = prefixed
 
45
        self._precious = precious
44
46
 
45
47
    def filename(self, file_id):
46
48
        """Return the path relative to the transport root."""
47
 
        return file_id + WeaveStore.FILE_SUFFIX
 
49
        if self._prefixed:
 
50
            return hash_prefix(file_id) + file_id + WeaveStore.FILE_SUFFIX
 
51
        else:
 
52
            return file_id + WeaveStore.FILE_SUFFIX
48
53
 
49
54
    def __iter__(self):
50
55
        l = len(WeaveStore.FILE_SUFFIX)
51
 
        for f in self._transport.list_dir('.'):
52
 
            if f.endswith(WeaveStore.FILE_SUFFIX):
53
 
                f = f[:-l]
54
 
                yield f
 
56
        for relpath, st in self._iter_relpaths():
 
57
            if relpath.endswith(WeaveStore.FILE_SUFFIX):
 
58
                yield os.path.basename(relpath[:-l])
55
59
 
56
60
    def __contains__(self, fileid):
57
61
        """"""
61
65
        return self._transport.get(self.filename(file_id))
62
66
 
63
67
    def _put(self, file_id, f):
 
68
        if self._prefixed:
 
69
            try:
 
70
                self._transport.mkdir(hash_prefix(file_id))
 
71
            except FileExists:
 
72
                pass
64
73
        return self._transport.put(self.filename(file_id), f)
65
74
 
66
75
    def get_weave(self, file_id, transaction):
70
79
            return weave
71
80
        w = read_weave(self._get(file_id))
72
81
        transaction.map.add_weave(file_id, w)
73
 
        transaction.register_clean(w)
 
82
        transaction.register_clean(w, precious=self._precious)
74
83
        return w
75
84
 
76
 
 
77
85
    def get_lines(self, file_id, rev_id, transaction):
78
86
        """Return text from a particular version of a weave.
79
87
 
88
96
        except NoSuchFile:
89
97
            weave = Weave(weave_name=file_id)
90
98
            transaction.map.add_weave(file_id, weave)
91
 
            transaction.register_clean(weave)
 
99
            transaction.register_clean(weave, precious=self._precious)
92
100
            return weave
93
101
 
94
102
    def put_weave(self, file_id, weave, transaction):
99
107
        sio = StringIO()
100
108
        write_weave_v5(weave, sio)
101
109
        sio.seek(0)
102
 
 
103
110
        self._put(file_id, sio)
104
111
 
105
 
 
106
112
    def add_text(self, file_id, rev_id, new_lines, parents, transaction):
107
113
        w = self.get_weave_or_empty(file_id, transaction)
108
114
        parent_idxs = map(w.lookup, parents)