Patchwork [1,of,6,V2] localrepo: add isfilecached to check filecache-ed property is already cached

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 10, 2017, 2:18 p.m.
Message ID <d5ba581532af80641a88.1499696294@speaknoevil>
Download mbox | patch
Permalink /patch/22200/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - July 10, 2017, 2:18 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1499695791 -32400
#      Mon Jul 10 23:09:51 2017 +0900
# Node ID d5ba581532af80641a881860c1dc718773892a40
# Parent  ccb3e5399921db16e95e7429cc2fb9ef82ee846e
localrepo: add isfilecached to check filecache-ed property is already cached

isfilecached() encapsulates internal implementation of filecache-ed
property.

"name in repo.unfiltered().__dict__" or so can't be used for this
purpose, because corresponded entry in __dict__ might be discarded by
repo.invalidate(), repo.invalidatedirstate() or so (fsmonitor does so,
for example).

This patch makes isfilecached() return not only whether filecache-ed
property is already cached, but also already cached value (or None),
in order to avoid subsequent access to cached object via "repo.NAME",
which prevents main Mercurial procedure after reposetup() from
validating cache.
Yuya Nishihara - July 11, 2017, 1:59 p.m.
On Mon, 10 Jul 2017 23:18:14 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1499695791 -32400
> #      Mon Jul 10 23:09:51 2017 +0900
> # Node ID d5ba581532af80641a881860c1dc718773892a40
> # Parent  ccb3e5399921db16e95e7429cc2fb9ef82ee846e
> localrepo: add isfilecached to check filecache-ed property is already cached

Queued, thanks.

It'll be nice if eventually filecaches can be extended by a plain class
inheritance, but that would require a fair amount of work.

> diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
> --- a/mercurial/localrepo.py
> +++ b/mercurial/localrepo.py
> @@ -103,6 +103,16 @@ class storecache(_basefilecache):
>      def join(self, obj, fname):
>          return obj.sjoin(fname)
>  
> +def isfilecached(repo, name):
> +    """check if a repo has already cached "name" filecache-ed property
> +
> +    This returns (cachedobj-or-None, iscached) tuple.
> +    """
> +    cacheentry = repo.unfiltered()._filecache.get(name, None)
> +    if not cacheentry:
> +        return None, False
> +    return cacheentry.obj, True

I prefer not calling this an "is" function because the return value can't
be evaluated as a boolean. I don't have a better alternative, but getfilecached
or getfilecachedobj seems less bad.

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -103,6 +103,16 @@  class storecache(_basefilecache):
     def join(self, obj, fname):
         return obj.sjoin(fname)
 
+def isfilecached(repo, name):
+    """check if a repo has already cached "name" filecache-ed property
+
+    This returns (cachedobj-or-None, iscached) tuple.
+    """
+    cacheentry = repo.unfiltered()._filecache.get(name, None)
+    if not cacheentry:
+        return None, False
+    return cacheentry.obj, True
+
 class unfilteredpropertycache(util.propertycache):
     """propertycache that apply to unfiltered repo only"""