Merge branch '8784-dir-listings'
[arvados.git] / services / fuse / arvados_fuse / fresh.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import time
6 import ciso8601
7 import calendar
8 import functools
9
10 def convertTime(t):
11     """Parse Arvados timestamp to unix time."""
12     if not t:
13         return 0
14     try:
15         return calendar.timegm(ciso8601.parse_datetime_unaware(t).timetuple())
16     except (TypeError, ValueError):
17         return 0
18
19 def use_counter(orig_func):
20     @functools.wraps(orig_func)
21     def use_counter_wrapper(self, *args, **kwargs):
22         try:
23             self.inc_use()
24             return orig_func(self, *args, **kwargs)
25         finally:
26             self.dec_use()
27     return use_counter_wrapper
28
29 def check_update(orig_func):
30     @functools.wraps(orig_func)
31     def check_update_wrapper(self, *args, **kwargs):
32         self.checkupdate()
33         return orig_func(self, *args, **kwargs)
34     return check_update_wrapper
35
36 class FreshBase(object):
37     """Base class for maintaining object lifecycle.
38
39     Functions include:
40
41     * Indicate if an object is up to date (stale() == false) or needs to be
42       updated sets stale() == True).  Use invalidate() to mark the object as
43       stale.  An object is also automatically stale if it has not been updated
44       in `_poll_time` seconds.
45
46     * Record access time (atime) timestamp
47
48     * Manage internal use count used by the inode cache ("inc_use" and
49       "dec_use").  An object which is in use cannot be cleared by the inode
50       cache.
51
52     * Manage the kernel reference count ("inc_ref" and "dec_ref").  An object
53       which is referenced by the kernel cannot have its inode entry deleted.
54
55     * Record cache footprint, cache priority
56
57     * Record Arvados uuid at the time the object is placed in the cache
58
59     * Clear the object contents (invalidates the object)
60
61     """
62     def __init__(self):
63         self._stale = True
64         self._poll = False
65         self._last_update = time.time()
66         self._atime = time.time()
67         self._poll_time = 60
68         self.use_count = 0
69         self.ref_count = 0
70         self.dead = False
71         self.cache_size = 0
72         self.cache_uuid = None
73         self.allow_attr_cache = True
74         self.allow_dirent_cache = True
75
76     def invalidate(self):
77         """Indicate that object contents should be refreshed from source."""
78         self._stale = True
79
80     def kernel_invalidate(self):
81         """Indicate that an invalidation for this object should be sent to the kernel."""
82         pass
83
84     # Test if the entries dict is stale.
85     def stale(self):
86         if self._stale:
87             return True
88         if self._poll:
89             return (self._last_update + self._poll_time) < self._atime
90         return False
91
92     def fresh(self):
93         self._stale = False
94         self._last_update = time.time()
95
96     def atime(self):
97         return self._atime
98
99     def persisted(self):
100         return False
101
102     def clear(self):
103         pass
104
105     def in_use(self):
106         return self.use_count > 0
107
108     def inc_use(self):
109         self.use_count += 1
110
111     def dec_use(self):
112         self.use_count -= 1
113
114     def inc_ref(self):
115         self.ref_count += 1
116         return self.ref_count
117
118     def dec_ref(self, n):
119         self.ref_count -= n
120         return self.ref_count
121
122     def has_ref(self, only_children):
123         """Determine if there are any kernel references to this
124         object or its children.
125
126         If only_children is True, ignore refcount of self and only consider
127         children.
128         """
129         if only_children:
130             return False
131         else:
132             return self.ref_count > 0
133
134     def objsize(self):
135         return 0
136
137     def uuid(self):
138         return None
139
140     def finalize(self):
141         pass