~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/weave.py

  • Committer: Robert Collins
  • Date: 2005-10-08 00:02:33 UTC
  • mfrom: (1421)
  • mto: This revision was merged to the branch mainline in revision 1422.
  • Revision ID: robertc@robertcollins.net-20051008000233-c7c5d0d2b7000da0
merge from newformat stuff and upgrade

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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
 
24
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
class WeaveStore(Store):
 
36
    """Collection of several weave files in a directory.
 
37
 
 
38
    This has some shortcuts for reading and writing them.
 
39
    """
 
40
    FILE_SUFFIX = '.weave'
 
41
 
 
42
    def __init__(self, transport):
 
43
        self._transport = transport
 
44
 
 
45
    def filename(self, file_id):
 
46
        """Return the path relative to the transport root."""
 
47
        return file_id + WeaveStore.FILE_SUFFIX
 
48
 
 
49
    def __iter__(self):
 
50
        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
 
55
 
 
56
    def __contains__(self, fileid):
 
57
        """"""
 
58
        return self._transport.has(self.filename(fileid))
 
59
 
 
60
    def _get(self, file_id):
 
61
        return self._transport.get(self.filename(file_id))
 
62
 
 
63
    def _put(self, file_id, f):
 
64
        return self._transport.put(self.filename(file_id), f)
 
65
 
 
66
    def get_weave(self, file_id, transaction):
 
67
        weave = transaction.map.find_weave(file_id)
 
68
        if weave:
 
69
            mutter("cache hit in %s for %s", self, file_id)
 
70
            return weave
 
71
        w = read_weave(self._get(file_id))
 
72
        transaction.map.add_weave(file_id, w)
 
73
        transaction.register_clean(w)
 
74
        return w
 
75
 
 
76
 
 
77
    def get_lines(self, file_id, rev_id, transaction):
 
78
        """Return text from a particular version of a weave.
 
79
 
 
80
        Returned as a list of lines."""
 
81
        w = self.get_weave(file_id, transaction)
 
82
        return w.get(w.lookup(rev_id))
 
83
    
 
84
    def get_weave_or_empty(self, file_id, transaction):
 
85
        """Return a weave, or an empty one if it doesn't exist.""" 
 
86
        try:
 
87
            return self.get_weave(file_id, transaction)
 
88
        except NoSuchFile:
 
89
            weave = Weave(weave_name=file_id)
 
90
            transaction.map.add_weave(file_id, weave)
 
91
            transaction.register_clean(weave)
 
92
            return weave
 
93
 
 
94
    def put_weave(self, file_id, weave, transaction):
 
95
        """Write back a modified weave"""
 
96
        transaction.register_dirty(weave)
 
97
        # TODO FOR WRITE TRANSACTIONS: this should be done in a callback
 
98
        # from the transaction, when it decides to save.
 
99
        sio = StringIO()
 
100
        write_weave_v5(weave, sio)
 
101
        sio.seek(0)
 
102
 
 
103
        self._put(file_id, sio)
 
104
 
 
105
 
 
106
    def add_text(self, file_id, rev_id, new_lines, parents, transaction):
 
107
        w = self.get_weave_or_empty(file_id, transaction)
 
108
        parent_idxs = map(w.lookup, parents)
 
109
        w.add(rev_id, parent_idxs, new_lines)
 
110
        self.put_weave(file_id, w, transaction)
 
111
        
 
112
    def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents,
 
113
                           transaction):
 
114
        w = self.get_weave_or_empty(file_id, transaction)
 
115
        parent_idxs = map(w.lookup, parents)
 
116
        w.add_identical(old_rev_id, new_rev_id, parent_idxs)
 
117
        self.put_weave(file_id, w, transaction)
 
118
     
 
119
    def copy_multi(self, from_store, file_ids):
 
120
        assert isinstance(from_store, WeaveStore)
 
121
        for f in file_ids:
 
122
            mutter("copy weave {%s} into %s", f, self)
 
123
            self._put(f, from_store._get(f))