~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-09 23:42:12 UTC
  • Revision ID: robertc@robertcollins.net-20051009234212-7973344d900afb0b
merge in niemeyers prefixed-store patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
 
23
23
from cStringIO import StringIO
 
24
from stat import ST_MODE, S_ISDIR
24
25
import os
25
26
import errno
26
27
 
27
28
from bzrlib.weavefile import read_weave, write_weave_v5
28
29
from bzrlib.weave import Weave
29
 
from bzrlib.store import Store
 
30
from bzrlib.store import Store, hash_prefix
30
31
from bzrlib.atomicfile import AtomicFile
31
 
from bzrlib.errors import NoSuchFile
 
32
from bzrlib.errors import NoSuchFile, FileExists
32
33
from bzrlib.trace import mutter
33
34
 
34
35
 
39
40
    """
40
41
    FILE_SUFFIX = '.weave'
41
42
 
42
 
    def __init__(self, transport):
 
43
    def __init__(self, transport, prefixed=False):
43
44
        self._transport = transport
 
45
        self._prefixed = prefixed
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
 
53
 
 
54
    def _iter_relpaths(self):
 
55
        transport = self._transport
 
56
        queue = list(transport.list_dir('.'))
 
57
        while queue:
 
58
            relpath = queue.pop(0)
 
59
            st = transport.stat(relpath)
 
60
            if S_ISDIR(st[ST_MODE]):
 
61
                for i, basename in enumerate(transport.list_dir(relpath)):
 
62
                    queue.insert(i, relpath+'/'+basename)
 
63
            else:
 
64
                yield relpath, st
48
65
 
49
66
    def __iter__(self):
50
67
        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
 
68
        for relpath, st in self._iter_relpaths():
 
69
            if relpath.endswith(WeaveStore.FILE_SUFFIX):
 
70
                yield os.path.basename(relpath[:-l])
55
71
 
56
72
    def __contains__(self, fileid):
57
73
        """"""
61
77
        return self._transport.get(self.filename(file_id))
62
78
 
63
79
    def _put(self, file_id, f):
 
80
        if self._prefixed:
 
81
            try:
 
82
                self._transport.mkdir(hash_prefix(file_id))
 
83
            except FileExists:
 
84
                pass
64
85
        return self._transport.put(self.filename(file_id), f)
65
86
 
66
87
    def get_weave(self, file_id, transaction):