Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: chrome/common/extensions/docs/server2/caching_file_system.py

Issue 1151283007: Docserver overhaul: Gitiles away from me. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self-nits Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import logging 5 import logging
6 import posixpath 6 import posixpath
7 import sys 7 import sys
8 8
9 from file_system import FileSystem, StatInfo, FileNotFoundError 9 from file_system import FileSystem, StatInfo, FileNotFoundError
10 from future import All, Future 10 from future import All, Future
11 from path_util import AssertIsDirectory, IsDirectory, ToDirectory 11 from path_util import AssertIsDirectory, IsDirectory, ToDirectory
12 from third_party.json_schema_compiler.memoize import memoize 12 from third_party.json_schema_compiler.memoize import memoize
13 13
14 14
15 class CachingFileSystem(FileSystem): 15 class CachingFileSystem(FileSystem):
16 '''FileSystem which implements a caching layer on top of |file_system|. If 16 '''FileSystem which implements a caching layer on top of |file_system|. If
17 |fail_on_miss| is True then cache misses throw a FileNotFoundError rather than 17 |fail_on_miss| is True then cache misses throw a FileNotFoundError rather than
18 falling back onto the underlying FileSystem. 18 falling back onto the underlying FileSystem.
19 19
20 If the underlying FileSystem is versioned (i.e., it implements GetVersion to 20 If the underlying FileSystem is versioned (i.e., it implements GetVersion to
not at google - send to devlin 2015/06/04 22:40:45 update this comment?
Ken Rockot(use gerrit already) 2015/06/05 00:21:50 DELETED!
21 return something other than None), this will create a persistent stat cache 21 return something other than None), this will create a persistent stat cache
22 (keyed on the FileSystem instance's version) as an additional optimization. 22 (keyed on the FileSystem instance's version) as an additional optimization.
23 ''' 23 '''
24 def __init__(self, file_system, object_store_creator, fail_on_miss=False): 24 def __init__(self, file_system, object_store_creator, fail_on_miss=False):
25 self._file_system = file_system 25 self._file_system = file_system
26 self._fail_on_miss = fail_on_miss 26 self._fail_on_miss = fail_on_miss
27 def create_object_store(category, try_versioning=False, **optargs): 27 def create_object_store(category, start_empty=True):
28 version = file_system.GetVersion()
29 versioned = try_versioning and version is not None
30 if versioned:
31 identity = '%s/%s' % (file_system.GetIdentity(), version)
32 else:
33 identity = file_system.GetIdentity()
34 optargs['start_empty'] = optargs.get('start_empty', not versioned)
35 return object_store_creator.Create( 28 return object_store_creator.Create(
36 CachingFileSystem, 29 CachingFileSystem,
37 category='%s/%s' % (identity, category), 30 category='%s/%s' % (file_system.GetIdentity(), category),
38 **optargs) 31 start_empty=start_empty)
39 self._stat_cache = create_object_store('stat', try_versioning=True) 32 # We only start the stat cache empty if |fail_on_miss| is False, i.e. if
40 # The read caches can start populated (start_empty=False) because file 33 # we're NOT running on a live instance and we can afford to fall back onto
41 # updates are picked up by the stat, so it doesn't need the force-refresh 34 # the underlying FileSystem impl.
42 # which starting empty is designed for. Without this optimisation, cron 35 self._stat_cache = create_object_store('stat', start_empty=not fail_on_miss)
43 # runs are extra slow.
44 self._read_cache = create_object_store('read', start_empty=False) 36 self._read_cache = create_object_store('read', start_empty=False)
45 self._walk_cache = create_object_store('walk', start_empty=False) 37 self._walk_cache = create_object_store('walk', start_empty=False)
46 38
47 def Refresh(self): 39 def Refresh(self):
48 return self._file_system.Refresh() 40 return self._file_system.Refresh()
49 41
50 def StatAsync(self, path): 42 def StatAsync(self, path):
51 '''Stats the directory given, or if a file is given, stats the file's parent 43 '''Stats the directory given, or if a file is given, stats the file's parent
52 directory to get info about the file. 44 directory to get info about the file.
53 ''' 45 '''
(...skipping 16 matching lines...) Expand all
70 return StatInfo(file_version) 62 return StatInfo(file_version)
71 63
72 def raise_cache_miss(path): 64 def raise_cache_miss(path):
73 raise FileNotFoundError('Got cache miss when trying to stat %s' % path) 65 raise FileNotFoundError('Got cache miss when trying to stat %s' % path)
74 66
75 dir_stat = self._stat_cache.Get(dir_path).Get() 67 dir_stat = self._stat_cache.Get(dir_path).Get()
76 if dir_stat is not None: 68 if dir_stat is not None:
77 return Future(callback=lambda: make_stat_info(dir_stat)) 69 return Future(callback=lambda: make_stat_info(dir_stat))
78 70
79 if self._fail_on_miss: 71 if self._fail_on_miss:
80 logging.warning('Bailing on stat cache miss for %s' % dir_path) 72 logging.warning('Bailing on stat cache miss for %s on %s' %
73 (dir_path, self.GetIdentity()))
81 return Future(callback=lambda: raise_cache_miss(dir_path)) 74 return Future(callback=lambda: raise_cache_miss(dir_path))
82 75
83 def next(dir_stat): 76 def next(dir_stat):
84 assert dir_stat is not None # should have raised a FileNotFoundError 77 assert dir_stat is not None # should have raised a FileNotFoundError
85 # We only ever need to cache the dir stat. 78 # We only ever need to cache the dir stat.
86 self._stat_cache.Set(dir_path, dir_stat) 79 self._stat_cache.Set(dir_path, dir_stat)
87 return make_stat_info(dir_stat) 80 return make_stat_info(dir_stat)
88 return self._MemoizedStatAsyncFromFileSystem(dir_path).Then(next) 81 return self._MemoizedStatAsyncFromFileSystem(dir_path).Then(next)
89 82
90 @memoize 83 @memoize
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 (path, data) for path, (data, version) in cached_read_values.iteritems() 127 (path, data) for path, (data, version) in cached_read_values.iteritems()
135 if version is not None and stat_futures[path].Get().version == version) 128 if version is not None and stat_futures[path].Get().version == version)
136 129
137 if skip_not_found: 130 if skip_not_found:
138 # Filter out paths which we know do not exist, i.e. if |path| is in 131 # Filter out paths which we know do not exist, i.e. if |path| is in
139 # |cached_read_values| *and* has a None version, then it doesn't exist. 132 # |cached_read_values| *and* has a None version, then it doesn't exist.
140 # See the above declaration of |cached_read_values| for more information. 133 # See the above declaration of |cached_read_values| for more information.
141 paths = [path for path in paths 134 paths = [path for path in paths
142 if cached_read_values.get(path, (None, True))[1]] 135 if cached_read_values.get(path, (None, True))[1]]
143 136
144 if len(up_to_date_data) == len(paths): 137 remaining_paths = set(paths) - set(up_to_date_data.iterkeys())
138 if len(remaining_paths) == 0:
145 # Everything was cached and up-to-date. 139 # Everything was cached and up-to-date.
146 return Future(value=up_to_date_data) 140 return Future(value=up_to_date_data)
147 141
142 def raise_cache_miss(paths):
143 raise FileNotFoundError('Got cache miss when trying to stat %s' % paths)
144
145 if self._fail_on_miss:
146 # Ignore missing values and return anyway.
147 logging.warn('Read cache miss for %s on %s' %
148 (remaining_paths, self.GetIdentity()))
149 return Future(callback=lambda: raise_cache_miss(remaining_paths))
150
148 def next(new_results): 151 def next(new_results):
149 # Update the cache. This is a path -> (data, version) mapping. 152 # Update the cache. This is a path -> (data, version) mapping.
150 self._read_cache.SetMulti( 153 self._read_cache.SetMulti(
151 dict((path, (new_result, stat_futures[path].Get().version)) 154 dict((path, (new_result, stat_futures[path].Get().version))
152 for path, new_result in new_results.iteritems())) 155 for path, new_result in new_results.iteritems()))
153 # Update the read cache to include files that weren't found, to prevent 156 # Update the read cache to include files that weren't found, to prevent
154 # constantly trying to read a file we now know doesn't exist. 157 # constantly trying to read a file we now know doesn't exist.
155 self._read_cache.SetMulti( 158 self._read_cache.SetMulti(
156 dict((path, (None, None)) for path in paths 159 dict((path, (None, None)) for path in paths
157 if stat_futures[path].Get() is None)) 160 if stat_futures[path].Get() is None))
158 new_results.update(up_to_date_data) 161 new_results.update(up_to_date_data)
159 return new_results 162 return new_results
163
160 # Read in the values that were uncached or old. 164 # Read in the values that were uncached or old.
161 return self._file_system.Read(set(paths) - set(up_to_date_data.iterkeys()), 165 return self._file_system.Read(remaining_paths,
162 skip_not_found=skip_not_found).Then(next) 166 skip_not_found=skip_not_found).Then(next)
163 167
164 def GetCommitID(self): 168 def GetCommitID(self):
165 return self._file_system.GetCommitID() 169 return self._file_system.GetCommitID()
166 170
167 def GetPreviousCommitID(self): 171 def GetPreviousCommitID(self):
168 return self._file_system.GetPreviousCommitID() 172 return self._file_system.GetPreviousCommitID()
169 173
170 def Walk(self, root, depth=-1): 174 def Walk(self, root, depth=-1):
171 '''Overrides FileSystem.Walk() to provide caching functionality. 175 '''Overrides FileSystem.Walk() to provide caching functionality.
(...skipping 18 matching lines...) Expand all
190 return self._file_system.Walk(root, depth=depth, file_lister=file_lister) 194 return self._file_system.Walk(root, depth=depth, file_lister=file_lister)
191 195
192 def GetIdentity(self): 196 def GetIdentity(self):
193 return self._file_system.GetIdentity() 197 return self._file_system.GetIdentity()
194 198
195 def GetVersion(self): 199 def GetVersion(self):
196 return self._file_system.GetVersion() 200 return self._file_system.GetVersion()
197 201
198 def __repr__(self): 202 def __repr__(self):
199 return '%s of <%s>' % (type(self).__name__, repr(self._file_system)) 203 return '%s of <%s>' % (type(self).__name__, repr(self._file_system))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698