~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/__init__.py

  • Committer: John Arbash Meinel
  • Date: 2005-11-10 01:10:01 UTC
  • mto: This revision was merged to the branch mainline in revision 1508.
  • Revision ID: john@arbash-meinel.com-20051110011001-68e7ffc01241d226
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
    def __len__(self):
55
55
        raise NotImplementedError('Children should define their length')
56
56
 
57
 
    def get(self, file_id, suffix=None):
 
57
    def get(self, fileid, suffix=None):
58
58
        """Returns a file reading from a particular entry.
59
59
        
60
 
        If suffix is present, retrieve the named suffix for file_id.
 
60
        If suffix is present, retrieve the named suffix for fileid.
61
61
        """
62
62
        raise NotImplementedError
63
63
 
64
64
    def __getitem__(self, fileid):
65
 
        """DEPRECATED. Please use .get(file_id) instead."""
 
65
        """DEPRECATED. Please use .get(fileid) instead."""
66
66
        raise NotImplementedError
67
67
 
68
68
    #def __contains__(self, fileid):
76
76
        """Add a file object f to the store accessible from the given fileid"""
77
77
        raise NotImplementedError('Children of Store must define their method of adding entries.')
78
78
 
79
 
    def has_id(self, file_id, suffix=None):
80
 
        """Return True or false for the presence of file_id in the store.
 
79
    def has_id(self, fileid, suffix=None):
 
80
        """Return True or false for the presence of fileid in the store.
81
81
        
82
82
        suffix, if present, is a per file suffix, i.e. for digital signature 
83
83
        data."""
150
150
        """
151
151
        mutter("add store entry %r" % (fileid))
152
152
        
153
 
        if suffix is not None:
154
 
            fn = self._relpath(fileid, [suffix])
155
 
        else:
156
 
            fn = self._relpath(fileid)
157
 
        if self._transport.has(fn):
158
 
            raise BzrError("store %r already contains id %r" % (self._transport.base, fileid))
 
153
        names = self._id_to_names(fileid, suffix)
 
154
        if self._transport.has_any(names):
 
155
            raise BzrError("store %r already contains id %r" 
 
156
                           % (self._transport.base, fileid))
159
157
 
160
158
        if self._prefixed:
161
159
            try:
163
161
            except errors.FileExists:
164
162
                pass
165
163
 
166
 
        self._add(fn, f)
 
164
        self._add(names[0], f)
 
165
 
 
166
    def _add(self, relpath, f):
 
167
        """Actually add the file to the given location.
 
168
        This should be overridden by children.
 
169
        """
 
170
        raise NotImplementedError('children need to implement this function.')
167
171
 
168
172
    def _check_fileid(self, fileid):
169
173
        if not isinstance(fileid, basestring):
171
175
        if '\\' in fileid or '/' in fileid:
172
176
            raise ValueError("invalid store id %r" % fileid)
173
177
 
174
 
    def has_id(self, fileid, suffix=None):
175
 
        """See Store.has_id."""
 
178
    def _id_to_names(self, fileid, suffix):
 
179
        """Return the names in the expected order"""
176
180
        if suffix is not None:
177
181
            fn = self._relpath(fileid, [suffix])
178
182
        else:
179
183
            fn = self._relpath(fileid)
180
 
        return self._transport.has(fn)
 
184
 
 
185
        fn_gz = fn + '.gz'
 
186
        if self._compressed:
 
187
            return fn_gz, fn
 
188
        else:
 
189
            return fn, fn_gz
 
190
 
 
191
    def has_id(self, fileid, suffix=None):
 
192
        """See Store.has_id."""
 
193
        return self._transport.has_any(self._id_to_names(fileid, suffix))
 
194
 
 
195
    def _get_name(self, fileid, suffix=None):
 
196
        """A special check, which returns the name of an existing file.
 
197
        
 
198
        This is similar in spirit to 'has_id', but it is designed
 
199
        to return information about which file the store has.
 
200
        """
 
201
        for name in self._id_to_names(fileid, suffix=suffix):
 
202
            if self._transport.has(name):
 
203
                return name
 
204
        return None
181
205
 
182
206
    def _get(self, filename):
183
207
        """Return an vanilla file stream for clients to read from.
189
213
 
190
214
    def get(self, fileid, suffix=None):
191
215
        """See Store.get()."""
192
 
        if suffix is None or suffix == 'gz':
193
 
            fn = self._relpath(fileid)
194
 
        else:
195
 
            fn = self._relpath(fileid, [suffix])
196
 
        try:
197
 
            return self._get(fn)
198
 
        except errors.NoSuchFile:
199
 
            raise KeyError(fileid)
 
216
        names = self._id_to_names(fileid, suffix)
 
217
        for name in names:
 
218
            try:
 
219
                return self._get(name)
 
220
            except errors.NoSuchFile:
 
221
                pass
 
222
        raise KeyError(fileid)
200
223
 
201
 
    def __init__(self, a_transport, prefixed=False):
 
224
    def __init__(self, a_transport, prefixed=False, compressed=False):
202
225
        assert isinstance(a_transport, transport.Transport)
203
226
        super(TransportStore, self).__init__()
204
227
        self._transport = a_transport
205
228
        self._prefixed = prefixed
206
 
        # conflating the .gz extension and user suffixes was a mistake.
207
 
        # RBC 20051017 - TODO SOON, separate them again.
 
229
        self._compressed = compressed
208
230
        self._suffixes = set()
209
231
 
210
232
    def _iter_files_recursive(self):
229
251
    def __len__(self):
230
252
        return len(list(self.__iter__()))
231
253
 
232
 
    def _relpath(self, fileid, suffixes=[]):
 
254
    def _relpath(self, fileid, suffixes=None):
233
255
        self._check_fileid(fileid)
234
 
        for suffix in suffixes:
235
 
            if not suffix in self._suffixes:
236
 
                raise ValueError("Unregistered suffix %r" % suffix)
237
 
            self._check_fileid(suffix)
 
256
        if suffixes:
 
257
            for suffix in suffixes:
 
258
                if not suffix in self._suffixes:
 
259
                    raise ValueError("Unregistered suffix %r" % suffix)
 
260
                self._check_fileid(suffix)
 
261
        else:
 
262
            suffixes = []
238
263
        if self._prefixed:
239
264
            path = [hash_prefix(fileid) + fileid]
240
265
        else:
257
282
    def register_suffix(self, suffix):
258
283
        """Register a suffix as being expected in this store."""
259
284
        self._check_fileid(suffix)
 
285
        if suffix == 'gz':
 
286
            raise ValueError('You cannot register the "gz" suffix.')
260
287
        self._suffixes.add(suffix)
261
288
 
262
289
    def total_size(self):
303
330
        if self.cache_store.has_id(fileid, suffix):
304
331
            return True
305
332
        if self.source_store.has_id(fileid, suffix):
306
 
            # We could copy at this time
 
333
            # We could asynchronously copy at this time
307
334
            return True
308
335
        return False
309
336
 
316
343
    ids = [f for f in store_from]
317
344
    store_to.copy_multi(store_from, ids)
318
345
 
319
 
def hash_prefix(file_id):
320
 
    return "%02x/" % (adler32(file_id) & 0xff)
 
346
def hash_prefix(fileid):
 
347
    return "%02x/" % (adler32(fileid) & 0xff)
321
348