1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-25 17:27:31 +00:00

list: only load cache if needed

This commit is contained in:
Marian Beermann 2017-03-02 00:24:22 +01:00
parent ec1304bd36
commit c50ffc21b0
2 changed files with 25 additions and 7 deletions

View file

@ -1032,9 +1032,6 @@ def write(bytestring):
def _list_archive(self, args, repository, manifest, key, write): def _list_archive(self, args, repository, manifest, key, write):
matcher, _ = self.build_matcher(args.patterns, args.paths) matcher, _ = self.build_matcher(args.patterns, args.paths)
with Cache(repository, key, manifest, lock_wait=self.lock_wait) as cache:
archive = Archive(repository, key, manifest, args.location.archive, cache=cache,
consider_part_files=args.consider_part_files)
if args.format is not None: if args.format is not None:
format = args.format format = args.format
elif args.short: elif args.short:
@ -1042,11 +1039,23 @@ def _list_archive(self, args, repository, manifest, key, write):
else: else:
format = "{mode} {user:6} {group:6} {size:8} {isomtime} {path}{extra}{NL}" format = "{mode} {user:6} {group:6} {size:8} {isomtime} {path}{extra}{NL}"
def _list_inner(cache):
archive = Archive(repository, key, manifest, args.location.archive, cache=cache,
consider_part_files=args.consider_part_files)
formatter = ItemFormatter(archive, format, json=args.json) formatter = ItemFormatter(archive, format, json=args.json)
write(safe_encode(formatter.begin())) write(safe_encode(formatter.begin()))
for item in archive.iter_items(lambda item: matcher.match(item.path)): for item in archive.iter_items(lambda item: matcher.match(item.path)):
write(safe_encode(formatter.format_item(item))) write(safe_encode(formatter.format_item(item)))
write(safe_encode(formatter.end())) write(safe_encode(formatter.end()))
# Only load the cache if it will be used
if ItemFormatter.format_needs_cache(format):
with Cache(repository, key, manifest, lock_wait=self.lock_wait) as cache:
_list_inner(cache)
else:
_list_inner(cache=None)
return self.exit_code return self.exit_code
def _list_repository(self, args, manifest, write): def _list_repository(self, args, manifest, write):

View file

@ -1617,6 +1617,10 @@ class ItemFormatter(BaseFormatter):
('health', ) ('health', )
) )
KEYS_REQUIRING_CACHE = (
'dsize', 'dcsize', 'unique_chunks',
)
@classmethod @classmethod
def available_keys(cls): def available_keys(cls):
class FakeArchive: class FakeArchive:
@ -1648,6 +1652,11 @@ def keys_help(cls):
assert not keys, str(keys) assert not keys, str(keys)
return "\n".join(help) return "\n".join(help)
@classmethod
def format_needs_cache(cls, format):
format_keys = {f[1] for f in Formatter().parse(format)}
return any(key in cls.KEYS_REQUIRING_CACHE for key in format_keys)
def __init__(self, archive, format, *, json=False): def __init__(self, archive, format, *, json=False):
self.archive = archive self.archive = archive
self.json = json self.json = json