| 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 os | 5 import os |
| 6 | 6 |
| 7 import object_store |
| 8 |
| 9 APPS = 'Apps' |
| 10 CRON = 'Cron' |
| 11 EXTENSIONS = 'Extensions' |
| 12 HANDLEBAR = 'Handlebar' |
| 13 IDL = 'IDL' |
| 14 INTRO = 'Intro' |
| 15 JSON = 'JSON' |
| 16 LIST = 'List' |
| 17 PERMS = 'Perms' |
| 18 RENDER = 'Render' |
| 19 STATIC = 'Static' |
| 20 ZIP = 'Zip' |
| 21 |
| 22 class _CacheEntry(object): |
| 23 def __init__(self, cache_data, version): |
| 24 self._cache_data = cache_data |
| 25 self.version = version |
| 26 |
| 7 class FileSystemCache(object): | 27 class FileSystemCache(object): |
| 8 """This class caches FileSystem data that has been processed. | 28 """This class caches FileSystem data that has been processed. |
| 9 """ | 29 """ |
| 10 class Builder(object): | 30 class Builder(object): |
| 11 """A class to build a FileSystemCache. | 31 """A class to build a FileSystemCache. |
| 12 """ | 32 """ |
| 13 def __init__(self, file_system): | 33 def __init__(self, file_system, object_store): |
| 14 self._file_system = file_system | 34 self._file_system = file_system |
| 35 self._object_store = object_store |
| 15 | 36 |
| 16 def build(self, populate_function): | 37 def build(self, populate_function, namespace): |
| 17 return FileSystemCache(self._file_system, populate_function) | 38 return FileSystemCache(self._file_system, |
| 39 populate_function, |
| 40 self._object_store, |
| 41 namespace) |
| 18 | 42 |
| 19 class _CacheEntry(object): | 43 def __init__(self, file_system, populate_function, object_store, namespace): |
| 20 def __init__(self, cache_data, version): | |
| 21 self._cache_data = cache_data | |
| 22 self.version = version | |
| 23 | |
| 24 def __init__(self, file_system, populate_function): | |
| 25 self._file_system = file_system | 44 self._file_system = file_system |
| 26 self._populate_function = populate_function | 45 self._populate_function = populate_function |
| 27 self._cache = {} | 46 self._object_store = object_store |
| 47 self._namespace = 'FileSystemCache.' + namespace |
| 48 |
| 49 def _MakeKey(self, key): |
| 50 return self._namespace + '.' + key |
| 28 | 51 |
| 29 def _RecursiveList(self, files): | 52 def _RecursiveList(self, files): |
| 30 all_files = files[:] | 53 all_files = files[:] |
| 31 dirs = {} | 54 dirs = {} |
| 32 for filename in files: | 55 for filename in files: |
| 33 if filename.endswith('/'): | 56 if filename.endswith('/'): |
| 34 all_files.remove(filename) | 57 all_files.remove(filename) |
| 35 dirs.update(self._file_system.Read([filename]).Get()) | 58 dirs.update(self._file_system.Read([filename]).Get()) |
| 36 for dir_, files in dirs.iteritems(): | 59 for dir_, files in dirs.iteritems(): |
| 37 all_files.extend(self._RecursiveList([dir_ + f for f in files])) | 60 all_files.extend(self._RecursiveList([dir_ + f for f in files])) |
| 38 return all_files | 61 return all_files |
| 39 | 62 |
| 40 def GetFromFile(self, path): | 63 def GetFromFile(self, path): |
| 41 """Calls |populate_function| on the contents of the file at |path|. | 64 """Calls |populate_function| on the contents of the file at |path|. |
| 42 """ | 65 """ |
| 43 version = self._file_system.Stat(path).version | 66 version = self._file_system.Stat(path).version |
| 44 if path in self._cache: | 67 cache_entry = self._object_store.Get(self._MakeKey(path), |
| 45 if version > self._cache[path].version: | 68 object_store.FILE_SYSTEM_CACHE, |
| 46 self._cache.pop(path) | 69 time=0).Get() |
| 47 else: | 70 |
| 48 return self._cache[path]._cache_data | 71 if (cache_entry is not None) and (version == cache_entry.version): |
| 49 cache_data = self._file_system.ReadSingle(path) | 72 return cache_entry._cache_data |
| 50 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 73 cache_data = self._populate_function(self._file_system.ReadSingle(path)) |
| 51 version) | 74 self._object_store.Set(self._MakeKey(path), |
| 52 return self._cache[path]._cache_data | 75 _CacheEntry(cache_data, version), |
| 76 object_store.FILE_SYSTEM_CACHE, |
| 77 time=0) |
| 78 return cache_data |
| 53 | 79 |
| 54 def GetFromFileListing(self, path): | 80 def GetFromFileListing(self, path): |
| 55 """Calls |populate_function| on the listing of the files at |path|. | 81 """Calls |populate_function| on the listing of the files at |path|. |
| 56 Assumes that the path given is to a directory. | 82 Assumes that the path given is to a directory. |
| 57 """ | 83 """ |
| 58 if not path.endswith('/'): | 84 if not path.endswith('/'): |
| 59 path += '/' | 85 path += '/' |
| 60 version = self._file_system.Stat(path).version | 86 version = self._file_system.Stat(path).version |
| 61 if path in self._cache: | 87 cache_entry = self._object_store.Get( |
| 62 if version > self._cache[path].version: | 88 self._MakeKey(path), |
| 63 self._cache.pop(path) | 89 object_store.FILE_SYSTEM_CACHE_LISTING, |
| 64 else: | 90 time=0).Get() |
| 65 return self._cache[path]._cache_data | 91 if (cache_entry is not None) and (version == cache_entry.version): |
| 66 cache_data = self._RecursiveList( | 92 return cache_entry._cache_data |
| 67 [path + f for f in self._file_system.ReadSingle(path)]) | 93 cache_data = self._populate_function(self._RecursiveList( |
| 68 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 94 [path + f for f in self._file_system.ReadSingle(path)])) |
| 69 version) | 95 self._object_store.Set(self._MakeKey(path), |
| 70 return self._cache[path]._cache_data | 96 _CacheEntry(cache_data, version), |
| 97 object_store.FILE_SYSTEM_CACHE_LISTING, |
| 98 time=0) |
| 99 return cache_data |
| OLD | NEW |