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 """ | 15 """ |
16 def __init__(self, file_system, object_store_creator_factory): | 16 def __init__(self, file_system, object_store_creator): |
17 self._file_system = file_system | 17 self._file_system = file_system |
18 self._object_store_creator_factory = object_store_creator_factory | 18 self._object_store_creator = object_store_creator |
19 | 19 |
20 def Create(self, populate_function, cls, category=None): | 20 def Create(self, populate_function, cls, category=None): |
21 """Create a CompiledFileSystem that populates the cache by calling | 21 """Create a CompiledFileSystem that populates the cache by calling |
22 |populate_function| with (path, data), where |data| is the data that was | 22 |populate_function| with (path, data), where |data| is the data that was |
23 fetched from |path|. | 23 fetched from |path|. |
24 The namespace for the file system is derived like ObjectStoreCreator: from | 24 The namespace for the file system is derived like ObjectStoreCreator: from |
25 |cls| along with an optional |category|. | 25 |cls| along with an optional |category|. |
26 """ | 26 """ |
27 assert isinstance(cls, type) | 27 assert isinstance(cls, type) |
28 assert not cls.__name__[0].islower() # guard against non-class types | 28 assert not cls.__name__[0].islower() # guard against non-class types |
29 full_name = cls.__name__ | 29 full_name = cls.__name__ |
30 if category is not None: | 30 if category is not None: |
31 full_name = '%s/%s' % (full_name, category) | 31 full_name = '%s/%s' % (full_name, category) |
32 object_store_creator = self._object_store_creator_factory.Create( | 32 def create_object_store(category): |
33 CompiledFileSystem) | 33 return self._object_store_creator.Create( |
34 return CompiledFileSystem( | 34 CompiledFileSystem, category='%s/%s' % (full_name, category)) |
35 self._file_system, | 35 return CompiledFileSystem(self._file_system, |
36 populate_function, | 36 populate_function, |
37 object_store_creator.Create(category='%s/file' % full_name), | 37 create_object_store('file'), |
38 object_store_creator.Create(category='%s/list' % full_name)) | 38 create_object_store('list')) |
39 | 39 |
40 def CreateIdentity(self, cls): | 40 def CreateIdentity(self, cls): |
41 '''Handy helper to get or create the identity compiled file system. | 41 '''Handy helper to get or create the identity compiled file system. |
42 GetFromFile will return the file's contents. | 42 GetFromFile will return the file's contents. |
43 GetFromFileListing will return the directory list. | 43 GetFromFileListing will return the directory list. |
44 ''' | 44 ''' |
45 return self.Create(lambda _, x: x, cls) | 45 return self.Create(lambda _, x: x, cls) |
46 | 46 |
47 def __init__(self, | 47 def __init__(self, |
48 file_system, | 48 file_system, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 """ | 86 """ |
87 if not path.endswith('/'): | 87 if not path.endswith('/'): |
88 path += '/' | 88 path += '/' |
89 version = self._file_system.Stat(path).version | 89 version = self._file_system.Stat(path).version |
90 cache_entry = self._list_object_store.Get(path).Get() | 90 cache_entry = self._list_object_store.Get(path).Get() |
91 if (cache_entry is not None) and (version == cache_entry.version): | 91 if (cache_entry is not None) and (version == cache_entry.version): |
92 return cache_entry._cache_data | 92 return cache_entry._cache_data |
93 cache_data = self._populate_function(path, self._RecursiveList(path)) | 93 cache_data = self._populate_function(path, self._RecursiveList(path)) |
94 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) | 94 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) |
95 return cache_data | 95 return cache_data |
OLD | NEW |