~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/weave.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-09 06:49:00 UTC
  • Revision ID: mbp@sourcefrog.net-20050309064900-74935ffb7350b24b
import more files from baz

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
 
        self._cache = {}
45
 
        self.enable_cache = False
46
 
 
47
 
 
48
 
    def filename(self, file_id):
49
 
        """Return the path relative to the transport root."""
50
 
        return file_id + WeaveStore.FILE_SUFFIX
51
 
 
52
 
    def __iter__(self):
53
 
        l = len(WeaveStore.FILE_SUFFIX)
54
 
        for f in self._transport.list_dir('.'):
55
 
            if f.endswith(WeaveStore.FILE_SUFFIX):
56
 
                f = f[:-l]
57
 
                yield f
58
 
 
59
 
    def __contains__(self, fileid):
60
 
        """"""
61
 
        return self._transport.has(self.filename(fileid))
62
 
 
63
 
    def _get(self, file_id):
64
 
        return self._transport.get(self.filename(file_id))
65
 
 
66
 
    def _put(self, file_id, f):
67
 
        return self._transport.put(self.filename(file_id), f)
68
 
 
69
 
    def get_weave(self, file_id):
70
 
        if self.enable_cache:
71
 
            if file_id in self._cache:
72
 
                mutter("cache hit in %s for %s", self, file_id)
73
 
                return self._cache[file_id]
74
 
        w = read_weave(self._get(file_id))
75
 
        if self.enable_cache:
76
 
            self._cache[file_id] = w
77
 
        return w
78
 
 
79
 
 
80
 
    def get_lines(self, file_id, rev_id):
81
 
        """Return text from a particular version of a weave.
82
 
 
83
 
        Returned as a list of lines."""
84
 
        w = self.get_weave(file_id)
85
 
        return w.get(w.lookup(rev_id))
86
 
    
87
 
 
88
 
    def get_weave_or_empty(self, file_id):
89
 
        """Return a weave, or an empty one if it doesn't exist.""" 
90
 
        try:
91
 
            inf = self._get(file_id)
92
 
        except NoSuchFile:
93
 
            return Weave(weave_name=file_id)
94
 
        else:
95
 
            return read_weave(inf)
96
 
    
97
 
 
98
 
    def put_weave(self, file_id, weave):
99
 
        """Write back a modified weave"""
100
 
        if self.enable_cache:
101
 
            self._cache[file_id] = weave
102
 
 
103
 
        sio = StringIO()
104
 
        write_weave_v5(weave, sio)
105
 
        sio.seek(0)
106
 
 
107
 
        self._put(file_id, sio)
108
 
 
109
 
 
110
 
    def add_text(self, file_id, rev_id, new_lines, parents):
111
 
        w = self.get_weave_or_empty(file_id)
112
 
        parent_idxs = map(w.lookup, parents)
113
 
        w.add(rev_id, parent_idxs, new_lines)
114
 
        self.put_weave(file_id, w)
115
 
        
116
 
    def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents):
117
 
        w = self.get_weave_or_empty(file_id)
118
 
        parent_idxs = map(w.lookup, parents)
119
 
        w.add_identical(old_rev_id, new_rev_id, parent_idxs)
120
 
        self.put_weave(file_id, w)
121
 
     
122
 
    def copy_multi(self, from_store, file_ids):
123
 
        assert isinstance(from_store, WeaveStore)
124
 
        for f in file_ids:
125
 
            mutter("copy weave {%s} into %s", f, self)
126
 
            self._put(f, from_store._get(f))