From c50ffc21b045837502b7e4007ab174a6525ae778 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Thu, 2 Mar 2017 00:24:22 +0100 Subject: [PATCH] list: only load cache if needed --- src/borg/archiver.py | 23 ++++++++++++++++------- src/borg/helpers.py | 9 +++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 9f98411a4..3961762b5 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -1032,21 +1032,30 @@ def write(bytestring): def _list_archive(self, args, repository, manifest, key, write): matcher, _ = self.build_matcher(args.patterns, args.paths) - with Cache(repository, key, manifest, lock_wait=self.lock_wait) as cache: + if args.format is not None: + format = args.format + elif args.short: + format = "{path}{NL}" + else: + 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) - if args.format is not None: - format = args.format - elif args.short: - format = "{path}{NL}" - else: - format = "{mode} {user:6} {group:6} {size:8} {isomtime} {path}{extra}{NL}" formatter = ItemFormatter(archive, format, json=args.json) write(safe_encode(formatter.begin())) for item in archive.iter_items(lambda item: matcher.match(item.path)): write(safe_encode(formatter.format_item(item))) 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 def _list_repository(self, args, manifest, write): diff --git a/src/borg/helpers.py b/src/borg/helpers.py index 2f4429a35..be1d97410 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -1617,6 +1617,10 @@ class ItemFormatter(BaseFormatter): ('health', ) ) + KEYS_REQUIRING_CACHE = ( + 'dsize', 'dcsize', 'unique_chunks', + ) + @classmethod def available_keys(cls): class FakeArchive: @@ -1648,6 +1652,11 @@ def keys_help(cls): assert not keys, str(keys) 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): self.archive = archive self.json = json