| 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 class _CacheEntry(object): | 5 class _CacheEntry(object): |
| 6 def __init__(self, cache_data, version): | 6 def __init__(self, cache_data, version): |
| 7 self._cache_data = cache_data | 7 self._cache_data = cache_data |
| 8 self.version = version | 8 self.version = version |
| 9 | 9 |
| 10 class CompiledFileSystem(object): | 10 class CompiledFileSystem(object): |
| 11 """This class caches FileSystem data that has been processed. | 11 """This class caches FileSystem data that has been processed. |
| 12 """ | 12 """ |
| 13 class Factory(object): | 13 class Factory(object): |
| 14 """A class to build a CompiledFileSystem backed by |file_system|. | 14 """A class to build a CompiledFileSystem backed by |file_system|. |
| 15 Set an explicit |store_type| for tests. | 15 Set an explicit |store_type| for tests. |
| 16 """ | 16 """ |
| 17 def __init__(self, | 17 def __init__(self, |
| 18 file_system, | 18 file_system, |
| 19 object_store_creator_factory, | 19 object_store_creator_factory, |
| 20 store_type=None): | 20 store_type=None): |
| 21 self._file_system = file_system | 21 self._file_system = file_system |
| 22 self._object_store_creator_factory = object_store_creator_factory | 22 self._object_store_creator_factory = object_store_creator_factory |
| 23 self._store_type = store_type | 23 self._store_type = store_type |
| 24 self._identity_instance = None | 24 self._identity_instance = None |
| 25 | 25 |
| 26 def Create(self, populate_function, cls, category=None, version=None): | 26 def Create(self, populate_function, cls, category=None): |
| 27 """Create a CompiledFileSystem that populates the cache by calling | 27 """Create a CompiledFileSystem that populates the cache by calling |
| 28 |populate_function| with (path, data), where |data| is the data that was | 28 |populate_function| with (path, data), where |data| is the data that was |
| 29 fetched from |path|. | 29 fetched from |path|. |
| 30 The namespace for the file system is derived like ObjectStoreCreator: from | 30 The namespace for the file system is derived like ObjectStoreCreator: from |
| 31 |cls| along with an optional |category| and |version|. | 31 |cls| along with an optional |category|. |
| 32 """ | 32 """ |
| 33 assert isinstance(cls, type) | 33 assert isinstance(cls, type) |
| 34 assert not cls.__name__[0].islower() # guard against non-class types | 34 assert not cls.__name__[0].islower() # guard against non-class types |
| 35 full_name = cls.__name__ | 35 full_name = cls.__name__ |
| 36 if category is not None: | 36 if category is not None: |
| 37 full_name = '%s/%s' % (full_name, category) | 37 full_name = '%s/%s' % (full_name, category) |
| 38 # TODO(kalman): Fix ServerInstance.CreateForTest to not give an empty FS. | 38 # TODO(kalman): Fix ServerInstance.CreateForTest to not give an empty FS. |
| 39 if self._file_system and self._file_system.GetVersion() is not None: | 39 return self._Create(populate_function, full_name) |
| 40 version = (self._file_system.GetVersion() + | |
| 41 (version if version is not None else 0)) | |
| 42 return self._Create(populate_function, full_name, version=version) | |
| 43 | 40 |
| 44 def GetOrCreateIdentity(self): | 41 def GetOrCreateIdentity(self): |
| 45 '''Handy helper to get or create the identity compiled file system. | 42 '''Handy helper to get or create the identity compiled file system. |
| 46 GetFromFile will return the file's contents. | 43 GetFromFile will return the file's contents. |
| 47 GetFromFileListing will return the directory list. | 44 GetFromFileListing will return the directory list. |
| 48 ''' | 45 ''' |
| 49 if self._identity_instance is None: | 46 if self._identity_instance is None: |
| 50 self._identity_instance = self._Create(lambda _, x: x, 'id') | 47 self._identity_instance = self._Create(lambda _, x: x, 'id') |
| 51 return self._identity_instance | 48 return self._identity_instance |
| 52 | 49 |
| 53 def _Create(self, populate_function, full_name, version=None): | 50 def _Create(self, populate_function, full_name): |
| 54 object_store_creator = self._object_store_creator_factory.Create( | 51 object_store_creator = self._object_store_creator_factory.Create( |
| 55 CompiledFileSystem, | 52 CompiledFileSystem, |
| 56 store_type=self._store_type) | 53 store_type=self._store_type) |
| 57 return CompiledFileSystem( | 54 return CompiledFileSystem( |
| 58 self._file_system, | 55 self._file_system, |
| 59 populate_function, | 56 populate_function, |
| 60 object_store_creator.Create(category='%s/file' % full_name, | 57 object_store_creator.Create(category='%s/file' % full_name), |
| 61 version=version), | 58 object_store_creator.Create(category='%s/list' % full_name)) |
| 62 object_store_creator.Create(category='%s/list' % full_name, | |
| 63 version=version)) | |
| 64 | 59 |
| 65 def __init__(self, | 60 def __init__(self, |
| 66 file_system, | 61 file_system, |
| 67 populate_function, | 62 populate_function, |
| 68 file_object_store, | 63 file_object_store, |
| 69 list_object_store): | 64 list_object_store): |
| 70 self._file_system = file_system | 65 self._file_system = file_system |
| 71 self._populate_function = populate_function | 66 self._populate_function = populate_function |
| 72 self._file_object_store = file_object_store | 67 self._file_object_store = file_object_store |
| 73 self._list_object_store = list_object_store | 68 self._list_object_store = list_object_store |
| (...skipping 30 matching lines...) Expand all Loading... |
| 104 """ | 99 """ |
| 105 if not path.endswith('/'): | 100 if not path.endswith('/'): |
| 106 path += '/' | 101 path += '/' |
| 107 version = self._file_system.Stat(path).version | 102 version = self._file_system.Stat(path).version |
| 108 cache_entry = self._list_object_store.Get(path).Get() | 103 cache_entry = self._list_object_store.Get(path).Get() |
| 109 if (cache_entry is not None) and (version == cache_entry.version): | 104 if (cache_entry is not None) and (version == cache_entry.version): |
| 110 return cache_entry._cache_data | 105 return cache_entry._cache_data |
| 111 cache_data = self._populate_function(path, self._RecursiveList(path)) | 106 cache_data = self._populate_function(path, self._RecursiveList(path)) |
| 112 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) | 107 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) |
| 113 return cache_data | 108 return cache_data |
| OLD | NEW |