~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-10 23:18:27 UTC
  • mfrom: (1437)
  • mto: This revision was merged to the branch mainline in revision 1438.
  • Revision ID: robertc@robertcollins.net-20051010231827-f9e2dda2e92bf565
mergeĀ fromĀ upstream

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):
43
43
        self._transport = transport
 
44
        self._prefixed = prefixed
44
45
 
45
46
    def filename(self, file_id):
46
47
        """Return the path relative to the transport root."""
47
 
        return file_id + WeaveStore.FILE_SUFFIX
 
48
        if self._prefixed:
 
49
            return hash_prefix(file_id) + file_id + WeaveStore.FILE_SUFFIX
 
50
        else:
 
51
            return file_id + WeaveStore.FILE_SUFFIX
48
52
 
49
53
    def __iter__(self):
50
54
        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
        for relpath, st in self._iter_relpaths():
 
56
            if relpath.endswith(WeaveStore.FILE_SUFFIX):
 
57
                yield os.path.basename(relpath[:-l])
55
58
 
56
59
    def __contains__(self, fileid):
57
60
        """"""
61
64
        return self._transport.get(self.filename(file_id))
62
65
 
63
66
    def _put(self, file_id, f):
 
67
        if self._prefixed:
 
68
            try:
 
69
                self._transport.mkdir(hash_prefix(file_id))
 
70
            except FileExists:
 
71
                pass
64
72
        return self._transport.put(self.filename(file_id), f)
65
73
 
66
74
    def get_weave(self, file_id, transaction):
73
81
        transaction.register_clean(w)
74
82
        return w
75
83
 
76
 
 
77
84
    def get_lines(self, file_id, rev_id, transaction):
78
85
        """Return text from a particular version of a weave.
79
86
 
99
106
        sio = StringIO()
100
107
        write_weave_v5(weave, sio)
101
108
        sio.seek(0)
102
 
 
103
109
        self._put(file_id, sio)
104
110
 
105
 
 
106
111
    def add_text(self, file_id, rev_id, new_lines, parents, transaction):
107
112
        w = self.get_weave_or_empty(file_id, transaction)
108
113
        parent_idxs = map(w.lookup, parents)