1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
1 |
# Copyright (C) 2005 by Canonical Development Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""
|
|
18 |
A store that keeps the full text of every version.
|
|
19 |
||
20 |
This store keeps uncompressed versions of the full text. It does not
|
|
21 |
do any sort of delta compression.
|
|
22 |
"""
|
|
23 |
||
24 |
import os, tempfile |
|
25 |
||
26 |
import bzrlib.store |
|
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
27 |
from bzrlib.store import hash_prefix |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
28 |
from bzrlib.trace import mutter |
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
29 |
from bzrlib.errors import BzrError, FileExists |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
30 |
|
31 |
from cStringIO import StringIO |
|
1430
by Robert Collins
touchup the prefixed-store patch |
32 |
from stat import ST_SIZE |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
33 |
|
1092.2.24
by Robert Collins
merge from martins newformat branch - brings in transport abstraction |
34 |
|
35 |
class TextStore(bzrlib.store.TransportStore): |
|
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
36 |
"""Store that holds files indexed by unique names.
|
37 |
||
38 |
Files can be added, but not modified once they are in. Typically
|
|
39 |
the hash is used as the name, or something else known to be unique,
|
|
40 |
such as a UUID.
|
|
41 |
||
1393.2.4
by John Arbash Meinel
All tests pass. |
42 |
Files are stored uncompressed, with no delta compression.
|
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
43 |
"""
|
44 |
||
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
45 |
def __init__(self, transport, prefixed=False): |
1092.2.24
by Robert Collins
merge from martins newformat branch - brings in transport abstraction |
46 |
super(TextStore, self).__init__(transport) |
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
47 |
self._prefixed = prefixed |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
48 |
|
49 |
def _check_fileid(self, fileid): |
|
50 |
if not isinstance(fileid, basestring): |
|
51 |
raise TypeError('Fileids should be a string type: %s %r' % (type(fileid), fileid)) |
|
52 |
if '\\' in fileid or '/' in fileid: |
|
53 |
raise ValueError("invalid store id %r" % fileid) |
|
54 |
||
55 |
def _relpath(self, fileid): |
|
56 |
self._check_fileid(fileid) |
|
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
57 |
if self._prefixed: |
58 |
return hash_prefix(fileid) + fileid |
|
59 |
else: |
|
60 |
return fileid |
|
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
61 |
|
62 |
def add(self, f, fileid): |
|
63 |
"""Add contents of a file into the store.
|
|
64 |
||
65 |
f -- A file-like object, or string
|
|
66 |
"""
|
|
67 |
mutter("add store entry %r" % (fileid)) |
|
68 |
||
69 |
fn = self._relpath(fileid) |
|
70 |
if self._transport.has(fn): |
|
71 |
raise BzrError("store %r already contains id %r" % (self._transport.base, fileid)) |
|
72 |
||
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
73 |
if self._prefixed: |
74 |
try: |
|
75 |
self._transport.mkdir(hash_prefix(fileid)) |
|
76 |
except FileExists: |
|
77 |
pass
|
|
78 |
||
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
79 |
self._transport.put(fn, f) |
80 |
||
81 |
def __contains__(self, fileid): |
|
82 |
""""""
|
|
83 |
fn = self._relpath(fileid) |
|
84 |
return self._transport.has(fn) |
|
85 |
||
86 |
def has(self, fileids, pb=None): |
|
87 |
"""Return True/False for each entry in fileids.
|
|
88 |
||
89 |
:param fileids: A List or generator yielding file ids.
|
|
90 |
:return: A generator or list returning True/False for each entry.
|
|
91 |
"""
|
|
92 |
relpaths = (self._relpath(fid) for fid in fileids) |
|
93 |
return self._transport.has_multi(relpaths, pb=pb) |
|
94 |
||
95 |
def get(self, fileids, permit_failure=False, pb=None): |
|
96 |
"""Return a set of files, one for each requested entry.
|
|
97 |
|
|
98 |
TODO: Write some tests to make sure that permit_failure is
|
|
99 |
handled correctly.
|
|
100 |
||
101 |
TODO: What should the exception be for a missing file?
|
|
102 |
KeyError, or NoSuchFile?
|
|
103 |
"""
|
|
104 |
||
105 |
# This next code gets a bit hairy because it can allow
|
|
106 |
# to not request a file which doesn't seem to exist.
|
|
107 |
# Also, the same fileid may be requested twice, so we
|
|
108 |
# can't just build up a map.
|
|
109 |
rel_paths = [self._relpath(fid) for fid in fileids] |
|
110 |
is_requested = [] |
|
111 |
||
112 |
if permit_failure: |
|
113 |
existing_paths = [] |
|
114 |
for path, has in zip(rel_paths, |
|
115 |
self._transport.has_multi(rel_paths)): |
|
116 |
if has: |
|
117 |
existing_paths.append(path) |
|
118 |
is_requested.append(True) |
|
119 |
else: |
|
120 |
is_requested.append(False) |
|
121 |
#mutter('Retrieving %s out of %s' % (existing_paths, rel_paths))
|
|
122 |
else: |
|
123 |
#mutter('Retrieving all %s' % (rel_paths, ))
|
|
124 |
existing_paths = rel_paths |
|
125 |
is_requested = [True for x in rel_paths] |
|
126 |
||
127 |
count = 0 |
|
128 |
for f in self._transport.get_multi(existing_paths, pb=pb): |
|
129 |
assert count < len(is_requested) |
|
130 |
while not is_requested[count]: |
|
131 |
yield None |
|
132 |
count += 1 |
|
1393.2.4
by John Arbash Meinel
All tests pass. |
133 |
yield f |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
134 |
count += 1 |
135 |
||
136 |
while count < len(is_requested): |
|
137 |
yield None |
|
138 |
count += 1 |
|
139 |
||
140 |
def __iter__(self): |
|
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
141 |
for relpath, st in self._iter_relpaths(): |
142 |
yield os.path.basename(relpath) |
|
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
143 |
|
144 |
def __len__(self): |
|
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
145 |
return len(list(self._iter_relpath())) |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
146 |
|
147 |
def total_size(self): |
|
148 |
"""Return (count, bytes)
|
|
149 |
||
150 |
This is the (compressed) size stored on disk, not the size of
|
|
151 |
the content."""
|
|
152 |
total = 0 |
|
153 |
count = 0 |
|
1429
by Robert Collins
merge in niemeyers prefixed-store patch |
154 |
for relpath, st in self._iter_relpaths(): |
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
155 |
count += 1 |
156 |
total += st[ST_SIZE] |
|
157 |
||
158 |
return count, total |
|
159 |
||
1092.2.24
by Robert Collins
merge from martins newformat branch - brings in transport abstraction |
160 |
|
1393.2.2
by John Arbash Meinel
Updated stores to use Transport |
161 |
class ScratchTextStore(TextStore): |
162 |
"""Self-destructing test subclass of TextStore.
|
|
163 |
||
164 |
The Store only exists for the lifetime of the Python object.
|
|
165 |
Obviously you should not put anything precious in it.
|
|
166 |
"""
|
|
167 |
def __init__(self): |
|
168 |
from transport import transport |
|
169 |
super(ScratchTextStore, self).__init__(transport(tempfile.mkdtemp())) |
|
170 |
||
171 |
def __del__(self): |
|
172 |
self._transport.delete_multi(self._transport.list_dir('.')) |
|
173 |
os.rmdir(self._transport.base) |
|
174 |
mutter("%r destroyed" % self) |
|
175 |