| OLD | NEW |
| 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 posixpath | 5 import posixpath |
| 6 import sys | 6 import sys |
| 7 | 7 |
| 8 from file_system import FileSystem, StatInfo, FileNotFoundError | 8 from file_system import FileSystem, StatInfo, FileNotFoundError |
| 9 from future import Future | 9 from future import Future |
| 10 from path_util import IsDirectory | 10 from path_util import IsDirectory, ToDirectory |
| 11 from third_party.json_schema_compiler.memoize import memoize | 11 from third_party.json_schema_compiler.memoize import memoize |
| 12 | 12 |
| 13 | 13 |
| 14 class CachingFileSystem(FileSystem): | 14 class CachingFileSystem(FileSystem): |
| 15 '''FileSystem which implements a caching layer on top of |file_system|. It's | 15 '''FileSystem which implements a caching layer on top of |file_system|. It's |
| 16 smart, using Stat() to decided whether to skip Read()ing from |file_system|, | 16 smart, using Stat() to decided whether to skip Read()ing from |file_system|, |
| 17 and only Stat()ing directories never files. | 17 and only Stat()ing directories never files. |
| 18 ''' | 18 ''' |
| 19 def __init__(self, file_system, object_store_creator): | 19 def __init__(self, file_system, object_store_creator): |
| 20 self._file_system = file_system | 20 self._file_system = file_system |
| (...skipping 15 matching lines...) Expand all Loading... |
| 36 def Stat(self, path): | 36 def Stat(self, path): |
| 37 return self.StatAsync(path).Get() | 37 return self.StatAsync(path).Get() |
| 38 | 38 |
| 39 def StatAsync(self, path): | 39 def StatAsync(self, path): |
| 40 '''Stats the directory given, or if a file is given, stats the file's parent | 40 '''Stats the directory given, or if a file is given, stats the file's parent |
| 41 directory to get info about the file. | 41 directory to get info about the file. |
| 42 ''' | 42 ''' |
| 43 # Always stat the parent directory, since it will have the stat of the child | 43 # Always stat the parent directory, since it will have the stat of the child |
| 44 # anyway, and this gives us an entire directory's stat info at once. | 44 # anyway, and this gives us an entire directory's stat info at once. |
| 45 dir_path, file_path = posixpath.split(path) | 45 dir_path, file_path = posixpath.split(path) |
| 46 if dir_path and not dir_path.endswith('/'): | 46 dir_path = ToDirectory(dir_path) |
| 47 dir_path += '/' | |
| 48 | 47 |
| 49 def make_stat_info(dir_stat): | 48 def make_stat_info(dir_stat): |
| 50 '''Converts a dir stat into the correct resulting StatInfo; if the Stat | 49 '''Converts a dir stat into the correct resulting StatInfo; if the Stat |
| 51 was for a file, the StatInfo should just contain that file. | 50 was for a file, the StatInfo should just contain that file. |
| 52 ''' | 51 ''' |
| 53 if path == dir_path: | 52 if path == dir_path: |
| 54 return dir_stat | 53 return dir_stat |
| 55 # Was a file stat. Extract that file. | 54 # Was a file stat. Extract that file. |
| 56 file_version = dir_stat.child_versions.get(file_path) | 55 file_version = dir_stat.child_versions.get(file_path) |
| 57 if file_version is None: | 56 if file_version is None: |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 return new_results | 125 return new_results |
| 127 # Read in the values that were uncached or old. | 126 # Read in the values that were uncached or old. |
| 128 return self._file_system.Read(set(paths) - set(fresh_data.iterkeys()), | 127 return self._file_system.Read(set(paths) - set(fresh_data.iterkeys()), |
| 129 skip_not_found=skip_not_found).Then(next) | 128 skip_not_found=skip_not_found).Then(next) |
| 130 | 129 |
| 131 def GetIdentity(self): | 130 def GetIdentity(self): |
| 132 return self._file_system.GetIdentity() | 131 return self._file_system.GetIdentity() |
| 133 | 132 |
| 134 def __repr__(self): | 133 def __repr__(self): |
| 135 return '%s of <%s>' % (type(self).__name__, repr(self._file_system)) | 134 return '%s of <%s>' % (type(self).__name__, repr(self._file_system)) |
| OLD | NEW |